]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/bfd.c
Sync top level configure and makefiles
[thirdparty/binutils-gdb.git] / bfd / bfd.c
1 /* Generic BFD library interface and support routines.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22 /*
23 INODE
24 typedef bfd, Error reporting, BFD front end, BFD front end
25
26 SECTION
27 <<typedef bfd>>
28
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.
32
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.
36
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;
47 .
48 .enum bfd_direction
49 . {
50 . no_direction = 0,
51 . read_direction = 1,
52 . write_direction = 2,
53 . both_direction = 3
54 . };
55 .
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 . };
63 .
64 .enum bfd_plugin_format
65 . {
66 . bfd_plugin_unknown = 0,
67 . bfd_plugin_yes = 1,
68 . bfd_plugin_no = 2
69 . };
70 .
71 .struct bfd_build_id
72 . {
73 . bfd_size_type size;
74 . bfd_byte data[1];
75 . };
76 .
77
78 CODE_FRAGMENT
79 .struct bfd
80 .{
81 . {* The filename the application opened the BFD with. *}
82 . const char *filename;
83 .
84 . {* A pointer to the target jump table. *}
85 . const struct bfd_target *xvec;
86 .
87 . {* The IOSTREAM, and corresponding IO vector that provide access
88 . to the file backing the BFD. *}
89 . void *iostream;
90 . const struct bfd_iovec *iovec;
91 .
92 . {* The caching routines use these to maintain a
93 . least-recently-used list of BFDs. *}
94 . struct bfd *lru_prev, *lru_next;
95 .
96 . {* Track current file position (or current buffer offset for
97 . in-memory BFDs). When a file is closed by the caching routines,
98 . BFD retains state information on the file here. *}
99 . ufile_ptr where;
100 .
101 . {* File modified time, if mtime_set is TRUE. *}
102 . long mtime;
103 .
104 . {* A unique identifier of the BFD *}
105 . unsigned int id;
106 .
107 . {* Format_specific flags. *}
108 . flagword flags;
109 .
110 . {* Values that may appear in the flags field of a BFD. These also
111 . appear in the object_flags field of the bfd_target structure, where
112 . they indicate the set of flags used by that backend (not all flags
113 . are meaningful for all object file formats) (FIXME: at the moment,
114 . the object_flags values have mostly just been copied from backend
115 . to another, and are not necessarily correct). *}
116 .
117 .#define BFD_NO_FLAGS 0x0
118 .
119 . {* BFD contains relocation entries. *}
120 .#define HAS_RELOC 0x1
121 .
122 . {* BFD is directly executable. *}
123 .#define EXEC_P 0x2
124 .
125 . {* BFD has line number information (basically used for F_LNNO in a
126 . COFF header). *}
127 .#define HAS_LINENO 0x4
128 .
129 . {* BFD has debugging information. *}
130 .#define HAS_DEBUG 0x08
131 .
132 . {* BFD has symbols. *}
133 .#define HAS_SYMS 0x10
134 .
135 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
136 . header). *}
137 .#define HAS_LOCALS 0x20
138 .
139 . {* BFD is a dynamic object. *}
140 .#define DYNAMIC 0x40
141 .
142 . {* Text section is write protected (if D_PAGED is not set, this is
143 . like an a.out NMAGIC file) (the linker sets this by default, but
144 . clears it for -r or -N). *}
145 .#define WP_TEXT 0x80
146 .
147 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
148 . linker sets this by default, but clears it for -r or -n or -N). *}
149 .#define D_PAGED 0x100
150 .
151 . {* BFD is relaxable (this means that bfd_relax_section may be able to
152 . do something) (sometimes bfd_relax_section can do something even if
153 . this is not set). *}
154 .#define BFD_IS_RELAXABLE 0x200
155 .
156 . {* This may be set before writing out a BFD to request using a
157 . traditional format. For example, this is used to request that when
158 . writing out an a.out object the symbols not be hashed to eliminate
159 . duplicates. *}
160 .#define BFD_TRADITIONAL_FORMAT 0x400
161 .
162 . {* This flag indicates that the BFD contents are actually cached
163 . in memory. If this is set, iostream points to a malloc'd
164 . bfd_in_memory struct. *}
165 .#define BFD_IN_MEMORY 0x800
166 .
167 . {* This BFD has been created by the linker and doesn't correspond
168 . to any input file. *}
169 .#define BFD_LINKER_CREATED 0x1000
170 .
171 . {* This may be set before writing out a BFD to request that it
172 . be written using values for UIDs, GIDs, timestamps, etc. that
173 . will be consistent from run to run. *}
174 .#define BFD_DETERMINISTIC_OUTPUT 0x2000
175 .
176 . {* Compress sections in this BFD. *}
177 .#define BFD_COMPRESS 0x4000
178 .
179 . {* Decompress sections in this BFD. *}
180 .#define BFD_DECOMPRESS 0x8000
181 .
182 . {* BFD is a dummy, for plugins. *}
183 .#define BFD_PLUGIN 0x10000
184 .
185 . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
186 .#define BFD_COMPRESS_GABI 0x20000
187 .
188 . {* Convert ELF common symbol type to STT_COMMON or STT_OBJECT in this
189 . BFD. *}
190 .#define BFD_CONVERT_ELF_COMMON 0x40000
191 .
192 . {* Use the ELF STT_COMMON type in this BFD. *}
193 .#define BFD_USE_ELF_STT_COMMON 0x80000
194 .
195 . {* Put pathnames into archives (non-POSIX). *}
196 .#define BFD_ARCHIVE_FULL_PATH 0x100000
197 .
198 .#define BFD_CLOSED_BY_CACHE 0x200000
199
200 . {* Compress sections in this BFD with SHF_COMPRESSED zstd. *}
201 .#define BFD_COMPRESS_ZSTD 0x400000
202 .
203 . {* Don't generate ELF section header. *}
204 .#define BFD_NO_SECTION_HEADER 0x800000
205 .
206 . {* Flags bits which are for BFD use only. *}
207 .#define BFD_FLAGS_FOR_BFD_USE_MASK \
208 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
209 . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
210 . | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON \
211 . | BFD_NO_SECTION_HEADER)
212 .
213 . {* The format which belongs to the BFD. (object, core, etc.) *}
214 . ENUM_BITFIELD (bfd_format) format : 3;
215 .
216 . {* The direction with which the BFD was opened. *}
217 . ENUM_BITFIELD (bfd_direction) direction : 2;
218 .
219 . {* POSIX.1-2017 (IEEE Std 1003.1) says of fopen : "When a file is
220 . opened with update mode ('+' as the second or third character in
221 . the mode argument), both input and output may be performed on
222 . the associated stream. However, the application shall ensure
223 . that output is not directly followed by input without an
224 . intervening call to fflush() or to a file positioning function
225 . (fseek(), fsetpos(), or rewind()), and input is not directly
226 . followed by output without an intervening call to a file
227 . positioning function, unless the input operation encounters
228 . end-of-file."
229 . This field tracks the last IO operation, so that bfd can insert
230 . a seek when IO direction changes. *}
231 . ENUM_BITFIELD (bfd_last_io) last_io : 2;
232 .
233 . {* Is the file descriptor being cached? That is, can it be closed as
234 . needed, and re-opened when accessed later? *}
235 . unsigned int cacheable : 1;
236 .
237 . {* Marks whether there was a default target specified when the
238 . BFD was opened. This is used to select which matching algorithm
239 . to use to choose the back end. *}
240 . unsigned int target_defaulted : 1;
241 .
242 . {* ... and here: (``once'' means at least once). *}
243 . unsigned int opened_once : 1;
244 .
245 . {* Set if we have a locally maintained mtime value, rather than
246 . getting it from the file each time. *}
247 . unsigned int mtime_set : 1;
248 .
249 . {* Flag set if symbols from this BFD should not be exported. *}
250 . unsigned int no_export : 1;
251 .
252 . {* Remember when output has begun, to stop strange things
253 . from happening. *}
254 . unsigned int output_has_begun : 1;
255 .
256 . {* Have archive map. *}
257 . unsigned int has_armap : 1;
258 .
259 . {* Set if this is a thin archive. *}
260 . unsigned int is_thin_archive : 1;
261 .
262 . {* Set if this archive should not cache element positions. *}
263 . unsigned int no_element_cache : 1;
264 .
265 . {* Set if only required symbols should be added in the link hash table for
266 . this object. Used by VMS linkers. *}
267 . unsigned int selective_search : 1;
268 .
269 . {* Set if this is the linker output BFD. *}
270 . unsigned int is_linker_output : 1;
271 .
272 . {* Set if this is the linker input BFD. *}
273 . unsigned int is_linker_input : 1;
274 .
275 . {* If this is an input for a compiler plug-in library. *}
276 . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
277 .
278 . {* Set if this is a plugin output file. *}
279 . unsigned int lto_output : 1;
280 .
281 . {* Set if this is a slim LTO object not loaded with a compiler plugin. *}
282 . unsigned int lto_slim_object : 1;
283 .
284 . {* Do not attempt to modify this file. Set when detecting errors
285 . that BFD is not prepared to handle for objcopy/strip. *}
286 . unsigned int read_only : 1;
287 .
288 . {* Set to dummy BFD created when claimed by a compiler plug-in
289 . library. *}
290 . bfd *plugin_dummy_bfd;
291 .
292 . {* The offset of this bfd in the file, typically 0 if it is not
293 . contained in an archive. *}
294 . ufile_ptr origin;
295 .
296 . {* The origin in the archive of the proxy entry. This will
297 . normally be the same as origin, except for thin archives,
298 . when it will contain the current offset of the proxy in the
299 . thin archive rather than the offset of the bfd in its actual
300 . container. *}
301 . ufile_ptr proxy_origin;
302 .
303 . {* A hash table for section names. *}
304 . struct bfd_hash_table section_htab;
305 .
306 . {* Pointer to linked list of sections. *}
307 . struct bfd_section *sections;
308 .
309 . {* The last section on the section list. *}
310 . struct bfd_section *section_last;
311 .
312 . {* The number of sections. *}
313 . unsigned int section_count;
314 .
315 . {* The archive plugin file descriptor. *}
316 . int archive_plugin_fd;
317 .
318 . {* The number of opens on the archive plugin file descriptor. *}
319 . unsigned int archive_plugin_fd_open_count;
320 .
321 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
322 . be used only for archive elements. *}
323 . int archive_pass;
324 .
325 . {* The total size of memory from bfd_alloc. *}
326 . bfd_size_type alloc_size;
327 .
328 . {* Stuff only useful for object files:
329 . The start address. *}
330 . bfd_vma start_address;
331 .
332 . {* Symbol table for output BFD (with symcount entries).
333 . Also used by the linker to cache input BFD symbols. *}
334 . struct bfd_symbol **outsymbols;
335 .
336 . {* Used for input and output. *}
337 . unsigned int symcount;
338 .
339 . {* Used for slurped dynamic symbol tables. *}
340 . unsigned int dynsymcount;
341 .
342 . {* Pointer to structure which contains architecture information. *}
343 . const struct bfd_arch_info *arch_info;
344 .
345 . {* Cached length of file for bfd_get_size. 0 until bfd_get_size is
346 . called, 1 if stat returns an error or the file size is too large to
347 . return in ufile_ptr. Both 0 and 1 should be treated as "unknown". *}
348 . ufile_ptr size;
349 .
350 . {* Stuff only useful for archives. *}
351 . void *arelt_data;
352 . struct bfd *my_archive; {* The containing archive BFD. *}
353 . struct bfd *archive_next; {* The next BFD in the archive. *}
354 . struct bfd *archive_head; {* The first BFD in the archive. *}
355 . struct bfd *nested_archives; {* List of nested archive in a flattened
356 . thin archive. *}
357 .
358 . union {
359 . {* For input BFDs, a chain of BFDs involved in a link. *}
360 . struct bfd *next;
361 . {* For output BFD, the linker hash table. *}
362 . struct bfd_link_hash_table *hash;
363 . } link;
364 .
365 . {* Used by the back end to hold private data. *}
366 . union
367 . {
368 . struct aout_data_struct *aout_data;
369 . struct artdata *aout_ar_data;
370 . struct coff_tdata *coff_obj_data;
371 . struct pe_tdata *pe_obj_data;
372 . struct xcoff_tdata *xcoff_obj_data;
373 . struct ecoff_tdata *ecoff_obj_data;
374 . struct srec_data_struct *srec_data;
375 . struct verilog_data_struct *verilog_data;
376 . struct ihex_data_struct *ihex_data;
377 . struct tekhex_data_struct *tekhex_data;
378 . struct elf_obj_tdata *elf_obj_data;
379 . struct mmo_data_struct *mmo_data;
380 . struct trad_core_struct *trad_core_data;
381 . struct som_data_struct *som_data;
382 . struct hpux_core_struct *hpux_core_data;
383 . struct hppabsd_core_struct *hppabsd_core_data;
384 . struct sgi_core_struct *sgi_core_data;
385 . struct lynx_core_struct *lynx_core_data;
386 . struct osf_core_struct *osf_core_data;
387 . struct cisco_core_struct *cisco_core_data;
388 . struct netbsd_core_struct *netbsd_core_data;
389 . struct mach_o_data_struct *mach_o_data;
390 . struct mach_o_fat_data_struct *mach_o_fat_data;
391 . struct plugin_data_struct *plugin_data;
392 . struct bfd_pef_data_struct *pef_data;
393 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
394 . struct bfd_sym_data_struct *sym_data;
395 . void *any;
396 . }
397 . tdata;
398 .
399 . {* Used by the application to hold private data. *}
400 . void *usrdata;
401 .
402 . {* Where all the allocated stuff under this BFD goes. This is a
403 . struct objalloc *, but we use void * to avoid requiring the inclusion
404 . of objalloc.h. *}
405 . void *memory;
406 .
407 . {* For input BFDs, the build ID, if the object has one. *}
408 . const struct bfd_build_id *build_id;
409 .};
410 .
411
412 EXTERNAL
413 .static inline const char *
414 .bfd_get_filename (const bfd *abfd)
415 .{
416 . return abfd->filename;
417 .}
418 .
419 .static inline bool
420 .bfd_get_cacheable (const bfd *abfd)
421 .{
422 . return abfd->cacheable;
423 .}
424 .
425 .static inline enum bfd_format
426 .bfd_get_format (const bfd *abfd)
427 .{
428 . return abfd->format;
429 .}
430 .
431 .static inline flagword
432 .bfd_get_file_flags (const bfd *abfd)
433 .{
434 . return abfd->flags;
435 .}
436 .
437 .static inline bfd_vma
438 .bfd_get_start_address (const bfd *abfd)
439 .{
440 . return abfd->start_address;
441 .}
442 .
443 .static inline unsigned int
444 .bfd_get_symcount (const bfd *abfd)
445 .{
446 . return abfd->symcount;
447 .}
448 .
449 .static inline unsigned int
450 .bfd_get_dynamic_symcount (const bfd *abfd)
451 .{
452 . return abfd->dynsymcount;
453 .}
454 .
455 .static inline struct bfd_symbol **
456 .bfd_get_outsymbols (const bfd *abfd)
457 .{
458 . return abfd->outsymbols;
459 .}
460 .
461 .static inline unsigned int
462 .bfd_count_sections (const bfd *abfd)
463 .{
464 . return abfd->section_count;
465 .}
466 .
467 .static inline bool
468 .bfd_has_map (const bfd *abfd)
469 .{
470 . return abfd->has_armap;
471 .}
472 .
473 .static inline bool
474 .bfd_is_thin_archive (const bfd *abfd)
475 .{
476 . return abfd->is_thin_archive;
477 .}
478 .
479 .static inline void *
480 .bfd_usrdata (const bfd *abfd)
481 .{
482 . return abfd->usrdata;
483 .}
484 .
485 .{* See note beside bfd_set_section_userdata. *}
486 .static inline bool
487 .bfd_set_cacheable (bfd * abfd, bool val)
488 .{
489 . abfd->cacheable = val;
490 . return true;
491 .}
492 .
493 .static inline void
494 .bfd_set_thin_archive (bfd *abfd, bool val)
495 .{
496 . abfd->is_thin_archive = val;
497 .}
498 .
499 .static inline void
500 .bfd_set_usrdata (bfd *abfd, void *val)
501 .{
502 . abfd->usrdata = val;
503 .}
504 .
505 .static inline asection *
506 .bfd_asymbol_section (const asymbol *sy)
507 .{
508 . return sy->section;
509 .}
510 .
511 .static inline bfd_vma
512 .bfd_asymbol_value (const asymbol *sy)
513 .{
514 . return sy->section->vma + sy->value;
515 .}
516 .
517 .static inline const char *
518 .bfd_asymbol_name (const asymbol *sy)
519 .{
520 . return sy->name;
521 .}
522 .
523 .static inline struct bfd *
524 .bfd_asymbol_bfd (const asymbol *sy)
525 .{
526 . return sy->the_bfd;
527 .}
528 .
529 .static inline void
530 .bfd_set_asymbol_name (asymbol *sy, const char *name)
531 .{
532 . sy->name = name;
533 .}
534 .
535 .{* For input sections return the original size on disk of the
536 . section. For output sections return the current size. *}
537 .static inline bfd_size_type
538 .bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
539 .{
540 . if (abfd->direction != write_direction && sec->rawsize != 0)
541 . return sec->rawsize;
542 . return sec->size;
543 .}
544 .
545 .{* Find the address one past the end of SEC. *}
546 .static inline bfd_size_type
547 .bfd_get_section_limit (const bfd *abfd, const asection *sec)
548 .{
549 . return (bfd_get_section_limit_octets (abfd, sec)
550 . / bfd_octets_per_byte (abfd, sec));
551 .}
552 .
553 .{* For input sections return the larger of the current size and the
554 . original size on disk of the section. For output sections return
555 . the current size. *}
556 .static inline bfd_size_type
557 .bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
558 .{
559 . if (abfd->direction != write_direction && sec->rawsize > sec->size)
560 . return sec->rawsize;
561 . return sec->size;
562 .}
563 .
564 .{* Functions to handle insertion and deletion of a bfd's sections. These
565 . only handle the list pointers, ie. do not adjust section_count,
566 . target_index etc. *}
567 .static inline void
568 .bfd_section_list_remove (bfd *abfd, asection *s)
569 .{
570 . asection *next = s->next;
571 . asection *prev = s->prev;
572 . if (prev)
573 . prev->next = next;
574 . else
575 . abfd->sections = next;
576 . if (next)
577 . next->prev = prev;
578 . else
579 . abfd->section_last = prev;
580 .}
581 .
582 .static inline void
583 .bfd_section_list_append (bfd *abfd, asection *s)
584 .{
585 . s->next = 0;
586 . if (abfd->section_last)
587 . {
588 . s->prev = abfd->section_last;
589 . abfd->section_last->next = s;
590 . }
591 . else
592 . {
593 . s->prev = 0;
594 . abfd->sections = s;
595 . }
596 . abfd->section_last = s;
597 .}
598 .
599 .static inline void
600 .bfd_section_list_prepend (bfd *abfd, asection *s)
601 .{
602 . s->prev = 0;
603 . if (abfd->sections)
604 . {
605 . s->next = abfd->sections;
606 . abfd->sections->prev = s;
607 . }
608 . else
609 . {
610 . s->next = 0;
611 . abfd->section_last = s;
612 . }
613 . abfd->sections = s;
614 .}
615 .
616 .static inline void
617 .bfd_section_list_insert_after (bfd *abfd, asection *a, asection *s)
618 .{
619 . asection *next = a->next;
620 . s->next = next;
621 . s->prev = a;
622 . a->next = s;
623 . if (next)
624 . next->prev = s;
625 . else
626 . abfd->section_last = s;
627 .}
628 .
629 .static inline void
630 .bfd_section_list_insert_before (bfd *abfd, asection *b, asection *s)
631 .{
632 . asection *prev = b->prev;
633 . s->prev = prev;
634 . s->next = b;
635 . b->prev = s;
636 . if (prev)
637 . prev->next = s;
638 . else
639 . abfd->sections = s;
640 .}
641 .
642 .static inline bool
643 .bfd_section_removed_from_list (const bfd *abfd, const asection *s)
644 .{
645 . return s->next ? s->next->prev != s : abfd->section_last != s;
646 .}
647 .
648 */
649
650 #include "sysdep.h"
651 #include <stdarg.h>
652 #include "bfd.h"
653 #include "bfdver.h"
654 #include "libiberty.h"
655 #include "demangle.h"
656 #include "safe-ctype.h"
657 #include "bfdlink.h"
658 #include "libbfd.h"
659 #include "coff/internal.h"
660 #include "coff/sym.h"
661 #include "libcoff.h"
662 #include "libecoff.h"
663 #undef obj_symbols
664 #include "elf-bfd.h"
665
666 #ifndef EXIT_FAILURE
667 #define EXIT_FAILURE 1
668 #endif
669
670 \f
671 /* provide storage for subsystem, stack and heap data which may have been
672 passed in on the command line. Ld puts this data into a bfd_link_info
673 struct which ultimately gets passed in to the bfd. When it arrives, copy
674 it to the following struct so that the data will be available in coffcode.h
675 where it is needed. The typedef's used are defined in bfd.h */
676 \f
677 /*
678 INODE
679 Error reporting, Initialization, typedef bfd, BFD front end
680
681 SECTION
682 Error reporting
683
684 Most BFD functions return nonzero on success (check their
685 individual documentation for precise semantics). On an error,
686 they call <<bfd_set_error>> to set an error condition that callers
687 can check by calling <<bfd_get_error>>.
688 If that returns <<bfd_error_system_call>>, then check
689 <<errno>>.
690
691 The easiest way to report a BFD error to the user is to
692 use <<bfd_perror>>.
693
694 The BFD error is thread-local.
695
696 SUBSECTION
697 Type <<bfd_error_type>>
698
699 The values returned by <<bfd_get_error>> are defined by the
700 enumerated type <<bfd_error_type>>.
701
702 CODE_FRAGMENT
703 .typedef enum bfd_error
704 .{
705 . bfd_error_no_error = 0,
706 . bfd_error_system_call,
707 . bfd_error_invalid_target,
708 . bfd_error_wrong_format,
709 . bfd_error_wrong_object_format,
710 . bfd_error_invalid_operation,
711 . bfd_error_no_memory,
712 . bfd_error_no_symbols,
713 . bfd_error_no_armap,
714 . bfd_error_no_more_archived_files,
715 . bfd_error_malformed_archive,
716 . bfd_error_missing_dso,
717 . bfd_error_file_not_recognized,
718 . bfd_error_file_ambiguously_recognized,
719 . bfd_error_no_contents,
720 . bfd_error_nonrepresentable_section,
721 . bfd_error_no_debug_section,
722 . bfd_error_bad_value,
723 . bfd_error_file_truncated,
724 . bfd_error_file_too_big,
725 . bfd_error_sorry,
726 . bfd_error_on_input,
727 . bfd_error_invalid_error_code
728 .}
729 .bfd_error_type;
730 .
731 */
732
733 static TLS bfd_error_type bfd_error;
734 static TLS bfd_error_type input_error;
735 static TLS bfd *input_bfd;
736 static TLS char *_bfd_error_buf;
737
738 const char *const bfd_errmsgs[] =
739 {
740 N_("no error"),
741 N_("system call error"),
742 N_("invalid bfd target"),
743 N_("file in wrong format"),
744 N_("archive object file in wrong format"),
745 N_("invalid operation"),
746 N_("memory exhausted"),
747 N_("no symbols"),
748 N_("archive has no index; run ranlib to add one"),
749 N_("no more archived files"),
750 N_("malformed archive"),
751 N_("DSO missing from command line"),
752 N_("file format not recognized"),
753 N_("file format is ambiguous"),
754 N_("section has no contents"),
755 N_("nonrepresentable section on output"),
756 N_("symbol needs debug section which does not exist"),
757 N_("bad value"),
758 N_("file truncated"),
759 N_("file too big"),
760 N_("sorry, cannot handle this file"),
761 N_("error reading %s: %s"),
762 N_("#<invalid error code>")
763 };
764
765 /*
766 FUNCTION
767 bfd_get_error
768
769 SYNOPSIS
770 bfd_error_type bfd_get_error (void);
771
772 DESCRIPTION
773 Return the current BFD error condition.
774 */
775
776 bfd_error_type
777 bfd_get_error (void)
778 {
779 return bfd_error;
780 }
781
782 /*
783 FUNCTION
784 bfd_set_error
785
786 SYNOPSIS
787 void bfd_set_error (bfd_error_type error_tag);
788
789 DESCRIPTION
790 Set the BFD error condition to be @var{error_tag}.
791
792 @var{error_tag} must not be bfd_error_on_input. Use
793 bfd_set_input_error for input errors instead.
794 */
795
796 void
797 bfd_set_error (bfd_error_type error_tag)
798 {
799 bfd_error = error_tag;
800 if (bfd_error >= bfd_error_on_input)
801 abort ();
802 }
803
804 /*
805 FUNCTION
806 bfd_set_input_error
807
808 SYNOPSIS
809 void bfd_set_input_error (bfd *input, bfd_error_type error_tag);
810
811 DESCRIPTION
812
813 Set the BFD error condition to be bfd_error_on_input.
814 @var{input} is the input bfd where the error occurred, and
815 @var{error_tag} the bfd_error_type error.
816 */
817
818 void
819 bfd_set_input_error (bfd *input, bfd_error_type error_tag)
820 {
821 /* This is an error that occurred during bfd_close when writing an
822 archive, but on one of the input files. */
823 bfd_error = bfd_error_on_input;
824 _bfd_clear_error_data ();
825 input_bfd = input;
826 input_error = error_tag;
827 if (input_error >= bfd_error_on_input)
828 abort ();
829 }
830
831 /*
832 FUNCTION
833 bfd_errmsg
834
835 SYNOPSIS
836 const char *bfd_errmsg (bfd_error_type error_tag);
837
838 DESCRIPTION
839 Return a string describing the error @var{error_tag}, or
840 the system error if @var{error_tag} is <<bfd_error_system_call>>.
841 */
842
843 const char *
844 bfd_errmsg (bfd_error_type error_tag)
845 {
846 #ifndef errno
847 extern int errno;
848 #endif
849 if (error_tag == bfd_error_on_input)
850 {
851 const char *msg = bfd_errmsg (input_error);
852 char *ret = bfd_asprintf (_(bfd_errmsgs[error_tag]),
853 bfd_get_filename (input_bfd), msg);
854 if (ret)
855 return ret;
856
857 /* Ick, what to do on out of memory? */
858 return msg;
859 }
860
861 if (error_tag == bfd_error_system_call)
862 return xstrerror (errno);
863
864 if (error_tag > bfd_error_invalid_error_code)
865 error_tag = bfd_error_invalid_error_code; /* sanity check */
866
867 return _(bfd_errmsgs[error_tag]);
868 }
869
870 /*
871 FUNCTION
872 bfd_perror
873
874 SYNOPSIS
875 void bfd_perror (const char *message);
876
877 DESCRIPTION
878 Print to the standard error stream a string describing the
879 last BFD error that occurred, or the last system error if
880 the last BFD error was a system call failure. If @var{message}
881 is non-NULL and non-empty, the error string printed is preceded
882 by @var{message}, a colon, and a space. It is followed by a newline.
883 */
884
885 void
886 bfd_perror (const char *message)
887 {
888 fflush (stdout);
889 if (message == NULL || *message == '\0')
890 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
891 else
892 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
893 fflush (stderr);
894 }
895
896 /*
897 INTERNAL_FUNCTION
898 _bfd_clear_error_data
899
900 SYNOPSIS
901 void _bfd_clear_error_data (void);
902
903 DESCRIPTION
904 Free any data associated with the BFD error.
905 */
906
907 void
908 _bfd_clear_error_data (void)
909 {
910 free (_bfd_error_buf);
911 _bfd_error_buf = NULL;
912 }
913
914 /*
915 INTERNAL_FUNCTION
916 bfd_asprintf
917
918 SYNOPSIS
919 char *bfd_asprintf (const char *fmt, ...);
920
921 DESCRIPTION
922 Primarily for error reporting, this function is like
923 libiberty's xasprintf except that it can return NULL on no
924 memory and the returned string should not be freed. Uses a
925 thread-local malloc'd buffer managed by libbfd, _bfd_error_buf.
926 Be aware that a call to this function frees the result of any
927 previous call. bfd_errmsg (bfd_error_on_input) also calls
928 this function.
929 */
930
931 char *
932 bfd_asprintf (const char *fmt, ...)
933 {
934 free (_bfd_error_buf);
935 _bfd_error_buf = NULL;
936 va_list ap;
937 va_start (ap, fmt);
938 int count = vasprintf (&_bfd_error_buf, fmt, ap);
939 va_end (ap);
940 if (count == -1)
941 {
942 bfd_set_error (bfd_error_no_memory);
943 _bfd_error_buf = NULL;
944 }
945 return _bfd_error_buf;
946 }
947
948 /*
949 SUBSECTION
950 BFD error handler
951
952 Some BFD functions want to print messages describing the
953 problem. They call a BFD error handler function. This
954 function may be overridden by the program.
955
956 The BFD error handler acts like vprintf.
957
958 CODE_FRAGMENT
959 .typedef void (*bfd_error_handler_type) (const char *, va_list);
960 .
961 */
962
963 /* The program name used when printing BFD error messages. */
964
965 static const char *_bfd_error_program_name;
966
967 /* Support for positional parameters. */
968
969 union _bfd_doprnt_args
970 {
971 int i;
972 long l;
973 long long ll;
974 double d;
975 long double ld;
976 void *p;
977 enum
978 {
979 Bad,
980 Int,
981 Long,
982 LongLong,
983 Double,
984 LongDouble,
985 Ptr
986 } type;
987 };
988
989 /* Maximum number of _bfd_error_handler args. Don't increase this
990 without changing the code handling positional parameters. */
991 #define MAX_ARGS 9
992
993 /* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
994 little and extended to handle '%pA', '%pB' and positional parameters. */
995
996 #define PRINT_TYPE(TYPE, FIELD) \
997 do \
998 { \
999 TYPE value = (TYPE) args[arg_no].FIELD; \
1000 result = print (stream, specifier, value); \
1001 } while (0)
1002
1003 typedef int (*print_func) (void *, const char *, ...);
1004
1005 static int
1006 _bfd_doprnt (print_func print, void *stream, const char *format,
1007 union _bfd_doprnt_args *args)
1008 {
1009 const char *ptr = format;
1010 char specifier[128];
1011 int total_printed = 0;
1012 unsigned int arg_count = 0;
1013
1014 while (*ptr != '\0')
1015 {
1016 int result;
1017
1018 if (*ptr != '%')
1019 {
1020 /* While we have regular characters, print them. */
1021 char *end = strchr (ptr, '%');
1022 if (end != NULL)
1023 result = print (stream, "%.*s", (int) (end - ptr), ptr);
1024 else
1025 result = print (stream, "%s", ptr);
1026 ptr += result;
1027 }
1028 else if (ptr[1] == '%')
1029 {
1030 fputc ('%', stream);
1031 result = 1;
1032 ptr += 2;
1033 }
1034 else
1035 {
1036 /* We have a format specifier! */
1037 char *sptr = specifier;
1038 int wide_width = 0, short_width = 0;
1039 unsigned int arg_no;
1040
1041 /* Copy the % and move forward. */
1042 *sptr++ = *ptr++;
1043
1044 /* Check for a positional parameter. */
1045 arg_no = -1u;
1046 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1047 {
1048 arg_no = *ptr - '1';
1049 ptr += 2;
1050 }
1051
1052 /* Move past flags. */
1053 while (strchr ("-+ #0'I", *ptr))
1054 *sptr++ = *ptr++;
1055
1056 if (*ptr == '*')
1057 {
1058 int value;
1059 unsigned int arg_index;
1060
1061 ptr++;
1062 arg_index = arg_count;
1063 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1064 {
1065 arg_index = *ptr - '1';
1066 ptr += 2;
1067 }
1068 value = abs (args[arg_index].i);
1069 arg_count++;
1070 sptr += sprintf (sptr, "%d", value);
1071 }
1072 else
1073 /* Handle explicit numeric value. */
1074 while (ISDIGIT (*ptr))
1075 *sptr++ = *ptr++;
1076
1077 /* Precision. */
1078 if (*ptr == '.')
1079 {
1080 /* Copy and go past the period. */
1081 *sptr++ = *ptr++;
1082 if (*ptr == '*')
1083 {
1084 int value;
1085 unsigned int arg_index;
1086
1087 ptr++;
1088 arg_index = arg_count;
1089 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1090 {
1091 arg_index = *ptr - '1';
1092 ptr += 2;
1093 }
1094 value = abs (args[arg_index].i);
1095 arg_count++;
1096 sptr += sprintf (sptr, "%d", value);
1097 }
1098 else
1099 /* Handle explicit numeric value. */
1100 while (ISDIGIT (*ptr))
1101 *sptr++ = *ptr++;
1102 }
1103 while (strchr ("hlL", *ptr))
1104 {
1105 switch (*ptr)
1106 {
1107 case 'h':
1108 short_width = 1;
1109 break;
1110 case 'l':
1111 wide_width++;
1112 break;
1113 case 'L':
1114 wide_width = 2;
1115 break;
1116 default:
1117 abort();
1118 }
1119 *sptr++ = *ptr++;
1120 }
1121
1122 /* Copy the type specifier, and NULL terminate. */
1123 *sptr++ = *ptr++;
1124 *sptr = '\0';
1125 if ((int) arg_no < 0)
1126 arg_no = arg_count;
1127
1128 switch (ptr[-1])
1129 {
1130 case 'd':
1131 case 'i':
1132 case 'o':
1133 case 'u':
1134 case 'x':
1135 case 'X':
1136 case 'c':
1137 {
1138 /* Short values are promoted to int, so just copy it
1139 as an int and trust the C library printf to cast it
1140 to the right width. */
1141 if (short_width)
1142 PRINT_TYPE (int, i);
1143 else
1144 {
1145 switch (wide_width)
1146 {
1147 case 0:
1148 PRINT_TYPE (int, i);
1149 break;
1150 case 1:
1151 PRINT_TYPE (long, l);
1152 break;
1153 case 2:
1154 default:
1155 #if defined (__MSVCRT__)
1156 sptr[-3] = 'I';
1157 sptr[-2] = '6';
1158 sptr[-1] = '4';
1159 *sptr++ = ptr[-1];
1160 *sptr = '\0';
1161 #endif
1162 PRINT_TYPE (long long, ll);
1163 break;
1164 }
1165 }
1166 }
1167 break;
1168 case 'f':
1169 case 'e':
1170 case 'E':
1171 case 'g':
1172 case 'G':
1173 {
1174 if (wide_width == 0)
1175 PRINT_TYPE (double, d);
1176 else
1177 PRINT_TYPE (long double, ld);
1178 }
1179 break;
1180 case 's':
1181 PRINT_TYPE (char *, p);
1182 break;
1183 case 'p':
1184 if (*ptr == 'A')
1185 {
1186 asection *sec;
1187 bfd *abfd;
1188 const char *group = NULL;
1189 struct coff_comdat_info *ci;
1190
1191 ptr++;
1192 sec = (asection *) args[arg_no].p;
1193 if (sec == NULL)
1194 /* Invoking %pA with a null section pointer is an
1195 internal error. */
1196 abort ();
1197 abfd = sec->owner;
1198 if (abfd != NULL
1199 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1200 && elf_next_in_group (sec) != NULL
1201 && (sec->flags & SEC_GROUP) == 0)
1202 group = elf_group_name (sec);
1203 else if (abfd != NULL
1204 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
1205 && (ci = bfd_coff_get_comdat_section (sec->owner,
1206 sec)) != NULL)
1207 group = ci->name;
1208 if (group != NULL)
1209 result = print (stream, "%s[%s]", sec->name, group);
1210 else
1211 result = print (stream, "%s", sec->name);
1212 }
1213 else if (*ptr == 'B')
1214 {
1215 bfd *abfd;
1216
1217 ptr++;
1218 abfd = (bfd *) args[arg_no].p;
1219 if (abfd == NULL)
1220 /* Invoking %pB with a null bfd pointer is an
1221 internal error. */
1222 abort ();
1223 else if (abfd->my_archive
1224 && !bfd_is_thin_archive (abfd->my_archive))
1225 result = print (stream, "%s(%s)",
1226 bfd_get_filename (abfd->my_archive),
1227 bfd_get_filename (abfd));
1228 else
1229 result = print (stream, "%s", bfd_get_filename (abfd));
1230 }
1231 else
1232 PRINT_TYPE (void *, p);
1233 break;
1234 default:
1235 abort();
1236 }
1237 arg_count++;
1238 }
1239 if (result == -1)
1240 return -1;
1241 total_printed += result;
1242 }
1243
1244 return total_printed;
1245 }
1246
1247 /* First pass over FORMAT to gather ARGS. Returns number of args. */
1248
1249 static unsigned int
1250 _bfd_doprnt_scan (const char *format, va_list ap, union _bfd_doprnt_args *args)
1251 {
1252 const char *ptr = format;
1253 unsigned int arg_count = 0;
1254
1255 for (unsigned int i = 0; i < MAX_ARGS; i++)
1256 args[i].type = Bad;
1257
1258 while (*ptr != '\0')
1259 {
1260 if (*ptr != '%')
1261 {
1262 ptr = strchr (ptr, '%');
1263 if (ptr == NULL)
1264 break;
1265 }
1266 else if (ptr[1] == '%')
1267 ptr += 2;
1268 else
1269 {
1270 int wide_width = 0, short_width = 0;
1271 unsigned int arg_no;
1272 int arg_type;
1273
1274 ptr++;
1275
1276 /* Check for a positional parameter. */
1277 arg_no = -1u;
1278 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1279 {
1280 arg_no = *ptr - '1';
1281 ptr += 2;
1282 }
1283
1284 /* Move past flags. */
1285 while (strchr ("-+ #0'I", *ptr))
1286 ptr++;
1287
1288 if (*ptr == '*')
1289 {
1290 unsigned int arg_index;
1291
1292 ptr++;
1293 arg_index = arg_count;
1294 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1295 {
1296 arg_index = *ptr - '1';
1297 ptr += 2;
1298 }
1299 if (arg_index >= MAX_ARGS)
1300 abort ();
1301 args[arg_index].type = Int;
1302 arg_count++;
1303 }
1304 else
1305 /* Handle explicit numeric value. */
1306 while (ISDIGIT (*ptr))
1307 ptr++;
1308
1309 /* Precision. */
1310 if (*ptr == '.')
1311 {
1312 ptr++;
1313 if (*ptr == '*')
1314 {
1315 unsigned int arg_index;
1316
1317 ptr++;
1318 arg_index = arg_count;
1319 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1320 {
1321 arg_index = *ptr - '1';
1322 ptr += 2;
1323 }
1324 if (arg_index >= MAX_ARGS)
1325 abort ();
1326 args[arg_index].type = Int;
1327 arg_count++;
1328 }
1329 else
1330 /* Handle explicit numeric value. */
1331 while (ISDIGIT (*ptr))
1332 ptr++;
1333 }
1334 while (strchr ("hlL", *ptr))
1335 {
1336 switch (*ptr)
1337 {
1338 case 'h':
1339 short_width = 1;
1340 break;
1341 case 'l':
1342 wide_width++;
1343 break;
1344 case 'L':
1345 wide_width = 2;
1346 break;
1347 default:
1348 abort();
1349 }
1350 ptr++;
1351 }
1352
1353 ptr++;
1354 if ((int) arg_no < 0)
1355 arg_no = arg_count;
1356
1357 arg_type = Bad;
1358 switch (ptr[-1])
1359 {
1360 case 'd':
1361 case 'i':
1362 case 'o':
1363 case 'u':
1364 case 'x':
1365 case 'X':
1366 case 'c':
1367 {
1368 if (short_width)
1369 arg_type = Int;
1370 else
1371 {
1372 switch (wide_width)
1373 {
1374 case 0:
1375 arg_type = Int;
1376 break;
1377 case 1:
1378 arg_type = Long;
1379 break;
1380 case 2:
1381 default:
1382 arg_type = LongLong;
1383 break;
1384 }
1385 }
1386 }
1387 break;
1388 case 'f':
1389 case 'e':
1390 case 'E':
1391 case 'g':
1392 case 'G':
1393 {
1394 if (wide_width == 0)
1395 arg_type = Double;
1396 else
1397 arg_type = LongDouble;
1398 }
1399 break;
1400 case 's':
1401 arg_type = Ptr;
1402 break;
1403 case 'p':
1404 if (*ptr == 'A' || *ptr == 'B')
1405 ptr++;
1406 arg_type = Ptr;
1407 break;
1408 default:
1409 abort();
1410 }
1411
1412 if (arg_no >= MAX_ARGS)
1413 abort ();
1414 args[arg_no].type = arg_type;
1415 arg_count++;
1416 }
1417 }
1418
1419 for (unsigned int i = 0; i < arg_count; i++)
1420 {
1421 switch (args[i].type)
1422 {
1423 case Int:
1424 args[i].i = va_arg (ap, int);
1425 break;
1426 case Long:
1427 args[i].l = va_arg (ap, long);
1428 break;
1429 case LongLong:
1430 args[i].ll = va_arg (ap, long long);
1431 break;
1432 case Double:
1433 args[i].d = va_arg (ap, double);
1434 break;
1435 case LongDouble:
1436 args[i].ld = va_arg (ap, long double);
1437 break;
1438 case Ptr:
1439 args[i].p = va_arg (ap, void *);
1440 break;
1441 default:
1442 abort ();
1443 }
1444 }
1445
1446 return arg_count;
1447 }
1448
1449 /* The standard error handler that prints to stderr. */
1450
1451 static void
1452 error_handler_fprintf (const char *fmt, va_list ap)
1453 {
1454 union _bfd_doprnt_args args[MAX_ARGS];
1455
1456 _bfd_doprnt_scan (fmt, ap, args);
1457
1458 /* PR 4992: Don't interrupt output being sent to stdout. */
1459 fflush (stdout);
1460
1461 fprintf (stderr, "%s: ", _bfd_get_error_program_name ());
1462
1463 _bfd_doprnt ((print_func) fprintf, stderr, fmt, args);
1464
1465 /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
1466 warning, so use the fputc function to avoid it. */
1467 fputc ('\n', stderr);
1468 fflush (stderr);
1469 }
1470
1471 /* Control printing to a string buffer. */
1472 struct buf_stream
1473 {
1474 char *ptr;
1475 int left;
1476 };
1477
1478 /* An fprintf like function that instead prints to a string buffer. */
1479
1480 static int
1481 err_sprintf (void *stream, const char *fmt, ...)
1482 {
1483 struct buf_stream *s = stream;
1484 va_list ap;
1485
1486 va_start (ap, fmt);
1487 int total = vsnprintf (s->ptr, s->left, fmt, ap);
1488 va_end (ap);
1489 if (total < 0)
1490 ;
1491 else if (total > s->left)
1492 {
1493 s->ptr += s->left;
1494 s->left = 0;
1495 }
1496 else
1497 {
1498 s->ptr += total;
1499 s->left -= total;
1500 }
1501 return total;
1502 }
1503
1504 /* Communicate the bfd processed by bfd_check_format_matches to the
1505 error handling function error_handler_sprintf. */
1506
1507 static bfd *error_handler_bfd;
1508
1509 /* An error handler that prints to a string, then dups that string to
1510 a per-xvec cache. */
1511
1512 static void
1513 error_handler_sprintf (const char *fmt, va_list ap)
1514 {
1515 union _bfd_doprnt_args args[MAX_ARGS];
1516 char error_buf[1024];
1517 struct buf_stream error_stream;
1518
1519 _bfd_doprnt_scan (fmt, ap, args);
1520
1521 error_stream.ptr = error_buf;
1522 error_stream.left = sizeof (error_buf);
1523 _bfd_doprnt (err_sprintf, &error_stream, fmt, args);
1524
1525 size_t len = error_stream.ptr - error_buf;
1526 struct per_xvec_message **warn
1527 = _bfd_per_xvec_warn (error_handler_bfd->xvec, len + 1);
1528 if (*warn)
1529 {
1530 memcpy ((*warn)->message, error_buf, len);
1531 (*warn)->message[len] = 0;
1532 }
1533 }
1534
1535 /* This is a function pointer to the routine which should handle BFD
1536 error messages. It is called when a BFD routine encounters an
1537 error for which it wants to print a message. Going through a
1538 function pointer permits a program linked against BFD to intercept
1539 the messages and deal with them itself. */
1540
1541 static bfd_error_handler_type _bfd_error_internal = error_handler_fprintf;
1542
1543 /*
1544 FUNCTION
1545 _bfd_error_handler
1546
1547 SYNOPSIS
1548 void _bfd_error_handler (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
1549
1550 DESCRIPTION
1551 This is the default routine to handle BFD error messages.
1552 Like fprintf (stderr, ...), but also handles some extra format
1553 specifiers.
1554
1555 %pA section name from section. For group components, prints
1556 group name too.
1557 %pB file name from bfd. For archive components, prints
1558 archive too.
1559
1560 Beware: Only supports a maximum of 9 format arguments.
1561 */
1562
1563 void
1564 _bfd_error_handler (const char *fmt, ...)
1565 {
1566 va_list ap;
1567
1568 va_start (ap, fmt);
1569 _bfd_error_internal (fmt, ap);
1570 va_end (ap);
1571 }
1572
1573 /*
1574 FUNCTION
1575 bfd_set_error_handler
1576
1577 SYNOPSIS
1578 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
1579
1580 DESCRIPTION
1581 Set the BFD error handler function. Returns the previous
1582 function.
1583 */
1584
1585 bfd_error_handler_type
1586 bfd_set_error_handler (bfd_error_handler_type pnew)
1587 {
1588 bfd_error_handler_type pold;
1589
1590 pold = _bfd_error_internal;
1591 _bfd_error_internal = pnew;
1592 return pold;
1593 }
1594
1595 /*
1596 INTERNAL_FUNCTION
1597 _bfd_set_error_handler_caching
1598
1599 SYNOPSIS
1600 bfd_error_handler_type _bfd_set_error_handler_caching (bfd *);
1601
1602 DESCRIPTION
1603 Set the BFD error handler function to one that stores messages
1604 to the per_xvec_warn array. Returns the previous function.
1605 */
1606
1607 bfd_error_handler_type
1608 _bfd_set_error_handler_caching (bfd *abfd)
1609 {
1610 error_handler_bfd = abfd;
1611 return bfd_set_error_handler (error_handler_sprintf);
1612 }
1613
1614 /*
1615 FUNCTION
1616 bfd_set_error_program_name
1617
1618 SYNOPSIS
1619 void bfd_set_error_program_name (const char *);
1620
1621 DESCRIPTION
1622 Set the program name to use when printing a BFD error. This
1623 is printed before the error message followed by a colon and
1624 space. The string must not be changed after it is passed to
1625 this function.
1626 */
1627
1628 void
1629 bfd_set_error_program_name (const char *name)
1630 {
1631 _bfd_error_program_name = name;
1632 }
1633
1634 /*
1635 INTERNAL_FUNCTION
1636 _bfd_get_error_program_name
1637
1638 SYNOPSIS
1639 const char *_bfd_get_error_program_name (void);
1640
1641 DESCRIPTION
1642 Get the program name used when printing a BFD error.
1643 */
1644
1645 const char *
1646 _bfd_get_error_program_name (void)
1647 {
1648 if (_bfd_error_program_name != NULL)
1649 return _bfd_error_program_name;
1650 return "BFD";
1651 }
1652
1653 /*
1654 SUBSECTION
1655 BFD assert handler
1656
1657 If BFD finds an internal inconsistency, the bfd assert
1658 handler is called with information on the BFD version, BFD
1659 source file and line. If this happens, most programs linked
1660 against BFD are expected to want to exit with an error, or mark
1661 the current BFD operation as failed, so it is recommended to
1662 override the default handler, which just calls
1663 _bfd_error_handler and continues.
1664
1665 CODE_FRAGMENT
1666 .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
1667 . const char *bfd_version,
1668 . const char *bfd_file,
1669 . int bfd_line);
1670 .
1671 */
1672
1673 /* Note the use of bfd_ prefix on the parameter names above: we want to
1674 show which one is the message and which is the version by naming the
1675 parameters, but avoid polluting the program-using-bfd namespace as
1676 the typedef is visible in the exported headers that the program
1677 includes. Below, it's just for consistency. */
1678
1679 static void
1680 _bfd_default_assert_handler (const char *bfd_formatmsg,
1681 const char *bfd_version,
1682 const char *bfd_file,
1683 int bfd_line)
1684
1685 {
1686 _bfd_error_handler (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
1687 }
1688
1689 /* Similar to _bfd_error_handler, a program can decide to exit on an
1690 internal BFD error. We use a non-variadic type to simplify passing
1691 on parameters to other functions, e.g. _bfd_error_handler. */
1692
1693 static bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
1694
1695 /*
1696 FUNCTION
1697 bfd_set_assert_handler
1698
1699 SYNOPSIS
1700 bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
1701
1702 DESCRIPTION
1703 Set the BFD assert handler function. Returns the previous
1704 function.
1705 */
1706
1707 bfd_assert_handler_type
1708 bfd_set_assert_handler (bfd_assert_handler_type pnew)
1709 {
1710 bfd_assert_handler_type pold;
1711
1712 pold = _bfd_assert_handler;
1713 _bfd_assert_handler = pnew;
1714 return pold;
1715 }
1716
1717 /*
1718 INODE
1719 Initialization, Threading, Error reporting, BFD front end
1720
1721 FUNCTION
1722 bfd_init
1723
1724 SYNOPSIS
1725 unsigned int bfd_init (void);
1726
1727 DESCRIPTION
1728 This routine must be called before any other BFD function to
1729 initialize magical internal data structures.
1730 Returns a magic number, which may be used to check
1731 that the bfd library is configured as expected by users.
1732
1733 .{* Value returned by bfd_init. *}
1734 .#define BFD_INIT_MAGIC (sizeof (struct bfd_section))
1735 .
1736 */
1737
1738 unsigned int
1739 bfd_init (void)
1740 {
1741 bfd_error = bfd_error_no_error;
1742 input_bfd = NULL;
1743 _bfd_clear_error_data ();
1744 input_error = bfd_error_no_error;
1745 _bfd_error_program_name = NULL;
1746 _bfd_error_internal = error_handler_fprintf;
1747 _bfd_assert_handler = _bfd_default_assert_handler;
1748
1749 return BFD_INIT_MAGIC;
1750 }
1751 \f
1752
1753 /*
1754 INODE
1755 Threading, Miscellaneous, Initialization, BFD front end
1756
1757 SECTION
1758 Threading
1759
1760 BFD has limited support for thread-safety. Most BFD globals
1761 are protected by locks, while the error-related globals are
1762 thread-local. A given BFD cannot safely be used from two
1763 threads at the same time; it is up to the application to do
1764 any needed locking. However, it is ok for different threads
1765 to work on different BFD objects at the same time.
1766
1767 SUBSECTION
1768 Thread functions.
1769
1770 CODE_FRAGMENT
1771 .typedef bool (*bfd_lock_unlock_fn_type) (void *);
1772 */
1773
1774 /* The lock and unlock functions, if set. */
1775 static bfd_lock_unlock_fn_type lock_fn;
1776 static bfd_lock_unlock_fn_type unlock_fn;
1777 static void *lock_data;
1778
1779 /*
1780 FUNCTION
1781 bfd_thread_init
1782
1783 SYNOPSIS
1784 bool bfd_thread_init
1785 (bfd_lock_unlock_fn_type lock,
1786 bfd_lock_unlock_fn_type unlock,
1787 void *data);
1788
1789 DESCRIPTION
1790
1791 Initialize BFD threading. The functions passed in will be
1792 used to lock and unlock global data structures. This may only
1793 be called a single time in a given process. Returns true on
1794 success and false on error. DATA is passed verbatim to the
1795 lock and unlock functions. The lock and unlock functions
1796 should return true on success, or set the BFD error and return
1797 false on failure.
1798 */
1799
1800 bool
1801 bfd_thread_init (bfd_lock_unlock_fn_type lock, bfd_lock_unlock_fn_type unlock,
1802 void *data)
1803 {
1804 /* Both functions must be set, and this cannot have been called
1805 before. */
1806 if (lock == NULL || unlock == NULL || unlock_fn != NULL)
1807 {
1808 bfd_set_error (bfd_error_invalid_operation);
1809 return false;
1810 }
1811
1812 lock_fn = lock;
1813 unlock_fn = unlock;
1814 lock_data = data;
1815 return true;
1816 }
1817
1818 /*
1819 FUNCTION
1820 bfd_thread_cleanup
1821
1822 SYNOPSIS
1823 void bfd_thread_cleanup (void);
1824
1825 DESCRIPTION
1826 Clean up any thread-local state. This should be called by a
1827 thread that uses any BFD functions, before the thread exits.
1828 It is fine to call this multiple times, or to call it and then
1829 later call BFD functions on the same thread again.
1830 */
1831
1832 void
1833 bfd_thread_cleanup (void)
1834 {
1835 _bfd_clear_error_data ();
1836 }
1837
1838 /*
1839 INTERNAL_FUNCTION
1840 bfd_lock
1841
1842 SYNOPSIS
1843 bool bfd_lock (void);
1844
1845 DESCRIPTION
1846 Acquire the global BFD lock, if needed. Returns true on
1847 success, false on error.
1848 */
1849
1850 bool
1851 bfd_lock (void)
1852 {
1853 if (lock_fn != NULL)
1854 return lock_fn (lock_data);
1855 return true;
1856 }
1857
1858 /*
1859 INTERNAL_FUNCTION
1860 bfd_unlock
1861
1862 SYNOPSIS
1863 bool bfd_unlock (void);
1864
1865 DESCRIPTION
1866 Release the global BFD lock, if needed. Returns true on
1867 success, false on error.
1868 */
1869
1870 bool
1871 bfd_unlock (void)
1872 {
1873 if (unlock_fn != NULL)
1874 return unlock_fn (lock_data);
1875 return true;
1876 }
1877
1878
1879 /*
1880 INODE
1881 Miscellaneous, Memory Usage, Threading, BFD front end
1882
1883 SECTION
1884 Miscellaneous
1885
1886 SUBSECTION
1887 Miscellaneous functions
1888 */
1889
1890 /*
1891 FUNCTION
1892 bfd_get_reloc_upper_bound
1893
1894 SYNOPSIS
1895 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
1896
1897 DESCRIPTION
1898 Return the number of bytes required to store the
1899 relocation information associated with section @var{sect}
1900 attached to bfd @var{abfd}. If an error occurs, return -1.
1901
1902 */
1903
1904 long
1905 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1906 {
1907 if (abfd->format != bfd_object)
1908 {
1909 bfd_set_error (bfd_error_invalid_operation);
1910 return -1;
1911 }
1912
1913 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
1914 }
1915
1916 /*
1917 FUNCTION
1918 bfd_canonicalize_reloc
1919
1920 SYNOPSIS
1921 long bfd_canonicalize_reloc
1922 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
1923
1924 DESCRIPTION
1925 Call the back end associated with the open BFD
1926 @var{abfd} and translate the external form of the relocation
1927 information attached to @var{sec} into the internal canonical
1928 form. Place the table into memory at @var{loc}, which has
1929 been preallocated, usually by a call to
1930 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
1931 -1 on error.
1932
1933 The @var{syms} table is also needed for horrible internal magic
1934 reasons.
1935
1936 */
1937 long
1938 bfd_canonicalize_reloc (bfd *abfd,
1939 sec_ptr asect,
1940 arelent **location,
1941 asymbol **symbols)
1942 {
1943 if (abfd->format != bfd_object)
1944 {
1945 bfd_set_error (bfd_error_invalid_operation);
1946 return -1;
1947 }
1948
1949 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
1950 (abfd, asect, location, symbols));
1951 }
1952
1953 /*
1954 FUNCTION
1955 bfd_set_reloc
1956
1957 SYNOPSIS
1958 void bfd_set_reloc
1959 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
1960
1961 DESCRIPTION
1962 Set the relocation pointer and count within
1963 section @var{sec} to the values @var{rel} and @var{count}.
1964 The argument @var{abfd} is ignored.
1965
1966 .#define bfd_set_reloc(abfd, asect, location, count) \
1967 . BFD_SEND (abfd, _bfd_set_reloc, (abfd, asect, location, count))
1968 */
1969
1970 /*
1971 FUNCTION
1972 bfd_set_file_flags
1973
1974 SYNOPSIS
1975 bool bfd_set_file_flags (bfd *abfd, flagword flags);
1976
1977 DESCRIPTION
1978 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
1979
1980 Possible errors are:
1981 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
1982 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
1983 o <<bfd_error_invalid_operation>> -
1984 The flag word contained a bit which was not applicable to the
1985 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
1986 on a BFD format which does not support demand paging.
1987
1988 */
1989
1990 bool
1991 bfd_set_file_flags (bfd *abfd, flagword flags)
1992 {
1993 if (abfd->format != bfd_object)
1994 {
1995 bfd_set_error (bfd_error_wrong_format);
1996 return false;
1997 }
1998
1999 if (bfd_read_p (abfd))
2000 {
2001 bfd_set_error (bfd_error_invalid_operation);
2002 return false;
2003 }
2004
2005 abfd->flags = flags;
2006 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
2007 {
2008 bfd_set_error (bfd_error_invalid_operation);
2009 return false;
2010 }
2011
2012 return true;
2013 }
2014
2015 void
2016 bfd_assert (const char *file, int line)
2017 {
2018 /* xgettext:c-format */
2019 (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
2020 BFD_VERSION_STRING, file, line);
2021 }
2022
2023 /* A more or less friendly abort message. In libbfd.h abort is
2024 defined to call this function. */
2025
2026 void
2027 _bfd_abort (const char *file, int line, const char *fn)
2028 {
2029 if (fn != NULL)
2030 _bfd_error_handler
2031 /* xgettext:c-format */
2032 (_("BFD %s internal error, aborting at %s:%d in %s\n"),
2033 BFD_VERSION_STRING, file, line, fn);
2034 else
2035 _bfd_error_handler
2036 /* xgettext:c-format */
2037 (_("BFD %s internal error, aborting at %s:%d\n"),
2038 BFD_VERSION_STRING, file, line);
2039 _bfd_error_handler (_("Please report this bug.\n"));
2040 _exit (EXIT_FAILURE);
2041 }
2042
2043 /*
2044 FUNCTION
2045 bfd_get_arch_size
2046
2047 SYNOPSIS
2048 int bfd_get_arch_size (bfd *abfd);
2049
2050 DESCRIPTION
2051 Returns the normalized architecture address size, in bits, as
2052 determined by the object file's format. By normalized, we mean
2053 either 32 or 64. For ELF, this information is included in the
2054 header. Use bfd_arch_bits_per_address for number of bits in
2055 the architecture address.
2056
2057 Returns the arch size in bits if known, <<-1>> otherwise.
2058 */
2059
2060 int
2061 bfd_get_arch_size (bfd *abfd)
2062 {
2063 if (abfd->xvec->flavour == bfd_target_elf_flavour)
2064 return get_elf_backend_data (abfd)->s->arch_size;
2065
2066 return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
2067 }
2068
2069 /*
2070 FUNCTION
2071 bfd_get_sign_extend_vma
2072
2073 SYNOPSIS
2074 int bfd_get_sign_extend_vma (bfd *abfd);
2075
2076 DESCRIPTION
2077 Indicates if the target architecture "naturally" sign extends
2078 an address. Some architectures implicitly sign extend address
2079 values when they are converted to types larger than the size
2080 of an address. For instance, bfd_get_start_address() will
2081 return an address sign extended to fill a bfd_vma when this is
2082 the case.
2083
2084 Returns <<1>> if the target architecture is known to sign
2085 extend addresses, <<0>> if the target architecture is known to
2086 not sign extend addresses, and <<-1>> otherwise.
2087 */
2088
2089 int
2090 bfd_get_sign_extend_vma (bfd *abfd)
2091 {
2092 const char *name;
2093
2094 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2095 return get_elf_backend_data (abfd)->sign_extend_vma;
2096
2097 name = bfd_get_target (abfd);
2098
2099 /* Return a proper value for DJGPP & PE COFF.
2100 This function is required for DWARF2 support, but there is
2101 no place to store this information in the COFF back end.
2102 Should enough other COFF targets add support for DWARF2,
2103 a place will have to be found. Until then, this hack will do. */
2104 if (startswith (name, "coff-go32")
2105 || strcmp (name, "pe-i386") == 0
2106 || strcmp (name, "pei-i386") == 0
2107 || strcmp (name, "pe-x86-64") == 0
2108 || strcmp (name, "pei-x86-64") == 0
2109 || strcmp (name, "pe-aarch64-little") == 0
2110 || strcmp (name, "pei-aarch64-little") == 0
2111 || strcmp (name, "pe-arm-wince-little") == 0
2112 || strcmp (name, "pei-arm-wince-little") == 0
2113 || strcmp (name, "pei-loongarch64") == 0
2114 || strcmp (name, "pei-riscv64-little") == 0
2115 || strcmp (name, "aixcoff-rs6000") == 0
2116 || strcmp (name, "aix5coff64-rs6000") == 0)
2117 return 1;
2118
2119 if (startswith (name, "mach-o"))
2120 return 0;
2121
2122 bfd_set_error (bfd_error_wrong_format);
2123 return -1;
2124 }
2125
2126 /*
2127 FUNCTION
2128 bfd_set_start_address
2129
2130 SYNOPSIS
2131 bool bfd_set_start_address (bfd *abfd, bfd_vma vma);
2132
2133 DESCRIPTION
2134 Make @var{vma} the entry point of output BFD @var{abfd}.
2135
2136 Returns <<TRUE>> on success, <<FALSE>> otherwise.
2137 */
2138
2139 bool
2140 bfd_set_start_address (bfd *abfd, bfd_vma vma)
2141 {
2142 abfd->start_address = vma;
2143 return true;
2144 }
2145
2146 /*
2147 FUNCTION
2148 bfd_get_gp_size
2149
2150 SYNOPSIS
2151 unsigned int bfd_get_gp_size (bfd *abfd);
2152
2153 DESCRIPTION
2154 Return the maximum size of objects to be optimized using the GP
2155 register under MIPS ECOFF. This is typically set by the <<-G>>
2156 argument to the compiler, assembler or linker.
2157 */
2158
2159 unsigned int
2160 bfd_get_gp_size (bfd *abfd)
2161 {
2162 if (abfd->format == bfd_object)
2163 {
2164 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2165 return ecoff_data (abfd)->gp_size;
2166 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2167 return elf_gp_size (abfd);
2168 }
2169 return 0;
2170 }
2171
2172 /*
2173 FUNCTION
2174 bfd_set_gp_size
2175
2176 SYNOPSIS
2177 void bfd_set_gp_size (bfd *abfd, unsigned int i);
2178
2179 DESCRIPTION
2180 Set the maximum size of objects to be optimized using the GP
2181 register under ECOFF or MIPS ELF. This is typically set by
2182 the <<-G>> argument to the compiler, assembler or linker.
2183 */
2184
2185 void
2186 bfd_set_gp_size (bfd *abfd, unsigned int i)
2187 {
2188 /* Don't try to set GP size on an archive or core file! */
2189 if (abfd->format != bfd_object)
2190 return;
2191
2192 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2193 ecoff_data (abfd)->gp_size = i;
2194 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2195 elf_gp_size (abfd) = i;
2196 }
2197
2198 /* Get the GP value. This is an internal function used by some of the
2199 relocation special_function routines on targets which support a GP
2200 register. */
2201
2202 bfd_vma
2203 _bfd_get_gp_value (bfd *abfd)
2204 {
2205 if (! abfd)
2206 return 0;
2207 if (abfd->format != bfd_object)
2208 return 0;
2209
2210 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2211 return ecoff_data (abfd)->gp;
2212 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2213 return elf_gp (abfd);
2214
2215 return 0;
2216 }
2217
2218 /* Set the GP value. */
2219
2220 void
2221 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
2222 {
2223 if (! abfd)
2224 abort ();
2225 if (abfd->format != bfd_object)
2226 return;
2227
2228 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
2229 ecoff_data (abfd)->gp = v;
2230 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
2231 elf_gp (abfd) = v;
2232 }
2233
2234 /*
2235 FUNCTION
2236 bfd_set_gp_value
2237
2238 SYNOPSIS
2239 void bfd_set_gp_value (bfd *abfd, bfd_vma v);
2240
2241 DESCRIPTION
2242 Allow external access to the fucntion to set the GP value.
2243 This is specifically added for gdb-compile support.
2244 */
2245
2246 void
2247 bfd_set_gp_value (bfd *abfd, bfd_vma v)
2248 {
2249 _bfd_set_gp_value (abfd, v);
2250 }
2251
2252 /*
2253 FUNCTION
2254 bfd_scan_vma
2255
2256 SYNOPSIS
2257 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
2258
2259 DESCRIPTION
2260 Convert, like <<strtoul>> or <<stdtoull> depending on the size
2261 of a <<bfd_vma>>, a numerical expression @var{string} into a
2262 <<bfd_vma>> integer, and return that integer.
2263 */
2264
2265 bfd_vma
2266 bfd_scan_vma (const char *string, const char **end, int base)
2267 {
2268 if (sizeof (bfd_vma) <= sizeof (unsigned long))
2269 return strtoul (string, (char **) end, base);
2270
2271 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
2272 return strtoull (string, (char **) end, base);
2273
2274 abort ();
2275 }
2276
2277 /*
2278 FUNCTION
2279 bfd_copy_private_header_data
2280
2281 SYNOPSIS
2282 bool bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
2283
2284 DESCRIPTION
2285 Copy private BFD header information from the BFD @var{ibfd} to the
2286 the BFD @var{obfd}. This copies information that may require
2287 sections to exist, but does not require symbol tables. Return
2288 <<true>> on success, <<false>> on error.
2289 Possible error returns are:
2290
2291 o <<bfd_error_no_memory>> -
2292 Not enough memory exists to create private data for @var{obfd}.
2293
2294 .#define bfd_copy_private_header_data(ibfd, obfd) \
2295 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
2296 . (ibfd, obfd))
2297
2298 */
2299
2300 /*
2301 FUNCTION
2302 bfd_copy_private_bfd_data
2303
2304 SYNOPSIS
2305 bool bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
2306
2307 DESCRIPTION
2308 Copy private BFD information from the BFD @var{ibfd} to the
2309 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
2310 Possible error returns are:
2311
2312 o <<bfd_error_no_memory>> -
2313 Not enough memory exists to create private data for @var{obfd}.
2314
2315 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
2316 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
2317 . (ibfd, obfd))
2318
2319 */
2320
2321 /*
2322 FUNCTION
2323 bfd_set_private_flags
2324
2325 SYNOPSIS
2326 bool bfd_set_private_flags (bfd *abfd, flagword flags);
2327
2328 DESCRIPTION
2329 Set private BFD flag information in the BFD @var{abfd}.
2330 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
2331 returns are:
2332
2333 o <<bfd_error_no_memory>> -
2334 Not enough memory exists to create private data for @var{obfd}.
2335
2336 .#define bfd_set_private_flags(abfd, flags) \
2337 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
2338
2339 */
2340
2341 /*
2342 FUNCTION
2343 Other functions
2344
2345 DESCRIPTION
2346 The following functions exist but have not yet been documented.
2347
2348 .#define bfd_sizeof_headers(abfd, info) \
2349 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
2350 .
2351 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
2352 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2353 . (abfd, syms, sec, off, file, func, line, NULL))
2354 .
2355 .#define bfd_find_nearest_line_with_alt(abfd, alt_filename, sec, syms, off, \
2356 . file, func, line, disc) \
2357 . BFD_SEND (abfd, _bfd_find_nearest_line_with_alt, \
2358 . (abfd, alt_filename, syms, sec, off, file, func, line, disc))
2359 .
2360 .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
2361 . line, disc) \
2362 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2363 . (abfd, syms, sec, off, file, func, line, disc))
2364 .
2365 .#define bfd_find_line(abfd, syms, sym, file, line) \
2366 . BFD_SEND (abfd, _bfd_find_line, \
2367 . (abfd, syms, sym, file, line))
2368 .
2369 .#define bfd_find_inliner_info(abfd, file, func, line) \
2370 . BFD_SEND (abfd, _bfd_find_inliner_info, \
2371 . (abfd, file, func, line))
2372 .
2373 .#define bfd_debug_info_start(abfd) \
2374 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
2375 .
2376 .#define bfd_debug_info_end(abfd) \
2377 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
2378 .
2379 .#define bfd_debug_info_accumulate(abfd, section) \
2380 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
2381 .
2382 .#define bfd_stat_arch_elt(abfd, stat) \
2383 . BFD_SEND (abfd->my_archive ? abfd->my_archive : abfd, \
2384 . _bfd_stat_arch_elt, (abfd, stat))
2385 .
2386 .#define bfd_update_armap_timestamp(abfd) \
2387 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
2388 .
2389 .#define bfd_set_arch_mach(abfd, arch, mach)\
2390 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
2391 .
2392 .#define bfd_relax_section(abfd, section, link_info, again) \
2393 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
2394 .
2395 .#define bfd_gc_sections(abfd, link_info) \
2396 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
2397 .
2398 .#define bfd_lookup_section_flags(link_info, flag_info, section) \
2399 . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
2400 .
2401 .#define bfd_merge_sections(abfd, link_info) \
2402 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
2403 .
2404 .#define bfd_is_group_section(abfd, sec) \
2405 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
2406 .
2407 .#define bfd_group_name(abfd, sec) \
2408 . BFD_SEND (abfd, _bfd_group_name, (abfd, sec))
2409 .
2410 .#define bfd_discard_group(abfd, sec) \
2411 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
2412 .
2413 .#define bfd_link_hash_table_create(abfd) \
2414 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
2415 .
2416 .#define bfd_link_add_symbols(abfd, info) \
2417 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
2418 .
2419 .#define bfd_link_just_syms(abfd, sec, info) \
2420 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
2421 .
2422 .#define bfd_final_link(abfd, info) \
2423 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
2424 .
2425 .#define bfd_free_cached_info(abfd) \
2426 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
2427 .
2428 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
2429 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
2430 .
2431 .#define bfd_print_private_bfd_data(abfd, file)\
2432 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
2433 .
2434 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
2435 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
2436 .
2437 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
2438 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
2439 . dyncount, dynsyms, ret))
2440 .
2441 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
2442 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
2443 .
2444 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
2445 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
2446 .
2447 */
2448
2449 /*
2450 FUNCTION
2451 bfd_get_relocated_section_contents
2452
2453 SYNOPSIS
2454 bfd_byte *bfd_get_relocated_section_contents
2455 (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
2456 bool, asymbol **);
2457
2458 DESCRIPTION
2459 Read and relocate the indirect link_order section, into DATA
2460 (if non-NULL) or to a malloc'd buffer. Return the buffer, or
2461 NULL on errors.
2462 */
2463
2464 bfd_byte *
2465 bfd_get_relocated_section_contents (bfd *abfd,
2466 struct bfd_link_info *link_info,
2467 struct bfd_link_order *link_order,
2468 bfd_byte *data,
2469 bool relocatable,
2470 asymbol **symbols)
2471 {
2472 bfd *abfd2;
2473 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
2474 bfd_byte *, bool, asymbol **);
2475
2476 if (link_order->type == bfd_indirect_link_order)
2477 {
2478 abfd2 = link_order->u.indirect.section->owner;
2479 if (abfd2 == NULL)
2480 abfd2 = abfd;
2481 }
2482 else
2483 abfd2 = abfd;
2484
2485 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
2486
2487 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
2488 }
2489
2490 /*
2491 FUNCTION
2492 bfd_record_phdr
2493
2494 SYNOPSIS
2495 bool bfd_record_phdr
2496 (bfd *, unsigned long, bool, flagword, bool, bfd_vma,
2497 bool, bool, unsigned int, struct bfd_section **);
2498
2499 DESCRIPTION
2500 Record information about an ELF program header.
2501 */
2502
2503 bool
2504 bfd_record_phdr (bfd *abfd,
2505 unsigned long type,
2506 bool flags_valid,
2507 flagword flags,
2508 bool at_valid,
2509 bfd_vma at, /* Bytes. */
2510 bool includes_filehdr,
2511 bool includes_phdrs,
2512 unsigned int count,
2513 asection **secs)
2514 {
2515 struct elf_segment_map *m, **pm;
2516 size_t amt;
2517 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2518
2519 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2520 return true;
2521
2522 amt = sizeof (struct elf_segment_map);
2523 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
2524 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2525 if (m == NULL)
2526 return false;
2527
2528 m->p_type = type;
2529 m->p_flags = flags;
2530 m->p_paddr = at * opb;
2531 m->p_flags_valid = flags_valid;
2532 m->p_paddr_valid = at_valid;
2533 m->includes_filehdr = includes_filehdr;
2534 m->includes_phdrs = includes_phdrs;
2535 m->count = count;
2536 if (count > 0)
2537 memcpy (m->sections, secs, count * sizeof (asection *));
2538
2539 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
2540 ;
2541 *pm = m;
2542
2543 return true;
2544 }
2545
2546 #ifdef BFD64
2547 /* Return true iff this target is 32-bit. */
2548
2549 static bool
2550 is32bit (bfd *abfd)
2551 {
2552 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2553 {
2554 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2555 return bed->s->elfclass == ELFCLASS32;
2556 }
2557
2558 /* For non-ELF targets, use architecture information. */
2559 return bfd_arch_bits_per_address (abfd) <= 32;
2560 }
2561 #endif
2562
2563 /*
2564 FUNCTION
2565 bfd_sprintf_vma
2566 bfd_fprintf_vma
2567
2568 SYNOPSIS
2569 void bfd_sprintf_vma (bfd *, char *, bfd_vma);
2570 void bfd_fprintf_vma (bfd *, void *, bfd_vma);
2571
2572 DESCRIPTION
2573 bfd_sprintf_vma and bfd_fprintf_vma display an address in the
2574 target's address size.
2575
2576 EXTERNAL
2577 .#define bfd_printf_vma(abfd,x) bfd_fprintf_vma (abfd, stdout, x)
2578 .
2579 */
2580
2581 void
2582 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
2583 {
2584 #ifdef BFD64
2585 if (!is32bit (abfd))
2586 {
2587 sprintf (buf, "%016" PRIx64, (uint64_t) value);
2588 return;
2589 }
2590 #endif
2591 sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
2592 }
2593
2594 void
2595 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
2596 {
2597 #ifdef BFD64
2598 if (!is32bit (abfd))
2599 {
2600 fprintf ((FILE *) stream, "%016" PRIx64, (uint64_t) value);
2601 return;
2602 }
2603 #endif
2604 fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
2605 }
2606
2607 /*
2608 FUNCTION
2609 bfd_alt_mach_code
2610
2611 SYNOPSIS
2612 bool bfd_alt_mach_code (bfd *abfd, int alternative);
2613
2614 DESCRIPTION
2615
2616 When more than one machine code number is available for the
2617 same machine type, this function can be used to switch between
2618 the preferred one (alternative == 0) and any others. Currently,
2619 only ELF supports this feature, with up to two alternate
2620 machine codes.
2621 */
2622
2623 bool
2624 bfd_alt_mach_code (bfd *abfd, int alternative)
2625 {
2626 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2627 {
2628 int code;
2629
2630 switch (alternative)
2631 {
2632 case 0:
2633 code = get_elf_backend_data (abfd)->elf_machine_code;
2634 break;
2635
2636 case 1:
2637 code = get_elf_backend_data (abfd)->elf_machine_alt1;
2638 if (code == 0)
2639 return false;
2640 break;
2641
2642 case 2:
2643 code = get_elf_backend_data (abfd)->elf_machine_alt2;
2644 if (code == 0)
2645 return false;
2646 break;
2647
2648 default:
2649 return false;
2650 }
2651
2652 elf_elfheader (abfd)->e_machine = code;
2653
2654 return true;
2655 }
2656
2657 return false;
2658 }
2659
2660 /*
2661 FUNCTION
2662 bfd_emul_get_maxpagesize
2663
2664 SYNOPSIS
2665 bfd_vma bfd_emul_get_maxpagesize (const char *);
2666
2667 DESCRIPTION
2668 Returns the maximum page size, in bytes, as determined by
2669 emulation.
2670 */
2671
2672 bfd_vma
2673 bfd_emul_get_maxpagesize (const char *emul)
2674 {
2675 const bfd_target *target;
2676
2677 target = bfd_find_target (emul, NULL);
2678 if (target != NULL
2679 && target->flavour == bfd_target_elf_flavour)
2680 return xvec_get_elf_backend_data (target)->maxpagesize;
2681
2682 return 0;
2683 }
2684
2685 /*
2686 FUNCTION
2687 bfd_emul_get_commonpagesize
2688
2689 SYNOPSIS
2690 bfd_vma bfd_emul_get_commonpagesize (const char *);
2691
2692 DESCRIPTION
2693 Returns the common page size, in bytes, as determined by
2694 emulation.
2695 */
2696
2697 bfd_vma
2698 bfd_emul_get_commonpagesize (const char *emul)
2699 {
2700 const bfd_target *target;
2701
2702 target = bfd_find_target (emul, NULL);
2703 if (target != NULL
2704 && target->flavour == bfd_target_elf_flavour)
2705 {
2706 const struct elf_backend_data *bed;
2707
2708 bed = xvec_get_elf_backend_data (target);
2709 return bed->commonpagesize;
2710 }
2711 return 0;
2712 }
2713
2714 /*
2715 FUNCTION
2716 bfd_demangle
2717
2718 SYNOPSIS
2719 char *bfd_demangle (bfd *, const char *, int);
2720
2721 DESCRIPTION
2722 Wrapper around cplus_demangle. Strips leading underscores and
2723 other such chars that would otherwise confuse the demangler.
2724 If passed a g++ v3 ABI mangled name, returns a buffer allocated
2725 with malloc holding the demangled name. Returns NULL otherwise
2726 and on memory alloc failure.
2727 */
2728
2729 char *
2730 bfd_demangle (bfd *abfd, const char *name, int options)
2731 {
2732 char *res, *alloc;
2733 const char *pre, *suf;
2734 size_t pre_len;
2735 bool skip_lead;
2736
2737 skip_lead = (abfd != NULL
2738 && *name != '\0'
2739 && bfd_get_symbol_leading_char (abfd) == *name);
2740 if (skip_lead)
2741 ++name;
2742
2743 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
2744 or the MS PE format. These formats have a number of leading '.'s
2745 on at least some symbols, so we remove all dots to avoid
2746 confusing the demangler. */
2747 pre = name;
2748 while (*name == '.' || *name == '$')
2749 ++name;
2750 pre_len = name - pre;
2751
2752 /* Strip off @plt and suchlike too. */
2753 alloc = NULL;
2754 suf = strchr (name, '@');
2755 if (suf != NULL)
2756 {
2757 alloc = (char *) bfd_malloc (suf - name + 1);
2758 if (alloc == NULL)
2759 return NULL;
2760 memcpy (alloc, name, suf - name);
2761 alloc[suf - name] = '\0';
2762 name = alloc;
2763 }
2764
2765 res = cplus_demangle (name, options);
2766
2767 free (alloc);
2768
2769 if (res == NULL)
2770 {
2771 if (skip_lead)
2772 {
2773 size_t len = strlen (pre) + 1;
2774 alloc = (char *) bfd_malloc (len);
2775 if (alloc == NULL)
2776 return NULL;
2777 memcpy (alloc, pre, len);
2778 return alloc;
2779 }
2780 return NULL;
2781 }
2782
2783 /* Put back any prefix or suffix. */
2784 if (pre_len != 0 || suf != NULL)
2785 {
2786 size_t len;
2787 size_t suf_len;
2788 char *final;
2789
2790 len = strlen (res);
2791 if (suf == NULL)
2792 suf = res + len;
2793 suf_len = strlen (suf) + 1;
2794 final = (char *) bfd_malloc (pre_len + len + suf_len);
2795 if (final != NULL)
2796 {
2797 memcpy (final, pre, pre_len);
2798 memcpy (final + pre_len, res, len);
2799 memcpy (final + pre_len + len, suf, suf_len);
2800 }
2801 free (res);
2802 res = final;
2803 }
2804
2805 return res;
2806 }
2807
2808 /* Get the linker information. */
2809
2810 struct bfd_link_info *
2811 _bfd_get_link_info (bfd *abfd)
2812 {
2813 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2814 return NULL;
2815
2816 return elf_link_info (abfd);
2817 }