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