Coverage Report

Created: 2026-04-04 08:16

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