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