1 /* Read ELF (Executable and Linking Format) object files for GDB.
2 Copyright 1991, 1992 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
5 This file is part of GDB.
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.
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.
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. */
23 #include <time.h> /* For time_t in libbfd.h. */
24 #include "libbfd.h" /* For bfd_elf_find_section */
30 #include "stabsread.h"
31 #include "gdb-stabs.h"
32 #include "complaints.h"
36 /* The struct elfinfo is available only during ELF symbol table and
37 psymtab reading. It is destroyed at the complation of psymtab-reading.
38 It's local to elf_symfile_read. */
41 file_ptr dboffset
; /* Offset to dwarf debug section */
42 unsigned int dbsize
; /* Size of dwarf debug section */
43 file_ptr lnoffset
; /* Offset to dwarf line number section */
44 unsigned int lnsize
; /* Size of dwarf line number section */
45 asection
*stabsect
; /* Section pointer for .stab section */
46 asection
*stabindexsect
; /* Section pointer for .stab.index section */
49 /* Various things we might complain about... */
51 struct complaint section_info_complaint
=
52 {"elf/stab section information %s without a preceding file symbol", 0, 0};
54 struct complaint section_info_dup_complaint
=
55 {"duplicated elf/stab section information for %s", 0, 0};
57 struct complaint stab_info_mismatch_complaint
=
58 {"elf/stab section information missing for %s", 0, 0};
60 struct complaint stab_info_questionable_complaint
=
61 {"elf/stab section information questionable for %s", 0, 0};
64 elf_symfile_init
PARAMS ((struct objfile
*));
67 elf_new_init
PARAMS ((struct objfile
*));
70 elf_symfile_read
PARAMS ((struct objfile
*, struct section_offsets
*, int));
73 elf_symfile_finish
PARAMS ((struct objfile
*));
76 elf_symtab_read
PARAMS ((bfd
*, CORE_ADDR
, struct objfile
*));
79 free_elfinfo
PARAMS ((PTR
));
81 static struct section_offsets
*
82 elf_symfile_offsets
PARAMS ((struct objfile
*, CORE_ADDR
));
85 record_minimal_symbol_and_info
PARAMS ((char *, CORE_ADDR
,
86 enum minimal_symbol_type
, char *,
90 elf_locate_sections
PARAMS ((bfd
*, asection
*, PTR
));
92 /* We are called once per section from elf_symfile_read. We
93 need to examine each section we are passed, check to see
94 if it is something we are interested in processing, and
95 if so, stash away some access information for the section.
97 For now we recognize the dwarf debug information sections and
98 line number sections from matching their section names. The
99 ELF definition is no real help here since it has no direct
100 knowledge of DWARF (by design, so any debugging format can be
103 We also recognize the ".stab" sections used by the Sun compilers
104 released with Solaris 2.
106 FIXME: The section names should not be hardwired strings. */
109 elf_locate_sections (ignore_abfd
, sectp
, eip
)
114 register struct elfinfo
*ei
;
116 ei
= (struct elfinfo
*) eip
;
117 if (STREQ (sectp
-> name
, ".debug"))
119 ei
-> dboffset
= sectp
-> filepos
;
120 ei
-> dbsize
= bfd_get_section_size_before_reloc (sectp
);
122 else if (STREQ (sectp
-> name
, ".line"))
124 ei
-> lnoffset
= sectp
-> filepos
;
125 ei
-> lnsize
= bfd_get_section_size_before_reloc (sectp
);
127 else if (STREQ (sectp
-> name
, ".stab"))
129 ei
-> stabsect
= sectp
;
131 else if (STREQ (sectp
-> name
, ".stab.index"))
133 ei
-> stabindexsect
= sectp
;
137 #if 0 /* Currently unused */
140 elf_interpreter (abfd
)
147 interp_sec
= bfd_get_section_by_name (abfd
, ".interp");
150 size
= bfd_section_size (abfd
, interp_sec
);
151 interp
= alloca (size
);
152 if (bfd_get_section_contents (abfd
, interp_sec
, interp
, (file_ptr
)0,
155 interp
= savestring (interp
, size
- 1);
168 record_minimal_symbol_and_info (name
, address
, ms_type
, info
, objfile
)
171 enum minimal_symbol_type ms_type
;
172 char *info
; /* FIXME, is this really char *? */
173 struct objfile
*objfile
;
175 name
= obsavestring (name
, strlen (name
), &objfile
-> symbol_obstack
);
176 prim_record_minimal_symbol_and_info (name
, address
, ms_type
, info
, -1);
183 elf_symtab_read -- read the symbol table of an ELF file
187 void elf_symtab_read (bfd *abfd, CORE_ADDR addr,
188 struct objfile *objfile)
192 Given an open bfd, a base address to relocate symbols to, and a
193 flag that specifies whether or not this bfd is for an executable
194 or not (may be shared library for example), add all the global
195 function and data symbols to the minimal symbol table.
197 In stabs-in-ELF, as implemented by Sun, there are some local symbols
198 defined in the ELF symbol table, which can be used to locate
199 the beginnings of sections from each ".o" file that was linked to
200 form the executable objfile. We gather any such info and record it
201 in data structures hung off the objfile's private data.
206 elf_symtab_read (abfd
, addr
, objfile
)
209 struct objfile
*objfile
;
211 unsigned int storage_needed
;
213 asymbol
**symbol_table
;
214 unsigned int number_of_symbols
;
217 struct cleanup
*back_to
;
219 enum minimal_symbol_type ms_type
;
220 /* If sectinfo is nonNULL, it contains section info that should end up
221 filed in the objfile. */
222 struct stab_section_info
*sectinfo
= NULL
;
223 /* If filesym is nonzero, it points to a file symbol, but we haven't
224 seen any section info for it yet. */
225 asymbol
*filesym
= 0;
226 struct dbx_symfile_info
*dbx
= (struct dbx_symfile_info
*)
227 objfile
->sym_private
;
230 storage_needed
= get_symtab_upper_bound (abfd
);
231 if (storage_needed
> 0)
233 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
234 back_to
= make_cleanup (free
, symbol_table
);
235 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
236 for (i
= 0; i
< number_of_symbols
; i
++)
238 sym
= symbol_table
[i
];
239 if (sym
-> name
== NULL
|| *sym
-> name
== '\0')
241 /* Skip names that don't exist (shouldn't happen), or names
242 that are null strings (may happen). */
245 if (sym
-> flags
& BSF_FILE
)
247 /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
248 Chain any old one onto the objfile; remember new sym. */
249 if (sectinfo
!= NULL
)
251 sectinfo
-> next
= dbx
-> stab_section_info
;
252 dbx
-> stab_section_info
= sectinfo
;
257 else if (sym
-> flags
& (BSF_GLOBAL
| BSF_LOCAL
| BSF_WEAK
))
259 /* Select global/local/weak symbols. Note that bfd puts abs
260 symbols in their own section, so all symbols we are
261 interested in will have a section. */
262 /* Bfd symbols are section relative. */
263 symaddr
= sym
-> value
+ sym
-> section
-> vma
;
264 /* Relocate all non-absolute symbols by base address. */
265 if (sym
-> section
!= &bfd_abs_section
)
269 /* For non-absolute symbols, use the type of the section
270 they are relative to, to intuit text/data. Bfd provides
271 no way of figuring this out for absolute symbols. */
272 if (sym
-> section
== &bfd_abs_section
)
276 else if (sym
-> section
-> flags
& SEC_CODE
)
278 if (sym
-> flags
& BSF_GLOBAL
)
282 else if (sym
->name
[0] == '.' && sym
->name
[1] == 'L')
283 /* Looks like a compiler-generated label. Skip it.
284 The assembler should be skipping these (to keep
285 executables small), but apparently with gcc on the
286 delta m88k SVR4, it loses. So to have us check too
287 should be harmless (but I encourage people to fix this
288 in the assembler instead of adding checks here). */
292 ms_type
= mst_file_text
;
295 else if (sym
-> section
-> flags
& SEC_DATA
)
297 if (sym
-> flags
& BSF_GLOBAL
)
299 if (sym
-> section
-> flags
& SEC_HAS_CONTENTS
)
308 else if (sym
-> flags
& BSF_LOCAL
)
310 /* Named Local variable in a Data section. Check its
311 name for stabs-in-elf. The STREQ macro checks the
312 first character inline, so we only actually do a
313 strcmp function call on names that start with 'B'
315 index
= SECT_OFF_MAX
;
316 if (STREQ ("Bbss.bss", sym
-> name
))
318 index
= SECT_OFF_BSS
;
320 else if (STREQ ("Ddata.data", sym
-> name
))
322 index
= SECT_OFF_DATA
;
324 else if (STREQ ("Drodata.rodata", sym
-> name
))
326 index
= SECT_OFF_RODATA
;
328 if (index
!= SECT_OFF_MAX
)
330 /* Found a special local symbol. Allocate a
331 sectinfo, if needed, and fill it in. */
332 if (sectinfo
== NULL
)
334 sectinfo
= (struct stab_section_info
*)
335 xmmalloc (objfile
-> md
, sizeof (*sectinfo
));
336 memset ((PTR
) sectinfo
, 0, sizeof (*sectinfo
));
339 complain (§ion_info_complaint
,
344 sectinfo
-> filename
=
345 (char *) filesym
-> name
;
348 if (sectinfo
-> sections
[index
] != 0)
350 complain (§ion_info_dup_complaint
,
351 sectinfo
-> filename
);
353 /* Bfd symbols are section relative. */
354 symaddr
= sym
-> value
+ sym
-> section
-> vma
;
355 /* Relocate non-absolute symbols by base address. */
356 if (sym
-> section
!= &bfd_abs_section
)
360 sectinfo
-> sections
[index
] = symaddr
;
361 /* The special local symbols don't go in the
362 minimal symbol table, so ignore this one. */
365 /* Not a special stabs-in-elf symbol, do regular
366 symbol processing. */
367 if (sym
-> section
-> flags
& SEC_HAS_CONTENTS
)
369 ms_type
= mst_file_data
;
373 ms_type
= mst_file_bss
;
378 ms_type
= mst_unknown
;
383 /* FIXME: Solaris2 shared libraries include lots of
384 odd "absolute" and "undefined" symbols, that play
385 hob with actions like finding what function the PC
386 is in. Ignore them if they aren't text, data, or bss. */
387 /* ms_type = mst_unknown; */
388 continue; /* Skip this symbol. */
390 /* Pass symbol size field in via BFD. FIXME!!! */
391 size
= ((elf_symbol_type
*) sym
) -> internal_elf_sym
.st_size
;
392 record_minimal_symbol_and_info ((char *) sym
-> name
, symaddr
,
393 ms_type
, (PTR
) size
, objfile
);
396 do_cleanups (back_to
);
400 /* Scan and build partial symbols for a symbol file.
401 We have been initialized by a call to elf_symfile_init, which
402 currently does nothing.
404 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
405 in each section. We simplify it down to a single offset for all
408 MAINLINE is true if we are reading the main symbol
409 table (as opposed to a shared lib or dynamically loaded file).
411 This function only does the minimum work necessary for letting the
412 user "name" things symbolically; it does not read the entire symtab.
413 Instead, it reads the external and static symbols and puts them in partial
414 symbol tables. When more extensive information is requested of a
415 file, the corresponding partial symbol table is mutated into a full
416 fledged symbol table by going back and reading the symbols
419 We look for sections with specific names, to tell us what debug
420 format to look for: FIXME!!!
422 dwarf_build_psymtabs() builds psymtabs for DWARF symbols;
423 elfstab_build_psymtabs() handles STABS symbols.
425 Note that ELF files have a "minimal" symbol table, which looks a lot
426 like a COFF symbol table, but has only the minimal information necessary
427 for linking. We process this also, and use the information to
428 build gdb's minimal symbol table. This gives us some minimal debugging
429 capability even for files compiled without -g. */
432 elf_symfile_read (objfile
, section_offsets
, mainline
)
433 struct objfile
*objfile
;
434 struct section_offsets
*section_offsets
;
437 bfd
*abfd
= objfile
->obfd
;
439 struct cleanup
*back_to
;
442 init_minimal_symbol_collection ();
443 back_to
= make_cleanup (discard_minimal_symbols
, 0);
445 memset ((char *) &ei
, 0, sizeof (ei
));
447 /* Allocate struct to keep track of the symfile */
448 objfile
->sym_private
= (PTR
)
449 xmmalloc (objfile
-> md
, sizeof (struct dbx_symfile_info
));
450 memset ((char *) objfile
->sym_private
, 0, sizeof (struct dbx_symfile_info
));
451 make_cleanup (free_elfinfo
, (PTR
) objfile
);
453 /* Process the normal ELF symbol table first. This may write some
454 chain of info into the dbx_symfile_info in objfile->sym_private,
455 which can later be used by elfstab_offset_sections. */
457 /* FIXME, should take a section_offsets param, not just an offset. */
458 offset
= ANOFFSET (section_offsets
, 0);
459 elf_symtab_read (abfd
, offset
, objfile
);
461 /* Now process debugging information, which is contained in
462 special ELF sections. We first have to find them... */
464 bfd_map_over_sections (abfd
, elf_locate_sections
, (PTR
) &ei
);
465 if (ei
.dboffset
&& ei
.lnoffset
)
468 dwarf_build_psymtabs (objfile
,
469 section_offsets
, mainline
,
470 ei
.dboffset
, ei
.dbsize
,
471 ei
.lnoffset
, ei
.lnsize
);
477 /* FIXME: Sun didn't really know how to implement this well.
478 They made .stab sections that don't point to the .stabstr
479 section with the sh_link field. BFD doesn't make string table
480 sections visible to the caller. So we have to search the
481 ELF section table, not the BFD section table, for the string
483 struct elf32_internal_shdr
*elf_sect
;
485 elf_sect
= bfd_elf_find_section (abfd
, ".stabstr");
487 elfstab_build_psymtabs (objfile
,
490 ei
.stabsect
->filepos
, /* .stab offset */
491 bfd_get_section_size_before_reloc (ei
.stabsect
),/* .stab size */
492 (file_ptr
) elf_sect
->sh_offset
, /* .stabstr offset */
493 elf_sect
->sh_size
); /* .stabstr size */
496 if (!have_partial_symbols ())
499 printf_filtered ("(no debugging symbols found)...");
503 /* Install any minimal symbols that have been collected as the current
504 minimal symbols for this objfile. */
506 install_minimal_symbols (objfile
);
508 do_cleanups (back_to
);
511 /* This cleans up the objfile's sym_private pointer, and the chain of
512 stab_section_info's, that might be dangling from it. */
518 struct objfile
*objfile
= (struct objfile
*)objp
;
519 struct dbx_symfile_info
*dbxinfo
= (struct dbx_symfile_info
*)
520 objfile
->sym_private
;
521 struct stab_section_info
*ssi
, *nssi
;
523 ssi
= dbxinfo
->stab_section_info
;
527 mfree (objfile
->md
, ssi
);
531 dbxinfo
->stab_section_info
= 0; /* Just say No mo info about this. */
535 /* Initialize anything that needs initializing when a completely new symbol
536 file is specified (not just adding some symbols from another file, e.g. a
539 We reinitialize buildsym, since we may be reading stabs from an ELF file. */
542 elf_new_init (ignore
)
543 struct objfile
*ignore
;
545 stabsread_new_init ();
546 buildsym_new_init ();
549 /* Perform any local cleanups required when we are done with a particular
550 objfile. I.E, we are in the process of discarding all symbol information
551 for an objfile, freeing up all memory held for it, and unlinking the
552 objfile struct from the global list of known objfiles. */
555 elf_symfile_finish (objfile
)
556 struct objfile
*objfile
;
558 if (objfile
-> sym_private
!= NULL
)
560 mfree (objfile
-> md
, objfile
-> sym_private
);
564 /* ELF specific initialization routine for reading symbols.
566 It is passed a pointer to a struct sym_fns which contains, among other
567 things, the BFD for the file whose symbols are being read, and a slot for
568 a pointer to "private data" which we can fill with goodies.
570 For now at least, we have nothing in particular to do, so this function is
574 elf_symfile_init (ignore
)
575 struct objfile
*ignore
;
579 /* ELF specific parsing routine for section offsets.
581 Plain and simple for now. */
584 struct section_offsets
*
585 elf_symfile_offsets (objfile
, addr
)
586 struct objfile
*objfile
;
589 struct section_offsets
*section_offsets
;
592 section_offsets
= (struct section_offsets
*)
593 obstack_alloc (&objfile
-> psymbol_obstack
,
594 sizeof (struct section_offsets
) +
595 sizeof (section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
597 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
598 ANOFFSET (section_offsets
, i
) = addr
;
600 return section_offsets
;
603 /* When handling an ELF file that contains Sun STABS debug info,
604 some of the debug info is relative to the particular chunk of the
605 section that was generated in its individual .o file. E.g.
606 offsets to static variables are relative to the start of the data
607 segment *for that module before linking*. This information is
608 painfully squirreled away in the ELF symbol table as local symbols
609 with wierd names. Go get 'em when needed. */
612 elfstab_offset_sections (objfile
, pst
)
613 struct objfile
*objfile
;
614 struct partial_symtab
*pst
;
616 char *filename
= pst
->filename
;
617 struct dbx_symfile_info
*dbx
= (struct dbx_symfile_info
*)
618 objfile
->sym_private
;
619 struct stab_section_info
*maybe
= dbx
->stab_section_info
;
620 struct stab_section_info
*questionable
= 0;
624 /* The ELF symbol info doesn't include path names, so strip the path
625 (if any) from the psymtab filename. */
626 while (0 != (p
= strchr (filename
, '/')))
629 /* FIXME: This linear search could speed up significantly
630 if it was chained in the right order to match how we search it,
631 and if we unchained when we found a match. */
632 for (; maybe
; maybe
= maybe
->next
)
634 if (filename
[0] == maybe
->filename
[0]
635 && STREQ (filename
, maybe
->filename
))
637 /* We found a match. But there might be several source files
638 (from different directories) with the same name. */
639 if (0 == maybe
->found
)
641 questionable
= maybe
; /* Might use it later. */
645 if (maybe
== 0 && questionable
!= 0)
647 complain (&stab_info_questionable_complaint
, filename
);
648 maybe
= questionable
;
653 /* Found it! Allocate a new psymtab struct, and fill it in. */
655 pst
->section_offsets
= (struct section_offsets
*)
656 obstack_alloc (&objfile
-> psymbol_obstack
,
657 sizeof (struct section_offsets
) +
658 sizeof (pst
->section_offsets
->offsets
) * (SECT_OFF_MAX
-1));
660 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
661 ANOFFSET (pst
->section_offsets
, i
) = maybe
->sections
[i
];
665 /* We were unable to find any offsets for this file. Complain. */
666 if (dbx
->stab_section_info
) /* If there *is* any info, */
667 complain (&stab_info_mismatch_complaint
, filename
);
670 /* Register that we are able to handle ELF object file formats and DWARF
673 Unlike other object file formats, where the debugging information format
674 is implied by the object file format, the ELF object file format and the
675 DWARF debugging information format are two distinct, and potentially
676 separate entities. I.E. it is perfectly possible to have ELF objects
677 with debugging formats other than DWARF. And it is conceivable that the
678 DWARF debugging format might be used with another object file format,
679 like COFF, by simply using COFF's custom section feature.
681 GDB, and to a lesser extent BFD, should support the notion of separate
682 object file formats and debugging information formats. For now, we just
683 use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
684 object file format and the DWARF debugging format. */
686 static struct sym_fns elf_sym_fns
=
688 "elf", /* sym_name: name or name prefix of BFD target type */
689 3, /* sym_namelen: number of significant sym_name chars */
690 elf_new_init
, /* sym_new_init: init anything gbl to entire symtab */
691 elf_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
692 elf_symfile_read
, /* sym_read: read a symbol file into symtab */
693 elf_symfile_finish
, /* sym_finish: finished with file, cleanup */
694 elf_symfile_offsets
, /* sym_offsets: Translate ext. to int. relocation */
695 NULL
/* next: pointer to next struct sym_fns */
699 _initialize_elfread ()
701 add_symtab_fns (&elf_sym_fns
);