]>
Commit | Line | Data |
---|---|---|
35f5886e | 1 | /* Read ELF (Executable and Linking Format) object files for GDB. |
c2e4669f | 2 | Copyright 1991, 1992 Free Software Foundation, Inc. |
35f5886e FF |
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 * | |
c2e4669f | 27 | * to Fred Fish at Cygnus Support ([email protected]) * |
35f5886e FF |
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. * | |
35f5886e FF |
32 | * * |
33 | ************************************************************************/ | |
34 | ||
35f5886e | 35 | #include "defs.h" |
f5f0679a SC |
36 | #include "elf/common.h" |
37 | #include "elf/external.h" | |
38 | #include "elf/internal.h" | |
35f5886e | 39 | #include "bfd.h" |
35f5886e | 40 | #include "symtab.h" |
1ab3bf1b | 41 | #include "symfile.h" |
5e2e79f8 | 42 | #include "objfiles.h" |
a048c8f5 | 43 | |
35f5886e FF |
44 | #define STREQ(a,b) (strcmp((a),(b))==0) |
45 | ||
46 | struct elfinfo { | |
47 | unsigned int dboffset; /* Offset to dwarf debug section */ | |
48 | unsigned int dbsize; /* Size of dwarf debug section */ | |
49 | unsigned int lnoffset; /* Offset to dwarf line number section */ | |
50 | unsigned int lnsize; /* Size of dwarf line number section */ | |
51 | }; | |
52 | ||
1ab3bf1b | 53 | static void |
80d68b1d | 54 | elf_symfile_init PARAMS ((struct objfile *)); |
1ab3bf1b JG |
55 | |
56 | static void | |
80d68b1d | 57 | elf_new_init PARAMS ((struct objfile *)); |
1ab3bf1b JG |
58 | |
59 | static void | |
80d68b1d FF |
60 | elf_symfile_read PARAMS ((struct objfile *, CORE_ADDR, int)); |
61 | ||
62 | static void | |
63 | elf_symfile_finish PARAMS ((struct objfile *)); | |
1ab3bf1b JG |
64 | |
65 | static void | |
66 | elf_symtab_read PARAMS ((bfd *, CORE_ADDR, int, struct objfile *)); | |
67 | ||
68 | static void | |
69 | record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type, | |
70 | struct objfile *)); | |
71 | ||
72 | static void | |
73 | elf_locate_sections PARAMS ((bfd *, asection *, PTR)); | |
74 | ||
35f5886e FF |
75 | /* We are called once per section from elf_symfile_read. We |
76 | need to examine each section we are passed, check to see | |
77 | if it is something we are interested in processing, and | |
78 | if so, stash away some access information for the section. | |
79 | ||
80 | For now we recognize the dwarf debug information sections and | |
81 | line number sections from matching their section names. The | |
82 | ELF definition is no real help here since it has no direct | |
83 | knowledge of DWARF (by design, so any debugging format can be | |
84 | used). | |
85 | ||
86 | FIXME: The section names should not be hardwired strings. */ | |
87 | ||
88 | static void | |
1ab3bf1b JG |
89 | elf_locate_sections (abfd, sectp, eip) |
90 | bfd *abfd; | |
91 | asection *sectp; | |
92 | PTR eip; | |
35f5886e | 93 | { |
1ab3bf1b JG |
94 | register struct elfinfo *ei; |
95 | ||
96 | ei = (struct elfinfo *) eip; | |
35f5886e FF |
97 | if (STREQ (sectp -> name, ".debug")) |
98 | { | |
99 | ei -> dboffset = sectp -> filepos; | |
e5849334 | 100 | ei -> dbsize = bfd_get_section_size_before_reloc (sectp); |
35f5886e FF |
101 | } |
102 | else if (STREQ (sectp -> name, ".line")) | |
103 | { | |
104 | ei -> lnoffset = sectp -> filepos; | |
e5849334 | 105 | ei -> lnsize = bfd_get_section_size_before_reloc (sectp); |
35f5886e FF |
106 | } |
107 | } | |
108 | ||
1ab3bf1b JG |
109 | #if 0 /* Currently unused */ |
110 | ||
f8b76e70 | 111 | char * |
1ab3bf1b JG |
112 | elf_interpreter (abfd) |
113 | bfd *abfd; | |
f8b76e70 FF |
114 | { |
115 | sec_ptr interp_sec; | |
116 | unsigned size; | |
117 | char *interp = NULL; | |
118 | ||
119 | interp_sec = bfd_get_section_by_name (abfd, ".interp"); | |
120 | if (interp_sec) | |
121 | { | |
122 | size = bfd_section_size (abfd, interp_sec); | |
123 | interp = alloca (size); | |
124 | if (bfd_get_section_contents (abfd, interp_sec, interp, (file_ptr)0, | |
125 | size)) | |
126 | { | |
127 | interp = savestring (interp, size - 1); | |
128 | } | |
129 | else | |
130 | { | |
131 | interp = NULL; | |
132 | } | |
133 | } | |
134 | return (interp); | |
135 | } | |
136 | ||
1ab3bf1b JG |
137 | #endif |
138 | ||
a7446af6 FF |
139 | /* |
140 | ||
141 | LOCAL FUNCTION | |
142 | ||
1ab3bf1b | 143 | record_minimal_symbol -- add entry to minimal symbol table |
a7446af6 FF |
144 | |
145 | SYNOPSIS | |
146 | ||
1ab3bf1b | 147 | static void record_minimal_symbol (char *name, CORE_ADDR address) |
a7446af6 FF |
148 | |
149 | DESCRIPTION | |
150 | ||
151 | Given a pointer to the name of a symbol that should be added to the | |
1ab3bf1b JG |
152 | minimal symbol table and the address associated with that symbol, records |
153 | this information for later use in building the minimal symbol table. | |
a7446af6 | 154 | |
a7446af6 FF |
155 | */ |
156 | ||
157 | static void | |
1ab3bf1b JG |
158 | record_minimal_symbol (name, address, ms_type, objfile) |
159 | char *name; | |
160 | CORE_ADDR address; | |
161 | enum minimal_symbol_type ms_type; | |
162 | struct objfile *objfile; | |
a7446af6 | 163 | { |
1ab3bf1b JG |
164 | name = obsavestring (name, strlen (name), &objfile -> symbol_obstack); |
165 | prim_record_minimal_symbol (name, address, ms_type); | |
a7446af6 FF |
166 | } |
167 | ||
f8b76e70 FF |
168 | /* |
169 | ||
170 | LOCAL FUNCTION | |
171 | ||
172 | elf_symtab_read -- read the symbol table of an ELF file | |
173 | ||
174 | SYNOPSIS | |
175 | ||
1ab3bf1b JG |
176 | void elf_symtab_read (bfd *abfd, CORE_ADDR addr, int mainline, |
177 | struct objfile *objfile) | |
f8b76e70 FF |
178 | |
179 | DESCRIPTION | |
180 | ||
181 | Given an open bfd, a base address to relocate symbols to, and a | |
182 | flag that specifies whether or not this bfd is for an executable | |
183 | or not (may be shared library for example), add all the global | |
1ab3bf1b | 184 | function and data symbols to the minimal symbol table. |
f8b76e70 FF |
185 | |
186 | */ | |
187 | ||
a7446af6 | 188 | static void |
1ab3bf1b JG |
189 | elf_symtab_read (abfd, addr, mainline, objfile) |
190 | bfd *abfd; | |
191 | CORE_ADDR addr; | |
192 | int mainline; | |
193 | struct objfile *objfile; | |
a7446af6 FF |
194 | { |
195 | unsigned int storage_needed; | |
196 | asymbol *sym; | |
197 | asymbol **symbol_table; | |
198 | unsigned int number_of_symbols; | |
199 | unsigned int i; | |
200 | struct cleanup *back_to; | |
f8b76e70 | 201 | CORE_ADDR symaddr; |
1ab3bf1b | 202 | enum minimal_symbol_type ms_type; |
a7446af6 FF |
203 | |
204 | storage_needed = get_symtab_upper_bound (abfd); | |
205 | ||
206 | if (storage_needed > 0) | |
207 | { | |
208 | symbol_table = (asymbol **) bfd_xmalloc (storage_needed); | |
209 | back_to = make_cleanup (free, symbol_table); | |
210 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); | |
211 | ||
212 | for (i = 0; i < number_of_symbols; i++) | |
213 | { | |
214 | sym = *symbol_table++; | |
d35bf52d FF |
215 | /* Select global/weak symbols that are defined in a specific section. |
216 | Note that bfd now puts abs symbols in their own section, so | |
217 | all symbols we are interested in will have a section. */ | |
218 | if ((sym -> flags & (BSF_GLOBAL | BSF_WEAK)) | |
219 | && (sym -> section != NULL)) | |
a7446af6 | 220 | { |
f8b76e70 | 221 | symaddr = sym -> value; |
c2e4669f JG |
222 | /* Relocate all non-absolute symbols by base address. */ |
223 | if (sym -> section != &bfd_abs_section) | |
f8b76e70 | 224 | { |
f8b76e70 FF |
225 | symaddr += addr; |
226 | } | |
227 | /* For non-absolute symbols, use the type of the section | |
228 | they are relative to, to intuit text/data. Bfd provides | |
229 | no way of figuring this out for absolute symbols. */ | |
d35bf52d | 230 | if (sym -> section -> flags & SEC_CODE) |
f8b76e70 | 231 | { |
1ab3bf1b | 232 | ms_type = mst_text; |
f8b76e70 | 233 | } |
d35bf52d | 234 | else if (sym -> section -> flags & SEC_DATA) |
f8b76e70 | 235 | { |
1ab3bf1b | 236 | ms_type = mst_data; |
f8b76e70 FF |
237 | } |
238 | else | |
239 | { | |
1ab3bf1b | 240 | ms_type = mst_unknown; |
f8b76e70 | 241 | } |
1ab3bf1b | 242 | record_minimal_symbol ((char *) sym -> name, symaddr, ms_type, objfile); |
a7446af6 FF |
243 | } |
244 | } | |
245 | do_cleanups (back_to); | |
246 | } | |
247 | } | |
248 | ||
35f5886e FF |
249 | /* Scan and build partial symbols for a symbol file. |
250 | We have been initialized by a call to elf_symfile_init, which | |
251 | currently does nothing. | |
252 | ||
253 | ADDR is the address relative to which the symbols in it are (e.g. | |
254 | the base address of the text segment). | |
255 | ||
256 | MAINLINE is true if we are reading the main symbol | |
257 | table (as opposed to a shared lib or dynamically loaded file). | |
258 | ||
259 | This function only does the minimum work necessary for letting the | |
260 | user "name" things symbolically; it does not read the entire symtab. | |
261 | Instead, it reads the external and static symbols and puts them in partial | |
262 | symbol tables. When more extensive information is requested of a | |
263 | file, the corresponding partial symbol table is mutated into a full | |
264 | fledged symbol table by going back and reading the symbols | |
265 | for real. The function dwarf_psymtab_to_symtab() is the function that | |
a7446af6 FF |
266 | does this for DWARF symbols. |
267 | ||
268 | Note that ELF files have a "minimal" symbol table, which looks a lot | |
269 | like a COFF symbol table, but has only the minimal information necessary | |
270 | for linking. We process this also, and just use the information to | |
1ab3bf1b | 271 | add to gdb's minimal symbol table. This gives us some minimal debugging |
a7446af6 FF |
272 | capability even for files compiled without -g. |
273 | */ | |
35f5886e FF |
274 | |
275 | static void | |
80d68b1d FF |
276 | elf_symfile_read (objfile, addr, mainline) |
277 | struct objfile *objfile; | |
1ab3bf1b JG |
278 | CORE_ADDR addr; |
279 | int mainline; | |
35f5886e | 280 | { |
80d68b1d | 281 | bfd *abfd = objfile->obfd; |
35f5886e | 282 | struct elfinfo ei; |
a7446af6 | 283 | struct cleanup *back_to; |
35f5886e | 284 | |
1ab3bf1b JG |
285 | init_minimal_symbol_collection (); |
286 | back_to = make_cleanup (discard_minimal_symbols, 0); | |
a7446af6 FF |
287 | |
288 | /* Process the normal ELF symbol table first. */ | |
289 | ||
80d68b1d | 290 | elf_symtab_read (abfd, addr, mainline, objfile); |
a7446af6 FF |
291 | |
292 | /* Now process the DWARF debugging information, which is contained in | |
293 | special ELF sections. We first have to find them... */ | |
294 | ||
295 | (void) memset ((char *) &ei, 0, sizeof (ei)); | |
1ab3bf1b | 296 | bfd_map_over_sections (abfd, elf_locate_sections, (PTR) &ei); |
35f5886e FF |
297 | if (ei.dboffset && ei.lnoffset) |
298 | { | |
35f5886e FF |
299 | dwarf_build_psymtabs (fileno ((FILE *)(abfd -> iostream)), |
300 | bfd_get_filename (abfd), | |
301 | addr, mainline, | |
302 | ei.dboffset, ei.dbsize, | |
80d68b1d | 303 | ei.lnoffset, ei.lnsize, objfile); |
35f5886e | 304 | } |
a7446af6 | 305 | |
1ab3bf1b | 306 | if (!have_partial_symbols ()) |
35f5886e FF |
307 | { |
308 | wrap_here (""); | |
309 | printf_filtered ("(no debugging symbols found)..."); | |
310 | wrap_here (""); | |
311 | } | |
a7446af6 | 312 | |
1ab3bf1b JG |
313 | /* Install any minimal symbols that have been collected as the current |
314 | minimal symbols for this objfile. */ | |
315 | ||
80d68b1d | 316 | install_minimal_symbols (objfile); |
1ab3bf1b | 317 | |
a7446af6 | 318 | do_cleanups (back_to); |
35f5886e FF |
319 | } |
320 | ||
321 | /* Initialize anything that needs initializing when a completely new symbol | |
322 | file is specified (not just adding some symbols from another file, e.g. a | |
323 | shared library). | |
324 | ||
325 | For now at least, we have nothing in particular to do, so this function is | |
326 | just a stub. */ | |
327 | ||
328 | static void | |
80d68b1d FF |
329 | elf_new_init (objfile) |
330 | struct objfile *objfile; | |
331 | { | |
332 | buildsym_new_init (); | |
333 | } | |
334 | ||
335 | /* Perform any local cleanups required when we are done with a particular | |
336 | objfile. I.E, we are in the process of discarding all symbol information | |
337 | for an objfile, freeing up all memory held for it, and unlinking the | |
338 | objfile struct from the global list of known objfiles. */ | |
339 | ||
340 | static void | |
341 | elf_symfile_finish (objfile) | |
342 | struct objfile *objfile; | |
35f5886e | 343 | { |
80d68b1d FF |
344 | if (objfile -> sym_private != NULL) |
345 | { | |
346 | mfree (objfile -> md, objfile -> sym_private); | |
347 | } | |
35f5886e FF |
348 | } |
349 | ||
350 | /* ELF specific initialization routine for reading symbols. | |
351 | ||
352 | It is passed a pointer to a struct sym_fns which contains, among other | |
353 | things, the BFD for the file whose symbols are being read, and a slot for | |
354 | a pointer to "private data" which we can fill with goodies. | |
355 | ||
356 | For now at least, we have nothing in particular to do, so this function is | |
357 | just a stub. */ | |
358 | ||
359 | static void | |
80d68b1d FF |
360 | elf_symfile_init (objfile) |
361 | struct objfile *objfile; | |
35f5886e FF |
362 | { |
363 | } | |
364 | ||
365 | \f | |
366 | /* Register that we are able to handle ELF object file formats and DWARF | |
367 | debugging formats. | |
368 | ||
369 | Unlike other object file formats, where the debugging information format | |
370 | is implied by the object file format, the ELF object file format and the | |
371 | DWARF debugging information format are two distinct, and potentially | |
372 | separate entities. I.E. it is perfectly possible to have ELF objects | |
373 | with debugging formats other than DWARF. And it is conceivable that the | |
374 | DWARF debugging format might be used with another object file format, | |
375 | like COFF, by simply using COFF's custom section feature. | |
376 | ||
377 | GDB, and to a lesser extent BFD, should support the notion of separate | |
378 | object file formats and debugging information formats. For now, we just | |
379 | use "elf" in the same sense as "a.out" or "coff", to imply both the ELF | |
380 | object file format and the DWARF debugging format. */ | |
381 | ||
80d68b1d FF |
382 | static struct sym_fns elf_sym_fns = |
383 | { | |
35f5886e FF |
384 | "elf", /* sym_name: name or name prefix of BFD target type */ |
385 | 3, /* sym_namelen: number of significant sym_name chars */ | |
386 | elf_new_init, /* sym_new_init: init anything gbl to entire symtab */ | |
387 | elf_symfile_init, /* sym_init: read initial info, setup for sym_read() */ | |
388 | elf_symfile_read, /* sym_read: read a symbol file into symtab */ | |
80d68b1d | 389 | elf_symfile_finish, /* sym_finish: finished with file, cleanup */ |
35f5886e FF |
390 | NULL /* next: pointer to next struct sym_fns */ |
391 | }; | |
392 | ||
393 | void | |
1ab3bf1b | 394 | _initialize_elfread () |
35f5886e FF |
395 | { |
396 | add_symtab_fns (&elf_sym_fns); | |
397 | } |