Coverage Report

Created: 2026-07-25 10:20

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