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