* gas/config/tc-hppa.h (obj_frob_symbol): Delete ELF specific
[binutils-gdb.git] / bfd / syms.c
1 /* Generic symbol-table support for the BFD library.
2 Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
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
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /*
22 SECTION
23 Symbols
24
25 BFD tries to maintain as much symbol information as it can when
26 it moves information from file to file. BFD passes information
27 to applications though the <<asymbol>> structure. When the
28 application requests the symbol table, BFD reads the table in
29 the native form and translates parts of it into the internal
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
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
38 understand. If a coff symbol table were read, but were written
39 through an a.out back end, all the coff specific information
40 would be lost. The symbol table of a BFD
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
46 like the linker to output a symbol as it was read, since the ``behind
47 the scenes'' information will be still available.
48 @menu
49 @* Reading Symbols::
50 @* Writing Symbols::
51 @* typedef asymbol::
52 @* symbol handling functions::
53 @end menu
54
55 INODE
56 Reading Symbols, Writing Symbols, Symbols, Symbols
57 SUBSECTION
58 Reading symbols
59
60 There are two stages to reading a symbol table from a BFD:
61 allocating storage, and the actual reading process. This is an
62 excerpt from an application which reads the symbol table:
63
64 | long storage_needed;
65 | asymbol **symbol_table;
66 | long number_of_symbols;
67 | long i;
68 |
69 | storage_needed = bfd_get_symtab_upper_bound (abfd);
70 |
71 | if (storage_needed < 0)
72 | FAIL
73 |
74 | if (storage_needed == 0) {
75 | return ;
76 | }
77 | symbol_table = (asymbol **) xmalloc (storage_needed);
78 | ...
79 | number_of_symbols =
80 | bfd_canonicalize_symtab (abfd, symbol_table);
81 |
82 | if (number_of_symbols < 0)
83 | FAIL
84 |
85 | for (i = 0; i < number_of_symbols; i++) {
86 | process_symbol (symbol_table[i]);
87 | }
88
89 All storage for the symbols themselves is in an obstack
90 connected to the BFD; it is freed when the BFD is closed.
91
92
93 INODE
94 Writing Symbols, typedef asymbol, Reading Symbols, Symbols
95 SUBSECTION
96 Writing symbols
97
98 Writing of a symbol table is automatic when a BFD open for
99 writing is closed. The application attaches a vector of
100 pointers to pointers to symbols to the BFD being written, and
101 fills in the symbol count. The close and cleanup code reads
102 through the table provided and performs all the necessary
103 operations. The BFD output code must always be provided with an
104 ``owned'' symbol: one which has come from another BFD, or one
105 which has been created using <<bfd_make_empty_symbol>>. Here is an
106 example showing the creation of a symbol table with only one element:
107
108 | #include "bfd.h"
109 | main()
110 | {
111 | bfd *abfd;
112 | asymbol *ptrs[2];
113 | asymbol *new;
114 |
115 | abfd = bfd_openw("foo","a.out-sunos-big");
116 | bfd_set_format(abfd, bfd_object);
117 | new = bfd_make_empty_symbol(abfd);
118 | new->name = "dummy_symbol";
119 | new->section = bfd_make_section_old_way(abfd, ".text");
120 | new->flags = BSF_GLOBAL;
121 | new->value = 0x12345;
122 |
123 | ptrs[0] = new;
124 | ptrs[1] = (asymbol *)0;
125 |
126 | bfd_set_symtab(abfd, ptrs, 1);
127 | bfd_close(abfd);
128 | }
129 |
130 | ./makesym
131 | nm foo
132 | 00012345 A dummy_symbol
133
134 Many formats cannot represent arbitary symbol information; for
135 instance, the <<a.out>> object format does not allow an
136 arbitary number of sections. A symbol pointing to a section
137 which is not one of <<.text>>, <<.data>> or <<.bss>> cannot
138 be described.
139
140 */
141
142
143
144 /*
145 DOCDD
146 INODE
147 typedef asymbol, symbol handling functions, Writing Symbols, Symbols
148
149 */
150 /*
151 SUBSECTION
152 typedef asymbol
153
154 An <<asymbol>> has the form:
155
156 */
157
158 /*
159 CODE_FRAGMENT
160
161 .
162 .typedef struct symbol_cache_entry
163 .{
164 . {* A pointer to the BFD which owns the symbol. This information
165 . is necessary so that a back end can work out what additional
166 . information (invisible to the application writer) is carried
167 . with the symbol.
168 .
169 . This field is *almost* redundant, since you can use section->owner
170 . instead, except that some symbols point to the global sections
171 . bfd_{abs,com,und}_section. This could be fixed by making
172 . these globals be per-bfd (or per-target-flavor). FIXME. *}
173 .
174 . struct _bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *}
175 .
176 . {* The text of the symbol. The name is left alone, and not copied; the
177 . application may not alter it. *}
178 . CONST char *name;
179 .
180 . {* The value of the symbol. This really should be a union of a
181 . numeric value with a pointer, since some flags indicate that
182 . a pointer to another symbol is stored here. *}
183 . symvalue value;
184 .
185 . {* Attributes of a symbol: *}
186 .
187 .#define BSF_NO_FLAGS 0x00
188 .
189 . {* The symbol has local scope; <<static>> in <<C>>. The value
190 . is the offset into the section of the data. *}
191 .#define BSF_LOCAL 0x01
192 .
193 . {* The symbol has global scope; initialized data in <<C>>. The
194 . value is the offset into the section of the data. *}
195 .#define BSF_GLOBAL 0x02
196 .
197 . {* The symbol has global scope and is exported. The value is
198 . the offset into the section of the data. *}
199 .#define BSF_EXPORT BSF_GLOBAL {* no real difference *}
200 .
201 . {* A normal C symbol would be one of:
202 . <<BSF_LOCAL>>, <<BSF_FORT_COMM>>, <<BSF_UNDEFINED>> or
203 . <<BSF_GLOBAL>> *}
204 .
205 . {* The symbol is a debugging record. The value has an arbitary
206 . meaning. *}
207 .#define BSF_DEBUGGING 0x08
208 .
209 . {* The symbol denotes a function entry point. Used in ELF,
210 . perhaps others someday. *}
211 .#define BSF_FUNCTION 0x10
212 .
213 . {* Used by the linker. *}
214 .#define BSF_KEEP 0x20
215 .#define BSF_KEEP_G 0x40
216 .
217 . {* A weak global symbol, overridable without warnings by
218 . a regular global symbol of the same name. *}
219 .#define BSF_WEAK 0x80
220 .
221 . {* This symbol was created to point to a section, e.g. ELF's
222 . STT_SECTION symbols. *}
223 .#define BSF_SECTION_SYM 0x100
224 .
225 . {* The symbol used to be a common symbol, but now it is
226 . allocated. *}
227 .#define BSF_OLD_COMMON 0x200
228 .
229 . {* The default value for common data. *}
230 .#define BFD_FORT_COMM_DEFAULT_VALUE 0
231 .
232 . {* In some files the type of a symbol sometimes alters its
233 . location in an output file - ie in coff a <<ISFCN>> symbol
234 . which is also <<C_EXT>> symbol appears where it was
235 . declared and not at the end of a section. This bit is set
236 . by the target BFD part to convey this information. *}
237 .
238 .#define BSF_NOT_AT_END 0x400
239 .
240 . {* Signal that the symbol is the label of constructor section. *}
241 .#define BSF_CONSTRUCTOR 0x800
242 .
243 . {* Signal that the symbol is a warning symbol. If the symbol
244 . is a warning symbol, then the value field (I know this is
245 . tacky) will point to the asymbol which when referenced will
246 . cause the warning. *}
247 .#define BSF_WARNING 0x1000
248 .
249 . {* Signal that the symbol is indirect. The value of the symbol
250 . is a pointer to an undefined asymbol which contains the
251 . name to use instead. *}
252 .#define BSF_INDIRECT 0x2000
253 .
254 . {* BSF_FILE marks symbols that contain a file name. This is used
255 . for ELF STT_FILE symbols. *}
256 .#define BSF_FILE 0x4000
257 .
258 . {* Symbol is from dynamic linking information. *}
259 .#define BSF_DYNAMIC 0x8000
260 .
261 . flagword flags;
262 .
263 . {* A pointer to the section to which this symbol is
264 . relative. This will always be non NULL, there are special
265 . sections for undefined and absolute symbols *}
266 . struct sec *section;
267 .
268 . {* Back end special data. This is being phased out in favour
269 . of making this a union. *}
270 . PTR udata;
271 .
272 .} asymbol;
273 */
274
275 #include "bfd.h"
276 #include "sysdep.h"
277
278 #include "libbfd.h"
279 #include "aout/stab_gnu.h"
280
281 /*
282 DOCDD
283 INODE
284 symbol handling functions, , typedef asymbol, Symbols
285 SUBSECTION
286 Symbol handling functions
287 */
288
289 /*
290 FUNCTION
291 bfd_get_symtab_upper_bound
292
293 DESCRIPTION
294 Return the number of bytes required to store a vector of pointers
295 to <<asymbols>> for all the symbols in the BFD @var{abfd},
296 including a terminal NULL pointer. If there are no symbols in
297 the BFD, then return 0. If an error occurs, return -1.
298
299 .#define bfd_get_symtab_upper_bound(abfd) \
300 . BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
301
302 */
303
304 /*
305 FUNCTION
306 bfd_is_local_label
307
308 SYNOPSIS
309 boolean bfd_is_local_label(bfd *abfd, asymbol *sym);
310
311 DESCRIPTION
312 Return true if the given symbol @var{sym} in the BFD @var{abfd} is
313 a compiler generated local label, else return false.
314 .#define bfd_is_local_label(abfd, sym) \
315 . BFD_SEND (abfd, _bfd_is_local_label,(abfd, sym))
316 */
317
318 /*
319 FUNCTION
320 bfd_canonicalize_symtab
321
322 DESCRIPTION
323 Read the symbols from the BFD @var{abfd}, and fills in
324 the vector @var{location} with pointers to the symbols and
325 a trailing NULL.
326 Return the actual number of symbol pointers, not
327 including the NULL.
328
329
330 .#define bfd_canonicalize_symtab(abfd, location) \
331 . BFD_SEND (abfd, _bfd_canonicalize_symtab,\
332 . (abfd, location))
333
334 */
335
336
337 /*
338 FUNCTION
339 bfd_set_symtab
340
341 SYNOPSIS
342 boolean bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int count);
343
344 DESCRIPTION
345 Arrange that when the output BFD @var{abfd} is closed,
346 the table @var{location} of @var{count} pointers to symbols
347 will be written.
348 */
349
350 boolean
351 bfd_set_symtab (abfd, location, symcount)
352 bfd *abfd;
353 asymbol **location;
354 unsigned int symcount;
355 {
356 if ((abfd->format != bfd_object) || (bfd_read_p (abfd)))
357 {
358 bfd_set_error (bfd_error_invalid_operation);
359 return false;
360 }
361
362 bfd_get_outsymbols (abfd) = location;
363 bfd_get_symcount (abfd) = symcount;
364 return true;
365 }
366
367 /*
368 FUNCTION
369 bfd_print_symbol_vandf
370
371 SYNOPSIS
372 void bfd_print_symbol_vandf(PTR file, asymbol *symbol);
373
374 DESCRIPTION
375 Print the value and flags of the @var{symbol} supplied to the
376 stream @var{file}.
377 */
378 void
379 bfd_print_symbol_vandf (arg, symbol)
380 PTR arg;
381 asymbol *symbol;
382 {
383 FILE *file = (FILE *) arg;
384 flagword type = symbol->flags;
385 if (symbol->section != (asection *) NULL)
386 {
387 fprintf_vma (file, symbol->value + symbol->section->vma);
388 }
389 else
390 {
391 fprintf_vma (file, symbol->value);
392 }
393
394 /* This presumes that a symbol can not be both BSF_DEBUGGING and
395 BSF_DYNAMIC. */
396 fprintf (file, " %c%c%c%c%c%c%c",
397 (type & BSF_LOCAL) ? 'l' : ' ',
398 (type & BSF_GLOBAL) ? 'g' : ' ',
399 (type & BSF_WEAK) ? 'w' : ' ',
400 (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
401 (type & BSF_WARNING) ? 'W' : ' ',
402 (type & BSF_INDIRECT) ? 'I' : ' ',
403 (type & BSF_DEBUGGING) ? 'd'
404 : (type & BSF_DYNAMIC) ? 'D' : ' ');
405 }
406
407
408 /*
409 FUNCTION
410 bfd_make_empty_symbol
411
412 DESCRIPTION
413 Create a new <<asymbol>> structure for the BFD @var{abfd}
414 and return a pointer to it.
415
416 This routine is necessary because each back end has private
417 information surrounding the <<asymbol>>. Building your own
418 <<asymbol>> and pointing to it will not create the private
419 information, and will cause problems later on.
420
421 .#define bfd_make_empty_symbol(abfd) \
422 . BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
423 */
424
425 /*
426 FUNCTION
427 bfd_make_debug_symbol
428
429 DESCRIPTION
430 Create a new <<asymbol>> structure for the BFD @var{abfd},
431 to be used as a debugging symbol. Further details of its use have
432 yet to be worked out.
433
434 .#define bfd_make_debug_symbol(abfd,ptr,size) \
435 . BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
436 */
437
438 struct section_to_type
439 {
440 CONST char *section;
441 char type;
442 };
443
444 /* Map section names to POSIX/BSD single-character symbol types.
445 This table is probably incomplete. It is sorted for convenience of
446 adding entries. Since it is so short, a linear search is used. */
447 static CONST struct section_to_type stt[] =
448 {
449 {"*DEBUG*", 'N'},
450 {".bss", 'b'},
451 {".data", 'd'},
452 {".rdata", 'r'}, /* Read only data. */
453 {".rodata", 'r'}, /* Read only data. */
454 {".sbss", 's'}, /* Small BSS (uninitialized data). */
455 {".scommon", 'c'}, /* Small common. */
456 {".sdata", 'g'}, /* Small initialized data. */
457 {".text", 't'},
458 {0, 0}
459 };
460
461 /* Return the single-character symbol type corresponding to
462 section S, or '?' for an unknown COFF section. */
463
464 static char
465 coff_section_type (s)
466 char *s;
467 {
468 CONST struct section_to_type *t;
469
470 for (t = &stt[0]; t->section; t++)
471 if (!strcmp (s, t->section))
472 return t->type;
473 return '?';
474 }
475
476 #ifndef islower
477 #define islower(c) ((c) >= 'a' && (c) <= 'z')
478 #endif
479 #ifndef toupper
480 #define toupper(c) (islower(c) ? ((c) & ~0x20) : (c))
481 #endif
482
483 /*
484 FUNCTION
485 bfd_decode_symclass
486
487 DESCRIPTION
488 Return a character corresponding to the symbol
489 class of @var{symbol}, or '?' for an unknown class.
490
491 SYNOPSIS
492 int bfd_decode_symclass(asymbol *symbol);
493 */
494 int
495 bfd_decode_symclass (symbol)
496 asymbol *symbol;
497 {
498 char c;
499
500 if (bfd_is_com_section (symbol->section))
501 return 'C';
502 if (bfd_is_und_section (symbol->section))
503 return 'U';
504 if (bfd_is_ind_section (symbol->section))
505 return 'I';
506 if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL)))
507 return '?';
508
509 if (bfd_is_abs_section (symbol->section))
510 c = 'a';
511 else if (symbol->section)
512 c = coff_section_type (symbol->section->name);
513 else
514 return '?';
515 if (symbol->flags & BSF_GLOBAL)
516 c = toupper (c);
517 return c;
518
519 /* We don't have to handle these cases just yet, but we will soon:
520 N_SETV: 'v';
521 N_SETA: 'l';
522 N_SETT: 'x';
523 N_SETD: 'z';
524 N_SETB: 's';
525 N_INDR: 'i';
526 */
527 }
528
529 /*
530 FUNCTION
531 bfd_symbol_info
532
533 DESCRIPTION
534 Fill in the basic info about symbol that nm needs.
535 Additional info may be added by the back-ends after
536 calling this function.
537
538 SYNOPSIS
539 void bfd_symbol_info(asymbol *symbol, symbol_info *ret);
540 */
541
542 void
543 bfd_symbol_info (symbol, ret)
544 asymbol *symbol;
545 symbol_info *ret;
546 {
547 ret->type = bfd_decode_symclass (symbol);
548 if (ret->type != 'U')
549 ret->value = symbol->value + symbol->section->vma;
550 else
551 ret->value = 0;
552 ret->name = symbol->name;
553 }
554
555 void
556 bfd_symbol_is_absolute ()
557 {
558 abort ();
559 }