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 "sysdep.h"
82       #include "bfd.h"
83       int main (void)
84       @{
85         bfd *abfd;
86         asymbol *ptrs[2];
87         asymbol *new;
88
89         abfd = bfd_openw ("foo","a.out-sunos-big");
90         bfd_set_format (abfd, bfd_object);
91         new = bfd_make_empty_symbol (abfd);
92         new->name = "dummy_symbol";
93         new->section = bfd_make_section_old_way (abfd, ".text");
94         new->flags = BSF_GLOBAL;
95         new->value = 0x12345;
96
97         ptrs[0] = new;
98         ptrs[1] = 0;
99
100         bfd_set_symtab (abfd, ptrs, 1);
101         bfd_close (abfd);
102         return 0;
103       @}
104
105       ./makesym
106       nm foo
107       00012345 A dummy_symbol
108@end example
109
110Many formats cannot represent arbitrary symbol information; for
111instance, the @code{a.out} object format does not allow an
112arbitrary number of sections. A symbol pointing to a section
113which is not one  of @code{.text}, @code{.data} or @code{.bss} cannot
114be described.
115
116@node Mini Symbols, typedef asymbol, Writing Symbols, Symbols
117@subsection Mini Symbols
118Mini symbols provide read-only access to the symbol table.
119They use less memory space, but require more time to access.
120They can be useful for tools like nm or objdump, which may
121have to handle symbol tables of extremely large executables.
122
123The @code{bfd_read_minisymbols} function will read the symbols
124into memory in an internal form.  It will return a @code{void *}
125pointer to a block of memory, a symbol count, and the size of
126each symbol.  The pointer is allocated using @code{malloc}, and
127should be freed by the caller when it is no longer needed.
128
129The function @code{bfd_minisymbol_to_symbol} will take a pointer
130to a minisymbol, and a pointer to a structure returned by
131@code{bfd_make_empty_symbol}, and return a @code{asymbol} structure.
132The return value may or may not be the same as the value from
133@code{bfd_make_empty_symbol} which was passed in.
134
135
136@node typedef asymbol, symbol handling functions, Mini Symbols, Symbols
137@subsection typedef asymbol
138An @code{asymbol} has the form:
139
140
141@example
142
143typedef struct bfd_symbol
144@{
145  /* A pointer to the BFD which owns the symbol. This information
146     is necessary so that a back end can work out what additional
147     information (invisible to the application writer) is carried
148     with the symbol.
149
150     This field is *almost* redundant, since you can use section->owner
151     instead, except that some symbols point to the global sections
152     bfd_@{abs,com,und@}_section.  This could be fixed by making
153     these globals be per-bfd (or per-target-flavor).  FIXME.  */
154  struct bfd *the_bfd; /* Use bfd_asymbol_bfd(sym) to access this field.  */
155
156  /* The text of the symbol. The name is left alone, and not copied; the
157     application may not alter it.  */
158  const char *name;
159
160  /* The value of the symbol.  This really should be a union of a
161     numeric value with a pointer, since some flags indicate that
162     a pointer to another symbol is stored here.  */
163  symvalue value;
164
165  /* Attributes of a symbol.  */
166#define BSF_NO_FLAGS            0
167
168  /* The symbol has local scope; @code{static} in @code{C}. The value
169     is the offset into the section of the data.  */
170#define BSF_LOCAL               (1 << 0)
171
172  /* The symbol has global scope; initialized data in @code{C}. The
173     value is the offset into the section of the data.  */
174#define BSF_GLOBAL              (1 << 1)
175
176  /* The symbol has global scope and is exported. The value is
177     the offset into the section of the data.  */
178#define BSF_EXPORT              BSF_GLOBAL /* No real difference.  */
179
180  /* A normal C symbol would be one of:
181     @code{BSF_LOCAL}, @code{BSF_UNDEFINED} or @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           (1 << 2)
186
187  /* The symbol denotes a function entry point.  Used in ELF,
188     perhaps others someday.  */
189#define BSF_FUNCTION            (1 << 3)
190
191  /* Used by the linker.  */
192#define BSF_KEEP                (1 << 5)
193
194  /* An ELF common symbol.  */
195#define BSF_ELF_COMMON          (1 << 6)
196
197  /* A weak global symbol, overridable without warnings by
198     a regular global symbol of the same name.  */
199#define BSF_WEAK                (1 << 7)
200
201  /* This symbol was created to point to a section, e.g. ELF's
202     STT_SECTION symbols.  */
203#define BSF_SECTION_SYM         (1 << 8)
204
205  /* The symbol used to be a common symbol, but now it is
206     allocated.  */
207#define BSF_OLD_COMMON          (1 << 9)
208
209  /* In some files the type of a symbol sometimes alters its
210     location in an output file - ie in coff a @code{ISFCN} symbol
211     which is also @code{C_EXT} symbol appears where it was
212     declared and not at the end of a section.  This bit is set
213     by the target BFD part to convey this information.  */
214#define BSF_NOT_AT_END          (1 << 10)
215
216  /* Signal that the symbol is the label of constructor section.  */
217#define BSF_CONSTRUCTOR         (1 << 11)
218
219  /* Signal that the symbol is a warning symbol.  The name is a
220     warning.  The name of the next symbol is the one to warn about;
221     if a reference is made to a symbol with the same name as the next
222     symbol, a warning is issued by the linker.  */
223#define BSF_WARNING             (1 << 12)
224
225  /* Signal that the symbol is indirect.  This symbol is an indirect
226     pointer to the symbol with the same name as the next symbol.  */
227#define BSF_INDIRECT            (1 << 13)
228
229  /* BSF_FILE marks symbols that contain a file name.  This is used
230     for ELF STT_FILE symbols.  */
231#define BSF_FILE                (1 << 14)
232
233  /* Symbol is from dynamic linking information.  */
234#define BSF_DYNAMIC             (1 << 15)
235
236  /* The symbol denotes a data object.  Used in ELF, and perhaps
237     others someday.  */
238#define BSF_OBJECT              (1 << 16)
239
240  /* This symbol is a debugging symbol.  The value is the offset
241     into the section of the data.  BSF_DEBUGGING should be set
242     as well.  */
243#define BSF_DEBUGGING_RELOC     (1 << 17)
244
245  /* This symbol is thread local.  Used in ELF.  */
246#define BSF_THREAD_LOCAL        (1 << 18)
247
248  /* This symbol represents a complex relocation expression,
249     with the expression tree serialized in the symbol name.  */
250#define BSF_RELC                (1 << 19)
251
252  /* This symbol represents a signed complex relocation expression,
253     with the expression tree serialized in the symbol name.  */
254#define BSF_SRELC               (1 << 20)
255
256  /* This symbol was created by bfd_get_synthetic_symtab.  */
257#define BSF_SYNTHETIC           (1 << 21)
258
259  /* This symbol is an indirect code object.  Unrelated to BSF_INDIRECT.
260     The dynamic linker will compute the value of this symbol by
261     calling the function that it points to.  BSF_FUNCTION must
262     also be also set.  */
263#define BSF_GNU_INDIRECT_FUNCTION (1 << 22)
264  /* This symbol is a globally unique data object.  The dynamic linker
265     will make sure that in the entire process there is just one symbol
266     with this name and type in use.  BSF_OBJECT must also be set.  */
267#define BSF_GNU_UNIQUE          (1 << 23)
268
269  /* This section symbol should be included in the symbol table.  */
270#define BSF_SECTION_SYM_USED    (1 << 24)
271
272  flagword flags;
273
274  /* A pointer to the section to which this symbol is
275     relative.  This will always be non NULL, there are special
276     sections for undefined and absolute symbols.  */
277  struct bfd_section *section;
278
279  /* Back end special data.  */
280  union
281    @{
282      void *p;
283      bfd_vma i;
284    @}
285  udata;
286@}
287asymbol;
288
289@end example
290
291@node symbol handling functions,  , typedef asymbol, Symbols
292@subsection Symbol handling functions
293
294
295@findex bfd_get_symtab_upper_bound
296@subsubsection @code{bfd_get_symtab_upper_bound}
297@strong{Description}@*
298Return the number of bytes required to store a vector of pointers
299to @code{asymbols} for all the symbols in the BFD @var{abfd},
300including a terminal NULL pointer. If there are no symbols in
301the BFD, then return 0.  If an error occurs, return -1.
302@example
303#define bfd_get_symtab_upper_bound(abfd) \
304       BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
305
306@end example
307
308@findex bfd_is_local_label
309@subsubsection @code{bfd_is_local_label}
310@strong{Synopsis}
311@example
312bool bfd_is_local_label (bfd *abfd, asymbol *sym);
313@end example
314@strong{Description}@*
315Return TRUE if the given symbol @var{sym} in the BFD @var{abfd} is
316a compiler generated local label, else return FALSE.
317
318@findex bfd_is_local_label_name
319@subsubsection @code{bfd_is_local_label_name}
320@strong{Synopsis}
321@example
322bool bfd_is_local_label_name (bfd *abfd, const char *name);
323@end example
324@strong{Description}@*
325Return TRUE if a symbol with the name @var{name} in the BFD
326@var{abfd} is a compiler generated local label, else return
327FALSE.  This just checks whether the name has the form of a
328local label.
329@example
330#define bfd_is_local_label_name(abfd, name) \
331       BFD_SEND (abfd, _bfd_is_local_label_name, (abfd, name))
332
333@end example
334
335@findex bfd_is_target_special_symbol
336@subsubsection @code{bfd_is_target_special_symbol}
337@strong{Synopsis}
338@example
339bool bfd_is_target_special_symbol (bfd *abfd, asymbol *sym);
340@end example
341@strong{Description}@*
342Return TRUE iff a symbol @var{sym} in the BFD @var{abfd} is something
343special to the particular target represented by the BFD.  Such symbols
344should normally not be mentioned to the user.
345@example
346#define bfd_is_target_special_symbol(abfd, sym) \
347       BFD_SEND (abfd, _bfd_is_target_special_symbol, (abfd, sym))
348
349@end example
350
351@findex bfd_canonicalize_symtab
352@subsubsection @code{bfd_canonicalize_symtab}
353@strong{Description}@*
354Read the symbols from the BFD @var{abfd}, and fills in
355the vector @var{location} with pointers to the symbols and
356a trailing NULL.
357Return the actual number of symbol pointers, not
358including the NULL.
359@example
360#define bfd_canonicalize_symtab(abfd, location) \
361       BFD_SEND (abfd, _bfd_canonicalize_symtab, (abfd, location))
362
363@end example
364
365@findex bfd_set_symtab
366@subsubsection @code{bfd_set_symtab}
367@strong{Synopsis}
368@example
369bool bfd_set_symtab
370   (bfd *abfd, asymbol **location, unsigned int count);
371@end example
372@strong{Description}@*
373Arrange that when the output BFD @var{abfd} is closed,
374the table @var{location} of @var{count} pointers to symbols
375will be written.
376
377@findex bfd_print_symbol_vandf
378@subsubsection @code{bfd_print_symbol_vandf}
379@strong{Synopsis}
380@example
381void bfd_print_symbol_vandf (bfd *abfd, void *file, asymbol *symbol);
382@end example
383@strong{Description}@*
384Print the value and flags of the @var{symbol} supplied to the
385stream @var{file}.
386
387@findex bfd_make_empty_symbol
388@subsubsection @code{bfd_make_empty_symbol}
389@strong{Description}@*
390Create a new @code{asymbol} structure for the BFD @var{abfd}
391and return a pointer to it.
392
393This routine is necessary because each back end has private
394information surrounding the @code{asymbol}. Building your own
395@code{asymbol} and pointing to it will not create the private
396information, and will cause problems later on.
397@example
398#define bfd_make_empty_symbol(abfd) \
399       BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
400
401@end example
402
403@findex _bfd_generic_make_empty_symbol
404@subsubsection @code{_bfd_generic_make_empty_symbol}
405@strong{Synopsis}
406@example
407asymbol *_bfd_generic_make_empty_symbol (bfd *);
408@end example
409@strong{Description}@*
410Create a new @code{asymbol} structure for the BFD @var{abfd}
411and return a pointer to it.  Used by core file routines,
412binary back-end and anywhere else where no private info
413is needed.
414
415@findex bfd_make_debug_symbol
416@subsubsection @code{bfd_make_debug_symbol}
417@strong{Description}@*
418Create a new @code{asymbol} structure for the BFD @var{abfd},
419to be used as a debugging symbol.  Further details of its use have
420yet to be worked out.
421@example
422#define bfd_make_debug_symbol(abfd,ptr,size) \
423       BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
424
425@end example
426
427@findex bfd_decode_symclass
428@subsubsection @code{bfd_decode_symclass}
429@strong{Description}@*
430Return a character corresponding to the symbol
431class of @var{symbol}, or '?' for an unknown class.
432
433@strong{Synopsis}
434@example
435int bfd_decode_symclass (asymbol *symbol);
436@end example
437@findex bfd_is_undefined_symclass
438@subsubsection @code{bfd_is_undefined_symclass}
439@strong{Description}@*
440Returns non-zero if the class symbol returned by
441bfd_decode_symclass represents an undefined symbol.
442Returns zero otherwise.
443
444@strong{Synopsis}
445@example
446bool bfd_is_undefined_symclass (int symclass);
447@end example
448@findex bfd_symbol_info
449@subsubsection @code{bfd_symbol_info}
450@strong{Description}@*
451Fill in the basic info about symbol that nm needs.
452Additional info may be added by the back-ends after
453calling this function.
454
455@strong{Synopsis}
456@example
457void bfd_symbol_info (asymbol *symbol, symbol_info *ret);
458@end example
459@findex bfd_copy_private_symbol_data
460@subsubsection @code{bfd_copy_private_symbol_data}
461@strong{Synopsis}
462@example
463bool bfd_copy_private_symbol_data
464   (bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
465@end example
466@strong{Description}@*
467Copy private symbol information from @var{isym} in the BFD
468@var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
469Return @code{TRUE} on success, @code{FALSE} on error.  Possible error
470returns are:
471
472@itemize @bullet
473
474@item
475@code{bfd_error_no_memory} -
476Not enough memory exists to create private data for @var{osec}.
477@end itemize
478@example
479#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
480       BFD_SEND (obfd, _bfd_copy_private_symbol_data, \
481                 (ibfd, isymbol, obfd, osymbol))
482
483@end example
484
485