* complaints.c: New file, code moved from utils.c.
[platform/upstream/binutils.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 /************************************************************************
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@cygnus.com)                      *
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  *                                                                      *
33  ************************************************************************/
34
35 #include "defs.h"
36 #include "elf/common.h"
37 #include "elf/external.h"
38 #include "elf/internal.h"
39 #include "bfd.h"
40 #include "libbfd.h"             /* For bfd_elf_find_section */
41 #include "symtab.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "buildsym.h"
45 #include "gdb-stabs.h"
46 #include "complaints.h"
47 #include <string.h>
48
49 #define STREQ(a,b) (strcmp((a),(b))==0)
50
51 /* The struct elfinfo is available only during ELF symbol table and
52    psymtab reading.  It is destroyed at the complation of psymtab-reading.
53    It's local to elf_symfile_read.  */
54
55 struct elfinfo {
56   file_ptr dboffset;            /* Offset to dwarf debug section */
57   unsigned int dbsize;          /* Size of dwarf debug section */
58   file_ptr lnoffset;            /* Offset to dwarf line number section */
59   unsigned int lnsize;          /* Size of dwarf line number section */
60   asection *stabsect;           /* Section pointer for .stab section */
61   asection *stabindexsect;      /* Section pointer for .stab.index section */
62 };
63
64 /* Various things we might complain about... */
65
66 struct complaint section_info_complaint = 
67   {"elf/stab section information %s without a preceding file symbol", 0, 0};
68
69 struct complaint section_info_dup_complaint = 
70   {"duplicated elf/stab section information for %s", 0, 0};
71
72 struct complaint stab_info_mismatch_complaint = 
73   {"elf/stab section information missing for %s", 0, 0};
74
75 struct complaint stab_info_questionable_complaint = 
76   {"elf/stab section information questionable for %s", 0, 0};
77
78 static void
79 elf_symfile_init PARAMS ((struct objfile *));
80
81 static void
82 elf_new_init PARAMS ((struct objfile *));
83
84 static void
85 elf_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
86
87 static void
88 elf_symfile_finish PARAMS ((struct objfile *));
89
90 static void
91 elf_symtab_read PARAMS ((bfd *,  CORE_ADDR, struct objfile *));
92
93 static void
94 free_elfinfo PARAMS ((PTR));
95
96 static struct section_offsets *
97 elf_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
98
99 #if 0
100 static void
101 record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,
102                                struct objfile *));
103 #endif
104
105 static void
106 record_minimal_symbol_and_info PARAMS ((char *, CORE_ADDR,
107                                         enum minimal_symbol_type, char *,
108                                         struct objfile *));
109
110 static void
111 elf_locate_sections PARAMS ((bfd *, asection *, PTR));
112
113 /* We are called once per section from elf_symfile_read.  We
114    need to examine each section we are passed, check to see
115    if it is something we are interested in processing, and
116    if so, stash away some access information for the section.
117
118    For now we recognize the dwarf debug information sections and
119    line number sections from matching their section names.  The
120    ELF definition is no real help here since it has no direct
121    knowledge of DWARF (by design, so any debugging format can be
122    used).
123
124    We also recognize the ".stab" sections used by the Sun compilers
125    released with Solaris 2.
126
127    FIXME:  The section names should not be hardwired strings. */
128
129 static void
130 elf_locate_sections (ignore_abfd, sectp, eip)
131      bfd *ignore_abfd;
132      asection *sectp;
133      PTR eip;
134 {
135   register struct elfinfo *ei;
136
137   ei = (struct elfinfo *) eip;
138   if (STREQ (sectp -> name, ".debug"))
139     {
140       ei -> dboffset = sectp -> filepos;
141       ei -> dbsize = bfd_get_section_size_before_reloc (sectp);
142     }
143   else if (STREQ (sectp -> name, ".line"))
144     {
145       ei -> lnoffset = sectp -> filepos;
146       ei -> lnsize = bfd_get_section_size_before_reloc (sectp);
147     }
148   else if (STREQ (sectp -> name, ".stab"))
149     {
150       ei -> stabsect = sectp;
151     }
152   else if (STREQ (sectp -> name, ".stab.index"))
153     {
154       ei -> stabindexsect = sectp;
155     }
156 }
157
158 #if 0   /* Currently unused */
159
160 char *
161 elf_interpreter (abfd)
162      bfd *abfd;
163 {
164   sec_ptr interp_sec;
165   unsigned size;
166   char *interp = NULL;
167
168   interp_sec = bfd_get_section_by_name (abfd, ".interp");
169   if (interp_sec)
170     {
171       size = bfd_section_size (abfd, interp_sec);
172       interp = alloca (size);
173       if (bfd_get_section_contents (abfd, interp_sec, interp, (file_ptr)0,
174                                     size))
175         {
176           interp = savestring (interp, size - 1);
177         }
178       else
179         {
180           interp = NULL;
181         }
182     }
183   return (interp);
184 }
185
186 #endif
187
188 /*
189
190 LOCAL FUNCTION
191
192         record_minimal_symbol -- add entry to minimal symbol table
193
194 SYNOPSIS
195
196         static void record_minimal_symbol (char *name, CORE_ADDR address)
197
198 DESCRIPTION
199
200         Given a pointer to the name of a symbol that should be added to the
201         minimal symbol table and the address associated with that symbol, records
202         this information for later use in building the minimal symbol table.
203
204  */
205
206 #if 0   /* FIXME:  Unused */
207
208 static void
209 record_minimal_symbol (name, address, ms_type, objfile)
210      char *name;
211      CORE_ADDR address;
212      enum minimal_symbol_type ms_type;
213      struct objfile *objfile;
214 {
215   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
216   prim_record_minimal_symbol (name, address, ms_type);
217 }
218
219 #endif
220
221 static void
222 record_minimal_symbol_and_info (name, address, ms_type, info, objfile)
223      char *name;
224      CORE_ADDR address;
225      enum minimal_symbol_type ms_type;
226      char *info;                /* FIXME, is this really char *? */
227      struct objfile *objfile;
228 {
229   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
230   prim_record_minimal_symbol_and_info (name, address, ms_type, info);
231 }
232
233 /*
234
235 LOCAL FUNCTION
236
237         elf_symtab_read -- read the symbol table of an ELF file
238
239 SYNOPSIS
240
241         void elf_symtab_read (bfd *abfd, CORE_ADDR addr,
242                               struct objfile *objfile)
243
244 DESCRIPTION
245
246         Given an open bfd, a base address to relocate symbols to, and a
247         flag that specifies whether or not this bfd is for an executable
248         or not (may be shared library for example), add all the global
249         function and data symbols to the minimal symbol table.
250
251         In stabs-in-ELF, as implemented by Sun, there are some local symbols
252         defined in the ELF symbol table, which can be used to locate
253         the beginnings of sections from each ".o" file that was linked to
254         form the executable objfile.  We gather any such info and record it
255         in data structures hung off the objfile's private data.
256
257 */
258
259 static void
260 elf_symtab_read (abfd, addr, objfile)
261      bfd *abfd;
262      CORE_ADDR addr;
263      struct objfile *objfile;
264 {
265   unsigned int storage_needed;
266   asymbol *sym;
267   asymbol **symbol_table;
268   unsigned int number_of_symbols;
269   unsigned int i;
270   int index;
271   struct cleanup *back_to;
272   CORE_ADDR symaddr;
273   enum minimal_symbol_type ms_type;
274   /* If sectinfo is nonzero, it contains section info that should end up
275      filed in the objfile.  */
276   struct stab_section_info *sectinfo = 0;
277   /* If filesym is nonzero, it points to a file symbol, but we haven't
278      seen any section info for it yet.  */
279   asymbol *filesym = 0;
280   struct dbx_symfile_info *dbx = (struct dbx_symfile_info *)
281                                  objfile->sym_private;
282   
283   storage_needed = get_symtab_upper_bound (abfd);
284
285   if (storage_needed > 0)
286     {
287       symbol_table = (asymbol **) xmalloc (storage_needed);
288       back_to = make_cleanup (free, symbol_table);
289       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
290   
291       for (i = 0; i < number_of_symbols; i++)
292         {
293           sym = symbol_table[i];
294           /* Select global/weak symbols that are defined in a specific section.
295              Note that bfd now puts abs symbols in their own section, so
296              all symbols we are interested in will have a section. */
297           if ((sym -> flags & (BSF_GLOBAL | BSF_WEAK))
298               && (sym -> section != NULL))
299             {
300               symaddr = sym -> value;
301               /* Relocate all non-absolute symbols by base address.  */
302               if (sym -> section != &bfd_abs_section)
303                 symaddr += addr;
304
305               /* For non-absolute symbols, use the type of the section
306                  they are relative to, to intuit text/data.  Bfd provides
307                  no way of figuring this out for absolute symbols. */
308               if (sym -> section -> flags & SEC_CODE)
309                 {
310                   ms_type = mst_text;
311                 }
312               else if (sym -> section -> flags & SEC_DATA)
313                 {
314                   ms_type = mst_data;
315                 }
316               else
317                 {
318                   /* FIXME:  Solaris2 shared libraries include lots of
319                      odd "absolute" and "undefined" symbols, that play 
320                      hob with actions like finding what function the PC
321                      is in.  Ignore them if they aren't text or data.  */
322                   /* ms_type = mst_unknown; */
323                   continue;             /* Skip this symbol. */
324                 }
325               /* Pass symbol size field in via BFD.  FIXME!!!  */
326               record_minimal_symbol_and_info ((char *) sym -> name,
327                          symaddr, ms_type, sym->udata, objfile);
328             }
329
330           /* See if this is a debugging symbol that helps Solaris
331              stabs-in-elf debugging.  */
332
333           else if (sym->flags & BSF_FILE)
334             {
335               /* Chain any old one onto the objfile; remember new sym.  */
336               if (sectinfo)
337                 {
338                     sectinfo->next = dbx->stab_section_info;
339                     dbx->stab_section_info = sectinfo;
340                     sectinfo = 0;
341                 }
342               filesym = sym;
343             }
344           else if ((sym->flags & BSF_LOCAL) &&
345                    (sym->section) &&
346                    (sym->section->flags & SEC_DATA) &&
347                    (sym->name))
348             {
349               /* Named Local variable in a Data section.  Check its name.  */
350               index = -1;
351               switch (sym->name[1])
352                 {
353                   case 'b':
354                         if (!strcmp ("Bbss.bss", sym->name))
355                           index = SECT_OFF_BSS;
356                         break;
357                   case 'd':
358                         if (!strcmp ("Ddata.data", sym->name))
359                           index = SECT_OFF_DATA;
360                         break;
361                   case 'r':
362                         if (!strcmp ("Drodata.rodata", sym->name))
363                           index = SECT_OFF_RODATA;
364                         break;
365                 }
366               if (index > 0)
367                 {
368                   /* We have some new info.  Allocate a sectinfo, if
369                      needed, and fill it in.  */
370                   if (!sectinfo)
371                     {
372                       sectinfo = (struct stab_section_info *)
373                                  xmmalloc (objfile -> md,
374                                            sizeof (*sectinfo));
375                       memset ((PTR) sectinfo, 0, sizeof (*sectinfo));
376                       if (!filesym)
377                         complain (&section_info_complaint, sym->name);
378                       else
379                         sectinfo->filename = (char *)filesym->name;
380                     }
381                   if (sectinfo->sections[index])
382                     complain (&section_info_dup_complaint, sectinfo->filename);
383
384                   symaddr = sym -> value;
385                   /* Relocate all non-absolute symbols by base address.  */
386                   if (sym -> section != &bfd_abs_section)
387                       symaddr += addr;
388                   sectinfo->sections[index] = symaddr;
389                 }
390             }
391         }
392       do_cleanups (back_to);
393     }
394 }
395
396 /* Scan and build partial symbols for a symbol file.
397    We have been initialized by a call to elf_symfile_init, which 
398    currently does nothing.
399
400    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
401    in each section.  We simplify it down to a single offset for all
402    symbols.  FIXME.
403
404    MAINLINE is true if we are reading the main symbol
405    table (as opposed to a shared lib or dynamically loaded file).
406
407    This function only does the minimum work necessary for letting the
408    user "name" things symbolically; it does not read the entire symtab.
409    Instead, it reads the external and static symbols and puts them in partial
410    symbol tables.  When more extensive information is requested of a
411    file, the corresponding partial symbol table is mutated into a full
412    fledged symbol table by going back and reading the symbols
413    for real.
414
415    We look for sections with specific names, to tell us what debug
416    format to look for:  FIXME!!!
417
418    dwarf_build_psymtabs() builds psymtabs for DWARF symbols;
419    elfstab_build_psymtabs() handles STABS symbols.
420
421    Note that ELF files have a "minimal" symbol table, which looks a lot
422    like a COFF symbol table, but has only the minimal information necessary
423    for linking.  We process this also, and use the information to
424    build gdb's minimal symbol table.  This gives us some minimal debugging
425    capability even for files compiled without -g.  */
426
427 static void
428 elf_symfile_read (objfile, section_offsets, mainline)
429      struct objfile *objfile;
430      struct section_offsets *section_offsets;
431      int mainline;
432 {
433   bfd *abfd = objfile->obfd;
434   struct elfinfo ei;
435   struct dbx_symfile_info *dbx;
436   struct cleanup *back_to;
437   asection *text_sect;
438   CORE_ADDR offset;
439
440   init_minimal_symbol_collection ();
441   back_to = make_cleanup (discard_minimal_symbols, 0);
442
443   memset ((char *) &ei, 0, sizeof (ei));
444
445   /* Allocate struct to keep track of the symfile */
446   objfile->sym_private = (PTR)
447     xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
448   memset ((char *) objfile->sym_private, 0, sizeof (struct dbx_symfile_info));
449   make_cleanup (free_elfinfo, (PTR) objfile);
450
451   /* Process the normal ELF symbol table first.  This may write some 
452      chain of info into the dbx_symfile_info in objfile->sym_private,
453      which can later be used by elfstab_offset_sections.  */
454
455   /* FIXME, should take a section_offsets param, not just an offset.  */
456   offset = ANOFFSET (section_offsets, 0);
457   elf_symtab_read (abfd, offset, objfile);
458
459   /* Now process debugging information, which is contained in
460      special ELF sections.  We first have to find them... */
461
462   bfd_map_over_sections (abfd, elf_locate_sections, (PTR) &ei);
463   if (ei.dboffset && ei.lnoffset)
464     {
465       /* DWARF sections */
466       dwarf_build_psymtabs (objfile,
467                             section_offsets, mainline,
468                             ei.dboffset, ei.dbsize,
469                             ei.lnoffset, ei.lnsize);
470     }
471   if (ei.stabsect)
472     {
473       /* STABS sections */
474
475       /* FIXME:  Sun didn't really know how to implement this well.
476          They made .stab sections that don't point to the .stabstr
477          section with the sh_link field.  BFD doesn't make string table
478          sections visible to the caller.  So we have to search the
479          ELF section table, not the BFD section table, for the string
480          table.  */
481       struct elf_internal_shdr *elf_sect;
482
483       elf_sect = bfd_elf_find_section (abfd, ".stabstr");
484       if (elf_sect)
485         elfstab_build_psymtabs (objfile,
486           section_offsets,
487           mainline,
488           ei.stabsect->filepos,                         /* .stab offset */
489           bfd_get_section_size_before_reloc (ei.stabsect),/* .stab size */
490           (file_ptr) elf_sect->sh_offset,               /* .stabstr offset */
491           elf_sect->sh_size);                           /* .stabstr size */
492     }
493
494   if (!have_partial_symbols ())
495     {
496       wrap_here ("");
497       printf_filtered ("(no debugging symbols found)...");
498       wrap_here ("");
499     }
500
501   /* Install any minimal symbols that have been collected as the current
502      minimal symbols for this objfile. */
503
504   install_minimal_symbols (objfile);
505
506   do_cleanups (back_to);
507 }
508
509 /* This cleans up the objfile's sym_private pointer, and the chain of
510    stab_section_info's, that might be dangling from it.  */
511
512 static void
513 free_elfinfo (objp)
514      PTR objp;
515 {
516   struct objfile *objfile = (struct objfile *)objp;
517   struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
518                                      objfile->sym_private;
519   struct stab_section_info *ssi, *nssi;
520
521   ssi = dbxinfo->stab_section_info;
522   while (ssi)
523     {
524       nssi = ssi->next;
525       mfree (objfile->md, ssi);
526       ssi = nssi;
527     }
528
529   dbxinfo->stab_section_info = 0;       /* Just say No mo info about this.  */
530 }
531
532
533 /* Initialize anything that needs initializing when a completely new symbol
534    file is specified (not just adding some symbols from another file, e.g. a
535    shared library).
536
537    We reinitialize buildsym, since we may be reading stabs from an ELF file.  */
538
539 static void
540 elf_new_init (ignore)
541      struct objfile *ignore;
542 {
543   stabsread_new_init ();
544   buildsym_new_init ();
545 }
546
547 /* Perform any local cleanups required when we are done with a particular
548    objfile.  I.E, we are in the process of discarding all symbol information
549    for an objfile, freeing up all memory held for it, and unlinking the
550    objfile struct from the global list of known objfiles. */
551
552 static void
553 elf_symfile_finish (objfile)
554      struct objfile *objfile;
555 {
556   if (objfile -> sym_private != NULL)
557     {
558       mfree (objfile -> md, objfile -> sym_private);
559     }
560 }
561
562 /* ELF specific initialization routine for reading symbols.
563
564    It is passed a pointer to a struct sym_fns which contains, among other
565    things, the BFD for the file whose symbols are being read, and a slot for
566    a pointer to "private data" which we can fill with goodies.
567
568    For now at least, we have nothing in particular to do, so this function is
569    just a stub. */
570
571 static void
572 elf_symfile_init (ignore)
573      struct objfile *ignore;
574 {
575 }
576
577 /* ELF specific parsing routine for section offsets.
578
579    Plain and simple for now.  */
580
581 static
582 struct section_offsets *
583 elf_symfile_offsets (objfile, addr)
584      struct objfile *objfile;
585      CORE_ADDR addr;
586 {
587   struct section_offsets *section_offsets;
588   int i;
589  
590   section_offsets = (struct section_offsets *)
591     obstack_alloc (&objfile -> psymbol_obstack,
592                    sizeof (struct section_offsets) +
593                           sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
594
595   for (i = 0; i < SECT_OFF_MAX; i++)
596     ANOFFSET (section_offsets, i) = addr;
597   
598   return section_offsets;
599 }
600 \f
601 /* When handling an ELF file that contains Sun STABS debug info,
602    some of the debug info is relative to the particular chunk of the
603    section that was generated in its individual .o file.  E.g.
604    offsets to static variables are relative to the start of the data
605    segment *for that module before linking*.  This information is
606    painfully squirreled away in the ELF symbol table as local symbols
607    with wierd names.  Go get 'em when needed.  */
608
609 void
610 elfstab_offset_sections (objfile, pst)
611      struct objfile *objfile;
612      struct partial_symtab *pst;
613 {
614   char *filename = pst->filename;
615   struct dbx_symfile_info *dbx = (struct dbx_symfile_info *)
616                                  objfile->sym_private;
617   struct stab_section_info *maybe = dbx->stab_section_info;
618   struct stab_section_info *questionable = 0;
619   int i;
620   char *p;
621
622   /* The ELF symbol info doesn't include path names, so strip the path
623      (if any) from the psymtab filename.  */
624   while (0 != (p = strchr (filename, '/')))
625     filename = p+1;
626
627   /* FIXME:  This linear search could speed up significantly
628      if it was chained in the right order to match how we search it,
629      and if we unchained when we found a match. */
630   for (; maybe; maybe = maybe->next)
631     {
632       if (filename[0] == maybe->filename[0]
633           && !strcmp (filename, maybe->filename))
634         {
635           /* We found a match.  But there might be several source files
636              (from different directories) with the same name.  */
637           if (0 == maybe->found)
638             break;
639           questionable = maybe;         /* Might use it later.  */
640         }
641     }
642
643   if (maybe == 0 && questionable != 0)
644     {
645       complain (&stab_info_questionable_complaint, filename);
646       maybe = questionable;
647     }
648
649   if (maybe)
650     {
651       /* Found it!  Allocate a new psymtab struct, and fill it in.  */
652       maybe->found++;
653       pst->section_offsets = (struct section_offsets *)
654         obstack_alloc (&objfile -> psymbol_obstack,
655                        sizeof (struct section_offsets) +
656                sizeof (pst->section_offsets->offsets) * (SECT_OFF_MAX-1));
657
658       for (i = 0; i < SECT_OFF_MAX; i++)
659         ANOFFSET (pst->section_offsets, i) = maybe->sections[i];
660       return;
661     }
662
663   /* We were unable to find any offsets for this file.  Complain.  */
664   if (dbx->stab_section_info)           /* If there *is* any info, */
665     complain (&stab_info_mismatch_complaint, filename);
666 }
667 \f
668 /*  Register that we are able to handle ELF object file formats and DWARF
669     debugging formats.
670
671     Unlike other object file formats, where the debugging information format
672     is implied by the object file format, the ELF object file format and the
673     DWARF debugging information format are two distinct, and potentially
674     separate entities.  I.E. it is perfectly possible to have ELF objects
675     with debugging formats other than DWARF.  And it is conceivable that the
676     DWARF debugging format might be used with another object file format,
677     like COFF, by simply using COFF's custom section feature.
678
679     GDB, and to a lesser extent BFD, should support the notion of separate
680     object file formats and debugging information formats.  For now, we just
681     use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
682     object file format and the DWARF debugging format. */
683
684 static struct sym_fns elf_sym_fns =
685 {
686   "elf",                /* sym_name: name or name prefix of BFD target type */
687   3,                    /* sym_namelen: number of significant sym_name chars */
688   elf_new_init,         /* sym_new_init: init anything gbl to entire symtab */
689   elf_symfile_init,     /* sym_init: read initial info, setup for sym_read() */
690   elf_symfile_read,     /* sym_read: read a symbol file into symtab */
691   elf_symfile_finish,   /* sym_finish: finished with file, cleanup */
692   elf_symfile_offsets,  /* sym_offsets:  Translate ext. to int. relocation */
693   NULL                  /* next: pointer to next struct sym_fns */
694 };
695
696 void
697 _initialize_elfread ()
698 {
699   add_symtab_fns (&elf_sym_fns);
700 }