Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / binutils / bfd / doc / bfdt.texi
1 @section @code{typedef bfd}
2 A BFD has type @code{bfd}; objects of this type are the
3 cornerstone of any application using BFD. Using BFD
4 consists of making references though the BFD and to data in the BFD.
5
6 Here is the structure that defines the type @code{bfd}.  It
7 contains the major data about the file and pointers
8 to the rest of the data.
9
10
11 @example
12
13 struct _bfd
14 @{
15   /* The filename the application opened the BFD with.  */
16   const char *filename;
17
18   /* A pointer to the target jump table.  */
19   const struct bfd_target *xvec;
20
21   /* To avoid dragging too many header files into every file that
22      includes `@code{bfd.h}', IOSTREAM has been declared as a "char *",
23      and MTIME as a "long".  Their correct types, to which they
24      are cast when used, are "FILE *" and "time_t".    The iostream
25      is the result of an fopen on the filename.  However, if the
26      BFD_IN_MEMORY flag is set, then iostream is actually a pointer
27      to a bfd_in_memory struct.  */
28   PTR iostream;
29
30   /* Is the file descriptor being cached?  That is, can it be closed as
31      needed, and re-opened when accessed later?  */
32   boolean cacheable;
33
34   /* Marks whether there was a default target specified when the
35      BFD was opened. This is used to select which matching algorithm
36      to use to choose the back end.  */
37   boolean target_defaulted;
38
39   /* The caching routines use these to maintain a
40      least-recently-used list of BFDs.  */
41   struct _bfd *lru_prev, *lru_next;
42
43   /* When a file is closed by the caching routines, BFD retains
44      state information on the file here...  */
45   ufile_ptr where;
46
47   /* ... and here: (``once'' means at least once).  */
48   boolean opened_once;
49
50   /* Set if we have a locally maintained mtime value, rather than
51      getting it from the file each time.  */
52   boolean mtime_set;
53
54   /* File modified time, if mtime_set is true.  */
55   long mtime;
56
57   /* Reserved for an unimplemented file locking extension.  */
58   int ifd;
59
60   /* The format which belongs to the BFD. (object, core, etc.)  */
61   bfd_format format;
62
63   /* The direction with which the BFD was opened.  */
64   enum bfd_direction
65     @{
66       no_direction = 0,
67       read_direction = 1,
68       write_direction = 2,
69       both_direction = 3
70     @}
71   direction;
72
73   /* Format_specific flags.  */
74   flagword flags;
75
76   /* Currently my_archive is tested before adding origin to
77      anything. I believe that this can become always an add of
78      origin, with origin set to 0 for non archive files.  */
79   ufile_ptr origin;
80
81   /* Remember when output has begun, to stop strange things
82      from happening.  */
83   boolean output_has_begun;
84
85   /* A hash table for section names.  */
86   struct bfd_hash_table section_htab;
87
88   /* Pointer to linked list of sections.  */
89   struct sec *sections;
90
91   /* The place where we add to the section list.  */
92   struct sec **section_tail;
93
94   /* The number of sections.  */
95   unsigned int section_count;
96
97   /* Stuff only useful for object files:
98      The start address.  */
99   bfd_vma start_address;
100
101   /* Used for input and output.  */
102   unsigned int symcount;
103
104   /* Symbol table for output BFD (with symcount entries).  */
105   struct symbol_cache_entry  **outsymbols;
106
107   /* Pointer to structure which contains architecture information.  */
108   const struct bfd_arch_info *arch_info;
109
110   /* Stuff only useful for archives.  */
111   PTR arelt_data;
112   struct _bfd *my_archive;     /* The containing archive BFD.  */
113   struct _bfd *next;           /* The next BFD in the archive.  */
114   struct _bfd *archive_head;   /* The first BFD in the archive.  */
115   boolean has_armap;
116
117   /* A chain of BFD structures involved in a link.  */
118   struct _bfd *link_next;
119
120   /* A field used by _bfd_generic_link_add_archive_symbols.  This will
121      be used only for archive elements.  */
122   int archive_pass;
123
124   /* Used by the back end to hold private data.  */
125   union
126     @{
127       struct aout_data_struct *aout_data;
128       struct artdata *aout_ar_data;
129       struct _oasys_data *oasys_obj_data;
130       struct _oasys_ar_data *oasys_ar_data;
131       struct coff_tdata *coff_obj_data;
132       struct pe_tdata *pe_obj_data;
133       struct xcoff_tdata *xcoff_obj_data;
134       struct ecoff_tdata *ecoff_obj_data;
135       struct ieee_data_struct *ieee_data;
136       struct ieee_ar_data_struct *ieee_ar_data;
137       struct srec_data_struct *srec_data;
138       struct ihex_data_struct *ihex_data;
139       struct tekhex_data_struct *tekhex_data;
140       struct elf_obj_tdata *elf_obj_data;
141       struct nlm_obj_tdata *nlm_obj_data;
142       struct bout_data_struct *bout_data;
143       struct mmo_data_struct *mmo_data;
144       struct sun_core_struct *sun_core_data;
145       struct sco5_core_struct *sco5_core_data;
146       struct trad_core_struct *trad_core_data;
147       struct som_data_struct *som_data;
148       struct hpux_core_struct *hpux_core_data;
149       struct hppabsd_core_struct *hppabsd_core_data;
150       struct sgi_core_struct *sgi_core_data;
151       struct lynx_core_struct *lynx_core_data;
152       struct osf_core_struct *osf_core_data;
153       struct cisco_core_struct *cisco_core_data;
154       struct versados_data_struct *versados_data;
155       struct netbsd_core_struct *netbsd_core_data;
156       PTR any;
157     @}
158   tdata;
159
160   /* Used by the application to hold private data.  */
161   PTR usrdata;
162
163   /* Where all the allocated stuff under this BFD goes.  This is a
164      struct objalloc *, but we use PTR to avoid requiring the inclusion of
165      objalloc.h.  */
166   PTR memory;
167 @};
168
169 @end example
170 @section Error reporting
171 Most BFD functions return nonzero on success (check their
172 individual documentation for precise semantics).  On an error,
173 they call @code{bfd_set_error} to set an error condition that callers
174 can check by calling @code{bfd_get_error}.
175 If that returns @code{bfd_error_system_call}, then check
176 @code{errno}.
177
178 The easiest way to report a BFD error to the user is to
179 use @code{bfd_perror}.
180
181 @subsection Type @code{bfd_error_type}
182 The values returned by @code{bfd_get_error} are defined by the
183 enumerated type @code{bfd_error_type}.
184
185
186 @example
187
188 typedef enum bfd_error
189 @{
190   bfd_error_no_error = 0,
191   bfd_error_system_call,
192   bfd_error_invalid_target,
193   bfd_error_wrong_format,
194   bfd_error_wrong_object_format,
195   bfd_error_invalid_operation,
196   bfd_error_no_memory,
197   bfd_error_no_symbols,
198   bfd_error_no_armap,
199   bfd_error_no_more_archived_files,
200   bfd_error_malformed_archive,
201   bfd_error_file_not_recognized,
202   bfd_error_file_ambiguously_recognized,
203   bfd_error_no_contents,
204   bfd_error_nonrepresentable_section,
205   bfd_error_no_debug_section,
206   bfd_error_bad_value,
207   bfd_error_file_truncated,
208   bfd_error_file_too_big,
209   bfd_error_invalid_error_code
210 @}
211 bfd_error_type;
212
213 @end example
214 @findex bfd_get_error
215 @subsubsection @code{bfd_get_error}
216 @strong{Synopsis}
217 @example
218 bfd_error_type bfd_get_error (void);
219 @end example
220 @strong{Description}@*
221 Return the current BFD error condition.
222
223 @findex bfd_set_error
224 @subsubsection @code{bfd_set_error}
225 @strong{Synopsis}
226 @example
227 void bfd_set_error (bfd_error_type error_tag);
228 @end example
229 @strong{Description}@*
230 Set the BFD error condition to be @var{error_tag}.
231
232 @findex bfd_errmsg
233 @subsubsection @code{bfd_errmsg}
234 @strong{Synopsis}
235 @example
236 const char *bfd_errmsg (bfd_error_type error_tag);
237 @end example
238 @strong{Description}@*
239 Return a string describing the error @var{error_tag}, or
240 the system error if @var{error_tag} is @code{bfd_error_system_call}.
241
242 @findex bfd_perror
243 @subsubsection @code{bfd_perror}
244 @strong{Synopsis}
245 @example
246 void bfd_perror (const char *message);
247 @end example
248 @strong{Description}@*
249 Print to the standard error stream a string describing the
250 last BFD error that occurred, or the last system error if
251 the last BFD error was a system call failure.  If @var{message}
252 is non-NULL and non-empty, the error string printed is preceded
253 by @var{message}, a colon, and a space.  It is followed by a newline.
254
255 @subsection BFD error handler
256 Some BFD functions want to print messages describing the
257 problem.  They call a BFD error handler function.  This
258 function may be overriden by the program.
259
260 The BFD error handler acts like printf.
261
262
263 @example
264
265 typedef void (*bfd_error_handler_type) PARAMS ((const char *, ...));
266
267 @end example
268 @findex bfd_set_error_handler
269 @subsubsection @code{bfd_set_error_handler}
270 @strong{Synopsis}
271 @example
272 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
273 @end example
274 @strong{Description}@*
275 Set the BFD error handler function.  Returns the previous
276 function.
277
278 @findex bfd_set_error_program_name
279 @subsubsection @code{bfd_set_error_program_name}
280 @strong{Synopsis}
281 @example
282 void bfd_set_error_program_name (const char *);
283 @end example
284 @strong{Description}@*
285 Set the program name to use when printing a BFD error.  This
286 is printed before the error message followed by a colon and
287 space.  The string must not be changed after it is passed to
288 this function.
289
290 @findex bfd_get_error_handler
291 @subsubsection @code{bfd_get_error_handler}
292 @strong{Synopsis}
293 @example
294 bfd_error_handler_type bfd_get_error_handler (void);
295 @end example
296 @strong{Description}@*
297 Return the BFD error handler function.
298
299 @findex bfd_archive_filename
300 @subsubsection @code{bfd_archive_filename}
301 @strong{Synopsis}
302 @example
303 const char *bfd_archive_filename (bfd *);
304 @end example
305 @strong{Description}@*
306 For a BFD that is a component of an archive, returns a string
307 with both the archive name and file name.  For other BFDs, just
308 returns the file name.
309
310 @section Symbols
311
312
313 @findex bfd_get_reloc_upper_bound
314 @subsubsection @code{bfd_get_reloc_upper_bound}
315 @strong{Synopsis}
316 @example
317 long bfd_get_reloc_upper_bound(bfd *abfd, asection *sect);
318 @end example
319 @strong{Description}@*
320 Return the number of bytes required to store the
321 relocation information associated with section @var{sect}
322 attached to bfd @var{abfd}.  If an error occurs, return -1.
323
324 @findex bfd_canonicalize_reloc
325 @subsubsection @code{bfd_canonicalize_reloc}
326 @strong{Synopsis}
327 @example
328 long bfd_canonicalize_reloc
329    (bfd *abfd,
330     asection *sec,
331     arelent **loc,
332     asymbol **syms);
333 @end example
334 @strong{Description}@*
335 Call the back end associated with the open BFD
336 @var{abfd} and translate the external form of the relocation
337 information attached to @var{sec} into the internal canonical
338 form.  Place the table into memory at @var{loc}, which has
339 been preallocated, usually by a call to
340 @code{bfd_get_reloc_upper_bound}.  Returns the number of relocs, or
341 -1 on error.
342
343 The @var{syms} table is also needed for horrible internal magic
344 reasons.
345
346 @findex bfd_set_reloc
347 @subsubsection @code{bfd_set_reloc}
348 @strong{Synopsis}
349 @example
350 void bfd_set_reloc
351    (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
352 @end example
353 @strong{Description}@*
354 Set the relocation pointer and count within
355 section @var{sec} to the values @var{rel} and @var{count}.
356 The argument @var{abfd} is ignored.
357
358 @findex bfd_set_file_flags
359 @subsubsection @code{bfd_set_file_flags}
360 @strong{Synopsis}
361 @example
362 boolean bfd_set_file_flags(bfd *abfd, flagword flags);
363 @end example
364 @strong{Description}@*
365 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
366
367 Possible errors are:
368 @itemize @bullet
369
370 @item
371 @code{bfd_error_wrong_format} - The target bfd was not of object format.
372 @item
373 @code{bfd_error_invalid_operation} - The target bfd was open for reading.
374 @item
375 @code{bfd_error_invalid_operation} -
376 The flag word contained a bit which was not applicable to the
377 type of file.  E.g., an attempt was made to set the @code{D_PAGED} bit
378 on a BFD format which does not support demand paging.
379 @end itemize
380
381 @findex bfd_get_arch_size
382 @subsubsection @code{bfd_get_arch_size}
383 @strong{Synopsis}
384 @example
385 int bfd_get_arch_size (bfd *abfd);
386 @end example
387 @strong{Description}@*
388 Returns the architecture address size, in bits, as determined
389 by the object file's format.  For ELF, this information is
390 included in the header.
391
392 @strong{Returns}@*
393 Returns the arch size in bits if known, @code{-1} otherwise.
394
395 @findex bfd_get_sign_extend_vma
396 @subsubsection @code{bfd_get_sign_extend_vma}
397 @strong{Synopsis}
398 @example
399 int bfd_get_sign_extend_vma (bfd *abfd);
400 @end example
401 @strong{Description}@*
402 Indicates if the target architecture "naturally" sign extends
403 an address.  Some architectures implicitly sign extend address
404 values when they are converted to types larger than the size
405 of an address.  For instance, bfd_get_start_address() will
406 return an address sign extended to fill a bfd_vma when this is
407 the case.
408
409 @strong{Returns}@*
410 Returns @code{1} if the target architecture is known to sign
411 extend addresses, @code{0} if the target architecture is known to
412 not sign extend addresses, and @code{-1} otherwise.
413
414 @findex bfd_set_start_address
415 @subsubsection @code{bfd_set_start_address}
416 @strong{Synopsis}
417 @example
418 boolean bfd_set_start_address(bfd *abfd, bfd_vma vma);
419 @end example
420 @strong{Description}@*
421 Make @var{vma} the entry point of output BFD @var{abfd}.
422
423 @strong{Returns}@*
424 Returns @code{true} on success, @code{false} otherwise.
425
426 @findex bfd_get_mtime
427 @subsubsection @code{bfd_get_mtime}
428 @strong{Synopsis}
429 @example
430 long bfd_get_mtime(bfd *abfd);
431 @end example
432 @strong{Description}@*
433 Return the file modification time (as read from the file system, or
434 from the archive header for archive members).
435
436 @findex bfd_get_size
437 @subsubsection @code{bfd_get_size}
438 @strong{Synopsis}
439 @example
440 long bfd_get_size(bfd *abfd);
441 @end example
442 @strong{Description}@*
443 Return the file size (as read from file system) for the file
444 associated with BFD @var{abfd}.
445
446 The initial motivation for, and use of, this routine is not
447 so we can get the exact size of the object the BFD applies to, since
448 that might not be generally possible (archive members for example).
449 It would be ideal if someone could eventually modify
450 it so that such results were guaranteed.
451
452 Instead, we want to ask questions like "is this NNN byte sized
453 object I'm about to try read from file offset YYY reasonable?"
454 As as example of where we might do this, some object formats
455 use string tables for which the first @code{sizeof (long)} bytes of the
456 table contain the size of the table itself, including the size bytes.
457 If an application tries to read what it thinks is one of these
458 string tables, without some way to validate the size, and for
459 some reason the size is wrong (byte swapping error, wrong location
460 for the string table, etc.), the only clue is likely to be a read
461 error when it tries to read the table, or a "virtual memory
462 exhausted" error when it tries to allocate 15 bazillon bytes
463 of space for the 15 bazillon byte table it is about to read.
464 This function at least allows us to answer the quesion, "is the
465 size reasonable?".
466
467 @findex bfd_get_gp_size
468 @subsubsection @code{bfd_get_gp_size}
469 @strong{Synopsis}
470 @example
471 unsigned int bfd_get_gp_size(bfd *abfd);
472 @end example
473 @strong{Description}@*
474 Return the maximum size of objects to be optimized using the GP
475 register under MIPS ECOFF.  This is typically set by the @code{-G}
476 argument to the compiler, assembler or linker.
477
478 @findex bfd_set_gp_size
479 @subsubsection @code{bfd_set_gp_size}
480 @strong{Synopsis}
481 @example
482 void bfd_set_gp_size(bfd *abfd, unsigned int i);
483 @end example
484 @strong{Description}@*
485 Set the maximum size of objects to be optimized using the GP
486 register under ECOFF or MIPS ELF.  This is typically set by
487 the @code{-G} argument to the compiler, assembler or linker.
488
489 @findex bfd_scan_vma
490 @subsubsection @code{bfd_scan_vma}
491 @strong{Synopsis}
492 @example
493 bfd_vma bfd_scan_vma(const char *string, const char **end, int base);
494 @end example
495 @strong{Description}@*
496 Convert, like @code{strtoul}, a numerical expression
497 @var{string} into a @code{bfd_vma} integer, and return that integer.
498 (Though without as many bells and whistles as @code{strtoul}.)
499 The expression is assumed to be unsigned (i.e., positive).
500 If given a @var{base}, it is used as the base for conversion.
501 A base of 0 causes the function to interpret the string
502 in hex if a leading "0x" or "0X" is found, otherwise
503 in octal if a leading zero is found, otherwise in decimal.
504
505 Overflow is not detected.
506
507 @findex bfd_copy_private_bfd_data
508 @subsubsection @code{bfd_copy_private_bfd_data}
509 @strong{Synopsis}
510 @example
511 boolean bfd_copy_private_bfd_data(bfd *ibfd, bfd *obfd);
512 @end example
513 @strong{Description}@*
514 Copy private BFD information from the BFD @var{ibfd} to the
515 the BFD @var{obfd}.  Return @code{true} on success, @code{false} on error.
516 Possible error returns are:
517
518 @itemize @bullet
519
520 @item
521 @code{bfd_error_no_memory} -
522 Not enough memory exists to create private data for @var{obfd}.
523 @end itemize
524 @example
525 #define bfd_copy_private_bfd_data(ibfd, obfd) \
526      BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
527                (ibfd, obfd))
528 @end example
529
530 @findex bfd_merge_private_bfd_data
531 @subsubsection @code{bfd_merge_private_bfd_data}
532 @strong{Synopsis}
533 @example
534 boolean bfd_merge_private_bfd_data(bfd *ibfd, bfd *obfd);
535 @end example
536 @strong{Description}@*
537 Merge private BFD information from the BFD @var{ibfd} to the
538 the output file BFD @var{obfd} when linking.  Return @code{true}
539 on success, @code{false} on error.  Possible error returns are:
540
541 @itemize @bullet
542
543 @item
544 @code{bfd_error_no_memory} -
545 Not enough memory exists to create private data for @var{obfd}.
546 @end itemize
547 @example
548 #define bfd_merge_private_bfd_data(ibfd, obfd) \
549      BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
550                (ibfd, obfd))
551 @end example
552
553 @findex bfd_set_private_flags
554 @subsubsection @code{bfd_set_private_flags}
555 @strong{Synopsis}
556 @example
557 boolean bfd_set_private_flags(bfd *abfd, flagword flags);
558 @end example
559 @strong{Description}@*
560 Set private BFD flag information in the BFD @var{abfd}.
561 Return @code{true} on success, @code{false} on error.  Possible error
562 returns are:
563
564 @itemize @bullet
565
566 @item
567 @code{bfd_error_no_memory} -
568 Not enough memory exists to create private data for @var{obfd}.
569 @end itemize
570 @example
571 #define bfd_set_private_flags(abfd, flags) \
572      BFD_SEND (abfd, _bfd_set_private_flags, \
573                (abfd, flags))
574 @end example
575
576 @findex stuff
577 @subsubsection @code{stuff}
578 @strong{Description}@*
579 Stuff which should be documented:
580 @example
581 #define bfd_sizeof_headers(abfd, reloc) \
582      BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
583
584 #define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
585      BFD_SEND (abfd, _bfd_find_nearest_line,  (abfd, sec, syms, off, file, func, line))
586
587        /* Do these three do anything useful at all, for any back end?  */
588 #define bfd_debug_info_start(abfd) \
589         BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
590
591 #define bfd_debug_info_end(abfd) \
592         BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
593
594 #define bfd_debug_info_accumulate(abfd, section) \
595         BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
596
597
598 #define bfd_stat_arch_elt(abfd, stat) \
599         BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
600
601 #define bfd_update_armap_timestamp(abfd) \
602         BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
603
604 #define bfd_set_arch_mach(abfd, arch, mach)\
605         BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
606
607 #define bfd_relax_section(abfd, section, link_info, again) \
608        BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
609
610 #define bfd_gc_sections(abfd, link_info) \
611        BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
612
613 #define bfd_merge_sections(abfd, link_info) \
614        BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
615
616 #define bfd_link_hash_table_create(abfd) \
617        BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
618
619 #define bfd_link_add_symbols(abfd, info) \
620        BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
621
622 #define bfd_final_link(abfd, info) \
623        BFD_SEND (abfd, _bfd_final_link, (abfd, info))
624
625 #define bfd_free_cached_info(abfd) \
626        BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
627
628 #define bfd_get_dynamic_symtab_upper_bound(abfd) \
629        BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
630
631 #define bfd_print_private_bfd_data(abfd, file)\
632        BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
633
634 #define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
635        BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
636
637 #define bfd_get_dynamic_reloc_upper_bound(abfd) \
638        BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
639
640 #define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
641        BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
642
643 extern bfd_byte *bfd_get_relocated_section_contents
644        PARAMS ((bfd *, struct bfd_link_info *,
645                  struct bfd_link_order *, bfd_byte *,
646                  boolean, asymbol **));
647
648 @end example
649
650 @findex bfd_alt_mach_code
651 @subsubsection @code{bfd_alt_mach_code}
652 @strong{Synopsis}
653 @example
654 boolean bfd_alt_mach_code(bfd *abfd, int index);
655 @end example
656 @strong{Description}@*
657 When more than one machine code number is available for the
658 same machine type, this function can be used to switch between
659 the preferred one (index == 0) and any others.  Currently,
660 only ELF supports this feature, with up to two alternate
661 machine codes.
662