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