use remote-utils facilities for baud_rate
[deliverable/binutils-gdb.git] / gdb / elfread.c
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.
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 #include "defs.h"
22 #include "bfd.h"
23 #include <time.h> /* For time_t in libbfd.h. */
24 #include "libbfd.h" /* For bfd_elf_find_section */
25 #include "libelf.h"
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "buildsym.h"
30 #include "stabsread.h"
31 #include "gdb-stabs.h"
32 #include "complaints.h"
33 #include <string.h>
34 #include "demangle.h"
35
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. */
39
40 struct elfinfo {
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 */
47 };
48
49 /* Various things we might complain about... */
50
51 struct complaint section_info_complaint =
52 {"elf/stab section information %s without a preceding file symbol", 0, 0};
53
54 struct complaint section_info_dup_complaint =
55 {"duplicated elf/stab section information for %s", 0, 0};
56
57 struct complaint stab_info_mismatch_complaint =
58 {"elf/stab section information missing for %s", 0, 0};
59
60 struct complaint stab_info_questionable_complaint =
61 {"elf/stab section information questionable for %s", 0, 0};
62
63 static void
64 elf_symfile_init PARAMS ((struct objfile *));
65
66 static void
67 elf_new_init PARAMS ((struct objfile *));
68
69 static void
70 elf_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
71
72 static void
73 elf_symfile_finish PARAMS ((struct objfile *));
74
75 static void
76 elf_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *));
77
78 static void
79 free_elfinfo PARAMS ((PTR));
80
81 static struct section_offsets *
82 elf_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
83
84 static void
85 record_minimal_symbol_and_info PARAMS ((char *, CORE_ADDR,
86 enum minimal_symbol_type, char *,
87 struct objfile *));
88
89 static void
90 elf_locate_sections PARAMS ((bfd *, asection *, PTR));
91
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.
96
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
101 used).
102
103 We also recognize the ".stab" sections used by the Sun compilers
104 released with Solaris 2.
105
106 FIXME: The section names should not be hardwired strings. */
107
108 static void
109 elf_locate_sections (ignore_abfd, sectp, eip)
110 bfd *ignore_abfd;
111 asection *sectp;
112 PTR eip;
113 {
114 register struct elfinfo *ei;
115
116 ei = (struct elfinfo *) eip;
117 if (STREQ (sectp -> name, ".debug"))
118 {
119 ei -> dboffset = sectp -> filepos;
120 ei -> dbsize = bfd_get_section_size_before_reloc (sectp);
121 }
122 else if (STREQ (sectp -> name, ".line"))
123 {
124 ei -> lnoffset = sectp -> filepos;
125 ei -> lnsize = bfd_get_section_size_before_reloc (sectp);
126 }
127 else if (STREQ (sectp -> name, ".stab"))
128 {
129 ei -> stabsect = sectp;
130 }
131 else if (STREQ (sectp -> name, ".stab.index"))
132 {
133 ei -> stabindexsect = sectp;
134 }
135 }
136
137 #if 0 /* Currently unused */
138
139 char *
140 elf_interpreter (abfd)
141 bfd *abfd;
142 {
143 sec_ptr interp_sec;
144 unsigned size;
145 char *interp = NULL;
146
147 interp_sec = bfd_get_section_by_name (abfd, ".interp");
148 if (interp_sec)
149 {
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,
153 size))
154 {
155 interp = savestring (interp, size - 1);
156 }
157 else
158 {
159 interp = NULL;
160 }
161 }
162 return (interp);
163 }
164
165 #endif
166
167 static void
168 record_minimal_symbol_and_info (name, address, ms_type, info, objfile)
169 char *name;
170 CORE_ADDR address;
171 enum minimal_symbol_type ms_type;
172 char *info; /* FIXME, is this really char *? */
173 struct objfile *objfile;
174 {
175 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
176 prim_record_minimal_symbol_and_info (name, address, ms_type, info, -1);
177 }
178
179 /*
180
181 LOCAL FUNCTION
182
183 elf_symtab_read -- read the symbol table of an ELF file
184
185 SYNOPSIS
186
187 void elf_symtab_read (bfd *abfd, CORE_ADDR addr,
188 struct objfile *objfile)
189
190 DESCRIPTION
191
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.
196
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.
202
203 */
204
205 static void
206 elf_symtab_read (abfd, addr, objfile)
207 bfd *abfd;
208 CORE_ADDR addr;
209 struct objfile *objfile;
210 {
211 unsigned int storage_needed;
212 asymbol *sym;
213 asymbol **symbol_table;
214 unsigned int number_of_symbols;
215 unsigned int i;
216 int index;
217 struct cleanup *back_to;
218 CORE_ADDR symaddr;
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;
228 unsigned long size;
229
230 storage_needed = get_symtab_upper_bound (abfd);
231 if (storage_needed > 0)
232 {
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++)
237 {
238 sym = symbol_table[i];
239 if (sym -> name == NULL || *sym -> name == '\0')
240 {
241 /* Skip names that don't exist (shouldn't happen), or names
242 that are null strings (may happen). */
243 continue;
244 }
245 if (sym -> flags & BSF_FILE)
246 {
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)
250 {
251 sectinfo -> next = dbx -> stab_section_info;
252 dbx -> stab_section_info = sectinfo;
253 sectinfo = NULL;
254 }
255 filesym = sym;
256 }
257 else if (sym -> flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
258 {
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)
266 {
267 symaddr += addr;
268 }
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)
273 {
274 ms_type = mst_abs;
275 }
276 else if (sym -> section -> flags & SEC_CODE)
277 {
278 if (sym -> flags & BSF_GLOBAL)
279 {
280 ms_type = mst_text;
281 }
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). */
289 continue;
290 else
291 {
292 ms_type = mst_file_text;
293 }
294 }
295 else if (sym -> section -> flags & SEC_DATA)
296 {
297 if (sym -> flags & BSF_GLOBAL)
298 {
299 if (sym -> section -> flags & SEC_HAS_CONTENTS)
300 {
301 ms_type = mst_data;
302 }
303 else
304 {
305 ms_type = mst_bss;
306 }
307 }
308 else if (sym -> flags & BSF_LOCAL)
309 {
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'
314 or 'D' */
315 index = SECT_OFF_MAX;
316 if (STREQ ("Bbss.bss", sym -> name))
317 {
318 index = SECT_OFF_BSS;
319 }
320 else if (STREQ ("Ddata.data", sym -> name))
321 {
322 index = SECT_OFF_DATA;
323 }
324 else if (STREQ ("Drodata.rodata", sym -> name))
325 {
326 index = SECT_OFF_RODATA;
327 }
328 if (index != SECT_OFF_MAX)
329 {
330 /* Found a special local symbol. Allocate a
331 sectinfo, if needed, and fill it in. */
332 if (sectinfo == NULL)
333 {
334 sectinfo = (struct stab_section_info *)
335 xmmalloc (objfile -> md, sizeof (*sectinfo));
336 memset ((PTR) sectinfo, 0, sizeof (*sectinfo));
337 if (filesym == NULL)
338 {
339 complain (&section_info_complaint,
340 sym -> name);
341 }
342 else
343 {
344 sectinfo -> filename =
345 (char *) filesym -> name;
346 }
347 }
348 if (sectinfo -> sections[index] != 0)
349 {
350 complain (&section_info_dup_complaint,
351 sectinfo -> filename);
352 }
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)
357 {
358 symaddr += addr;
359 }
360 sectinfo -> sections[index] = symaddr;
361 /* The special local symbols don't go in the
362 minimal symbol table, so ignore this one. */
363 continue;
364 }
365 /* Not a special stabs-in-elf symbol, do regular
366 symbol processing. */
367 if (sym -> section -> flags & SEC_HAS_CONTENTS)
368 {
369 ms_type = mst_file_data;
370 }
371 else
372 {
373 ms_type = mst_file_bss;
374 }
375 }
376 else
377 {
378 ms_type = mst_unknown;
379 }
380 }
381 else
382 {
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. */
389 }
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);
394 }
395 }
396 do_cleanups (back_to);
397 }
398 }
399
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.
403
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
406 symbols. FIXME.
407
408 MAINLINE is true if we are reading the main symbol
409 table (as opposed to a shared lib or dynamically loaded file).
410
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
417 for real.
418
419 We look for sections with specific names, to tell us what debug
420 format to look for: FIXME!!!
421
422 dwarf_build_psymtabs() builds psymtabs for DWARF symbols;
423 elfstab_build_psymtabs() handles STABS symbols.
424
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. */
430
431 static void
432 elf_symfile_read (objfile, section_offsets, mainline)
433 struct objfile *objfile;
434 struct section_offsets *section_offsets;
435 int mainline;
436 {
437 bfd *abfd = objfile->obfd;
438 struct elfinfo ei;
439 struct cleanup *back_to;
440 CORE_ADDR offset;
441
442 init_minimal_symbol_collection ();
443 back_to = make_cleanup (discard_minimal_symbols, 0);
444
445 memset ((char *) &ei, 0, sizeof (ei));
446
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);
452
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. */
456
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);
460
461 /* Now process debugging information, which is contained in
462 special ELF sections. We first have to find them... */
463
464 bfd_map_over_sections (abfd, elf_locate_sections, (PTR) &ei);
465 if (ei.dboffset && ei.lnoffset)
466 {
467 /* DWARF sections */
468 dwarf_build_psymtabs (objfile,
469 section_offsets, mainline,
470 ei.dboffset, ei.dbsize,
471 ei.lnoffset, ei.lnsize);
472 }
473 if (ei.stabsect)
474 {
475 /* STABS sections */
476
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
482 table. */
483 struct elf32_internal_shdr *elf_sect;
484
485 elf_sect = bfd_elf_find_section (abfd, ".stabstr");
486 if (elf_sect)
487 elfstab_build_psymtabs (objfile,
488 section_offsets,
489 mainline,
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 */
494 }
495
496 if (!have_partial_symbols ())
497 {
498 wrap_here ("");
499 printf_filtered ("(no debugging symbols found)...");
500 wrap_here ("");
501 }
502
503 /* Install any minimal symbols that have been collected as the current
504 minimal symbols for this objfile. */
505
506 install_minimal_symbols (objfile);
507
508 do_cleanups (back_to);
509 }
510
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. */
513
514 static void
515 free_elfinfo (objp)
516 PTR objp;
517 {
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;
522
523 ssi = dbxinfo->stab_section_info;
524 while (ssi)
525 {
526 nssi = ssi->next;
527 mfree (objfile->md, ssi);
528 ssi = nssi;
529 }
530
531 dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */
532 }
533
534
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
537 shared library).
538
539 We reinitialize buildsym, since we may be reading stabs from an ELF file. */
540
541 static void
542 elf_new_init (ignore)
543 struct objfile *ignore;
544 {
545 stabsread_new_init ();
546 buildsym_new_init ();
547 }
548
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. */
553
554 static void
555 elf_symfile_finish (objfile)
556 struct objfile *objfile;
557 {
558 if (objfile -> sym_private != NULL)
559 {
560 mfree (objfile -> md, objfile -> sym_private);
561 }
562 }
563
564 /* ELF specific initialization routine for reading symbols.
565
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.
569
570 For now at least, we have nothing in particular to do, so this function is
571 just a stub. */
572
573 static void
574 elf_symfile_init (ignore)
575 struct objfile *ignore;
576 {
577 }
578
579 /* ELF specific parsing routine for section offsets.
580
581 Plain and simple for now. */
582
583 static
584 struct section_offsets *
585 elf_symfile_offsets (objfile, addr)
586 struct objfile *objfile;
587 CORE_ADDR addr;
588 {
589 struct section_offsets *section_offsets;
590 int i;
591
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));
596
597 for (i = 0; i < SECT_OFF_MAX; i++)
598 ANOFFSET (section_offsets, i) = addr;
599
600 return section_offsets;
601 }
602 \f
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. */
610
611 void
612 elfstab_offset_sections (objfile, pst)
613 struct objfile *objfile;
614 struct partial_symtab *pst;
615 {
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;
621 int i;
622 char *p;
623
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, '/')))
627 filename = p+1;
628
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)
633 {
634 if (filename[0] == maybe->filename[0]
635 && STREQ (filename, maybe->filename))
636 {
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)
640 break;
641 questionable = maybe; /* Might use it later. */
642 }
643 }
644
645 if (maybe == 0 && questionable != 0)
646 {
647 complain (&stab_info_questionable_complaint, filename);
648 maybe = questionable;
649 }
650
651 if (maybe)
652 {
653 /* Found it! Allocate a new psymtab struct, and fill it in. */
654 maybe->found++;
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));
659
660 for (i = 0; i < SECT_OFF_MAX; i++)
661 ANOFFSET (pst->section_offsets, i) = maybe->sections[i];
662 return;
663 }
664
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);
668 }
669 \f
670 /* Register that we are able to handle ELF object file formats and DWARF
671 debugging formats.
672
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.
680
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. */
685
686 static struct sym_fns elf_sym_fns =
687 {
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 */
696 };
697
698 void
699 _initialize_elfread ()
700 {
701 add_symtab_fns (&elf_sym_fns);
702 }
This page took 0.046 seconds and 4 git commands to generate.