]>
Commit | Line | Data |
---|---|---|
6724ff46 | 1 | /* Generic symbol-table support for the BFD library. |
c188b0be | 2 | Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. |
6724ff46 RP |
3 | Written by Cygnus Support. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
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 | |
c3246d9b | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
6724ff46 | 20 | |
0cda46cf SC |
21 | /* |
22 | SECTION | |
23 | Symbols | |
24 | ||
c188b0be | 25 | BFD tries to maintain as much symbol information as it can when |
0cda46cf SC |
26 | it moves information from file to file. BFD passes information |
27 | to applications though the <<asymbol>> structure. When the | |
e98e6ec1 | 28 | application requests the symbol table, BFD reads the table in |
0cda46cf | 29 | the native form and translates parts of it into the internal |
c188b0be DM |
30 | format. To maintain more than the information passed to |
31 | applications, some targets keep some information ``behind the | |
32 | scenes'' in a structure only the particular back end knows | |
0cda46cf SC |
33 | about. For example, the coff back end keeps the original |
34 | symbol table structure as well as the canonical structure when | |
35 | a BFD is read in. On output, the coff back end can reconstruct | |
36 | the output symbol table so that no information is lost, even | |
37 | information unique to coff which BFD doesn't know or | |
c188b0be | 38 | understand. If a coff symbol table were read, but were written |
0cda46cf | 39 | through an a.out back end, all the coff specific information |
e98e6ec1 | 40 | would be lost. The symbol table of a BFD |
0cda46cf SC |
41 | is not necessarily read in until a canonicalize request is |
42 | made. Then the BFD back end fills in a table provided by the | |
43 | application with pointers to the canonical information. To | |
44 | output symbols, the application provides BFD with a table of | |
45 | pointers to pointers to <<asymbol>>s. This allows applications | |
c188b0be | 46 | like the linker to output a symbol as it was read, since the ``behind |
57a1867e | 47 | the scenes'' information will be still available. |
6724ff46 | 48 | @menu |
151760d0 RP |
49 | @* Reading Symbols:: |
50 | @* Writing Symbols:: | |
86aac8ea | 51 | @* Mini Symbols:: |
151760d0 RP |
52 | @* typedef asymbol:: |
53 | @* symbol handling functions:: | |
6724ff46 RP |
54 | @end menu |
55 | ||
c188b0be DM |
56 | INODE |
57 | Reading Symbols, Writing Symbols, Symbols, Symbols | |
0cda46cf | 58 | SUBSECTION |
c91884b3 | 59 | Reading symbols |
0cda46cf | 60 | |
c188b0be | 61 | There are two stages to reading a symbol table from a BFD: |
0cda46cf | 62 | allocating storage, and the actual reading process. This is an |
c188b0be | 63 | excerpt from an application which reads the symbol table: |
0cda46cf | 64 | |
ec591fcf | 65 | | long storage_needed; |
e98e6ec1 | 66 | | asymbol **symbol_table; |
ec591fcf ILT |
67 | | long number_of_symbols; |
68 | | long i; | |
57a1867e | 69 | | |
ec591fcf ILT |
70 | | storage_needed = bfd_get_symtab_upper_bound (abfd); |
71 | | | |
72 | | if (storage_needed < 0) | |
73 | | FAIL | |
57a1867e | 74 | | |
e98e6ec1 SC |
75 | | if (storage_needed == 0) { |
76 | | return ; | |
77 | | } | |
57a1867e | 78 | | symbol_table = (asymbol **) xmalloc (storage_needed); |
e98e6ec1 | 79 | | ... |
57a1867e DM |
80 | | number_of_symbols = |
81 | | bfd_canonicalize_symtab (abfd, symbol_table); | |
82 | | | |
ec591fcf ILT |
83 | | if (number_of_symbols < 0) |
84 | | FAIL | |
85 | | | |
e98e6ec1 SC |
86 | | for (i = 0; i < number_of_symbols; i++) { |
87 | | process_symbol (symbol_table[i]); | |
88 | | } | |
0cda46cf SC |
89 | |
90 | All storage for the symbols themselves is in an obstack | |
c188b0be | 91 | connected to the BFD; it is freed when the BFD is closed. |
0cda46cf | 92 | |
6724ff46 | 93 | |
c188b0be | 94 | INODE |
86aac8ea | 95 | Writing Symbols, Mini Symbols, Reading Symbols, Symbols |
0cda46cf | 96 | SUBSECTION |
c91884b3 | 97 | Writing symbols |
0cda46cf | 98 | |
0cda46cf SC |
99 | Writing of a symbol table is automatic when a BFD open for |
100 | writing is closed. The application attaches a vector of | |
101 | pointers to pointers to symbols to the BFD being written, and | |
102 | fills in the symbol count. The close and cleanup code reads | |
103 | through the table provided and performs all the necessary | |
c188b0be DM |
104 | operations. The BFD output code must always be provided with an |
105 | ``owned'' symbol: one which has come from another BFD, or one | |
106 | which has been created using <<bfd_make_empty_symbol>>. Here is an | |
0cda46cf SC |
107 | example showing the creation of a symbol table with only one element: |
108 | ||
e98e6ec1 | 109 | | #include "bfd.h" |
57a1867e | 110 | | main() |
e98e6ec1 SC |
111 | | { |
112 | | bfd *abfd; | |
113 | | asymbol *ptrs[2]; | |
114 | | asymbol *new; | |
57a1867e | 115 | | |
e98e6ec1 SC |
116 | | abfd = bfd_openw("foo","a.out-sunos-big"); |
117 | | bfd_set_format(abfd, bfd_object); | |
118 | | new = bfd_make_empty_symbol(abfd); | |
119 | | new->name = "dummy_symbol"; | |
120 | | new->section = bfd_make_section_old_way(abfd, ".text"); | |
121 | | new->flags = BSF_GLOBAL; | |
122 | | new->value = 0x12345; | |
57a1867e | 123 | | |
e98e6ec1 SC |
124 | | ptrs[0] = new; |
125 | | ptrs[1] = (asymbol *)0; | |
57a1867e | 126 | | |
e98e6ec1 SC |
127 | | bfd_set_symtab(abfd, ptrs, 1); |
128 | | bfd_close(abfd); | |
129 | | } | |
57a1867e DM |
130 | | |
131 | | ./makesym | |
e98e6ec1 SC |
132 | | nm foo |
133 | | 00012345 A dummy_symbol | |
6724ff46 | 134 | |
0cda46cf | 135 | Many formats cannot represent arbitary symbol information; for |
c188b0be | 136 | instance, the <<a.out>> object format does not allow an |
0cda46cf SC |
137 | arbitary number of sections. A symbol pointing to a section |
138 | which is not one of <<.text>>, <<.data>> or <<.bss>> cannot | |
57a1867e | 139 | be described. |
6724ff46 | 140 | |
c3246d9b | 141 | INODE |
86aac8ea | 142 | Mini Symbols, typedef asymbol, Writing Symbols, Symbols |
c3246d9b | 143 | SUBSECTION |
86aac8ea | 144 | Mini Symbols |
c3246d9b ILT |
145 | |
146 | Mini symbols provide read-only access to the symbol table. | |
147 | They use less memory space, but require more time to access. | |
148 | They can be useful for tools like nm or objdump, which may | |
149 | have to handle symbol tables of extremely large executables. | |
150 | ||
151 | The <<bfd_read_minisymbols>> function will read the symbols | |
152 | into memory in an internal form. It will return a <<void *>> | |
153 | pointer to a block of memory, a symbol count, and the size of | |
154 | each symbol. The pointer is allocated using <<malloc>>, and | |
155 | should be freed by the caller when it is no longer needed. | |
156 | ||
157 | The function <<bfd_minisymbol_to_symbol>> will take a pointer | |
158 | to a minisymbol, and a pointer to a structure returned by | |
159 | <<bfd_make_empty_symbol>>, and return a <<asymbol>> structure. | |
160 | The return value may or may not be the same as the value from | |
161 | <<bfd_make_empty_symbol>> which was passed in. | |
162 | ||
6724ff46 RP |
163 | */ |
164 | ||
165 | ||
c188b0be | 166 | |
e98e6ec1 | 167 | /* |
c188b0be DM |
168 | DOCDD |
169 | INODE | |
86aac8ea | 170 | typedef asymbol, symbol handling functions, Mini Symbols, Symbols |
6724ff46 RP |
171 | |
172 | */ | |
0cda46cf | 173 | /* |
e98e6ec1 | 174 | SUBSECTION |
0cda46cf | 175 | typedef asymbol |
6724ff46 | 176 | |
0cda46cf | 177 | An <<asymbol>> has the form: |
6724ff46 | 178 | |
e98e6ec1 SC |
179 | */ |
180 | ||
181 | /* | |
182 | CODE_FRAGMENT | |
183 | ||
c188b0be | 184 | . |
57a1867e | 185 | .typedef struct symbol_cache_entry |
0cda46cf | 186 | .{ |
e98e6ec1 SC |
187 | . {* A pointer to the BFD which owns the symbol. This information |
188 | . is necessary so that a back end can work out what additional | |
c188b0be DM |
189 | . information (invisible to the application writer) is carried |
190 | . with the symbol. | |
191 | . | |
192 | . This field is *almost* redundant, since you can use section->owner | |
193 | . instead, except that some symbols point to the global sections | |
194 | . bfd_{abs,com,und}_section. This could be fixed by making | |
195 | . these globals be per-bfd (or per-target-flavor). FIXME. *} | |
e98e6ec1 | 196 | . |
c188b0be | 197 | . struct _bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *} |
e98e6ec1 | 198 | . |
c188b0be | 199 | . {* The text of the symbol. The name is left alone, and not copied; the |
e98e6ec1 SC |
200 | . application may not alter it. *} |
201 | . CONST char *name; | |
202 | . | |
c188b0be DM |
203 | . {* The value of the symbol. This really should be a union of a |
204 | . numeric value with a pointer, since some flags indicate that | |
205 | . a pointer to another symbol is stored here. *} | |
e98e6ec1 SC |
206 | . symvalue value; |
207 | . | |
208 | . {* Attributes of a symbol: *} | |
209 | . | |
0cda46cf | 210 | .#define BSF_NO_FLAGS 0x00 |
e98e6ec1 SC |
211 | . |
212 | . {* The symbol has local scope; <<static>> in <<C>>. The value | |
213 | . is the offset into the section of the data. *} | |
0cda46cf | 214 | .#define BSF_LOCAL 0x01 |
e98e6ec1 SC |
215 | . |
216 | . {* The symbol has global scope; initialized data in <<C>>. The | |
217 | . value is the offset into the section of the data. *} | |
0cda46cf | 218 | .#define BSF_GLOBAL 0x02 |
e98e6ec1 | 219 | . |
c188b0be | 220 | . {* The symbol has global scope and is exported. The value is |
e98e6ec1 | 221 | . the offset into the section of the data. *} |
c188b0be | 222 | .#define BSF_EXPORT BSF_GLOBAL {* no real difference *} |
e98e6ec1 SC |
223 | . |
224 | . {* A normal C symbol would be one of: | |
225 | . <<BSF_LOCAL>>, <<BSF_FORT_COMM>>, <<BSF_UNDEFINED>> or | |
c188b0be | 226 | . <<BSF_GLOBAL>> *} |
e98e6ec1 SC |
227 | . |
228 | . {* The symbol is a debugging record. The value has an arbitary | |
229 | . meaning. *} | |
c188b0be | 230 | .#define BSF_DEBUGGING 0x08 |
e98e6ec1 | 231 | . |
c188b0be DM |
232 | . {* The symbol denotes a function entry point. Used in ELF, |
233 | . perhaps others someday. *} | |
234 | .#define BSF_FUNCTION 0x10 | |
e98e6ec1 | 235 | . |
c188b0be DM |
236 | . {* Used by the linker. *} |
237 | .#define BSF_KEEP 0x20 | |
238 | .#define BSF_KEEP_G 0x40 | |
e98e6ec1 | 239 | . |
c188b0be DM |
240 | . {* A weak global symbol, overridable without warnings by |
241 | . a regular global symbol of the same name. *} | |
242 | .#define BSF_WEAK 0x80 | |
243 | . | |
244 | . {* This symbol was created to point to a section, e.g. ELF's | |
245 | . STT_SECTION symbols. *} | |
246 | .#define BSF_SECTION_SYM 0x100 | |
e98e6ec1 SC |
247 | . |
248 | . {* The symbol used to be a common symbol, but now it is | |
249 | . allocated. *} | |
c188b0be | 250 | .#define BSF_OLD_COMMON 0x200 |
e98e6ec1 SC |
251 | . |
252 | . {* The default value for common data. *} | |
0cda46cf | 253 | .#define BFD_FORT_COMM_DEFAULT_VALUE 0 |
e98e6ec1 SC |
254 | . |
255 | . {* In some files the type of a symbol sometimes alters its | |
256 | . location in an output file - ie in coff a <<ISFCN>> symbol | |
257 | . which is also <<C_EXT>> symbol appears where it was | |
258 | . declared and not at the end of a section. This bit is set | |
259 | . by the target BFD part to convey this information. *} | |
260 | . | |
c188b0be | 261 | .#define BSF_NOT_AT_END 0x400 |
e98e6ec1 SC |
262 | . |
263 | . {* Signal that the symbol is the label of constructor section. *} | |
c188b0be | 264 | .#define BSF_CONSTRUCTOR 0x800 |
e98e6ec1 | 265 | . |
86aac8ea ILT |
266 | . {* Signal that the symbol is a warning symbol. The name is a |
267 | . warning. The name of the next symbol is the one to warn about; | |
268 | . if a reference is made to a symbol with the same name as the next | |
269 | . symbol, a warning is issued by the linker. *} | |
c188b0be | 270 | .#define BSF_WARNING 0x1000 |
e98e6ec1 | 271 | . |
86aac8ea ILT |
272 | . {* Signal that the symbol is indirect. This symbol is an indirect |
273 | . pointer to the symbol with the same name as the next symbol. *} | |
c188b0be DM |
274 | .#define BSF_INDIRECT 0x2000 |
275 | . | |
276 | . {* BSF_FILE marks symbols that contain a file name. This is used | |
277 | . for ELF STT_FILE symbols. *} | |
278 | .#define BSF_FILE 0x4000 | |
e98e6ec1 | 279 | . |
0ee75d02 ILT |
280 | . {* Symbol is from dynamic linking information. *} |
281 | .#define BSF_DYNAMIC 0x8000 | |
282 | . | |
0cda46cf | 283 | . flagword flags; |
e98e6ec1 | 284 | . |
57a1867e | 285 | . {* A pointer to the section to which this symbol is |
e98e6ec1 | 286 | . relative. This will always be non NULL, there are special |
89665c85 | 287 | . sections for undefined and absolute symbols. *} |
0cda46cf | 288 | . struct sec *section; |
e98e6ec1 | 289 | . |
89665c85 SC |
290 | . {* Back end special data. *} |
291 | . union | |
292 | . { | |
293 | . PTR p; | |
294 | . bfd_vma i; | |
295 | . } udata; | |
e98e6ec1 | 296 | . |
0cda46cf | 297 | .} asymbol; |
6724ff46 RP |
298 | */ |
299 | ||
6724ff46 | 300 | #include "bfd.h" |
7d68537f | 301 | #include "sysdep.h" |
6724ff46 | 302 | #include "libbfd.h" |
86aac8ea | 303 | #include "bfdlink.h" |
e98e6ec1 | 304 | #include "aout/stab_gnu.h" |
57a1867e | 305 | |
0cda46cf | 306 | /* |
c188b0be DM |
307 | DOCDD |
308 | INODE | |
309 | symbol handling functions, , typedef asymbol, Symbols | |
0cda46cf | 310 | SUBSECTION |
c91884b3 | 311 | Symbol handling functions |
6724ff46 RP |
312 | */ |
313 | ||
0cda46cf SC |
314 | /* |
315 | FUNCTION | |
ec591fcf | 316 | bfd_get_symtab_upper_bound |
0cda46cf SC |
317 | |
318 | DESCRIPTION | |
c188b0be DM |
319 | Return the number of bytes required to store a vector of pointers |
320 | to <<asymbols>> for all the symbols in the BFD @var{abfd}, | |
0cda46cf | 321 | including a terminal NULL pointer. If there are no symbols in |
ec591fcf ILT |
322 | the BFD, then return 0. If an error occurs, return -1. |
323 | ||
324 | .#define bfd_get_symtab_upper_bound(abfd) \ | |
325 | . BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd)) | |
326 | ||
327 | */ | |
0cda46cf | 328 | |
ec591fcf ILT |
329 | /* |
330 | FUNCTION | |
331 | bfd_is_local_label | |
6724ff46 | 332 | |
ec591fcf ILT |
333 | SYNOPSIS |
334 | boolean bfd_is_local_label(bfd *abfd, asymbol *sym); | |
335 | ||
336 | DESCRIPTION | |
337 | Return true if the given symbol @var{sym} in the BFD @var{abfd} is | |
338 | a compiler generated local label, else return false. | |
339 | .#define bfd_is_local_label(abfd, sym) \ | |
340 | . BFD_SEND (abfd, _bfd_is_local_label,(abfd, sym)) | |
6724ff46 RP |
341 | */ |
342 | ||
0cda46cf SC |
343 | /* |
344 | FUNCTION | |
345 | bfd_canonicalize_symtab | |
346 | ||
347 | DESCRIPTION | |
c188b0be DM |
348 | Read the symbols from the BFD @var{abfd}, and fills in |
349 | the vector @var{location} with pointers to the symbols and | |
57a1867e | 350 | a trailing NULL. |
c188b0be | 351 | Return the actual number of symbol pointers, not |
0cda46cf | 352 | including the NULL. |
6724ff46 | 353 | |
6724ff46 | 354 | |
0cda46cf SC |
355 | .#define bfd_canonicalize_symtab(abfd, location) \ |
356 | . BFD_SEND (abfd, _bfd_canonicalize_symtab,\ | |
357 | . (abfd, location)) | |
358 | ||
6724ff46 RP |
359 | */ |
360 | ||
361 | ||
0cda46cf SC |
362 | /* |
363 | FUNCTION | |
364 | bfd_set_symtab | |
365 | ||
0cda46cf | 366 | SYNOPSIS |
c188b0be DM |
367 | boolean bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int count); |
368 | ||
369 | DESCRIPTION | |
370 | Arrange that when the output BFD @var{abfd} is closed, | |
371 | the table @var{location} of @var{count} pointers to symbols | |
372 | will be written. | |
6724ff46 RP |
373 | */ |
374 | ||
375 | boolean | |
376 | bfd_set_symtab (abfd, location, symcount) | |
377 | bfd *abfd; | |
378 | asymbol **location; | |
379 | unsigned int symcount; | |
380 | { | |
57a1867e DM |
381 | if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) |
382 | { | |
383 | bfd_set_error (bfd_error_invalid_operation); | |
384 | return false; | |
385 | } | |
6724ff46 RP |
386 | |
387 | bfd_get_outsymbols (abfd) = location; | |
388 | bfd_get_symcount (abfd) = symcount; | |
389 | return true; | |
390 | } | |
391 | ||
0cda46cf SC |
392 | /* |
393 | FUNCTION | |
394 | bfd_print_symbol_vandf | |
6724ff46 | 395 | |
0cda46cf SC |
396 | SYNOPSIS |
397 | void bfd_print_symbol_vandf(PTR file, asymbol *symbol); | |
c188b0be DM |
398 | |
399 | DESCRIPTION | |
400 | Print the value and flags of the @var{symbol} supplied to the | |
401 | stream @var{file}. | |
6724ff46 RP |
402 | */ |
403 | void | |
57a1867e DM |
404 | bfd_print_symbol_vandf (arg, symbol) |
405 | PTR arg; | |
406 | asymbol *symbol; | |
6724ff46 | 407 | { |
0ee75d02 | 408 | FILE *file = (FILE *) arg; |
6724ff46 | 409 | flagword type = symbol->flags; |
57a1867e DM |
410 | if (symbol->section != (asection *) NULL) |
411 | { | |
412 | fprintf_vma (file, symbol->value + symbol->section->vma); | |
413 | } | |
414 | else | |
415 | { | |
416 | fprintf_vma (file, symbol->value); | |
417 | } | |
0ee75d02 ILT |
418 | |
419 | /* This presumes that a symbol can not be both BSF_DEBUGGING and | |
89665c85 | 420 | BSF_DYNAMIC, nor both BSF_FUNCTION and BSF_FILE. */ |
57a1867e | 421 | fprintf (file, " %c%c%c%c%c%c%c", |
89665c85 SC |
422 | ((type & BSF_LOCAL) |
423 | ? (type & BSF_GLOBAL) ? '!' : 'l' | |
424 | : (type & BSF_GLOBAL) ? 'g' : ' '), | |
57a1867e DM |
425 | (type & BSF_WEAK) ? 'w' : ' ', |
426 | (type & BSF_CONSTRUCTOR) ? 'C' : ' ', | |
427 | (type & BSF_WARNING) ? 'W' : ' ', | |
428 | (type & BSF_INDIRECT) ? 'I' : ' ', | |
89665c85 SC |
429 | (type & BSF_DEBUGGING) ? 'd' : (type & BSF_DYNAMIC) ? 'D' : ' ', |
430 | (type & BSF_FUNCTION) ? 'F' : (type & BSF_FILE) ? 'f' : ' '); | |
6724ff46 RP |
431 | } |
432 | ||
433 | ||
0cda46cf SC |
434 | /* |
435 | FUNCTION | |
436 | bfd_make_empty_symbol | |
437 | ||
438 | DESCRIPTION | |
c188b0be DM |
439 | Create a new <<asymbol>> structure for the BFD @var{abfd} |
440 | and return a pointer to it. | |
6724ff46 | 441 | |
c188b0be | 442 | This routine is necessary because each back end has private |
0cda46cf SC |
443 | information surrounding the <<asymbol>>. Building your own |
444 | <<asymbol>> and pointing to it will not create the private | |
445 | information, and will cause problems later on. | |
446 | ||
447 | .#define bfd_make_empty_symbol(abfd) \ | |
448 | . BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd)) | |
6724ff46 | 449 | */ |
7d68537f | 450 | |
c188b0be DM |
451 | /* |
452 | FUNCTION | |
453 | bfd_make_debug_symbol | |
454 | ||
455 | DESCRIPTION | |
456 | Create a new <<asymbol>> structure for the BFD @var{abfd}, | |
457 | to be used as a debugging symbol. Further details of its use have | |
458 | yet to be worked out. | |
459 | ||
460 | .#define bfd_make_debug_symbol(abfd,ptr,size) \ | |
461 | . BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size)) | |
462 | */ | |
463 | ||
464 | struct section_to_type | |
465 | { | |
466 | CONST char *section; | |
467 | char type; | |
468 | }; | |
469 | ||
470 | /* Map section names to POSIX/BSD single-character symbol types. | |
471 | This table is probably incomplete. It is sorted for convenience of | |
472 | adding entries. Since it is so short, a linear search is used. */ | |
57a1867e DM |
473 | static CONST struct section_to_type stt[] = |
474 | { | |
c188b0be DM |
475 | {"*DEBUG*", 'N'}, |
476 | {".bss", 'b'}, | |
477 | {".data", 'd'}, | |
ec591fcf ILT |
478 | {".rdata", 'r'}, /* Read only data. */ |
479 | {".rodata", 'r'}, /* Read only data. */ | |
480 | {".sbss", 's'}, /* Small BSS (uninitialized data). */ | |
481 | {".scommon", 'c'}, /* Small common. */ | |
482 | {".sdata", 'g'}, /* Small initialized data. */ | |
c188b0be DM |
483 | {".text", 't'}, |
484 | {0, 0} | |
485 | }; | |
486 | ||
487 | /* Return the single-character symbol type corresponding to | |
89665c85 SC |
488 | section S, or '?' for an unknown COFF section. |
489 | ||
490 | Check for any leading string which matches, so .text5 returns | |
491 | 't' as well as .text */ | |
c188b0be DM |
492 | |
493 | static char | |
494 | coff_section_type (s) | |
495 | char *s; | |
496 | { | |
497 | CONST struct section_to_type *t; | |
498 | ||
89665c85 SC |
499 | for (t = &stt[0]; t->section; t++) |
500 | if (!strncmp (s, t->section, strlen (t->section))) | |
c188b0be | 501 | return t->type; |
89665c85 | 502 | |
c188b0be DM |
503 | return '?'; |
504 | } | |
505 | ||
506 | #ifndef islower | |
507 | #define islower(c) ((c) >= 'a' && (c) <= 'z') | |
508 | #endif | |
509 | #ifndef toupper | |
510 | #define toupper(c) (islower(c) ? ((c) & ~0x20) : (c)) | |
511 | #endif | |
512 | ||
0cda46cf SC |
513 | /* |
514 | FUNCTION | |
515 | bfd_decode_symclass | |
516 | ||
517 | DESCRIPTION | |
c188b0be DM |
518 | Return a character corresponding to the symbol |
519 | class of @var{symbol}, or '?' for an unknown class. | |
7d68537f | 520 | |
0cda46cf SC |
521 | SYNOPSIS |
522 | int bfd_decode_symclass(asymbol *symbol); | |
7d68537f FF |
523 | */ |
524 | int | |
57a1867e DM |
525 | bfd_decode_symclass (symbol) |
526 | asymbol *symbol; | |
7d68537f | 527 | { |
c188b0be DM |
528 | char c; |
529 | ||
530 | if (bfd_is_com_section (symbol->section)) | |
531 | return 'C'; | |
ec591fcf | 532 | if (bfd_is_und_section (symbol->section)) |
c188b0be | 533 | return 'U'; |
ec591fcf | 534 | if (bfd_is_ind_section (symbol->section)) |
c188b0be | 535 | return 'I'; |
89665c85 SC |
536 | if (symbol->flags & BSF_WEAK) |
537 | return 'W'; | |
57a1867e | 538 | if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL))) |
c188b0be DM |
539 | return '?'; |
540 | ||
ec591fcf | 541 | if (bfd_is_abs_section (symbol->section)) |
c188b0be DM |
542 | c = 'a'; |
543 | else if (symbol->section) | |
544 | c = coff_section_type (symbol->section->name); | |
545 | else | |
546 | return '?'; | |
547 | if (symbol->flags & BSF_GLOBAL) | |
548 | c = toupper (c); | |
549 | return c; | |
7d68537f FF |
550 | |
551 | /* We don't have to handle these cases just yet, but we will soon: | |
57a1867e DM |
552 | N_SETV: 'v'; |
553 | N_SETA: 'l'; | |
7d68537f FF |
554 | N_SETT: 'x'; |
555 | N_SETD: 'z'; | |
556 | N_SETB: 's'; | |
557 | N_INDR: 'i'; | |
558 | */ | |
7d68537f | 559 | } |
e98e6ec1 | 560 | |
c188b0be DM |
561 | /* |
562 | FUNCTION | |
563 | bfd_symbol_info | |
564 | ||
565 | DESCRIPTION | |
566 | Fill in the basic info about symbol that nm needs. | |
567 | Additional info may be added by the back-ends after | |
568 | calling this function. | |
569 | ||
570 | SYNOPSIS | |
571 | void bfd_symbol_info(asymbol *symbol, symbol_info *ret); | |
572 | */ | |
e98e6ec1 | 573 | |
c188b0be | 574 | void |
57a1867e DM |
575 | bfd_symbol_info (symbol, ret) |
576 | asymbol *symbol; | |
577 | symbol_info *ret; | |
c188b0be DM |
578 | { |
579 | ret->type = bfd_decode_symclass (symbol); | |
580 | if (ret->type != 'U') | |
57a1867e | 581 | ret->value = symbol->value + symbol->section->vma; |
c188b0be DM |
582 | else |
583 | ret->value = 0; | |
584 | ret->name = symbol->name; | |
585 | } | |
586 | ||
587 | void | |
57a1867e | 588 | bfd_symbol_is_absolute () |
e98e6ec1 | 589 | { |
57a1867e | 590 | abort (); |
e98e6ec1 | 591 | } |
89665c85 SC |
592 | |
593 | /* | |
594 | FUNCTION | |
595 | bfd_copy_private_symbol_data | |
596 | ||
597 | SYNOPSIS | |
598 | boolean bfd_copy_private_symbol_data(bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym); | |
599 | ||
600 | DESCRIPTION | |
601 | Copy private symbol information from @var{isym} in the BFD | |
602 | @var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}. | |
603 | Return <<true>> on success, <<false>> on error. Possible error | |
604 | returns are: | |
605 | ||
606 | o <<bfd_error_no_memory>> - | |
607 | Not enough memory exists to create private data for @var{osec}. | |
608 | ||
609 | .#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \ | |
610 | . BFD_SEND (ibfd, _bfd_copy_private_symbol_data, \ | |
611 | . (ibfd, isymbol, obfd, osymbol)) | |
612 | ||
613 | */ | |
c3246d9b ILT |
614 | |
615 | /* The generic version of the function which returns mini symbols. | |
616 | This is used when the backend does not provide a more efficient | |
617 | version. It just uses BFD asymbol structures as mini symbols. */ | |
618 | ||
619 | long | |
620 | _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep) | |
621 | bfd *abfd; | |
622 | boolean dynamic; | |
623 | PTR *minisymsp; | |
624 | unsigned int *sizep; | |
625 | { | |
626 | long storage; | |
627 | asymbol **syms = NULL; | |
628 | long symcount; | |
629 | ||
630 | if (dynamic) | |
631 | storage = bfd_get_dynamic_symtab_upper_bound (abfd); | |
632 | else | |
633 | storage = bfd_get_symtab_upper_bound (abfd); | |
634 | if (storage < 0) | |
635 | goto error_return; | |
636 | ||
86aac8ea | 637 | syms = (asymbol **) bfd_malloc ((size_t) storage); |
c3246d9b | 638 | if (syms == NULL) |
86aac8ea | 639 | goto error_return; |
c3246d9b ILT |
640 | |
641 | if (dynamic) | |
642 | symcount = bfd_canonicalize_dynamic_symtab (abfd, syms); | |
643 | else | |
644 | symcount = bfd_canonicalize_symtab (abfd, syms); | |
645 | if (symcount < 0) | |
646 | goto error_return; | |
647 | ||
648 | *minisymsp = (PTR) syms; | |
649 | *sizep = sizeof (asymbol *); | |
650 | return symcount; | |
651 | ||
652 | error_return: | |
653 | if (syms != NULL) | |
654 | free (syms); | |
655 | return -1; | |
656 | } | |
657 | ||
658 | /* The generic version of the function which converts a minisymbol to | |
659 | an asymbol. We don't worry about the sym argument we are passed; | |
660 | we just return the asymbol the minisymbol points to. */ | |
661 | ||
662 | /*ARGSUSED*/ | |
663 | asymbol * | |
664 | _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym) | |
665 | bfd *abfd; | |
666 | boolean dynamic; | |
667 | const PTR minisym; | |
668 | asymbol *sym; | |
669 | { | |
670 | return *(asymbol **) minisym; | |
671 | } | |
86aac8ea ILT |
672 | |
673 | /* Look through stabs debugging information in .stab and .stabstr | |
674 | sections to find the source file and line closest to a desired | |
675 | location. This is used by COFF and ELF targets. It sets *pfound | |
676 | to true if it finds some information. The *pinfo field is used to | |
677 | pass cached information in and out of this routine; this first time | |
678 | the routine is called for a BFD, *pinfo should be NULL. The value | |
679 | placed in *pinfo should be saved with the BFD, and passed back each | |
680 | time this function is called. */ | |
681 | ||
682 | /* A pointer to this structure is stored in *pinfo. */ | |
683 | ||
684 | struct stab_find_info | |
685 | { | |
686 | /* The .stab section. */ | |
687 | asection *stabsec; | |
688 | /* The .stabstr section. */ | |
689 | asection *strsec; | |
690 | /* The contents of the .stab section. */ | |
691 | bfd_byte *stabs; | |
692 | /* The contents of the .stabstr section. */ | |
693 | bfd_byte *strs; | |
694 | /* An malloc buffer to hold the file name. */ | |
695 | char *filename; | |
696 | /* Cached values to restart quickly. */ | |
697 | bfd_vma cached_offset; | |
698 | bfd_byte *cached_stab; | |
699 | bfd_byte *cached_str; | |
700 | bfd_size_type cached_stroff; | |
701 | }; | |
702 | ||
703 | boolean | |
704 | _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, pfound, | |
705 | pfilename, pfnname, pline, pinfo) | |
706 | bfd *abfd; | |
707 | asymbol **symbols; | |
708 | asection *section; | |
709 | bfd_vma offset; | |
710 | boolean *pfound; | |
711 | const char **pfilename; | |
712 | const char **pfnname; | |
713 | unsigned int *pline; | |
714 | PTR *pinfo; | |
715 | { | |
716 | struct stab_find_info *info; | |
717 | bfd_size_type stabsize, strsize; | |
718 | bfd_byte *stab, *stabend, *str; | |
719 | bfd_size_type stroff; | |
720 | bfd_vma fnaddr; | |
721 | char *directory_name, *main_file_name, *current_file_name, *line_file_name; | |
722 | char *fnname; | |
723 | bfd_vma low_func_vma, low_line_vma; | |
724 | ||
725 | *pfound = false; | |
726 | *pfilename = bfd_get_filename (abfd); | |
727 | *pfnname = NULL; | |
728 | *pline = 0; | |
729 | ||
730 | info = (struct stab_find_info *) *pinfo; | |
731 | if (info != NULL) | |
732 | { | |
733 | if (info->stabsec == NULL || info->strsec == NULL) | |
734 | { | |
735 | /* No stabs debugging information. */ | |
736 | return true; | |
737 | } | |
738 | ||
739 | stabsize = info->stabsec->_raw_size; | |
740 | strsize = info->strsec->_raw_size; | |
741 | } | |
742 | else | |
743 | { | |
744 | long reloc_size, reloc_count; | |
745 | arelent **reloc_vector; | |
746 | ||
747 | info = (struct stab_find_info *) bfd_zalloc (abfd, sizeof *info); | |
748 | if (info == NULL) | |
749 | return false; | |
750 | ||
751 | /* FIXME: When using the linker --split-by-file or | |
752 | --split-by-reloc options, it is possible for the .stab and | |
753 | .stabstr sections to be split. We should handle that. */ | |
754 | ||
755 | info->stabsec = bfd_get_section_by_name (abfd, ".stab"); | |
756 | info->strsec = bfd_get_section_by_name (abfd, ".stabstr"); | |
757 | ||
758 | if (info->stabsec == NULL || info->strsec == NULL) | |
759 | { | |
760 | /* No stabs debugging information. Set *pinfo so that we | |
761 | can return quickly in the info != NULL case above. */ | |
762 | *pinfo = info; | |
763 | return true; | |
764 | } | |
765 | ||
766 | stabsize = info->stabsec->_raw_size; | |
767 | strsize = info->strsec->_raw_size; | |
768 | ||
769 | info->stabs = (bfd_byte *) bfd_alloc (abfd, stabsize); | |
770 | info->strs = (bfd_byte *) bfd_alloc (abfd, strsize); | |
771 | if (info->stabs == NULL || info->strs == NULL) | |
772 | return false; | |
773 | ||
774 | if (! bfd_get_section_contents (abfd, info->stabsec, info->stabs, 0, | |
775 | stabsize) | |
776 | || ! bfd_get_section_contents (abfd, info->strsec, info->strs, 0, | |
777 | strsize)) | |
778 | return false; | |
779 | ||
780 | /* If this is a relocateable object file, we have to relocate | |
781 | the entries in .stab. This should always be simple 32 bit | |
782 | relocations against symbols defined in this object file, so | |
783 | this should be no big deal. */ | |
784 | reloc_size = bfd_get_reloc_upper_bound (abfd, info->stabsec); | |
785 | if (reloc_size < 0) | |
786 | return false; | |
787 | reloc_vector = (arelent **) bfd_malloc (reloc_size); | |
788 | if (reloc_vector == NULL && reloc_size != 0) | |
789 | return false; | |
790 | reloc_count = bfd_canonicalize_reloc (abfd, info->stabsec, reloc_vector, | |
791 | symbols); | |
792 | if (reloc_count < 0) | |
793 | { | |
794 | if (reloc_vector != NULL) | |
795 | free (reloc_vector); | |
796 | return false; | |
797 | } | |
798 | if (reloc_count > 0) | |
799 | { | |
800 | arelent **pr; | |
801 | ||
802 | for (pr = reloc_vector; *pr != NULL; pr++) | |
803 | { | |
804 | arelent *r; | |
805 | unsigned long val; | |
806 | asymbol *sym; | |
807 | ||
808 | r = *pr; | |
809 | if (r->howto->rightshift != 0 | |
810 | || r->howto->size != 2 | |
811 | || r->howto->bitsize != 32 | |
812 | || r->howto->pc_relative | |
813 | || r->howto->bitpos != 0 | |
814 | || r->howto->dst_mask != 0xffffffff) | |
815 | { | |
816 | (*_bfd_error_handler) | |
817 | ("Unsupported .stab relocation"); | |
818 | bfd_set_error (bfd_error_invalid_operation); | |
819 | if (reloc_vector != NULL) | |
820 | free (reloc_vector); | |
821 | return false; | |
822 | } | |
823 | ||
824 | val = bfd_get_32 (abfd, info->stabs + r->address); | |
825 | val &= r->howto->src_mask; | |
826 | sym = *r->sym_ptr_ptr; | |
827 | val += sym->value + sym->section->vma + r->addend; | |
828 | bfd_put_32 (abfd, val, info->stabs + r->address); | |
829 | } | |
830 | } | |
831 | ||
832 | if (reloc_vector != NULL) | |
833 | free (reloc_vector); | |
834 | ||
835 | *pinfo = info; | |
836 | } | |
837 | ||
838 | /* We are passed a section relative offset. The offsets in the | |
839 | stabs information are absolute. */ | |
840 | offset += bfd_get_section_vma (abfd, section); | |
841 | ||
842 | /* Stabs entries use a 12 byte format: | |
843 | 4 byte string table index | |
844 | 1 byte stab type | |
845 | 1 byte stab other field | |
846 | 2 byte stab desc field | |
847 | 4 byte stab value | |
848 | FIXME: This will have to change for a 64 bit object format. | |
849 | ||
850 | The stabs symbols are divided into compilation units. For the | |
851 | first entry in each unit, the type of 0, the value is the length | |
852 | of the string table for this unit, and the desc field is the | |
853 | number of stabs symbols for this unit. */ | |
854 | ||
855 | #define STRDXOFF (0) | |
856 | #define TYPEOFF (4) | |
857 | #define OTHEROFF (5) | |
858 | #define DESCOFF (6) | |
859 | #define VALOFF (8) | |
860 | #define STABSIZE (12) | |
861 | ||
862 | /* It would be nice if we could skip ahead to the stabs symbols for | |
863 | the next compilation unit to quickly scan through the compilation | |
864 | units. Unfortunately, since each line number gets a separate | |
865 | stabs entry, it is entirely plausible that a large source file | |
866 | will overflow the 16 bit count of stabs entries. */ | |
867 | fnaddr = 0; | |
868 | directory_name = NULL; | |
869 | main_file_name = NULL; | |
870 | current_file_name = NULL; | |
871 | line_file_name = NULL; | |
872 | fnname = NULL; | |
873 | low_func_vma = 0; | |
874 | low_line_vma = 0; | |
875 | ||
876 | stabend = info->stabs + stabsize; | |
877 | ||
878 | if (info->cached_stab == NULL || offset < info->cached_offset) | |
879 | { | |
880 | stab = info->stabs; | |
881 | str = info->strs; | |
882 | stroff = 0; | |
883 | } | |
884 | else | |
885 | { | |
886 | stab = info->cached_stab; | |
887 | str = info->cached_str; | |
888 | stroff = info->cached_stroff; | |
889 | } | |
890 | ||
891 | info->cached_offset = offset; | |
892 | ||
893 | for (; stab < stabend; stab += STABSIZE) | |
894 | { | |
895 | boolean done; | |
896 | bfd_vma val; | |
897 | char *name; | |
898 | ||
899 | done = false; | |
900 | ||
901 | switch (stab[TYPEOFF]) | |
902 | { | |
903 | case 0: | |
904 | /* This is the first entry in a compilation unit. */ | |
905 | if ((bfd_size_type) ((info->strs + strsize) - str) < stroff) | |
906 | { | |
907 | done = true; | |
908 | break; | |
909 | } | |
910 | str += stroff; | |
911 | stroff = bfd_get_32 (abfd, stab + VALOFF); | |
912 | break; | |
913 | ||
914 | case N_SO: | |
915 | /* The main file name. */ | |
916 | ||
917 | val = bfd_get_32 (abfd, stab + VALOFF); | |
918 | if (val > offset) | |
919 | { | |
920 | done = true; | |
921 | break; | |
922 | } | |
923 | ||
924 | name = str + bfd_get_32 (abfd, stab + STRDXOFF); | |
925 | ||
926 | /* An empty string indicates the end of the compilation | |
927 | unit. */ | |
928 | if (*name == '\0') | |
929 | { | |
930 | /* If there are functions in different sections, they | |
931 | may have addresses larger than val, but we don't want | |
932 | to forget the file name. When there are functions in | |
933 | different cases, there is supposed to be an N_FUN at | |
934 | the end of the function indicating where it ends. */ | |
935 | if (low_func_vma < val || fnname == NULL) | |
936 | main_file_name = NULL; | |
937 | break; | |
938 | } | |
939 | ||
940 | /* We know that we have to get to at least this point in the | |
941 | stabs entries for this offset. */ | |
942 | info->cached_stab = stab; | |
943 | info->cached_str = str; | |
944 | info->cached_stroff = stroff; | |
945 | ||
946 | current_file_name = name; | |
947 | ||
948 | /* Look ahead to the next symbol. Two consecutive N_SO | |
949 | symbols are a directory and a file name. */ | |
950 | if (stab + STABSIZE >= stabend | |
951 | || *(stab + STABSIZE + TYPEOFF) != N_SO) | |
952 | directory_name = NULL; | |
953 | else | |
954 | { | |
955 | stab += STABSIZE; | |
956 | directory_name = current_file_name; | |
957 | current_file_name = str + bfd_get_32 (abfd, stab + STRDXOFF); | |
958 | } | |
959 | ||
960 | main_file_name = current_file_name; | |
961 | ||
962 | break; | |
963 | ||
964 | case N_SOL: | |
965 | /* The name of an include file. */ | |
966 | current_file_name = str + bfd_get_32 (abfd, stab + STRDXOFF); | |
967 | break; | |
968 | ||
969 | case N_SLINE: | |
970 | case N_DSLINE: | |
971 | case N_BSLINE: | |
972 | /* A line number. The value is relative to the start of the | |
973 | current function. */ | |
974 | val = fnaddr + bfd_get_32 (abfd, stab + VALOFF); | |
975 | if (val >= low_line_vma && val <= offset) | |
976 | { | |
977 | *pline = bfd_get_16 (abfd, stab + DESCOFF); | |
978 | low_line_vma = val; | |
979 | line_file_name = current_file_name; | |
980 | } | |
981 | break; | |
982 | ||
983 | case N_FUN: | |
984 | /* A function name. */ | |
985 | val = bfd_get_32 (abfd, stab + VALOFF); | |
986 | name = str + bfd_get_32 (abfd, stab + STRDXOFF); | |
987 | ||
988 | /* An empty string here indicates the end of a function, and | |
989 | the value is relative to fnaddr. */ | |
990 | ||
991 | if (*name == '\0') | |
992 | { | |
993 | val += fnaddr; | |
994 | if (val >= low_func_vma && val < offset) | |
995 | fnname = NULL; | |
996 | } | |
997 | else | |
998 | { | |
999 | if (val >= low_func_vma && val <= offset) | |
1000 | { | |
1001 | fnname = name; | |
1002 | low_func_vma = val; | |
1003 | } | |
1004 | ||
1005 | fnaddr = val; | |
1006 | } | |
1007 | ||
1008 | break; | |
1009 | } | |
1010 | ||
1011 | if (done) | |
1012 | break; | |
1013 | } | |
1014 | ||
1015 | if (main_file_name == NULL) | |
1016 | { | |
1017 | /* No information found. */ | |
1018 | return true; | |
1019 | } | |
1020 | ||
1021 | *pfound = true; | |
1022 | ||
1023 | if (*pline != 0) | |
1024 | main_file_name = line_file_name; | |
1025 | ||
1026 | if (main_file_name != NULL) | |
1027 | { | |
1028 | if (main_file_name[0] == '/' || directory_name == NULL) | |
1029 | *pfilename = main_file_name; | |
1030 | else | |
1031 | { | |
1032 | size_t dirlen; | |
1033 | ||
1034 | dirlen = strlen (directory_name); | |
1035 | if (info->filename == NULL | |
1036 | || strncmp (info->filename, directory_name, dirlen) != 0 | |
1037 | || strcmp (info->filename + dirlen, main_file_name) != 0) | |
1038 | { | |
1039 | if (info->filename != NULL) | |
1040 | free (info->filename); | |
1041 | info->filename = (char *) bfd_malloc (dirlen + | |
1042 | strlen (main_file_name) | |
1043 | + 1); | |
1044 | if (info->filename == NULL) | |
1045 | return false; | |
1046 | strcpy (info->filename, directory_name); | |
1047 | strcpy (info->filename + dirlen, main_file_name); | |
1048 | } | |
1049 | ||
1050 | *pfilename = info->filename; | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | if (fnname != NULL) | |
1055 | { | |
1056 | char *s; | |
1057 | ||
1058 | /* This will typically be something like main:F(0,1), so we want | |
1059 | to clobber the colon. It's OK to change the name, since the | |
1060 | string is in our own local storage anyhow. */ | |
1061 | ||
1062 | s = strchr (fnname, ':'); | |
1063 | if (s != NULL) | |
1064 | *s = '\0'; | |
1065 | ||
1066 | *pfnname = fnname; | |
1067 | } | |
1068 | ||
1069 | return true; | |
1070 | } |