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