,
[deliverable/binutils-gdb.git] / gdb / elfread.c
CommitLineData
35f5886e 1/* Read ELF (Executable and Linking Format) object files for GDB.
c2e4669f 2 Copyright 1991, 1992 Free Software Foundation, Inc.
35f5886e
FF
3 Written by Fred Fish at Cygnus Support.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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 *
c2e4669f 27 * to Fred Fish at Cygnus Support (fnf@cygnus.com) *
35f5886e
FF
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. *
35f5886e
FF
32 * *
33 ************************************************************************/
34
35f5886e 35#include "defs.h"
f5f0679a
SC
36#include "elf/common.h"
37#include "elf/external.h"
38#include "elf/internal.h"
35f5886e 39#include "bfd.h"
f70be3e4 40#include "libbfd.h" /* For bfd_elf_find_section */
35f5886e 41#include "symtab.h"
1ab3bf1b 42#include "symfile.h"
5e2e79f8 43#include "objfiles.h"
be772100 44#include "buildsym.h"
a048c8f5 45
35f5886e
FF
46#define STREQ(a,b) (strcmp((a),(b))==0)
47
48struct 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 */
346168a2
JG
53 asection *stabsect; /* Section pointer for .stab section */
54 asection *stabindexsect; /* Section pointer for .stab.index section */
35f5886e
FF
55};
56
1ab3bf1b 57static void
80d68b1d 58elf_symfile_init PARAMS ((struct objfile *));
1ab3bf1b
JG
59
60static void
80d68b1d 61elf_new_init PARAMS ((struct objfile *));
1ab3bf1b
JG
62
63static void
80d68b1d
FF
64elf_symfile_read PARAMS ((struct objfile *, CORE_ADDR, int));
65
66static void
67elf_symfile_finish PARAMS ((struct objfile *));
1ab3bf1b
JG
68
69static void
be772100 70elf_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *));
1ab3bf1b 71
51b57ded 72#if 0
1ab3bf1b
JG
73static void
74record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,
75 struct objfile *));
51b57ded
FF
76#endif
77
78static void
79record_minimal_symbol_and_info PARAMS ((char *, CORE_ADDR,
80 enum minimal_symbol_type, char *,
81 struct objfile *));
1ab3bf1b
JG
82
83static void
84elf_locate_sections PARAMS ((bfd *, asection *, PTR));
85
35f5886e
FF
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
346168a2
JG
97 We also recognize the ".stab" sections used by the Sun compilers
98 released with Solaris 2.
99
35f5886e
FF
100 FIXME: The section names should not be hardwired strings. */
101
102static void
be772100
JG
103elf_locate_sections (ignore_abfd, sectp, eip)
104 bfd *ignore_abfd;
1ab3bf1b
JG
105 asection *sectp;
106 PTR eip;
35f5886e 107{
1ab3bf1b
JG
108 register struct elfinfo *ei;
109
110 ei = (struct elfinfo *) eip;
35f5886e
FF
111 if (STREQ (sectp -> name, ".debug"))
112 {
113 ei -> dboffset = sectp -> filepos;
e5849334 114 ei -> dbsize = bfd_get_section_size_before_reloc (sectp);
35f5886e
FF
115 }
116 else if (STREQ (sectp -> name, ".line"))
117 {
118 ei -> lnoffset = sectp -> filepos;
e5849334 119 ei -> lnsize = bfd_get_section_size_before_reloc (sectp);
35f5886e 120 }
346168a2
JG
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 }
35f5886e
FF
129}
130
1ab3bf1b
JG
131#if 0 /* Currently unused */
132
f8b76e70 133char *
1ab3bf1b
JG
134elf_interpreter (abfd)
135 bfd *abfd;
f8b76e70
FF
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
1ab3bf1b
JG
159#endif
160
a7446af6
FF
161/*
162
163LOCAL FUNCTION
164
1ab3bf1b 165 record_minimal_symbol -- add entry to minimal symbol table
a7446af6
FF
166
167SYNOPSIS
168
1ab3bf1b 169 static void record_minimal_symbol (char *name, CORE_ADDR address)
a7446af6
FF
170
171DESCRIPTION
172
173 Given a pointer to the name of a symbol that should be added to the
1ab3bf1b
JG
174 minimal symbol table and the address associated with that symbol, records
175 this information for later use in building the minimal symbol table.
a7446af6 176
a7446af6
FF
177 */
178
51b57ded
FF
179#if 0 /* FIXME: Unused */
180
a7446af6 181static void
1ab3bf1b
JG
182record_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;
a7446af6 187{
1ab3bf1b
JG
188 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
189 prim_record_minimal_symbol (name, address, ms_type);
a7446af6
FF
190}
191
51b57ded
FF
192#endif
193
346168a2
JG
194static void
195record_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
f8b76e70
FF
206/*
207
208LOCAL FUNCTION
209
210 elf_symtab_read -- read the symbol table of an ELF file
211
212SYNOPSIS
213
be772100 214 void elf_symtab_read (bfd *abfd, CORE_ADDR addr,
1ab3bf1b 215 struct objfile *objfile)
f8b76e70
FF
216
217DESCRIPTION
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
1ab3bf1b 222 function and data symbols to the minimal symbol table.
f8b76e70
FF
223
224*/
225
a7446af6 226static void
be772100 227elf_symtab_read (abfd, addr, objfile)
1ab3bf1b
JG
228 bfd *abfd;
229 CORE_ADDR addr;
1ab3bf1b 230 struct objfile *objfile;
a7446af6
FF
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;
f8b76e70 238 CORE_ADDR symaddr;
1ab3bf1b 239 enum minimal_symbol_type ms_type;
a7446af6
FF
240
241 storage_needed = get_symtab_upper_bound (abfd);
242
243 if (storage_needed > 0)
244 {
3b0b9220 245 symbol_table = (asymbol **) xmalloc (storage_needed);
a7446af6
FF
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++;
d35bf52d
FF
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))
a7446af6 257 {
f8b76e70 258 symaddr = sym -> value;
c2e4669f
JG
259 /* Relocate all non-absolute symbols by base address. */
260 if (sym -> section != &bfd_abs_section)
f8b76e70 261 {
f8b76e70
FF
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. */
d35bf52d 267 if (sym -> section -> flags & SEC_CODE)
f8b76e70 268 {
1ab3bf1b 269 ms_type = mst_text;
f8b76e70 270 }
d35bf52d 271 else if (sym -> section -> flags & SEC_DATA)
f8b76e70 272 {
1ab3bf1b 273 ms_type = mst_data;
f8b76e70
FF
274 }
275 else
276 {
1ab3bf1b 277 ms_type = mst_unknown;
f8b76e70 278 }
346168a2
JG
279 /* Pass symbol size field in via BFD. FIXME!!! */
280 record_minimal_symbol_and_info ((char *) sym -> name,
281 symaddr, ms_type, sym->udata, objfile);
a7446af6
FF
282 }
283 }
284 do_cleanups (back_to);
285 }
286}
287
35f5886e
FF
288/* Scan and build partial symbols for a symbol file.
289 We have been initialized by a call to elf_symfile_init, which
290 currently does nothing.
291
292 ADDR is the address relative to which the symbols in it are (e.g.
293 the base address of the text segment).
294
295 MAINLINE is true if we are reading the main symbol
296 table (as opposed to a shared lib or dynamically loaded file).
297
298 This function only does the minimum work necessary for letting the
299 user "name" things symbolically; it does not read the entire symtab.
300 Instead, it reads the external and static symbols and puts them in partial
301 symbol tables. When more extensive information is requested of a
302 file, the corresponding partial symbol table is mutated into a full
303 fledged symbol table by going back and reading the symbols
346168a2
JG
304 for real.
305
306 We look for sections with specific names, to tell us what debug
307 format to look for: FIXME!!!
308
309 dwarf_build_psymtabs() builds psymtabs for DWARF symbols;
310 elfstab_build_psymtabs() handles STABS symbols.
a7446af6
FF
311
312 Note that ELF files have a "minimal" symbol table, which looks a lot
313 like a COFF symbol table, but has only the minimal information necessary
346168a2
JG
314 for linking. We process this also, and use the information to
315 build gdb's minimal symbol table. This gives us some minimal debugging
316 capability even for files compiled without -g. */
35f5886e
FF
317
318static void
80d68b1d
FF
319elf_symfile_read (objfile, addr, mainline)
320 struct objfile *objfile;
1ab3bf1b
JG
321 CORE_ADDR addr;
322 int mainline;
35f5886e 323{
80d68b1d 324 bfd *abfd = objfile->obfd;
35f5886e 325 struct elfinfo ei;
a7446af6 326 struct cleanup *back_to;
6b801388 327 asection *text_sect;
346168a2 328 CORE_ADDR offset;
35f5886e 329
1ab3bf1b
JG
330 init_minimal_symbol_collection ();
331 back_to = make_cleanup (discard_minimal_symbols, 0);
a7446af6 332
6b801388
FF
333 /* Compute the amount to relocate all symbols by. The value passed in
334 as ADDR is typically either the actual address of the text section,
335 or a user specified address. By subtracting off the actual address
336 of the text section, we can compute the relocation amount. */
337
338 text_sect = bfd_get_section_by_name (objfile -> obfd, ".text");
346168a2 339 offset = addr - bfd_section_vma (objfile -> obfd, text_sect);
6b801388 340
a7446af6
FF
341 /* Process the normal ELF symbol table first. */
342
346168a2 343 elf_symtab_read (abfd, offset, objfile);
a7446af6 344
346168a2 345 /* Now process debugging information, which is contained in
a7446af6
FF
346 special ELF sections. We first have to find them... */
347
348 (void) memset ((char *) &ei, 0, sizeof (ei));
1ab3bf1b 349 bfd_map_over_sections (abfd, elf_locate_sections, (PTR) &ei);
35f5886e
FF
350 if (ei.dboffset && ei.lnoffset)
351 {
346168a2 352 /* DWARF sections */
35f5886e
FF
353 dwarf_build_psymtabs (fileno ((FILE *)(abfd -> iostream)),
354 bfd_get_filename (abfd),
346168a2 355 offset, mainline,
35f5886e 356 ei.dboffset, ei.dbsize,
80d68b1d 357 ei.lnoffset, ei.lnsize, objfile);
35f5886e 358 }
346168a2
JG
359 if (ei.stabsect)
360 {
361 /* STABS sections */
362
363 /* FIXME: Sun didn't really know how to implement this well.
364 They made .stab sections that don't point to the .stabstr
365 section with the sh_link field. BFD doesn't make string table
366 sections visible to the caller. So we have to search the
367 ELF section table, not the BFD section table, for the string
368 table. */
f70be3e4 369 struct elf_internal_shdr *elf_sect;
346168a2 370
f70be3e4 371 elf_sect = bfd_elf_find_section (abfd, ".stabstr");
346168a2
JG
372 if (elf_sect)
373 elfstab_build_psymtabs (objfile,
374 addr, /* We really pass the text seg addr, not the offset, here. */
375 mainline,
376 ei.stabsect->filepos, /* .stab offset */
377 bfd_get_section_size_before_reloc (ei.stabsect),/* .stab size */
378 elf_sect->sh_offset, /* .stabstr offset */
379 elf_sect->sh_size); /* .stabstr size */
380 }
a7446af6 381
1ab3bf1b 382 if (!have_partial_symbols ())
35f5886e
FF
383 {
384 wrap_here ("");
385 printf_filtered ("(no debugging symbols found)...");
386 wrap_here ("");
387 }
a7446af6 388
1ab3bf1b
JG
389 /* Install any minimal symbols that have been collected as the current
390 minimal symbols for this objfile. */
391
80d68b1d 392 install_minimal_symbols (objfile);
1ab3bf1b 393
a7446af6 394 do_cleanups (back_to);
35f5886e
FF
395}
396
397/* Initialize anything that needs initializing when a completely new symbol
398 file is specified (not just adding some symbols from another file, e.g. a
399 shared library).
400
346168a2 401 We reinitialize buildsym, since we may be reading stabs from an ELF file. */
35f5886e
FF
402
403static void
be772100
JG
404elf_new_init (ignore)
405 struct objfile *ignore;
80d68b1d
FF
406{
407 buildsym_new_init ();
408}
409
410/* Perform any local cleanups required when we are done with a particular
411 objfile. I.E, we are in the process of discarding all symbol information
412 for an objfile, freeing up all memory held for it, and unlinking the
413 objfile struct from the global list of known objfiles. */
414
415static void
416elf_symfile_finish (objfile)
417 struct objfile *objfile;
35f5886e 418{
80d68b1d
FF
419 if (objfile -> sym_private != NULL)
420 {
421 mfree (objfile -> md, objfile -> sym_private);
422 }
35f5886e
FF
423}
424
425/* ELF specific initialization routine for reading symbols.
426
427 It is passed a pointer to a struct sym_fns which contains, among other
428 things, the BFD for the file whose symbols are being read, and a slot for
429 a pointer to "private data" which we can fill with goodies.
430
431 For now at least, we have nothing in particular to do, so this function is
432 just a stub. */
433
434static void
be772100
JG
435elf_symfile_init (ignore)
436 struct objfile *ignore;
35f5886e
FF
437{
438}
439
440\f
441/* Register that we are able to handle ELF object file formats and DWARF
442 debugging formats.
443
444 Unlike other object file formats, where the debugging information format
445 is implied by the object file format, the ELF object file format and the
446 DWARF debugging information format are two distinct, and potentially
447 separate entities. I.E. it is perfectly possible to have ELF objects
448 with debugging formats other than DWARF. And it is conceivable that the
449 DWARF debugging format might be used with another object file format,
450 like COFF, by simply using COFF's custom section feature.
451
452 GDB, and to a lesser extent BFD, should support the notion of separate
453 object file formats and debugging information formats. For now, we just
454 use "elf" in the same sense as "a.out" or "coff", to imply both the ELF
455 object file format and the DWARF debugging format. */
456
80d68b1d
FF
457static struct sym_fns elf_sym_fns =
458{
35f5886e
FF
459 "elf", /* sym_name: name or name prefix of BFD target type */
460 3, /* sym_namelen: number of significant sym_name chars */
461 elf_new_init, /* sym_new_init: init anything gbl to entire symtab */
462 elf_symfile_init, /* sym_init: read initial info, setup for sym_read() */
463 elf_symfile_read, /* sym_read: read a symbol file into symtab */
80d68b1d 464 elf_symfile_finish, /* sym_finish: finished with file, cleanup */
35f5886e
FF
465 NULL /* next: pointer to next struct sym_fns */
466};
467
468void
1ab3bf1b 469_initialize_elfread ()
35f5886e
FF
470{
471 add_symtab_fns (&elf_sym_fns);
472}
This page took 0.075378 seconds and 4 git commands to generate.