1@section Symbols
2BFD tries to maintain as much symbol information as it can when
3it moves information from file to file. BFD passes information
4to applications though the @code{asymbol} structure. When the
5application requests the symbol table, BFD reads the table in
6the native form and translates parts of it into the internal
7format. To maintain more than the information passed to
8applications, some targets keep some information ``behind the
9scenes'' in a structure only the particular back end knows
10about. For example, the coff back end keeps the original
11symbol table structure as well as the canonical structure when
12a BFD is read in. On output, the coff back end can reconstruct
13the output symbol table so that no information is lost, even
14information unique to coff which BFD doesn't know or
15understand. If a coff symbol table were read, but were written
16through an a.out back end, all the coff specific information
17would be lost. The symbol table of a BFD
18is not necessarily read in until a canonicalize request is
19made. Then the BFD back end fills in a table provided by the
20application with pointers to the canonical information.  To
21output symbols, the application provides BFD with a table of
22pointers to pointers to @code{asymbol}s. This allows applications
23like the linker to output a symbol as it was read, since the ``behind
24the scenes'' information will be still available.
25@menu
26* Reading Symbols::
27* Writing Symbols::
28* Mini Symbols::
29* typedef asymbol::
30* symbol handling functions::
31@end menu
32
33@node Reading Symbols, Writing Symbols, Symbols, Symbols
34@subsection Reading symbols
35There are two stages to reading a symbol table from a BFD:
36allocating storage, and the actual reading process. This is an
37excerpt from an application which reads the symbol table:
38
39@example
40         long storage_needed;
41         asymbol **symbol_table;
42         long number_of_symbols;
43         long i;
44
45         storage_needed = bfd_get_symtab_upper_bound (abfd);
46
47         if (storage_needed < 0)
48           FAIL
49
50         if (storage_needed == 0)
51           return;
52         
53         symbol_table = xmalloc (storage_needed);
54           ...
55         number_of_symbols =
56            bfd_canonicalize_symtab (abfd, symbol_table);
57
58         if (number_of_symbols < 0)
59           FAIL
60
61         for (i = 0; i < number_of_symbols; i++)
62           process_symbol (symbol_table[i]);
63@end example
64
65All storage for the symbols themselves is in an objalloc
66connected to the BFD; it is freed when the BFD is closed.
67
68@node Writing Symbols, Mini Symbols, Reading Symbols, Symbols
69@subsection Writing symbols
70Writing of a symbol table is automatic when a BFD open for
71writing is closed. The application attaches a vector of
72pointers to pointers to symbols to the BFD being written, and
73fills in the symbol count. The close and cleanup code reads
74through the table provided and performs all the necessary
75operations. The BFD output code must always be provided with an
76``owned'' symbol: one which has come from another BFD, or one
77which has been created using @code{bfd_make_empty_symbol}.  Here is an
78example showing the creation of a symbol table with only one element:
79
80@example
81       #include "bfd.h"
82       int main (void)
83       @{
84         bfd *abfd;
85         asymbol *ptrs[2];
86         asymbol *new;
87
88         abfd = bfd_openw ("foo","a.out-sunos-big");
89         bfd_set_format (abfd, bfd_object);
90         new = bfd_make_empty_symbol (abfd);
91         new->name = "dummy_symbol";
92         new->section = bfd_make_section_old_way (abfd, ".text");
93         new->flags = BSF_GLOBAL;
94         new->value = 0x12345;
95
96         ptrs[0] = new;
97         ptrs[1] = 0;
98
99         bfd_set_symtab (abfd, ptrs, 1);
100         bfd_close (abfd);
101         return 0;
102       @}
103
104       ./makesym
105       nm foo
106       00012345 A dummy_symbol
107@end example
108
109Many formats cannot represent arbitrary symbol information; for
110instance, the @code{a.out} object format does not allow an
111arbitrary number of sections. A symbol pointing to a section
112which is not one  of @code{.text}, @code{.data} or @code{.bss} cannot
113be described.
114
115@node Mini Symbols, typedef asymbol, Writing Symbols, Symbols
116@subsection Mini Symbols
117Mini symbols provide read-only access to the symbol table.
118They use less memory space, but require more time to access.
119They can be useful for tools like nm or objdump, which may
120have to handle symbol tables of extremely large executables.
121
122The @code{bfd_read_minisymbols} function will read the symbols
123into memory in an internal form.  It will return a @code{void *}
124pointer to a block of memory, a symbol count, and the size of
125each symbol.  The pointer is allocated using @code{malloc}, and
126should be freed by the caller when it is no longer needed.
127
128The function @code{bfd_minisymbol_to_symbol} will take a pointer
129to a minisymbol, and a pointer to a structure returned by
130@code{bfd_make_empty_symbol}, and return a @code{asymbol} structure.
131The return value may or may not be the same as the value from
132@code{bfd_make_empty_symbol} which was passed in.
133
134
135@node typedef asymbol, symbol handling functions, Mini Symbols, Symbols
136@subsection typedef asymbol
137An @code{asymbol} has the form:
138
139
140@example
141
142typedef struct bfd_symbol
143@{
144  /* A pointer to the BFD which owns the symbol. This information
145     is necessary so that a back end can work out what additional
146     information (invisible to the application writer) is carried
147     with the symbol.
148
149     This field is *almost* redundant, since you can use section->owner
150     instead, except that some symbols point to the global sections
151     bfd_@{abs,com,und@}_section.  This could be fixed by making
152     these globals be per-bfd (or per-target-flavor).  FIXME.  */
153  struct bfd *the_bfd; /* Use bfd_asymbol_bfd(sym) to access this field.  */
154
155  /* The text of the symbol. The name is left alone, and not copied; the
156     application may not alter it.  */
157  const char *name;
158
159  /* The value of the symbol.  This really should be a union of a
160     numeric value with a pointer, since some flags indicate that
161     a pointer to another symbol is stored here.  */
162  symvalue value;
163
164  /* Attributes of a symbol.  */
165#define BSF_NO_FLAGS    0x00
166
167  /* The symbol has local scope; @code{static} in @code{C}. The value
168     is the offset into the section of the data.  */
169#define BSF_LOCAL      0x01
170
171  /* The symbol has global scope; initialized data in @code{C}. The
172     value is the offset into the section of the data.  */
173#define BSF_GLOBAL     0x02
174
175  /* The symbol has global scope and is exported. The value is
176     the offset into the section of the data.  */
177#define BSF_EXPORT     BSF_GLOBAL /* No real difference.  */
178
179  /* A normal C symbol would be one of:
180     @code{BSF_LOCAL}, @code{BSF_FORT_COMM},  @code{BSF_UNDEFINED} or
181     @code{BSF_GLOBAL}.  */
182
183  /* The symbol is a debugging record. The value has an arbitrary
184     meaning, unless BSF_DEBUGGING_RELOC is also set.  */
185#define BSF_DEBUGGING  0x08
186
187  /* The symbol denotes a function entry point.  Used in ELF,
188     perhaps others someday.  */
189#define BSF_FUNCTION    0x10
190
191  /* Used by the linker.  */
192#define BSF_KEEP        0x20
193#define BSF_KEEP_G      0x40
194
195  /* A weak global symbol, overridable without warnings by
196     a regular global symbol of the same name.  */
197#define BSF_WEAK        0x80
198
199  /* This symbol was created to point to a section, e.g. ELF's
200     STT_SECTION symbols.  */
201#define BSF_SECTION_SYM 0x100
202
203  /* The symbol used to be a common symbol, but now it is
204     allocated.  */
205#define BSF_OLD_COMMON  0x200
206
207  /* The default value for common data.  */
208#define BFD_FORT_COMM_DEFAULT_VALUE 0
209
210  /* In some files the type of a symbol sometimes alters its
211     location in an output file - ie in coff a @code{ISFCN} symbol
212     which is also @code{C_EXT} symbol appears where it was
213     declared and not at the end of a section.  This bit is set
214     by the target BFD part to convey this information.  */
215#define BSF_NOT_AT_END    0x400
216
217  /* Signal that the symbol is the label of constructor section.  */
218#define BSF_CONSTRUCTOR   0x800
219
220  /* Signal that the symbol is a warning symbol.  The name is a
221     warning.  The name of the next symbol is the one to warn about;
222     if a reference is made to a symbol with the same name as the next
223     symbol, a warning is issued by the linker.  */
224#define BSF_WARNING       0x1000
225
226  /* Signal that the symbol is indirect.  This symbol is an indirect
227     pointer to the symbol with the same name as the next symbol.  */
228#define BSF_INDIRECT      0x2000
229
230  /* BSF_FILE marks symbols that contain a file name.  This is used
231     for ELF STT_FILE symbols.  */
232#define BSF_FILE          0x4000
233
234  /* Symbol is from dynamic linking information.  */
235#define BSF_DYNAMIC       0x8000
236
237  /* The symbol denotes a data object.  Used in ELF, and perhaps
238     others someday.  */
239#define BSF_OBJECT        0x10000
240
241  /* This symbol is a debugging symbol.  The value is the offset
242     into the section of the data.  BSF_DEBUGGING should be set
243     as well.  */
244#define BSF_DEBUGGING_RELOC 0x20000
245
246  /* This symbol is thread local.  Used in ELF.  */
247#define BSF_THREAD_LOCAL  0x40000
248
249  flagword flags;
250
251  /* A pointer to the section to which this symbol is
252     relative.  This will always be non NULL, there are special
253     sections for undefined and absolute symbols.  */
254  struct bfd_section *section;
255
256  /* Back end special data.  */
257  union
258    @{
259      void *p;
260      bfd_vma i;
261    @}
262  udata;
263@}
264asymbol;
265
266@end example
267
268@node symbol handling functions,  , typedef asymbol, Symbols
269@subsection Symbol handling functions
270
271
272@findex bfd_get_symtab_upper_bound
273@subsubsection @code{bfd_get_symtab_upper_bound}
274@strong{Description}@*
275Return the number of bytes required to store a vector of pointers
276to @code{asymbols} for all the symbols in the BFD @var{abfd},
277including a terminal NULL pointer. If there are no symbols in
278the BFD, then return 0.  If an error occurs, return -1.
279@example
280#define bfd_get_symtab_upper_bound(abfd) \
281     BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
282
283@end example
284
285@findex bfd_is_local_label
286@subsubsection @code{bfd_is_local_label}
287@strong{Synopsis}
288@example
289bfd_boolean bfd_is_local_label (bfd *abfd, asymbol *sym);
290@end example
291@strong{Description}@*
292Return TRUE if the given symbol @var{sym} in the BFD @var{abfd} is
293a compiler generated local label, else return FALSE.
294
295@findex bfd_is_local_label_name
296@subsubsection @code{bfd_is_local_label_name}
297@strong{Synopsis}
298@example
299bfd_boolean bfd_is_local_label_name (bfd *abfd, const char *name);
300@end example
301@strong{Description}@*
302Return TRUE if a symbol with the name @var{name} in the BFD
303@var{abfd} is a compiler generated local label, else return
304FALSE.  This just checks whether the name has the form of a
305local label.
306@example
307#define bfd_is_local_label_name(abfd, name) \
308  BFD_SEND (abfd, _bfd_is_local_label_name, (abfd, name))
309
310@end example
311
312@findex bfd_canonicalize_symtab
313@subsubsection @code{bfd_canonicalize_symtab}
314@strong{Description}@*
315Read the symbols from the BFD @var{abfd}, and fills in
316the vector @var{location} with pointers to the symbols and
317a trailing NULL.
318Return the actual number of symbol pointers, not
319including the NULL.
320@example
321#define bfd_canonicalize_symtab(abfd, location) \
322  BFD_SEND (abfd, _bfd_canonicalize_symtab, (abfd, location))
323
324@end example
325
326@findex bfd_set_symtab
327@subsubsection @code{bfd_set_symtab}
328@strong{Synopsis}
329@example
330bfd_boolean bfd_set_symtab
331   (bfd *abfd, asymbol **location, unsigned int count);
332@end example
333@strong{Description}@*
334Arrange that when the output BFD @var{abfd} is closed,
335the table @var{location} of @var{count} pointers to symbols
336will be written.
337
338@findex bfd_print_symbol_vandf
339@subsubsection @code{bfd_print_symbol_vandf}
340@strong{Synopsis}
341@example
342void bfd_print_symbol_vandf (bfd *abfd, void *file, asymbol *symbol);
343@end example
344@strong{Description}@*
345Print the value and flags of the @var{symbol} supplied to the
346stream @var{file}.
347
348@findex bfd_make_empty_symbol
349@subsubsection @code{bfd_make_empty_symbol}
350@strong{Description}@*
351Create a new @code{asymbol} structure for the BFD @var{abfd}
352and return a pointer to it.
353
354This routine is necessary because each back end has private
355information surrounding the @code{asymbol}. Building your own
356@code{asymbol} and pointing to it will not create the private
357information, and will cause problems later on.
358@example
359#define bfd_make_empty_symbol(abfd) \
360  BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
361
362@end example
363
364@findex _bfd_generic_make_empty_symbol
365@subsubsection @code{_bfd_generic_make_empty_symbol}
366@strong{Synopsis}
367@example
368asymbol *_bfd_generic_make_empty_symbol (bfd *);
369@end example
370@strong{Description}@*
371Create a new @code{asymbol} structure for the BFD @var{abfd}
372and return a pointer to it.  Used by core file routines,
373binary back-end and anywhere else where no private info
374is needed.
375
376@findex bfd_make_debug_symbol
377@subsubsection @code{bfd_make_debug_symbol}
378@strong{Description}@*
379Create a new @code{asymbol} structure for the BFD @var{abfd},
380to be used as a debugging symbol.  Further details of its use have
381yet to be worked out.
382@example
383#define bfd_make_debug_symbol(abfd,ptr,size) \
384  BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
385
386@end example
387
388@findex bfd_decode_symclass
389@subsubsection @code{bfd_decode_symclass}
390@strong{Description}@*
391Return a character corresponding to the symbol
392class of @var{symbol}, or '?' for an unknown class.
393
394@strong{Synopsis}
395@example
396int bfd_decode_symclass (asymbol *symbol);
397@end example
398@findex bfd_is_undefined_symclass
399@subsubsection @code{bfd_is_undefined_symclass}
400@strong{Description}@*
401Returns non-zero if the class symbol returned by
402bfd_decode_symclass represents an undefined symbol.
403Returns zero otherwise.
404
405@strong{Synopsis}
406@example
407bfd_boolean bfd_is_undefined_symclass (int symclass);
408@end example
409@findex bfd_symbol_info
410@subsubsection @code{bfd_symbol_info}
411@strong{Description}@*
412Fill in the basic info about symbol that nm needs.
413Additional info may be added by the back-ends after
414calling this function.
415
416@strong{Synopsis}
417@example
418void bfd_symbol_info (asymbol *symbol, symbol_info *ret);
419@end example
420@findex bfd_copy_private_symbol_data
421@subsubsection @code{bfd_copy_private_symbol_data}
422@strong{Synopsis}
423@example
424bfd_boolean bfd_copy_private_symbol_data
425   (bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
426@end example
427@strong{Description}@*
428Copy private symbol information from @var{isym} in the BFD
429@var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
430Return @code{TRUE} on success, @code{FALSE} on error.  Possible error
431returns are:
432
433@itemize @bullet
434
435@item
436@code{bfd_error_no_memory} -
437Not enough memory exists to create private data for @var{osec}.
438@end itemize
439@example
440#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
441  BFD_SEND (obfd, _bfd_copy_private_symbol_data, \
442            (ibfd, isymbol, obfd, osymbol))
443
444@end example
445
446