Automatic date update in version.in
[binutils-gdb.git] / bfd / bfd.c
blob8fd86f68e6e4fc76d5b4496a304d478150d35c71
1 /* Generic BFD library interface and support routines.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 INODE
24 typedef bfd, Error reporting, BFD front end, BFD front end
26 SECTION
27 <<typedef bfd>>
29 A BFD has type <<bfd>>; objects of this type are the
30 cornerstone of any application using BFD. Using BFD
31 consists of making references though the BFD and to data in the BFD.
33 Here is the structure that defines the type <<bfd>>. It
34 contains the major data about the file and pointers
35 to the rest of the data.
37 EXTERNAL
38 .typedef enum bfd_format
39 . {
40 . bfd_unknown = 0, {* File format is unknown. *}
41 . bfd_object, {* Linker/assembler/compiler output. *}
42 . bfd_archive, {* Object archive file. *}
43 . bfd_core, {* Core dump. *}
44 . bfd_type_end {* Marks the end; don't use it! *}
45 . }
46 .bfd_format;
48 .enum bfd_direction
49 . {
50 . no_direction = 0,
51 . read_direction = 1,
52 . write_direction = 2,
53 . both_direction = 3
54 . };
56 .enum bfd_last_io
57 . {
58 . bfd_io_seek = 0,
59 . bfd_io_read = 1,
60 . bfd_io_write = 2,
61 . bfd_io_force = 3
62 . };
64 .enum bfd_plugin_format
65 . {
66 . bfd_plugin_unknown = 0,
67 . bfd_plugin_yes = 1,
68 . bfd_plugin_no = 2
69 . };
71 .struct bfd_build_id
72 . {
73 . bfd_size_type size;
74 . bfd_byte data[1];
75 . };
77 .enum bfd_lto_object_type
78 . {
79 . lto_non_object, {* Not an LTO object. *}
80 . lto_non_ir_object, {* An object without LTO IR. *}
81 . lto_slim_ir_object, {* A slim LTO IR object. *}
82 . lto_fat_ir_object {* A fat LTO IR object. *}
83 . };
85 .struct bfd_mmapped_entry
86 . {
87 . void *addr;
88 . size_t size;
89 . };
91 .struct bfd_mmapped
92 . {
93 . struct bfd_mmapped *next;
94 . unsigned int max_entry;
95 . unsigned int next_entry;
96 . struct bfd_mmapped_entry entries[1];
97 . };
100 CODE_FRAGMENT
101 .struct bfd
103 . {* The filename the application opened the BFD with. *}
104 . const char *filename;
106 . {* A pointer to the target jump table. *}
107 . const struct bfd_target *xvec;
109 . {* The IOSTREAM, and corresponding IO vector that provide access
110 . to the file backing the BFD. *}
111 . void *iostream;
112 . const struct bfd_iovec *iovec;
114 . {* The caching routines use these to maintain a
115 . least-recently-used list of BFDs. *}
116 . struct bfd *lru_prev, *lru_next;
118 . {* Track current file position (or current buffer offset for
119 . in-memory BFDs). When a file is closed by the caching routines,
120 . BFD retains state information on the file here. *}
121 . ufile_ptr where;
123 . {* File modified time, if mtime_set is TRUE. *}
124 . long mtime;
126 . {* A unique identifier of the BFD *}
127 . unsigned int id;
129 . {* Format_specific flags. *}
130 . flagword flags;
132 . {* Values that may appear in the flags field of a BFD. These also
133 . appear in the object_flags field of the bfd_target structure, where
134 . they indicate the set of flags used by that backend (not all flags
135 . are meaningful for all object file formats) (FIXME: at the moment,
136 . the object_flags values have mostly just been copied from backend
137 . to another, and are not necessarily correct). *}
139 .#define BFD_NO_FLAGS 0x0
141 . {* BFD contains relocation entries. *}
142 .#define HAS_RELOC 0x1
144 . {* BFD is directly executable. *}
145 .#define EXEC_P 0x2
147 . {* BFD has line number information (basically used for F_LNNO in a
148 . COFF header). *}
149 .#define HAS_LINENO 0x4
151 . {* BFD has debugging information. *}
152 .#define HAS_DEBUG 0x08
154 . {* BFD has symbols. *}
155 .#define HAS_SYMS 0x10
157 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
158 . header). *}
159 .#define HAS_LOCALS 0x20
161 . {* BFD is a dynamic object. *}
162 .#define DYNAMIC 0x40
164 . {* Text section is write protected (if D_PAGED is not set, this is
165 . like an a.out NMAGIC file) (the linker sets this by default, but
166 . clears it for -r or -N). *}
167 .#define WP_TEXT 0x80
169 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
170 . linker sets this by default, but clears it for -r or -n or -N). *}
171 .#define D_PAGED 0x100
173 . {* BFD is relaxable (this means that bfd_relax_section may be able to
174 . do something) (sometimes bfd_relax_section can do something even if
175 . this is not set). *}
176 .#define BFD_IS_RELAXABLE 0x200
178 . {* This may be set before writing out a BFD to request using a
179 . traditional format. For example, this is used to request that when
180 . writing out an a.out object the symbols not be hashed to eliminate
181 . duplicates. *}
182 .#define BFD_TRADITIONAL_FORMAT 0x400
184 . {* This flag indicates that the BFD contents are actually cached
185 . in memory. If this is set, iostream points to a malloc'd
186 . bfd_in_memory struct. *}
187 .#define BFD_IN_MEMORY 0x800
189 . {* This BFD has been created by the linker and doesn't correspond
190 . to any input file. *}
191 .#define BFD_LINKER_CREATED 0x1000
193 . {* This may be set before writing out a BFD to request that it
194 . be written using values for UIDs, GIDs, timestamps, etc. that
195 . will be consistent from run to run. *}
196 .#define BFD_DETERMINISTIC_OUTPUT 0x2000
198 . {* Compress sections in this BFD. *}
199 .#define BFD_COMPRESS 0x4000
201 . {* Decompress sections in this BFD. *}
202 .#define BFD_DECOMPRESS 0x8000
204 . {* BFD is a dummy, for plugins. *}
205 .#define BFD_PLUGIN 0x10000
207 . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
208 .#define BFD_COMPRESS_GABI 0x20000
210 . {* Convert ELF common symbol type to STT_COMMON or STT_OBJECT in this
211 . BFD. *}
212 .#define BFD_CONVERT_ELF_COMMON 0x40000
214 . {* Use the ELF STT_COMMON type in this BFD. *}
215 .#define BFD_USE_ELF_STT_COMMON 0x80000
217 . {* Put pathnames into archives (non-POSIX). *}
218 .#define BFD_ARCHIVE_FULL_PATH 0x100000
220 .#define BFD_CLOSED_BY_CACHE 0x200000
222 . {* Compress sections in this BFD with SHF_COMPRESSED zstd. *}
223 .#define BFD_COMPRESS_ZSTD 0x400000
225 . {* Don't generate ELF section header. *}
226 .#define BFD_NO_SECTION_HEADER 0x800000
228 . {* Flags bits which are for BFD use only. *}
229 .#define BFD_FLAGS_FOR_BFD_USE_MASK \
230 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
231 . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
232 . | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON \
233 . | BFD_NO_SECTION_HEADER)
235 . {* The format which belongs to the BFD. (object, core, etc.) *}
236 . ENUM_BITFIELD (bfd_format) format : 3;
238 . {* The direction with which the BFD was opened. *}
239 . ENUM_BITFIELD (bfd_direction) direction : 2;
241 . {* POSIX.1-2017 (IEEE Std 1003.1) says of fopen : "When a file is
242 . opened with update mode ('+' as the second or third character in
243 . the mode argument), both input and output may be performed on
244 . the associated stream. However, the application shall ensure
245 . that output is not directly followed by input without an
246 . intervening call to fflush() or to a file positioning function
247 . (fseek(), fsetpos(), or rewind()), and input is not directly
248 . followed by output without an intervening call to a file
249 . positioning function, unless the input operation encounters
250 . end-of-file."
251 . This field tracks the last IO operation, so that bfd can insert
252 . a seek when IO direction changes. *}
253 . ENUM_BITFIELD (bfd_last_io) last_io : 2;
255 . {* Is the file descriptor being cached? That is, can it be closed as
256 . needed, and re-opened when accessed later? *}
257 . unsigned int cacheable : 1;
259 . {* Marks whether there was a default target specified when the
260 . BFD was opened. This is used to select which matching algorithm
261 . to use to choose the back end. *}
262 . unsigned int target_defaulted : 1;
264 . {* ... and here: (``once'' means at least once). *}
265 . unsigned int opened_once : 1;
267 . {* Set if we have a locally maintained mtime value, rather than
268 . getting it from the file each time. *}
269 . unsigned int mtime_set : 1;
271 . {* Flag set if symbols from this BFD should not be exported. *}
272 . unsigned int no_export : 1;
274 . {* Remember when output has begun, to stop strange things
275 . from happening. *}
276 . unsigned int output_has_begun : 1;
278 . {* Have archive map. *}
279 . unsigned int has_armap : 1;
281 . {* Set if this is a thin archive. *}
282 . unsigned int is_thin_archive : 1;
284 . {* Set if this archive should not cache element positions. *}
285 . unsigned int no_element_cache : 1;
287 . {* Set if only required symbols should be added in the link hash table for
288 . this object. Used by VMS linkers. *}
289 . unsigned int selective_search : 1;
291 . {* Set if this is the linker output BFD. *}
292 . unsigned int is_linker_output : 1;
294 . {* Set if this is the linker input BFD. *}
295 . unsigned int is_linker_input : 1;
297 . {* If this is an input for a compiler plug-in library. *}
298 . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
300 . {* Set if this is a plugin output file. *}
301 . unsigned int lto_output : 1;
303 . {* Do not attempt to modify this file. Set when detecting errors
304 . that BFD is not prepared to handle for objcopy/strip. *}
305 . unsigned int read_only : 1;
307 . {* LTO object type. *}
308 . ENUM_BITFIELD (bfd_lto_object_type) lto_type : 2;
310 . {* Set to dummy BFD created when claimed by a compiler plug-in
311 . library. *}
312 . bfd *plugin_dummy_bfd;
314 . {* The offset of this bfd in the file, typically 0 if it is not
315 . contained in an archive. *}
316 . ufile_ptr origin;
318 . {* The origin in the archive of the proxy entry. This will
319 . normally be the same as origin, except for thin archives,
320 . when it will contain the current offset of the proxy in the
321 . thin archive rather than the offset of the bfd in its actual
322 . container. *}
323 . ufile_ptr proxy_origin;
325 . {* A hash table for section names. *}
326 . struct bfd_hash_table section_htab;
328 . {* Pointer to linked list of sections. *}
329 . struct bfd_section *sections;
331 . {* The last section on the section list. *}
332 . struct bfd_section *section_last;
334 . {* The number of sections. *}
335 . unsigned int section_count;
337 . {* The archive plugin file descriptor. *}
338 . int archive_plugin_fd;
340 . {* The number of opens on the archive plugin file descriptor. *}
341 . unsigned int archive_plugin_fd_open_count;
343 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
344 . be used only for archive elements. *}
345 . int archive_pass;
347 . {* The total size of memory from bfd_alloc. *}
348 . bfd_size_type alloc_size;
350 . {* Stuff only useful for object files:
351 . The start address. *}
352 . bfd_vma start_address;
354 . {* Symbol table for output BFD (with symcount entries).
355 . Also used by the linker to cache input BFD symbols. *}
356 . struct bfd_symbol **outsymbols;
358 . {* Used for input and output. *}
359 . unsigned int symcount;
361 . {* Used for slurped dynamic symbol tables. *}
362 . unsigned int dynsymcount;
364 . {* Pointer to structure which contains architecture information. *}
365 . const struct bfd_arch_info *arch_info;
367 . {* Cached length of file for bfd_get_size. 0 until bfd_get_size is
368 . called, 1 if stat returns an error or the file size is too large to
369 . return in ufile_ptr. Both 0 and 1 should be treated as "unknown". *}
370 . ufile_ptr size;
372 . {* Stuff only useful for archives. *}
373 . void *arelt_data;
374 . struct bfd *my_archive; {* The containing archive BFD. *}
375 . struct bfd *archive_next; {* The next BFD in the archive. *}
376 . struct bfd *archive_head; {* The first BFD in the archive. *}
377 . struct bfd *nested_archives; {* List of nested archive in a flattened
378 . thin archive. *}
380 . union {
381 . {* For input BFDs, a chain of BFDs involved in a link. *}
382 . struct bfd *next;
383 . {* For output BFD, the linker hash table. *}
384 . struct bfd_link_hash_table *hash;
385 . } link;
387 . {* Used by the back end to hold private data. *}
388 . union
390 . struct aout_data_struct *aout_data;
391 . struct artdata *aout_ar_data;
392 . struct coff_tdata *coff_obj_data;
393 . struct pe_tdata *pe_obj_data;
394 . struct xcoff_tdata *xcoff_obj_data;
395 . struct ecoff_tdata *ecoff_obj_data;
396 . struct srec_data_struct *srec_data;
397 . struct verilog_data_struct *verilog_data;
398 . struct ihex_data_struct *ihex_data;
399 . struct tekhex_data_struct *tekhex_data;
400 . struct elf_obj_tdata *elf_obj_data;
401 . struct mmo_data_struct *mmo_data;
402 . struct trad_core_struct *trad_core_data;
403 . struct som_data_struct *som_data;
404 . struct hpux_core_struct *hpux_core_data;
405 . struct hppabsd_core_struct *hppabsd_core_data;
406 . struct sgi_core_struct *sgi_core_data;
407 . struct lynx_core_struct *lynx_core_data;
408 . struct osf_core_struct *osf_core_data;
409 . struct cisco_core_struct *cisco_core_data;
410 . struct netbsd_core_struct *netbsd_core_data;
411 . struct mach_o_data_struct *mach_o_data;
412 . struct mach_o_fat_data_struct *mach_o_fat_data;
413 . struct plugin_data_struct *plugin_data;
414 . struct bfd_pef_data_struct *pef_data;
415 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
416 . struct bfd_sym_data_struct *sym_data;
417 . void *any;
419 . tdata;
421 . {* Used by the application to hold private data. *}
422 . void *usrdata;
424 . {* Where all the allocated stuff under this BFD goes. This is a
425 . struct objalloc *, but we use void * to avoid requiring the inclusion
426 . of objalloc.h. *}
427 . void *memory;
429 . {* For input BFDs, the build ID, if the object has one. *}
430 . const struct bfd_build_id *build_id;
432 . {* For input BFDs, mmapped entries. *}
433 . struct bfd_mmapped *mmapped;
437 EXTERNAL
438 .static inline const char *
439 .bfd_get_filename (const bfd *abfd)
441 . return abfd->filename;
444 .static inline bool
445 .bfd_get_cacheable (const bfd *abfd)
447 . return abfd->cacheable;
450 .static inline enum bfd_format
451 .bfd_get_format (const bfd *abfd)
453 . return abfd->format;
456 .static inline enum bfd_lto_object_type
457 .bfd_get_lto_type (const bfd *abfd)
459 . return abfd->lto_type;
462 .static inline flagword
463 .bfd_get_file_flags (const bfd *abfd)
465 . return abfd->flags;
468 .static inline bfd_vma
469 .bfd_get_start_address (const bfd *abfd)
471 . return abfd->start_address;
474 .static inline unsigned int
475 .bfd_get_symcount (const bfd *abfd)
477 . return abfd->symcount;
480 .static inline unsigned int
481 .bfd_get_dynamic_symcount (const bfd *abfd)
483 . return abfd->dynsymcount;
486 .static inline struct bfd_symbol **
487 .bfd_get_outsymbols (const bfd *abfd)
489 . return abfd->outsymbols;
492 .static inline unsigned int
493 .bfd_count_sections (const bfd *abfd)
495 . return abfd->section_count;
498 .static inline bool
499 .bfd_has_map (const bfd *abfd)
501 . return abfd->has_armap;
504 .static inline bool
505 .bfd_is_thin_archive (const bfd *abfd)
507 . return abfd->is_thin_archive;
510 .static inline void *
511 .bfd_usrdata (const bfd *abfd)
513 . return abfd->usrdata;
516 .{* See note beside bfd_set_section_userdata. *}
517 .static inline bool
518 .bfd_set_cacheable (bfd * abfd, bool val)
520 . abfd->cacheable = val;
521 . return true;
524 .static inline void
525 .bfd_set_thin_archive (bfd *abfd, bool val)
527 . abfd->is_thin_archive = val;
530 .static inline void
531 .bfd_set_usrdata (bfd *abfd, void *val)
533 . abfd->usrdata = val;
536 .static inline asection *
537 .bfd_asymbol_section (const asymbol *sy)
539 . return sy->section;
542 .static inline bfd_vma
543 .bfd_asymbol_value (const asymbol *sy)
545 . return sy->section->vma + sy->value;
548 .static inline const char *
549 .bfd_asymbol_name (const asymbol *sy)
551 . return sy->name;
554 .static inline struct bfd *
555 .bfd_asymbol_bfd (const asymbol *sy)
557 . return sy->the_bfd;
560 .static inline void
561 .bfd_set_asymbol_name (asymbol *sy, const char *name)
563 . sy->name = name;
566 .{* For input sections return the original size on disk of the
567 . section. For output sections return the current size. *}
568 .static inline bfd_size_type
569 .bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
571 . if (abfd->direction != write_direction && sec->rawsize != 0)
572 . return sec->rawsize;
573 . return sec->size;
576 .{* Find the address one past the end of SEC. *}
577 .static inline bfd_size_type
578 .bfd_get_section_limit (const bfd *abfd, const asection *sec)
580 . return (bfd_get_section_limit_octets (abfd, sec)
581 . / bfd_octets_per_byte (abfd, sec));
584 .{* For input sections return the larger of the current size and the
585 . original size on disk of the section. For output sections return
586 . the current size. *}
587 .static inline bfd_size_type
588 .bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
590 . if (abfd->direction != write_direction && sec->rawsize > sec->size)
591 . return sec->rawsize;
592 . return sec->size;
595 .{* Functions to handle insertion and deletion of a bfd's sections. These
596 . only handle the list pointers, ie. do not adjust section_count,
597 . target_index etc. *}
598 .static inline void
599 .bfd_section_list_remove (bfd *abfd, asection *s)
601 . asection *next = s->next;
602 . asection *prev = s->prev;
603 . if (prev)
604 . prev->next = next;
605 . else
606 . abfd->sections = next;
607 . if (next)
608 . next->prev = prev;
609 . else
610 . abfd->section_last = prev;
613 .static inline void
614 .bfd_section_list_append (bfd *abfd, asection *s)
616 . s->next = 0;
617 . if (abfd->section_last)
619 . s->prev = abfd->section_last;
620 . abfd->section_last->next = s;
622 . else
624 . s->prev = 0;
625 . abfd->sections = s;
627 . abfd->section_last = s;
630 .static inline void
631 .bfd_section_list_prepend (bfd *abfd, asection *s)
633 . s->prev = 0;
634 . if (abfd->sections)
636 . s->next = abfd->sections;
637 . abfd->sections->prev = s;
639 . else
641 . s->next = 0;
642 . abfd->section_last = s;
644 . abfd->sections = s;
647 .static inline void
648 .bfd_section_list_insert_after (bfd *abfd, asection *a, asection *s)
650 . asection *next = a->next;
651 . s->next = next;
652 . s->prev = a;
653 . a->next = s;
654 . if (next)
655 . next->prev = s;
656 . else
657 . abfd->section_last = s;
660 .static inline void
661 .bfd_section_list_insert_before (bfd *abfd, asection *b, asection *s)
663 . asection *prev = b->prev;
664 . s->prev = prev;
665 . s->next = b;
666 . b->prev = s;
667 . if (prev)
668 . prev->next = s;
669 . else
670 . abfd->sections = s;
673 .static inline bool
674 .bfd_section_removed_from_list (const bfd *abfd, const asection *s)
676 . return s->next ? s->next->prev != s : abfd->section_last != s;
681 #include "sysdep.h"
682 #include <stdarg.h>
683 #include "bfd.h"
684 #include "bfdver.h"
685 #include "libiberty.h"
686 #include "demangle.h"
687 #include "safe-ctype.h"
688 #include "bfdlink.h"
689 #include "libbfd.h"
690 #include "coff/internal.h"
691 #include "coff/sym.h"
692 #include "libcoff.h"
693 #include "libecoff.h"
694 #undef obj_symbols
695 #include "elf-bfd.h"
697 #ifndef EXIT_FAILURE
698 #define EXIT_FAILURE 1
699 #endif
702 /* provide storage for subsystem, stack and heap data which may have been
703 passed in on the command line. Ld puts this data into a bfd_link_info
704 struct which ultimately gets passed in to the bfd. When it arrives, copy
705 it to the following struct so that the data will be available in coffcode.h
706 where it is needed. The typedef's used are defined in bfd.h */
709 INODE
710 Error reporting, Initialization, typedef bfd, BFD front end
712 SECTION
713 Error reporting
715 Most BFD functions return nonzero on success (check their
716 individual documentation for precise semantics). On an error,
717 they call <<bfd_set_error>> to set an error condition that callers
718 can check by calling <<bfd_get_error>>.
719 If that returns <<bfd_error_system_call>>, then check
720 <<errno>>.
722 The easiest way to report a BFD error to the user is to
723 use <<bfd_perror>>.
725 The BFD error is thread-local.
727 SUBSECTION
728 Type <<bfd_error_type>>
730 The values returned by <<bfd_get_error>> are defined by the
731 enumerated type <<bfd_error_type>>.
733 CODE_FRAGMENT
734 .typedef enum bfd_error
736 . bfd_error_no_error = 0,
737 . bfd_error_system_call,
738 . bfd_error_invalid_target,
739 . bfd_error_wrong_format,
740 . bfd_error_wrong_object_format,
741 . bfd_error_invalid_operation,
742 . bfd_error_no_memory,
743 . bfd_error_no_symbols,
744 . bfd_error_no_armap,
745 . bfd_error_no_more_archived_files,
746 . bfd_error_malformed_archive,
747 . bfd_error_missing_dso,
748 . bfd_error_file_not_recognized,
749 . bfd_error_file_ambiguously_recognized,
750 . bfd_error_no_contents,
751 . bfd_error_nonrepresentable_section,
752 . bfd_error_no_debug_section,
753 . bfd_error_bad_value,
754 . bfd_error_file_truncated,
755 . bfd_error_file_too_big,
756 . bfd_error_sorry,
757 . bfd_error_on_input,
758 . bfd_error_invalid_error_code
760 .bfd_error_type;
764 static TLS bfd_error_type bfd_error;
765 static TLS bfd_error_type input_error;
766 static TLS bfd *input_bfd;
767 static TLS char *_bfd_error_buf;
769 const char *const bfd_errmsgs[] =
771 N_("no error"),
772 N_("system call error"),
773 N_("invalid bfd target"),
774 N_("file in wrong format"),
775 N_("archive object file in wrong format"),
776 N_("invalid operation"),
777 N_("memory exhausted"),
778 N_("no symbols"),
779 N_("archive has no index; run ranlib to add one"),
780 N_("no more archived files"),
781 N_("malformed archive"),
782 N_("DSO missing from command line"),
783 N_("file format not recognized"),
784 N_("file format is ambiguous"),
785 N_("section has no contents"),
786 N_("nonrepresentable section on output"),
787 N_("symbol needs debug section which does not exist"),
788 N_("bad value"),
789 N_("file truncated"),
790 N_("file too big"),
791 N_("sorry, cannot handle this file"),
792 N_("error reading %s: %s"),
793 N_("#<invalid error code>")
797 FUNCTION
798 bfd_get_error
800 SYNOPSIS
801 bfd_error_type bfd_get_error (void);
803 DESCRIPTION
804 Return the current BFD error condition.
807 bfd_error_type
808 bfd_get_error (void)
810 return bfd_error;
814 FUNCTION
815 bfd_set_error
817 SYNOPSIS
818 void bfd_set_error (bfd_error_type error_tag);
820 DESCRIPTION
821 Set the BFD error condition to be @var{error_tag}.
823 @var{error_tag} must not be bfd_error_on_input. Use
824 bfd_set_input_error for input errors instead.
827 void
828 bfd_set_error (bfd_error_type error_tag)
830 bfd_error = error_tag;
831 if (bfd_error >= bfd_error_on_input)
832 abort ();
836 FUNCTION
837 bfd_set_input_error
839 SYNOPSIS
840 void bfd_set_input_error (bfd *input, bfd_error_type error_tag);
842 DESCRIPTION
844 Set the BFD error condition to be bfd_error_on_input.
845 @var{input} is the input bfd where the error occurred, and
846 @var{error_tag} the bfd_error_type error.
849 void
850 bfd_set_input_error (bfd *input, bfd_error_type error_tag)
852 /* This is an error that occurred during bfd_close when writing an
853 archive, but on one of the input files. */
854 bfd_error = bfd_error_on_input;
855 _bfd_clear_error_data ();
856 input_bfd = input;
857 input_error = error_tag;
858 if (input_error >= bfd_error_on_input)
859 abort ();
863 FUNCTION
864 bfd_errmsg
866 SYNOPSIS
867 const char *bfd_errmsg (bfd_error_type error_tag);
869 DESCRIPTION
870 Return a string describing the error @var{error_tag}, or
871 the system error if @var{error_tag} is <<bfd_error_system_call>>.
874 const char *
875 bfd_errmsg (bfd_error_type error_tag)
877 #ifndef errno
878 extern int errno;
879 #endif
880 if (error_tag == bfd_error_on_input)
882 const char *msg = bfd_errmsg (input_error);
883 char *ret = bfd_asprintf (_(bfd_errmsgs[error_tag]),
884 bfd_get_filename (input_bfd), msg);
885 if (ret)
886 return ret;
888 /* Ick, what to do on out of memory? */
889 return msg;
892 if (error_tag == bfd_error_system_call)
893 return xstrerror (errno);
895 if (error_tag > bfd_error_invalid_error_code)
896 error_tag = bfd_error_invalid_error_code; /* sanity check */
898 return _(bfd_errmsgs[error_tag]);
902 FUNCTION
903 bfd_perror
905 SYNOPSIS
906 void bfd_perror (const char *message);
908 DESCRIPTION
909 Print to the standard error stream a string describing the
910 last BFD error that occurred, or the last system error if
911 the last BFD error was a system call failure. If @var{message}
912 is non-NULL and non-empty, the error string printed is preceded
913 by @var{message}, a colon, and a space. It is followed by a newline.
916 void
917 bfd_perror (const char *message)
919 fflush (stdout);
920 if (message == NULL || *message == '\0')
921 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
922 else
923 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
924 fflush (stderr);
928 INTERNAL_FUNCTION
929 _bfd_clear_error_data
931 SYNOPSIS
932 void _bfd_clear_error_data (void);
934 DESCRIPTION
935 Free any data associated with the BFD error.
938 void
939 _bfd_clear_error_data (void)
941 free (_bfd_error_buf);
942 _bfd_error_buf = NULL;
946 INTERNAL_FUNCTION
947 bfd_asprintf
949 SYNOPSIS
950 char *bfd_asprintf (const char *fmt, ...);
952 DESCRIPTION
953 Primarily for error reporting, this function is like
954 libiberty's xasprintf except that it can return NULL on no
955 memory and the returned string should not be freed. Uses a
956 thread-local malloc'd buffer managed by libbfd, _bfd_error_buf.
957 Be aware that a call to this function frees the result of any
958 previous call. bfd_errmsg (bfd_error_on_input) also calls
959 this function.
962 char *
963 bfd_asprintf (const char *fmt, ...)
965 free (_bfd_error_buf);
966 _bfd_error_buf = NULL;
967 va_list ap;
968 va_start (ap, fmt);
969 int count = vasprintf (&_bfd_error_buf, fmt, ap);
970 va_end (ap);
971 if (count == -1)
973 bfd_set_error (bfd_error_no_memory);
974 _bfd_error_buf = NULL;
976 return _bfd_error_buf;
980 SUBSECTION
981 BFD error handler
983 Some BFD functions want to print messages describing the
984 problem. They call a BFD error handler function. This
985 function may be overridden by the program.
987 The BFD error handler acts like vprintf.
989 CODE_FRAGMENT
990 .typedef void (*bfd_error_handler_type) (const char *, va_list);
994 /* The program name used when printing BFD error messages. */
996 static const char *_bfd_error_program_name;
998 /* Support for positional parameters. */
1000 union _bfd_doprnt_args
1002 int i;
1003 long l;
1004 long long ll;
1005 double d;
1006 long double ld;
1007 void *p;
1008 enum
1010 Bad,
1011 Int,
1012 Long,
1013 LongLong,
1014 Double,
1015 LongDouble,
1017 } type;
1020 /* Maximum number of _bfd_error_handler args. Don't increase this
1021 without changing the code handling positional parameters. */
1022 #define MAX_ARGS 9
1024 /* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
1025 little and extended to handle '%pA', '%pB' and positional parameters. */
1027 #define PRINT_TYPE(TYPE, FIELD) \
1028 do \
1030 TYPE value = (TYPE) args[arg_no].FIELD; \
1031 result = print (stream, specifier, value); \
1032 } while (0)
1035 CODE_FRAGMENT
1036 .typedef int (*bfd_print_callback) (void *, const char *, ...);
1039 static int
1040 _bfd_doprnt (bfd_print_callback print, void *stream, const char *format,
1041 union _bfd_doprnt_args *args)
1043 const char *ptr = format;
1044 char specifier[128];
1045 int total_printed = 0;
1046 unsigned int arg_count = 0;
1048 while (*ptr != '\0')
1050 int result;
1052 if (*ptr != '%')
1054 /* While we have regular characters, print them. */
1055 char *end = strchr (ptr, '%');
1056 if (end != NULL)
1057 result = print (stream, "%.*s", (int) (end - ptr), ptr);
1058 else
1059 result = print (stream, "%s", ptr);
1060 ptr += result;
1062 else if (ptr[1] == '%')
1064 print (stream, "%%");
1065 result = 1;
1066 ptr += 2;
1068 else
1070 /* We have a format specifier! */
1071 char *sptr = specifier;
1072 int wide_width = 0, short_width = 0;
1073 unsigned int arg_no;
1075 /* Copy the % and move forward. */
1076 *sptr++ = *ptr++;
1078 /* Check for a positional parameter. */
1079 arg_no = -1u;
1080 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1082 arg_no = *ptr - '1';
1083 ptr += 2;
1086 /* Move past flags. */
1087 while (strchr ("-+ #0'I", *ptr))
1088 *sptr++ = *ptr++;
1090 if (*ptr == '*')
1092 int value;
1093 unsigned int arg_index;
1095 ptr++;
1096 arg_index = arg_count;
1097 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1099 arg_index = *ptr - '1';
1100 ptr += 2;
1102 value = abs (args[arg_index].i);
1103 arg_count++;
1104 sptr += sprintf (sptr, "%d", value);
1106 else
1107 /* Handle explicit numeric value. */
1108 while (ISDIGIT (*ptr))
1109 *sptr++ = *ptr++;
1111 /* Precision. */
1112 if (*ptr == '.')
1114 /* Copy and go past the period. */
1115 *sptr++ = *ptr++;
1116 if (*ptr == '*')
1118 int value;
1119 unsigned int arg_index;
1121 ptr++;
1122 arg_index = arg_count;
1123 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1125 arg_index = *ptr - '1';
1126 ptr += 2;
1128 value = abs (args[arg_index].i);
1129 arg_count++;
1130 sptr += sprintf (sptr, "%d", value);
1132 else
1133 /* Handle explicit numeric value. */
1134 while (ISDIGIT (*ptr))
1135 *sptr++ = *ptr++;
1137 while (strchr ("hlL", *ptr))
1139 switch (*ptr)
1141 case 'h':
1142 short_width = 1;
1143 break;
1144 case 'l':
1145 wide_width++;
1146 break;
1147 case 'L':
1148 wide_width = 2;
1149 break;
1150 default:
1151 abort();
1153 *sptr++ = *ptr++;
1156 /* Copy the type specifier, and NULL terminate. */
1157 *sptr++ = *ptr++;
1158 *sptr = '\0';
1159 if ((int) arg_no < 0)
1160 arg_no = arg_count;
1162 switch (ptr[-1])
1164 case 'd':
1165 case 'i':
1166 case 'o':
1167 case 'u':
1168 case 'x':
1169 case 'X':
1170 case 'c':
1172 /* Short values are promoted to int, so just copy it
1173 as an int and trust the C library printf to cast it
1174 to the right width. */
1175 if (short_width)
1176 PRINT_TYPE (int, i);
1177 else
1179 switch (wide_width)
1181 case 0:
1182 PRINT_TYPE (int, i);
1183 break;
1184 case 1:
1185 PRINT_TYPE (long, l);
1186 break;
1187 case 2:
1188 default:
1189 #if defined (__MSVCRT__)
1190 sptr[-3] = 'I';
1191 sptr[-2] = '6';
1192 sptr[-1] = '4';
1193 *sptr++ = ptr[-1];
1194 *sptr = '\0';
1195 #endif
1196 PRINT_TYPE (long long, ll);
1197 break;
1201 break;
1202 case 'f':
1203 case 'e':
1204 case 'E':
1205 case 'g':
1206 case 'G':
1208 if (wide_width == 0)
1209 PRINT_TYPE (double, d);
1210 else
1211 PRINT_TYPE (long double, ld);
1213 break;
1214 case 's':
1215 PRINT_TYPE (char *, p);
1216 break;
1217 case 'p':
1218 if (*ptr == 'A')
1220 asection *sec;
1221 bfd *abfd;
1222 const char *group = NULL;
1223 struct coff_comdat_info *ci;
1225 ptr++;
1226 sec = (asection *) args[arg_no].p;
1227 if (sec == NULL)
1228 /* Invoking %pA with a null section pointer is an
1229 internal error. */
1230 abort ();
1231 abfd = sec->owner;
1232 if (abfd != NULL
1233 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1234 && elf_next_in_group (sec) != NULL
1235 && (sec->flags & SEC_GROUP) == 0)
1236 group = elf_group_name (sec);
1237 else if (abfd != NULL
1238 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
1239 && (ci = bfd_coff_get_comdat_section (sec->owner,
1240 sec)) != NULL)
1241 group = ci->name;
1242 if (group != NULL)
1243 result = print (stream, "%s[%s]", sec->name, group);
1244 else
1245 result = print (stream, "%s", sec->name);
1247 else if (*ptr == 'B')
1249 bfd *abfd;
1251 ptr++;
1252 abfd = (bfd *) args[arg_no].p;
1253 if (abfd == NULL)
1254 /* Invoking %pB with a null bfd pointer is an
1255 internal error. */
1256 abort ();
1257 else if (abfd->my_archive
1258 && !bfd_is_thin_archive (abfd->my_archive))
1259 result = print (stream, "%s(%s)",
1260 bfd_get_filename (abfd->my_archive),
1261 bfd_get_filename (abfd));
1262 else
1263 result = print (stream, "%s", bfd_get_filename (abfd));
1265 else
1266 PRINT_TYPE (void *, p);
1267 break;
1268 default:
1269 abort();
1271 arg_count++;
1273 if (result == -1)
1274 return -1;
1275 total_printed += result;
1278 return total_printed;
1281 /* First pass over FORMAT to gather ARGS. Returns number of args. */
1283 static unsigned int
1284 _bfd_doprnt_scan (const char *format, va_list ap, union _bfd_doprnt_args *args)
1286 const char *ptr = format;
1287 unsigned int arg_count = 0;
1289 for (unsigned int i = 0; i < MAX_ARGS; i++)
1290 args[i].type = Bad;
1292 while (*ptr != '\0')
1294 if (*ptr != '%')
1296 ptr = strchr (ptr, '%');
1297 if (ptr == NULL)
1298 break;
1300 else if (ptr[1] == '%')
1301 ptr += 2;
1302 else
1304 int wide_width = 0, short_width = 0;
1305 unsigned int arg_no;
1306 int arg_type;
1308 ptr++;
1310 /* Check for a positional parameter. */
1311 arg_no = -1u;
1312 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1314 arg_no = *ptr - '1';
1315 ptr += 2;
1318 /* Move past flags. */
1319 while (strchr ("-+ #0'I", *ptr))
1320 ptr++;
1322 if (*ptr == '*')
1324 unsigned int arg_index;
1326 ptr++;
1327 arg_index = arg_count;
1328 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1330 arg_index = *ptr - '1';
1331 ptr += 2;
1333 if (arg_index >= MAX_ARGS)
1334 abort ();
1335 args[arg_index].type = Int;
1336 arg_count++;
1338 else
1339 /* Handle explicit numeric value. */
1340 while (ISDIGIT (*ptr))
1341 ptr++;
1343 /* Precision. */
1344 if (*ptr == '.')
1346 ptr++;
1347 if (*ptr == '*')
1349 unsigned int arg_index;
1351 ptr++;
1352 arg_index = arg_count;
1353 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1355 arg_index = *ptr - '1';
1356 ptr += 2;
1358 if (arg_index >= MAX_ARGS)
1359 abort ();
1360 args[arg_index].type = Int;
1361 arg_count++;
1363 else
1364 /* Handle explicit numeric value. */
1365 while (ISDIGIT (*ptr))
1366 ptr++;
1368 while (strchr ("hlL", *ptr))
1370 switch (*ptr)
1372 case 'h':
1373 short_width = 1;
1374 break;
1375 case 'l':
1376 wide_width++;
1377 break;
1378 case 'L':
1379 wide_width = 2;
1380 break;
1381 default:
1382 abort();
1384 ptr++;
1387 ptr++;
1388 if ((int) arg_no < 0)
1389 arg_no = arg_count;
1391 arg_type = Bad;
1392 switch (ptr[-1])
1394 case 'd':
1395 case 'i':
1396 case 'o':
1397 case 'u':
1398 case 'x':
1399 case 'X':
1400 case 'c':
1402 if (short_width)
1403 arg_type = Int;
1404 else
1406 switch (wide_width)
1408 case 0:
1409 arg_type = Int;
1410 break;
1411 case 1:
1412 arg_type = Long;
1413 break;
1414 case 2:
1415 default:
1416 arg_type = LongLong;
1417 break;
1421 break;
1422 case 'f':
1423 case 'e':
1424 case 'E':
1425 case 'g':
1426 case 'G':
1428 if (wide_width == 0)
1429 arg_type = Double;
1430 else
1431 arg_type = LongDouble;
1433 break;
1434 case 's':
1435 arg_type = Ptr;
1436 break;
1437 case 'p':
1438 if (*ptr == 'A' || *ptr == 'B')
1439 ptr++;
1440 arg_type = Ptr;
1441 break;
1442 default:
1443 abort();
1446 if (arg_no >= MAX_ARGS)
1447 abort ();
1448 args[arg_no].type = arg_type;
1449 arg_count++;
1453 for (unsigned int i = 0; i < arg_count; i++)
1455 switch (args[i].type)
1457 case Int:
1458 args[i].i = va_arg (ap, int);
1459 break;
1460 case Long:
1461 args[i].l = va_arg (ap, long);
1462 break;
1463 case LongLong:
1464 args[i].ll = va_arg (ap, long long);
1465 break;
1466 case Double:
1467 args[i].d = va_arg (ap, double);
1468 break;
1469 case LongDouble:
1470 args[i].ld = va_arg (ap, long double);
1471 break;
1472 case Ptr:
1473 args[i].p = va_arg (ap, void *);
1474 break;
1475 default:
1476 abort ();
1480 return arg_count;
1483 static void
1484 _bfd_print (bfd_print_callback print_func, void *stream,
1485 const char *fmt, va_list ap)
1487 union _bfd_doprnt_args args[MAX_ARGS];
1489 _bfd_doprnt_scan (fmt, ap, args);
1490 _bfd_doprnt (print_func, stream, fmt, args);
1494 FUNCTION
1495 bfd_print_error
1497 SYNOPSIS
1498 void bfd_print_error (bfd_print_callback print_func,
1499 void *stream, const char *fmt, va_list ap);
1501 DESCRIPTION
1503 This formats FMT and AP according to BFD "printf" rules,
1504 sending the output to STREAM by repeated calls to PRINT_FUNC.
1505 PRINT_FUNC is a printf-like function; it does not need to
1506 implement the BFD printf format extensions. This can be used
1507 in a callback that is set via bfd_set_error_handler to turn
1508 the error into ordinary output.
1511 void
1512 bfd_print_error (bfd_print_callback print_func, void *stream,
1513 const char *fmt, va_list ap)
1515 print_func (stream, "%s: ", _bfd_get_error_program_name ());
1516 _bfd_print (print_func, stream, fmt, ap);
1519 /* The standard error handler that prints to stderr. */
1521 static void
1522 error_handler_fprintf (const char *fmt, va_list ap)
1524 /* PR 4992: Don't interrupt output being sent to stdout. */
1525 fflush (stdout);
1527 bfd_print_error ((bfd_print_callback) fprintf, stderr, fmt, ap);
1529 /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
1530 warning, so use the fputc function to avoid it. */
1531 fputc ('\n', stderr);
1532 fflush (stderr);
1535 /* Control printing to a string buffer. */
1536 struct buf_stream
1538 char *ptr;
1539 int left;
1542 /* An fprintf like function that instead prints to a string buffer. */
1544 static int
1545 err_sprintf (void *stream, const char *fmt, ...)
1547 struct buf_stream *s = stream;
1548 va_list ap;
1550 va_start (ap, fmt);
1551 int total = vsnprintf (s->ptr, s->left, fmt, ap);
1552 va_end (ap);
1553 if (total < 0)
1555 else if (total > s->left)
1557 s->ptr += s->left;
1558 s->left = 0;
1560 else
1562 s->ptr += total;
1563 s->left -= total;
1565 return total;
1568 /* Communicate the bfd processed by bfd_check_format_matches to the
1569 error handling function error_handler_sprintf. */
1571 static bfd *error_handler_bfd;
1573 /* An error handler that prints to a string, then dups that string to
1574 a per-xvec cache. */
1576 static void
1577 error_handler_sprintf (const char *fmt, va_list ap)
1579 char error_buf[1024];
1580 struct buf_stream error_stream;
1582 error_stream.ptr = error_buf;
1583 error_stream.left = sizeof (error_buf);
1585 _bfd_print (err_sprintf, &error_stream, fmt, ap);
1587 size_t len = error_stream.ptr - error_buf;
1588 struct per_xvec_message **warn
1589 = _bfd_per_xvec_warn (error_handler_bfd->xvec, len + 1);
1590 if (*warn)
1592 memcpy ((*warn)->message, error_buf, len);
1593 (*warn)->message[len] = 0;
1597 /* This is a function pointer to the routine which should handle BFD
1598 error messages. It is called when a BFD routine encounters an
1599 error for which it wants to print a message. Going through a
1600 function pointer permits a program linked against BFD to intercept
1601 the messages and deal with them itself. */
1603 static bfd_error_handler_type _bfd_error_internal = error_handler_fprintf;
1606 FUNCTION
1607 _bfd_error_handler
1609 SYNOPSIS
1610 void _bfd_error_handler (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
1612 DESCRIPTION
1613 This is the default routine to handle BFD error messages.
1614 Like fprintf (stderr, ...), but also handles some extra format
1615 specifiers.
1617 %pA section name from section. For group components, prints
1618 group name too.
1619 %pB file name from bfd. For archive components, prints
1620 archive too.
1622 Beware: Only supports a maximum of 9 format arguments.
1625 void
1626 _bfd_error_handler (const char *fmt, ...)
1628 va_list ap;
1630 va_start (ap, fmt);
1631 _bfd_error_internal (fmt, ap);
1632 va_end (ap);
1636 FUNCTION
1637 bfd_set_error_handler
1639 SYNOPSIS
1640 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
1642 DESCRIPTION
1643 Set the BFD error handler function. Returns the previous
1644 function.
1647 bfd_error_handler_type
1648 bfd_set_error_handler (bfd_error_handler_type pnew)
1650 bfd_error_handler_type pold;
1652 pold = _bfd_error_internal;
1653 _bfd_error_internal = pnew;
1654 return pold;
1658 INTERNAL_FUNCTION
1659 _bfd_set_error_handler_caching
1661 SYNOPSIS
1662 bfd_error_handler_type _bfd_set_error_handler_caching (bfd *);
1664 DESCRIPTION
1665 Set the BFD error handler function to one that stores messages
1666 to the per_xvec_warn array. Returns the previous function.
1669 bfd_error_handler_type
1670 _bfd_set_error_handler_caching (bfd *abfd)
1672 error_handler_bfd = abfd;
1673 return bfd_set_error_handler (error_handler_sprintf);
1677 FUNCTION
1678 bfd_set_error_program_name
1680 SYNOPSIS
1681 void bfd_set_error_program_name (const char *);
1683 DESCRIPTION
1684 Set the program name to use when printing a BFD error. This
1685 is printed before the error message followed by a colon and
1686 space. The string must not be changed after it is passed to
1687 this function.
1690 void
1691 bfd_set_error_program_name (const char *name)
1693 _bfd_error_program_name = name;
1697 INTERNAL_FUNCTION
1698 _bfd_get_error_program_name
1700 SYNOPSIS
1701 const char *_bfd_get_error_program_name (void);
1703 DESCRIPTION
1704 Get the program name used when printing a BFD error.
1707 const char *
1708 _bfd_get_error_program_name (void)
1710 if (_bfd_error_program_name != NULL)
1711 return _bfd_error_program_name;
1712 return "BFD";
1716 SUBSECTION
1717 BFD assert handler
1719 If BFD finds an internal inconsistency, the bfd assert
1720 handler is called with information on the BFD version, BFD
1721 source file and line. If this happens, most programs linked
1722 against BFD are expected to want to exit with an error, or mark
1723 the current BFD operation as failed, so it is recommended to
1724 override the default handler, which just calls
1725 _bfd_error_handler and continues.
1727 CODE_FRAGMENT
1728 .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
1729 . const char *bfd_version,
1730 . const char *bfd_file,
1731 . int bfd_line);
1735 /* Note the use of bfd_ prefix on the parameter names above: we want to
1736 show which one is the message and which is the version by naming the
1737 parameters, but avoid polluting the program-using-bfd namespace as
1738 the typedef is visible in the exported headers that the program
1739 includes. Below, it's just for consistency. */
1741 static void
1742 _bfd_default_assert_handler (const char *bfd_formatmsg,
1743 const char *bfd_version,
1744 const char *bfd_file,
1745 int bfd_line)
1748 _bfd_error_handler (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
1751 /* Similar to _bfd_error_handler, a program can decide to exit on an
1752 internal BFD error. We use a non-variadic type to simplify passing
1753 on parameters to other functions, e.g. _bfd_error_handler. */
1755 static bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
1758 FUNCTION
1759 bfd_set_assert_handler
1761 SYNOPSIS
1762 bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
1764 DESCRIPTION
1765 Set the BFD assert handler function. Returns the previous
1766 function.
1769 bfd_assert_handler_type
1770 bfd_set_assert_handler (bfd_assert_handler_type pnew)
1772 bfd_assert_handler_type pold;
1774 pold = _bfd_assert_handler;
1775 _bfd_assert_handler = pnew;
1776 return pold;
1780 INODE
1781 Initialization, Threading, Error reporting, BFD front end
1783 FUNCTION
1784 bfd_init
1786 SYNOPSIS
1787 unsigned int bfd_init (void);
1789 DESCRIPTION
1790 This routine must be called before any other BFD function to
1791 initialize magical internal data structures.
1792 Returns a magic number, which may be used to check
1793 that the bfd library is configured as expected by users.
1795 .{* Value returned by bfd_init. *}
1796 .#define BFD_INIT_MAGIC (sizeof (struct bfd_section))
1800 unsigned int
1801 bfd_init (void)
1803 bfd_error = bfd_error_no_error;
1804 input_bfd = NULL;
1805 _bfd_clear_error_data ();
1806 input_error = bfd_error_no_error;
1807 _bfd_error_internal = error_handler_fprintf;
1808 _bfd_assert_handler = _bfd_default_assert_handler;
1810 return BFD_INIT_MAGIC;
1815 INODE
1816 Threading, Miscellaneous, Initialization, BFD front end
1818 SECTION
1819 Threading
1821 BFD has limited support for thread-safety. Most BFD globals
1822 are protected by locks, while the error-related globals are
1823 thread-local. A given BFD cannot safely be used from two
1824 threads at the same time; it is up to the application to do
1825 any needed locking. However, it is ok for different threads
1826 to work on different BFD objects at the same time.
1828 SUBSECTION
1829 Thread functions.
1831 CODE_FRAGMENT
1832 .typedef bool (*bfd_lock_unlock_fn_type) (void *);
1835 /* The lock and unlock functions, if set. */
1836 static bfd_lock_unlock_fn_type lock_fn;
1837 static bfd_lock_unlock_fn_type unlock_fn;
1838 static void *lock_data;
1841 FUNCTION
1842 bfd_thread_init
1844 SYNOPSIS
1845 bool bfd_thread_init
1846 (bfd_lock_unlock_fn_type lock,
1847 bfd_lock_unlock_fn_type unlock,
1848 void *data);
1850 DESCRIPTION
1852 Initialize BFD threading. The functions passed in will be
1853 used to lock and unlock global data structures. This may only
1854 be called a single time in a given process. Returns true on
1855 success and false on error. DATA is passed verbatim to the
1856 lock and unlock functions. The lock and unlock functions
1857 should return true on success, or set the BFD error and return
1858 false on failure.
1861 bool
1862 bfd_thread_init (bfd_lock_unlock_fn_type lock, bfd_lock_unlock_fn_type unlock,
1863 void *data)
1865 /* Both functions must be set, and this cannot have been called
1866 before. */
1867 if (lock == NULL || unlock == NULL || unlock_fn != NULL)
1869 bfd_set_error (bfd_error_invalid_operation);
1870 return false;
1873 lock_fn = lock;
1874 unlock_fn = unlock;
1875 lock_data = data;
1876 return true;
1880 FUNCTION
1881 bfd_thread_cleanup
1883 SYNOPSIS
1884 void bfd_thread_cleanup (void);
1886 DESCRIPTION
1887 Clean up any thread-local state. This should be called by a
1888 thread that uses any BFD functions, before the thread exits.
1889 It is fine to call this multiple times, or to call it and then
1890 later call BFD functions on the same thread again.
1893 void
1894 bfd_thread_cleanup (void)
1896 _bfd_clear_error_data ();
1900 INTERNAL_FUNCTION
1901 bfd_lock
1903 SYNOPSIS
1904 bool bfd_lock (void);
1906 DESCRIPTION
1907 Acquire the global BFD lock, if needed. Returns true on
1908 success, false on error.
1911 bool
1912 bfd_lock (void)
1914 if (lock_fn != NULL)
1915 return lock_fn (lock_data);
1916 return true;
1920 INTERNAL_FUNCTION
1921 bfd_unlock
1923 SYNOPSIS
1924 bool bfd_unlock (void);
1926 DESCRIPTION
1927 Release the global BFD lock, if needed. Returns true on
1928 success, false on error.
1931 bool
1932 bfd_unlock (void)
1934 if (unlock_fn != NULL)
1935 return unlock_fn (lock_data);
1936 return true;
1941 INODE
1942 Miscellaneous, Memory Usage, Threading, BFD front end
1944 SECTION
1945 Miscellaneous
1947 SUBSECTION
1948 Miscellaneous functions
1952 FUNCTION
1953 bfd_get_reloc_upper_bound
1955 SYNOPSIS
1956 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
1958 DESCRIPTION
1959 Return the number of bytes required to store the
1960 relocation information associated with section @var{sect}
1961 attached to bfd @var{abfd}. If an error occurs, return -1.
1965 long
1966 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1968 if (abfd->format != bfd_object)
1970 bfd_set_error (bfd_error_invalid_operation);
1971 return -1;
1974 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
1978 FUNCTION
1979 bfd_canonicalize_reloc
1981 SYNOPSIS
1982 long bfd_canonicalize_reloc
1983 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
1985 DESCRIPTION
1986 Call the back end associated with the open BFD
1987 @var{abfd} and translate the external form of the relocation
1988 information attached to @var{sec} into the internal canonical
1989 form. Place the table into memory at @var{loc}, which has
1990 been preallocated, usually by a call to
1991 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
1992 -1 on error.
1994 The @var{syms} table is also needed for horrible internal magic
1995 reasons.
1998 long
1999 bfd_canonicalize_reloc (bfd *abfd,
2000 sec_ptr asect,
2001 arelent **location,
2002 asymbol **symbols)
2004 if (abfd->format != bfd_object)
2006 bfd_set_error (bfd_error_invalid_operation);
2007 return -1;
2010 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
2011 (abfd, asect, location, symbols));
2015 FUNCTION
2016 bfd_set_reloc
2018 SYNOPSIS
2019 void bfd_set_reloc
2020 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
2022 DESCRIPTION
2023 Set the relocation pointer and count within
2024 section @var{sec} to the values @var{rel} and @var{count}.
2025 The argument @var{abfd} is ignored.
2027 .#define bfd_set_reloc(abfd, asect, location, count) \
2028 . BFD_SEND (abfd, _bfd_set_reloc, (abfd, asect, location, count))
2032 FUNCTION
2033 bfd_set_file_flags
2035 SYNOPSIS
2036 bool bfd_set_file_flags (bfd *abfd, flagword flags);
2038 DESCRIPTION
2039 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
2041 Possible errors are:
2042 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
2043 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
2044 o <<bfd_error_invalid_operation>> -
2045 The flag word contained a bit which was not applicable to the
2046 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
2047 on a BFD format which does not support demand paging.
2051 bool
2052 bfd_set_file_flags (bfd *abfd, flagword flags)
2054 if (abfd->format != bfd_object)
2056 bfd_set_error (bfd_error_wrong_format);
2057 return false;
2060 if (bfd_read_p (abfd))
2062 bfd_set_error (bfd_error_invalid_operation);
2063 return false;
2066 abfd->flags = flags;
2067 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
2069 bfd_set_error (bfd_error_invalid_operation);
2070 return false;
2073 return true;
2076 void
2077 bfd_assert (const char *file, int line)
2079 /* xgettext:c-format */
2080 (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
2081 BFD_VERSION_STRING, file, line);
2084 /* A more or less friendly abort message. In libbfd.h abort is
2085 defined to call this function. */
2087 void
2088 _bfd_abort (const char *file, int line, const char *fn)
2090 fflush (stdout);
2092 if (fn != NULL)
2093 fprintf (stderr, _("%s: BFD %s internal error, aborting at %s:%d in %s\n"),
2094 _bfd_get_error_program_name (), BFD_VERSION_STRING,
2095 file, line, fn);
2096 else
2097 fprintf (stderr, _("%s: BFD %s internal error, aborting at %s:%d\n"),
2098 _bfd_get_error_program_name (), BFD_VERSION_STRING,
2099 file, line);
2100 fprintf (stderr, _("Please report this bug.\n"));
2101 _exit (EXIT_FAILURE);
2105 FUNCTION
2106 bfd_get_arch_size
2108 SYNOPSIS
2109 int bfd_get_arch_size (bfd *abfd);
2111 DESCRIPTION
2112 Returns the normalized architecture address size, in bits, as
2113 determined by the object file's format. By normalized, we mean
2114 either 32 or 64. For ELF, this information is included in the
2115 header. Use bfd_arch_bits_per_address for number of bits in
2116 the architecture address.
2118 Returns the arch size in bits if known, <<-1>> otherwise.
2122 bfd_get_arch_size (bfd *abfd)
2124 if (abfd->xvec->flavour == bfd_target_elf_flavour)
2125 return get_elf_backend_data (abfd)->s->arch_size;
2127 return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
2131 FUNCTION
2132 bfd_get_sign_extend_vma
2134 SYNOPSIS
2135 int bfd_get_sign_extend_vma (bfd *abfd);
2137 DESCRIPTION
2138 Indicates if the target architecture "naturally" sign extends
2139 an address. Some architectures implicitly sign extend address
2140 values when they are converted to types larger than the size
2141 of an address. For instance, bfd_get_start_address() will
2142 return an address sign extended to fill a bfd_vma when this is
2143 the case.
2145 Returns <<1>> if the target architecture is known to sign
2146 extend addresses, <<0>> if the target architecture is known to
2147 not sign extend addresses, and <<-1>> otherwise.
2151 bfd_get_sign_extend_vma (bfd *abfd)
2153 const char *name;
2155 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2156 return get_elf_backend_data (abfd)->sign_extend_vma;
2158 name = bfd_get_target (abfd);
2160 /* Return a proper value for DJGPP & PE COFF.
2161 This function is required for DWARF2 support, but there is
2162 no place to store this information in the COFF back end.
2163 Should enough other COFF targets add support for DWARF2,
2164 a place will have to be found. Until then, this hack will do. */
2165 if (startswith (name, "coff-go32")
2166 || strcmp (name, "pe-i386") == 0
2167 || strcmp (name, "pei-i386") == 0
2168 || strcmp (name, "pe-x86-64") == 0
2169 || strcmp (name, "pei-x86-64") == 0
2170 || strcmp (name, "pe-aarch64-little") == 0
2171 || strcmp (name, "pei-aarch64-little") == 0
2172 || strcmp (name, "pe-arm-wince-little") == 0
2173 || strcmp (name, "pei-arm-wince-little") == 0
2174 || strcmp (name, "pei-loongarch64") == 0
2175 || strcmp (name, "pei-riscv64-little") == 0
2176 || strcmp (name, "aixcoff-rs6000") == 0
2177 || strcmp (name, "aix5coff64-rs6000") == 0)
2178 return 1;
2180 if (startswith (name, "mach-o"))
2181 return 0;
2183 bfd_set_error (bfd_error_wrong_format);
2184 return -1;
2188 FUNCTION
2189 bfd_set_start_address
2191 SYNOPSIS
2192 bool bfd_set_start_address (bfd *abfd, bfd_vma vma);
2194 DESCRIPTION
2195 Make @var{vma} the entry point of output BFD @var{abfd}.
2197 Returns <<TRUE>> on success, <<FALSE>> otherwise.
2200 bool
2201 bfd_set_start_address (bfd *abfd, bfd_vma vma)
2203 abfd->start_address = vma;
2204 return true;
2208 FUNCTION
2209 bfd_get_gp_size
2211 SYNOPSIS
2212 unsigned int bfd_get_gp_size (bfd *abfd);
2214 DESCRIPTION
2215 Return the maximum size of objects to be optimized using the GP
2216 register under MIPS ECOFF. This is typically set by the <<-G>>
2217 argument to the compiler, assembler or linker.
2220 unsigned int
2221 bfd_get_gp_size (bfd *abfd)
2223 if (abfd->format == bfd_object)
2225 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2226 return ecoff_data (abfd)->gp_size;
2227 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2228 return elf_gp_size (abfd);
2230 return 0;
2234 FUNCTION
2235 bfd_set_gp_size
2237 SYNOPSIS
2238 void bfd_set_gp_size (bfd *abfd, unsigned int i);
2240 DESCRIPTION
2241 Set the maximum size of objects to be optimized using the GP
2242 register under ECOFF or MIPS ELF. This is typically set by
2243 the <<-G>> argument to the compiler, assembler or linker.
2246 void
2247 bfd_set_gp_size (bfd *abfd, unsigned int i)
2249 /* Don't try to set GP size on an archive or core file! */
2250 if (abfd->format != bfd_object)
2251 return;
2253 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2254 ecoff_data (abfd)->gp_size = i;
2255 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2256 elf_gp_size (abfd) = i;
2259 /* Get the GP value. This is an internal function used by some of the
2260 relocation special_function routines on targets which support a GP
2261 register. */
2263 bfd_vma
2264 _bfd_get_gp_value (bfd *abfd)
2266 if (! abfd)
2267 return 0;
2268 if (abfd->format != bfd_object)
2269 return 0;
2271 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2272 return ecoff_data (abfd)->gp;
2273 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2274 return elf_gp (abfd);
2276 return 0;
2279 /* Set the GP value. */
2281 void
2282 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
2284 if (! abfd)
2285 abort ();
2286 if (abfd->format != bfd_object)
2287 return;
2289 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2290 ecoff_data (abfd)->gp = v;
2291 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2292 elf_gp (abfd) = v;
2296 FUNCTION
2297 bfd_set_gp_value
2299 SYNOPSIS
2300 void bfd_set_gp_value (bfd *abfd, bfd_vma v);
2302 DESCRIPTION
2303 Allow external access to the fucntion to set the GP value.
2304 This is specifically added for gdb-compile support.
2307 void
2308 bfd_set_gp_value (bfd *abfd, bfd_vma v)
2310 _bfd_set_gp_value (abfd, v);
2314 FUNCTION
2315 bfd_scan_vma
2317 SYNOPSIS
2318 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
2320 DESCRIPTION
2321 Convert, like <<strtoul>> or <<stdtoull> depending on the size
2322 of a <<bfd_vma>>, a numerical expression @var{string} into a
2323 <<bfd_vma>> integer, and return that integer.
2326 bfd_vma
2327 bfd_scan_vma (const char *string, const char **end, int base)
2329 if (sizeof (bfd_vma) <= sizeof (unsigned long))
2330 return strtoul (string, (char **) end, base);
2332 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
2333 return strtoull (string, (char **) end, base);
2335 abort ();
2339 FUNCTION
2340 bfd_copy_private_header_data
2342 SYNOPSIS
2343 bool bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
2345 DESCRIPTION
2346 Copy private BFD header information from the BFD @var{ibfd} to the
2347 the BFD @var{obfd}. This copies information that may require
2348 sections to exist, but does not require symbol tables. Return
2349 <<true>> on success, <<false>> on error.
2350 Possible error returns are:
2352 o <<bfd_error_no_memory>> -
2353 Not enough memory exists to create private data for @var{obfd}.
2355 .#define bfd_copy_private_header_data(ibfd, obfd) \
2356 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
2357 . (ibfd, obfd))
2362 FUNCTION
2363 bfd_copy_private_bfd_data
2365 SYNOPSIS
2366 bool bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
2368 DESCRIPTION
2369 Copy private BFD information from the BFD @var{ibfd} to the
2370 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
2371 Possible error returns are:
2373 o <<bfd_error_no_memory>> -
2374 Not enough memory exists to create private data for @var{obfd}.
2376 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
2377 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
2378 . (ibfd, obfd))
2383 FUNCTION
2384 bfd_set_private_flags
2386 SYNOPSIS
2387 bool bfd_set_private_flags (bfd *abfd, flagword flags);
2389 DESCRIPTION
2390 Set private BFD flag information in the BFD @var{abfd}.
2391 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
2392 returns are:
2394 o <<bfd_error_no_memory>> -
2395 Not enough memory exists to create private data for @var{obfd}.
2397 .#define bfd_set_private_flags(abfd, flags) \
2398 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
2403 FUNCTION
2404 Other functions
2406 DESCRIPTION
2407 The following functions exist but have not yet been documented.
2409 .#define bfd_sizeof_headers(abfd, info) \
2410 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
2412 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
2413 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2414 . (abfd, syms, sec, off, file, func, line, NULL))
2416 .#define bfd_find_nearest_line_with_alt(abfd, alt_filename, sec, syms, off, \
2417 . file, func, line, disc) \
2418 . BFD_SEND (abfd, _bfd_find_nearest_line_with_alt, \
2419 . (abfd, alt_filename, syms, sec, off, file, func, line, disc))
2421 .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
2422 . line, disc) \
2423 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2424 . (abfd, syms, sec, off, file, func, line, disc))
2426 .#define bfd_find_line(abfd, syms, sym, file, line) \
2427 . BFD_SEND (abfd, _bfd_find_line, \
2428 . (abfd, syms, sym, file, line))
2430 .#define bfd_find_inliner_info(abfd, file, func, line) \
2431 . BFD_SEND (abfd, _bfd_find_inliner_info, \
2432 . (abfd, file, func, line))
2434 .#define bfd_debug_info_start(abfd) \
2435 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
2437 .#define bfd_debug_info_end(abfd) \
2438 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
2440 .#define bfd_debug_info_accumulate(abfd, section) \
2441 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
2443 .#define bfd_stat_arch_elt(abfd, stat) \
2444 . BFD_SEND (abfd->my_archive ? abfd->my_archive : abfd, \
2445 . _bfd_stat_arch_elt, (abfd, stat))
2447 .#define bfd_update_armap_timestamp(abfd) \
2448 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
2450 .#define bfd_set_arch_mach(abfd, arch, mach)\
2451 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
2453 .#define bfd_relax_section(abfd, section, link_info, again) \
2454 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
2456 .#define bfd_gc_sections(abfd, link_info) \
2457 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
2459 .#define bfd_lookup_section_flags(link_info, flag_info, section) \
2460 . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
2462 .#define bfd_merge_sections(abfd, link_info) \
2463 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
2465 .#define bfd_is_group_section(abfd, sec) \
2466 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
2468 .#define bfd_group_name(abfd, sec) \
2469 . BFD_SEND (abfd, _bfd_group_name, (abfd, sec))
2471 .#define bfd_discard_group(abfd, sec) \
2472 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
2474 .#define bfd_link_hash_table_create(abfd) \
2475 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
2477 .#define bfd_link_add_symbols(abfd, info) \
2478 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
2480 .#define bfd_link_just_syms(abfd, sec, info) \
2481 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
2483 .#define bfd_final_link(abfd, info) \
2484 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
2486 .#define bfd_free_cached_info(abfd) \
2487 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
2489 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
2490 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
2492 .#define bfd_print_private_bfd_data(abfd, file)\
2493 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
2495 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
2496 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
2498 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
2499 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
2500 . dyncount, dynsyms, ret))
2502 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
2503 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
2505 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
2506 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
2511 FUNCTION
2512 bfd_get_relocated_section_contents
2514 SYNOPSIS
2515 bfd_byte *bfd_get_relocated_section_contents
2516 (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
2517 bool, asymbol **);
2519 DESCRIPTION
2520 Read and relocate the indirect link_order section, into DATA
2521 (if non-NULL) or to a malloc'd buffer. Return the buffer, or
2522 NULL on errors.
2525 bfd_byte *
2526 bfd_get_relocated_section_contents (bfd *abfd,
2527 struct bfd_link_info *link_info,
2528 struct bfd_link_order *link_order,
2529 bfd_byte *data,
2530 bool relocatable,
2531 asymbol **symbols)
2533 bfd *abfd2;
2534 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
2535 bfd_byte *, bool, asymbol **);
2537 if (link_order->type == bfd_indirect_link_order)
2539 abfd2 = link_order->u.indirect.section->owner;
2540 if (abfd2 == NULL)
2541 abfd2 = abfd;
2543 else
2544 abfd2 = abfd;
2546 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
2548 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
2552 FUNCTION
2553 bfd_record_phdr
2555 SYNOPSIS
2556 bool bfd_record_phdr
2557 (bfd *, unsigned long, bool, flagword, bool, bfd_vma,
2558 bool, bool, unsigned int, struct bfd_section **);
2560 DESCRIPTION
2561 Record information about an ELF program header.
2564 bool
2565 bfd_record_phdr (bfd *abfd,
2566 unsigned long type,
2567 bool flags_valid,
2568 flagword flags,
2569 bool at_valid,
2570 bfd_vma at, /* Bytes. */
2571 bool includes_filehdr,
2572 bool includes_phdrs,
2573 unsigned int count,
2574 asection **secs)
2576 struct elf_segment_map *m, **pm;
2577 size_t amt;
2578 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2580 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2581 return true;
2583 amt = sizeof (struct elf_segment_map);
2584 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
2585 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2586 if (m == NULL)
2587 return false;
2589 m->p_type = type;
2590 m->p_flags = flags;
2591 m->p_paddr = at * opb;
2592 m->p_flags_valid = flags_valid;
2593 m->p_paddr_valid = at_valid;
2594 m->includes_filehdr = includes_filehdr;
2595 m->includes_phdrs = includes_phdrs;
2596 m->count = count;
2597 if (count > 0)
2598 memcpy (m->sections, secs, count * sizeof (asection *));
2600 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
2602 *pm = m;
2604 return true;
2607 #ifdef BFD64
2608 /* Return true iff this target is 32-bit. */
2610 static bool
2611 is32bit (bfd *abfd)
2613 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2615 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2616 return bed->s->elfclass == ELFCLASS32;
2619 /* For non-ELF targets, use architecture information. */
2620 return bfd_arch_bits_per_address (abfd) <= 32;
2622 #endif
2625 FUNCTION
2626 bfd_sprintf_vma
2627 bfd_fprintf_vma
2629 SYNOPSIS
2630 void bfd_sprintf_vma (bfd *, char *, bfd_vma);
2631 void bfd_fprintf_vma (bfd *, void *, bfd_vma);
2633 DESCRIPTION
2634 bfd_sprintf_vma and bfd_fprintf_vma display an address in the
2635 target's address size.
2637 EXTERNAL
2638 .#define bfd_printf_vma(abfd,x) bfd_fprintf_vma (abfd, stdout, x)
2642 void
2643 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
2645 #ifdef BFD64
2646 if (!is32bit (abfd))
2648 sprintf (buf, "%016" PRIx64, (uint64_t) value);
2649 return;
2651 #endif
2652 sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
2655 void
2656 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
2658 #ifdef BFD64
2659 if (!is32bit (abfd))
2661 fprintf ((FILE *) stream, "%016" PRIx64, (uint64_t) value);
2662 return;
2664 #endif
2665 fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
2669 FUNCTION
2670 bfd_alt_mach_code
2672 SYNOPSIS
2673 bool bfd_alt_mach_code (bfd *abfd, int alternative);
2675 DESCRIPTION
2677 When more than one machine code number is available for the
2678 same machine type, this function can be used to switch between
2679 the preferred one (alternative == 0) and any others. Currently,
2680 only ELF supports this feature, with up to two alternate
2681 machine codes.
2684 bool
2685 bfd_alt_mach_code (bfd *abfd, int alternative)
2687 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2689 int code;
2691 switch (alternative)
2693 case 0:
2694 code = get_elf_backend_data (abfd)->elf_machine_code;
2695 break;
2697 case 1:
2698 code = get_elf_backend_data (abfd)->elf_machine_alt1;
2699 if (code == 0)
2700 return false;
2701 break;
2703 case 2:
2704 code = get_elf_backend_data (abfd)->elf_machine_alt2;
2705 if (code == 0)
2706 return false;
2707 break;
2709 default:
2710 return false;
2713 elf_elfheader (abfd)->e_machine = code;
2715 return true;
2718 return false;
2722 FUNCTION
2723 bfd_emul_get_maxpagesize
2725 SYNOPSIS
2726 bfd_vma bfd_emul_get_maxpagesize (const char *);
2728 DESCRIPTION
2729 Returns the maximum page size, in bytes, as determined by
2730 emulation.
2733 bfd_vma
2734 bfd_emul_get_maxpagesize (const char *emul)
2736 const bfd_target *target;
2738 target = bfd_find_target (emul, NULL);
2739 if (target != NULL
2740 && target->flavour == bfd_target_elf_flavour)
2741 return xvec_get_elf_backend_data (target)->maxpagesize;
2743 return 0;
2747 FUNCTION
2748 bfd_emul_get_commonpagesize
2750 SYNOPSIS
2751 bfd_vma bfd_emul_get_commonpagesize (const char *);
2753 DESCRIPTION
2754 Returns the common page size, in bytes, as determined by
2755 emulation.
2758 bfd_vma
2759 bfd_emul_get_commonpagesize (const char *emul)
2761 const bfd_target *target;
2763 target = bfd_find_target (emul, NULL);
2764 if (target != NULL
2765 && target->flavour == bfd_target_elf_flavour)
2767 const struct elf_backend_data *bed;
2769 bed = xvec_get_elf_backend_data (target);
2770 return bed->commonpagesize;
2772 return 0;
2776 FUNCTION
2777 bfd_demangle
2779 SYNOPSIS
2780 char *bfd_demangle (bfd *, const char *, int);
2782 DESCRIPTION
2783 Wrapper around cplus_demangle. Strips leading underscores and
2784 other such chars that would otherwise confuse the demangler.
2785 If passed a g++ v3 ABI mangled name, returns a buffer allocated
2786 with malloc holding the demangled name. Returns NULL otherwise
2787 and on memory alloc failure.
2790 char *
2791 bfd_demangle (bfd *abfd, const char *name, int options)
2793 char *res, *alloc;
2794 const char *pre, *suf;
2795 size_t pre_len;
2796 bool skip_lead;
2798 skip_lead = (abfd != NULL
2799 && *name != '\0'
2800 && bfd_get_symbol_leading_char (abfd) == *name);
2801 if (skip_lead)
2802 ++name;
2804 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
2805 or the MS PE format. These formats have a number of leading '.'s
2806 on at least some symbols, so we remove all dots to avoid
2807 confusing the demangler. */
2808 pre = name;
2809 while (*name == '.' || *name == '$')
2810 ++name;
2811 pre_len = name - pre;
2813 /* Strip off @plt and suchlike too. */
2814 alloc = NULL;
2815 suf = strchr (name, '@');
2816 if (suf != NULL)
2818 alloc = (char *) bfd_malloc (suf - name + 1);
2819 if (alloc == NULL)
2820 return NULL;
2821 memcpy (alloc, name, suf - name);
2822 alloc[suf - name] = '\0';
2823 name = alloc;
2826 res = cplus_demangle (name, options);
2828 free (alloc);
2830 if (res == NULL)
2832 if (skip_lead)
2834 size_t len = strlen (pre) + 1;
2835 alloc = (char *) bfd_malloc (len);
2836 if (alloc == NULL)
2837 return NULL;
2838 memcpy (alloc, pre, len);
2839 return alloc;
2841 return NULL;
2844 /* Put back any prefix or suffix. */
2845 if (pre_len != 0 || suf != NULL)
2847 size_t len;
2848 size_t suf_len;
2849 char *final;
2851 len = strlen (res);
2852 if (suf == NULL)
2853 suf = res + len;
2854 suf_len = strlen (suf) + 1;
2855 final = (char *) bfd_malloc (pre_len + len + suf_len);
2856 if (final != NULL)
2858 memcpy (final, pre, pre_len);
2859 memcpy (final + pre_len, res, len);
2860 memcpy (final + pre_len + len, suf, suf_len);
2862 free (res);
2863 res = final;
2866 return res;
2869 /* Get the linker information. */
2871 struct bfd_link_info *
2872 _bfd_get_link_info (bfd *abfd)
2874 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2875 return NULL;
2877 return elf_link_info (abfd);