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