Coverage Report

Created: 2026-03-10 08:46

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