Coverage Report

Created: 2024-05-21 06:29

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