Wed Jun 28 18:04:42 1995 Steve Chamberlain <sac@slash.cygnus.com>
[deliverable/binutils-gdb.git] / bfd / syms.c
CommitLineData
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
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
0cda46cf
SC
21/*
22SECTION
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::
51@* typedef asymbol::
52@* symbol handling functions::
6724ff46
RP
53@end menu
54
c188b0be
DM
55INODE
56Reading Symbols, Writing Symbols, Symbols, Symbols
0cda46cf 57SUBSECTION
c91884b3 58 Reading symbols
0cda46cf 59
c188b0be 60 There are two stages to reading a symbol table from a BFD:
0cda46cf 61 allocating storage, and the actual reading process. This is an
c188b0be 62 excerpt from an application which reads the symbol table:
0cda46cf 63
ec591fcf 64| long storage_needed;
e98e6ec1 65| asymbol **symbol_table;
ec591fcf
ILT
66| long number_of_symbols;
67| long i;
57a1867e 68|
ec591fcf
ILT
69| storage_needed = bfd_get_symtab_upper_bound (abfd);
70|
71| if (storage_needed < 0)
72| FAIL
57a1867e 73|
e98e6ec1
SC
74| if (storage_needed == 0) {
75| return ;
76| }
57a1867e 77| symbol_table = (asymbol **) xmalloc (storage_needed);
e98e6ec1 78| ...
57a1867e
DM
79| number_of_symbols =
80| bfd_canonicalize_symtab (abfd, symbol_table);
81|
ec591fcf
ILT
82| if (number_of_symbols < 0)
83| FAIL
84|
e98e6ec1
SC
85| for (i = 0; i < number_of_symbols; i++) {
86| process_symbol (symbol_table[i]);
87| }
0cda46cf
SC
88
89 All storage for the symbols themselves is in an obstack
c188b0be 90 connected to the BFD; it is freed when the BFD is closed.
0cda46cf 91
6724ff46 92
c188b0be
DM
93INODE
94Writing Symbols, typedef asymbol, Reading Symbols, Symbols
0cda46cf 95SUBSECTION
c91884b3 96 Writing symbols
0cda46cf 97
0cda46cf
SC
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
c188b0be
DM
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
0cda46cf
SC
106 example showing the creation of a symbol table with only one element:
107
e98e6ec1 108| #include "bfd.h"
57a1867e 109| main()
e98e6ec1
SC
110| {
111| bfd *abfd;
112| asymbol *ptrs[2];
113| asymbol *new;
57a1867e 114|
e98e6ec1
SC
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;
57a1867e 122|
e98e6ec1
SC
123| ptrs[0] = new;
124| ptrs[1] = (asymbol *)0;
57a1867e 125|
e98e6ec1
SC
126| bfd_set_symtab(abfd, ptrs, 1);
127| bfd_close(abfd);
128| }
57a1867e
DM
129|
130| ./makesym
e98e6ec1
SC
131| nm foo
132| 00012345 A dummy_symbol
6724ff46 133
0cda46cf 134 Many formats cannot represent arbitary symbol information; for
c188b0be 135 instance, the <<a.out>> object format does not allow an
0cda46cf
SC
136 arbitary number of sections. A symbol pointing to a section
137 which is not one of <<.text>>, <<.data>> or <<.bss>> cannot
57a1867e 138 be described.
6724ff46 139
6724ff46
RP
140*/
141
142
c188b0be 143
e98e6ec1 144/*
c188b0be
DM
145DOCDD
146INODE
147typedef asymbol, symbol handling functions, Writing Symbols, Symbols
6724ff46
RP
148
149*/
0cda46cf 150/*
e98e6ec1 151SUBSECTION
0cda46cf 152 typedef asymbol
6724ff46 153
0cda46cf 154 An <<asymbol>> has the form:
6724ff46 155
e98e6ec1
SC
156*/
157
158/*
159CODE_FRAGMENT
160
c188b0be 161.
57a1867e 162.typedef struct symbol_cache_entry
0cda46cf 163.{
e98e6ec1
SC
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
c188b0be
DM
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. *}
e98e6ec1 173.
c188b0be 174. struct _bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *}
e98e6ec1 175.
c188b0be 176. {* The text of the symbol. The name is left alone, and not copied; the
e98e6ec1
SC
177. application may not alter it. *}
178. CONST char *name;
179.
c188b0be
DM
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. *}
e98e6ec1
SC
183. symvalue value;
184.
185. {* Attributes of a symbol: *}
186.
0cda46cf 187.#define BSF_NO_FLAGS 0x00
e98e6ec1
SC
188.
189. {* The symbol has local scope; <<static>> in <<C>>. The value
190. is the offset into the section of the data. *}
0cda46cf 191.#define BSF_LOCAL 0x01
e98e6ec1
SC
192.
193. {* The symbol has global scope; initialized data in <<C>>. The
194. value is the offset into the section of the data. *}
0cda46cf 195.#define BSF_GLOBAL 0x02
e98e6ec1 196.
c188b0be 197. {* The symbol has global scope and is exported. The value is
e98e6ec1 198. the offset into the section of the data. *}
c188b0be 199.#define BSF_EXPORT BSF_GLOBAL {* no real difference *}
e98e6ec1
SC
200.
201. {* A normal C symbol would be one of:
202. <<BSF_LOCAL>>, <<BSF_FORT_COMM>>, <<BSF_UNDEFINED>> or
c188b0be 203. <<BSF_GLOBAL>> *}
e98e6ec1
SC
204.
205. {* The symbol is a debugging record. The value has an arbitary
206. meaning. *}
c188b0be 207.#define BSF_DEBUGGING 0x08
e98e6ec1 208.
c188b0be
DM
209. {* The symbol denotes a function entry point. Used in ELF,
210. perhaps others someday. *}
211.#define BSF_FUNCTION 0x10
e98e6ec1 212.
c188b0be
DM
213. {* Used by the linker. *}
214.#define BSF_KEEP 0x20
215.#define BSF_KEEP_G 0x40
e98e6ec1 216.
c188b0be
DM
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
e98e6ec1
SC
224.
225. {* The symbol used to be a common symbol, but now it is
226. allocated. *}
c188b0be 227.#define BSF_OLD_COMMON 0x200
e98e6ec1
SC
228.
229. {* The default value for common data. *}
0cda46cf 230.#define BFD_FORT_COMM_DEFAULT_VALUE 0
e98e6ec1
SC
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.
c188b0be 238.#define BSF_NOT_AT_END 0x400
e98e6ec1
SC
239.
240. {* Signal that the symbol is the label of constructor section. *}
c188b0be 241.#define BSF_CONSTRUCTOR 0x800
e98e6ec1
SC
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. *}
c188b0be 247.#define BSF_WARNING 0x1000
e98e6ec1
SC
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. *}
c188b0be
DM
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
e98e6ec1 257.
0ee75d02
ILT
258. {* Symbol is from dynamic linking information. *}
259.#define BSF_DYNAMIC 0x8000
260.
0cda46cf 261. flagword flags;
e98e6ec1 262.
57a1867e 263. {* A pointer to the section to which this symbol is
e98e6ec1 264. relative. This will always be non NULL, there are special
89665c85 265. sections for undefined and absolute symbols. *}
0cda46cf 266. struct sec *section;
e98e6ec1 267.
89665c85
SC
268. {* Back end special data. *}
269. union
270. {
271. PTR p;
272. bfd_vma i;
273. } udata;
e98e6ec1 274.
0cda46cf 275.} asymbol;
6724ff46
RP
276*/
277
6724ff46 278#include "bfd.h"
7d68537f 279#include "sysdep.h"
c188b0be 280
6724ff46 281#include "libbfd.h"
e98e6ec1 282#include "aout/stab_gnu.h"
57a1867e 283
0cda46cf 284/*
c188b0be
DM
285DOCDD
286INODE
287symbol handling functions, , typedef asymbol, Symbols
0cda46cf 288SUBSECTION
c91884b3 289 Symbol handling functions
6724ff46
RP
290*/
291
0cda46cf
SC
292/*
293FUNCTION
ec591fcf 294 bfd_get_symtab_upper_bound
0cda46cf
SC
295
296DESCRIPTION
c188b0be
DM
297 Return the number of bytes required to store a vector of pointers
298 to <<asymbols>> for all the symbols in the BFD @var{abfd},
0cda46cf 299 including a terminal NULL pointer. If there are no symbols in
ec591fcf
ILT
300 the BFD, then return 0. If an error occurs, return -1.
301
302.#define bfd_get_symtab_upper_bound(abfd) \
303. BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
304
305*/
0cda46cf 306
ec591fcf
ILT
307/*
308FUNCTION
309 bfd_is_local_label
6724ff46 310
ec591fcf
ILT
311SYNOPSIS
312 boolean bfd_is_local_label(bfd *abfd, asymbol *sym);
313
314DESCRIPTION
315 Return true if the given symbol @var{sym} in the BFD @var{abfd} is
316 a compiler generated local label, else return false.
317.#define bfd_is_local_label(abfd, sym) \
318. BFD_SEND (abfd, _bfd_is_local_label,(abfd, sym))
6724ff46
RP
319*/
320
0cda46cf
SC
321/*
322FUNCTION
323 bfd_canonicalize_symtab
324
325DESCRIPTION
c188b0be
DM
326 Read the symbols from the BFD @var{abfd}, and fills in
327 the vector @var{location} with pointers to the symbols and
57a1867e 328 a trailing NULL.
c188b0be 329 Return the actual number of symbol pointers, not
0cda46cf 330 including the NULL.
6724ff46 331
6724ff46 332
0cda46cf
SC
333.#define bfd_canonicalize_symtab(abfd, location) \
334. BFD_SEND (abfd, _bfd_canonicalize_symtab,\
335. (abfd, location))
336
6724ff46
RP
337*/
338
339
0cda46cf
SC
340/*
341FUNCTION
342 bfd_set_symtab
343
0cda46cf 344SYNOPSIS
c188b0be
DM
345 boolean bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int count);
346
347DESCRIPTION
348 Arrange that when the output BFD @var{abfd} is closed,
349 the table @var{location} of @var{count} pointers to symbols
350 will be written.
6724ff46
RP
351*/
352
353boolean
354bfd_set_symtab (abfd, location, symcount)
355 bfd *abfd;
356 asymbol **location;
357 unsigned int symcount;
358{
57a1867e
DM
359 if ((abfd->format != bfd_object) || (bfd_read_p (abfd)))
360 {
361 bfd_set_error (bfd_error_invalid_operation);
362 return false;
363 }
6724ff46
RP
364
365 bfd_get_outsymbols (abfd) = location;
366 bfd_get_symcount (abfd) = symcount;
367 return true;
368}
369
0cda46cf
SC
370/*
371FUNCTION
372 bfd_print_symbol_vandf
6724ff46 373
0cda46cf
SC
374SYNOPSIS
375 void bfd_print_symbol_vandf(PTR file, asymbol *symbol);
c188b0be
DM
376
377DESCRIPTION
378 Print the value and flags of the @var{symbol} supplied to the
379 stream @var{file}.
6724ff46
RP
380*/
381void
57a1867e
DM
382bfd_print_symbol_vandf (arg, symbol)
383 PTR arg;
384 asymbol *symbol;
6724ff46 385{
0ee75d02 386 FILE *file = (FILE *) arg;
6724ff46 387 flagword type = symbol->flags;
57a1867e
DM
388 if (symbol->section != (asection *) NULL)
389 {
390 fprintf_vma (file, symbol->value + symbol->section->vma);
391 }
392 else
393 {
394 fprintf_vma (file, symbol->value);
395 }
0ee75d02
ILT
396
397 /* This presumes that a symbol can not be both BSF_DEBUGGING and
89665c85 398 BSF_DYNAMIC, nor both BSF_FUNCTION and BSF_FILE. */
57a1867e 399 fprintf (file, " %c%c%c%c%c%c%c",
89665c85
SC
400 ((type & BSF_LOCAL)
401 ? (type & BSF_GLOBAL) ? '!' : 'l'
402 : (type & BSF_GLOBAL) ? 'g' : ' '),
57a1867e
DM
403 (type & BSF_WEAK) ? 'w' : ' ',
404 (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
405 (type & BSF_WARNING) ? 'W' : ' ',
406 (type & BSF_INDIRECT) ? 'I' : ' ',
89665c85
SC
407 (type & BSF_DEBUGGING) ? 'd' : (type & BSF_DYNAMIC) ? 'D' : ' ',
408 (type & BSF_FUNCTION) ? 'F' : (type & BSF_FILE) ? 'f' : ' ');
6724ff46
RP
409}
410
411
0cda46cf
SC
412/*
413FUNCTION
414 bfd_make_empty_symbol
415
416DESCRIPTION
c188b0be
DM
417 Create a new <<asymbol>> structure for the BFD @var{abfd}
418 and return a pointer to it.
6724ff46 419
c188b0be 420 This routine is necessary because each back end has private
0cda46cf
SC
421 information surrounding the <<asymbol>>. Building your own
422 <<asymbol>> and pointing to it will not create the private
423 information, and will cause problems later on.
424
425.#define bfd_make_empty_symbol(abfd) \
426. BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
6724ff46 427*/
7d68537f 428
c188b0be
DM
429/*
430FUNCTION
431 bfd_make_debug_symbol
432
433DESCRIPTION
434 Create a new <<asymbol>> structure for the BFD @var{abfd},
435 to be used as a debugging symbol. Further details of its use have
436 yet to be worked out.
437
438.#define bfd_make_debug_symbol(abfd,ptr,size) \
439. BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
440*/
441
442struct section_to_type
443{
444 CONST char *section;
445 char type;
446};
447
448/* Map section names to POSIX/BSD single-character symbol types.
449 This table is probably incomplete. It is sorted for convenience of
450 adding entries. Since it is so short, a linear search is used. */
57a1867e
DM
451static CONST struct section_to_type stt[] =
452{
c188b0be
DM
453 {"*DEBUG*", 'N'},
454 {".bss", 'b'},
455 {".data", 'd'},
ec591fcf
ILT
456 {".rdata", 'r'}, /* Read only data. */
457 {".rodata", 'r'}, /* Read only data. */
458 {".sbss", 's'}, /* Small BSS (uninitialized data). */
459 {".scommon", 'c'}, /* Small common. */
460 {".sdata", 'g'}, /* Small initialized data. */
c188b0be
DM
461 {".text", 't'},
462 {0, 0}
463};
464
465/* Return the single-character symbol type corresponding to
89665c85
SC
466 section S, or '?' for an unknown COFF section.
467
468 Check for any leading string which matches, so .text5 returns
469 't' as well as .text */
c188b0be
DM
470
471static char
472coff_section_type (s)
473 char *s;
474{
475 CONST struct section_to_type *t;
476
89665c85
SC
477 for (t = &stt[0]; t->section; t++)
478 if (!strncmp (s, t->section, strlen (t->section)))
c188b0be 479 return t->type;
89665c85 480
c188b0be
DM
481 return '?';
482}
483
484#ifndef islower
485#define islower(c) ((c) >= 'a' && (c) <= 'z')
486#endif
487#ifndef toupper
488#define toupper(c) (islower(c) ? ((c) & ~0x20) : (c))
489#endif
490
0cda46cf
SC
491/*
492FUNCTION
493 bfd_decode_symclass
494
495DESCRIPTION
c188b0be
DM
496 Return a character corresponding to the symbol
497 class of @var{symbol}, or '?' for an unknown class.
7d68537f 498
0cda46cf
SC
499SYNOPSIS
500 int bfd_decode_symclass(asymbol *symbol);
7d68537f
FF
501*/
502int
57a1867e
DM
503bfd_decode_symclass (symbol)
504 asymbol *symbol;
7d68537f 505{
c188b0be
DM
506 char c;
507
508 if (bfd_is_com_section (symbol->section))
509 return 'C';
ec591fcf 510 if (bfd_is_und_section (symbol->section))
c188b0be 511 return 'U';
ec591fcf 512 if (bfd_is_ind_section (symbol->section))
c188b0be 513 return 'I';
89665c85
SC
514 if (symbol->flags & BSF_WEAK)
515 return 'W';
57a1867e 516 if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL)))
c188b0be
DM
517 return '?';
518
ec591fcf 519 if (bfd_is_abs_section (symbol->section))
c188b0be
DM
520 c = 'a';
521 else if (symbol->section)
522 c = coff_section_type (symbol->section->name);
523 else
524 return '?';
525 if (symbol->flags & BSF_GLOBAL)
526 c = toupper (c);
527 return c;
7d68537f
FF
528
529 /* We don't have to handle these cases just yet, but we will soon:
57a1867e
DM
530 N_SETV: 'v';
531 N_SETA: 'l';
7d68537f
FF
532 N_SETT: 'x';
533 N_SETD: 'z';
534 N_SETB: 's';
535 N_INDR: 'i';
536 */
7d68537f 537}
e98e6ec1 538
c188b0be
DM
539/*
540FUNCTION
541 bfd_symbol_info
542
543DESCRIPTION
544 Fill in the basic info about symbol that nm needs.
545 Additional info may be added by the back-ends after
546 calling this function.
547
548SYNOPSIS
549 void bfd_symbol_info(asymbol *symbol, symbol_info *ret);
550*/
e98e6ec1 551
c188b0be 552void
57a1867e
DM
553bfd_symbol_info (symbol, ret)
554 asymbol *symbol;
555 symbol_info *ret;
c188b0be
DM
556{
557 ret->type = bfd_decode_symclass (symbol);
558 if (ret->type != 'U')
57a1867e 559 ret->value = symbol->value + symbol->section->vma;
c188b0be
DM
560 else
561 ret->value = 0;
562 ret->name = symbol->name;
563}
564
565void
57a1867e 566bfd_symbol_is_absolute ()
e98e6ec1 567{
57a1867e 568 abort ();
e98e6ec1 569}
89665c85
SC
570
571/*
572FUNCTION
573 bfd_copy_private_symbol_data
574
575SYNOPSIS
576 boolean bfd_copy_private_symbol_data(bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
577
578DESCRIPTION
579 Copy private symbol information from @var{isym} in the BFD
580 @var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
581 Return <<true>> on success, <<false>> on error. Possible error
582 returns are:
583
584 o <<bfd_error_no_memory>> -
585 Not enough memory exists to create private data for @var{osec}.
586
587.#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
588. BFD_SEND (ibfd, _bfd_copy_private_symbol_data, \
589. (ibfd, isymbol, obfd, osymbol))
590
591*/
This page took 0.176188 seconds and 4 git commands to generate.