bfd/
[binutils.git] / bfd / doc / syms.texi
blobd5dc6599f2a957ad412d25c0db936018a1135c03
1 @section Symbols
2 BFD tries to maintain as much symbol information as it can when
3 it moves information from file to file. BFD passes information
4 to applications though the @code{asymbol} structure. When the
5 application requests the symbol table, BFD reads the table in
6 the native form and translates parts of it into the internal
7 format. To maintain more than the information passed to
8 applications, some targets keep some information ``behind the
9 scenes'' in a structure only the particular back end knows
10 about. For example, the coff back end keeps the original
11 symbol table structure as well as the canonical structure when
12 a BFD is read in. On output, the coff back end can reconstruct
13 the output symbol table so that no information is lost, even
14 information unique to coff which BFD doesn't know or
15 understand. If a coff symbol table were read, but were written
16 through an a.out back end, all the coff specific information
17 would be lost. The symbol table of a BFD
18 is not necessarily read in until a canonicalize request is
19 made. Then the BFD back end fills in a table provided by the
20 application with pointers to the canonical information.  To
21 output symbols, the application provides BFD with a table of
22 pointers to pointers to @code{asymbol}s. This allows applications
23 like the linker to output a symbol as it was read, since the ``behind
24 the 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
33 @node Reading Symbols, Writing Symbols, Symbols, Symbols
34 @subsection Reading symbols
35 There are two stages to reading a symbol table from a BFD:
36 allocating storage, and the actual reading process. This is an
37 excerpt from an application which reads the symbol table:
39 @example
40          long storage_needed;
41          asymbol **symbol_table;
42          long number_of_symbols;
43          long i;
45          storage_needed = bfd_get_symtab_upper_bound (abfd);
47          if (storage_needed < 0)
48            FAIL
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);
58          if (number_of_symbols < 0)
59            FAIL
61          for (i = 0; i < number_of_symbols; i++)
62            process_symbol (symbol_table[i]);
63 @end example
65 All storage for the symbols themselves is in an objalloc
66 connected to the BFD; it is freed when the BFD is closed.
68 @node Writing Symbols, Mini Symbols, Reading Symbols, Symbols
69 @subsection Writing symbols
70 Writing of a symbol table is automatic when a BFD open for
71 writing is closed. The application attaches a vector of
72 pointers to pointers to symbols to the BFD being written, and
73 fills in the symbol count. The close and cleanup code reads
74 through the table provided and performs all the necessary
75 operations. The BFD output code must always be provided with an
76 ``owned'' symbol: one which has come from another BFD, or one
77 which has been created using @code{bfd_make_empty_symbol}.  Here is an
78 example showing the creation of a symbol table with only one element:
80 @example
81        #include "bfd.h"
82        int main (void)
83        @{
84          bfd *abfd;
85          asymbol *ptrs[2];
86          asymbol *new;
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;
96          ptrs[0] = new;
97          ptrs[1] = 0;
99          bfd_set_symtab (abfd, ptrs, 1);
100          bfd_close (abfd);
101          return 0;
102        @}
104        ./makesym
105        nm foo
106        00012345 A dummy_symbol
107 @end example
109 Many formats cannot represent arbitrary symbol information; for
110 instance, the @code{a.out} object format does not allow an
111 arbitrary number of sections. A symbol pointing to a section
112 which is not one  of @code{.text}, @code{.data} or @code{.bss} cannot
113 be described.
115 @node Mini Symbols, typedef asymbol, Writing Symbols, Symbols
116 @subsection Mini Symbols
117 Mini symbols provide read-only access to the symbol table.
118 They use less memory space, but require more time to access.
119 They can be useful for tools like nm or objdump, which may
120 have to handle symbol tables of extremely large executables.
122 The @code{bfd_read_minisymbols} function will read the symbols
123 into memory in an internal form.  It will return a @code{void *}
124 pointer to a block of memory, a symbol count, and the size of
125 each symbol.  The pointer is allocated using @code{malloc}, and
126 should be freed by the caller when it is no longer needed.
128 The function @code{bfd_minisymbol_to_symbol} will take a pointer
129 to a minisymbol, and a pointer to a structure returned by
130 @code{bfd_make_empty_symbol}, and return a @code{asymbol} structure.
131 The return value may or may not be the same as the value from
132 @code{bfd_make_empty_symbol} which was passed in.
135 @node typedef asymbol, symbol handling functions, Mini Symbols, Symbols
136 @subsection typedef asymbol
137 An @code{asymbol} has the form:
140 @example
142 typedef struct bfd_symbol
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.
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.  */
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;
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;
164   /* Attributes of a symbol.  */
165 #define BSF_NO_FLAGS           0x00
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              (1 << 0)
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             (1 << 1)
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.  */
179   /* A normal C symbol would be one of:
180      @code{BSF_LOCAL}, @code{BSF_COMMON},  @code{BSF_UNDEFINED} or
181      @code{BSF_GLOBAL}.  */
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)
187   /* The symbol denotes a function entry point.  Used in ELF,
188      perhaps others someday.  */
189 #define BSF_FUNCTION           (1 << 3)
191   /* Used by the linker.  */
192 #define BSF_KEEP               (1 << 5)
193 #define BSF_KEEP_G             (1 << 6)
195   /* A weak global symbol, overridable without warnings by
196      a regular global symbol of the same name.  */
197 #define BSF_WEAK               (1 << 7)
199   /* This symbol was created to point to a section, e.g. ELF's
200      STT_SECTION symbols.  */
201 #define BSF_SECTION_SYM        (1 << 8)
203   /* The symbol used to be a common symbol, but now it is
204      allocated.  */
205 #define BSF_OLD_COMMON         (1 << 9)
207   /* In some files the type of a symbol sometimes alters its
208      location in an output file - ie in coff a @code{ISFCN} symbol
209      which is also @code{C_EXT} symbol appears where it was
210      declared and not at the end of a section.  This bit is set
211      by the target BFD part to convey this information.  */
212 #define BSF_NOT_AT_END         (1 << 10)
214   /* Signal that the symbol is the label of constructor section.  */
215 #define BSF_CONSTRUCTOR        (1 << 11)
217   /* Signal that the symbol is a warning symbol.  The name is a
218      warning.  The name of the next symbol is the one to warn about;
219      if a reference is made to a symbol with the same name as the next
220      symbol, a warning is issued by the linker.  */
221 #define BSF_WARNING            (1 << 12)
223   /* Signal that the symbol is indirect.  This symbol is an indirect
224      pointer to the symbol with the same name as the next symbol.  */
225 #define BSF_INDIRECT           (1 << 13)
227   /* BSF_FILE marks symbols that contain a file name.  This is used
228      for ELF STT_FILE symbols.  */
229 #define BSF_FILE               (1 << 14)
231   /* Symbol is from dynamic linking information.  */
232 #define BSF_DYNAMIC            (1 << 15)
234   /* The symbol denotes a data object.  Used in ELF, and perhaps
235      others someday.  */
236 #define BSF_OBJECT             (1 << 16)
238   /* This symbol is a debugging symbol.  The value is the offset
239      into the section of the data.  BSF_DEBUGGING should be set
240      as well.  */
241 #define BSF_DEBUGGING_RELOC    (1 << 17)
243   /* This symbol is thread local.  Used in ELF.  */
244 #define BSF_THREAD_LOCAL       (1 << 18)
246   /* This symbol represents a complex relocation expression,
247      with the expression tree serialized in the symbol name.  */
248 #define BSF_RELC               (1 << 19)
250   /* This symbol represents a signed complex relocation expression,
251      with the expression tree serialized in the symbol name.  */
252 #define BSF_SRELC              (1 << 20)
254   /* This symbol was created by bfd_get_synthetic_symtab.  */
255 #define BSF_SYNTHETIC          (1 << 21)
257   /* This symbol is an indirect code object.  Unrelated to BSF_INDIRECT.
258      The dynamic linker will compute the value of this symbol by
259      calling the function that it points to.  BSF_FUNCTION must
260      also be also set.  */
261 #define BSF_GNU_INDIRECT_FUNCTION (1 << 22)
262   /* This symbol is a globally unique data object.  The dynamic linker
263      will make sure that in the entire process there is just one symbol
264      with this name and type in use.  BSF_OBJECT must also be set.  */
265 #define BSF_GNU_UNIQUE         (1 << 23)
267   flagword flags;
269   /* A pointer to the section to which this symbol is
270      relative.  This will always be non NULL, there are special
271      sections for undefined and absolute symbols.  */
272   struct bfd_section *section;
274   /* Back end special data.  */
275   union
276     @{
277       void *p;
278       bfd_vma i;
279     @}
280   udata;
282 asymbol;
284 @end example
286 @node symbol handling functions,  , typedef asymbol, Symbols
287 @subsection Symbol handling functions
290 @findex bfd_get_symtab_upper_bound
291 @subsubsection @code{bfd_get_symtab_upper_bound}
292 @strong{Description}@*
293 Return the number of bytes required to store a vector of pointers
294 to @code{asymbols} for all the symbols in the BFD @var{abfd},
295 including a terminal NULL pointer. If there are no symbols in
296 the BFD, then return 0.  If an error occurs, return -1.
297 @example
298 #define bfd_get_symtab_upper_bound(abfd) \
299      BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
301 @end example
303 @findex bfd_is_local_label
304 @subsubsection @code{bfd_is_local_label}
305 @strong{Synopsis}
306 @example
307 bfd_boolean bfd_is_local_label (bfd *abfd, asymbol *sym);
308 @end example
309 @strong{Description}@*
310 Return TRUE if the given symbol @var{sym} in the BFD @var{abfd} is
311 a compiler generated local label, else return FALSE.
313 @findex bfd_is_local_label_name
314 @subsubsection @code{bfd_is_local_label_name}
315 @strong{Synopsis}
316 @example
317 bfd_boolean bfd_is_local_label_name (bfd *abfd, const char *name);
318 @end example
319 @strong{Description}@*
320 Return TRUE if a symbol with the name @var{name} in the BFD
321 @var{abfd} is a compiler generated local label, else return
322 FALSE.  This just checks whether the name has the form of a
323 local label.
324 @example
325 #define bfd_is_local_label_name(abfd, name) \
326   BFD_SEND (abfd, _bfd_is_local_label_name, (abfd, name))
328 @end example
330 @findex bfd_is_target_special_symbol
331 @subsubsection @code{bfd_is_target_special_symbol}
332 @strong{Synopsis}
333 @example
334 bfd_boolean bfd_is_target_special_symbol (bfd *abfd, asymbol *sym);
335 @end example
336 @strong{Description}@*
337 Return TRUE iff a symbol @var{sym} in the BFD @var{abfd} is something
338 special to the particular target represented by the BFD.  Such symbols
339 should normally not be mentioned to the user.
340 @example
341 #define bfd_is_target_special_symbol(abfd, sym) \
342   BFD_SEND (abfd, _bfd_is_target_special_symbol, (abfd, sym))
344 @end example
346 @findex bfd_canonicalize_symtab
347 @subsubsection @code{bfd_canonicalize_symtab}
348 @strong{Description}@*
349 Read the symbols from the BFD @var{abfd}, and fills in
350 the vector @var{location} with pointers to the symbols and
351 a trailing NULL.
352 Return the actual number of symbol pointers, not
353 including the NULL.
354 @example
355 #define bfd_canonicalize_symtab(abfd, location) \
356   BFD_SEND (abfd, _bfd_canonicalize_symtab, (abfd, location))
358 @end example
360 @findex bfd_set_symtab
361 @subsubsection @code{bfd_set_symtab}
362 @strong{Synopsis}
363 @example
364 bfd_boolean bfd_set_symtab
365    (bfd *abfd, asymbol **location, unsigned int count);
366 @end example
367 @strong{Description}@*
368 Arrange that when the output BFD @var{abfd} is closed,
369 the table @var{location} of @var{count} pointers to symbols
370 will be written.
372 @findex bfd_print_symbol_vandf
373 @subsubsection @code{bfd_print_symbol_vandf}
374 @strong{Synopsis}
375 @example
376 void bfd_print_symbol_vandf (bfd *abfd, void *file, asymbol *symbol);
377 @end example
378 @strong{Description}@*
379 Print the value and flags of the @var{symbol} supplied to the
380 stream @var{file}.
382 @findex bfd_make_empty_symbol
383 @subsubsection @code{bfd_make_empty_symbol}
384 @strong{Description}@*
385 Create a new @code{asymbol} structure for the BFD @var{abfd}
386 and return a pointer to it.
388 This routine is necessary because each back end has private
389 information surrounding the @code{asymbol}. Building your own
390 @code{asymbol} and pointing to it will not create the private
391 information, and will cause problems later on.
392 @example
393 #define bfd_make_empty_symbol(abfd) \
394   BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
396 @end example
398 @findex _bfd_generic_make_empty_symbol
399 @subsubsection @code{_bfd_generic_make_empty_symbol}
400 @strong{Synopsis}
401 @example
402 asymbol *_bfd_generic_make_empty_symbol (bfd *);
403 @end example
404 @strong{Description}@*
405 Create a new @code{asymbol} structure for the BFD @var{abfd}
406 and return a pointer to it.  Used by core file routines,
407 binary back-end and anywhere else where no private info
408 is needed.
410 @findex bfd_make_debug_symbol
411 @subsubsection @code{bfd_make_debug_symbol}
412 @strong{Description}@*
413 Create a new @code{asymbol} structure for the BFD @var{abfd},
414 to be used as a debugging symbol.  Further details of its use have
415 yet to be worked out.
416 @example
417 #define bfd_make_debug_symbol(abfd,ptr,size) \
418   BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
420 @end example
422 @findex bfd_decode_symclass
423 @subsubsection @code{bfd_decode_symclass}
424 @strong{Description}@*
425 Return a character corresponding to the symbol
426 class of @var{symbol}, or '?' for an unknown class.
428 @strong{Synopsis}
429 @example
430 int bfd_decode_symclass (asymbol *symbol);
431 @end example
432 @findex bfd_is_undefined_symclass
433 @subsubsection @code{bfd_is_undefined_symclass}
434 @strong{Description}@*
435 Returns non-zero if the class symbol returned by
436 bfd_decode_symclass represents an undefined symbol.
437 Returns zero otherwise.
439 @strong{Synopsis}
440 @example
441 bfd_boolean bfd_is_undefined_symclass (int symclass);
442 @end example
443 @findex bfd_symbol_info
444 @subsubsection @code{bfd_symbol_info}
445 @strong{Description}@*
446 Fill in the basic info about symbol that nm needs.
447 Additional info may be added by the back-ends after
448 calling this function.
450 @strong{Synopsis}
451 @example
452 void bfd_symbol_info (asymbol *symbol, symbol_info *ret);
453 @end example
454 @findex bfd_copy_private_symbol_data
455 @subsubsection @code{bfd_copy_private_symbol_data}
456 @strong{Synopsis}
457 @example
458 bfd_boolean bfd_copy_private_symbol_data
459    (bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
460 @end example
461 @strong{Description}@*
462 Copy private symbol information from @var{isym} in the BFD
463 @var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
464 Return @code{TRUE} on success, @code{FALSE} on error.  Possible error
465 returns are:
467 @itemize @bullet
469 @item
470 @code{bfd_error_no_memory} -
471 Not enough memory exists to create private data for @var{osec}.
472 @end itemize
473 @example
474 #define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
475   BFD_SEND (obfd, _bfd_copy_private_symbol_data, \
476             (ibfd, isymbol, obfd, osymbol))
478 @end example