* xm-sun3os4.h, xm-sun4os4.h: Enable HAVE_MMAP.
[deliverable/binutils-gdb.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 * *
33 ************************************************************************/
34
35 #include <stdio.h>
36
37 #include "defs.h"
38 #include "elf/common.h"
39 #include "elf/external.h"
40 #include "elf/internal.h"
41 #include "bfd.h"
42 #include "symtab.h"
43 #include "symfile.h"
44
45 #define STREQ(a,b) (strcmp((a),(b))==0)
46
47 struct elfinfo {
48 unsigned int dboffset; /* Offset to dwarf debug section */
49 unsigned int dbsize; /* Size of dwarf debug section */
50 unsigned int lnoffset; /* Offset to dwarf line number section */
51 unsigned int lnsize; /* Size of dwarf line number section */
52 };
53
54 static void
55 elf_symfile_init PARAMS ((struct sym_fns *));
56
57 static void
58 elf_new_init PARAMS ((void));
59
60 static void
61 elf_symfile_read PARAMS ((struct sym_fns *, CORE_ADDR, int));
62
63 static void
64 elf_symtab_read PARAMS ((bfd *, CORE_ADDR, int, struct objfile *));
65
66 static void
67 record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,
68 struct objfile *));
69
70 static void
71 elf_locate_sections PARAMS ((bfd *, asection *, PTR));
72
73 /* We are called once per section from elf_symfile_read. We
74 need to examine each section we are passed, check to see
75 if it is something we are interested in processing, and
76 if so, stash away some access information for the section.
77
78 For now we recognize the dwarf debug information sections and
79 line number sections from matching their section names. The
80 ELF definition is no real help here since it has no direct
81 knowledge of DWARF (by design, so any debugging format can be
82 used).
83
84 FIXME: The section names should not be hardwired strings. */
85
86 static void
87 elf_locate_sections (abfd, sectp, eip)
88 bfd *abfd;
89 asection *sectp;
90 PTR eip;
91 {
92 register struct elfinfo *ei;
93
94 ei = (struct elfinfo *) eip;
95 if (STREQ (sectp -> name, ".debug"))
96 {
97 ei -> dboffset = sectp -> filepos;
98 ei -> dbsize = bfd_get_section_size_before_reloc (sectp);
99 }
100 else if (STREQ (sectp -> name, ".line"))
101 {
102 ei -> lnoffset = sectp -> filepos;
103 ei -> lnsize = bfd_get_section_size_before_reloc (sectp);
104 }
105 }
106
107 #if 0 /* Currently unused */
108
109 char *
110 elf_interpreter (abfd)
111 bfd *abfd;
112 {
113 sec_ptr interp_sec;
114 unsigned size;
115 char *interp = NULL;
116
117 interp_sec = bfd_get_section_by_name (abfd, ".interp");
118 if (interp_sec)
119 {
120 size = bfd_section_size (abfd, interp_sec);
121 interp = alloca (size);
122 if (bfd_get_section_contents (abfd, interp_sec, interp, (file_ptr)0,
123 size))
124 {
125 interp = savestring (interp, size - 1);
126 }
127 else
128 {
129 interp = NULL;
130 }
131 }
132 return (interp);
133 }
134
135 #endif
136
137 /*
138
139 LOCAL FUNCTION
140
141 record_minimal_symbol -- add entry to minimal symbol table
142
143 SYNOPSIS
144
145 static void record_minimal_symbol (char *name, CORE_ADDR address)
146
147 DESCRIPTION
148
149 Given a pointer to the name of a symbol that should be added to the
150 minimal symbol table and the address associated with that symbol, records
151 this information for later use in building the minimal symbol table.
152
153 */
154
155 static void
156 record_minimal_symbol (name, address, ms_type, objfile)
157 char *name;
158 CORE_ADDR address;
159 enum minimal_symbol_type ms_type;
160 struct objfile *objfile;
161 {
162 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
163 prim_record_minimal_symbol (name, address, ms_type);
164 }
165
166 /*
167
168 LOCAL FUNCTION
169
170 elf_symtab_read -- read the symbol table of an ELF file
171
172 SYNOPSIS
173
174 void elf_symtab_read (bfd *abfd, CORE_ADDR addr, int mainline,
175 struct objfile *objfile)
176
177 DESCRIPTION
178
179 Given an open bfd, a base address to relocate symbols to, and a
180 flag that specifies whether or not this bfd is for an executable
181 or not (may be shared library for example), add all the global
182 function and data symbols to the minimal symbol table.
183
184 */
185
186 static void
187 elf_symtab_read (abfd, addr, mainline, objfile)
188 bfd *abfd;
189 CORE_ADDR addr;
190 int mainline;
191 struct objfile *objfile;
192 {
193 unsigned int storage_needed;
194 asymbol *sym;
195 asymbol **symbol_table;
196 unsigned int number_of_symbols;
197 unsigned int i;
198 struct cleanup *back_to;
199 CORE_ADDR symaddr;
200 enum minimal_symbol_type ms_type;
201
202 storage_needed = get_symtab_upper_bound (abfd);
203
204 if (storage_needed > 0)
205 {
206 symbol_table = (asymbol **) bfd_xmalloc (storage_needed);
207 back_to = make_cleanup (free, symbol_table);
208 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
209
210 for (i = 0; i < number_of_symbols; i++)
211 {
212 sym = *symbol_table++;
213 /* Select global/weak symbols that are defined in a specific section.
214 Note that bfd now puts abs symbols in their own section, so
215 all symbols we are interested in will have a section. */
216 if ((sym -> flags & (BSF_GLOBAL | BSF_WEAK))
217 && (sym -> section != NULL))
218 {
219 symaddr = sym -> value;
220 /* Relocate all non-absolute symbols by base address.
221 FIXME: Can we eliminate the check for mainline now,
222 since shouldn't addr be 0 in this case? */
223 if (!mainline && (sym -> section != &bfd_abs_section))
224 {
225 symaddr += addr;
226 }
227 /* For non-absolute symbols, use the type of the section
228 they are relative to, to intuit text/data. Bfd provides
229 no way of figuring this out for absolute symbols. */
230 if (sym -> section -> flags & SEC_CODE)
231 {
232 ms_type = mst_text;
233 }
234 else if (sym -> section -> flags & SEC_DATA)
235 {
236 ms_type = mst_data;
237 }
238 else
239 {
240 ms_type = mst_unknown;
241 }
242 record_minimal_symbol ((char *) sym -> name, symaddr, ms_type, objfile);
243 }
244 }
245 do_cleanups (back_to);
246 }
247 }
248
249 /* Scan and build partial symbols for a symbol file.
250 We have been initialized by a call to elf_symfile_init, which
251 currently does nothing.
252
253 ADDR is the address relative to which the symbols in it are (e.g.
254 the base address of the text segment).
255
256 MAINLINE is true if we are reading the main symbol
257 table (as opposed to a shared lib or dynamically loaded file).
258
259 This function only does the minimum work necessary for letting the
260 user "name" things symbolically; it does not read the entire symtab.
261 Instead, it reads the external and static symbols and puts them in partial
262 symbol tables. When more extensive information is requested of a
263 file, the corresponding partial symbol table is mutated into a full
264 fledged symbol table by going back and reading the symbols
265 for real. The function dwarf_psymtab_to_symtab() is the function that
266 does this for DWARF symbols.
267
268 Note that ELF files have a "minimal" symbol table, which looks a lot
269 like a COFF symbol table, but has only the minimal information necessary
270 for linking. We process this also, and just use the information to
271 add to gdb's minimal symbol table. This gives us some minimal debugging
272 capability even for files compiled without -g.
273 */
274
275 static void
276 elf_symfile_read (sf, addr, mainline)
277 struct sym_fns *sf;
278 CORE_ADDR addr;
279 int mainline;
280 {
281 bfd *abfd = sf->objfile->obfd;
282 struct elfinfo ei;
283 struct cleanup *back_to;
284
285 init_minimal_symbol_collection ();
286 back_to = make_cleanup (discard_minimal_symbols, 0);
287
288 /* Process the normal ELF symbol table first. */
289
290 elf_symtab_read (abfd, addr, mainline, sf->objfile);
291
292 /* Now process the DWARF debugging information, which is contained in
293 special ELF sections. We first have to find them... */
294
295 (void) memset ((char *) &ei, 0, sizeof (ei));
296 bfd_map_over_sections (abfd, elf_locate_sections, (PTR) &ei);
297 if (ei.dboffset && ei.lnoffset)
298 {
299 dwarf_build_psymtabs (fileno ((FILE *)(abfd -> iostream)),
300 bfd_get_filename (abfd),
301 addr, mainline,
302 ei.dboffset, ei.dbsize,
303 ei.lnoffset, ei.lnsize, sf->objfile);
304 }
305
306 if (!have_partial_symbols ())
307 {
308 wrap_here ("");
309 printf_filtered ("(no debugging symbols found)...");
310 wrap_here ("");
311 }
312
313 /* Install any minimal symbols that have been collected as the current
314 minimal symbols for this objfile. */
315
316 install_minimal_symbols (sf -> objfile);
317
318 do_cleanups (back_to);
319 }
320
321 /* Initialize anything that needs initializing when a completely new symbol
322 file is specified (not just adding some symbols from another file, e.g. a
323 shared library).
324
325 For now at least, we have nothing in particular to do, so this function is
326 just a stub. */
327
328 static void
329 elf_new_init ()
330 {
331 }
332
333 /* ELF specific initialization routine for reading symbols.
334
335 It is passed a pointer to a struct sym_fns which contains, among other
336 things, the BFD for the file whose symbols are being read, and a slot for
337 a pointer to "private data" which we can fill with goodies.
338
339 For now at least, we have nothing in particular to do, so this function is
340 just a stub. */
341
342 static void
343 elf_symfile_init (sf)
344 struct sym_fns *sf;
345 {
346 }
347
348 \f
349 /* Register that we are able to handle ELF object file formats and DWARF
350 debugging formats.
351
352 Unlike other object file formats, where the debugging information format
353 is implied by the object file format, the ELF object file format and the
354 DWARF debugging information format are two distinct, and potentially
355 separate entities. I.E. it is perfectly possible to have ELF objects
356 with debugging formats other than DWARF. And it is conceivable that the
357 DWARF debugging format might be used with another object file format,
358 like COFF, by simply using COFF's custom section feature.
359
360 GDB, and to a lesser extent BFD, should support the notion of separate
361 object file formats and debugging information formats. For now, we just
362 use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
363 object file format and the DWARF debugging format. */
364
365 static struct sym_fns elf_sym_fns = {
366 "elf", /* sym_name: name or name prefix of BFD target type */
367 3, /* sym_namelen: number of significant sym_name chars */
368 elf_new_init, /* sym_new_init: init anything gbl to entire symtab */
369 elf_symfile_init, /* sym_init: read initial info, setup for sym_read() */
370 elf_symfile_read, /* sym_read: read a symbol file into symtab */
371 NULL, /* sym_bfd: accessor for symbol file being read */
372 NULL, /* sym_private: sym_init & sym_read shared info */
373 NULL /* next: pointer to next struct sym_fns */
374 };
375
376 void
377 _initialize_elfread ()
378 {
379 add_symtab_fns (&elf_sym_fns);
380 }
This page took 0.036388 seconds and 4 git commands to generate.