syms.c revision 1.1.1.1 1 1.1 skrll /* Generic symbol-table support for the BFD library.
2 1.1 skrll Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 1.1 skrll 2000, 2001, 2002, 2003, 2004, 2007
4 1.1 skrll Free Software Foundation, Inc.
5 1.1 skrll Written by Cygnus Support.
6 1.1 skrll
7 1.1 skrll This file is part of BFD, the Binary File Descriptor library.
8 1.1 skrll
9 1.1 skrll This program is free software; you can redistribute it and/or modify
10 1.1 skrll it under the terms of the GNU General Public License as published by
11 1.1 skrll the Free Software Foundation; either version 3 of the License, or
12 1.1 skrll (at your option) any later version.
13 1.1 skrll
14 1.1 skrll This program is distributed in the hope that it will be useful,
15 1.1 skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
16 1.1 skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 1.1 skrll GNU General Public License for more details.
18 1.1 skrll
19 1.1 skrll You should have received a copy of the GNU General Public License
20 1.1 skrll along with this program; if not, write to the Free Software
21 1.1 skrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 1.1 skrll MA 02110-1301, USA. */
23 1.1 skrll
24 1.1 skrll /*
25 1.1 skrll SECTION
26 1.1 skrll Symbols
27 1.1 skrll
28 1.1 skrll BFD tries to maintain as much symbol information as it can when
29 1.1 skrll it moves information from file to file. BFD passes information
30 1.1 skrll to applications though the <<asymbol>> structure. When the
31 1.1 skrll application requests the symbol table, BFD reads the table in
32 1.1 skrll the native form and translates parts of it into the internal
33 1.1 skrll format. To maintain more than the information passed to
34 1.1 skrll applications, some targets keep some information ``behind the
35 1.1 skrll scenes'' in a structure only the particular back end knows
36 1.1 skrll about. For example, the coff back end keeps the original
37 1.1 skrll symbol table structure as well as the canonical structure when
38 1.1 skrll a BFD is read in. On output, the coff back end can reconstruct
39 1.1 skrll the output symbol table so that no information is lost, even
40 1.1 skrll information unique to coff which BFD doesn't know or
41 1.1 skrll understand. If a coff symbol table were read, but were written
42 1.1 skrll through an a.out back end, all the coff specific information
43 1.1 skrll would be lost. The symbol table of a BFD
44 1.1 skrll is not necessarily read in until a canonicalize request is
45 1.1 skrll made. Then the BFD back end fills in a table provided by the
46 1.1 skrll application with pointers to the canonical information. To
47 1.1 skrll output symbols, the application provides BFD with a table of
48 1.1 skrll pointers to pointers to <<asymbol>>s. This allows applications
49 1.1 skrll like the linker to output a symbol as it was read, since the ``behind
50 1.1 skrll the scenes'' information will be still available.
51 1.1 skrll @menu
52 1.1 skrll @* Reading Symbols::
53 1.1 skrll @* Writing Symbols::
54 1.1 skrll @* Mini Symbols::
55 1.1 skrll @* typedef asymbol::
56 1.1 skrll @* symbol handling functions::
57 1.1 skrll @end menu
58 1.1 skrll
59 1.1 skrll INODE
60 1.1 skrll Reading Symbols, Writing Symbols, Symbols, Symbols
61 1.1 skrll SUBSECTION
62 1.1 skrll Reading symbols
63 1.1 skrll
64 1.1 skrll There are two stages to reading a symbol table from a BFD:
65 1.1 skrll allocating storage, and the actual reading process. This is an
66 1.1 skrll excerpt from an application which reads the symbol table:
67 1.1 skrll
68 1.1 skrll | long storage_needed;
69 1.1 skrll | asymbol **symbol_table;
70 1.1 skrll | long number_of_symbols;
71 1.1 skrll | long i;
72 1.1 skrll |
73 1.1 skrll | storage_needed = bfd_get_symtab_upper_bound (abfd);
74 1.1 skrll |
75 1.1 skrll | if (storage_needed < 0)
76 1.1 skrll | FAIL
77 1.1 skrll |
78 1.1 skrll | if (storage_needed == 0)
79 1.1 skrll | return;
80 1.1 skrll |
81 1.1 skrll | symbol_table = xmalloc (storage_needed);
82 1.1 skrll | ...
83 1.1 skrll | number_of_symbols =
84 1.1 skrll | bfd_canonicalize_symtab (abfd, symbol_table);
85 1.1 skrll |
86 1.1 skrll | if (number_of_symbols < 0)
87 1.1 skrll | FAIL
88 1.1 skrll |
89 1.1 skrll | for (i = 0; i < number_of_symbols; i++)
90 1.1 skrll | process_symbol (symbol_table[i]);
91 1.1 skrll
92 1.1 skrll All storage for the symbols themselves is in an objalloc
93 1.1 skrll connected to the BFD; it is freed when the BFD is closed.
94 1.1 skrll
95 1.1 skrll INODE
96 1.1 skrll Writing Symbols, Mini Symbols, Reading Symbols, Symbols
97 1.1 skrll SUBSECTION
98 1.1 skrll Writing symbols
99 1.1 skrll
100 1.1 skrll Writing of a symbol table is automatic when a BFD open for
101 1.1 skrll writing is closed. The application attaches a vector of
102 1.1 skrll pointers to pointers to symbols to the BFD being written, and
103 1.1 skrll fills in the symbol count. The close and cleanup code reads
104 1.1 skrll through the table provided and performs all the necessary
105 1.1 skrll operations. The BFD output code must always be provided with an
106 1.1 skrll ``owned'' symbol: one which has come from another BFD, or one
107 1.1 skrll which has been created using <<bfd_make_empty_symbol>>. Here is an
108 1.1 skrll example showing the creation of a symbol table with only one element:
109 1.1 skrll
110 1.1 skrll | #include "bfd.h"
111 1.1 skrll | int main (void)
112 1.1 skrll | {
113 1.1 skrll | bfd *abfd;
114 1.1 skrll | asymbol *ptrs[2];
115 1.1 skrll | asymbol *new;
116 1.1 skrll |
117 1.1 skrll | abfd = bfd_openw ("foo","a.out-sunos-big");
118 1.1 skrll | bfd_set_format (abfd, bfd_object);
119 1.1 skrll | new = bfd_make_empty_symbol (abfd);
120 1.1 skrll | new->name = "dummy_symbol";
121 1.1 skrll | new->section = bfd_make_section_old_way (abfd, ".text");
122 1.1 skrll | new->flags = BSF_GLOBAL;
123 1.1 skrll | new->value = 0x12345;
124 1.1 skrll |
125 1.1 skrll | ptrs[0] = new;
126 1.1 skrll | ptrs[1] = 0;
127 1.1 skrll |
128 1.1 skrll | bfd_set_symtab (abfd, ptrs, 1);
129 1.1 skrll | bfd_close (abfd);
130 1.1 skrll | return 0;
131 1.1 skrll | }
132 1.1 skrll |
133 1.1 skrll | ./makesym
134 1.1 skrll | nm foo
135 1.1 skrll | 00012345 A dummy_symbol
136 1.1 skrll
137 1.1 skrll Many formats cannot represent arbitrary symbol information; for
138 1.1 skrll instance, the <<a.out>> object format does not allow an
139 1.1 skrll arbitrary number of sections. A symbol pointing to a section
140 1.1 skrll which is not one of <<.text>>, <<.data>> or <<.bss>> cannot
141 1.1 skrll be described.
142 1.1 skrll
143 1.1 skrll INODE
144 1.1 skrll Mini Symbols, typedef asymbol, Writing Symbols, Symbols
145 1.1 skrll SUBSECTION
146 1.1 skrll Mini Symbols
147 1.1 skrll
148 1.1 skrll Mini symbols provide read-only access to the symbol table.
149 1.1 skrll They use less memory space, but require more time to access.
150 1.1 skrll They can be useful for tools like nm or objdump, which may
151 1.1 skrll have to handle symbol tables of extremely large executables.
152 1.1 skrll
153 1.1 skrll The <<bfd_read_minisymbols>> function will read the symbols
154 1.1 skrll into memory in an internal form. It will return a <<void *>>
155 1.1 skrll pointer to a block of memory, a symbol count, and the size of
156 1.1 skrll each symbol. The pointer is allocated using <<malloc>>, and
157 1.1 skrll should be freed by the caller when it is no longer needed.
158 1.1 skrll
159 1.1 skrll The function <<bfd_minisymbol_to_symbol>> will take a pointer
160 1.1 skrll to a minisymbol, and a pointer to a structure returned by
161 1.1 skrll <<bfd_make_empty_symbol>>, and return a <<asymbol>> structure.
162 1.1 skrll The return value may or may not be the same as the value from
163 1.1 skrll <<bfd_make_empty_symbol>> which was passed in.
164 1.1 skrll
165 1.1 skrll */
166 1.1 skrll
167 1.1 skrll /*
168 1.1 skrll DOCDD
169 1.1 skrll INODE
170 1.1 skrll typedef asymbol, symbol handling functions, Mini Symbols, Symbols
171 1.1 skrll
172 1.1 skrll */
173 1.1 skrll /*
174 1.1 skrll SUBSECTION
175 1.1 skrll typedef asymbol
176 1.1 skrll
177 1.1 skrll An <<asymbol>> has the form:
178 1.1 skrll
179 1.1 skrll */
180 1.1 skrll
181 1.1 skrll /*
182 1.1 skrll CODE_FRAGMENT
183 1.1 skrll
184 1.1 skrll .
185 1.1 skrll .typedef struct bfd_symbol
186 1.1 skrll .{
187 1.1 skrll . {* A pointer to the BFD which owns the symbol. This information
188 1.1 skrll . is necessary so that a back end can work out what additional
189 1.1 skrll . information (invisible to the application writer) is carried
190 1.1 skrll . with the symbol.
191 1.1 skrll .
192 1.1 skrll . This field is *almost* redundant, since you can use section->owner
193 1.1 skrll . instead, except that some symbols point to the global sections
194 1.1 skrll . bfd_{abs,com,und}_section. This could be fixed by making
195 1.1 skrll . these globals be per-bfd (or per-target-flavor). FIXME. *}
196 1.1 skrll . struct bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *}
197 1.1 skrll .
198 1.1 skrll . {* The text of the symbol. The name is left alone, and not copied; the
199 1.1 skrll . application may not alter it. *}
200 1.1 skrll . const char *name;
201 1.1 skrll .
202 1.1 skrll . {* The value of the symbol. This really should be a union of a
203 1.1 skrll . numeric value with a pointer, since some flags indicate that
204 1.1 skrll . a pointer to another symbol is stored here. *}
205 1.1 skrll . symvalue value;
206 1.1 skrll .
207 1.1 skrll . {* Attributes of a symbol. *}
208 1.1 skrll .#define BSF_NO_FLAGS 0x00
209 1.1 skrll .
210 1.1 skrll . {* The symbol has local scope; <<static>> in <<C>>. The value
211 1.1 skrll . is the offset into the section of the data. *}
212 1.1 skrll .#define BSF_LOCAL 0x01
213 1.1 skrll .
214 1.1 skrll . {* The symbol has global scope; initialized data in <<C>>. The
215 1.1 skrll . value is the offset into the section of the data. *}
216 1.1 skrll .#define BSF_GLOBAL 0x02
217 1.1 skrll .
218 1.1 skrll . {* The symbol has global scope and is exported. The value is
219 1.1 skrll . the offset into the section of the data. *}
220 1.1 skrll .#define BSF_EXPORT BSF_GLOBAL {* No real difference. *}
221 1.1 skrll .
222 1.1 skrll . {* A normal C symbol would be one of:
223 1.1 skrll . <<BSF_LOCAL>>, <<BSF_FORT_COMM>>, <<BSF_UNDEFINED>> or
224 1.1 skrll . <<BSF_GLOBAL>>. *}
225 1.1 skrll .
226 1.1 skrll . {* The symbol is a debugging record. The value has an arbitrary
227 1.1 skrll . meaning, unless BSF_DEBUGGING_RELOC is also set. *}
228 1.1 skrll .#define BSF_DEBUGGING 0x08
229 1.1 skrll .
230 1.1 skrll . {* The symbol denotes a function entry point. Used in ELF,
231 1.1 skrll . perhaps others someday. *}
232 1.1 skrll .#define BSF_FUNCTION 0x10
233 1.1 skrll .
234 1.1 skrll . {* Used by the linker. *}
235 1.1 skrll .#define BSF_KEEP 0x20
236 1.1 skrll .#define BSF_KEEP_G 0x40
237 1.1 skrll .
238 1.1 skrll . {* A weak global symbol, overridable without warnings by
239 1.1 skrll . a regular global symbol of the same name. *}
240 1.1 skrll .#define BSF_WEAK 0x80
241 1.1 skrll .
242 1.1 skrll . {* This symbol was created to point to a section, e.g. ELF's
243 1.1 skrll . STT_SECTION symbols. *}
244 1.1 skrll .#define BSF_SECTION_SYM 0x100
245 1.1 skrll .
246 1.1 skrll . {* The symbol used to be a common symbol, but now it is
247 1.1 skrll . allocated. *}
248 1.1 skrll .#define BSF_OLD_COMMON 0x200
249 1.1 skrll .
250 1.1 skrll . {* The default value for common data. *}
251 1.1 skrll .#define BFD_FORT_COMM_DEFAULT_VALUE 0
252 1.1 skrll .
253 1.1 skrll . {* In some files the type of a symbol sometimes alters its
254 1.1 skrll . location in an output file - ie in coff a <<ISFCN>> symbol
255 1.1 skrll . which is also <<C_EXT>> symbol appears where it was
256 1.1 skrll . declared and not at the end of a section. This bit is set
257 1.1 skrll . by the target BFD part to convey this information. *}
258 1.1 skrll .#define BSF_NOT_AT_END 0x400
259 1.1 skrll .
260 1.1 skrll . {* Signal that the symbol is the label of constructor section. *}
261 1.1 skrll .#define BSF_CONSTRUCTOR 0x800
262 1.1 skrll .
263 1.1 skrll . {* Signal that the symbol is a warning symbol. The name is a
264 1.1 skrll . warning. The name of the next symbol is the one to warn about;
265 1.1 skrll . if a reference is made to a symbol with the same name as the next
266 1.1 skrll . symbol, a warning is issued by the linker. *}
267 1.1 skrll .#define BSF_WARNING 0x1000
268 1.1 skrll .
269 1.1 skrll . {* Signal that the symbol is indirect. This symbol is an indirect
270 1.1 skrll . pointer to the symbol with the same name as the next symbol. *}
271 1.1 skrll .#define BSF_INDIRECT 0x2000
272 1.1 skrll .
273 1.1 skrll . {* BSF_FILE marks symbols that contain a file name. This is used
274 1.1 skrll . for ELF STT_FILE symbols. *}
275 1.1 skrll .#define BSF_FILE 0x4000
276 1.1 skrll .
277 1.1 skrll . {* Symbol is from dynamic linking information. *}
278 1.1 skrll .#define BSF_DYNAMIC 0x8000
279 1.1 skrll .
280 1.1 skrll . {* The symbol denotes a data object. Used in ELF, and perhaps
281 1.1 skrll . others someday. *}
282 1.1 skrll .#define BSF_OBJECT 0x10000
283 1.1 skrll .
284 1.1 skrll . {* This symbol is a debugging symbol. The value is the offset
285 1.1 skrll . into the section of the data. BSF_DEBUGGING should be set
286 1.1 skrll . as well. *}
287 1.1 skrll .#define BSF_DEBUGGING_RELOC 0x20000
288 1.1 skrll .
289 1.1 skrll . {* This symbol is thread local. Used in ELF. *}
290 1.1 skrll .#define BSF_THREAD_LOCAL 0x40000
291 1.1 skrll .
292 1.1 skrll . {* This symbol represents a complex relocation expression,
293 1.1 skrll . with the expression tree serialized in the symbol name. *}
294 1.1 skrll .#define BSF_RELC 0x80000
295 1.1 skrll .
296 1.1 skrll . {* This symbol represents a signed complex relocation expression,
297 1.1 skrll . with the expression tree serialized in the symbol name. *}
298 1.1 skrll .#define BSF_SRELC 0x100000
299 1.1 skrll .
300 1.1 skrll . {* This symbol was created by bfd_get_synthetic_symtab. *}
301 1.1 skrll .#define BSF_SYNTHETIC 0x200000
302 1.1 skrll .
303 1.1 skrll . flagword flags;
304 1.1 skrll .
305 1.1 skrll . {* A pointer to the section to which this symbol is
306 1.1 skrll . relative. This will always be non NULL, there are special
307 1.1 skrll . sections for undefined and absolute symbols. *}
308 1.1 skrll . struct bfd_section *section;
309 1.1 skrll .
310 1.1 skrll . {* Back end special data. *}
311 1.1 skrll . union
312 1.1 skrll . {
313 1.1 skrll . void *p;
314 1.1 skrll . bfd_vma i;
315 1.1 skrll . }
316 1.1 skrll . udata;
317 1.1 skrll .}
318 1.1 skrll .asymbol;
319 1.1 skrll .
320 1.1 skrll */
321 1.1 skrll
322 1.1 skrll #include "sysdep.h"
323 1.1 skrll #include "bfd.h"
324 1.1 skrll #include "libbfd.h"
325 1.1 skrll #include "safe-ctype.h"
326 1.1 skrll #include "bfdlink.h"
327 1.1 skrll #include "aout/stab_gnu.h"
328 1.1 skrll
329 1.1 skrll /*
330 1.1 skrll DOCDD
331 1.1 skrll INODE
332 1.1 skrll symbol handling functions, , typedef asymbol, Symbols
333 1.1 skrll SUBSECTION
334 1.1 skrll Symbol handling functions
335 1.1 skrll */
336 1.1 skrll
337 1.1 skrll /*
338 1.1 skrll FUNCTION
339 1.1 skrll bfd_get_symtab_upper_bound
340 1.1 skrll
341 1.1 skrll DESCRIPTION
342 1.1 skrll Return the number of bytes required to store a vector of pointers
343 1.1 skrll to <<asymbols>> for all the symbols in the BFD @var{abfd},
344 1.1 skrll including a terminal NULL pointer. If there are no symbols in
345 1.1 skrll the BFD, then return 0. If an error occurs, return -1.
346 1.1 skrll
347 1.1 skrll .#define bfd_get_symtab_upper_bound(abfd) \
348 1.1 skrll . BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
349 1.1 skrll .
350 1.1 skrll */
351 1.1 skrll
352 1.1 skrll /*
353 1.1 skrll FUNCTION
354 1.1 skrll bfd_is_local_label
355 1.1 skrll
356 1.1 skrll SYNOPSIS
357 1.1 skrll bfd_boolean bfd_is_local_label (bfd *abfd, asymbol *sym);
358 1.1 skrll
359 1.1 skrll DESCRIPTION
360 1.1 skrll Return TRUE if the given symbol @var{sym} in the BFD @var{abfd} is
361 1.1 skrll a compiler generated local label, else return FALSE.
362 1.1 skrll */
363 1.1 skrll
364 1.1 skrll bfd_boolean
365 1.1 skrll bfd_is_local_label (bfd *abfd, asymbol *sym)
366 1.1 skrll {
367 1.1 skrll /* The BSF_SECTION_SYM check is needed for IA-64, where every label that
368 1.1 skrll starts with '.' is local. This would accidentally catch section names
369 1.1 skrll if we didn't reject them here. */
370 1.1 skrll if ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_FILE | BSF_SECTION_SYM)) != 0)
371 1.1 skrll return FALSE;
372 1.1 skrll if (sym->name == NULL)
373 1.1 skrll return FALSE;
374 1.1 skrll return bfd_is_local_label_name (abfd, sym->name);
375 1.1 skrll }
376 1.1 skrll
377 1.1 skrll /*
378 1.1 skrll FUNCTION
379 1.1 skrll bfd_is_local_label_name
380 1.1 skrll
381 1.1 skrll SYNOPSIS
382 1.1 skrll bfd_boolean bfd_is_local_label_name (bfd *abfd, const char *name);
383 1.1 skrll
384 1.1 skrll DESCRIPTION
385 1.1 skrll Return TRUE if a symbol with the name @var{name} in the BFD
386 1.1 skrll @var{abfd} is a compiler generated local label, else return
387 1.1 skrll FALSE. This just checks whether the name has the form of a
388 1.1 skrll local label.
389 1.1 skrll
390 1.1 skrll .#define bfd_is_local_label_name(abfd, name) \
391 1.1 skrll . BFD_SEND (abfd, _bfd_is_local_label_name, (abfd, name))
392 1.1 skrll .
393 1.1 skrll */
394 1.1 skrll
395 1.1 skrll /*
396 1.1 skrll FUNCTION
397 1.1 skrll bfd_is_target_special_symbol
398 1.1 skrll
399 1.1 skrll SYNOPSIS
400 1.1 skrll bfd_boolean bfd_is_target_special_symbol (bfd *abfd, asymbol *sym);
401 1.1 skrll
402 1.1 skrll DESCRIPTION
403 1.1 skrll Return TRUE iff a symbol @var{sym} in the BFD @var{abfd} is something
404 1.1 skrll special to the particular target represented by the BFD. Such symbols
405 1.1 skrll should normally not be mentioned to the user.
406 1.1 skrll
407 1.1 skrll .#define bfd_is_target_special_symbol(abfd, sym) \
408 1.1 skrll . BFD_SEND (abfd, _bfd_is_target_special_symbol, (abfd, sym))
409 1.1 skrll .
410 1.1 skrll */
411 1.1 skrll
412 1.1 skrll /*
413 1.1 skrll FUNCTION
414 1.1 skrll bfd_canonicalize_symtab
415 1.1 skrll
416 1.1 skrll DESCRIPTION
417 1.1 skrll Read the symbols from the BFD @var{abfd}, and fills in
418 1.1 skrll the vector @var{location} with pointers to the symbols and
419 1.1 skrll a trailing NULL.
420 1.1 skrll Return the actual number of symbol pointers, not
421 1.1 skrll including the NULL.
422 1.1 skrll
423 1.1 skrll .#define bfd_canonicalize_symtab(abfd, location) \
424 1.1 skrll . BFD_SEND (abfd, _bfd_canonicalize_symtab, (abfd, location))
425 1.1 skrll .
426 1.1 skrll */
427 1.1 skrll
428 1.1 skrll /*
429 1.1 skrll FUNCTION
430 1.1 skrll bfd_set_symtab
431 1.1 skrll
432 1.1 skrll SYNOPSIS
433 1.1 skrll bfd_boolean bfd_set_symtab
434 1.1 skrll (bfd *abfd, asymbol **location, unsigned int count);
435 1.1 skrll
436 1.1 skrll DESCRIPTION
437 1.1 skrll Arrange that when the output BFD @var{abfd} is closed,
438 1.1 skrll the table @var{location} of @var{count} pointers to symbols
439 1.1 skrll will be written.
440 1.1 skrll */
441 1.1 skrll
442 1.1 skrll bfd_boolean
443 1.1 skrll bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int symcount)
444 1.1 skrll {
445 1.1 skrll if (abfd->format != bfd_object || bfd_read_p (abfd))
446 1.1 skrll {
447 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
448 1.1 skrll return FALSE;
449 1.1 skrll }
450 1.1 skrll
451 1.1 skrll bfd_get_outsymbols (abfd) = location;
452 1.1 skrll bfd_get_symcount (abfd) = symcount;
453 1.1 skrll return TRUE;
454 1.1 skrll }
455 1.1 skrll
456 1.1 skrll /*
457 1.1 skrll FUNCTION
458 1.1 skrll bfd_print_symbol_vandf
459 1.1 skrll
460 1.1 skrll SYNOPSIS
461 1.1 skrll void bfd_print_symbol_vandf (bfd *abfd, void *file, asymbol *symbol);
462 1.1 skrll
463 1.1 skrll DESCRIPTION
464 1.1 skrll Print the value and flags of the @var{symbol} supplied to the
465 1.1 skrll stream @var{file}.
466 1.1 skrll */
467 1.1 skrll void
468 1.1 skrll bfd_print_symbol_vandf (bfd *abfd, void *arg, asymbol *symbol)
469 1.1 skrll {
470 1.1 skrll FILE *file = arg;
471 1.1 skrll
472 1.1 skrll flagword type = symbol->flags;
473 1.1 skrll
474 1.1 skrll if (symbol->section != NULL)
475 1.1 skrll bfd_fprintf_vma (abfd, file, symbol->value + symbol->section->vma);
476 1.1 skrll else
477 1.1 skrll bfd_fprintf_vma (abfd, file, symbol->value);
478 1.1 skrll
479 1.1 skrll /* This presumes that a symbol can not be both BSF_DEBUGGING and
480 1.1 skrll BSF_DYNAMIC, nor more than one of BSF_FUNCTION, BSF_FILE, and
481 1.1 skrll BSF_OBJECT. */
482 1.1 skrll fprintf (file, " %c%c%c%c%c%c%c",
483 1.1 skrll ((type & BSF_LOCAL)
484 1.1 skrll ? (type & BSF_GLOBAL) ? '!' : 'l'
485 1.1 skrll : (type & BSF_GLOBAL) ? 'g' : ' '),
486 1.1 skrll (type & BSF_WEAK) ? 'w' : ' ',
487 1.1 skrll (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
488 1.1 skrll (type & BSF_WARNING) ? 'W' : ' ',
489 1.1 skrll (type & BSF_INDIRECT) ? 'I' : ' ',
490 1.1 skrll (type & BSF_DEBUGGING) ? 'd' : (type & BSF_DYNAMIC) ? 'D' : ' ',
491 1.1 skrll ((type & BSF_FUNCTION)
492 1.1 skrll ? 'F'
493 1.1 skrll : ((type & BSF_FILE)
494 1.1 skrll ? 'f'
495 1.1 skrll : ((type & BSF_OBJECT) ? 'O' : ' '))));
496 1.1 skrll }
497 1.1 skrll
498 1.1 skrll /*
499 1.1 skrll FUNCTION
500 1.1 skrll bfd_make_empty_symbol
501 1.1 skrll
502 1.1 skrll DESCRIPTION
503 1.1 skrll Create a new <<asymbol>> structure for the BFD @var{abfd}
504 1.1 skrll and return a pointer to it.
505 1.1 skrll
506 1.1 skrll This routine is necessary because each back end has private
507 1.1 skrll information surrounding the <<asymbol>>. Building your own
508 1.1 skrll <<asymbol>> and pointing to it will not create the private
509 1.1 skrll information, and will cause problems later on.
510 1.1 skrll
511 1.1 skrll .#define bfd_make_empty_symbol(abfd) \
512 1.1 skrll . BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
513 1.1 skrll .
514 1.1 skrll */
515 1.1 skrll
516 1.1 skrll /*
517 1.1 skrll FUNCTION
518 1.1 skrll _bfd_generic_make_empty_symbol
519 1.1 skrll
520 1.1 skrll SYNOPSIS
521 1.1 skrll asymbol *_bfd_generic_make_empty_symbol (bfd *);
522 1.1 skrll
523 1.1 skrll DESCRIPTION
524 1.1 skrll Create a new <<asymbol>> structure for the BFD @var{abfd}
525 1.1 skrll and return a pointer to it. Used by core file routines,
526 1.1 skrll binary back-end and anywhere else where no private info
527 1.1 skrll is needed.
528 1.1 skrll */
529 1.1 skrll
530 1.1 skrll asymbol *
531 1.1 skrll _bfd_generic_make_empty_symbol (bfd *abfd)
532 1.1 skrll {
533 1.1 skrll bfd_size_type amt = sizeof (asymbol);
534 1.1 skrll asymbol *new = bfd_zalloc (abfd, amt);
535 1.1 skrll if (new)
536 1.1 skrll new->the_bfd = abfd;
537 1.1 skrll return new;
538 1.1 skrll }
539 1.1 skrll
540 1.1 skrll /*
541 1.1 skrll FUNCTION
542 1.1 skrll bfd_make_debug_symbol
543 1.1 skrll
544 1.1 skrll DESCRIPTION
545 1.1 skrll Create a new <<asymbol>> structure for the BFD @var{abfd},
546 1.1 skrll to be used as a debugging symbol. Further details of its use have
547 1.1 skrll yet to be worked out.
548 1.1 skrll
549 1.1 skrll .#define bfd_make_debug_symbol(abfd,ptr,size) \
550 1.1 skrll . BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
551 1.1 skrll .
552 1.1 skrll */
553 1.1 skrll
554 1.1 skrll struct section_to_type
555 1.1 skrll {
556 1.1 skrll const char *section;
557 1.1 skrll char type;
558 1.1 skrll };
559 1.1 skrll
560 1.1 skrll /* Map section names to POSIX/BSD single-character symbol types.
561 1.1 skrll This table is probably incomplete. It is sorted for convenience of
562 1.1 skrll adding entries. Since it is so short, a linear search is used. */
563 1.1 skrll static const struct section_to_type stt[] =
564 1.1 skrll {
565 1.1 skrll {".bss", 'b'},
566 1.1 skrll {"code", 't'}, /* MRI .text */
567 1.1 skrll {".data", 'd'},
568 1.1 skrll {"*DEBUG*", 'N'},
569 1.1 skrll {".debug", 'N'}, /* MSVC's .debug (non-standard debug syms) */
570 1.1 skrll {".drectve", 'i'}, /* MSVC's .drective section */
571 1.1 skrll {".edata", 'e'}, /* MSVC's .edata (export) section */
572 1.1 skrll {".fini", 't'}, /* ELF fini section */
573 1.1 skrll {".idata", 'i'}, /* MSVC's .idata (import) section */
574 1.1 skrll {".init", 't'}, /* ELF init section */
575 1.1 skrll {".pdata", 'p'}, /* MSVC's .pdata (stack unwind) section */
576 1.1 skrll {".rdata", 'r'}, /* Read only data. */
577 1.1 skrll {".rodata", 'r'}, /* Read only data. */
578 1.1 skrll {".sbss", 's'}, /* Small BSS (uninitialized data). */
579 1.1 skrll {".scommon", 'c'}, /* Small common. */
580 1.1 skrll {".sdata", 'g'}, /* Small initialized data. */
581 1.1 skrll {".text", 't'},
582 1.1 skrll {"vars", 'd'}, /* MRI .data */
583 1.1 skrll {"zerovars", 'b'}, /* MRI .bss */
584 1.1 skrll {0, 0}
585 1.1 skrll };
586 1.1 skrll
587 1.1 skrll /* Return the single-character symbol type corresponding to
588 1.1 skrll section S, or '?' for an unknown COFF section.
589 1.1 skrll
590 1.1 skrll Check for any leading string which matches, so .text5 returns
591 1.1 skrll 't' as well as .text */
592 1.1 skrll
593 1.1 skrll static char
594 1.1 skrll coff_section_type (const char *s)
595 1.1 skrll {
596 1.1 skrll const struct section_to_type *t;
597 1.1 skrll
598 1.1 skrll for (t = &stt[0]; t->section; t++)
599 1.1 skrll if (!strncmp (s, t->section, strlen (t->section)))
600 1.1 skrll return t->type;
601 1.1 skrll
602 1.1 skrll return '?';
603 1.1 skrll }
604 1.1 skrll
605 1.1 skrll /* Return the single-character symbol type corresponding to section
606 1.1 skrll SECTION, or '?' for an unknown section. This uses section flags to
607 1.1 skrll identify sections.
608 1.1 skrll
609 1.1 skrll FIXME These types are unhandled: c, i, e, p. If we handled these also,
610 1.1 skrll we could perhaps obsolete coff_section_type. */
611 1.1 skrll
612 1.1 skrll static char
613 1.1 skrll decode_section_type (const struct bfd_section *section)
614 1.1 skrll {
615 1.1 skrll if (section->flags & SEC_CODE)
616 1.1 skrll return 't';
617 1.1 skrll if (section->flags & SEC_DATA)
618 1.1 skrll {
619 1.1 skrll if (section->flags & SEC_READONLY)
620 1.1 skrll return 'r';
621 1.1 skrll else if (section->flags & SEC_SMALL_DATA)
622 1.1 skrll return 'g';
623 1.1 skrll else
624 1.1 skrll return 'd';
625 1.1 skrll }
626 1.1 skrll if ((section->flags & SEC_HAS_CONTENTS) == 0)
627 1.1 skrll {
628 1.1 skrll if (section->flags & SEC_SMALL_DATA)
629 1.1 skrll return 's';
630 1.1 skrll else
631 1.1 skrll return 'b';
632 1.1 skrll }
633 1.1 skrll if (section->flags & SEC_DEBUGGING)
634 1.1 skrll return 'N';
635 1.1 skrll if ((section->flags & SEC_HAS_CONTENTS) && (section->flags & SEC_READONLY))
636 1.1 skrll return 'n';
637 1.1 skrll
638 1.1 skrll return '?';
639 1.1 skrll }
640 1.1 skrll
641 1.1 skrll /*
642 1.1 skrll FUNCTION
643 1.1 skrll bfd_decode_symclass
644 1.1 skrll
645 1.1 skrll DESCRIPTION
646 1.1 skrll Return a character corresponding to the symbol
647 1.1 skrll class of @var{symbol}, or '?' for an unknown class.
648 1.1 skrll
649 1.1 skrll SYNOPSIS
650 1.1 skrll int bfd_decode_symclass (asymbol *symbol);
651 1.1 skrll */
652 1.1 skrll int
653 1.1 skrll bfd_decode_symclass (asymbol *symbol)
654 1.1 skrll {
655 1.1 skrll char c;
656 1.1 skrll
657 1.1 skrll if (symbol->section && bfd_is_com_section (symbol->section))
658 1.1 skrll return 'C';
659 1.1 skrll if (bfd_is_und_section (symbol->section))
660 1.1 skrll {
661 1.1 skrll if (symbol->flags & BSF_WEAK)
662 1.1 skrll {
663 1.1 skrll /* If weak, determine if it's specifically an object
664 1.1 skrll or non-object weak. */
665 1.1 skrll if (symbol->flags & BSF_OBJECT)
666 1.1 skrll return 'v';
667 1.1 skrll else
668 1.1 skrll return 'w';
669 1.1 skrll }
670 1.1 skrll else
671 1.1 skrll return 'U';
672 1.1 skrll }
673 1.1 skrll if (bfd_is_ind_section (symbol->section))
674 1.1 skrll return 'I';
675 1.1 skrll if (symbol->flags & BSF_WEAK)
676 1.1 skrll {
677 1.1 skrll /* If weak, determine if it's specifically an object
678 1.1 skrll or non-object weak. */
679 1.1 skrll if (symbol->flags & BSF_OBJECT)
680 1.1 skrll return 'V';
681 1.1 skrll else
682 1.1 skrll return 'W';
683 1.1 skrll }
684 1.1 skrll if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL)))
685 1.1 skrll return '?';
686 1.1 skrll
687 1.1 skrll if (bfd_is_abs_section (symbol->section))
688 1.1 skrll c = 'a';
689 1.1 skrll else if (symbol->section)
690 1.1 skrll {
691 1.1 skrll c = coff_section_type (symbol->section->name);
692 1.1 skrll if (c == '?')
693 1.1 skrll c = decode_section_type (symbol->section);
694 1.1 skrll }
695 1.1 skrll else
696 1.1 skrll return '?';
697 1.1 skrll if (symbol->flags & BSF_GLOBAL)
698 1.1 skrll c = TOUPPER (c);
699 1.1 skrll return c;
700 1.1 skrll
701 1.1 skrll /* We don't have to handle these cases just yet, but we will soon:
702 1.1 skrll N_SETV: 'v';
703 1.1 skrll N_SETA: 'l';
704 1.1 skrll N_SETT: 'x';
705 1.1 skrll N_SETD: 'z';
706 1.1 skrll N_SETB: 's';
707 1.1 skrll N_INDR: 'i';
708 1.1 skrll */
709 1.1 skrll }
710 1.1 skrll
711 1.1 skrll /*
712 1.1 skrll FUNCTION
713 1.1 skrll bfd_is_undefined_symclass
714 1.1 skrll
715 1.1 skrll DESCRIPTION
716 1.1 skrll Returns non-zero if the class symbol returned by
717 1.1 skrll bfd_decode_symclass represents an undefined symbol.
718 1.1 skrll Returns zero otherwise.
719 1.1 skrll
720 1.1 skrll SYNOPSIS
721 1.1 skrll bfd_boolean bfd_is_undefined_symclass (int symclass);
722 1.1 skrll */
723 1.1 skrll
724 1.1 skrll bfd_boolean
725 1.1 skrll bfd_is_undefined_symclass (int symclass)
726 1.1 skrll {
727 1.1 skrll return symclass == 'U' || symclass == 'w' || symclass == 'v';
728 1.1 skrll }
729 1.1 skrll
730 1.1 skrll /*
731 1.1 skrll FUNCTION
732 1.1 skrll bfd_symbol_info
733 1.1 skrll
734 1.1 skrll DESCRIPTION
735 1.1 skrll Fill in the basic info about symbol that nm needs.
736 1.1 skrll Additional info may be added by the back-ends after
737 1.1 skrll calling this function.
738 1.1 skrll
739 1.1 skrll SYNOPSIS
740 1.1 skrll void bfd_symbol_info (asymbol *symbol, symbol_info *ret);
741 1.1 skrll */
742 1.1 skrll
743 1.1 skrll void
744 1.1 skrll bfd_symbol_info (asymbol *symbol, symbol_info *ret)
745 1.1 skrll {
746 1.1 skrll ret->type = bfd_decode_symclass (symbol);
747 1.1 skrll
748 1.1 skrll if (bfd_is_undefined_symclass (ret->type))
749 1.1 skrll ret->value = 0;
750 1.1 skrll else
751 1.1 skrll ret->value = symbol->value + symbol->section->vma;
752 1.1 skrll
753 1.1 skrll ret->name = symbol->name;
754 1.1 skrll }
755 1.1 skrll
756 1.1 skrll /*
757 1.1 skrll FUNCTION
758 1.1 skrll bfd_copy_private_symbol_data
759 1.1 skrll
760 1.1 skrll SYNOPSIS
761 1.1 skrll bfd_boolean bfd_copy_private_symbol_data
762 1.1 skrll (bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
763 1.1 skrll
764 1.1 skrll DESCRIPTION
765 1.1 skrll Copy private symbol information from @var{isym} in the BFD
766 1.1 skrll @var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
767 1.1 skrll Return <<TRUE>> on success, <<FALSE>> on error. Possible error
768 1.1 skrll returns are:
769 1.1 skrll
770 1.1 skrll o <<bfd_error_no_memory>> -
771 1.1 skrll Not enough memory exists to create private data for @var{osec}.
772 1.1 skrll
773 1.1 skrll .#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
774 1.1 skrll . BFD_SEND (obfd, _bfd_copy_private_symbol_data, \
775 1.1 skrll . (ibfd, isymbol, obfd, osymbol))
776 1.1 skrll .
777 1.1 skrll */
778 1.1 skrll
779 1.1 skrll /* The generic version of the function which returns mini symbols.
780 1.1 skrll This is used when the backend does not provide a more efficient
781 1.1 skrll version. It just uses BFD asymbol structures as mini symbols. */
782 1.1 skrll
783 1.1 skrll long
784 1.1 skrll _bfd_generic_read_minisymbols (bfd *abfd,
785 1.1 skrll bfd_boolean dynamic,
786 1.1 skrll void **minisymsp,
787 1.1 skrll unsigned int *sizep)
788 1.1 skrll {
789 1.1 skrll long storage;
790 1.1 skrll asymbol **syms = NULL;
791 1.1 skrll long symcount;
792 1.1 skrll
793 1.1 skrll if (dynamic)
794 1.1 skrll storage = bfd_get_dynamic_symtab_upper_bound (abfd);
795 1.1 skrll else
796 1.1 skrll storage = bfd_get_symtab_upper_bound (abfd);
797 1.1 skrll if (storage < 0)
798 1.1 skrll goto error_return;
799 1.1 skrll if (storage == 0)
800 1.1 skrll return 0;
801 1.1 skrll
802 1.1 skrll syms = bfd_malloc (storage);
803 1.1 skrll if (syms == NULL)
804 1.1 skrll goto error_return;
805 1.1 skrll
806 1.1 skrll if (dynamic)
807 1.1 skrll symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
808 1.1 skrll else
809 1.1 skrll symcount = bfd_canonicalize_symtab (abfd, syms);
810 1.1 skrll if (symcount < 0)
811 1.1 skrll goto error_return;
812 1.1 skrll
813 1.1 skrll *minisymsp = syms;
814 1.1 skrll *sizep = sizeof (asymbol *);
815 1.1 skrll return symcount;
816 1.1 skrll
817 1.1 skrll error_return:
818 1.1 skrll bfd_set_error (bfd_error_no_symbols);
819 1.1 skrll if (syms != NULL)
820 1.1 skrll free (syms);
821 1.1 skrll return -1;
822 1.1 skrll }
823 1.1 skrll
824 1.1 skrll /* The generic version of the function which converts a minisymbol to
825 1.1 skrll an asymbol. We don't worry about the sym argument we are passed;
826 1.1 skrll we just return the asymbol the minisymbol points to. */
827 1.1 skrll
828 1.1 skrll asymbol *
829 1.1 skrll _bfd_generic_minisymbol_to_symbol (bfd *abfd ATTRIBUTE_UNUSED,
830 1.1 skrll bfd_boolean dynamic ATTRIBUTE_UNUSED,
831 1.1 skrll const void *minisym,
832 1.1 skrll asymbol *sym ATTRIBUTE_UNUSED)
833 1.1 skrll {
834 1.1 skrll return *(asymbol **) minisym;
835 1.1 skrll }
836 1.1 skrll
837 1.1 skrll /* Look through stabs debugging information in .stab and .stabstr
838 1.1 skrll sections to find the source file and line closest to a desired
839 1.1 skrll location. This is used by COFF and ELF targets. It sets *pfound
840 1.1 skrll to TRUE if it finds some information. The *pinfo field is used to
841 1.1 skrll pass cached information in and out of this routine; this first time
842 1.1 skrll the routine is called for a BFD, *pinfo should be NULL. The value
843 1.1 skrll placed in *pinfo should be saved with the BFD, and passed back each
844 1.1 skrll time this function is called. */
845 1.1 skrll
846 1.1 skrll /* We use a cache by default. */
847 1.1 skrll
848 1.1 skrll #define ENABLE_CACHING
849 1.1 skrll
850 1.1 skrll /* We keep an array of indexentry structures to record where in the
851 1.1 skrll stabs section we should look to find line number information for a
852 1.1 skrll particular address. */
853 1.1 skrll
854 1.1 skrll struct indexentry
855 1.1 skrll {
856 1.1 skrll bfd_vma val;
857 1.1 skrll bfd_byte *stab;
858 1.1 skrll bfd_byte *str;
859 1.1 skrll char *directory_name;
860 1.1 skrll char *file_name;
861 1.1 skrll char *function_name;
862 1.1 skrll };
863 1.1 skrll
864 1.1 skrll /* Compare two indexentry structures. This is called via qsort. */
865 1.1 skrll
866 1.1 skrll static int
867 1.1 skrll cmpindexentry (const void *a, const void *b)
868 1.1 skrll {
869 1.1 skrll const struct indexentry *contestantA = a;
870 1.1 skrll const struct indexentry *contestantB = b;
871 1.1 skrll
872 1.1 skrll if (contestantA->val < contestantB->val)
873 1.1 skrll return -1;
874 1.1 skrll else if (contestantA->val > contestantB->val)
875 1.1 skrll return 1;
876 1.1 skrll else
877 1.1 skrll return 0;
878 1.1 skrll }
879 1.1 skrll
880 1.1 skrll /* A pointer to this structure is stored in *pinfo. */
881 1.1 skrll
882 1.1 skrll struct stab_find_info
883 1.1 skrll {
884 1.1 skrll /* The .stab section. */
885 1.1 skrll asection *stabsec;
886 1.1 skrll /* The .stabstr section. */
887 1.1 skrll asection *strsec;
888 1.1 skrll /* The contents of the .stab section. */
889 1.1 skrll bfd_byte *stabs;
890 1.1 skrll /* The contents of the .stabstr section. */
891 1.1 skrll bfd_byte *strs;
892 1.1 skrll
893 1.1 skrll /* A table that indexes stabs by memory address. */
894 1.1 skrll struct indexentry *indextable;
895 1.1 skrll /* The number of entries in indextable. */
896 1.1 skrll int indextablesize;
897 1.1 skrll
898 1.1 skrll #ifdef ENABLE_CACHING
899 1.1 skrll /* Cached values to restart quickly. */
900 1.1 skrll struct indexentry *cached_indexentry;
901 1.1 skrll bfd_vma cached_offset;
902 1.1 skrll bfd_byte *cached_stab;
903 1.1 skrll char *cached_file_name;
904 1.1 skrll #endif
905 1.1 skrll
906 1.1 skrll /* Saved ptr to malloc'ed filename. */
907 1.1 skrll char *filename;
908 1.1 skrll };
909 1.1 skrll
910 1.1 skrll bfd_boolean
911 1.1 skrll _bfd_stab_section_find_nearest_line (bfd *abfd,
912 1.1 skrll asymbol **symbols,
913 1.1 skrll asection *section,
914 1.1 skrll bfd_vma offset,
915 1.1 skrll bfd_boolean *pfound,
916 1.1 skrll const char **pfilename,
917 1.1 skrll const char **pfnname,
918 1.1 skrll unsigned int *pline,
919 1.1 skrll void **pinfo)
920 1.1 skrll {
921 1.1 skrll struct stab_find_info *info;
922 1.1 skrll bfd_size_type stabsize, strsize;
923 1.1 skrll bfd_byte *stab, *str;
924 1.1 skrll bfd_byte *last_stab = NULL;
925 1.1 skrll bfd_size_type stroff;
926 1.1 skrll struct indexentry *indexentry;
927 1.1 skrll char *file_name;
928 1.1 skrll char *directory_name;
929 1.1 skrll int saw_fun;
930 1.1 skrll bfd_boolean saw_line, saw_func;
931 1.1 skrll
932 1.1 skrll *pfound = FALSE;
933 1.1 skrll *pfilename = bfd_get_filename (abfd);
934 1.1 skrll *pfnname = NULL;
935 1.1 skrll *pline = 0;
936 1.1 skrll
937 1.1 skrll /* Stabs entries use a 12 byte format:
938 1.1 skrll 4 byte string table index
939 1.1 skrll 1 byte stab type
940 1.1 skrll 1 byte stab other field
941 1.1 skrll 2 byte stab desc field
942 1.1 skrll 4 byte stab value
943 1.1 skrll FIXME: This will have to change for a 64 bit object format.
944 1.1 skrll
945 1.1 skrll The stabs symbols are divided into compilation units. For the
946 1.1 skrll first entry in each unit, the type of 0, the value is the length
947 1.1 skrll of the string table for this unit, and the desc field is the
948 1.1 skrll number of stabs symbols for this unit. */
949 1.1 skrll
950 1.1 skrll #define STRDXOFF (0)
951 1.1 skrll #define TYPEOFF (4)
952 1.1 skrll #define OTHEROFF (5)
953 1.1 skrll #define DESCOFF (6)
954 1.1 skrll #define VALOFF (8)
955 1.1 skrll #define STABSIZE (12)
956 1.1 skrll
957 1.1 skrll info = *pinfo;
958 1.1 skrll if (info != NULL)
959 1.1 skrll {
960 1.1 skrll if (info->stabsec == NULL || info->strsec == NULL)
961 1.1 skrll {
962 1.1 skrll /* No stabs debugging information. */
963 1.1 skrll return TRUE;
964 1.1 skrll }
965 1.1 skrll
966 1.1 skrll stabsize = (info->stabsec->rawsize
967 1.1 skrll ? info->stabsec->rawsize
968 1.1 skrll : info->stabsec->size);
969 1.1 skrll strsize = (info->strsec->rawsize
970 1.1 skrll ? info->strsec->rawsize
971 1.1 skrll : info->strsec->size);
972 1.1 skrll }
973 1.1 skrll else
974 1.1 skrll {
975 1.1 skrll long reloc_size, reloc_count;
976 1.1 skrll arelent **reloc_vector;
977 1.1 skrll int i;
978 1.1 skrll char *name;
979 1.1 skrll char *function_name;
980 1.1 skrll bfd_size_type amt = sizeof *info;
981 1.1 skrll
982 1.1 skrll info = bfd_zalloc (abfd, amt);
983 1.1 skrll if (info == NULL)
984 1.1 skrll return FALSE;
985 1.1 skrll
986 1.1 skrll /* FIXME: When using the linker --split-by-file or
987 1.1 skrll --split-by-reloc options, it is possible for the .stab and
988 1.1 skrll .stabstr sections to be split. We should handle that. */
989 1.1 skrll
990 1.1 skrll info->stabsec = bfd_get_section_by_name (abfd, ".stab");
991 1.1 skrll info->strsec = bfd_get_section_by_name (abfd, ".stabstr");
992 1.1 skrll
993 1.1 skrll if (info->stabsec == NULL || info->strsec == NULL)
994 1.1 skrll {
995 1.1 skrll /* Try SOM section names. */
996 1.1 skrll info->stabsec = bfd_get_section_by_name (abfd, "$GDB_SYMBOLS$");
997 1.1 skrll info->strsec = bfd_get_section_by_name (abfd, "$GDB_STRINGS$");
998 1.1 skrll
999 1.1 skrll if (info->stabsec == NULL || info->strsec == NULL)
1000 1.1 skrll {
1001 1.1 skrll /* No stabs debugging information. Set *pinfo so that we
1002 1.1 skrll can return quickly in the info != NULL case above. */
1003 1.1 skrll *pinfo = info;
1004 1.1 skrll return TRUE;
1005 1.1 skrll }
1006 1.1 skrll }
1007 1.1 skrll
1008 1.1 skrll stabsize = (info->stabsec->rawsize
1009 1.1 skrll ? info->stabsec->rawsize
1010 1.1 skrll : info->stabsec->size);
1011 1.1 skrll strsize = (info->strsec->rawsize
1012 1.1 skrll ? info->strsec->rawsize
1013 1.1 skrll : info->strsec->size);
1014 1.1 skrll
1015 1.1 skrll info->stabs = bfd_alloc (abfd, stabsize);
1016 1.1 skrll info->strs = bfd_alloc (abfd, strsize);
1017 1.1 skrll if (info->stabs == NULL || info->strs == NULL)
1018 1.1 skrll return FALSE;
1019 1.1 skrll
1020 1.1 skrll if (! bfd_get_section_contents (abfd, info->stabsec, info->stabs,
1021 1.1 skrll 0, stabsize)
1022 1.1 skrll || ! bfd_get_section_contents (abfd, info->strsec, info->strs,
1023 1.1 skrll 0, strsize))
1024 1.1 skrll return FALSE;
1025 1.1 skrll
1026 1.1 skrll /* If this is a relocatable object file, we have to relocate
1027 1.1 skrll the entries in .stab. This should always be simple 32 bit
1028 1.1 skrll relocations against symbols defined in this object file, so
1029 1.1 skrll this should be no big deal. */
1030 1.1 skrll reloc_size = bfd_get_reloc_upper_bound (abfd, info->stabsec);
1031 1.1 skrll if (reloc_size < 0)
1032 1.1 skrll return FALSE;
1033 1.1 skrll reloc_vector = bfd_malloc (reloc_size);
1034 1.1 skrll if (reloc_vector == NULL && reloc_size != 0)
1035 1.1 skrll return FALSE;
1036 1.1 skrll reloc_count = bfd_canonicalize_reloc (abfd, info->stabsec, reloc_vector,
1037 1.1 skrll symbols);
1038 1.1 skrll if (reloc_count < 0)
1039 1.1 skrll {
1040 1.1 skrll if (reloc_vector != NULL)
1041 1.1 skrll free (reloc_vector);
1042 1.1 skrll return FALSE;
1043 1.1 skrll }
1044 1.1 skrll if (reloc_count > 0)
1045 1.1 skrll {
1046 1.1 skrll arelent **pr;
1047 1.1 skrll
1048 1.1 skrll for (pr = reloc_vector; *pr != NULL; pr++)
1049 1.1 skrll {
1050 1.1 skrll arelent *r;
1051 1.1 skrll unsigned long val;
1052 1.1 skrll asymbol *sym;
1053 1.1 skrll
1054 1.1 skrll r = *pr;
1055 1.1 skrll /* Ignore R_*_NONE relocs. */
1056 1.1 skrll if (r->howto->dst_mask == 0)
1057 1.1 skrll continue;
1058 1.1 skrll
1059 1.1 skrll if (r->howto->rightshift != 0
1060 1.1 skrll || r->howto->size != 2
1061 1.1 skrll || r->howto->bitsize != 32
1062 1.1 skrll || r->howto->pc_relative
1063 1.1 skrll || r->howto->bitpos != 0
1064 1.1 skrll || r->howto->dst_mask != 0xffffffff)
1065 1.1 skrll {
1066 1.1 skrll (*_bfd_error_handler)
1067 1.1 skrll (_("Unsupported .stab relocation"));
1068 1.1 skrll bfd_set_error (bfd_error_invalid_operation);
1069 1.1 skrll if (reloc_vector != NULL)
1070 1.1 skrll free (reloc_vector);
1071 1.1 skrll return FALSE;
1072 1.1 skrll }
1073 1.1 skrll
1074 1.1 skrll val = bfd_get_32 (abfd, info->stabs + r->address);
1075 1.1 skrll val &= r->howto->src_mask;
1076 1.1 skrll sym = *r->sym_ptr_ptr;
1077 1.1 skrll val += sym->value + sym->section->vma + r->addend;
1078 1.1 skrll bfd_put_32 (abfd, (bfd_vma) val, info->stabs + r->address);
1079 1.1 skrll }
1080 1.1 skrll }
1081 1.1 skrll
1082 1.1 skrll if (reloc_vector != NULL)
1083 1.1 skrll free (reloc_vector);
1084 1.1 skrll
1085 1.1 skrll /* First time through this function, build a table matching
1086 1.1 skrll function VM addresses to stabs, then sort based on starting
1087 1.1 skrll VM address. Do this in two passes: once to count how many
1088 1.1 skrll table entries we'll need, and a second to actually build the
1089 1.1 skrll table. */
1090 1.1 skrll
1091 1.1 skrll info->indextablesize = 0;
1092 1.1 skrll saw_fun = 1;
1093 1.1 skrll for (stab = info->stabs; stab < info->stabs + stabsize; stab += STABSIZE)
1094 1.1 skrll {
1095 1.1 skrll if (stab[TYPEOFF] == (bfd_byte) N_SO)
1096 1.1 skrll {
1097 1.1 skrll /* N_SO with null name indicates EOF */
1098 1.1 skrll if (bfd_get_32 (abfd, stab + STRDXOFF) == 0)
1099 1.1 skrll continue;
1100 1.1 skrll
1101 1.1 skrll /* if we did not see a function def, leave space for one. */
1102 1.1 skrll if (saw_fun == 0)
1103 1.1 skrll ++info->indextablesize;
1104 1.1 skrll
1105 1.1 skrll saw_fun = 0;
1106 1.1 skrll
1107 1.1 skrll /* two N_SO's in a row is a filename and directory. Skip */
1108 1.1 skrll if (stab + STABSIZE < info->stabs + stabsize
1109 1.1 skrll && *(stab + STABSIZE + TYPEOFF) == (bfd_byte) N_SO)
1110 1.1 skrll {
1111 1.1 skrll stab += STABSIZE;
1112 1.1 skrll }
1113 1.1 skrll }
1114 1.1 skrll else if (stab[TYPEOFF] == (bfd_byte) N_FUN)
1115 1.1 skrll {
1116 1.1 skrll saw_fun = 1;
1117 1.1 skrll ++info->indextablesize;
1118 1.1 skrll }
1119 1.1 skrll }
1120 1.1 skrll
1121 1.1 skrll if (saw_fun == 0)
1122 1.1 skrll ++info->indextablesize;
1123 1.1 skrll
1124 1.1 skrll if (info->indextablesize == 0)
1125 1.1 skrll return TRUE;
1126 1.1 skrll ++info->indextablesize;
1127 1.1 skrll
1128 1.1 skrll amt = info->indextablesize;
1129 1.1 skrll amt *= sizeof (struct indexentry);
1130 1.1 skrll info->indextable = bfd_alloc (abfd, amt);
1131 1.1 skrll if (info->indextable == NULL)
1132 1.1 skrll return FALSE;
1133 1.1 skrll
1134 1.1 skrll file_name = NULL;
1135 1.1 skrll directory_name = NULL;
1136 1.1 skrll saw_fun = 1;
1137 1.1 skrll
1138 1.1 skrll for (i = 0, stroff = 0, stab = info->stabs, str = info->strs;
1139 1.1 skrll i < info->indextablesize && stab < info->stabs + stabsize;
1140 1.1 skrll stab += STABSIZE)
1141 1.1 skrll {
1142 1.1 skrll switch (stab[TYPEOFF])
1143 1.1 skrll {
1144 1.1 skrll case 0:
1145 1.1 skrll /* This is the first entry in a compilation unit. */
1146 1.1 skrll if ((bfd_size_type) ((info->strs + strsize) - str) < stroff)
1147 1.1 skrll break;
1148 1.1 skrll str += stroff;
1149 1.1 skrll stroff = bfd_get_32 (abfd, stab + VALOFF);
1150 1.1 skrll break;
1151 1.1 skrll
1152 1.1 skrll case N_SO:
1153 1.1 skrll /* The main file name. */
1154 1.1 skrll
1155 1.1 skrll /* The following code creates a new indextable entry with
1156 1.1 skrll a NULL function name if there were no N_FUNs in a file.
1157 1.1 skrll Note that a N_SO without a file name is an EOF and
1158 1.1 skrll there could be 2 N_SO following it with the new filename
1159 1.1 skrll and directory. */
1160 1.1 skrll if (saw_fun == 0)
1161 1.1 skrll {
1162 1.1 skrll info->indextable[i].val = bfd_get_32 (abfd, last_stab + VALOFF);
1163 1.1 skrll info->indextable[i].stab = last_stab;
1164 1.1 skrll info->indextable[i].str = str;
1165 1.1 skrll info->indextable[i].directory_name = directory_name;
1166 1.1 skrll info->indextable[i].file_name = file_name;
1167 1.1 skrll info->indextable[i].function_name = NULL;
1168 1.1 skrll ++i;
1169 1.1 skrll }
1170 1.1 skrll saw_fun = 0;
1171 1.1 skrll
1172 1.1 skrll file_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
1173 1.1 skrll if (*file_name == '\0')
1174 1.1 skrll {
1175 1.1 skrll directory_name = NULL;
1176 1.1 skrll file_name = NULL;
1177 1.1 skrll saw_fun = 1;
1178 1.1 skrll }
1179 1.1 skrll else
1180 1.1 skrll {
1181 1.1 skrll last_stab = stab;
1182 1.1 skrll if (stab + STABSIZE >= info->stabs + stabsize
1183 1.1 skrll || *(stab + STABSIZE + TYPEOFF) != (bfd_byte) N_SO)
1184 1.1 skrll {
1185 1.1 skrll directory_name = NULL;
1186 1.1 skrll }
1187 1.1 skrll else
1188 1.1 skrll {
1189 1.1 skrll /* Two consecutive N_SOs are a directory and a
1190 1.1 skrll file name. */
1191 1.1 skrll stab += STABSIZE;
1192 1.1 skrll directory_name = file_name;
1193 1.1 skrll file_name = ((char *) str
1194 1.1 skrll + bfd_get_32 (abfd, stab + STRDXOFF));
1195 1.1 skrll }
1196 1.1 skrll }
1197 1.1 skrll break;
1198 1.1 skrll
1199 1.1 skrll case N_SOL:
1200 1.1 skrll /* The name of an include file. */
1201 1.1 skrll file_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
1202 1.1 skrll break;
1203 1.1 skrll
1204 1.1 skrll case N_FUN:
1205 1.1 skrll /* A function name. */
1206 1.1 skrll saw_fun = 1;
1207 1.1 skrll name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
1208 1.1 skrll
1209 1.1 skrll if (*name == '\0')
1210 1.1 skrll name = NULL;
1211 1.1 skrll
1212 1.1 skrll function_name = name;
1213 1.1 skrll
1214 1.1 skrll if (name == NULL)
1215 1.1 skrll continue;
1216 1.1 skrll
1217 1.1 skrll info->indextable[i].val = bfd_get_32 (abfd, stab + VALOFF);
1218 1.1 skrll info->indextable[i].stab = stab;
1219 1.1 skrll info->indextable[i].str = str;
1220 1.1 skrll info->indextable[i].directory_name = directory_name;
1221 1.1 skrll info->indextable[i].file_name = file_name;
1222 1.1 skrll info->indextable[i].function_name = function_name;
1223 1.1 skrll ++i;
1224 1.1 skrll break;
1225 1.1 skrll }
1226 1.1 skrll }
1227 1.1 skrll
1228 1.1 skrll if (saw_fun == 0)
1229 1.1 skrll {
1230 1.1 skrll info->indextable[i].val = bfd_get_32 (abfd, last_stab + VALOFF);
1231 1.1 skrll info->indextable[i].stab = last_stab;
1232 1.1 skrll info->indextable[i].str = str;
1233 1.1 skrll info->indextable[i].directory_name = directory_name;
1234 1.1 skrll info->indextable[i].file_name = file_name;
1235 1.1 skrll info->indextable[i].function_name = NULL;
1236 1.1 skrll ++i;
1237 1.1 skrll }
1238 1.1 skrll
1239 1.1 skrll info->indextable[i].val = (bfd_vma) -1;
1240 1.1 skrll info->indextable[i].stab = info->stabs + stabsize;
1241 1.1 skrll info->indextable[i].str = str;
1242 1.1 skrll info->indextable[i].directory_name = NULL;
1243 1.1 skrll info->indextable[i].file_name = NULL;
1244 1.1 skrll info->indextable[i].function_name = NULL;
1245 1.1 skrll ++i;
1246 1.1 skrll
1247 1.1 skrll info->indextablesize = i;
1248 1.1 skrll qsort (info->indextable, (size_t) i, sizeof (struct indexentry),
1249 1.1 skrll cmpindexentry);
1250 1.1 skrll
1251 1.1 skrll *pinfo = info;
1252 1.1 skrll }
1253 1.1 skrll
1254 1.1 skrll /* We are passed a section relative offset. The offsets in the
1255 1.1 skrll stabs information are absolute. */
1256 1.1 skrll offset += bfd_get_section_vma (abfd, section);
1257 1.1 skrll
1258 1.1 skrll #ifdef ENABLE_CACHING
1259 1.1 skrll if (info->cached_indexentry != NULL
1260 1.1 skrll && offset >= info->cached_offset
1261 1.1 skrll && offset < (info->cached_indexentry + 1)->val)
1262 1.1 skrll {
1263 1.1 skrll stab = info->cached_stab;
1264 1.1 skrll indexentry = info->cached_indexentry;
1265 1.1 skrll file_name = info->cached_file_name;
1266 1.1 skrll }
1267 1.1 skrll else
1268 1.1 skrll #endif
1269 1.1 skrll {
1270 1.1 skrll long low, high;
1271 1.1 skrll long mid = -1;
1272 1.1 skrll
1273 1.1 skrll /* Cache non-existent or invalid. Do binary search on
1274 1.1 skrll indextable. */
1275 1.1 skrll indexentry = NULL;
1276 1.1 skrll
1277 1.1 skrll low = 0;
1278 1.1 skrll high = info->indextablesize - 1;
1279 1.1 skrll while (low != high)
1280 1.1 skrll {
1281 1.1 skrll mid = (high + low) / 2;
1282 1.1 skrll if (offset >= info->indextable[mid].val
1283 1.1 skrll && offset < info->indextable[mid + 1].val)
1284 1.1 skrll {
1285 1.1 skrll indexentry = &info->indextable[mid];
1286 1.1 skrll break;
1287 1.1 skrll }
1288 1.1 skrll
1289 1.1 skrll if (info->indextable[mid].val > offset)
1290 1.1 skrll high = mid;
1291 1.1 skrll else
1292 1.1 skrll low = mid + 1;
1293 1.1 skrll }
1294 1.1 skrll
1295 1.1 skrll if (indexentry == NULL)
1296 1.1 skrll return TRUE;
1297 1.1 skrll
1298 1.1 skrll stab = indexentry->stab + STABSIZE;
1299 1.1 skrll file_name = indexentry->file_name;
1300 1.1 skrll }
1301 1.1 skrll
1302 1.1 skrll directory_name = indexentry->directory_name;
1303 1.1 skrll str = indexentry->str;
1304 1.1 skrll
1305 1.1 skrll saw_line = FALSE;
1306 1.1 skrll saw_func = FALSE;
1307 1.1 skrll for (; stab < (indexentry+1)->stab; stab += STABSIZE)
1308 1.1 skrll {
1309 1.1 skrll bfd_boolean done;
1310 1.1 skrll bfd_vma val;
1311 1.1 skrll
1312 1.1 skrll done = FALSE;
1313 1.1 skrll
1314 1.1 skrll switch (stab[TYPEOFF])
1315 1.1 skrll {
1316 1.1 skrll case N_SOL:
1317 1.1 skrll /* The name of an include file. */
1318 1.1 skrll val = bfd_get_32 (abfd, stab + VALOFF);
1319 1.1 skrll if (val <= offset)
1320 1.1 skrll {
1321 1.1 skrll file_name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
1322 1.1 skrll *pline = 0;
1323 1.1 skrll }
1324 1.1 skrll break;
1325 1.1 skrll
1326 1.1 skrll case N_SLINE:
1327 1.1 skrll case N_DSLINE:
1328 1.1 skrll case N_BSLINE:
1329 1.1 skrll /* A line number. If the function was specified, then the value
1330 1.1 skrll is relative to the start of the function. Otherwise, the
1331 1.1 skrll value is an absolute address. */
1332 1.1 skrll val = ((indexentry->function_name ? indexentry->val : 0)
1333 1.1 skrll + bfd_get_32 (abfd, stab + VALOFF));
1334 1.1 skrll /* If this line starts before our desired offset, or if it's
1335 1.1 skrll the first line we've been able to find, use it. The
1336 1.1 skrll !saw_line check works around a bug in GCC 2.95.3, which emits
1337 1.1 skrll the first N_SLINE late. */
1338 1.1 skrll if (!saw_line || val <= offset)
1339 1.1 skrll {
1340 1.1 skrll *pline = bfd_get_16 (abfd, stab + DESCOFF);
1341 1.1 skrll
1342 1.1 skrll #ifdef ENABLE_CACHING
1343 1.1 skrll info->cached_stab = stab;
1344 1.1 skrll info->cached_offset = val;
1345 1.1 skrll info->cached_file_name = file_name;
1346 1.1 skrll info->cached_indexentry = indexentry;
1347 1.1 skrll #endif
1348 1.1 skrll }
1349 1.1 skrll if (val > offset)
1350 1.1 skrll done = TRUE;
1351 1.1 skrll saw_line = TRUE;
1352 1.1 skrll break;
1353 1.1 skrll
1354 1.1 skrll case N_FUN:
1355 1.1 skrll case N_SO:
1356 1.1 skrll if (saw_func || saw_line)
1357 1.1 skrll done = TRUE;
1358 1.1 skrll saw_func = TRUE;
1359 1.1 skrll break;
1360 1.1 skrll }
1361 1.1 skrll
1362 1.1 skrll if (done)
1363 1.1 skrll break;
1364 1.1 skrll }
1365 1.1 skrll
1366 1.1 skrll *pfound = TRUE;
1367 1.1 skrll
1368 1.1 skrll if (file_name == NULL || IS_ABSOLUTE_PATH (file_name)
1369 1.1 skrll || directory_name == NULL)
1370 1.1 skrll *pfilename = file_name;
1371 1.1 skrll else
1372 1.1 skrll {
1373 1.1 skrll size_t dirlen;
1374 1.1 skrll
1375 1.1 skrll dirlen = strlen (directory_name);
1376 1.1 skrll if (info->filename == NULL
1377 1.1 skrll || strncmp (info->filename, directory_name, dirlen) != 0
1378 1.1 skrll || strcmp (info->filename + dirlen, file_name) != 0)
1379 1.1 skrll {
1380 1.1 skrll size_t len;
1381 1.1 skrll
1382 1.1 skrll if (info->filename != NULL)
1383 1.1 skrll free (info->filename);
1384 1.1 skrll len = strlen (file_name) + 1;
1385 1.1 skrll info->filename = bfd_malloc (dirlen + len);
1386 1.1 skrll if (info->filename == NULL)
1387 1.1 skrll return FALSE;
1388 1.1 skrll memcpy (info->filename, directory_name, dirlen);
1389 1.1 skrll memcpy (info->filename + dirlen, file_name, len);
1390 1.1 skrll }
1391 1.1 skrll
1392 1.1 skrll *pfilename = info->filename;
1393 1.1 skrll }
1394 1.1 skrll
1395 1.1 skrll if (indexentry->function_name != NULL)
1396 1.1 skrll {
1397 1.1 skrll char *s;
1398 1.1 skrll
1399 1.1 skrll /* This will typically be something like main:F(0,1), so we want
1400 1.1 skrll to clobber the colon. It's OK to change the name, since the
1401 1.1 skrll string is in our own local storage anyhow. */
1402 1.1 skrll s = strchr (indexentry->function_name, ':');
1403 1.1 skrll if (s != NULL)
1404 1.1 skrll *s = '\0';
1405 1.1 skrll
1406 1.1 skrll *pfnname = indexentry->function_name;
1407 1.1 skrll }
1408 1.1 skrll
1409 1.1 skrll return TRUE;
1410 1.1 skrll }
1411