5b4294a678c8c9be18249a5929933a8655f1d0f0
[binutils-gdb.git] / gdb / elfread.c
1 /* Read ELF (Executable and Linking Format) object files for GDB.
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /************************************************************************
22 * *
23 * NOTICE *
24 * *
25 * This file is still under construction. When it is complete, this *
26 * notice will be removed. Until then, direct any questions or changes *
27 * to Fred Fish at Cygnus Support (fnf@cygint) *
28 * *
29 * FIXME Still needs support for shared libraries. *
30 * FIXME Still needs support for core files. *
31 * FIXME The ".debug" and ".line" section names are hardwired. *
32 * FIXME Still needs support ELF symbol tables (as distinct *
33 * from DWARF support). Can use them to build the misc *
34 * function vector at least. This is fairly trivial once *
35 * bfd is extended to handle ELF symbol tables. *
36 * *
37 ************************************************************************/
38
39 #include <stdio.h>
40
41 #include "defs.h"
42 #include "elf-common.h"
43 #include "elf-external.h"
44 #include "elf-internal.h"
45 #include "bfd.h"
46 #include "symfile.h"
47 #include "symtab.h"
48 #include "ansidecl.h"
49
50 extern int EXFUN (strcmp, (CONST char *a, CONST char *b));
51 extern int EXFUN (dwarf_build_psymtabs,
52 (int desc, char *filename, CORE_ADDR addr, int mainline,
53 unsigned int dbfoff, unsigned int dbsize, unsigned int lnoffset,
54 unsigned int lnsize, struct objfile *objfile));
55
56 #define STREQ(a,b) (strcmp((a),(b))==0)
57
58 struct elfinfo {
59 unsigned int dboffset; /* Offset to dwarf debug section */
60 unsigned int dbsize; /* Size of dwarf debug section */
61 unsigned int lnoffset; /* Offset to dwarf line number section */
62 unsigned int lnsize; /* Size of dwarf line number section */
63 };
64
65 /* We are called once per section from elf_symfile_read. We
66 need to examine each section we are passed, check to see
67 if it is something we are interested in processing, and
68 if so, stash away some access information for the section.
69
70 For now we recognize the dwarf debug information sections and
71 line number sections from matching their section names. The
72 ELF definition is no real help here since it has no direct
73 knowledge of DWARF (by design, so any debugging format can be
74 used).
75
76 FIXME: The section names should not be hardwired strings. */
77
78 static void
79 DEFUN(elf_locate_sections, (abfd, sectp, ei),
80 bfd *abfd AND
81 asection *sectp AND
82 struct elfinfo *ei)
83 {
84 if (STREQ (sectp -> name, ".debug"))
85 {
86 ei -> dboffset = sectp -> filepos;
87 ei -> dbsize = sectp -> size;
88 }
89 else if (STREQ (sectp -> name, ".line"))
90 {
91 ei -> lnoffset = sectp -> filepos;
92 ei -> lnsize = sectp -> size;
93 }
94 }
95
96 /*
97
98 LOCAL FUNCTION
99
100 record_misc_function -- add entry to miscellaneous function vector
101
102 SYNOPSIS
103
104 static void record_misc_function (char *name, CORE_ADDR address)
105
106 DESCRIPTION
107
108 Given a pointer to the name of a symbol that should be added to the
109 miscellaneous function vector, and the address associated with that
110 symbol, records this information for later use in building the
111 miscellaneous function vector.
112
113 NOTES
114
115 FIXME: For now we just use mf_unknown as the type. This should be
116 fixed.
117 */
118
119 static void
120 DEFUN(record_misc_function, (name, address), char *name AND CORE_ADDR address)
121 {
122 prim_record_misc_function (obsavestring (name, strlen (name)), address,
123 mf_unknown);
124 }
125
126 static void
127 DEFUN (elf_symtab_read, (abfd, addr),
128 bfd *abfd AND
129 CORE_ADDR addr)
130 {
131 unsigned int storage_needed;
132 asymbol *sym;
133 asymbol **symbol_table;
134 unsigned int number_of_symbols;
135 unsigned int i;
136 struct cleanup *back_to;
137
138 storage_needed = get_symtab_upper_bound (abfd);
139
140 if (storage_needed > 0)
141 {
142 symbol_table = (asymbol **) bfd_xmalloc (storage_needed);
143 back_to = make_cleanup (free, symbol_table);
144 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
145
146 for (i = 0; i < number_of_symbols; i++)
147 {
148 sym = *symbol_table++;
149 /* Select global symbols that are defined in a specific section
150 or are absolute. */
151 if (sym -> flags & BSF_GLOBAL
152 && ((sym -> section != NULL) || (sym -> flags & BSF_ABSOLUTE)))
153 {
154 record_misc_function ((char *) sym -> name, sym -> value);
155 }
156 }
157 do_cleanups (back_to);
158 }
159 }
160
161 /* Scan and build partial symbols for a symbol file.
162 We have been initialized by a call to elf_symfile_init, which
163 currently does nothing.
164
165 ADDR is the address relative to which the symbols in it are (e.g.
166 the base address of the text segment).
167
168 MAINLINE is true if we are reading the main symbol
169 table (as opposed to a shared lib or dynamically loaded file).
170
171 This function only does the minimum work necessary for letting the
172 user "name" things symbolically; it does not read the entire symtab.
173 Instead, it reads the external and static symbols and puts them in partial
174 symbol tables. When more extensive information is requested of a
175 file, the corresponding partial symbol table is mutated into a full
176 fledged symbol table by going back and reading the symbols
177 for real. The function dwarf_psymtab_to_symtab() is the function that
178 does this for DWARF symbols.
179
180 Note that ELF files have a "minimal" symbol table, which looks a lot
181 like a COFF symbol table, but has only the minimal information necessary
182 for linking. We process this also, and just use the information to
183 add to the misc function vector. This gives us some minimal debugging
184 capability even for files compiled without -g.
185 */
186
187 static void
188 DEFUN(elf_symfile_read, (sf, addr, mainline),
189 struct sym_fns *sf AND
190 CORE_ADDR addr AND
191 int mainline)
192 {
193 bfd *abfd = sf->objfile->obfd;
194 struct elfinfo ei;
195 struct cleanup *back_to;
196
197 init_misc_bunches ();
198 back_to = make_cleanup (discard_misc_bunches, 0);
199
200 /* Process the normal ELF symbol table first. */
201
202 elf_symtab_read (abfd, addr);
203
204 /* Now process the DWARF debugging information, which is contained in
205 special ELF sections. We first have to find them... */
206
207 (void) memset ((char *) &ei, 0, sizeof (ei));
208 bfd_map_over_sections (abfd, elf_locate_sections, &ei);
209 if (ei.dboffset && ei.lnoffset)
210 {
211 addr = 0; /* FIXME: force address base to zero for now */
212 dwarf_build_psymtabs (fileno ((FILE *)(abfd -> iostream)),
213 bfd_get_filename (abfd),
214 addr, mainline,
215 ei.dboffset, ei.dbsize,
216 ei.lnoffset, ei.lnsize, sf->objfile);
217 }
218
219 if (!partial_symtab_list)
220 {
221 wrap_here ("");
222 printf_filtered ("(no debugging symbols found)...");
223 wrap_here ("");
224 }
225
226 /* Go over the miscellaneous functions and install them in the
227 miscellaneous function vector. */
228
229 condense_misc_bunches (!mainline);
230 do_cleanups (back_to);
231 }
232
233 /* Initialize anything that needs initializing when a completely new symbol
234 file is specified (not just adding some symbols from another file, e.g. a
235 shared library).
236
237 For now at least, we have nothing in particular to do, so this function is
238 just a stub. */
239
240 static void
241 DEFUN_VOID (elf_new_init)
242 {
243 }
244
245 /* ELF specific initialization routine for reading symbols.
246
247 It is passed a pointer to a struct sym_fns which contains, among other
248 things, the BFD for the file whose symbols are being read, and a slot for
249 a pointer to "private data" which we can fill with goodies.
250
251 For now at least, we have nothing in particular to do, so this function is
252 just a stub. */
253
254 static void
255 DEFUN(elf_symfile_init, (sf),
256 struct sym_fns *sf)
257 {
258 }
259
260 \f
261 /* Register that we are able to handle ELF object file formats and DWARF
262 debugging formats.
263
264 Unlike other object file formats, where the debugging information format
265 is implied by the object file format, the ELF object file format and the
266 DWARF debugging information format are two distinct, and potentially
267 separate entities. I.E. it is perfectly possible to have ELF objects
268 with debugging formats other than DWARF. And it is conceivable that the
269 DWARF debugging format might be used with another object file format,
270 like COFF, by simply using COFF's custom section feature.
271
272 GDB, and to a lesser extent BFD, should support the notion of separate
273 object file formats and debugging information formats. For now, we just
274 use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
275 object file format and the DWARF debugging format. */
276
277 static struct sym_fns elf_sym_fns = {
278 "elf", /* sym_name: name or name prefix of BFD target type */
279 3, /* sym_namelen: number of significant sym_name chars */
280 elf_new_init, /* sym_new_init: init anything gbl to entire symtab */
281 elf_symfile_init, /* sym_init: read initial info, setup for sym_read() */
282 elf_symfile_read, /* sym_read: read a symbol file into symtab */
283 NULL, /* sym_bfd: accessor for symbol file being read */
284 NULL, /* sym_private: sym_init & sym_read shared info */
285 NULL /* next: pointer to next struct sym_fns */
286 };
287
288 void
289 DEFUN_VOID (_initialize_elfread)
290 {
291 add_symtab_fns (&elf_sym_fns);
292 }