Coverage Report

Created: 2023-08-28 06:26

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