testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / bfd / bfd.c
bloba93be109f11135d44637e81fb92a7e57a2930cd3
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_yes_unused = 2,
69 . bfd_plugin_no = 3
70 . };
72 .struct bfd_build_id
73 . {
74 . bfd_size_type size;
75 . bfd_byte data[1];
76 . };
78 .enum bfd_lto_object_type
79 . {
80 . lto_non_object, {* Not an LTO object. *}
81 . lto_non_ir_object, {* An object without LTO IR. *}
82 . lto_slim_ir_object, {* A slim LTO IR object. *}
83 . lto_fat_ir_object {* A fat LTO IR object. *}
84 . };
86 .struct bfd_mmapped_entry
87 . {
88 . void *addr;
89 . size_t size;
90 . };
92 .struct bfd_mmapped
93 . {
94 . struct bfd_mmapped *next;
95 . unsigned int max_entry;
96 . unsigned int next_entry;
97 . struct bfd_mmapped_entry entries[1];
98 . };
101 CODE_FRAGMENT
102 .struct bfd
104 . {* The filename the application opened the BFD with. *}
105 . const char *filename;
107 . {* A pointer to the target jump table. *}
108 . const struct bfd_target *xvec;
110 . {* The IOSTREAM, and corresponding IO vector that provide access
111 . to the file backing the BFD. *}
112 . void *iostream;
113 . const struct bfd_iovec *iovec;
115 . {* The caching routines use these to maintain a
116 . least-recently-used list of BFDs. *}
117 . struct bfd *lru_prev, *lru_next;
119 . {* Track current file position (or current buffer offset for
120 . in-memory BFDs). When a file is closed by the caching routines,
121 . BFD retains state information on the file here. *}
122 . ufile_ptr where;
124 . {* File modified time, if mtime_set is TRUE. *}
125 . long mtime;
127 . {* A unique identifier of the BFD *}
128 . unsigned int id;
130 . {* Format_specific flags. *}
131 . flagword flags;
133 . {* Values that may appear in the flags field of a BFD. These also
134 . appear in the object_flags field of the bfd_target structure, where
135 . they indicate the set of flags used by that backend (not all flags
136 . are meaningful for all object file formats) (FIXME: at the moment,
137 . the object_flags values have mostly just been copied from backend
138 . to another, and are not necessarily correct). *}
140 .#define BFD_NO_FLAGS 0x0
142 . {* BFD contains relocation entries. *}
143 .#define HAS_RELOC 0x1
145 . {* BFD is directly executable. *}
146 .#define EXEC_P 0x2
148 . {* BFD has line number information (basically used for F_LNNO in a
149 . COFF header). *}
150 .#define HAS_LINENO 0x4
152 . {* BFD has debugging information. *}
153 .#define HAS_DEBUG 0x08
155 . {* BFD has symbols. *}
156 .#define HAS_SYMS 0x10
158 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
159 . header). *}
160 .#define HAS_LOCALS 0x20
162 . {* BFD is a dynamic object. *}
163 .#define DYNAMIC 0x40
165 . {* Text section is write protected (if D_PAGED is not set, this is
166 . like an a.out NMAGIC file) (the linker sets this by default, but
167 . clears it for -r or -N). *}
168 .#define WP_TEXT 0x80
170 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
171 . linker sets this by default, but clears it for -r or -n or -N). *}
172 .#define D_PAGED 0x100
174 . {* BFD is relaxable (this means that bfd_relax_section may be able to
175 . do something) (sometimes bfd_relax_section can do something even if
176 . this is not set). *}
177 .#define BFD_IS_RELAXABLE 0x200
179 . {* This may be set before writing out a BFD to request using a
180 . traditional format. For example, this is used to request that when
181 . writing out an a.out object the symbols not be hashed to eliminate
182 . duplicates. *}
183 .#define BFD_TRADITIONAL_FORMAT 0x400
185 . {* This flag indicates that the BFD contents are actually cached
186 . in memory. If this is set, iostream points to a malloc'd
187 . bfd_in_memory struct. *}
188 .#define BFD_IN_MEMORY 0x800
190 . {* This BFD has been created by the linker and doesn't correspond
191 . to any input file. *}
192 .#define BFD_LINKER_CREATED 0x1000
194 . {* This may be set before writing out a BFD to request that it
195 . be written using values for UIDs, GIDs, timestamps, etc. that
196 . will be consistent from run to run. *}
197 .#define BFD_DETERMINISTIC_OUTPUT 0x2000
199 . {* Compress sections in this BFD. *}
200 .#define BFD_COMPRESS 0x4000
202 . {* Decompress sections in this BFD. *}
203 .#define BFD_DECOMPRESS 0x8000
205 . {* BFD is a dummy, for plugins. *}
206 .#define BFD_PLUGIN 0x10000
208 . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
209 .#define BFD_COMPRESS_GABI 0x20000
211 . {* Convert ELF common symbol type to STT_COMMON or STT_OBJECT in this
212 . BFD. *}
213 .#define BFD_CONVERT_ELF_COMMON 0x40000
215 . {* Use the ELF STT_COMMON type in this BFD. *}
216 .#define BFD_USE_ELF_STT_COMMON 0x80000
218 . {* Put pathnames into archives (non-POSIX). *}
219 .#define BFD_ARCHIVE_FULL_PATH 0x100000
221 .#define BFD_CLOSED_BY_CACHE 0x200000
223 . {* Compress sections in this BFD with SHF_COMPRESSED zstd. *}
224 .#define BFD_COMPRESS_ZSTD 0x400000
226 . {* Don't generate ELF section header. *}
227 .#define BFD_NO_SECTION_HEADER 0x800000
229 . {* Flags bits which are for BFD use only. *}
230 .#define BFD_FLAGS_FOR_BFD_USE_MASK \
231 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
232 . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
233 . | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON \
234 . | BFD_NO_SECTION_HEADER)
236 . {* The format which belongs to the BFD. (object, core, etc.) *}
237 . ENUM_BITFIELD (bfd_format) format : 3;
239 . {* The direction with which the BFD was opened. *}
240 . ENUM_BITFIELD (bfd_direction) direction : 2;
242 . {* POSIX.1-2017 (IEEE Std 1003.1) says of fopen : "When a file is
243 . opened with update mode ('+' as the second or third character in
244 . the mode argument), both input and output may be performed on
245 . the associated stream. However, the application shall ensure
246 . that output is not directly followed by input without an
247 . intervening call to fflush() or to a file positioning function
248 . (fseek(), fsetpos(), or rewind()), and input is not directly
249 . followed by output without an intervening call to a file
250 . positioning function, unless the input operation encounters
251 . end-of-file."
252 . This field tracks the last IO operation, so that bfd can insert
253 . a seek when IO direction changes. *}
254 . ENUM_BITFIELD (bfd_last_io) last_io : 2;
256 . {* Is the file descriptor being cached? That is, can it be closed as
257 . needed, and re-opened when accessed later? *}
258 . unsigned int cacheable : 1;
260 . {* Marks whether there was a default target specified when the
261 . BFD was opened. This is used to select which matching algorithm
262 . to use to choose the back end. *}
263 . unsigned int target_defaulted : 1;
265 . {* ... and here: (``once'' means at least once). *}
266 . unsigned int opened_once : 1;
268 . {* Set if we have a locally maintained mtime value, rather than
269 . getting it from the file each time. *}
270 . unsigned int mtime_set : 1;
272 . {* Flag set if symbols from this BFD should not be exported. *}
273 . unsigned int no_export : 1;
275 . {* Remember when output has begun, to stop strange things
276 . from happening. *}
277 . unsigned int output_has_begun : 1;
279 . {* Have archive map. *}
280 . unsigned int has_armap : 1;
282 . {* Set if this is a thin archive. *}
283 . unsigned int is_thin_archive : 1;
285 . {* Set if this archive should not cache element positions. *}
286 . unsigned int no_element_cache : 1;
288 . {* Set if only required symbols should be added in the link hash table for
289 . this object. Used by VMS linkers. *}
290 . unsigned int selective_search : 1;
292 . {* Set if this is the linker output BFD. *}
293 . unsigned int is_linker_output : 1;
295 . {* Set if this is the linker input BFD. *}
296 . unsigned int is_linker_input : 1;
298 . {* If this is an input for a compiler plug-in library. *}
299 . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
301 . {* Set if this is a plugin output file. *}
302 . unsigned int lto_output : 1;
304 . {* Do not attempt to modify this file. Set when detecting errors
305 . that BFD is not prepared to handle for objcopy/strip. *}
306 . unsigned int read_only : 1;
308 . {* LTO object type. *}
309 . ENUM_BITFIELD (bfd_lto_object_type) lto_type : 2;
311 . {* Set if this BFD is currently being processed by
312 . bfd_check_format_matches. This is checked by the cache to
313 . avoid closing the BFD in this case. This should only be
314 . examined or modified while the BFD lock is held. *}
315 . unsigned int in_format_matches : 1;
317 . {* Set to dummy BFD created when claimed by a compiler plug-in
318 . library. *}
319 . bfd *plugin_dummy_bfd;
321 . {* The offset of this bfd in the file, typically 0 if it is not
322 . contained in an archive. *}
323 . ufile_ptr origin;
325 . {* The origin in the archive of the proxy entry. This will
326 . normally be the same as origin, except for thin archives,
327 . when it will contain the current offset of the proxy in the
328 . thin archive rather than the offset of the bfd in its actual
329 . container. *}
330 . ufile_ptr proxy_origin;
332 . {* A hash table for section names. *}
333 . struct bfd_hash_table section_htab;
335 . {* Pointer to linked list of sections. *}
336 . struct bfd_section *sections;
338 . {* The last section on the section list. *}
339 . struct bfd_section *section_last;
341 . {* The number of sections. *}
342 . unsigned int section_count;
344 . {* The archive plugin file descriptor. *}
345 . int archive_plugin_fd;
347 . {* The number of opens on the archive plugin file descriptor. *}
348 . unsigned int archive_plugin_fd_open_count;
350 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
351 . be used only for archive elements. *}
352 . int archive_pass;
354 . {* The total size of memory from bfd_alloc. *}
355 . bfd_size_type alloc_size;
357 . {* Stuff only useful for object files:
358 . The start address. *}
359 . bfd_vma start_address;
361 . {* Symbol table for output BFD (with symcount entries).
362 . Also used by the linker to cache input BFD symbols. *}
363 . struct bfd_symbol **outsymbols;
365 . {* Used for input and output. *}
366 . unsigned int symcount;
368 . {* Used for slurped dynamic symbol tables. *}
369 . unsigned int dynsymcount;
371 . {* Pointer to structure which contains architecture information. *}
372 . const struct bfd_arch_info *arch_info;
374 . {* Cached length of file for bfd_get_size. 0 until bfd_get_size is
375 . called, 1 if stat returns an error or the file size is too large to
376 . return in ufile_ptr. Both 0 and 1 should be treated as "unknown". *}
377 . ufile_ptr size;
379 . {* Stuff only useful for archives. *}
380 . void *arelt_data;
381 . struct bfd *my_archive; {* The containing archive BFD. *}
382 . struct bfd *archive_next; {* The next BFD in the archive. *}
383 . struct bfd *archive_head; {* The first BFD in the archive. *}
384 . struct bfd *nested_archives; {* List of nested archive in a flattened
385 . thin archive. *}
387 . union {
388 . {* For input BFDs, a chain of BFDs involved in a link. *}
389 . struct bfd *next;
390 . {* For output BFD, the linker hash table. *}
391 . struct bfd_link_hash_table *hash;
392 . } link;
394 . {* Used by the back end to hold private data. *}
395 . union
397 . struct aout_data_struct *aout_data;
398 . struct artdata *aout_ar_data;
399 . struct coff_tdata *coff_obj_data;
400 . struct pe_tdata *pe_obj_data;
401 . struct xcoff_tdata *xcoff_obj_data;
402 . struct ecoff_tdata *ecoff_obj_data;
403 . struct srec_data_struct *srec_data;
404 . struct verilog_data_struct *verilog_data;
405 . struct ihex_data_struct *ihex_data;
406 . struct tekhex_data_struct *tekhex_data;
407 . struct elf_obj_tdata *elf_obj_data;
408 . struct mmo_data_struct *mmo_data;
409 . struct trad_core_struct *trad_core_data;
410 . struct som_data_struct *som_data;
411 . struct hpux_core_struct *hpux_core_data;
412 . struct hppabsd_core_struct *hppabsd_core_data;
413 . struct sgi_core_struct *sgi_core_data;
414 . struct lynx_core_struct *lynx_core_data;
415 . struct osf_core_struct *osf_core_data;
416 . struct cisco_core_struct *cisco_core_data;
417 . struct netbsd_core_struct *netbsd_core_data;
418 . struct mach_o_data_struct *mach_o_data;
419 . struct mach_o_fat_data_struct *mach_o_fat_data;
420 . struct plugin_data_struct *plugin_data;
421 . struct bfd_pef_data_struct *pef_data;
422 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
423 . struct bfd_sym_data_struct *sym_data;
424 . void *any;
426 . tdata;
428 . {* Used by the application to hold private data. *}
429 . void *usrdata;
431 . {* Where all the allocated stuff under this BFD goes. This is a
432 . struct objalloc *, but we use void * to avoid requiring the inclusion
433 . of objalloc.h. *}
434 . void *memory;
436 . {* For input BFDs, the build ID, if the object has one. *}
437 . const struct bfd_build_id *build_id;
439 . {* For input BFDs, mmapped entries. *}
440 . struct bfd_mmapped *mmapped;
444 EXTERNAL
445 .static inline const char *
446 .bfd_get_filename (const bfd *abfd)
448 . return abfd->filename;
451 .static inline bool
452 .bfd_get_cacheable (const bfd *abfd)
454 . return abfd->cacheable;
457 .static inline enum bfd_format
458 .bfd_get_format (const bfd *abfd)
460 . return abfd->format;
463 .static inline enum bfd_lto_object_type
464 .bfd_get_lto_type (const bfd *abfd)
466 . return abfd->lto_type;
469 .static inline flagword
470 .bfd_get_file_flags (const bfd *abfd)
472 . return abfd->flags;
475 .static inline bfd_vma
476 .bfd_get_start_address (const bfd *abfd)
478 . return abfd->start_address;
481 .static inline unsigned int
482 .bfd_get_symcount (const bfd *abfd)
484 . return abfd->symcount;
487 .static inline unsigned int
488 .bfd_get_dynamic_symcount (const bfd *abfd)
490 . return abfd->dynsymcount;
493 .static inline struct bfd_symbol **
494 .bfd_get_outsymbols (const bfd *abfd)
496 . return abfd->outsymbols;
499 .static inline unsigned int
500 .bfd_count_sections (const bfd *abfd)
502 . return abfd->section_count;
505 .static inline bool
506 .bfd_has_map (const bfd *abfd)
508 . return abfd->has_armap;
511 .static inline bool
512 .bfd_is_thin_archive (const bfd *abfd)
514 . return abfd->is_thin_archive;
517 .static inline void *
518 .bfd_usrdata (const bfd *abfd)
520 . return abfd->usrdata;
523 .{* See note beside bfd_set_section_userdata. *}
524 .static inline bool
525 .bfd_set_cacheable (bfd * abfd, bool val)
527 . abfd->cacheable = val;
528 . return true;
531 .static inline void
532 .bfd_set_thin_archive (bfd *abfd, bool val)
534 . abfd->is_thin_archive = val;
537 .static inline void
538 .bfd_set_usrdata (bfd *abfd, void *val)
540 . abfd->usrdata = val;
543 .static inline asection *
544 .bfd_asymbol_section (const asymbol *sy)
546 . return sy->section;
549 .static inline bfd_vma
550 .bfd_asymbol_value (const asymbol *sy)
552 . return sy->section->vma + sy->value;
555 .static inline const char *
556 .bfd_asymbol_name (const asymbol *sy)
558 . return sy->name;
561 .static inline struct bfd *
562 .bfd_asymbol_bfd (const asymbol *sy)
564 . return sy->the_bfd;
567 .static inline void
568 .bfd_set_asymbol_name (asymbol *sy, const char *name)
570 . sy->name = name;
573 .{* For input sections return the original size on disk of the
574 . section. For output sections return the current size. *}
575 .static inline bfd_size_type
576 .bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
578 . if (abfd->direction != write_direction && sec->rawsize != 0)
579 . return sec->rawsize;
580 . return sec->size;
583 .{* Find the address one past the end of SEC. *}
584 .static inline bfd_size_type
585 .bfd_get_section_limit (const bfd *abfd, const asection *sec)
587 . return (bfd_get_section_limit_octets (abfd, sec)
588 . / bfd_octets_per_byte (abfd, sec));
591 .{* For input sections return the larger of the current size and the
592 . original size on disk of the section. For output sections return
593 . the current size. *}
594 .static inline bfd_size_type
595 .bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
597 . if (abfd->direction != write_direction && sec->rawsize > sec->size)
598 . return sec->rawsize;
599 . return sec->size;
602 .{* Functions to handle insertion and deletion of a bfd's sections. These
603 . only handle the list pointers, ie. do not adjust section_count,
604 . target_index etc. *}
605 .static inline void
606 .bfd_section_list_remove (bfd *abfd, asection *s)
608 . asection *next = s->next;
609 . asection *prev = s->prev;
610 . if (prev)
611 . prev->next = next;
612 . else
613 . abfd->sections = next;
614 . if (next)
615 . next->prev = prev;
616 . else
617 . abfd->section_last = prev;
620 .static inline void
621 .bfd_section_list_append (bfd *abfd, asection *s)
623 . s->next = 0;
624 . if (abfd->section_last)
626 . s->prev = abfd->section_last;
627 . abfd->section_last->next = s;
629 . else
631 . s->prev = 0;
632 . abfd->sections = s;
634 . abfd->section_last = s;
637 .static inline void
638 .bfd_section_list_prepend (bfd *abfd, asection *s)
640 . s->prev = 0;
641 . if (abfd->sections)
643 . s->next = abfd->sections;
644 . abfd->sections->prev = s;
646 . else
648 . s->next = 0;
649 . abfd->section_last = s;
651 . abfd->sections = s;
654 .static inline void
655 .bfd_section_list_insert_after (bfd *abfd, asection *a, asection *s)
657 . asection *next = a->next;
658 . s->next = next;
659 . s->prev = a;
660 . a->next = s;
661 . if (next)
662 . next->prev = s;
663 . else
664 . abfd->section_last = s;
667 .static inline void
668 .bfd_section_list_insert_before (bfd *abfd, asection *b, asection *s)
670 . asection *prev = b->prev;
671 . s->prev = prev;
672 . s->next = b;
673 . b->prev = s;
674 . if (prev)
675 . prev->next = s;
676 . else
677 . abfd->sections = s;
680 .static inline bool
681 .bfd_section_removed_from_list (const bfd *abfd, const asection *s)
683 . return s->next ? s->next->prev != s : abfd->section_last != s;
688 #include "sysdep.h"
689 #include <stdarg.h>
690 #include "bfd.h"
691 #include "bfdver.h"
692 #include "libiberty.h"
693 #include "demangle.h"
694 #include "safe-ctype.h"
695 #include "bfdlink.h"
696 #include "libbfd.h"
697 #include "coff/internal.h"
698 #include "coff/sym.h"
699 #include "libcoff.h"
700 #include "libecoff.h"
701 #undef obj_symbols
702 #include "elf-bfd.h"
704 #ifndef EXIT_FAILURE
705 #define EXIT_FAILURE 1
706 #endif
709 /* provide storage for subsystem, stack and heap data which may have been
710 passed in on the command line. Ld puts this data into a bfd_link_info
711 struct which ultimately gets passed in to the bfd. When it arrives, copy
712 it to the following struct so that the data will be available in coffcode.h
713 where it is needed. The typedef's used are defined in bfd.h */
716 INODE
717 Error reporting, Initialization, typedef bfd, BFD front end
719 SECTION
720 Error reporting
722 Most BFD functions return nonzero on success (check their
723 individual documentation for precise semantics). On an error,
724 they call <<bfd_set_error>> to set an error condition that callers
725 can check by calling <<bfd_get_error>>.
726 If that returns <<bfd_error_system_call>>, then check
727 <<errno>>.
729 The easiest way to report a BFD error to the user is to
730 use <<bfd_perror>>.
732 The BFD error is thread-local.
734 SUBSECTION
735 Type <<bfd_error_type>>
737 The values returned by <<bfd_get_error>> are defined by the
738 enumerated type <<bfd_error_type>>.
740 CODE_FRAGMENT
741 .typedef enum bfd_error
743 . bfd_error_no_error = 0,
744 . bfd_error_system_call,
745 . bfd_error_invalid_target,
746 . bfd_error_wrong_format,
747 . bfd_error_wrong_object_format,
748 . bfd_error_invalid_operation,
749 . bfd_error_no_memory,
750 . bfd_error_no_symbols,
751 . bfd_error_no_armap,
752 . bfd_error_no_more_archived_files,
753 . bfd_error_malformed_archive,
754 . bfd_error_missing_dso,
755 . bfd_error_file_not_recognized,
756 . bfd_error_file_ambiguously_recognized,
757 . bfd_error_no_contents,
758 . bfd_error_nonrepresentable_section,
759 . bfd_error_no_debug_section,
760 . bfd_error_bad_value,
761 . bfd_error_file_truncated,
762 . bfd_error_file_too_big,
763 . bfd_error_sorry,
764 . bfd_error_on_input,
765 . bfd_error_invalid_error_code
767 .bfd_error_type;
771 static TLS bfd_error_type bfd_error;
772 static TLS bfd_error_type input_error;
773 static TLS bfd *input_bfd;
774 static TLS char *_bfd_error_buf;
776 const char *const bfd_errmsgs[] =
778 N_("no error"),
779 N_("system call error"),
780 N_("invalid bfd target"),
781 N_("file in wrong format"),
782 N_("archive object file in wrong format"),
783 N_("invalid operation"),
784 N_("memory exhausted"),
785 N_("no symbols"),
786 N_("archive has no index; run ranlib to add one"),
787 N_("no more archived files"),
788 N_("malformed archive"),
789 N_("DSO missing from command line"),
790 N_("file format not recognized"),
791 N_("file format is ambiguous"),
792 N_("section has no contents"),
793 N_("nonrepresentable section on output"),
794 N_("symbol needs debug section which does not exist"),
795 N_("bad value"),
796 N_("file truncated"),
797 N_("file too big"),
798 N_("sorry, cannot handle this file"),
799 N_("error reading %s: %s"),
800 N_("#<invalid error code>")
804 FUNCTION
805 bfd_get_error
807 SYNOPSIS
808 bfd_error_type bfd_get_error (void);
810 DESCRIPTION
811 Return the current BFD error condition.
814 bfd_error_type
815 bfd_get_error (void)
817 return bfd_error;
821 FUNCTION
822 bfd_set_error
824 SYNOPSIS
825 void bfd_set_error (bfd_error_type error_tag);
827 DESCRIPTION
828 Set the BFD error condition to be @var{error_tag}.
830 @var{error_tag} must not be bfd_error_on_input. Use
831 bfd_set_input_error for input errors instead.
834 void
835 bfd_set_error (bfd_error_type error_tag)
837 bfd_error = error_tag;
838 if (bfd_error >= bfd_error_on_input)
839 abort ();
843 FUNCTION
844 bfd_set_input_error
846 SYNOPSIS
847 void bfd_set_input_error (bfd *input, bfd_error_type error_tag);
849 DESCRIPTION
851 Set the BFD error condition to be bfd_error_on_input.
852 @var{input} is the input bfd where the error occurred, and
853 @var{error_tag} the bfd_error_type error.
856 void
857 bfd_set_input_error (bfd *input, bfd_error_type error_tag)
859 /* This is an error that occurred during bfd_close when writing an
860 archive, but on one of the input files. */
861 bfd_error = bfd_error_on_input;
862 _bfd_clear_error_data ();
863 input_bfd = input;
864 input_error = error_tag;
865 if (input_error >= bfd_error_on_input)
866 abort ();
870 FUNCTION
871 bfd_errmsg
873 SYNOPSIS
874 const char *bfd_errmsg (bfd_error_type error_tag);
876 DESCRIPTION
877 Return a string describing the error @var{error_tag}, or
878 the system error if @var{error_tag} is <<bfd_error_system_call>>.
881 const char *
882 bfd_errmsg (bfd_error_type error_tag)
884 #ifndef errno
885 extern int errno;
886 #endif
887 if (error_tag == bfd_error_on_input)
889 const char *msg = bfd_errmsg (input_error);
890 char *ret = bfd_asprintf (_(bfd_errmsgs[error_tag]),
891 bfd_get_filename (input_bfd), msg);
892 if (ret)
893 return ret;
895 /* Ick, what to do on out of memory? */
896 return msg;
899 if (error_tag == bfd_error_system_call)
900 return xstrerror (errno);
902 if (error_tag > bfd_error_invalid_error_code)
903 error_tag = bfd_error_invalid_error_code; /* sanity check */
905 return _(bfd_errmsgs[error_tag]);
909 FUNCTION
910 bfd_perror
912 SYNOPSIS
913 void bfd_perror (const char *message);
915 DESCRIPTION
916 Print to the standard error stream a string describing the
917 last BFD error that occurred, or the last system error if
918 the last BFD error was a system call failure. If @var{message}
919 is non-NULL and non-empty, the error string printed is preceded
920 by @var{message}, a colon, and a space. It is followed by a newline.
923 void
924 bfd_perror (const char *message)
926 fflush (stdout);
927 if (message == NULL || *message == '\0')
928 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
929 else
930 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
931 fflush (stderr);
935 INTERNAL_FUNCTION
936 _bfd_clear_error_data
938 SYNOPSIS
939 void _bfd_clear_error_data (void);
941 DESCRIPTION
942 Free any data associated with the BFD error.
945 void
946 _bfd_clear_error_data (void)
948 free (_bfd_error_buf);
949 _bfd_error_buf = NULL;
953 INTERNAL_FUNCTION
954 bfd_asprintf
956 SYNOPSIS
957 char *bfd_asprintf (const char *fmt, ...);
959 DESCRIPTION
960 Primarily for error reporting, this function is like
961 libiberty's xasprintf except that it can return NULL on no
962 memory and the returned string should not be freed. Uses a
963 thread-local malloc'd buffer managed by libbfd, _bfd_error_buf.
964 Be aware that a call to this function frees the result of any
965 previous call. bfd_errmsg (bfd_error_on_input) also calls
966 this function.
969 char *
970 bfd_asprintf (const char *fmt, ...)
972 free (_bfd_error_buf);
973 _bfd_error_buf = NULL;
974 va_list ap;
975 va_start (ap, fmt);
976 int count = vasprintf (&_bfd_error_buf, fmt, ap);
977 va_end (ap);
978 if (count == -1)
980 bfd_set_error (bfd_error_no_memory);
981 _bfd_error_buf = NULL;
983 return _bfd_error_buf;
987 SUBSECTION
988 BFD error handler
990 Some BFD functions want to print messages describing the
991 problem. They call a BFD error handler function. This
992 function may be overridden by the program.
994 The BFD error handler acts like vprintf.
996 CODE_FRAGMENT
997 .typedef void (*bfd_error_handler_type) (const char *, va_list);
1001 /* The program name used when printing BFD error messages. */
1003 static const char *_bfd_error_program_name;
1005 /* Support for positional parameters. */
1007 union _bfd_doprnt_args
1009 int i;
1010 long l;
1011 long long ll;
1012 double d;
1013 long double ld;
1014 void *p;
1015 enum
1017 Bad,
1018 Int,
1019 Long,
1020 LongLong,
1021 Double,
1022 LongDouble,
1024 } type;
1027 /* Maximum number of _bfd_error_handler args. Don't increase this
1028 without changing the code handling positional parameters. */
1029 #define MAX_ARGS 9
1031 /* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
1032 little and extended to handle '%pA', '%pB' and positional parameters. */
1034 #define PRINT_TYPE(TYPE, FIELD) \
1035 do \
1037 TYPE value = (TYPE) args[arg_no].FIELD; \
1038 result = print (stream, specifier, value); \
1039 } while (0)
1042 CODE_FRAGMENT
1043 .typedef int (*bfd_print_callback) (void *, const char *, ...);
1046 static int
1047 _bfd_doprnt (bfd_print_callback print, void *stream, const char *format,
1048 union _bfd_doprnt_args *args)
1050 const char *ptr = format;
1051 char specifier[128];
1052 int total_printed = 0;
1053 unsigned int arg_count = 0;
1055 while (*ptr != '\0')
1057 int result;
1059 if (*ptr != '%')
1061 /* While we have regular characters, print them. */
1062 char *end = strchr (ptr, '%');
1063 if (end != NULL)
1064 result = print (stream, "%.*s", (int) (end - ptr), ptr);
1065 else
1066 result = print (stream, "%s", ptr);
1067 ptr += result;
1069 else if (ptr[1] == '%')
1071 print (stream, "%%");
1072 result = 1;
1073 ptr += 2;
1075 else
1077 /* We have a format specifier! */
1078 char *sptr = specifier;
1079 int wide_width = 0, short_width = 0;
1080 unsigned int arg_no;
1082 /* Copy the % and move forward. */
1083 *sptr++ = *ptr++;
1085 /* Check for a positional parameter. */
1086 arg_no = -1u;
1087 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1089 arg_no = *ptr - '1';
1090 ptr += 2;
1093 /* Move past flags. */
1094 while (strchr ("-+ #0'I", *ptr))
1095 *sptr++ = *ptr++;
1097 if (*ptr == '*')
1099 int value;
1100 unsigned int arg_index;
1102 ptr++;
1103 arg_index = arg_count;
1104 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1106 arg_index = *ptr - '1';
1107 ptr += 2;
1109 value = abs (args[arg_index].i);
1110 arg_count++;
1111 sptr += sprintf (sptr, "%d", value);
1113 else
1114 /* Handle explicit numeric value. */
1115 while (ISDIGIT (*ptr))
1116 *sptr++ = *ptr++;
1118 /* Precision. */
1119 if (*ptr == '.')
1121 /* Copy and go past the period. */
1122 *sptr++ = *ptr++;
1123 if (*ptr == '*')
1125 int value;
1126 unsigned int arg_index;
1128 ptr++;
1129 arg_index = arg_count;
1130 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1132 arg_index = *ptr - '1';
1133 ptr += 2;
1135 value = abs (args[arg_index].i);
1136 arg_count++;
1137 sptr += sprintf (sptr, "%d", value);
1139 else
1140 /* Handle explicit numeric value. */
1141 while (ISDIGIT (*ptr))
1142 *sptr++ = *ptr++;
1144 while (strchr ("hlL", *ptr))
1146 switch (*ptr)
1148 case 'h':
1149 short_width = 1;
1150 break;
1151 case 'l':
1152 wide_width++;
1153 break;
1154 case 'L':
1155 wide_width = 2;
1156 break;
1157 default:
1158 abort();
1160 *sptr++ = *ptr++;
1163 /* Copy the type specifier, and NULL terminate. */
1164 *sptr++ = *ptr++;
1165 *sptr = '\0';
1166 if ((int) arg_no < 0)
1167 arg_no = arg_count;
1169 switch (ptr[-1])
1171 case 'd':
1172 case 'i':
1173 case 'o':
1174 case 'u':
1175 case 'x':
1176 case 'X':
1177 case 'c':
1179 /* Short values are promoted to int, so just copy it
1180 as an int and trust the C library printf to cast it
1181 to the right width. */
1182 if (short_width)
1183 PRINT_TYPE (int, i);
1184 else
1186 switch (wide_width)
1188 case 0:
1189 PRINT_TYPE (int, i);
1190 break;
1191 case 1:
1192 PRINT_TYPE (long, l);
1193 break;
1194 case 2:
1195 default:
1196 #if defined (__MSVCRT__)
1197 sptr[-3] = 'I';
1198 sptr[-2] = '6';
1199 sptr[-1] = '4';
1200 *sptr++ = ptr[-1];
1201 *sptr = '\0';
1202 #endif
1203 PRINT_TYPE (long long, ll);
1204 break;
1208 break;
1209 case 'f':
1210 case 'e':
1211 case 'E':
1212 case 'g':
1213 case 'G':
1215 if (wide_width == 0)
1216 PRINT_TYPE (double, d);
1217 else
1218 PRINT_TYPE (long double, ld);
1220 break;
1221 case 's':
1222 PRINT_TYPE (char *, p);
1223 break;
1224 case 'p':
1225 if (*ptr == 'A')
1227 asection *sec;
1228 bfd *abfd;
1229 const char *group = NULL;
1230 struct coff_comdat_info *ci;
1232 ptr++;
1233 sec = (asection *) args[arg_no].p;
1234 if (sec == NULL)
1235 /* Invoking %pA with a null section pointer is an
1236 internal error. */
1237 abort ();
1238 abfd = sec->owner;
1239 if (abfd != NULL
1240 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1241 && elf_next_in_group (sec) != NULL
1242 && (sec->flags & SEC_GROUP) == 0)
1243 group = elf_group_name (sec);
1244 else if (abfd != NULL
1245 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
1246 && (ci = bfd_coff_get_comdat_section (sec->owner,
1247 sec)) != NULL)
1248 group = ci->name;
1249 if (group != NULL)
1250 result = print (stream, "%s[%s]", sec->name, group);
1251 else
1252 result = print (stream, "%s", sec->name);
1254 else if (*ptr == 'B')
1256 bfd *abfd;
1258 ptr++;
1259 abfd = (bfd *) args[arg_no].p;
1260 if (abfd == NULL)
1261 /* Invoking %pB with a null bfd pointer is an
1262 internal error. */
1263 abort ();
1264 else if (abfd->my_archive
1265 && !bfd_is_thin_archive (abfd->my_archive))
1266 result = print (stream, "%s(%s)",
1267 bfd_get_filename (abfd->my_archive),
1268 bfd_get_filename (abfd));
1269 else
1270 result = print (stream, "%s", bfd_get_filename (abfd));
1272 else
1273 PRINT_TYPE (void *, p);
1274 break;
1275 default:
1276 abort();
1278 arg_count++;
1280 if (result == -1)
1281 return -1;
1282 total_printed += result;
1285 return total_printed;
1288 /* First pass over FORMAT to gather ARGS. Returns number of args. */
1290 static unsigned int
1291 _bfd_doprnt_scan (const char *format, va_list ap, union _bfd_doprnt_args *args)
1293 const char *ptr = format;
1294 unsigned int arg_count = 0;
1296 for (unsigned int i = 0; i < MAX_ARGS; i++)
1297 args[i].type = Bad;
1299 while (*ptr != '\0')
1301 if (*ptr != '%')
1303 ptr = strchr (ptr, '%');
1304 if (ptr == NULL)
1305 break;
1307 else if (ptr[1] == '%')
1308 ptr += 2;
1309 else
1311 int wide_width = 0, short_width = 0;
1312 unsigned int arg_no;
1313 int arg_type;
1315 ptr++;
1317 /* Check for a positional parameter. */
1318 arg_no = -1u;
1319 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1321 arg_no = *ptr - '1';
1322 ptr += 2;
1325 /* Move past flags. */
1326 while (strchr ("-+ #0'I", *ptr))
1327 ptr++;
1329 if (*ptr == '*')
1331 unsigned int arg_index;
1333 ptr++;
1334 arg_index = arg_count;
1335 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1337 arg_index = *ptr - '1';
1338 ptr += 2;
1340 if (arg_index >= MAX_ARGS)
1341 abort ();
1342 args[arg_index].type = Int;
1343 arg_count++;
1345 else
1346 /* Handle explicit numeric value. */
1347 while (ISDIGIT (*ptr))
1348 ptr++;
1350 /* Precision. */
1351 if (*ptr == '.')
1353 ptr++;
1354 if (*ptr == '*')
1356 unsigned int arg_index;
1358 ptr++;
1359 arg_index = arg_count;
1360 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1362 arg_index = *ptr - '1';
1363 ptr += 2;
1365 if (arg_index >= MAX_ARGS)
1366 abort ();
1367 args[arg_index].type = Int;
1368 arg_count++;
1370 else
1371 /* Handle explicit numeric value. */
1372 while (ISDIGIT (*ptr))
1373 ptr++;
1375 while (strchr ("hlL", *ptr))
1377 switch (*ptr)
1379 case 'h':
1380 short_width = 1;
1381 break;
1382 case 'l':
1383 wide_width++;
1384 break;
1385 case 'L':
1386 wide_width = 2;
1387 break;
1388 default:
1389 abort();
1391 ptr++;
1394 ptr++;
1395 if ((int) arg_no < 0)
1396 arg_no = arg_count;
1398 arg_type = Bad;
1399 switch (ptr[-1])
1401 case 'd':
1402 case 'i':
1403 case 'o':
1404 case 'u':
1405 case 'x':
1406 case 'X':
1407 case 'c':
1409 if (short_width)
1410 arg_type = Int;
1411 else
1413 switch (wide_width)
1415 case 0:
1416 arg_type = Int;
1417 break;
1418 case 1:
1419 arg_type = Long;
1420 break;
1421 case 2:
1422 default:
1423 arg_type = LongLong;
1424 break;
1428 break;
1429 case 'f':
1430 case 'e':
1431 case 'E':
1432 case 'g':
1433 case 'G':
1435 if (wide_width == 0)
1436 arg_type = Double;
1437 else
1438 arg_type = LongDouble;
1440 break;
1441 case 's':
1442 arg_type = Ptr;
1443 break;
1444 case 'p':
1445 if (*ptr == 'A' || *ptr == 'B')
1446 ptr++;
1447 arg_type = Ptr;
1448 break;
1449 default:
1450 abort();
1453 if (arg_no >= MAX_ARGS)
1454 abort ();
1455 args[arg_no].type = arg_type;
1456 arg_count++;
1460 for (unsigned int i = 0; i < arg_count; i++)
1462 switch (args[i].type)
1464 case Int:
1465 args[i].i = va_arg (ap, int);
1466 break;
1467 case Long:
1468 args[i].l = va_arg (ap, long);
1469 break;
1470 case LongLong:
1471 args[i].ll = va_arg (ap, long long);
1472 break;
1473 case Double:
1474 args[i].d = va_arg (ap, double);
1475 break;
1476 case LongDouble:
1477 args[i].ld = va_arg (ap, long double);
1478 break;
1479 case Ptr:
1480 args[i].p = va_arg (ap, void *);
1481 break;
1482 default:
1483 abort ();
1487 return arg_count;
1490 static void
1491 _bfd_print (bfd_print_callback print_func, void *stream,
1492 const char *fmt, va_list ap)
1494 union _bfd_doprnt_args args[MAX_ARGS];
1496 _bfd_doprnt_scan (fmt, ap, args);
1497 _bfd_doprnt (print_func, stream, fmt, args);
1501 FUNCTION
1502 bfd_print_error
1504 SYNOPSIS
1505 void bfd_print_error (bfd_print_callback print_func,
1506 void *stream, const char *fmt, va_list ap);
1508 DESCRIPTION
1510 This formats FMT and AP according to BFD "printf" rules,
1511 sending the output to STREAM by repeated calls to PRINT_FUNC.
1512 PRINT_FUNC is a printf-like function; it does not need to
1513 implement the BFD printf format extensions. This can be used
1514 in a callback that is set via bfd_set_error_handler to turn
1515 the error into ordinary output.
1518 void
1519 bfd_print_error (bfd_print_callback print_func, void *stream,
1520 const char *fmt, va_list ap)
1522 print_func (stream, "%s: ", _bfd_get_error_program_name ());
1523 _bfd_print (print_func, stream, fmt, ap);
1526 /* The standard error handler that prints to stderr. */
1528 static void
1529 error_handler_fprintf (const char *fmt, va_list ap)
1531 /* PR 4992: Don't interrupt output being sent to stdout. */
1532 fflush (stdout);
1534 bfd_print_error ((bfd_print_callback) fprintf, stderr, fmt, ap);
1536 /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
1537 warning, so use the fputc function to avoid it. */
1538 fputc ('\n', stderr);
1539 fflush (stderr);
1542 /* Control printing to a string buffer. */
1543 struct buf_stream
1545 char *ptr;
1546 int left;
1549 /* An fprintf like function that instead prints to a string buffer. */
1551 static int
1552 err_sprintf (void *stream, const char *fmt, ...)
1554 struct buf_stream *s = stream;
1555 va_list ap;
1557 va_start (ap, fmt);
1558 int total = vsnprintf (s->ptr, s->left, fmt, ap);
1559 va_end (ap);
1560 if (total < 0)
1562 else if (total > s->left)
1564 s->ptr += s->left;
1565 s->left = 0;
1567 else
1569 s->ptr += total;
1570 s->left -= total;
1572 return total;
1576 INTERNAL
1577 .{* Cached _bfd_check_format messages are put in this. *}
1578 .struct per_xvec_message
1580 . struct per_xvec_message *next;
1581 . char message[];
1584 .{* A list of per_xvec_message objects. The targ field indicates
1585 . which xvec this list holds; PER_XVEC_NO_TARGET is only set for the
1586 . root of the list and indicates that the entry isn't yet used. The
1587 . abfd field is only needed in the root entry of the list. *}
1588 .struct per_xvec_messages
1590 . bfd *abfd;
1591 . const bfd_target *targ;
1592 . struct per_xvec_message *messages;
1593 . struct per_xvec_messages *next;
1596 .#define PER_XVEC_NO_TARGET ((const bfd_target *) -1)
1599 /* Helper function to find or allocate the correct per-xvec object
1600 when emitting a message. */
1602 static struct per_xvec_message *
1603 _bfd_per_xvec_warn (struct per_xvec_messages *messages, size_t alloc)
1605 const bfd_target *targ = messages->abfd->xvec;
1607 struct per_xvec_messages *prev = NULL;
1608 struct per_xvec_messages *iter = messages;
1610 if (iter->targ == PER_XVEC_NO_TARGET)
1611 iter->targ = targ;
1612 else
1613 for (; iter != NULL; iter = iter->next)
1615 if (iter->targ == targ)
1616 break;
1617 prev = iter;
1620 if (iter == NULL)
1622 iter = bfd_malloc (sizeof (*iter));
1623 if (iter == NULL)
1624 return NULL;
1625 iter->abfd = messages->abfd;
1626 iter->targ = targ;
1627 iter->messages = NULL;
1628 iter->next = NULL;
1629 prev->next = iter;
1632 struct per_xvec_message **m = &iter->messages;
1633 int count = 0;
1634 while (*m)
1636 m = &(*m)->next;
1637 count++;
1639 /* Anti-fuzzer measure. Don't cache more than 5 messages. */
1640 if (count < 5)
1642 *m = bfd_malloc (sizeof (**m) + alloc);
1643 if (*m != NULL)
1644 (*m)->next = NULL;
1646 return *m;
1649 /* Communicate the error-message container processed by
1650 bfd_check_format_matches to the error handling function
1651 error_handler_sprintf. When non-NULL, _bfd_error_handler will call
1652 error_handler_sprintf; when NULL, _bfd_error_internal will be used
1653 instead. */
1655 static TLS struct per_xvec_messages *error_handler_messages;
1657 /* A special value for error_handler_messages that indicates that the
1658 error should simply be ignored. */
1659 #define IGNORE_ERROR_MESSAGES ((struct per_xvec_messages *) -1)
1661 /* An error handler that prints to a string, then dups that string to
1662 a per-xvec cache. */
1664 static void
1665 error_handler_sprintf (const char *fmt, va_list ap)
1667 char error_buf[1024];
1668 struct buf_stream error_stream;
1670 error_stream.ptr = error_buf;
1671 error_stream.left = sizeof (error_buf);
1673 _bfd_print (err_sprintf, &error_stream, fmt, ap);
1675 size_t len = error_stream.ptr - error_buf;
1676 struct per_xvec_message *warn
1677 = _bfd_per_xvec_warn (error_handler_messages, len + 1);
1678 if (warn)
1680 memcpy (warn->message, error_buf, len);
1681 warn->message[len] = 0;
1685 /* This is a function pointer to the routine which should handle BFD
1686 error messages. It is called when a BFD routine encounters an
1687 error for which it wants to print a message. Going through a
1688 function pointer permits a program linked against BFD to intercept
1689 the messages and deal with them itself. */
1691 static bfd_error_handler_type _bfd_error_internal = error_handler_fprintf;
1694 FUNCTION
1695 _bfd_error_handler
1697 SYNOPSIS
1698 void _bfd_error_handler (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
1700 DESCRIPTION
1701 This is the default routine to handle BFD error messages.
1702 Like fprintf (stderr, ...), but also handles some extra format
1703 specifiers.
1705 %pA section name from section. For group components, prints
1706 group name too.
1707 %pB file name from bfd. For archive components, prints
1708 archive too.
1710 Beware: Only supports a maximum of 9 format arguments.
1713 void
1714 _bfd_error_handler (const char *fmt, ...)
1716 va_list ap;
1718 va_start (ap, fmt);
1719 if (error_handler_messages == IGNORE_ERROR_MESSAGES)
1721 /* Nothing. */
1723 else if (error_handler_messages != NULL)
1724 error_handler_sprintf (fmt, ap);
1725 else
1726 _bfd_error_internal (fmt, ap);
1727 va_end (ap);
1731 FUNCTION
1732 bfd_set_error_handler
1734 SYNOPSIS
1735 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
1737 DESCRIPTION
1738 Set the BFD error handler function. Returns the previous
1739 function.
1742 bfd_error_handler_type
1743 bfd_set_error_handler (bfd_error_handler_type pnew)
1745 bfd_error_handler_type pold;
1747 pold = _bfd_error_internal;
1748 _bfd_error_internal = pnew;
1749 return pold;
1753 INTERNAL_FUNCTION
1754 _bfd_set_error_handler_caching
1756 SYNOPSIS
1757 struct per_xvec_messages *_bfd_set_error_handler_caching (struct per_xvec_messages *);
1759 DESCRIPTION
1760 Set the BFD error handler function to one that stores messages
1761 to the per_xvec_messages object. Returns the previous object
1762 to which messages are stored. Note that two sequential calls
1763 to this with a non-NULL argument will cause output to be
1764 dropped, rather than gathered.
1767 struct per_xvec_messages *
1768 _bfd_set_error_handler_caching (struct per_xvec_messages *messages)
1770 struct per_xvec_messages *old = error_handler_messages;
1771 if (old == NULL)
1772 error_handler_messages = messages;
1773 else
1774 error_handler_messages = IGNORE_ERROR_MESSAGES;
1775 return old;
1779 INTERNAL_FUNCTION
1780 _bfd_restore_error_handler_caching
1782 SYNOPSIS
1783 void _bfd_restore_error_handler_caching (struct per_xvec_messages *);
1785 DESCRIPTION
1786 Reset the BFD error handler object to an earlier value.
1789 void
1790 _bfd_restore_error_handler_caching (struct per_xvec_messages *old)
1792 error_handler_messages = old;
1796 FUNCTION
1797 bfd_set_error_program_name
1799 SYNOPSIS
1800 void bfd_set_error_program_name (const char *);
1802 DESCRIPTION
1803 Set the program name to use when printing a BFD error. This
1804 is printed before the error message followed by a colon and
1805 space. The string must not be changed after it is passed to
1806 this function.
1809 void
1810 bfd_set_error_program_name (const char *name)
1812 _bfd_error_program_name = name;
1816 INTERNAL_FUNCTION
1817 _bfd_get_error_program_name
1819 SYNOPSIS
1820 const char *_bfd_get_error_program_name (void);
1822 DESCRIPTION
1823 Get the program name used when printing a BFD error.
1826 const char *
1827 _bfd_get_error_program_name (void)
1829 if (_bfd_error_program_name != NULL)
1830 return _bfd_error_program_name;
1831 return "BFD";
1835 SUBSECTION
1836 BFD assert handler
1838 If BFD finds an internal inconsistency, the bfd assert
1839 handler is called with information on the BFD version, BFD
1840 source file and line. If this happens, most programs linked
1841 against BFD are expected to want to exit with an error, or mark
1842 the current BFD operation as failed, so it is recommended to
1843 override the default handler, which just calls
1844 _bfd_error_handler and continues.
1846 CODE_FRAGMENT
1847 .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
1848 . const char *bfd_version,
1849 . const char *bfd_file,
1850 . int bfd_line);
1854 /* Note the use of bfd_ prefix on the parameter names above: we want to
1855 show which one is the message and which is the version by naming the
1856 parameters, but avoid polluting the program-using-bfd namespace as
1857 the typedef is visible in the exported headers that the program
1858 includes. Below, it's just for consistency. */
1860 static void
1861 _bfd_default_assert_handler (const char *bfd_formatmsg,
1862 const char *bfd_version,
1863 const char *bfd_file,
1864 int bfd_line)
1867 _bfd_error_handler (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
1870 /* Similar to _bfd_error_handler, a program can decide to exit on an
1871 internal BFD error. We use a non-variadic type to simplify passing
1872 on parameters to other functions, e.g. _bfd_error_handler. */
1874 static bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
1877 FUNCTION
1878 bfd_set_assert_handler
1880 SYNOPSIS
1881 bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
1883 DESCRIPTION
1884 Set the BFD assert handler function. Returns the previous
1885 function.
1888 bfd_assert_handler_type
1889 bfd_set_assert_handler (bfd_assert_handler_type pnew)
1891 bfd_assert_handler_type pold;
1893 pold = _bfd_assert_handler;
1894 _bfd_assert_handler = pnew;
1895 return pold;
1899 INODE
1900 Initialization, Threading, Error reporting, BFD front end
1902 FUNCTION
1903 bfd_init
1905 SYNOPSIS
1906 unsigned int bfd_init (void);
1908 DESCRIPTION
1909 This routine must be called before any other BFD function to
1910 initialize magical internal data structures.
1911 Returns a magic number, which may be used to check
1912 that the bfd library is configured as expected by users.
1914 .{* Value returned by bfd_init. *}
1915 .#define BFD_INIT_MAGIC (sizeof (struct bfd_section))
1919 unsigned int
1920 bfd_init (void)
1922 bfd_error = bfd_error_no_error;
1923 input_bfd = NULL;
1924 _bfd_clear_error_data ();
1925 input_error = bfd_error_no_error;
1926 _bfd_error_internal = error_handler_fprintf;
1927 _bfd_assert_handler = _bfd_default_assert_handler;
1929 return BFD_INIT_MAGIC;
1934 INODE
1935 Threading, Miscellaneous, Initialization, BFD front end
1937 SECTION
1938 Threading
1940 BFD has limited support for thread-safety. Most BFD globals
1941 are protected by locks, while the error-related globals are
1942 thread-local. A given BFD cannot safely be used from two
1943 threads at the same time; it is up to the application to do
1944 any needed locking. However, it is ok for different threads
1945 to work on different BFD objects at the same time.
1947 SUBSECTION
1948 Thread functions.
1950 CODE_FRAGMENT
1951 .typedef bool (*bfd_lock_unlock_fn_type) (void *);
1954 /* The lock and unlock functions, if set. */
1955 static bfd_lock_unlock_fn_type lock_fn;
1956 static bfd_lock_unlock_fn_type unlock_fn;
1957 static void *lock_data;
1960 FUNCTION
1961 bfd_thread_init
1963 SYNOPSIS
1964 bool bfd_thread_init
1965 (bfd_lock_unlock_fn_type lock,
1966 bfd_lock_unlock_fn_type unlock,
1967 void *data);
1969 DESCRIPTION
1971 Initialize BFD threading. The functions passed in will be
1972 used to lock and unlock global data structures. This may only
1973 be called a single time in a given process. Returns true on
1974 success and false on error. DATA is passed verbatim to the
1975 lock and unlock functions. The lock and unlock functions
1976 should return true on success, or set the BFD error and return
1977 false on failure.
1980 bool
1981 bfd_thread_init (bfd_lock_unlock_fn_type lock, bfd_lock_unlock_fn_type unlock,
1982 void *data)
1984 /* Both functions must be set, and this cannot have been called
1985 before. */
1986 if (lock == NULL || unlock == NULL || unlock_fn != NULL)
1988 bfd_set_error (bfd_error_invalid_operation);
1989 return false;
1992 lock_fn = lock;
1993 unlock_fn = unlock;
1994 lock_data = data;
1995 return true;
1999 FUNCTION
2000 bfd_thread_cleanup
2002 SYNOPSIS
2003 void bfd_thread_cleanup (void);
2005 DESCRIPTION
2006 Clean up any thread-local state. This should be called by a
2007 thread that uses any BFD functions, before the thread exits.
2008 It is fine to call this multiple times, or to call it and then
2009 later call BFD functions on the same thread again.
2012 void
2013 bfd_thread_cleanup (void)
2015 _bfd_clear_error_data ();
2019 INTERNAL_FUNCTION
2020 bfd_lock
2022 SYNOPSIS
2023 bool bfd_lock (void);
2025 DESCRIPTION
2026 Acquire the global BFD lock, if needed. Returns true on
2027 success, false on error.
2030 bool
2031 bfd_lock (void)
2033 if (lock_fn != NULL)
2034 return lock_fn (lock_data);
2035 return true;
2039 INTERNAL_FUNCTION
2040 bfd_unlock
2042 SYNOPSIS
2043 bool bfd_unlock (void);
2045 DESCRIPTION
2046 Release the global BFD lock, if needed. Returns true on
2047 success, false on error.
2050 bool
2051 bfd_unlock (void)
2053 if (unlock_fn != NULL)
2054 return unlock_fn (lock_data);
2055 return true;
2060 INODE
2061 Miscellaneous, Memory Usage, Threading, BFD front end
2063 SECTION
2064 Miscellaneous
2066 SUBSECTION
2067 Miscellaneous functions
2071 FUNCTION
2072 bfd_get_reloc_upper_bound
2074 SYNOPSIS
2075 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
2077 DESCRIPTION
2078 Return the number of bytes required to store the
2079 relocation information associated with section @var{sect}
2080 attached to bfd @var{abfd}. If an error occurs, return -1.
2084 long
2085 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2087 if (abfd->format != bfd_object)
2089 bfd_set_error (bfd_error_invalid_operation);
2090 return -1;
2093 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
2097 FUNCTION
2098 bfd_canonicalize_reloc
2100 SYNOPSIS
2101 long bfd_canonicalize_reloc
2102 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
2104 DESCRIPTION
2105 Call the back end associated with the open BFD
2106 @var{abfd} and translate the external form of the relocation
2107 information attached to @var{sec} into the internal canonical
2108 form. Place the table into memory at @var{loc}, which has
2109 been preallocated, usually by a call to
2110 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
2111 -1 on error.
2113 The @var{syms} table is also needed for horrible internal magic
2114 reasons.
2117 long
2118 bfd_canonicalize_reloc (bfd *abfd,
2119 sec_ptr asect,
2120 arelent **location,
2121 asymbol **symbols)
2123 if (abfd->format != bfd_object)
2125 bfd_set_error (bfd_error_invalid_operation);
2126 return -1;
2129 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
2130 (abfd, asect, location, symbols));
2134 FUNCTION
2135 bfd_set_reloc
2137 SYNOPSIS
2138 void bfd_set_reloc
2139 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
2141 DESCRIPTION
2142 Set the relocation pointer and count within
2143 section @var{sec} to the values @var{rel} and @var{count}.
2144 The argument @var{abfd} is ignored.
2146 .#define bfd_set_reloc(abfd, asect, location, count) \
2147 . BFD_SEND (abfd, _bfd_set_reloc, (abfd, asect, location, count))
2151 FUNCTION
2152 bfd_set_file_flags
2154 SYNOPSIS
2155 bool bfd_set_file_flags (bfd *abfd, flagword flags);
2157 DESCRIPTION
2158 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
2160 Possible errors are:
2161 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
2162 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
2163 o <<bfd_error_invalid_operation>> -
2164 The flag word contained a bit which was not applicable to the
2165 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
2166 on a BFD format which does not support demand paging.
2170 bool
2171 bfd_set_file_flags (bfd *abfd, flagword flags)
2173 if (abfd->format != bfd_object)
2175 bfd_set_error (bfd_error_wrong_format);
2176 return false;
2179 if (bfd_read_p (abfd))
2181 bfd_set_error (bfd_error_invalid_operation);
2182 return false;
2185 abfd->flags = flags;
2186 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
2188 bfd_set_error (bfd_error_invalid_operation);
2189 return false;
2192 return true;
2195 void
2196 bfd_assert (const char *file, int line)
2198 /* xgettext:c-format */
2199 (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
2200 BFD_VERSION_STRING, file, line);
2203 /* A more or less friendly abort message. In libbfd.h abort is
2204 defined to call this function. */
2206 void
2207 _bfd_abort (const char *file, int line, const char *fn)
2209 fflush (stdout);
2211 if (fn != NULL)
2212 fprintf (stderr, _("%s: BFD %s internal error, aborting at %s:%d in %s\n"),
2213 _bfd_get_error_program_name (), BFD_VERSION_STRING,
2214 file, line, fn);
2215 else
2216 fprintf (stderr, _("%s: BFD %s internal error, aborting at %s:%d\n"),
2217 _bfd_get_error_program_name (), BFD_VERSION_STRING,
2218 file, line);
2219 fprintf (stderr, _("Please report this bug.\n"));
2220 _exit (EXIT_FAILURE);
2224 FUNCTION
2225 bfd_get_arch_size
2227 SYNOPSIS
2228 int bfd_get_arch_size (bfd *abfd);
2230 DESCRIPTION
2231 Returns the normalized architecture address size, in bits, as
2232 determined by the object file's format. By normalized, we mean
2233 either 32 or 64. For ELF, this information is included in the
2234 header. Use bfd_arch_bits_per_address for number of bits in
2235 the architecture address.
2237 Returns the arch size in bits if known, <<-1>> otherwise.
2241 bfd_get_arch_size (bfd *abfd)
2243 if (abfd->xvec->flavour == bfd_target_elf_flavour)
2244 return get_elf_backend_data (abfd)->s->arch_size;
2246 return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
2250 FUNCTION
2251 bfd_get_sign_extend_vma
2253 SYNOPSIS
2254 int bfd_get_sign_extend_vma (bfd *abfd);
2256 DESCRIPTION
2257 Indicates if the target architecture "naturally" sign extends
2258 an address. Some architectures implicitly sign extend address
2259 values when they are converted to types larger than the size
2260 of an address. For instance, bfd_get_start_address() will
2261 return an address sign extended to fill a bfd_vma when this is
2262 the case.
2264 Returns <<1>> if the target architecture is known to sign
2265 extend addresses, <<0>> if the target architecture is known to
2266 not sign extend addresses, and <<-1>> otherwise.
2270 bfd_get_sign_extend_vma (bfd *abfd)
2272 const char *name;
2274 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2275 return get_elf_backend_data (abfd)->sign_extend_vma;
2277 name = bfd_get_target (abfd);
2279 /* Return a proper value for DJGPP & PE COFF.
2280 This function is required for DWARF2 support, but there is
2281 no place to store this information in the COFF back end.
2282 Should enough other COFF targets add support for DWARF2,
2283 a place will have to be found. Until then, this hack will do. */
2284 if (startswith (name, "coff-go32")
2285 || strcmp (name, "pe-i386") == 0
2286 || strcmp (name, "pei-i386") == 0
2287 || strcmp (name, "pe-x86-64") == 0
2288 || strcmp (name, "pei-x86-64") == 0
2289 || strcmp (name, "pe-aarch64-little") == 0
2290 || strcmp (name, "pei-aarch64-little") == 0
2291 || strcmp (name, "pe-arm-wince-little") == 0
2292 || strcmp (name, "pei-arm-wince-little") == 0
2293 || strcmp (name, "pei-loongarch64") == 0
2294 || strcmp (name, "pei-riscv64-little") == 0
2295 || strcmp (name, "aixcoff-rs6000") == 0
2296 || strcmp (name, "aix5coff64-rs6000") == 0)
2297 return 1;
2299 if (startswith (name, "mach-o"))
2300 return 0;
2302 bfd_set_error (bfd_error_wrong_format);
2303 return -1;
2307 FUNCTION
2308 bfd_set_start_address
2310 SYNOPSIS
2311 bool bfd_set_start_address (bfd *abfd, bfd_vma vma);
2313 DESCRIPTION
2314 Make @var{vma} the entry point of output BFD @var{abfd}.
2316 Returns <<TRUE>> on success, <<FALSE>> otherwise.
2319 bool
2320 bfd_set_start_address (bfd *abfd, bfd_vma vma)
2322 abfd->start_address = vma;
2323 return true;
2327 FUNCTION
2328 bfd_get_gp_size
2330 SYNOPSIS
2331 unsigned int bfd_get_gp_size (bfd *abfd);
2333 DESCRIPTION
2334 Return the maximum size of objects to be optimized using the GP
2335 register under MIPS ECOFF. This is typically set by the <<-G>>
2336 argument to the compiler, assembler or linker.
2339 unsigned int
2340 bfd_get_gp_size (bfd *abfd)
2342 if (abfd->format == bfd_object)
2344 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2345 return ecoff_data (abfd)->gp_size;
2346 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2347 return elf_gp_size (abfd);
2349 return 0;
2353 FUNCTION
2354 bfd_set_gp_size
2356 SYNOPSIS
2357 void bfd_set_gp_size (bfd *abfd, unsigned int i);
2359 DESCRIPTION
2360 Set the maximum size of objects to be optimized using the GP
2361 register under ECOFF or MIPS ELF. This is typically set by
2362 the <<-G>> argument to the compiler, assembler or linker.
2365 void
2366 bfd_set_gp_size (bfd *abfd, unsigned int i)
2368 /* Don't try to set GP size on an archive or core file! */
2369 if (abfd->format != bfd_object)
2370 return;
2372 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2373 ecoff_data (abfd)->gp_size = i;
2374 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2375 elf_gp_size (abfd) = i;
2378 /* Get the GP value. This is an internal function used by some of the
2379 relocation special_function routines on targets which support a GP
2380 register. */
2382 bfd_vma
2383 _bfd_get_gp_value (bfd *abfd)
2385 if (! abfd)
2386 return 0;
2387 if (abfd->format != bfd_object)
2388 return 0;
2390 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2391 return ecoff_data (abfd)->gp;
2392 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2393 return elf_gp (abfd);
2395 return 0;
2398 /* Set the GP value. */
2400 void
2401 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
2403 if (! abfd)
2404 abort ();
2405 if (abfd->format != bfd_object)
2406 return;
2408 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2409 ecoff_data (abfd)->gp = v;
2410 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2411 elf_gp (abfd) = v;
2415 FUNCTION
2416 bfd_set_gp_value
2418 SYNOPSIS
2419 void bfd_set_gp_value (bfd *abfd, bfd_vma v);
2421 DESCRIPTION
2422 Allow external access to the fucntion to set the GP value.
2423 This is specifically added for gdb-compile support.
2426 void
2427 bfd_set_gp_value (bfd *abfd, bfd_vma v)
2429 _bfd_set_gp_value (abfd, v);
2433 FUNCTION
2434 bfd_scan_vma
2436 SYNOPSIS
2437 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
2439 DESCRIPTION
2440 Convert, like <<strtoul>> or <<stdtoull> depending on the size
2441 of a <<bfd_vma>>, a numerical expression @var{string} into a
2442 <<bfd_vma>> integer, and return that integer.
2445 bfd_vma
2446 bfd_scan_vma (const char *string, const char **end, int base)
2448 if (sizeof (bfd_vma) <= sizeof (unsigned long))
2449 return strtoul (string, (char **) end, base);
2451 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
2452 return strtoull (string, (char **) end, base);
2454 abort ();
2458 FUNCTION
2459 bfd_copy_private_header_data
2461 SYNOPSIS
2462 bool bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
2464 DESCRIPTION
2465 Copy private BFD header information from the BFD @var{ibfd} to the
2466 the BFD @var{obfd}. This copies information that may require
2467 sections to exist, but does not require symbol tables. Return
2468 <<true>> on success, <<false>> on error.
2469 Possible error returns are:
2471 o <<bfd_error_no_memory>> -
2472 Not enough memory exists to create private data for @var{obfd}.
2474 .#define bfd_copy_private_header_data(ibfd, obfd) \
2475 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
2476 . (ibfd, obfd))
2481 FUNCTION
2482 bfd_copy_private_bfd_data
2484 SYNOPSIS
2485 bool bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
2487 DESCRIPTION
2488 Copy private BFD information from the BFD @var{ibfd} to the
2489 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
2490 Possible error returns are:
2492 o <<bfd_error_no_memory>> -
2493 Not enough memory exists to create private data for @var{obfd}.
2495 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
2496 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
2497 . (ibfd, obfd))
2502 FUNCTION
2503 bfd_set_private_flags
2505 SYNOPSIS
2506 bool bfd_set_private_flags (bfd *abfd, flagword flags);
2508 DESCRIPTION
2509 Set private BFD flag information in the BFD @var{abfd}.
2510 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
2511 returns are:
2513 o <<bfd_error_no_memory>> -
2514 Not enough memory exists to create private data for @var{obfd}.
2516 .#define bfd_set_private_flags(abfd, flags) \
2517 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
2522 FUNCTION
2523 Other functions
2525 DESCRIPTION
2526 The following functions exist but have not yet been documented.
2528 .#define bfd_sizeof_headers(abfd, info) \
2529 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
2531 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
2532 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2533 . (abfd, syms, sec, off, file, func, line, NULL))
2535 .#define bfd_find_nearest_line_with_alt(abfd, alt_filename, sec, syms, off, \
2536 . file, func, line, disc) \
2537 . BFD_SEND (abfd, _bfd_find_nearest_line_with_alt, \
2538 . (abfd, alt_filename, syms, sec, off, file, func, line, disc))
2540 .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
2541 . line, disc) \
2542 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2543 . (abfd, syms, sec, off, file, func, line, disc))
2545 .#define bfd_find_line(abfd, syms, sym, file, line) \
2546 . BFD_SEND (abfd, _bfd_find_line, \
2547 . (abfd, syms, sym, file, line))
2549 .#define bfd_find_inliner_info(abfd, file, func, line) \
2550 . BFD_SEND (abfd, _bfd_find_inliner_info, \
2551 . (abfd, file, func, line))
2553 .#define bfd_debug_info_start(abfd) \
2554 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
2556 .#define bfd_debug_info_end(abfd) \
2557 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
2559 .#define bfd_debug_info_accumulate(abfd, section) \
2560 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
2562 .#define bfd_stat_arch_elt(abfd, stat) \
2563 . BFD_SEND (abfd->my_archive ? abfd->my_archive : abfd, \
2564 . _bfd_stat_arch_elt, (abfd, stat))
2566 .#define bfd_update_armap_timestamp(abfd) \
2567 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
2569 .#define bfd_set_arch_mach(abfd, arch, mach)\
2570 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
2572 .#define bfd_relax_section(abfd, section, link_info, again) \
2573 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
2575 .#define bfd_gc_sections(abfd, link_info) \
2576 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
2578 .#define bfd_lookup_section_flags(link_info, flag_info, section) \
2579 . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
2581 .#define bfd_merge_sections(abfd, link_info) \
2582 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
2584 .#define bfd_is_group_section(abfd, sec) \
2585 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
2587 .#define bfd_group_name(abfd, sec) \
2588 . BFD_SEND (abfd, _bfd_group_name, (abfd, sec))
2590 .#define bfd_discard_group(abfd, sec) \
2591 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
2593 .#define bfd_link_hash_table_create(abfd) \
2594 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
2596 .#define bfd_link_add_symbols(abfd, info) \
2597 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
2599 .#define bfd_link_just_syms(abfd, sec, info) \
2600 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
2602 .#define bfd_final_link(abfd, info) \
2603 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
2605 .#define bfd_free_cached_info(abfd) \
2606 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
2608 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
2609 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
2611 .#define bfd_print_private_bfd_data(abfd, file)\
2612 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
2614 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
2615 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
2617 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
2618 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
2619 . dyncount, dynsyms, ret))
2621 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
2622 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
2624 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
2625 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
2630 FUNCTION
2631 bfd_get_relocated_section_contents
2633 SYNOPSIS
2634 bfd_byte *bfd_get_relocated_section_contents
2635 (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
2636 bool, asymbol **);
2638 DESCRIPTION
2639 Read and relocate the indirect link_order section, into DATA
2640 (if non-NULL) or to a malloc'd buffer. Return the buffer, or
2641 NULL on errors.
2644 bfd_byte *
2645 bfd_get_relocated_section_contents (bfd *abfd,
2646 struct bfd_link_info *link_info,
2647 struct bfd_link_order *link_order,
2648 bfd_byte *data,
2649 bool relocatable,
2650 asymbol **symbols)
2652 bfd *abfd2;
2653 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
2654 bfd_byte *, bool, asymbol **);
2656 if (link_order->type == bfd_indirect_link_order)
2658 abfd2 = link_order->u.indirect.section->owner;
2659 if (abfd2 == NULL)
2660 abfd2 = abfd;
2662 else
2663 abfd2 = abfd;
2665 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
2667 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
2671 FUNCTION
2672 bfd_record_phdr
2674 SYNOPSIS
2675 bool bfd_record_phdr
2676 (bfd *, unsigned long, bool, flagword, bool, bfd_vma,
2677 bool, bool, unsigned int, struct bfd_section **);
2679 DESCRIPTION
2680 Record information about an ELF program header.
2683 bool
2684 bfd_record_phdr (bfd *abfd,
2685 unsigned long type,
2686 bool flags_valid,
2687 flagword flags,
2688 bool at_valid,
2689 bfd_vma at, /* Bytes. */
2690 bool includes_filehdr,
2691 bool includes_phdrs,
2692 unsigned int count,
2693 asection **secs)
2695 struct elf_segment_map *m, **pm;
2696 size_t amt;
2697 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2699 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2700 return true;
2702 amt = sizeof (struct elf_segment_map);
2703 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
2704 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2705 if (m == NULL)
2706 return false;
2708 m->p_type = type;
2709 m->p_flags = flags;
2710 m->p_paddr = at * opb;
2711 m->p_flags_valid = flags_valid;
2712 m->p_paddr_valid = at_valid;
2713 m->includes_filehdr = includes_filehdr;
2714 m->includes_phdrs = includes_phdrs;
2715 m->count = count;
2716 if (count > 0)
2717 memcpy (m->sections, secs, count * sizeof (asection *));
2719 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
2721 *pm = m;
2723 return true;
2726 #ifdef BFD64
2727 /* Return true iff this target is 32-bit. */
2729 static bool
2730 is32bit (bfd *abfd)
2732 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2734 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2735 return bed->s->elfclass == ELFCLASS32;
2738 /* For non-ELF targets, use architecture information. */
2739 return bfd_arch_bits_per_address (abfd) <= 32;
2741 #endif
2744 FUNCTION
2745 bfd_sprintf_vma
2746 bfd_fprintf_vma
2748 SYNOPSIS
2749 void bfd_sprintf_vma (bfd *, char *, bfd_vma);
2750 void bfd_fprintf_vma (bfd *, void *, bfd_vma);
2752 DESCRIPTION
2753 bfd_sprintf_vma and bfd_fprintf_vma display an address in the
2754 target's address size.
2756 EXTERNAL
2757 .#define bfd_printf_vma(abfd,x) bfd_fprintf_vma (abfd, stdout, x)
2761 void
2762 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
2764 #ifdef BFD64
2765 if (!is32bit (abfd))
2767 sprintf (buf, "%016" PRIx64, (uint64_t) value);
2768 return;
2770 #endif
2771 sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
2774 void
2775 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
2777 #ifdef BFD64
2778 if (!is32bit (abfd))
2780 fprintf ((FILE *) stream, "%016" PRIx64, (uint64_t) value);
2781 return;
2783 #endif
2784 fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
2788 FUNCTION
2789 bfd_alt_mach_code
2791 SYNOPSIS
2792 bool bfd_alt_mach_code (bfd *abfd, int alternative);
2794 DESCRIPTION
2796 When more than one machine code number is available for the
2797 same machine type, this function can be used to switch between
2798 the preferred one (alternative == 0) and any others. Currently,
2799 only ELF supports this feature, with up to two alternate
2800 machine codes.
2803 bool
2804 bfd_alt_mach_code (bfd *abfd, int alternative)
2806 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2808 int code;
2810 switch (alternative)
2812 case 0:
2813 code = get_elf_backend_data (abfd)->elf_machine_code;
2814 break;
2816 case 1:
2817 code = get_elf_backend_data (abfd)->elf_machine_alt1;
2818 if (code == 0)
2819 return false;
2820 break;
2822 case 2:
2823 code = get_elf_backend_data (abfd)->elf_machine_alt2;
2824 if (code == 0)
2825 return false;
2826 break;
2828 default:
2829 return false;
2832 elf_elfheader (abfd)->e_machine = code;
2834 return true;
2837 return false;
2841 FUNCTION
2842 bfd_emul_get_maxpagesize
2844 SYNOPSIS
2845 bfd_vma bfd_emul_get_maxpagesize (const char *);
2847 DESCRIPTION
2848 Returns the maximum page size, in bytes, as determined by
2849 emulation.
2852 bfd_vma
2853 bfd_emul_get_maxpagesize (const char *emul)
2855 const bfd_target *target;
2857 target = bfd_find_target (emul, NULL);
2858 if (target != NULL
2859 && target->flavour == bfd_target_elf_flavour)
2860 return xvec_get_elf_backend_data (target)->maxpagesize;
2862 return 0;
2866 FUNCTION
2867 bfd_emul_get_commonpagesize
2869 SYNOPSIS
2870 bfd_vma bfd_emul_get_commonpagesize (const char *);
2872 DESCRIPTION
2873 Returns the common page size, in bytes, as determined by
2874 emulation.
2877 bfd_vma
2878 bfd_emul_get_commonpagesize (const char *emul)
2880 const bfd_target *target;
2882 target = bfd_find_target (emul, NULL);
2883 if (target != NULL
2884 && target->flavour == bfd_target_elf_flavour)
2886 const struct elf_backend_data *bed;
2888 bed = xvec_get_elf_backend_data (target);
2889 return bed->commonpagesize;
2891 return 0;
2895 FUNCTION
2896 bfd_demangle
2898 SYNOPSIS
2899 char *bfd_demangle (bfd *, const char *, int);
2901 DESCRIPTION
2902 Wrapper around cplus_demangle. Strips leading underscores and
2903 other such chars that would otherwise confuse the demangler.
2904 If passed a g++ v3 ABI mangled name, returns a buffer allocated
2905 with malloc holding the demangled name. Returns NULL otherwise
2906 and on memory alloc failure.
2909 char *
2910 bfd_demangle (bfd *abfd, const char *name, int options)
2912 char *res, *alloc;
2913 const char *pre, *suf;
2914 size_t pre_len;
2915 bool skip_lead;
2917 skip_lead = (abfd != NULL
2918 && *name != '\0'
2919 && bfd_get_symbol_leading_char (abfd) == *name);
2920 if (skip_lead)
2921 ++name;
2923 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
2924 or the MS PE format. These formats have a number of leading '.'s
2925 on at least some symbols, so we remove all dots to avoid
2926 confusing the demangler. */
2927 pre = name;
2928 while (*name == '.' || *name == '$')
2929 ++name;
2930 pre_len = name - pre;
2932 /* Strip off @plt and suchlike too. */
2933 alloc = NULL;
2934 suf = strchr (name, '@');
2935 if (suf != NULL)
2937 alloc = (char *) bfd_malloc (suf - name + 1);
2938 if (alloc == NULL)
2939 return NULL;
2940 memcpy (alloc, name, suf - name);
2941 alloc[suf - name] = '\0';
2942 name = alloc;
2945 res = cplus_demangle (name, options);
2947 free (alloc);
2949 if (res == NULL)
2951 if (skip_lead)
2953 size_t len = strlen (pre) + 1;
2954 alloc = (char *) bfd_malloc (len);
2955 if (alloc == NULL)
2956 return NULL;
2957 memcpy (alloc, pre, len);
2958 return alloc;
2960 return NULL;
2963 /* Put back any prefix or suffix. */
2964 if (pre_len != 0 || suf != NULL)
2966 size_t len;
2967 size_t suf_len;
2968 char *final;
2970 len = strlen (res);
2971 if (suf == NULL)
2972 suf = res + len;
2973 suf_len = strlen (suf) + 1;
2974 final = (char *) bfd_malloc (pre_len + len + suf_len);
2975 if (final != NULL)
2977 memcpy (final, pre, pre_len);
2978 memcpy (final + pre_len, res, len);
2979 memcpy (final + pre_len + len, suf, suf_len);
2981 free (res);
2982 res = final;
2985 return res;
2988 /* Get the linker information. */
2990 struct bfd_link_info *
2991 _bfd_get_link_info (bfd *abfd)
2993 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2994 return NULL;
2996 return elf_link_info (abfd);