Subversion Repositories Kolibri OS

Rev

Rev 5197 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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