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