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