1 /* Generic symbol-table support for the BFD library.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
23 BFD trys to maintain as much symbol information as it can when it
24 moves information from file to file. BFD passes information to
25 applications though the @code{asymbol} structure. When the application
26 requests the symbol table, BFD reads the table in the native form and
27 translates parts of it into the internal format. To maintain more than
28 the infomation passed to applications some targets keep
29 some information 'behind the sceans', in a structure only the
30 particular back end knows about. For example, the coff back end keeps
31 the original symbol table structure as well as the canonical structure
32 when a BFD is read in. On output, the coff back end can reconstruct
33 the output symbol table so that no information is lost, even
34 information unique to coff which BFD doesn't know or understand. If a
35 coff symbol table was read, but was written through an a.out back end,
36 all the coff specific information would be lost. (.. until BFD 2 :).
38 The symbol table of a BFD is not necessarily read in until a
39 canonicalize request is made. Then the BFD back end fills in a table
40 provided by the application with pointers to the canonical
43 To output symbols, the application provides BFD with a table of
44 pointers to pointers to @code{asymbol}s. This allows applications like
45 the linker to output a symbol as read, since the 'behind the sceens'
46 information will be still available.
52 * symbol handling functions::
55 @node Reading Symbols, Writing Symbols, Symbols, Symbols
56 @subsection Reading Symbols
57 There are two stages to reading a symbol table from a BFD; allocating
58 storage, and the actual reading process. This is an excerpt from an
59 appliction which reads the symbol table:
62 unsigned int storage_needed;
63 asymbol **symbol_table;
64 unsigned int number_of_symbols;
67 storage_needed = get_symtab_upper_bound (abfd);
69 if (storage_needed == 0) {
72 symbol_table = (asymbol **) malloc (storage_needed);
75 bfd_canonicalize_symtab (abfd, symbol_table);
77 for (i = 0; i < number_of_symbols; i++) {
78 process_symbol (symbol_table[i]);
82 All storage for the symbols themselves is in an obstack connected to
83 the BFD, and is freed when the BFD is closed.
85 @node Writing Symbols, typedef asymbol, Reading Symbols, Symbols
86 @subsection Writing Symbols
87 Writing of a symbol table is automatic when a BFD open for writing
88 is closed. The application attaches a vector of pointers to pointers to symbols
89 to the BFD being written, and fills in the symbol count. The close and
90 cleanup code reads through the table provided and performs all the
91 necessary operations. The outputing code must always be provided with
92 an 'owned' symbol; one which has come from another BFD, or one which
93 has been created using @code{bfd_make_empty_symbol}.
95 An example showing the creation of a symbol table with only one
106 abfd = bfd_openw("foo","a.out-sunos-big");
107 bfd_set_format(abfd, bfd_object);
108 new = bfd_make_empty_symbol(abfd);
109 new->name = "dummy_symbol";
110 new->section = (asection *)0;
111 new->flags = BSF_ABSOLUTE | BSF_GLOBAL;
112 new->value = 0x12345;
115 ptrs[1] = (asymbol *)0;
117 bfd_set_symtab(abfd, ptrs, 1);
123 00012345 A dummy_symbol
128 Many formats cannot represent arbitary symbol information; for
129 instance the @code{a.out} object format does not allow an arbitary
130 number of sections. A symbol pointing to a section which is not one of
131 @code{.text}, @code{.data} or @code{.bss} cannot be described.
136 @node typedef asymbol, symbol handling functions, Writing Symbols, Symbols
140 @subsection typedef asymbol
141 An @code{asymbol} has the form:
145 $typedef struct symbol_cache_entry
147 A pointer to the BFD which owns the symbol. This information is
148 necessary so that a back end can work out what additional (invisible to
149 the application writer) information is carried with the symbol.
151 $ struct _bfd *the_bfd;
153 The text of the symbol. The name is left alone, and not copied - the
154 application may not alter it.
158 The value of the symbol.
162 Attributes of a symbol:
164 $#define BSF_NO_FLAGS 0x00
166 The symbol has local scope; @code{static} in @code{C}. The value is
167 the offset into the section of the data.
169 $#define BSF_LOCAL 0x01
171 The symbol has global scope; initialized data in @code{C}. The value
172 is the offset into the section of the data.
174 $#define BSF_GLOBAL 0x02
178 $#define BSF_IMPORT 0x04
180 The symbol has global scope, and is exported. The value is the offset
181 into the section of the data.
183 $#define BSF_EXPORT 0x08
185 The symbol is undefined. @code{extern} in @code{C}. The value has no meaning.
187 $#define BSF_UNDEFINED 0x10
189 The symbol is common, initialized to zero; default in @code{C}. The
190 value is the size of the object in bytes.
192 $#define BSF_FORT_COMM 0x20
194 A normal @code{C} symbol would be one of:
195 @code{BSF_LOCAL}, @code{BSF_FORT_COMM}, @code{BSF_UNDEFINED} or @code{BSF_EXPORT|BSD_GLOBAL}
197 The symbol is a debugging record. The value has an arbitary meaning.
199 $#define BSF_DEBUGGING 0x40
201 The symbol has no section attached, any value is the actual value and
202 is not a relative offset to a section.
204 $#define BSF_ABSOLUTE 0x80
208 $#define BSF_KEEP 0x10000
209 $#define BSF_KEEP_G 0x80000
213 $#define BSF_WEAK 0x100000
214 $#define BSF_CTOR 0x200000
215 $#define BSF_FAKE 0x400000
217 The symbol used to be a common symbol, but now it is allocated.
219 $#define BSF_OLD_COMMON 0x800000
221 The default value for common data.
223 $#define BFD_FORT_COMM_DEFAULT_VALUE 0
225 In some files the type of a symbol sometimes alters its location
226 in an output file - ie in coff a @code{ISFCN} symbol which is also @code{C_EXT}
227 symbol appears where it was declared and not at the end of a section.
228 This bit is set by the target BFD part to convey this information.
230 $#define BSF_NOT_AT_END 0x40000
232 Signal that the symbol is the label of constructor section.
234 $#define BSF_CONSTRUCTOR 0x1000000
236 Signal that the symbol is a warning symbol. If the symbol is a warning
237 symbol, then the value field (I know this is tacky) will point to the
238 asymbol which when referenced will cause the warning.
240 $#define BSF_WARNING 0x2000000
242 Signal that the symbol is indirect. The value of the symbol is a
243 pointer to an undefined asymbol which contains the name to use
246 $#define BSF_INDIRECT 0x4000000
250 Aointer to the section to which this symbol is relative, or 0 if the
251 symbol is absolute or undefined. Note that it is not sufficient to set
252 this location to 0 to mark a symbol as absolute - the flag
253 @code{BSF_ABSOLUTE} must be set also.
255 $ struct sec *section;
257 Back end special data. This is being phased out in favour of making
271 @node symbol handling functions, Symbols, typedef asymbol, Symbols
272 @subsection Symbol Handling Functions
276 /*proto* get_symtab_upper_bound
277 Returns the number of bytes required in a vector of pointers to
278 @code{asymbols} for all the symbols in the supplied BFD, including a
279 terminal NULL pointer. If there are no symbols in the BFD, then 0 is
282 #define get_symtab_upper_bound(abfd) \
283 BFD_SEND (abfd, _get_symtab_upper_bound, (abfd))
288 /*proto* bfd_canonicalize_symtab
289 Supplied a BFD and a pointer to an uninitialized vector of pointers.
290 This reads in the symbols from the BFD, and fills in the table with
291 pointers to the symbols, and a trailing NULL. The routine returns the
292 actual number of symbol pointers not including the NULL.
295 #define bfd_canonicalize_symtab(abfd, location) \
296 BFD_SEND (abfd, _bfd_canonicalize_symtab,\
303 /*proto* bfd_set_symtab
304 Provided a table of pointers to to symbols and a count, writes to the
305 output BFD the symbols when closed.
307 *; PROTO(boolean, bfd_set_symtab, (bfd *, asymbol **, unsigned int ));
311 bfd_set_symtab (abfd, location, symcount)
314 unsigned int symcount;
316 if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) {
317 bfd_error = invalid_operation;
321 bfd_get_outsymbols (abfd) = location;
322 bfd_get_symcount (abfd) = symcount;
326 /*proto* bfd_print_symbol_vandf
327 Prints the value and flags of the symbol supplied to the stream file.
329 *; PROTO(void, bfd_print_symbol_vandf, (PTR file, asymbol *symbol));
332 DEFUN(bfd_print_symbol_vandf,(file, symbol),
336 flagword type = symbol->flags;
337 if (symbol->section != (asection *)NULL)
339 fprintf_vma(file, symbol->value+symbol->section->vma);
343 fprintf_vma(file, symbol->value);
345 fprintf(file," %c%c%c%c%c%c%c%c%c%c",
346 (type & BSF_LOCAL) ? 'l':' ',
347 (type & BSF_GLOBAL) ? 'g' : ' ',
348 (type & BSF_IMPORT) ? 'i' : ' ',
349 (type & BSF_EXPORT) ? 'e' : ' ',
350 (type & BSF_UNDEFINED) ? 'u' : ' ',
351 (type & BSF_FORT_COMM) ? 'c' : ' ',
352 (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
353 (type & BSF_WARNING) ? 'W' : ' ',
354 (type & BSF_INDIRECT) ? 'I' : ' ',
355 (type & BSF_DEBUGGING) ? 'd' :' ');
360 /*proto* bfd_make_empty_symbol
361 This function creates a new @code{asymbol} structure for the BFD, and
362 returns a pointer to it.
364 This routine is necessary, since each back end has private information
365 surrounding the @code{asymbol}. Building your own @code{asymbol} and
366 pointing to it will not create the private information, and will cause
369 #define bfd_make_empty_symbol(abfd) \
370 BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))