1 /* Generic BFD library interface and support routines.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
28 A BFD has type <<bfd>>; objects of this type are the
29 cornerstone of any application using BFD. Using BFD
30 consists of making references though the BFD and to data in the BFD.
32 Here is the structure that defines the type <<bfd>>. It
33 contains the major data about the file and pointers
34 to the rest of the data.
42 . write_direction = 2,
48 . {* A unique identifier of the BFD *}
51 . {* The filename the application opened the BFD with. *}
52 . const char *filename;
54 . {* A pointer to the target jump table. *}
55 . const struct bfd_target *xvec;
57 . {* The IOSTREAM, and corresponding IO vector that provide access
58 . to the file backing the BFD. *}
60 . const struct bfd_iovec *iovec;
62 . {* The caching routines use these to maintain a
63 . least-recently-used list of BFDs. *}
64 . struct bfd *lru_prev, *lru_next;
66 . {* When a file is closed by the caching routines, BFD retains
67 . state information on the file here... *}
70 . {* File modified time, if mtime_set is TRUE. *}
73 . {* Reserved for an unimplemented file locking extension. *}
76 . {* The format which belongs to the BFD. (object, core, etc.) *}
79 . {* The direction with which the BFD was opened. *}
80 . enum bfd_direction direction;
82 . {* Format_specific flags. *}
85 . {* Values that may appear in the flags field of a BFD. These also
86 . appear in the object_flags field of the bfd_target structure, where
87 . they indicate the set of flags used by that backend (not all flags
88 . are meaningful for all object file formats) (FIXME: at the moment,
89 . the object_flags values have mostly just been copied from backend
90 . to another, and are not necessarily correct). *}
92 .#define BFD_NO_FLAGS 0x00
94 . {* BFD contains relocation entries. *}
95 .#define HAS_RELOC 0x01
97 . {* BFD is directly executable. *}
100 . {* BFD has line number information (basically used for F_LNNO in a
102 .#define HAS_LINENO 0x04
104 . {* BFD has debugging information. *}
105 .#define HAS_DEBUG 0x08
107 . {* BFD has symbols. *}
108 .#define HAS_SYMS 0x10
110 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
112 .#define HAS_LOCALS 0x20
114 . {* BFD is a dynamic object. *}
115 .#define DYNAMIC 0x40
117 . {* Text section is write protected (if D_PAGED is not set, this is
118 . like an a.out NMAGIC file) (the linker sets this by default, but
119 . clears it for -r or -N). *}
120 .#define WP_TEXT 0x80
122 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
123 . linker sets this by default, but clears it for -r or -n or -N). *}
124 .#define D_PAGED 0x100
126 . {* BFD is relaxable (this means that bfd_relax_section may be able to
127 . do something) (sometimes bfd_relax_section can do something even if
128 . this is not set). *}
129 .#define BFD_IS_RELAXABLE 0x200
131 . {* This may be set before writing out a BFD to request using a
132 . traditional format. For example, this is used to request that when
133 . writing out an a.out object the symbols not be hashed to eliminate
135 .#define BFD_TRADITIONAL_FORMAT 0x400
137 . {* This flag indicates that the BFD contents are actually cached
138 . in memory. If this is set, iostream points to a bfd_in_memory
140 .#define BFD_IN_MEMORY 0x800
142 . {* The sections in this BFD specify a memory page. *}
143 .#define HAS_LOAD_PAGE 0x1000
145 . {* This BFD has been created by the linker and doesn't correspond
146 . to any input file. *}
147 .#define BFD_LINKER_CREATED 0x2000
149 . {* This may be set before writing out a BFD to request that it
150 . be written using values for UIDs, GIDs, timestamps, etc. that
151 . will be consistent from run to run. *}
152 .#define BFD_DETERMINISTIC_OUTPUT 0x4000
154 . {* Currently my_archive is tested before adding origin to
155 . anything. I believe that this can become always an add of
156 . origin, with origin set to 0 for non archive files. *}
159 . {* The origin in the archive of the proxy entry. This will
160 . normally be the same as origin, except for thin archives,
161 . when it will contain the current offset of the proxy in the
162 . thin archive rather than the offset of the bfd in its actual
164 . ufile_ptr proxy_origin;
166 . {* A hash table for section names. *}
167 . struct bfd_hash_table section_htab;
169 . {* Pointer to linked list of sections. *}
170 . struct bfd_section *sections;
172 . {* The last section on the section list. *}
173 . struct bfd_section *section_last;
175 . {* The number of sections. *}
176 . unsigned int section_count;
178 . {* Stuff only useful for object files:
179 . The start address. *}
180 . bfd_vma start_address;
182 . {* Used for input and output. *}
183 . unsigned int symcount;
185 . {* Symbol table for output BFD (with symcount entries).
186 . Also used by the linker to cache input BFD symbols. *}
187 . struct bfd_symbol **outsymbols;
189 . {* Used for slurped dynamic symbol tables. *}
190 . unsigned int dynsymcount;
192 . {* Pointer to structure which contains architecture information. *}
193 . const struct bfd_arch_info *arch_info;
195 . {* Stuff only useful for archives. *}
197 . struct bfd *my_archive; {* The containing archive BFD. *}
198 . struct bfd *archive_next; {* The next BFD in the archive. *}
199 . struct bfd *archive_head; {* The first BFD in the archive. *}
200 . struct bfd *nested_archives; {* List of nested archive in a flattened
203 . {* A chain of BFD structures involved in a link. *}
204 . struct bfd *link_next;
206 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
207 . be used only for archive elements. *}
210 . {* Used by the back end to hold private data. *}
213 . struct aout_data_struct *aout_data;
214 . struct artdata *aout_ar_data;
215 . struct _oasys_data *oasys_obj_data;
216 . struct _oasys_ar_data *oasys_ar_data;
217 . struct coff_tdata *coff_obj_data;
218 . struct pe_tdata *pe_obj_data;
219 . struct xcoff_tdata *xcoff_obj_data;
220 . struct ecoff_tdata *ecoff_obj_data;
221 . struct ieee_data_struct *ieee_data;
222 . struct ieee_ar_data_struct *ieee_ar_data;
223 . struct srec_data_struct *srec_data;
224 . struct verilog_data_struct *verilog_data;
225 . struct ihex_data_struct *ihex_data;
226 . struct tekhex_data_struct *tekhex_data;
227 . struct elf_obj_tdata *elf_obj_data;
228 . struct nlm_obj_tdata *nlm_obj_data;
229 . struct bout_data_struct *bout_data;
230 . struct mmo_data_struct *mmo_data;
231 . struct sun_core_struct *sun_core_data;
232 . struct sco5_core_struct *sco5_core_data;
233 . struct trad_core_struct *trad_core_data;
234 . struct som_data_struct *som_data;
235 . struct hpux_core_struct *hpux_core_data;
236 . struct hppabsd_core_struct *hppabsd_core_data;
237 . struct sgi_core_struct *sgi_core_data;
238 . struct lynx_core_struct *lynx_core_data;
239 . struct osf_core_struct *osf_core_data;
240 . struct cisco_core_struct *cisco_core_data;
241 . struct versados_data_struct *versados_data;
242 . struct netbsd_core_struct *netbsd_core_data;
243 . struct mach_o_data_struct *mach_o_data;
244 . struct mach_o_fat_data_struct *mach_o_fat_data;
245 . struct plugin_data_struct *plugin_data;
246 . struct bfd_pef_data_struct *pef_data;
247 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
248 . struct bfd_sym_data_struct *sym_data;
253 . {* Used by the application to hold private data. *}
256 . {* Where all the allocated stuff under this BFD goes. This is a
257 . struct objalloc *, but we use void * to avoid requiring the inclusion
261 . {* Is the file descriptor being cached? That is, can it be closed as
262 . needed, and re-opened when accessed later? *}
263 . unsigned int cacheable : 1;
265 . {* Marks whether there was a default target specified when the
266 . BFD was opened. This is used to select which matching algorithm
267 . to use to choose the back end. *}
268 . unsigned int target_defaulted : 1;
270 . {* ... and here: (``once'' means at least once). *}
271 . unsigned int opened_once : 1;
273 . {* Set if we have a locally maintained mtime value, rather than
274 . getting it from the file each time. *}
275 . unsigned int mtime_set : 1;
277 . {* Flag set if symbols from this BFD should not be exported. *}
278 . unsigned int no_export : 1;
280 . {* Remember when output has begun, to stop strange things
282 . unsigned int output_has_begun : 1;
284 . {* Have archive map. *}
285 . unsigned int has_armap : 1;
287 . {* Set if this is a thin archive. *}
288 . unsigned int is_thin_archive : 1;
290 . {* Set if only required symbols should be added in the link hash table for
291 . this object. Used by VMS linkers. *}
292 . unsigned int selective_search : 1;
301 #include "libiberty.h"
302 #include "demangle.h"
303 #include "safe-ctype.h"
306 #include "coff/internal.h"
307 #include "coff/sym.h"
309 #include "libecoff.h"
314 #define EXIT_FAILURE 1
318 /* provide storage for subsystem, stack and heap data which may have been
319 passed in on the command line. Ld puts this data into a bfd_link_info
320 struct which ultimately gets passed in to the bfd. When it arrives, copy
321 it to the following struct so that the data will be available in coffcode.h
322 where it is needed. The typedef's used are defined in bfd.h */
328 Most BFD functions return nonzero on success (check their
329 individual documentation for precise semantics). On an error,
330 they call <<bfd_set_error>> to set an error condition that callers
331 can check by calling <<bfd_get_error>>.
332 If that returns <<bfd_error_system_call>>, then check
335 The easiest way to report a BFD error to the user is to
339 Type <<bfd_error_type>>
341 The values returned by <<bfd_get_error>> are defined by the
342 enumerated type <<bfd_error_type>>.
346 .typedef enum bfd_error
348 . bfd_error_no_error = 0,
349 . bfd_error_system_call,
350 . bfd_error_invalid_target,
351 . bfd_error_wrong_format,
352 . bfd_error_wrong_object_format,
353 . bfd_error_invalid_operation,
354 . bfd_error_no_memory,
355 . bfd_error_no_symbols,
356 . bfd_error_no_armap,
357 . bfd_error_no_more_archived_files,
358 . bfd_error_malformed_archive,
359 . bfd_error_file_not_recognized,
360 . bfd_error_file_ambiguously_recognized,
361 . bfd_error_no_contents,
362 . bfd_error_nonrepresentable_section,
363 . bfd_error_no_debug_section,
364 . bfd_error_bad_value,
365 . bfd_error_file_truncated,
366 . bfd_error_file_too_big,
367 . bfd_error_on_input,
368 . bfd_error_invalid_error_code
374 static bfd_error_type bfd_error
= bfd_error_no_error
;
375 static bfd
*input_bfd
= NULL
;
376 static bfd_error_type input_error
= bfd_error_no_error
;
378 const char *const bfd_errmsgs
[] =
381 N_("System call error"),
382 N_("Invalid bfd target"),
383 N_("File in wrong format"),
384 N_("Archive object file in wrong format"),
385 N_("Invalid operation"),
386 N_("Memory exhausted"),
388 N_("Archive has no index; run ranlib to add one"),
389 N_("No more archived files"),
390 N_("Malformed archive"),
391 N_("File format not recognized"),
392 N_("File format is ambiguous"),
393 N_("Section has no contents"),
394 N_("Nonrepresentable section on output"),
395 N_("Symbol needs debug section which does not exist"),
397 N_("File truncated"),
399 N_("Error reading %s: %s"),
400 N_("#<Invalid error code>")
408 bfd_error_type bfd_get_error (void);
411 Return the current BFD error condition.
425 void bfd_set_error (bfd_error_type error_tag, ...);
428 Set the BFD error condition to be @var{error_tag}.
429 If @var{error_tag} is bfd_error_on_input, then this function
430 takes two more parameters, the input bfd where the error
431 occurred, and the bfd_error_type error.
435 bfd_set_error (bfd_error_type error_tag
, ...)
437 bfd_error
= error_tag
;
438 if (error_tag
== bfd_error_on_input
)
440 /* This is an error that occurred during bfd_close when
441 writing an archive, but on one of the input files. */
444 va_start (ap
, error_tag
);
445 input_bfd
= va_arg (ap
, bfd
*);
446 input_error
= (bfd_error_type
) va_arg (ap
, int);
447 if (input_error
>= bfd_error_on_input
)
458 const char *bfd_errmsg (bfd_error_type error_tag);
461 Return a string describing the error @var{error_tag}, or
462 the system error if @var{error_tag} is <<bfd_error_system_call>>.
466 bfd_errmsg (bfd_error_type error_tag
)
471 if (error_tag
== bfd_error_on_input
)
474 const char *msg
= bfd_errmsg (input_error
);
476 if (asprintf (&buf
, _(bfd_errmsgs
[error_tag
]), input_bfd
->filename
, msg
)
480 /* Ick, what to do on out of memory? */
484 if (error_tag
== bfd_error_system_call
)
485 return xstrerror (errno
);
487 if (error_tag
> bfd_error_invalid_error_code
)
488 error_tag
= bfd_error_invalid_error_code
; /* sanity check */
490 return _(bfd_errmsgs
[error_tag
]);
498 void bfd_perror (const char *message);
501 Print to the standard error stream a string describing the
502 last BFD error that occurred, or the last system error if
503 the last BFD error was a system call failure. If @var{message}
504 is non-NULL and non-empty, the error string printed is preceded
505 by @var{message}, a colon, and a space. It is followed by a newline.
509 bfd_perror (const char *message
)
511 if (message
== NULL
|| *message
== '\0')
512 fprintf (stderr
, "%s\n", bfd_errmsg (bfd_get_error ()));
514 fprintf (stderr
, "%s: %s\n", message
, bfd_errmsg (bfd_get_error ()));
521 Some BFD functions want to print messages describing the
522 problem. They call a BFD error handler function. This
523 function may be overridden by the program.
525 The BFD error handler acts like printf.
529 .typedef void (*bfd_error_handler_type) (const char *, ...);
533 /* The program name used when printing BFD error messages. */
535 static const char *_bfd_error_program_name
;
537 /* This is the default routine to handle BFD error messages.
538 Like fprintf (stderr, ...), but also handles some extra format specifiers.
540 %A section name from section. For group components, print group name too.
541 %B file name from bfd. For archive components, prints archive too.
543 Note - because these two extra format specifiers require special handling
544 they are scanned for and processed in this function, before calling
545 vfprintf. This means that the *arguments* for these format specifiers
546 must be the first ones in the variable argument list, regardless of where
547 the specifiers appear in the format string. Thus for example calling
548 this function with a format string of:
550 "blah %s blah %A blah %d blah %B"
552 would involve passing the arguments as:
554 "blah %s blah %A blah %d blah %B",
562 _bfd_default_error_handler (const char *fmt
, ...)
566 const char *new_fmt
, *p
;
570 /* PR 4992: Don't interrupt output being sent to stdout. */
573 if (_bfd_error_program_name
!= NULL
)
574 fprintf (stderr
, "%s: ", _bfd_error_program_name
);
576 fprintf (stderr
, "BFD: ");
582 /* Reserve enough space for the existing format string. */
583 avail
-= strlen (fmt
) + 1;
585 _exit (EXIT_FAILURE
);
591 size_t len
, extra
, trim
;
594 if (p
== NULL
|| p
[1] == '\0')
599 memcpy (bufp
, fmt
, len
+ 1);
604 if (p
[1] == 'A' || p
[1] == 'B')
607 memcpy (bufp
, fmt
, len
);
612 /* If we run out of space, tough, you lose your ridiculously
613 long file or section name. It's not safe to try to alloc
614 memory here; We might be printing an out of memory message. */
625 bfd
*abfd
= va_arg (ap
, bfd
*);
628 /* Invoking %B with a null bfd pointer is an internal error. */
630 else if (abfd
->my_archive
)
631 snprintf (bufp
, avail
, "%s(%s)",
632 abfd
->my_archive
->filename
, abfd
->filename
);
634 snprintf (bufp
, avail
, "%s", abfd
->filename
);
638 asection
*sec
= va_arg (ap
, asection
*);
640 const char *group
= NULL
;
641 struct coff_comdat_info
*ci
;
644 /* Invoking %A with a null section pointer is an internal error. */
648 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
649 && elf_next_in_group (sec
) != NULL
650 && (sec
->flags
& SEC_GROUP
) == 0)
651 group
= elf_group_name (sec
);
652 else if (abfd
!= NULL
653 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
654 && (ci
= bfd_coff_get_comdat_section (sec
->owner
,
658 snprintf (bufp
, avail
, "%s[%s]", sec
->name
, group
);
660 snprintf (bufp
, avail
, "%s", sec
->name
);
663 avail
= avail
- len
+ 2;
665 /* We need to replace any '%' we printed by "%%".
666 First count how many. */
670 while ((q
= strchr (q
, '%')) != NULL
)
676 /* If there isn't room, trim off the end of the string. */
681 trim
= extra
- avail
;
694 /* Now double all '%' chars, shuffling the string as we go. */
697 while ((q
[extra
] = *q
) != '%')
707 vfprintf (stderr
, new_fmt
, ap
);
713 /* This is a function pointer to the routine which should handle BFD
714 error messages. It is called when a BFD routine encounters an
715 error for which it wants to print a message. Going through a
716 function pointer permits a program linked against BFD to intercept
717 the messages and deal with them itself. */
719 bfd_error_handler_type _bfd_error_handler
= _bfd_default_error_handler
;
723 bfd_set_error_handler
726 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
729 Set the BFD error handler function. Returns the previous
733 bfd_error_handler_type
734 bfd_set_error_handler (bfd_error_handler_type pnew
)
736 bfd_error_handler_type pold
;
738 pold
= _bfd_error_handler
;
739 _bfd_error_handler
= pnew
;
745 bfd_set_error_program_name
748 void bfd_set_error_program_name (const char *);
751 Set the program name to use when printing a BFD error. This
752 is printed before the error message followed by a colon and
753 space. The string must not be changed after it is passed to
758 bfd_set_error_program_name (const char *name
)
760 _bfd_error_program_name
= name
;
765 bfd_get_error_handler
768 bfd_error_handler_type bfd_get_error_handler (void);
771 Return the BFD error handler function.
774 bfd_error_handler_type
775 bfd_get_error_handler (void)
777 return _bfd_error_handler
;
785 Miscellaneous functions
790 bfd_get_reloc_upper_bound
793 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
796 Return the number of bytes required to store the
797 relocation information associated with section @var{sect}
798 attached to bfd @var{abfd}. If an error occurs, return -1.
803 bfd_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
805 if (abfd
->format
!= bfd_object
)
807 bfd_set_error (bfd_error_invalid_operation
);
811 return BFD_SEND (abfd
, _get_reloc_upper_bound
, (abfd
, asect
));
816 bfd_canonicalize_reloc
819 long bfd_canonicalize_reloc
820 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
823 Call the back end associated with the open BFD
824 @var{abfd} and translate the external form of the relocation
825 information attached to @var{sec} into the internal canonical
826 form. Place the table into memory at @var{loc}, which has
827 been preallocated, usually by a call to
828 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
831 The @var{syms} table is also needed for horrible internal magic
836 bfd_canonicalize_reloc (bfd
*abfd
,
841 if (abfd
->format
!= bfd_object
)
843 bfd_set_error (bfd_error_invalid_operation
);
847 return BFD_SEND (abfd
, _bfd_canonicalize_reloc
,
848 (abfd
, asect
, location
, symbols
));
857 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
860 Set the relocation pointer and count within
861 section @var{sec} to the values @var{rel} and @var{count}.
862 The argument @var{abfd} is ignored.
867 bfd_set_reloc (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
872 asect
->orelocation
= location
;
873 asect
->reloc_count
= count
;
881 bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags);
884 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
887 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
888 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
889 o <<bfd_error_invalid_operation>> -
890 The flag word contained a bit which was not applicable to the
891 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
892 on a BFD format which does not support demand paging.
897 bfd_set_file_flags (bfd
*abfd
, flagword flags
)
899 if (abfd
->format
!= bfd_object
)
901 bfd_set_error (bfd_error_wrong_format
);
905 if (bfd_read_p (abfd
))
907 bfd_set_error (bfd_error_invalid_operation
);
911 bfd_get_file_flags (abfd
) = flags
;
912 if ((flags
& bfd_applicable_file_flags (abfd
)) != flags
)
914 bfd_set_error (bfd_error_invalid_operation
);
922 bfd_assert (const char *file
, int line
)
924 (*_bfd_error_handler
) (_("BFD %s assertion fail %s:%d"),
925 BFD_VERSION_STRING
, file
, line
);
928 /* A more or less friendly abort message. In libbfd.h abort is
929 defined to call this function. */
932 _bfd_abort (const char *file
, int line
, const char *fn
)
935 (*_bfd_error_handler
)
936 (_("BFD %s internal error, aborting at %s line %d in %s\n"),
937 BFD_VERSION_STRING
, file
, line
, fn
);
939 (*_bfd_error_handler
)
940 (_("BFD %s internal error, aborting at %s line %d\n"),
941 BFD_VERSION_STRING
, file
, line
);
942 (*_bfd_error_handler
) (_("Please report this bug.\n"));
943 _exit (EXIT_FAILURE
);
951 int bfd_get_arch_size (bfd *abfd);
954 Returns the architecture address size, in bits, as determined
955 by the object file's format. For ELF, this information is
956 included in the header.
959 Returns the arch size in bits if known, <<-1>> otherwise.
963 bfd_get_arch_size (bfd
*abfd
)
965 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
)
966 return get_elf_backend_data (abfd
)->s
->arch_size
;
973 bfd_get_sign_extend_vma
976 int bfd_get_sign_extend_vma (bfd *abfd);
979 Indicates if the target architecture "naturally" sign extends
980 an address. Some architectures implicitly sign extend address
981 values when they are converted to types larger than the size
982 of an address. For instance, bfd_get_start_address() will
983 return an address sign extended to fill a bfd_vma when this is
987 Returns <<1>> if the target architecture is known to sign
988 extend addresses, <<0>> if the target architecture is known to
989 not sign extend addresses, and <<-1>> otherwise.
993 bfd_get_sign_extend_vma (bfd
*abfd
)
997 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
998 return get_elf_backend_data (abfd
)->sign_extend_vma
;
1000 name
= bfd_get_target (abfd
);
1002 /* Return a proper value for DJGPP & PE COFF.
1003 This function is required for DWARF2 support, but there is
1004 no place to store this information in the COFF back end.
1005 Should enough other COFF targets add support for DWARF2,
1006 a place will have to be found. Until then, this hack will do. */
1007 if (CONST_STRNEQ (name
, "coff-go32")
1008 || strcmp (name
, "pe-i386") == 0
1009 || strcmp (name
, "pei-i386") == 0
1010 || strcmp (name
, "pe-x86-64") == 0
1011 || strcmp (name
, "pei-x86-64") == 0
1012 || strcmp (name
, "pe-arm-wince-little") == 0
1013 || strcmp (name
, "pei-arm-wince-little") == 0)
1016 if (CONST_STRNEQ (name
, "mach-o"))
1019 bfd_set_error (bfd_error_wrong_format
);
1025 bfd_set_start_address
1028 bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma);
1031 Make @var{vma} the entry point of output BFD @var{abfd}.
1034 Returns <<TRUE>> on success, <<FALSE>> otherwise.
1038 bfd_set_start_address (bfd
*abfd
, bfd_vma vma
)
1040 abfd
->start_address
= vma
;
1049 unsigned int bfd_get_gp_size (bfd *abfd);
1052 Return the maximum size of objects to be optimized using the GP
1053 register under MIPS ECOFF. This is typically set by the <<-G>>
1054 argument to the compiler, assembler or linker.
1058 bfd_get_gp_size (bfd
*abfd
)
1060 if (abfd
->format
== bfd_object
)
1062 if (abfd
->xvec
->flavour
== bfd_target_ecoff_flavour
)
1063 return ecoff_data (abfd
)->gp_size
;
1064 else if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1065 return elf_gp_size (abfd
);
1075 void bfd_set_gp_size (bfd *abfd, unsigned int i);
1078 Set the maximum size of objects to be optimized using the GP
1079 register under ECOFF or MIPS ELF. This is typically set by
1080 the <<-G>> argument to the compiler, assembler or linker.
1084 bfd_set_gp_size (bfd
*abfd
, unsigned int i
)
1086 /* Don't try to set GP size on an archive or core file! */
1087 if (abfd
->format
!= bfd_object
)
1090 if (abfd
->xvec
->flavour
== bfd_target_ecoff_flavour
)
1091 ecoff_data (abfd
)->gp_size
= i
;
1092 else if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1093 elf_gp_size (abfd
) = i
;
1096 /* Get the GP value. This is an internal function used by some of the
1097 relocation special_function routines on targets which support a GP
1101 _bfd_get_gp_value (bfd
*abfd
)
1105 if (abfd
->format
!= bfd_object
)
1108 if (abfd
->xvec
->flavour
== bfd_target_ecoff_flavour
)
1109 return ecoff_data (abfd
)->gp
;
1110 else if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1111 return elf_gp (abfd
);
1116 /* Set the GP value. */
1119 _bfd_set_gp_value (bfd
*abfd
, bfd_vma v
)
1123 if (abfd
->format
!= bfd_object
)
1126 if (abfd
->xvec
->flavour
== bfd_target_ecoff_flavour
)
1127 ecoff_data (abfd
)->gp
= v
;
1128 else if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1137 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
1140 Convert, like <<strtoul>>, a numerical expression
1141 @var{string} into a <<bfd_vma>> integer, and return that integer.
1142 (Though without as many bells and whistles as <<strtoul>>.)
1143 The expression is assumed to be unsigned (i.e., positive).
1144 If given a @var{base}, it is used as the base for conversion.
1145 A base of 0 causes the function to interpret the string
1146 in hex if a leading "0x" or "0X" is found, otherwise
1147 in octal if a leading zero is found, otherwise in decimal.
1149 If the value would overflow, the maximum <<bfd_vma>> value is
1154 bfd_scan_vma (const char *string
, const char **end
, int base
)
1158 unsigned int cutlim
;
1161 /* Let the host do it if possible. */
1162 if (sizeof (bfd_vma
) <= sizeof (unsigned long))
1163 return strtoul (string
, (char **) end
, base
);
1165 #ifdef HAVE_STRTOULL
1166 if (sizeof (bfd_vma
) <= sizeof (unsigned long long))
1167 return strtoull (string
, (char **) end
, base
);
1172 if (string
[0] == '0')
1174 if ((string
[1] == 'x') || (string
[1] == 'X'))
1181 if ((base
< 2) || (base
> 36))
1186 && (string
[1] == 'x' || string
[1] == 'X')
1187 && ISXDIGIT (string
[2]))
1192 cutoff
= (~ (bfd_vma
) 0) / (bfd_vma
) base
;
1193 cutlim
= (~ (bfd_vma
) 0) % (bfd_vma
) base
;
1201 if (ISDIGIT (digit
))
1202 digit
= digit
- '0';
1203 else if (ISALPHA (digit
))
1204 digit
= TOUPPER (digit
) - 'A' + 10;
1207 if (digit
>= (unsigned int) base
)
1209 if (value
> cutoff
|| (value
== cutoff
&& digit
> cutlim
))
1211 value
= value
* base
+ digit
;
1216 value
= ~ (bfd_vma
) 0;
1226 bfd_copy_private_header_data
1229 bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
1232 Copy private BFD header information from the BFD @var{ibfd} to the
1233 the BFD @var{obfd}. This copies information that may require
1234 sections to exist, but does not require symbol tables. Return
1235 <<true>> on success, <<false>> on error.
1236 Possible error returns are:
1238 o <<bfd_error_no_memory>> -
1239 Not enough memory exists to create private data for @var{obfd}.
1241 .#define bfd_copy_private_header_data(ibfd, obfd) \
1242 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
1249 bfd_copy_private_bfd_data
1252 bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
1255 Copy private BFD information from the BFD @var{ibfd} to the
1256 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
1257 Possible error returns are:
1259 o <<bfd_error_no_memory>> -
1260 Not enough memory exists to create private data for @var{obfd}.
1262 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
1263 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
1270 bfd_merge_private_bfd_data
1273 bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd);
1276 Merge private BFD information from the BFD @var{ibfd} to the
1277 the output file BFD @var{obfd} when linking. Return <<TRUE>>
1278 on success, <<FALSE>> on error. Possible error returns are:
1280 o <<bfd_error_no_memory>> -
1281 Not enough memory exists to create private data for @var{obfd}.
1283 .#define bfd_merge_private_bfd_data(ibfd, obfd) \
1284 . BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
1291 bfd_set_private_flags
1294 bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags);
1297 Set private BFD flag information in the BFD @var{abfd}.
1298 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
1301 o <<bfd_error_no_memory>> -
1302 Not enough memory exists to create private data for @var{obfd}.
1304 .#define bfd_set_private_flags(abfd, flags) \
1305 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
1314 The following functions exist but have not yet been documented.
1316 .#define bfd_sizeof_headers(abfd, info) \
1317 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
1319 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
1320 . BFD_SEND (abfd, _bfd_find_nearest_line, \
1321 . (abfd, sec, syms, off, file, func, line))
1323 .#define bfd_find_line(abfd, syms, sym, file, line) \
1324 . BFD_SEND (abfd, _bfd_find_line, \
1325 . (abfd, syms, sym, file, line))
1327 .#define bfd_find_inliner_info(abfd, file, func, line) \
1328 . BFD_SEND (abfd, _bfd_find_inliner_info, \
1329 . (abfd, file, func, line))
1331 .#define bfd_debug_info_start(abfd) \
1332 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1334 .#define bfd_debug_info_end(abfd) \
1335 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1337 .#define bfd_debug_info_accumulate(abfd, section) \
1338 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1340 .#define bfd_stat_arch_elt(abfd, stat) \
1341 . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1343 .#define bfd_update_armap_timestamp(abfd) \
1344 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1346 .#define bfd_set_arch_mach(abfd, arch, mach)\
1347 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1349 .#define bfd_relax_section(abfd, section, link_info, again) \
1350 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
1352 .#define bfd_gc_sections(abfd, link_info) \
1353 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
1355 .#define bfd_merge_sections(abfd, link_info) \
1356 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
1358 .#define bfd_is_group_section(abfd, sec) \
1359 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
1361 .#define bfd_discard_group(abfd, sec) \
1362 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
1364 .#define bfd_link_hash_table_create(abfd) \
1365 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1367 .#define bfd_link_hash_table_free(abfd, hash) \
1368 . BFD_SEND (abfd, _bfd_link_hash_table_free, (hash))
1370 .#define bfd_link_add_symbols(abfd, info) \
1371 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1373 .#define bfd_link_just_syms(abfd, sec, info) \
1374 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
1376 .#define bfd_final_link(abfd, info) \
1377 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
1379 .#define bfd_free_cached_info(abfd) \
1380 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1382 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1383 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1385 .#define bfd_print_private_bfd_data(abfd, file)\
1386 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1388 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1389 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1391 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
1392 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
1393 . dyncount, dynsyms, ret))
1395 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1396 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1398 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1399 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1401 .extern bfd_byte *bfd_get_relocated_section_contents
1402 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
1403 . bfd_boolean, asymbol **);
1409 bfd_get_relocated_section_contents (bfd
*abfd
,
1410 struct bfd_link_info
*link_info
,
1411 struct bfd_link_order
*link_order
,
1413 bfd_boolean relocatable
,
1417 bfd_byte
*(*fn
) (bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
1418 bfd_byte
*, bfd_boolean
, asymbol
**);
1420 if (link_order
->type
== bfd_indirect_link_order
)
1422 abfd2
= link_order
->u
.indirect
.section
->owner
;
1429 fn
= abfd2
->xvec
->_bfd_get_relocated_section_contents
;
1431 return (*fn
) (abfd
, link_info
, link_order
, data
, relocatable
, symbols
);
1434 /* Record information about an ELF program header. */
1437 bfd_record_phdr (bfd
*abfd
,
1439 bfd_boolean flags_valid
,
1441 bfd_boolean at_valid
,
1443 bfd_boolean includes_filehdr
,
1444 bfd_boolean includes_phdrs
,
1448 struct elf_segment_map
*m
, **pm
;
1451 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
1454 amt
= sizeof (struct elf_segment_map
);
1455 amt
+= ((bfd_size_type
) count
- 1) * sizeof (asection
*);
1456 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
1463 m
->p_flags_valid
= flags_valid
;
1464 m
->p_paddr_valid
= at_valid
;
1465 m
->includes_filehdr
= includes_filehdr
;
1466 m
->includes_phdrs
= includes_phdrs
;
1469 memcpy (m
->sections
, secs
, count
* sizeof (asection
*));
1471 for (pm
= &elf_tdata (abfd
)->segment_map
; *pm
!= NULL
; pm
= &(*pm
)->next
)
1479 /* Return true iff this target is 32-bit. */
1484 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1486 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1487 return bed
->s
->elfclass
== ELFCLASS32
;
1490 /* For non-ELF targets, use architecture information. */
1491 return bfd_arch_bits_per_address (abfd
) <= 32;
1495 /* bfd_sprintf_vma and bfd_fprintf_vma display an address in the
1496 target's address size. */
1499 bfd_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
1504 sprintf (buf
, "%08lx", (unsigned long) value
& 0xffffffff);
1508 sprintf_vma (buf
, value
);
1512 bfd_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
1517 fprintf ((FILE *) stream
, "%08lx", (unsigned long) value
& 0xffffffff);
1521 fprintf_vma ((FILE *) stream
, value
);
1529 bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative);
1533 When more than one machine code number is available for the
1534 same machine type, this function can be used to switch between
1535 the preferred one (alternative == 0) and any others. Currently,
1536 only ELF supports this feature, with up to two alternate
1541 bfd_alt_mach_code (bfd
*abfd
, int alternative
)
1543 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1547 switch (alternative
)
1550 code
= get_elf_backend_data (abfd
)->elf_machine_code
;
1554 code
= get_elf_backend_data (abfd
)->elf_machine_alt1
;
1560 code
= get_elf_backend_data (abfd
)->elf_machine_alt2
;
1569 elf_elfheader (abfd
)->e_machine
= code
;
1580 .struct bfd_preserve
1585 . const struct bfd_arch_info *arch_info;
1586 . struct bfd_section *sections;
1587 . struct bfd_section *section_last;
1588 . unsigned int section_count;
1589 . struct bfd_hash_table section_htab;
1599 bfd_boolean bfd_preserve_save (bfd *, struct bfd_preserve *);
1602 When testing an object for compatibility with a particular
1603 target back-end, the back-end object_p function needs to set
1604 up certain fields in the bfd on successfully recognizing the
1605 object. This typically happens in a piecemeal fashion, with
1606 failures possible at many points. On failure, the bfd is
1607 supposed to be restored to its initial state, which is
1608 virtually impossible. However, restoring a subset of the bfd
1609 state works in practice. This function stores the subset and
1610 reinitializes the bfd.
1615 bfd_preserve_save (bfd
*abfd
, struct bfd_preserve
*preserve
)
1617 preserve
->tdata
= abfd
->tdata
.any
;
1618 preserve
->arch_info
= abfd
->arch_info
;
1619 preserve
->flags
= abfd
->flags
;
1620 preserve
->sections
= abfd
->sections
;
1621 preserve
->section_last
= abfd
->section_last
;
1622 preserve
->section_count
= abfd
->section_count
;
1623 preserve
->section_htab
= abfd
->section_htab
;
1625 if (! bfd_hash_table_init (&abfd
->section_htab
, bfd_section_hash_newfunc
,
1626 sizeof (struct section_hash_entry
)))
1629 abfd
->tdata
.any
= NULL
;
1630 abfd
->arch_info
= &bfd_default_arch_struct
;
1631 abfd
->flags
&= BFD_IN_MEMORY
;
1632 abfd
->sections
= NULL
;
1633 abfd
->section_last
= NULL
;
1634 abfd
->section_count
= 0;
1641 bfd_preserve_restore
1644 void bfd_preserve_restore (bfd *, struct bfd_preserve *);
1647 This function restores bfd state saved by bfd_preserve_save.
1648 If MARKER is non-NULL in struct bfd_preserve then that block
1649 and all subsequently bfd_alloc'd memory is freed.
1654 bfd_preserve_restore (bfd
*abfd
, struct bfd_preserve
*preserve
)
1656 bfd_hash_table_free (&abfd
->section_htab
);
1658 abfd
->tdata
.any
= preserve
->tdata
;
1659 abfd
->arch_info
= preserve
->arch_info
;
1660 abfd
->flags
= preserve
->flags
;
1661 abfd
->section_htab
= preserve
->section_htab
;
1662 abfd
->sections
= preserve
->sections
;
1663 abfd
->section_last
= preserve
->section_last
;
1664 abfd
->section_count
= preserve
->section_count
;
1666 /* bfd_release frees all memory more recently bfd_alloc'd than
1667 its arg, as well as its arg. */
1668 if (preserve
->marker
!= NULL
)
1670 bfd_release (abfd
, preserve
->marker
);
1671 preserve
->marker
= NULL
;
1680 void bfd_preserve_finish (bfd *, struct bfd_preserve *);
1683 This function should be called when the bfd state saved by
1684 bfd_preserve_save is no longer needed. ie. when the back-end
1685 object_p function returns with success.
1690 bfd_preserve_finish (bfd
*abfd ATTRIBUTE_UNUSED
, struct bfd_preserve
*preserve
)
1692 /* It would be nice to be able to free more memory here, eg. old
1693 tdata, but that's not possible since these blocks are sitting
1694 inside bfd_alloc'd memory. The section hash is on a separate
1696 bfd_hash_table_free (&preserve
->section_htab
);
1701 bfd_emul_get_maxpagesize
1704 bfd_vma bfd_emul_get_maxpagesize (const char *);
1707 Returns the maximum page size, in bytes, as determined by
1711 Returns the maximum page size in bytes for ELF, 0 otherwise.
1715 bfd_emul_get_maxpagesize (const char *emul
)
1717 const bfd_target
*target
;
1719 target
= bfd_find_target (emul
, NULL
);
1721 && target
->flavour
== bfd_target_elf_flavour
)
1722 return xvec_get_elf_backend_data (target
)->maxpagesize
;
1728 bfd_elf_set_pagesize (const bfd_target
*target
, bfd_vma size
,
1729 int offset
, const bfd_target
*orig_target
)
1731 if (target
->flavour
== bfd_target_elf_flavour
)
1733 const struct elf_backend_data
*bed
;
1735 bed
= xvec_get_elf_backend_data (target
);
1736 *((bfd_vma
*) ((char *) bed
+ offset
)) = size
;
1739 if (target
->alternative_target
1740 && target
->alternative_target
!= orig_target
)
1741 bfd_elf_set_pagesize (target
->alternative_target
, size
, offset
,
1747 bfd_emul_set_maxpagesize
1750 void bfd_emul_set_maxpagesize (const char *, bfd_vma);
1753 For ELF, set the maximum page size for the emulation. It is
1754 a no-op for other formats.
1759 bfd_emul_set_maxpagesize (const char *emul
, bfd_vma size
)
1761 const bfd_target
*target
;
1763 target
= bfd_find_target (emul
, NULL
);
1765 bfd_elf_set_pagesize (target
, size
,
1766 offsetof (struct elf_backend_data
,
1767 maxpagesize
), target
);
1772 bfd_emul_get_commonpagesize
1775 bfd_vma bfd_emul_get_commonpagesize (const char *);
1778 Returns the common page size, in bytes, as determined by
1782 Returns the common page size in bytes for ELF, 0 otherwise.
1786 bfd_emul_get_commonpagesize (const char *emul
)
1788 const bfd_target
*target
;
1790 target
= bfd_find_target (emul
, NULL
);
1792 && target
->flavour
== bfd_target_elf_flavour
)
1793 return xvec_get_elf_backend_data (target
)->commonpagesize
;
1800 bfd_emul_set_commonpagesize
1803 void bfd_emul_set_commonpagesize (const char *, bfd_vma);
1806 For ELF, set the common page size for the emulation. It is
1807 a no-op for other formats.
1812 bfd_emul_set_commonpagesize (const char *emul
, bfd_vma size
)
1814 const bfd_target
*target
;
1816 target
= bfd_find_target (emul
, NULL
);
1818 bfd_elf_set_pagesize (target
, size
,
1819 offsetof (struct elf_backend_data
,
1820 commonpagesize
), target
);
1828 char *bfd_demangle (bfd *, const char *, int);
1831 Wrapper around cplus_demangle. Strips leading underscores and
1832 other such chars that would otherwise confuse the demangler.
1833 If passed a g++ v3 ABI mangled name, returns a buffer allocated
1834 with malloc holding the demangled name. Returns NULL otherwise
1835 and on memory alloc failure.
1839 bfd_demangle (bfd
*abfd
, const char *name
, int options
)
1842 const char *pre
, *suf
;
1844 bfd_boolean skip_lead
;
1846 skip_lead
= (abfd
!= NULL
1848 && bfd_get_symbol_leading_char (abfd
) == *name
);
1852 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
1853 or the MS PE format. These formats have a number of leading '.'s
1854 on at least some symbols, so we remove all dots to avoid
1855 confusing the demangler. */
1857 while (*name
== '.' || *name
== '$')
1859 pre_len
= name
- pre
;
1861 /* Strip off @plt and suchlike too. */
1863 suf
= strchr (name
, '@');
1866 alloc
= (char *) bfd_malloc (suf
- name
+ 1);
1869 memcpy (alloc
, name
, suf
- name
);
1870 alloc
[suf
- name
] = '\0';
1874 res
= cplus_demangle (name
, options
);
1883 size_t len
= strlen (pre
) + 1;
1884 alloc
= (char *) bfd_malloc (len
);
1887 memcpy (alloc
, pre
, len
);
1893 /* Put back any prefix or suffix. */
1894 if (pre_len
!= 0 || suf
!= NULL
)
1903 suf_len
= strlen (suf
) + 1;
1904 final
= (char *) bfd_malloc (pre_len
+ len
+ suf_len
);
1907 memcpy (final
, pre
, pre_len
);
1908 memcpy (final
+ pre_len
, res
, len
);
1909 memcpy (final
+ pre_len
+ len
, suf
, suf_len
);