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