Subversion Repositories Kolibri OS

Rev

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