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