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