]> Git Repo - binutils.git/blob - gdb/elfread.c
ansi name abuse changes
[binutils.git] / gdb / elfread.c
1 /* Read ELF (Executable and Linking Format) object files for GDB.
2    Copyright (C) 1991 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@cygint)                          *
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  * FIXME        Still needs support ELF symbol tables (as distinct      *
33  *              from DWARF support).  Can use them to build the misc    *
34  *              function vector at least.  This is fairly trivial once  *
35  *              bfd is extended to handle ELF symbol tables.            *
36  *                                                                      *
37  ************************************************************************/
38
39 #include <stdio.h>
40
41 #include "defs.h"
42 #include "param.h"
43 #include "elf-common.h"
44 #include "elf-external.h"
45 #include "elf-internal.h"
46 #include "bfd.h"
47 #include "symfile.h"
48 #include "symtab.h"
49 #include "ansidecl.h"
50
51 extern int EXFUN (strcmp, (CONST char *a, CONST char *b));
52 extern int EXFUN (dwarf_build_psymtabs,
53      (int desc, char *filename, CORE_ADDR addr, int mainline,
54       unsigned int dbfoff, unsigned int dbsize, unsigned int lnoffset,
55       unsigned int lnsize, struct objfile *objfile));
56
57 #define STREQ(a,b) (strcmp((a),(b))==0)
58
59 struct elfinfo {
60   unsigned int dboffset;        /* Offset to dwarf debug section */
61   unsigned int dbsize;          /* Size of dwarf debug section */
62   unsigned int lnoffset;        /* Offset to dwarf line number section */
63   unsigned int lnsize;          /* Size of dwarf line number section */
64 };
65
66 /* We are called once per section from elf_symfile_read.  We
67    need to examine each section we are passed, check to see
68    if it is something we are interested in processing, and
69    if so, stash away some access information for the section.
70
71    For now we recognize the dwarf debug information sections and
72    line number sections from matching their section names.  The
73    ELF definition is no real help here since it has no direct
74    knowledge of DWARF (by design, so any debugging format can be
75    used).
76
77    FIXME:  The section names should not be hardwired strings. */
78
79 static void
80 DEFUN(elf_locate_sections, (abfd, sectp, ei),
81       bfd *abfd AND
82       asection *sectp AND
83       struct elfinfo *ei)
84 {
85   if (STREQ (sectp -> name, ".debug"))
86     {
87       ei -> dboffset = sectp -> filepos;
88       ei -> dbsize = sectp -> size;
89     }
90   else if (STREQ (sectp -> name, ".line"))
91     {
92       ei -> lnoffset = sectp -> filepos;
93       ei -> lnsize = sectp -> size;
94     }
95 }
96
97 /*
98
99 LOCAL FUNCTION
100
101         record_misc_function -- add entry to miscellaneous function vector
102
103 SYNOPSIS
104
105         static void record_misc_function (char *name, CORE_ADDR address)
106
107 DESCRIPTION
108
109         Given a pointer to the name of a symbol that should be added to the
110         miscellaneous function vector, and the address associated with that
111         symbol, records this information for later use in building the
112         miscellaneous function vector.
113
114 NOTES
115
116         FIXME:  For now we just use mf_unknown as the type.  This should be
117         fixed.
118  */
119
120 static void
121 DEFUN(record_misc_function, (name, address), char *name AND CORE_ADDR address)
122 {
123   prim_record_misc_function (obsavestring (name, strlen (name)), address,
124                              mf_unknown);
125 }
126
127 static void
128 DEFUN (elf_symtab_read, (abfd, addr),
129        bfd *abfd AND
130        CORE_ADDR addr)
131 {
132   unsigned int storage_needed;
133   asymbol *sym;
134   asymbol **symbol_table;
135   unsigned int number_of_symbols;
136   unsigned int i;
137   struct cleanup *back_to;
138   
139   storage_needed = get_symtab_upper_bound (abfd);
140
141   if (storage_needed > 0)
142     {
143       symbol_table = (asymbol **) bfd_xmalloc (storage_needed);
144       back_to = make_cleanup (free, symbol_table);
145       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
146   
147       for (i = 0; i < number_of_symbols; i++)
148         {
149           sym = *symbol_table++;
150           /* Select global symbols that are defined in a specific section
151              or are absolute. */
152           if (sym -> flags & BSF_GLOBAL
153               && ((sym -> section != NULL) || (sym -> flags & BSF_ABSOLUTE)))
154             {
155               record_misc_function ((char *) sym -> name, sym -> value);
156             }
157         }
158       do_cleanups (back_to);
159     }
160 }
161
162 /* Scan and build partial symbols for a symbol file.
163    We have been initialized by a call to elf_symfile_init, which 
164    currently does nothing.
165
166    ADDR is the address relative to which the symbols in it are (e.g.
167    the base address of the text segment).
168
169    MAINLINE is true if we are reading the main symbol
170    table (as opposed to a shared lib or dynamically loaded file).
171
172    This function only does the minimum work necessary for letting the
173    user "name" things symbolically; it does not read the entire symtab.
174    Instead, it reads the external and static symbols and puts them in partial
175    symbol tables.  When more extensive information is requested of a
176    file, the corresponding partial symbol table is mutated into a full
177    fledged symbol table by going back and reading the symbols
178    for real.  The function dwarf_psymtab_to_symtab() is the function that
179    does this for DWARF symbols.
180
181    Note that ELF files have a "minimal" symbol table, which looks a lot
182    like a COFF symbol table, but has only the minimal information necessary
183    for linking.  We process this also, and just use the information to
184    add to the misc function vector.  This gives us some minimal debugging
185    capability even for files compiled without -g.
186  */
187
188 static void
189 DEFUN(elf_symfile_read, (sf, addr, mainline),
190       struct sym_fns *sf AND
191       CORE_ADDR addr AND
192       int mainline)
193 {
194   bfd *abfd = sf->objfile->obfd;
195   struct elfinfo ei;
196   struct cleanup *back_to;
197
198   init_misc_bunches ();
199   back_to = make_cleanup (discard_misc_bunches, 0);
200
201   /* Process the normal ELF symbol table first. */
202
203   elf_symtab_read (abfd, addr);
204
205   /* Now process the DWARF debugging information, which is contained in
206      special ELF sections.  We first have to find them... */
207
208   (void) memset ((char *) &ei, 0, sizeof (ei));
209   bfd_map_over_sections (abfd, elf_locate_sections, &ei);
210   if (ei.dboffset && ei.lnoffset)
211     {
212       addr = 0; /* FIXME: force address base to zero for now */
213       dwarf_build_psymtabs (fileno ((FILE *)(abfd -> iostream)),
214                             bfd_get_filename (abfd),
215                             addr, mainline,
216                             ei.dboffset, ei.dbsize,
217                             ei.lnoffset, ei.lnsize, sf->objfile);
218     }
219
220   if (!partial_symtab_list)
221     {
222       wrap_here ("");
223       printf_filtered ("(no debugging symbols found)...");
224       wrap_here ("");
225     }
226
227   /* Go over the miscellaneous functions and install them in the
228      miscellaneous function vector. */
229   
230   condense_misc_bunches (!mainline);
231   do_cleanups (back_to);
232 }
233
234 /* Initialize anything that needs initializing when a completely new symbol
235    file is specified (not just adding some symbols from another file, e.g. a
236    shared library).
237
238    For now at least, we have nothing in particular to do, so this function is
239    just a stub. */
240
241 static void
242 DEFUN_VOID (elf_new_init)
243 {
244 }
245
246 /* ELF specific initialization routine for reading symbols.
247
248    It is passed a pointer to a struct sym_fns which contains, among other
249    things, the BFD for the file whose symbols are being read, and a slot for
250    a pointer to "private data" which we can fill with goodies.
251
252    For now at least, we have nothing in particular to do, so this function is
253    just a stub. */
254
255 static void
256 DEFUN(elf_symfile_init, (sf),
257       struct sym_fns *sf)
258 {
259 }
260
261 \f
262 /*  Register that we are able to handle ELF object file formats and DWARF
263     debugging formats.
264
265     Unlike other object file formats, where the debugging information format
266     is implied by the object file format, the ELF object file format and the
267     DWARF debugging information format are two distinct, and potentially
268     separate entities.  I.E. it is perfectly possible to have ELF objects
269     with debugging formats other than DWARF.  And it is conceivable that the
270     DWARF debugging format might be used with another object file format,
271     like COFF, by simply using COFF's custom section feature.
272
273     GDB, and to a lesser extent BFD, should support the notion of separate
274     object file formats and debugging information formats.  For now, we just
275     use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
276     object file format and the DWARF debugging format. */
277
278 static struct sym_fns elf_sym_fns = {
279   "elf",                /* sym_name: name or name prefix of BFD target type */
280   3,                    /* sym_namelen: number of significant sym_name chars */
281   elf_new_init,         /* sym_new_init: init anything gbl to entire symtab */
282   elf_symfile_init,     /* sym_init: read initial info, setup for sym_read() */
283   elf_symfile_read,     /* sym_read: read a symbol file into symtab */
284   NULL,                 /* sym_bfd: accessor for symbol file being read */
285   NULL,                 /* sym_private: sym_init & sym_read shared info */
286   NULL                  /* next: pointer to next struct sym_fns */
287 };
288
289 void
290 DEFUN_VOID (_initialize_elfread)
291 {
292   add_symtab_fns (&elf_sym_fns);
293 }
This page took 0.039692 seconds and 4 git commands to generate.