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