Coverage Report

Created: 2025-06-24 06:45

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