Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / sdbinutils / bfd / bfd.c
blob93d670867d5182d6344cd87567f1f587e8819f44
1 /* Generic BFD library interface and support routines.
2 Copyright (C) 1990-2022 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 CODE_FRAGMENT
39 .enum bfd_direction
40 . {
41 . no_direction = 0,
42 . read_direction = 1,
43 . write_direction = 2,
44 . both_direction = 3
45 . };
47 .enum bfd_plugin_format
48 . {
49 . bfd_plugin_unknown = 0,
50 . bfd_plugin_yes = 1,
51 . bfd_plugin_no = 2
52 . };
54 .struct bfd_build_id
55 . {
56 . bfd_size_type size;
57 . bfd_byte data[1];
58 . };
60 .struct bfd
62 . {* The filename the application opened the BFD with. *}
63 . const char *filename;
65 . {* A pointer to the target jump table. *}
66 . const struct bfd_target *xvec;
68 . {* The IOSTREAM, and corresponding IO vector that provide access
69 . to the file backing the BFD. *}
70 . void *iostream;
71 . const struct bfd_iovec *iovec;
73 . {* The caching routines use these to maintain a
74 . least-recently-used list of BFDs. *}
75 . struct bfd *lru_prev, *lru_next;
77 . {* Track current file position (or current buffer offset for
78 . in-memory BFDs). When a file is closed by the caching routines,
79 . BFD retains state information on the file here. *}
80 . ufile_ptr where;
82 . {* File modified time, if mtime_set is TRUE. *}
83 . long mtime;
85 . {* A unique identifier of the BFD *}
86 . unsigned int id;
88 . {* Format_specific flags. *}
89 . flagword flags;
91 . {* Values that may appear in the flags field of a BFD. These also
92 . appear in the object_flags field of the bfd_target structure, where
93 . they indicate the set of flags used by that backend (not all flags
94 . are meaningful for all object file formats) (FIXME: at the moment,
95 . the object_flags values have mostly just been copied from backend
96 . to another, and are not necessarily correct). *}
98 .#define BFD_NO_FLAGS 0x0
100 . {* BFD contains relocation entries. *}
101 .#define HAS_RELOC 0x1
103 . {* BFD is directly executable. *}
104 .#define EXEC_P 0x2
106 . {* BFD has line number information (basically used for F_LNNO in a
107 . COFF header). *}
108 .#define HAS_LINENO 0x4
110 . {* BFD has debugging information. *}
111 .#define HAS_DEBUG 0x08
113 . {* BFD has symbols. *}
114 .#define HAS_SYMS 0x10
116 . {* BFD has local symbols (basically used for F_LSYMS in a COFF
117 . header). *}
118 .#define HAS_LOCALS 0x20
120 . {* BFD is a dynamic object. *}
121 .#define DYNAMIC 0x40
123 . {* Text section is write protected (if D_PAGED is not set, this is
124 . like an a.out NMAGIC file) (the linker sets this by default, but
125 . clears it for -r or -N). *}
126 .#define WP_TEXT 0x80
128 . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
129 . linker sets this by default, but clears it for -r or -n or -N). *}
130 .#define D_PAGED 0x100
132 . {* BFD is relaxable (this means that bfd_relax_section may be able to
133 . do something) (sometimes bfd_relax_section can do something even if
134 . this is not set). *}
135 .#define BFD_IS_RELAXABLE 0x200
137 . {* This may be set before writing out a BFD to request using a
138 . traditional format. For example, this is used to request that when
139 . writing out an a.out object the symbols not be hashed to eliminate
140 . duplicates. *}
141 .#define BFD_TRADITIONAL_FORMAT 0x400
143 . {* This flag indicates that the BFD contents are actually cached
144 . in memory. If this is set, iostream points to a bfd_in_memory
145 . struct. *}
146 .#define BFD_IN_MEMORY 0x800
148 . {* This BFD has been created by the linker and doesn't correspond
149 . to any input file. *}
150 .#define BFD_LINKER_CREATED 0x1000
152 . {* This may be set before writing out a BFD to request that it
153 . be written using values for UIDs, GIDs, timestamps, etc. that
154 . will be consistent from run to run. *}
155 .#define BFD_DETERMINISTIC_OUTPUT 0x2000
157 . {* Compress sections in this BFD. *}
158 .#define BFD_COMPRESS 0x4000
160 . {* Decompress sections in this BFD. *}
161 .#define BFD_DECOMPRESS 0x8000
163 . {* BFD is a dummy, for plugins. *}
164 .#define BFD_PLUGIN 0x10000
166 . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
167 .#define BFD_COMPRESS_GABI 0x20000
169 . {* Convert ELF common symbol type to STT_COMMON or STT_OBJECT in this
170 . BFD. *}
171 .#define BFD_CONVERT_ELF_COMMON 0x40000
173 . {* Use the ELF STT_COMMON type in this BFD. *}
174 .#define BFD_USE_ELF_STT_COMMON 0x80000
176 . {* Put pathnames into archives (non-POSIX). *}
177 .#define BFD_ARCHIVE_FULL_PATH 0x100000
179 . {* Flags bits to be saved in bfd_preserve_save. *}
180 .#define BFD_FLAGS_SAVED \
181 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
182 . | BFD_PLUGIN | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON \
183 . | BFD_USE_ELF_STT_COMMON)
185 . {* Flags bits which are for BFD use only. *}
186 .#define BFD_FLAGS_FOR_BFD_USE_MASK \
187 . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
188 . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
189 . | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON)
191 . {* The format which belongs to the BFD. (object, core, etc.) *}
192 . ENUM_BITFIELD (bfd_format) format : 3;
194 . {* The direction with which the BFD was opened. *}
195 . ENUM_BITFIELD (bfd_direction) direction : 2;
197 . {* Is the file descriptor being cached? That is, can it be closed as
198 . needed, and re-opened when accessed later? *}
199 . unsigned int cacheable : 1;
201 . {* Marks whether there was a default target specified when the
202 . BFD was opened. This is used to select which matching algorithm
203 . to use to choose the back end. *}
204 . unsigned int target_defaulted : 1;
206 . {* ... and here: (``once'' means at least once). *}
207 . unsigned int opened_once : 1;
209 . {* Set if we have a locally maintained mtime value, rather than
210 . getting it from the file each time. *}
211 . unsigned int mtime_set : 1;
213 . {* Flag set if symbols from this BFD should not be exported. *}
214 . unsigned int no_export : 1;
216 . {* Remember when output has begun, to stop strange things
217 . from happening. *}
218 . unsigned int output_has_begun : 1;
220 . {* Have archive map. *}
221 . unsigned int has_armap : 1;
223 . {* Set if this is a thin archive. *}
224 . unsigned int is_thin_archive : 1;
226 . {* Set if this archive should not cache element positions. *}
227 . unsigned int no_element_cache : 1;
229 . {* Set if only required symbols should be added in the link hash table for
230 . this object. Used by VMS linkers. *}
231 . unsigned int selective_search : 1;
233 . {* Set if this is the linker output BFD. *}
234 . unsigned int is_linker_output : 1;
236 . {* Set if this is the linker input BFD. *}
237 . unsigned int is_linker_input : 1;
239 . {* If this is an input for a compiler plug-in library. *}
240 . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
242 . {* Set if this is a plugin output file. *}
243 . unsigned int lto_output : 1;
245 . {* Set if this is a slim LTO object not loaded with a compiler plugin. *}
246 . unsigned int lto_slim_object : 1;
248 . {* Do not attempt to modify this file. Set when detecting errors
249 . that BFD is not prepared to handle for objcopy/strip. *}
250 . unsigned int read_only : 1;
252 . {* Set to dummy BFD created when claimed by a compiler plug-in
253 . library. *}
254 . bfd *plugin_dummy_bfd;
256 . {* The offset of this bfd in the file, typically 0 if it is not
257 . contained in an archive. *}
258 . ufile_ptr origin;
260 . {* The origin in the archive of the proxy entry. This will
261 . normally be the same as origin, except for thin archives,
262 . when it will contain the current offset of the proxy in the
263 . thin archive rather than the offset of the bfd in its actual
264 . container. *}
265 . ufile_ptr proxy_origin;
267 . {* A hash table for section names. *}
268 . struct bfd_hash_table section_htab;
270 . {* Pointer to linked list of sections. *}
271 . struct bfd_section *sections;
273 . {* The last section on the section list. *}
274 . struct bfd_section *section_last;
276 . {* The number of sections. *}
277 . unsigned int section_count;
279 . {* The archive plugin file descriptor. *}
280 . int archive_plugin_fd;
282 . {* The number of opens on the archive plugin file descriptor. *}
283 . unsigned int archive_plugin_fd_open_count;
285 . {* A field used by _bfd_generic_link_add_archive_symbols. This will
286 . be used only for archive elements. *}
287 . int archive_pass;
289 . {* The total size of memory from bfd_alloc. *}
290 . bfd_size_type alloc_size;
292 . {* Stuff only useful for object files:
293 . The start address. *}
294 . bfd_vma start_address;
296 . {* Symbol table for output BFD (with symcount entries).
297 . Also used by the linker to cache input BFD symbols. *}
298 . struct bfd_symbol **outsymbols;
300 . {* Used for input and output. *}
301 . unsigned int symcount;
303 . {* Used for slurped dynamic symbol tables. *}
304 . unsigned int dynsymcount;
306 . {* Pointer to structure which contains architecture information. *}
307 . const struct bfd_arch_info *arch_info;
309 . {* Cached length of file for bfd_get_size. 0 until bfd_get_size is
310 . called, 1 if stat returns an error or the file size is too large to
311 . return in ufile_ptr. Both 0 and 1 should be treated as "unknown". *}
312 . ufile_ptr size;
314 . {* Stuff only useful for archives. *}
315 . void *arelt_data;
316 . struct bfd *my_archive; {* The containing archive BFD. *}
317 . struct bfd *archive_next; {* The next BFD in the archive. *}
318 . struct bfd *archive_head; {* The first BFD in the archive. *}
319 . struct bfd *nested_archives; {* List of nested archive in a flattened
320 . thin archive. *}
322 . union {
323 . {* For input BFDs, a chain of BFDs involved in a link. *}
324 . struct bfd *next;
325 . {* For output BFD, the linker hash table. *}
326 . struct bfd_link_hash_table *hash;
327 . } link;
329 . {* Used by the back end to hold private data. *}
330 . union
332 . struct aout_data_struct *aout_data;
333 . struct artdata *aout_ar_data;
334 . struct coff_tdata *coff_obj_data;
335 . struct pe_tdata *pe_obj_data;
336 . struct xcoff_tdata *xcoff_obj_data;
337 . struct ecoff_tdata *ecoff_obj_data;
338 . struct srec_data_struct *srec_data;
339 . struct verilog_data_struct *verilog_data;
340 . struct ihex_data_struct *ihex_data;
341 . struct tekhex_data_struct *tekhex_data;
342 . struct elf_obj_tdata *elf_obj_data;
343 . struct mmo_data_struct *mmo_data;
344 . struct sun_core_struct *sun_core_data;
345 . struct sco5_core_struct *sco5_core_data;
346 . struct trad_core_struct *trad_core_data;
347 . struct som_data_struct *som_data;
348 . struct hpux_core_struct *hpux_core_data;
349 . struct hppabsd_core_struct *hppabsd_core_data;
350 . struct sgi_core_struct *sgi_core_data;
351 . struct lynx_core_struct *lynx_core_data;
352 . struct osf_core_struct *osf_core_data;
353 . struct cisco_core_struct *cisco_core_data;
354 . struct versados_data_struct *versados_data;
355 . struct netbsd_core_struct *netbsd_core_data;
356 . struct mach_o_data_struct *mach_o_data;
357 . struct mach_o_fat_data_struct *mach_o_fat_data;
358 . struct plugin_data_struct *plugin_data;
359 . struct bfd_pef_data_struct *pef_data;
360 . struct bfd_pef_xlib_data_struct *pef_xlib_data;
361 . struct bfd_sym_data_struct *sym_data;
362 . struct asxxxx_data_struct *asxxxx_data;
363 . void *any;
365 . tdata;
367 . {* Used by the application to hold private data. *}
368 . void *usrdata;
370 . {* Where all the allocated stuff under this BFD goes. This is a
371 . struct objalloc *, but we use void * to avoid requiring the inclusion
372 . of objalloc.h. *}
373 . void *memory;
375 . {* For input BFDs, the build ID, if the object has one. *}
376 . const struct bfd_build_id *build_id;
379 .static inline const char *
380 .bfd_get_filename (const bfd *abfd)
382 . return abfd->filename;
385 .static inline bool
386 .bfd_get_cacheable (const bfd *abfd)
388 . return abfd->cacheable;
391 .static inline enum bfd_format
392 .bfd_get_format (const bfd *abfd)
394 . return abfd->format;
397 .static inline flagword
398 .bfd_get_file_flags (const bfd *abfd)
400 . return abfd->flags;
403 .static inline bfd_vma
404 .bfd_get_start_address (const bfd *abfd)
406 . return abfd->start_address;
409 .static inline unsigned int
410 .bfd_get_symcount (const bfd *abfd)
412 . return abfd->symcount;
415 .static inline unsigned int
416 .bfd_get_dynamic_symcount (const bfd *abfd)
418 . return abfd->dynsymcount;
421 .static inline struct bfd_symbol **
422 .bfd_get_outsymbols (const bfd *abfd)
424 . return abfd->outsymbols;
427 .static inline unsigned int
428 .bfd_count_sections (const bfd *abfd)
430 . return abfd->section_count;
433 .static inline bool
434 .bfd_has_map (const bfd *abfd)
436 . return abfd->has_armap;
439 .static inline bool
440 .bfd_is_thin_archive (const bfd *abfd)
442 . return abfd->is_thin_archive;
445 .static inline void *
446 .bfd_usrdata (const bfd *abfd)
448 . return abfd->usrdata;
451 .{* See note beside bfd_set_section_userdata. *}
452 .static inline bool
453 .bfd_set_cacheable (bfd * abfd, bool val)
455 . abfd->cacheable = val;
456 . return true;
459 .static inline void
460 .bfd_set_thin_archive (bfd *abfd, bool val)
462 . abfd->is_thin_archive = val;
465 .static inline void
466 .bfd_set_usrdata (bfd *abfd, void *val)
468 . abfd->usrdata = val;
471 .static inline asection *
472 .bfd_asymbol_section (const asymbol *sy)
474 . return sy->section;
477 .static inline bfd_vma
478 .bfd_asymbol_value (const asymbol *sy)
480 . return sy->section->vma + sy->value;
483 .static inline const char *
484 .bfd_asymbol_name (const asymbol *sy)
486 . return sy->name;
489 .static inline struct bfd *
490 .bfd_asymbol_bfd (const asymbol *sy)
492 . return sy->the_bfd;
495 .static inline void
496 .bfd_set_asymbol_name (asymbol *sy, const char *name)
498 . sy->name = name;
501 .static inline bfd_size_type
502 .bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
504 . if (abfd->direction != write_direction && sec->rawsize != 0)
505 . return sec->rawsize;
506 . return sec->size;
509 .{* Find the address one past the end of SEC. *}
510 .static inline bfd_size_type
511 .bfd_get_section_limit (const bfd *abfd, const asection *sec)
513 . return (bfd_get_section_limit_octets (abfd, sec)
514 . / bfd_octets_per_byte (abfd, sec));
517 .{* Functions to handle insertion and deletion of a bfd's sections. These
518 . only handle the list pointers, ie. do not adjust section_count,
519 . target_index etc. *}
520 .static inline void
521 .bfd_section_list_remove (bfd *abfd, asection *s)
523 . asection *next = s->next;
524 . asection *prev = s->prev;
525 . if (prev)
526 . prev->next = next;
527 . else
528 . abfd->sections = next;
529 . if (next)
530 . next->prev = prev;
531 . else
532 . abfd->section_last = prev;
535 .static inline void
536 .bfd_section_list_append (bfd *abfd, asection *s)
538 . s->next = 0;
539 . if (abfd->section_last)
541 . s->prev = abfd->section_last;
542 . abfd->section_last->next = s;
544 . else
546 . s->prev = 0;
547 . abfd->sections = s;
549 . abfd->section_last = s;
552 .static inline void
553 .bfd_section_list_prepend (bfd *abfd, asection *s)
555 . s->prev = 0;
556 . if (abfd->sections)
558 . s->next = abfd->sections;
559 . abfd->sections->prev = s;
561 . else
563 . s->next = 0;
564 . abfd->section_last = s;
566 . abfd->sections = s;
569 .static inline void
570 .bfd_section_list_insert_after (bfd *abfd, asection *a, asection *s)
572 . asection *next = a->next;
573 . s->next = next;
574 . s->prev = a;
575 . a->next = s;
576 . if (next)
577 . next->prev = s;
578 . else
579 . abfd->section_last = s;
582 .static inline void
583 .bfd_section_list_insert_before (bfd *abfd, asection *b, asection *s)
585 . asection *prev = b->prev;
586 . s->prev = prev;
587 . s->next = b;
588 . b->prev = s;
589 . if (prev)
590 . prev->next = s;
591 . else
592 . abfd->sections = s;
595 .static inline bool
596 .bfd_section_removed_from_list (const bfd *abfd, const asection *s)
598 . return s->next ? s->next->prev != s : abfd->section_last != s;
603 #include "sysdep.h"
604 #include <stdarg.h>
605 #include "bfd.h"
606 #include "bfdver.h"
607 #include "libiberty.h"
608 #include "demangle.h"
609 #include "safe-ctype.h"
610 #include "bfdlink.h"
611 #include "libbfd.h"
612 #include "coff/internal.h"
613 #include "coff/sym.h"
614 #include "libcoff.h"
615 #include "libecoff.h"
616 #undef obj_symbols
617 #include "elf-bfd.h"
619 #ifndef EXIT_FAILURE
620 #define EXIT_FAILURE 1
621 #endif
624 /* provide storage for subsystem, stack and heap data which may have been
625 passed in on the command line. Ld puts this data into a bfd_link_info
626 struct which ultimately gets passed in to the bfd. When it arrives, copy
627 it to the following struct so that the data will be available in coffcode.h
628 where it is needed. The typedef's used are defined in bfd.h */
631 INODE
632 Error reporting, Miscellaneous, typedef bfd, BFD front end
634 SECTION
635 Error reporting
637 Most BFD functions return nonzero on success (check their
638 individual documentation for precise semantics). On an error,
639 they call <<bfd_set_error>> to set an error condition that callers
640 can check by calling <<bfd_get_error>>.
641 If that returns <<bfd_error_system_call>>, then check
642 <<errno>>.
644 The easiest way to report a BFD error to the user is to
645 use <<bfd_perror>>.
647 SUBSECTION
648 Type <<bfd_error_type>>
650 The values returned by <<bfd_get_error>> are defined by the
651 enumerated type <<bfd_error_type>>.
653 CODE_FRAGMENT
655 .typedef enum bfd_error
657 . bfd_error_no_error = 0,
658 . bfd_error_system_call,
659 . bfd_error_invalid_target,
660 . bfd_error_wrong_format,
661 . bfd_error_wrong_object_format,
662 . bfd_error_invalid_operation,
663 . bfd_error_no_memory,
664 . bfd_error_no_symbols,
665 . bfd_error_no_armap,
666 . bfd_error_no_more_archived_files,
667 . bfd_error_malformed_archive,
668 . bfd_error_missing_dso,
669 . bfd_error_file_not_recognized,
670 . bfd_error_file_ambiguously_recognized,
671 . bfd_error_no_contents,
672 . bfd_error_nonrepresentable_section,
673 . bfd_error_no_debug_section,
674 . bfd_error_bad_value,
675 . bfd_error_file_truncated,
676 . bfd_error_file_too_big,
677 . bfd_error_sorry,
678 . bfd_error_on_input,
679 . bfd_error_invalid_error_code
681 .bfd_error_type;
685 static bfd_error_type bfd_error = bfd_error_no_error;
686 static bfd *input_bfd = NULL;
687 static bfd_error_type input_error = bfd_error_no_error;
689 const char *const bfd_errmsgs[] =
691 N_("no error"),
692 N_("system call error"),
693 N_("invalid bfd target"),
694 N_("file in wrong format"),
695 N_("archive object file in wrong format"),
696 N_("invalid operation"),
697 N_("memory exhausted"),
698 N_("no symbols"),
699 N_("archive has no index; run ranlib to add one"),
700 N_("no more archived files"),
701 N_("malformed archive"),
702 N_("DSO missing from command line"),
703 N_("file format not recognized"),
704 N_("file format is ambiguous"),
705 N_("section has no contents"),
706 N_("nonrepresentable section on output"),
707 N_("symbol needs debug section which does not exist"),
708 N_("bad value"),
709 N_("file truncated"),
710 N_("file too big"),
711 N_("sorry, cannot handle this file"),
712 N_("error reading %s: %s"),
713 N_("#<invalid error code>")
717 FUNCTION
718 bfd_get_error
720 SYNOPSIS
721 bfd_error_type bfd_get_error (void);
723 DESCRIPTION
724 Return the current BFD error condition.
727 bfd_error_type
728 bfd_get_error (void)
730 return bfd_error;
734 FUNCTION
735 bfd_set_error
737 SYNOPSIS
738 void bfd_set_error (bfd_error_type error_tag);
740 DESCRIPTION
741 Set the BFD error condition to be @var{error_tag}.
743 @var{error_tag} must not be bfd_error_on_input. Use
744 bfd_set_input_error for input errors instead.
747 void
748 bfd_set_error (bfd_error_type error_tag)
750 bfd_error = error_tag;
751 if (bfd_error >= bfd_error_on_input)
752 abort ();
756 FUNCTION
757 bfd_set_input_error
759 SYNOPSIS
760 void bfd_set_input_error (bfd *input, bfd_error_type error_tag);
762 DESCRIPTION
764 Set the BFD error condition to be bfd_error_on_input.
765 @var{input} is the input bfd where the error occurred, and
766 @var{error_tag} the bfd_error_type error.
769 void
770 bfd_set_input_error (bfd *input, bfd_error_type error_tag)
772 /* This is an error that occurred during bfd_close when writing an
773 archive, but on one of the input files. */
774 bfd_error = bfd_error_on_input;
775 input_bfd = input;
776 input_error = error_tag;
777 if (input_error >= bfd_error_on_input)
778 abort ();
782 FUNCTION
783 bfd_errmsg
785 SYNOPSIS
786 const char *bfd_errmsg (bfd_error_type error_tag);
788 DESCRIPTION
789 Return a string describing the error @var{error_tag}, or
790 the system error if @var{error_tag} is <<bfd_error_system_call>>.
793 const char *
794 bfd_errmsg (bfd_error_type error_tag)
796 #ifndef errno
797 extern int errno;
798 #endif
799 if (error_tag == bfd_error_on_input)
801 char *buf;
802 const char *msg = bfd_errmsg (input_error);
804 if (asprintf (&buf, _(bfd_errmsgs [error_tag]),
805 bfd_get_filename (input_bfd), msg) != -1)
806 return buf;
808 /* Ick, what to do on out of memory? */
809 return msg;
812 if (error_tag == bfd_error_system_call)
813 return xstrerror (errno);
815 if (error_tag > bfd_error_invalid_error_code)
816 error_tag = bfd_error_invalid_error_code; /* sanity check */
818 return _(bfd_errmsgs [error_tag]);
822 FUNCTION
823 bfd_perror
825 SYNOPSIS
826 void bfd_perror (const char *message);
828 DESCRIPTION
829 Print to the standard error stream a string describing the
830 last BFD error that occurred, or the last system error if
831 the last BFD error was a system call failure. If @var{message}
832 is non-NULL and non-empty, the error string printed is preceded
833 by @var{message}, a colon, and a space. It is followed by a newline.
836 void
837 bfd_perror (const char *message)
839 fflush (stdout);
840 if (message == NULL || *message == '\0')
841 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
842 else
843 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
844 fflush (stderr);
848 SUBSECTION
849 BFD error handler
851 Some BFD functions want to print messages describing the
852 problem. They call a BFD error handler function. This
853 function may be overridden by the program.
855 The BFD error handler acts like vprintf.
857 CODE_FRAGMENT
859 .typedef void (*bfd_error_handler_type) (const char *, va_list);
863 /* The program name used when printing BFD error messages. */
865 static const char *_bfd_error_program_name;
867 /* Support for positional parameters. */
869 union _bfd_doprnt_args
871 int i;
872 long l;
873 long long ll;
874 double d;
875 long double ld;
876 void *p;
877 enum
879 Bad,
880 Int,
881 Long,
882 LongLong,
883 Double,
884 LongDouble,
886 } type;
889 /* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
890 little and extended to handle '%pA', '%pB' and positional parameters. */
892 #define PRINT_TYPE(TYPE, FIELD) \
893 do \
895 TYPE value = (TYPE) args[arg_no].FIELD; \
896 result = fprintf (stream, specifier, value); \
897 } while (0)
899 static int
900 _bfd_doprnt (FILE *stream, const char *format, union _bfd_doprnt_args *args)
902 const char *ptr = format;
903 char specifier[128];
904 int total_printed = 0;
905 unsigned int arg_count = 0;
907 while (*ptr != '\0')
909 int result;
911 if (*ptr != '%')
913 /* While we have regular characters, print them. */
914 char *end = strchr (ptr, '%');
915 if (end != NULL)
916 result = fprintf (stream, "%.*s", (int) (end - ptr), ptr);
917 else
918 result = fprintf (stream, "%s", ptr);
919 ptr += result;
921 else if (ptr[1] == '%')
923 fputc ('%', stream);
924 result = 1;
925 ptr += 2;
927 else
929 /* We have a format specifier! */
930 char *sptr = specifier;
931 int wide_width = 0, short_width = 0;
932 unsigned int arg_no;
934 /* Copy the % and move forward. */
935 *sptr++ = *ptr++;
937 /* Check for a positional parameter. */
938 arg_no = -1u;
939 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
941 arg_no = *ptr - '1';
942 ptr += 2;
945 /* Move past flags. */
946 while (strchr ("-+ #0'I", *ptr))
947 *sptr++ = *ptr++;
949 if (*ptr == '*')
951 int value;
952 unsigned int arg_index;
954 ptr++;
955 arg_index = arg_count;
956 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
958 arg_index = *ptr - '1';
959 ptr += 2;
961 value = abs (args[arg_index].i);
962 arg_count++;
963 sptr += sprintf (sptr, "%d", value);
965 else
966 /* Handle explicit numeric value. */
967 while (ISDIGIT (*ptr))
968 *sptr++ = *ptr++;
970 /* Precision. */
971 if (*ptr == '.')
973 /* Copy and go past the period. */
974 *sptr++ = *ptr++;
975 if (*ptr == '*')
977 int value;
978 unsigned int arg_index;
980 ptr++;
981 arg_index = arg_count;
982 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
984 arg_index = *ptr - '1';
985 ptr += 2;
987 value = abs (args[arg_index].i);
988 arg_count++;
989 sptr += sprintf (sptr, "%d", value);
991 else
992 /* Handle explicit numeric value. */
993 while (ISDIGIT (*ptr))
994 *sptr++ = *ptr++;
996 while (strchr ("hlL", *ptr))
998 switch (*ptr)
1000 case 'h':
1001 short_width = 1;
1002 break;
1003 case 'l':
1004 wide_width++;
1005 break;
1006 case 'L':
1007 wide_width = 2;
1008 break;
1009 default:
1010 abort();
1012 *sptr++ = *ptr++;
1015 /* Copy the type specifier, and NULL terminate. */
1016 *sptr++ = *ptr++;
1017 *sptr = '\0';
1018 if ((int) arg_no < 0)
1019 arg_no = arg_count;
1021 switch (ptr[-1])
1023 case 'd':
1024 case 'i':
1025 case 'o':
1026 case 'u':
1027 case 'x':
1028 case 'X':
1029 case 'c':
1031 /* Short values are promoted to int, so just copy it
1032 as an int and trust the C library printf to cast it
1033 to the right width. */
1034 if (short_width)
1035 PRINT_TYPE (int, i);
1036 else
1038 switch (wide_width)
1040 case 0:
1041 PRINT_TYPE (int, i);
1042 break;
1043 case 1:
1044 PRINT_TYPE (long, l);
1045 break;
1046 case 2:
1047 default:
1048 #if defined (__MSVCRT__)
1049 sptr[-3] = 'I';
1050 sptr[-2] = '6';
1051 sptr[-1] = '4';
1052 *sptr++ = ptr[-1];
1053 *sptr = '\0';
1054 #endif
1055 PRINT_TYPE (long long, ll);
1056 break;
1060 break;
1061 case 'f':
1062 case 'e':
1063 case 'E':
1064 case 'g':
1065 case 'G':
1067 if (wide_width == 0)
1068 PRINT_TYPE (double, d);
1069 else
1070 PRINT_TYPE (long double, ld);
1072 break;
1073 case 's':
1074 PRINT_TYPE (char *, p);
1075 break;
1076 case 'p':
1077 if (*ptr == 'A')
1079 asection *sec;
1080 bfd *abfd;
1081 const char *group = NULL;
1082 struct coff_comdat_info *ci;
1084 ptr++;
1085 sec = (asection *) args[arg_no].p;
1086 if (sec == NULL)
1087 /* Invoking %pA with a null section pointer is an
1088 internal error. */
1089 abort ();
1090 abfd = sec->owner;
1091 if (abfd != NULL
1092 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1093 && elf_next_in_group (sec) != NULL
1094 && (sec->flags & SEC_GROUP) == 0)
1095 group = elf_group_name (sec);
1096 else if (abfd != NULL
1097 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
1098 && (ci = bfd_coff_get_comdat_section (sec->owner,
1099 sec)) != NULL)
1100 group = ci->name;
1101 if (group != NULL)
1102 result = fprintf (stream, "%s[%s]", sec->name, group);
1103 else
1104 result = fprintf (stream, "%s", sec->name);
1106 else if (*ptr == 'B')
1108 bfd *abfd;
1110 ptr++;
1111 abfd = (bfd *) args[arg_no].p;
1112 if (abfd == NULL)
1113 /* Invoking %pB with a null bfd pointer is an
1114 internal error. */
1115 abort ();
1116 else if (abfd->my_archive
1117 && !bfd_is_thin_archive (abfd->my_archive))
1118 result = fprintf (stream, "%s(%s)",
1119 bfd_get_filename (abfd->my_archive),
1120 bfd_get_filename (abfd));
1121 else
1122 result = fprintf (stream, "%s", bfd_get_filename (abfd));
1124 else
1125 PRINT_TYPE (void *, p);
1126 break;
1127 default:
1128 abort();
1130 arg_count++;
1132 if (result == -1)
1133 return -1;
1134 total_printed += result;
1137 return total_printed;
1140 /* First pass over FORMAT to gather ARGS. Returns number of args. */
1142 static unsigned int
1143 _bfd_doprnt_scan (const char *format, union _bfd_doprnt_args *args)
1145 const char *ptr = format;
1146 unsigned int arg_count = 0;
1148 while (*ptr != '\0')
1150 if (*ptr != '%')
1152 ptr = strchr (ptr, '%');
1153 if (ptr == NULL)
1154 break;
1156 else if (ptr[1] == '%')
1157 ptr += 2;
1158 else
1160 int wide_width = 0, short_width = 0;
1161 unsigned int arg_no;
1162 int arg_type;
1164 ptr++;
1166 /* Check for a positional parameter. */
1167 arg_no = -1u;
1168 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1170 arg_no = *ptr - '1';
1171 ptr += 2;
1174 /* Move past flags. */
1175 while (strchr ("-+ #0'I", *ptr))
1176 ptr++;
1178 if (*ptr == '*')
1180 unsigned int arg_index;
1182 ptr++;
1183 arg_index = arg_count;
1184 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1186 arg_index = *ptr - '1';
1187 ptr += 2;
1189 if (arg_index >= 9)
1190 abort ();
1191 args[arg_index].type = Int;
1192 arg_count++;
1194 else
1195 /* Handle explicit numeric value. */
1196 while (ISDIGIT (*ptr))
1197 ptr++;
1199 /* Precision. */
1200 if (*ptr == '.')
1202 ptr++;
1203 if (*ptr == '*')
1205 unsigned int arg_index;
1207 ptr++;
1208 arg_index = arg_count;
1209 if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
1211 arg_index = *ptr - '1';
1212 ptr += 2;
1214 if (arg_index >= 9)
1215 abort ();
1216 args[arg_index].type = Int;
1217 arg_count++;
1219 else
1220 /* Handle explicit numeric value. */
1221 while (ISDIGIT (*ptr))
1222 ptr++;
1224 while (strchr ("hlL", *ptr))
1226 switch (*ptr)
1228 case 'h':
1229 short_width = 1;
1230 break;
1231 case 'l':
1232 wide_width++;
1233 break;
1234 case 'L':
1235 wide_width = 2;
1236 break;
1237 default:
1238 abort();
1240 ptr++;
1243 ptr++;
1244 if ((int) arg_no < 0)
1245 arg_no = arg_count;
1247 arg_type = Bad;
1248 switch (ptr[-1])
1250 case 'd':
1251 case 'i':
1252 case 'o':
1253 case 'u':
1254 case 'x':
1255 case 'X':
1256 case 'c':
1258 if (short_width)
1259 arg_type = Int;
1260 else
1262 switch (wide_width)
1264 case 0:
1265 arg_type = Int;
1266 break;
1267 case 1:
1268 arg_type = Long;
1269 break;
1270 case 2:
1271 default:
1272 arg_type = LongLong;
1273 break;
1277 break;
1278 case 'f':
1279 case 'e':
1280 case 'E':
1281 case 'g':
1282 case 'G':
1284 if (wide_width == 0)
1285 arg_type = Double;
1286 else
1287 arg_type = LongDouble;
1289 break;
1290 case 's':
1291 arg_type = Ptr;
1292 break;
1293 case 'p':
1294 if (*ptr == 'A' || *ptr == 'B')
1295 ptr++;
1296 arg_type = Ptr;
1297 break;
1298 default:
1299 abort();
1302 if (arg_no >= 9)
1303 abort ();
1304 args[arg_no].type = arg_type;
1305 arg_count++;
1309 return arg_count;
1312 static void
1313 error_handler_internal (const char *fmt, va_list ap)
1315 unsigned int i, arg_count;
1316 union _bfd_doprnt_args args[9];
1318 for (i = 0; i < sizeof (args) / sizeof (args[0]); i++)
1319 args[i].type = Bad;
1321 arg_count = _bfd_doprnt_scan (fmt, args);
1322 for (i = 0; i < arg_count; i++)
1324 switch (args[i].type)
1326 case Int:
1327 args[i].i = va_arg (ap, int);
1328 break;
1329 case Long:
1330 args[i].l = va_arg (ap, long);
1331 break;
1332 case LongLong:
1333 args[i].ll = va_arg (ap, long long);
1334 break;
1335 case Double:
1336 args[i].d = va_arg (ap, double);
1337 break;
1338 case LongDouble:
1339 args[i].ld = va_arg (ap, long double);
1340 break;
1341 case Ptr:
1342 args[i].p = va_arg (ap, void *);
1343 break;
1344 default:
1345 abort ();
1349 /* PR 4992: Don't interrupt output being sent to stdout. */
1350 fflush (stdout);
1352 if (_bfd_error_program_name != NULL)
1353 fprintf (stderr, "%s: ", _bfd_error_program_name);
1354 else
1355 fprintf (stderr, "BFD: ");
1357 _bfd_doprnt (stderr, fmt, args);
1359 /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
1360 warning, so use the fputc function to avoid it. */
1361 fputc ('\n', stderr);
1362 fflush (stderr);
1365 /* This is a function pointer to the routine which should handle BFD
1366 error messages. It is called when a BFD routine encounters an
1367 error for which it wants to print a message. Going through a
1368 function pointer permits a program linked against BFD to intercept
1369 the messages and deal with them itself. */
1371 static bfd_error_handler_type _bfd_error_internal = error_handler_internal;
1374 FUNCTION
1375 _bfd_error_handler
1377 SYNOPSIS
1378 void _bfd_error_handler (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
1380 DESCRIPTION
1381 This is the default routine to handle BFD error messages.
1382 Like fprintf (stderr, ...), but also handles some extra format
1383 specifiers.
1385 %pA section name from section. For group components, prints
1386 group name too.
1387 %pB file name from bfd. For archive components, prints
1388 archive too.
1390 Beware: Only supports a maximum of 9 format arguments.
1393 void
1394 _bfd_error_handler (const char *fmt, ...)
1396 va_list ap;
1398 va_start (ap, fmt);
1399 _bfd_error_internal (fmt, ap);
1400 va_end (ap);
1404 FUNCTION
1405 bfd_set_error_handler
1407 SYNOPSIS
1408 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
1410 DESCRIPTION
1411 Set the BFD error handler function. Returns the previous
1412 function.
1415 bfd_error_handler_type
1416 bfd_set_error_handler (bfd_error_handler_type pnew)
1418 bfd_error_handler_type pold;
1420 pold = _bfd_error_internal;
1421 _bfd_error_internal = pnew;
1422 return pold;
1426 FUNCTION
1427 bfd_set_error_program_name
1429 SYNOPSIS
1430 void bfd_set_error_program_name (const char *);
1432 DESCRIPTION
1433 Set the program name to use when printing a BFD error. This
1434 is printed before the error message followed by a colon and
1435 space. The string must not be changed after it is passed to
1436 this function.
1439 void
1440 bfd_set_error_program_name (const char *name)
1442 _bfd_error_program_name = name;
1446 SUBSECTION
1447 BFD assert handler
1449 If BFD finds an internal inconsistency, the bfd assert
1450 handler is called with information on the BFD version, BFD
1451 source file and line. If this happens, most programs linked
1452 against BFD are expected to want to exit with an error, or mark
1453 the current BFD operation as failed, so it is recommended to
1454 override the default handler, which just calls
1455 _bfd_error_handler and continues.
1457 CODE_FRAGMENT
1459 .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
1460 . const char *bfd_version,
1461 . const char *bfd_file,
1462 . int bfd_line);
1466 /* Note the use of bfd_ prefix on the parameter names above: we want to
1467 show which one is the message and which is the version by naming the
1468 parameters, but avoid polluting the program-using-bfd namespace as
1469 the typedef is visible in the exported headers that the program
1470 includes. Below, it's just for consistency. */
1472 static void
1473 _bfd_default_assert_handler (const char *bfd_formatmsg,
1474 const char *bfd_version,
1475 const char *bfd_file,
1476 int bfd_line)
1479 _bfd_error_handler (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
1482 /* Similar to _bfd_error_handler, a program can decide to exit on an
1483 internal BFD error. We use a non-variadic type to simplify passing
1484 on parameters to other functions, e.g. _bfd_error_handler. */
1486 static bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
1489 FUNCTION
1490 bfd_set_assert_handler
1492 SYNOPSIS
1493 bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
1495 DESCRIPTION
1496 Set the BFD assert handler function. Returns the previous
1497 function.
1500 bfd_assert_handler_type
1501 bfd_set_assert_handler (bfd_assert_handler_type pnew)
1503 bfd_assert_handler_type pold;
1505 pold = _bfd_assert_handler;
1506 _bfd_assert_handler = pnew;
1507 return pold;
1511 INODE
1512 Miscellaneous, Memory Usage, Error reporting, BFD front end
1514 SECTION
1515 Miscellaneous
1517 SUBSECTION
1518 Miscellaneous functions
1522 FUNCTION
1523 bfd_get_reloc_upper_bound
1525 SYNOPSIS
1526 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
1528 DESCRIPTION
1529 Return the number of bytes required to store the
1530 relocation information associated with section @var{sect}
1531 attached to bfd @var{abfd}. If an error occurs, return -1.
1535 long
1536 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1538 if (abfd->format != bfd_object)
1540 bfd_set_error (bfd_error_invalid_operation);
1541 return -1;
1544 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
1548 FUNCTION
1549 bfd_canonicalize_reloc
1551 SYNOPSIS
1552 long bfd_canonicalize_reloc
1553 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
1555 DESCRIPTION
1556 Call the back end associated with the open BFD
1557 @var{abfd} and translate the external form of the relocation
1558 information attached to @var{sec} into the internal canonical
1559 form. Place the table into memory at @var{loc}, which has
1560 been preallocated, usually by a call to
1561 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
1562 -1 on error.
1564 The @var{syms} table is also needed for horrible internal magic
1565 reasons.
1568 long
1569 bfd_canonicalize_reloc (bfd *abfd,
1570 sec_ptr asect,
1571 arelent **location,
1572 asymbol **symbols)
1574 if (abfd->format != bfd_object)
1576 bfd_set_error (bfd_error_invalid_operation);
1577 return -1;
1580 return BFD_SEND (abfd, _bfd_canonicalize_reloc,
1581 (abfd, asect, location, symbols));
1585 FUNCTION
1586 bfd_set_reloc
1588 SYNOPSIS
1589 void bfd_set_reloc
1590 (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
1592 DESCRIPTION
1593 Set the relocation pointer and count within
1594 section @var{sec} to the values @var{rel} and @var{count}.
1595 The argument @var{abfd} is ignored.
1597 .#define bfd_set_reloc(abfd, asect, location, count) \
1598 . BFD_SEND (abfd, _bfd_set_reloc, (abfd, asect, location, count))
1602 FUNCTION
1603 bfd_set_file_flags
1605 SYNOPSIS
1606 bool bfd_set_file_flags (bfd *abfd, flagword flags);
1608 DESCRIPTION
1609 Set the flag word in the BFD @var{abfd} to the value @var{flags}.
1611 Possible errors are:
1612 o <<bfd_error_wrong_format>> - The target bfd was not of object format.
1613 o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
1614 o <<bfd_error_invalid_operation>> -
1615 The flag word contained a bit which was not applicable to the
1616 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
1617 on a BFD format which does not support demand paging.
1621 bool
1622 bfd_set_file_flags (bfd *abfd, flagword flags)
1624 if (abfd->format != bfd_object)
1626 bfd_set_error (bfd_error_wrong_format);
1627 return false;
1630 if (bfd_read_p (abfd))
1632 bfd_set_error (bfd_error_invalid_operation);
1633 return false;
1636 abfd->flags = flags;
1637 if ((flags & bfd_applicable_file_flags (abfd)) != flags)
1639 bfd_set_error (bfd_error_invalid_operation);
1640 return false;
1643 return true;
1646 void
1647 bfd_assert (const char *file, int line)
1649 /* xgettext:c-format */
1650 (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
1651 BFD_VERSION_STRING, file, line);
1654 /* A more or less friendly abort message. In libbfd.h abort is
1655 defined to call this function. */
1657 void
1658 _bfd_abort (const char *file, int line, const char *fn)
1660 if (fn != NULL)
1661 _bfd_error_handler
1662 /* xgettext:c-format */
1663 (_("BFD %s internal error, aborting at %s:%d in %s\n"),
1664 BFD_VERSION_STRING, file, line, fn);
1665 else
1666 _bfd_error_handler
1667 /* xgettext:c-format */
1668 (_("BFD %s internal error, aborting at %s:%d\n"),
1669 BFD_VERSION_STRING, file, line);
1670 _bfd_error_handler (_("Please report this bug.\n"));
1671 _exit (EXIT_FAILURE);
1675 FUNCTION
1676 bfd_get_arch_size
1678 SYNOPSIS
1679 int bfd_get_arch_size (bfd *abfd);
1681 DESCRIPTION
1682 Returns the normalized architecture address size, in bits, as
1683 determined by the object file's format. By normalized, we mean
1684 either 32 or 64. For ELF, this information is included in the
1685 header. Use bfd_arch_bits_per_address for number of bits in
1686 the architecture address.
1688 RETURNS
1689 Returns the arch size in bits if known, <<-1>> otherwise.
1693 bfd_get_arch_size (bfd *abfd)
1695 if (abfd->xvec->flavour == bfd_target_elf_flavour)
1696 return get_elf_backend_data (abfd)->s->arch_size;
1698 return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
1702 FUNCTION
1703 bfd_get_sign_extend_vma
1705 SYNOPSIS
1706 int bfd_get_sign_extend_vma (bfd *abfd);
1708 DESCRIPTION
1709 Indicates if the target architecture "naturally" sign extends
1710 an address. Some architectures implicitly sign extend address
1711 values when they are converted to types larger than the size
1712 of an address. For instance, bfd_get_start_address() will
1713 return an address sign extended to fill a bfd_vma when this is
1714 the case.
1716 RETURNS
1717 Returns <<1>> if the target architecture is known to sign
1718 extend addresses, <<0>> if the target architecture is known to
1719 not sign extend addresses, and <<-1>> otherwise.
1723 bfd_get_sign_extend_vma (bfd *abfd)
1725 const char *name;
1727 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1728 return get_elf_backend_data (abfd)->sign_extend_vma;
1730 name = bfd_get_target (abfd);
1732 /* Return a proper value for DJGPP & PE COFF.
1733 This function is required for DWARF2 support, but there is
1734 no place to store this information in the COFF back end.
1735 Should enough other COFF targets add support for DWARF2,
1736 a place will have to be found. Until then, this hack will do. */
1737 if (startswith (name, "coff-go32")
1738 || strcmp (name, "pe-i386") == 0
1739 || strcmp (name, "pei-i386") == 0
1740 || strcmp (name, "pe-x86-64") == 0
1741 || strcmp (name, "pei-x86-64") == 0
1742 || strcmp (name, "pei-aarch64-little") == 0
1743 || strcmp (name, "pe-arm-wince-little") == 0
1744 || strcmp (name, "pei-arm-wince-little") == 0
1745 || strcmp (name, "aixcoff-rs6000") == 0
1746 || strcmp (name, "aix5coff64-rs6000") == 0)
1747 return 1;
1749 if (startswith (name, "mach-o"))
1750 return 0;
1752 bfd_set_error (bfd_error_wrong_format);
1753 return -1;
1757 FUNCTION
1758 bfd_set_start_address
1760 SYNOPSIS
1761 bool bfd_set_start_address (bfd *abfd, bfd_vma vma);
1763 DESCRIPTION
1764 Make @var{vma} the entry point of output BFD @var{abfd}.
1766 RETURNS
1767 Returns <<TRUE>> on success, <<FALSE>> otherwise.
1770 bool
1771 bfd_set_start_address (bfd *abfd, bfd_vma vma)
1773 abfd->start_address = vma;
1774 return true;
1778 FUNCTION
1779 bfd_get_gp_size
1781 SYNOPSIS
1782 unsigned int bfd_get_gp_size (bfd *abfd);
1784 DESCRIPTION
1785 Return the maximum size of objects to be optimized using the GP
1786 register under MIPS ECOFF. This is typically set by the <<-G>>
1787 argument to the compiler, assembler or linker.
1790 unsigned int
1791 bfd_get_gp_size (bfd *abfd)
1793 if (abfd->format == bfd_object)
1795 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1796 return ecoff_data (abfd)->gp_size;
1797 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1798 return elf_gp_size (abfd);
1800 return 0;
1804 FUNCTION
1805 bfd_set_gp_size
1807 SYNOPSIS
1808 void bfd_set_gp_size (bfd *abfd, unsigned int i);
1810 DESCRIPTION
1811 Set the maximum size of objects to be optimized using the GP
1812 register under ECOFF or MIPS ELF. This is typically set by
1813 the <<-G>> argument to the compiler, assembler or linker.
1816 void
1817 bfd_set_gp_size (bfd *abfd, unsigned int i)
1819 /* Don't try to set GP size on an archive or core file! */
1820 if (abfd->format != bfd_object)
1821 return;
1823 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1824 ecoff_data (abfd)->gp_size = i;
1825 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1826 elf_gp_size (abfd) = i;
1829 /* Get the GP value. This is an internal function used by some of the
1830 relocation special_function routines on targets which support a GP
1831 register. */
1833 bfd_vma
1834 _bfd_get_gp_value (bfd *abfd)
1836 if (! abfd)
1837 return 0;
1838 if (abfd->format != bfd_object)
1839 return 0;
1841 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1842 return ecoff_data (abfd)->gp;
1843 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1844 return elf_gp (abfd);
1846 return 0;
1849 /* Set the GP value. */
1851 void
1852 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
1854 if (! abfd)
1855 abort ();
1856 if (abfd->format != bfd_object)
1857 return;
1859 if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
1860 ecoff_data (abfd)->gp = v;
1861 else if (abfd->xvec->flavour == bfd_target_elf_flavour)
1862 elf_gp (abfd) = v;
1866 FUNCTION
1867 bfd_set_gp_value
1869 SYNOPSIS
1870 void bfd_set_gp_value (bfd *abfd, bfd_vma v);
1872 DESCRIPTION
1873 Allow external access to the fucntion to set the GP value.
1874 This is specifically added for gdb-compile support.
1877 void
1878 bfd_set_gp_value (bfd *abfd, bfd_vma v)
1880 return _bfd_set_gp_value (abfd, v);
1884 FUNCTION
1885 bfd_scan_vma
1887 SYNOPSIS
1888 bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
1890 DESCRIPTION
1891 Convert, like <<strtoul>>, a numerical expression
1892 @var{string} into a <<bfd_vma>> integer, and return that integer.
1893 (Though without as many bells and whistles as <<strtoul>>.)
1894 The expression is assumed to be unsigned (i.e., positive).
1895 If given a @var{base}, it is used as the base for conversion.
1896 A base of 0 causes the function to interpret the string
1897 in hex if a leading "0x" or "0X" is found, otherwise
1898 in octal if a leading zero is found, otherwise in decimal.
1900 If the value would overflow, the maximum <<bfd_vma>> value is
1901 returned.
1904 bfd_vma
1905 bfd_scan_vma (const char *string, const char **end, int base)
1907 bfd_vma value;
1908 bfd_vma cutoff;
1909 unsigned int cutlim;
1910 int overflow;
1912 /* Let the host do it if possible. */
1913 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1914 return strtoul (string, (char **) end, base);
1916 if (sizeof (bfd_vma) <= sizeof (unsigned long long))
1917 return strtoull (string, (char **) end, base);
1919 if (base == 0)
1921 if (string[0] == '0')
1923 if ((string[1] == 'x') || (string[1] == 'X'))
1924 base = 16;
1925 else
1926 base = 8;
1930 if ((base < 2) || (base > 36))
1931 base = 10;
1933 if (base == 16
1934 && string[0] == '0'
1935 && (string[1] == 'x' || string[1] == 'X')
1936 && ISXDIGIT (string[2]))
1938 string += 2;
1941 cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
1942 cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
1943 value = 0;
1944 overflow = 0;
1945 while (1)
1947 unsigned int digit;
1949 digit = *string;
1950 if (ISDIGIT (digit))
1951 digit = digit - '0';
1952 else if (ISALPHA (digit))
1953 digit = TOUPPER (digit) - 'A' + 10;
1954 else
1955 break;
1956 if (digit >= (unsigned int) base)
1957 break;
1958 if (value > cutoff || (value == cutoff && digit > cutlim))
1959 overflow = 1;
1960 value = value * base + digit;
1961 ++string;
1964 if (overflow)
1965 value = ~ (bfd_vma) 0;
1967 if (end != NULL)
1968 *end = string;
1970 return value;
1974 FUNCTION
1975 bfd_copy_private_header_data
1977 SYNOPSIS
1978 bool bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
1980 DESCRIPTION
1981 Copy private BFD header information from the BFD @var{ibfd} to the
1982 the BFD @var{obfd}. This copies information that may require
1983 sections to exist, but does not require symbol tables. Return
1984 <<true>> on success, <<false>> on error.
1985 Possible error returns are:
1987 o <<bfd_error_no_memory>> -
1988 Not enough memory exists to create private data for @var{obfd}.
1990 .#define bfd_copy_private_header_data(ibfd, obfd) \
1991 . BFD_SEND (obfd, _bfd_copy_private_header_data, \
1992 . (ibfd, obfd))
1997 FUNCTION
1998 bfd_copy_private_bfd_data
2000 SYNOPSIS
2001 bool bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
2003 DESCRIPTION
2004 Copy private BFD information from the BFD @var{ibfd} to the
2005 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
2006 Possible error returns are:
2008 o <<bfd_error_no_memory>> -
2009 Not enough memory exists to create private data for @var{obfd}.
2011 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
2012 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
2013 . (ibfd, obfd))
2018 FUNCTION
2019 bfd_set_private_flags
2021 SYNOPSIS
2022 bool bfd_set_private_flags (bfd *abfd, flagword flags);
2024 DESCRIPTION
2025 Set private BFD flag information in the BFD @var{abfd}.
2026 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
2027 returns are:
2029 o <<bfd_error_no_memory>> -
2030 Not enough memory exists to create private data for @var{obfd}.
2032 .#define bfd_set_private_flags(abfd, flags) \
2033 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
2038 FUNCTION
2039 Other functions
2041 DESCRIPTION
2042 The following functions exist but have not yet been documented.
2044 .#define bfd_sizeof_headers(abfd, info) \
2045 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
2047 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
2048 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2049 . (abfd, syms, sec, off, file, func, line, NULL))
2051 .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
2052 . line, disc) \
2053 . BFD_SEND (abfd, _bfd_find_nearest_line, \
2054 . (abfd, syms, sec, off, file, func, line, disc))
2056 .#define bfd_find_line(abfd, syms, sym, file, line) \
2057 . BFD_SEND (abfd, _bfd_find_line, \
2058 . (abfd, syms, sym, file, line))
2060 .#define bfd_find_inliner_info(abfd, file, func, line) \
2061 . BFD_SEND (abfd, _bfd_find_inliner_info, \
2062 . (abfd, file, func, line))
2064 .#define bfd_debug_info_start(abfd) \
2065 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
2067 .#define bfd_debug_info_end(abfd) \
2068 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
2070 .#define bfd_debug_info_accumulate(abfd, section) \
2071 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
2073 .#define bfd_stat_arch_elt(abfd, stat) \
2074 . BFD_SEND (abfd->my_archive ? abfd->my_archive : abfd, \
2075 . _bfd_stat_arch_elt, (abfd, stat))
2077 .#define bfd_update_armap_timestamp(abfd) \
2078 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
2080 .#define bfd_set_arch_mach(abfd, arch, mach)\
2081 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
2083 .#define bfd_relax_section(abfd, section, link_info, again) \
2084 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
2086 .#define bfd_gc_sections(abfd, link_info) \
2087 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
2089 .#define bfd_lookup_section_flags(link_info, flag_info, section) \
2090 . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
2092 .#define bfd_merge_sections(abfd, link_info) \
2093 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
2095 .#define bfd_is_group_section(abfd, sec) \
2096 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
2098 .#define bfd_group_name(abfd, sec) \
2099 . BFD_SEND (abfd, _bfd_group_name, (abfd, sec))
2101 .#define bfd_discard_group(abfd, sec) \
2102 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
2104 .#define bfd_link_hash_table_create(abfd) \
2105 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
2107 .#define bfd_link_add_symbols(abfd, info) \
2108 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
2110 .#define bfd_link_just_syms(abfd, sec, info) \
2111 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
2113 .#define bfd_final_link(abfd, info) \
2114 . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
2116 .#define bfd_free_cached_info(abfd) \
2117 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
2119 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
2120 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
2122 .#define bfd_print_private_bfd_data(abfd, file)\
2123 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
2125 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
2126 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
2128 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
2129 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
2130 . dyncount, dynsyms, ret))
2132 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
2133 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
2135 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
2136 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
2138 .extern bfd_byte *bfd_get_relocated_section_contents
2139 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
2140 . bool, asymbol **);
2145 bfd_byte *
2146 bfd_get_relocated_section_contents (bfd *abfd,
2147 struct bfd_link_info *link_info,
2148 struct bfd_link_order *link_order,
2149 bfd_byte *data,
2150 bool relocatable,
2151 asymbol **symbols)
2153 bfd *abfd2;
2154 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
2155 bfd_byte *, bool, asymbol **);
2157 if (link_order->type == bfd_indirect_link_order)
2159 abfd2 = link_order->u.indirect.section->owner;
2160 if (abfd2 == NULL)
2161 abfd2 = abfd;
2163 else
2164 abfd2 = abfd;
2166 fn = abfd2->xvec->_bfd_get_relocated_section_contents;
2168 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
2171 /* Record information about an ELF program header. */
2173 bool
2174 bfd_record_phdr (bfd *abfd,
2175 unsigned long type,
2176 bool flags_valid,
2177 flagword flags,
2178 bool at_valid,
2179 bfd_vma at, /* Bytes. */
2180 bool includes_filehdr,
2181 bool includes_phdrs,
2182 unsigned int count,
2183 asection **secs)
2185 struct elf_segment_map *m, **pm;
2186 size_t amt;
2187 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2189 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2190 return true;
2192 amt = sizeof (struct elf_segment_map);
2193 amt += ((bfd_size_type) count - 1) * sizeof (asection *);
2194 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2195 if (m == NULL)
2196 return false;
2198 m->p_type = type;
2199 m->p_flags = flags;
2200 m->p_paddr = at * opb;
2201 m->p_flags_valid = flags_valid;
2202 m->p_paddr_valid = at_valid;
2203 m->includes_filehdr = includes_filehdr;
2204 m->includes_phdrs = includes_phdrs;
2205 m->count = count;
2206 if (count > 0)
2207 memcpy (m->sections, secs, count * sizeof (asection *));
2209 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
2211 *pm = m;
2213 return true;
2216 #ifdef BFD64
2217 /* Return true iff this target is 32-bit. */
2219 static bool
2220 is32bit (bfd *abfd)
2222 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2224 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2225 return bed->s->elfclass == ELFCLASS32;
2228 /* For non-ELF targets, use architecture information. */
2229 return bfd_arch_bits_per_address (abfd) <= 32;
2231 #endif
2233 /* bfd_sprintf_vma and bfd_fprintf_vma display an address in the
2234 target's address size. */
2236 void
2237 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
2239 #ifdef BFD64
2240 if (is32bit (abfd))
2242 sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
2243 return;
2245 #endif
2246 sprintf_vma (buf, value);
2249 void
2250 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
2252 #ifdef BFD64
2253 if (is32bit (abfd))
2255 fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
2256 return;
2258 #endif
2259 fprintf_vma ((FILE *) stream, value);
2263 FUNCTION
2264 bfd_alt_mach_code
2266 SYNOPSIS
2267 bool bfd_alt_mach_code (bfd *abfd, int alternative);
2269 DESCRIPTION
2271 When more than one machine code number is available for the
2272 same machine type, this function can be used to switch between
2273 the preferred one (alternative == 0) and any others. Currently,
2274 only ELF supports this feature, with up to two alternate
2275 machine codes.
2278 bool
2279 bfd_alt_mach_code (bfd *abfd, int alternative)
2281 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2283 int code;
2285 switch (alternative)
2287 case 0:
2288 code = get_elf_backend_data (abfd)->elf_machine_code;
2289 break;
2291 case 1:
2292 code = get_elf_backend_data (abfd)->elf_machine_alt1;
2293 if (code == 0)
2294 return false;
2295 break;
2297 case 2:
2298 code = get_elf_backend_data (abfd)->elf_machine_alt2;
2299 if (code == 0)
2300 return false;
2301 break;
2303 default:
2304 return false;
2307 elf_elfheader (abfd)->e_machine = code;
2309 return true;
2312 return false;
2316 FUNCTION
2317 bfd_emul_get_maxpagesize
2319 SYNOPSIS
2320 bfd_vma bfd_emul_get_maxpagesize (const char *);
2322 DESCRIPTION
2323 Returns the maximum page size, in bytes, as determined by
2324 emulation.
2326 RETURNS
2327 Returns the maximum page size in bytes for ELF, 0 otherwise.
2330 bfd_vma
2331 bfd_emul_get_maxpagesize (const char *emul)
2333 const bfd_target *target;
2335 target = bfd_find_target (emul, NULL);
2336 if (target != NULL
2337 && target->flavour == bfd_target_elf_flavour)
2338 return xvec_get_elf_backend_data (target)->maxpagesize;
2340 return 0;
2344 FUNCTION
2345 bfd_emul_get_commonpagesize
2347 SYNOPSIS
2348 bfd_vma bfd_emul_get_commonpagesize (const char *, bool);
2350 DESCRIPTION
2351 Returns the common page size, in bytes, as determined by
2352 emulation.
2354 RETURNS
2355 Returns the common page size in bytes for ELF, 0 otherwise.
2358 bfd_vma
2359 bfd_emul_get_commonpagesize (const char *emul, bool relro)
2361 const bfd_target *target;
2363 target = bfd_find_target (emul, NULL);
2364 if (target != NULL
2365 && target->flavour == bfd_target_elf_flavour)
2367 const struct elf_backend_data *bed;
2369 bed = xvec_get_elf_backend_data (target);
2370 if (relro)
2371 return bed->relropagesize;
2372 else
2373 return bed->commonpagesize;
2375 return 0;
2379 FUNCTION
2380 bfd_demangle
2382 SYNOPSIS
2383 char *bfd_demangle (bfd *, const char *, int);
2385 DESCRIPTION
2386 Wrapper around cplus_demangle. Strips leading underscores and
2387 other such chars that would otherwise confuse the demangler.
2388 If passed a g++ v3 ABI mangled name, returns a buffer allocated
2389 with malloc holding the demangled name. Returns NULL otherwise
2390 and on memory alloc failure.
2393 char *
2394 bfd_demangle (bfd *abfd, const char *name, int options)
2396 char *res, *alloc;
2397 const char *pre, *suf;
2398 size_t pre_len;
2399 bool skip_lead;
2401 skip_lead = (abfd != NULL
2402 && *name != '\0'
2403 && bfd_get_symbol_leading_char (abfd) == *name);
2404 if (skip_lead)
2405 ++name;
2407 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
2408 or the MS PE format. These formats have a number of leading '.'s
2409 on at least some symbols, so we remove all dots to avoid
2410 confusing the demangler. */
2411 pre = name;
2412 while (*name == '.' || *name == '$')
2413 ++name;
2414 pre_len = name - pre;
2416 /* Strip off @plt and suchlike too. */
2417 alloc = NULL;
2418 suf = strchr (name, '@');
2419 if (suf != NULL)
2421 alloc = (char *) bfd_malloc (suf - name + 1);
2422 if (alloc == NULL)
2423 return NULL;
2424 memcpy (alloc, name, suf - name);
2425 alloc[suf - name] = '\0';
2426 name = alloc;
2429 res = cplus_demangle (name, options);
2431 free (alloc);
2433 if (res == NULL)
2435 if (skip_lead)
2437 size_t len = strlen (pre) + 1;
2438 alloc = (char *) bfd_malloc (len);
2439 if (alloc == NULL)
2440 return NULL;
2441 memcpy (alloc, pre, len);
2442 return alloc;
2444 return NULL;
2447 /* Put back any prefix or suffix. */
2448 if (pre_len != 0 || suf != NULL)
2450 size_t len;
2451 size_t suf_len;
2452 char *final;
2454 len = strlen (res);
2455 if (suf == NULL)
2456 suf = res + len;
2457 suf_len = strlen (suf) + 1;
2458 final = (char *) bfd_malloc (pre_len + len + suf_len);
2459 if (final != NULL)
2461 memcpy (final, pre, pre_len);
2462 memcpy (final + pre_len, res, len);
2463 memcpy (final + pre_len + len, suf, suf_len);
2465 free (res);
2466 res = final;
2469 return res;
2473 FUNCTION
2474 bfd_update_compression_header
2476 SYNOPSIS
2477 void bfd_update_compression_header
2478 (bfd *abfd, bfd_byte *contents, asection *sec);
2480 DESCRIPTION
2481 Set the compression header at CONTENTS of SEC in ABFD and update
2482 elf_section_flags for compression.
2485 void
2486 bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
2487 asection *sec)
2489 if ((abfd->flags & BFD_COMPRESS) == 0)
2490 abort ();
2492 switch (bfd_get_flavour (abfd))
2494 case bfd_target_elf_flavour:
2495 if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
2497 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2498 struct bfd_elf_section_data * esd = elf_section_data (sec);
2500 /* Set the SHF_COMPRESSED bit. */
2501 elf_section_flags (sec) |= SHF_COMPRESSED;
2503 if (bed->s->elfclass == ELFCLASS32)
2505 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
2506 bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
2507 bfd_put_32 (abfd, sec->size, &echdr->ch_size);
2508 bfd_put_32 (abfd, 1u << sec->alignment_power,
2509 &echdr->ch_addralign);
2510 /* bfd_log2 (alignof (Elf32_Chdr)) */
2511 bfd_set_section_alignment (sec, 2);
2512 esd->this_hdr.sh_addralign = 4;
2514 else
2516 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
2517 bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
2518 bfd_put_32 (abfd, 0, &echdr->ch_reserved);
2519 bfd_put_64 (abfd, sec->size, &echdr->ch_size);
2520 bfd_put_64 (abfd, UINT64_C (1) << sec->alignment_power,
2521 &echdr->ch_addralign);
2522 /* bfd_log2 (alignof (Elf64_Chdr)) */
2523 bfd_set_section_alignment (sec, 3);
2524 esd->this_hdr.sh_addralign = 8;
2526 break;
2529 /* Clear the SHF_COMPRESSED bit. */
2530 elf_section_flags (sec) &= ~SHF_COMPRESSED;
2531 /* Fall through. */
2533 default:
2534 /* Write the zlib header. It should be "ZLIB" followed by
2535 the uncompressed section size, 8 bytes in big-endian
2536 order. */
2537 memcpy (contents, "ZLIB", 4);
2538 bfd_putb64 (sec->size, contents + 4);
2539 /* No way to keep the original alignment, just use 1 always. */
2540 bfd_set_section_alignment (sec, 0);
2541 break;
2546 FUNCTION
2547 bfd_check_compression_header
2549 SYNOPSIS
2550 bool bfd_check_compression_header
2551 (bfd *abfd, bfd_byte *contents, asection *sec,
2552 bfd_size_type *uncompressed_size,
2553 unsigned int *uncompressed_alignment_power);
2555 DESCRIPTION
2556 Check the compression header at CONTENTS of SEC in ABFD and
2557 store the uncompressed size in UNCOMPRESSED_SIZE and the
2558 uncompressed data alignment in UNCOMPRESSED_ALIGNMENT_POWER
2559 if the compression header is valid.
2561 RETURNS
2562 Return TRUE if the compression header is valid.
2565 bool
2566 bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
2567 asection *sec,
2568 bfd_size_type *uncompressed_size,
2569 unsigned int *uncompressed_alignment_power)
2571 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2572 && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
2574 Elf_Internal_Chdr chdr;
2575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2576 if (bed->s->elfclass == ELFCLASS32)
2578 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
2579 chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
2580 chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
2581 chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
2583 else
2585 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
2586 chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
2587 chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
2588 chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
2590 if (chdr.ch_type == ELFCOMPRESS_ZLIB
2591 && chdr.ch_addralign == (chdr.ch_addralign & -chdr.ch_addralign))
2593 *uncompressed_size = chdr.ch_size;
2594 *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
2595 return true;
2599 return false;
2603 FUNCTION
2604 bfd_get_compression_header_size
2606 SYNOPSIS
2607 int bfd_get_compression_header_size (bfd *abfd, asection *sec);
2609 DESCRIPTION
2610 Return the size of the compression header of SEC in ABFD.
2612 RETURNS
2613 Return the size of the compression header in bytes.
2617 bfd_get_compression_header_size (bfd *abfd, asection *sec)
2619 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2621 if (sec == NULL)
2623 if (!(abfd->flags & BFD_COMPRESS_GABI))
2624 return 0;
2626 else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
2627 return 0;
2629 if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
2630 return sizeof (Elf32_External_Chdr);
2631 else
2632 return sizeof (Elf64_External_Chdr);
2635 return 0;
2639 FUNCTION
2640 bfd_convert_section_size
2642 SYNOPSIS
2643 bfd_size_type bfd_convert_section_size
2644 (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size);
2646 DESCRIPTION
2647 Convert the size @var{size} of the section @var{isec} in input
2648 BFD @var{ibfd} to the section size in output BFD @var{obfd}.
2651 bfd_size_type
2652 bfd_convert_section_size (bfd *ibfd, sec_ptr isec, bfd *obfd,
2653 bfd_size_type size)
2655 bfd_size_type hdr_size;
2657 /* Do nothing if either input or output aren't ELF. */
2658 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2659 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2660 return size;
2662 /* Do nothing if ELF classes of input and output are the same. */
2663 if (get_elf_backend_data (ibfd)->s->elfclass
2664 == get_elf_backend_data (obfd)->s->elfclass)
2665 return size;
2667 /* Convert GNU property size. */
2668 if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
2669 return _bfd_elf_convert_gnu_property_size (ibfd, obfd);
2671 /* Do nothing if input file will be decompressed. */
2672 if ((ibfd->flags & BFD_DECOMPRESS))
2673 return size;
2675 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
2676 hdr_size = bfd_get_compression_header_size (ibfd, isec);
2677 if (hdr_size == 0)
2678 return size;
2680 /* Adjust the size of the output SHF_COMPRESSED section. */
2681 if (hdr_size == sizeof (Elf32_External_Chdr))
2682 return (size - sizeof (Elf32_External_Chdr)
2683 + sizeof (Elf64_External_Chdr));
2684 else
2685 return (size - sizeof (Elf64_External_Chdr)
2686 + sizeof (Elf32_External_Chdr));
2690 FUNCTION
2691 bfd_convert_section_contents
2693 SYNOPSIS
2694 bool bfd_convert_section_contents
2695 (bfd *ibfd, asection *isec, bfd *obfd,
2696 bfd_byte **ptr, bfd_size_type *ptr_size);
2698 DESCRIPTION
2699 Convert the contents, stored in @var{*ptr}, of the section
2700 @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
2701 if needed. The original buffer pointed to by @var{*ptr} may
2702 be freed and @var{*ptr} is returned with memory malloc'd by this
2703 function, and the new size written to @var{ptr_size}.
2706 bool
2707 bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
2708 bfd_byte **ptr, bfd_size_type *ptr_size)
2710 bfd_byte *contents;
2711 bfd_size_type ihdr_size, ohdr_size, size;
2712 Elf_Internal_Chdr chdr;
2713 bool use_memmove;
2715 /* Do nothing if either input or output aren't ELF. */
2716 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2717 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2718 return true;
2720 /* Do nothing if ELF classes of input and output are the same. */
2721 if (get_elf_backend_data (ibfd)->s->elfclass
2722 == get_elf_backend_data (obfd)->s->elfclass)
2723 return true;
2725 /* Convert GNU properties. */
2726 if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
2727 return _bfd_elf_convert_gnu_properties (ibfd, isec, obfd, ptr,
2728 ptr_size);
2730 /* Do nothing if input file will be decompressed. */
2731 if ((ibfd->flags & BFD_DECOMPRESS))
2732 return true;
2734 /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
2735 ihdr_size = bfd_get_compression_header_size (ibfd, isec);
2736 if (ihdr_size == 0)
2737 return true;
2739 /* PR 25221. Check for corrupt input sections. */
2740 if (ihdr_size > bfd_get_section_limit (ibfd, isec))
2741 /* FIXME: Issue a warning about a corrupt
2742 compression header size field ? */
2743 return false;
2745 contents = *ptr;
2747 /* Convert the contents of the input SHF_COMPRESSED section to
2748 output. Get the input compression header and the size of the
2749 output compression header. */
2750 if (ihdr_size == sizeof (Elf32_External_Chdr))
2752 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
2753 chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
2754 chdr.ch_size = bfd_get_32 (ibfd, &echdr->ch_size);
2755 chdr.ch_addralign = bfd_get_32 (ibfd, &echdr->ch_addralign);
2757 ohdr_size = sizeof (Elf64_External_Chdr);
2759 use_memmove = false;
2761 else if (ihdr_size != sizeof (Elf64_External_Chdr))
2763 /* FIXME: Issue a warning about a corrupt
2764 compression header size field ? */
2765 return false;
2767 else
2769 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
2770 chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
2771 chdr.ch_size = bfd_get_64 (ibfd, &echdr->ch_size);
2772 chdr.ch_addralign = bfd_get_64 (ibfd, &echdr->ch_addralign);
2774 ohdr_size = sizeof (Elf32_External_Chdr);
2775 use_memmove = true;
2778 size = bfd_section_size (isec) - ihdr_size + ohdr_size;
2779 if (!use_memmove)
2781 contents = (bfd_byte *) bfd_malloc (size);
2782 if (contents == NULL)
2783 return false;
2786 /* Write out the output compression header. */
2787 if (ohdr_size == sizeof (Elf32_External_Chdr))
2789 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
2790 bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
2791 bfd_put_32 (obfd, chdr.ch_size, &echdr->ch_size);
2792 bfd_put_32 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
2794 else
2796 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
2797 bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
2798 bfd_put_32 (obfd, 0, &echdr->ch_reserved);
2799 bfd_put_64 (obfd, chdr.ch_size, &echdr->ch_size);
2800 bfd_put_64 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
2803 /* Copy the compressed contents. */
2804 if (use_memmove)
2805 memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
2806 else
2808 memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
2809 free (*ptr);
2810 *ptr = contents;
2813 *ptr_size = size;
2814 return true;
2817 /* Get the linker information. */
2819 struct bfd_link_info *
2820 _bfd_get_link_info (bfd *abfd)
2822 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
2823 return NULL;
2825 return elf_link_info (abfd);