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