Coverage Report

Created: 2023-06-29 07:03

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