home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / bfd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  28.4 KB  |  1,149 lines

  1. /* Generic BFD library interface and support routines.
  2.    Copyright (C) 1990, 91, 92, 93, 94, 95, 1996 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /*
  22. SECTION
  23.     <<typedef bfd>>
  24.  
  25.     A BFD has type <<bfd>>; objects of this type are the
  26.     cornerstone of any application using BFD. Using BFD
  27.     consists of making references though the BFD and to data in the BFD.
  28.  
  29.     Here is the structure that defines the type <<bfd>>.  It
  30.     contains the major data about the file and pointers
  31.     to the rest of the data.
  32.  
  33. CODE_FRAGMENT
  34. .
  35. .struct _bfd 
  36. .{
  37. .    {* The filename the application opened the BFD with.  *}
  38. .    CONST char *filename;                
  39. .
  40. .    {* A pointer to the target jump table.             *}
  41. .    const struct bfd_target *xvec;
  42. .
  43. .    {* To avoid dragging too many header files into every file that
  44. .       includes `<<bfd.h>>', IOSTREAM has been declared as a "char
  45. .       *", and MTIME as a "long".  Their correct types, to which they
  46. .       are cast when used, are "FILE *" and "time_t".    The iostream
  47. .       is the result of an fopen on the filename.  However, if the
  48. .       BFD_IN_MEMORY flag is set, then iostream is actually a pointer
  49. .       to a bfd_in_memory struct.  *}
  50. .    PTR iostream;
  51. .
  52. .    {* Is the file descriptor being cached?  That is, can it be closed as
  53. .       needed, and re-opened when accessed later?  *}
  54. .
  55. .    boolean cacheable;
  56. .
  57. .    {* Marks whether there was a default target specified when the
  58. .       BFD was opened. This is used to select which matching algorithm
  59. .       to use to choose the back end. *}
  60. .
  61. .    boolean target_defaulted;
  62. .
  63. .    {* The caching routines use these to maintain a
  64. .       least-recently-used list of BFDs *}
  65. .
  66. .    struct _bfd *lru_prev, *lru_next;
  67. .
  68. .    {* When a file is closed by the caching routines, BFD retains
  69. .       state information on the file here: *}
  70. .
  71. .    file_ptr where;              
  72. .
  73. .    {* and here: (``once'' means at least once) *}
  74. .
  75. .    boolean opened_once;
  76. .
  77. .    {* Set if we have a locally maintained mtime value, rather than
  78. .       getting it from the file each time: *}
  79. .
  80. .    boolean mtime_set;
  81. .
  82. .    {* File modified time, if mtime_set is true: *}
  83. .
  84. .    long mtime;          
  85. .
  86. .    {* Reserved for an unimplemented file locking extension.*}
  87. .
  88. .    int ifd;
  89. .
  90. .    {* The format which belongs to the BFD. (object, core, etc.) *}
  91. .
  92. .    bfd_format format;
  93. .
  94. .    {* The direction the BFD was opened with*}
  95. .
  96. .    enum bfd_direction {no_direction = 0,
  97. .                        read_direction = 1,
  98. .                        write_direction = 2,
  99. .                        both_direction = 3} direction;
  100. .
  101. .    {* Format_specific flags*}
  102. .
  103. .    flagword flags;              
  104. .
  105. .    {* Currently my_archive is tested before adding origin to
  106. .       anything. I believe that this can become always an add of
  107. .       origin, with origin set to 0 for non archive files.   *}
  108. .
  109. .    file_ptr origin;             
  110. .
  111. .    {* Remember when output has begun, to stop strange things
  112. .       from happening. *}
  113. .    boolean output_has_begun;
  114. .
  115. .    {* Pointer to linked list of sections*}
  116. .    struct sec  *sections;
  117. .
  118. .    {* The number of sections *}
  119. .    unsigned int section_count;
  120. .
  121. .    {* Stuff only useful for object files: 
  122. .       The start address. *}
  123. .    bfd_vma start_address;
  124. .
  125. .    {* Used for input and output*}
  126. .    unsigned int symcount;
  127. .
  128. .    {* Symbol table for output BFD (with symcount entries) *}
  129. .    struct symbol_cache_entry  **outsymbols;             
  130. .
  131. .    {* Pointer to structure which contains architecture information*}
  132. .    const struct bfd_arch_info *arch_info;
  133. .
  134. .    {* Stuff only useful for archives:*}
  135. .    PTR arelt_data;              
  136. .    struct _bfd *my_archive;     {* The containing archive BFD.  *}
  137. .    struct _bfd *next;           {* The next BFD in the archive.  *}
  138. .    struct _bfd *archive_head;   {* The first BFD in the archive.  *}
  139. .    boolean has_armap;           
  140. .
  141. .    {* A chain of BFD structures involved in a link.  *}
  142. .    struct _bfd *link_next;
  143. .
  144. .    {* A field used by _bfd_generic_link_add_archive_symbols.  This will
  145. .       be used only for archive elements.  *}
  146. .    int archive_pass;
  147. .
  148. .    {* Used by the back end to hold private data. *}
  149. .
  150. .    union 
  151. .      {
  152. .      struct aout_data_struct *aout_data;
  153. .      struct artdata *aout_ar_data;
  154. .      struct _oasys_data *oasys_obj_data;
  155. .      struct _oasys_ar_data *oasys_ar_data;
  156. .      struct coff_tdata *coff_obj_data;
  157. .      struct pe_tdata *pe_obj_data;
  158. .      struct xcoff_tdata *xcoff_obj_data;
  159. .      struct ecoff_tdata *ecoff_obj_data;
  160. .      struct ieee_data_struct *ieee_data;
  161. .      struct ieee_ar_data_struct *ieee_ar_data;
  162. .      struct srec_data_struct *srec_data;
  163. .      struct ihex_data_struct *ihex_data;
  164. .      struct tekhex_data_struct *tekhex_data;
  165. .      struct elf_obj_tdata *elf_obj_data;
  166. .      struct nlm_obj_tdata *nlm_obj_data;
  167. .      struct bout_data_struct *bout_data;
  168. .      struct sun_core_struct *sun_core_data;
  169. .      struct trad_core_struct *trad_core_data;
  170. .      struct som_data_struct *som_data;
  171. .      struct hpux_core_struct *hpux_core_data;
  172. .      struct hppabsd_core_struct *hppabsd_core_data;
  173. .      struct sgi_core_struct *sgi_core_data;
  174. .      struct lynx_core_struct *lynx_core_data;
  175. .      struct osf_core_struct *osf_core_data;
  176. .      struct cisco_core_struct *cisco_core_data;
  177. .      struct versados_data_struct *versados_data;
  178. .      struct netbsd_core_struct *netbsd_core_data;
  179. .      PTR any;
  180. .      } tdata;
  181. .  
  182. .    {* Used by the application to hold private data*}
  183. .    PTR usrdata;
  184. .
  185. .    {* Where all the allocated stuff under this BFD goes *}
  186. .    struct obstack memory;
  187. .};
  188. .
  189. */
  190.  
  191. #include "bfd.h"
  192. #include "sysdep.h"
  193.  
  194. #ifdef ANSI_PROTOTYPES
  195. #include <stdarg.h>
  196. #else
  197. #include <varargs.h>
  198. #endif
  199.  
  200. #include "bfdlink.h"
  201. #include "libbfd.h"
  202. #include "coff/internal.h"
  203. #include "coff/sym.h"
  204. #include "libcoff.h"
  205. #include "libecoff.h"
  206. #undef obj_symbols
  207. #include "elf-bfd.h"
  208.  
  209. #include <ctype.h>
  210.  
  211. /* provide storage for subsystem, stack and heap data which may have been
  212.    passed in on the command line.  Ld puts this data into a bfd_link_info
  213.    struct which ultimately gets passed in to the bfd.  When it arrives, copy
  214.    it to the following struct so that the data will be available in coffcode.h
  215.    where it is needed.  The typedef's used are defined in bfd.h */
  216.  
  217.  
  218.  
  219. /*
  220. SECTION
  221.     Error reporting
  222.  
  223.     Most BFD functions return nonzero on success (check their
  224.     individual documentation for precise semantics).  On an error,
  225.     they call <<bfd_set_error>> to set an error condition that callers
  226.     can check by calling <<bfd_get_error>>.
  227.         If that returns <<bfd_error_system_call>>, then check
  228.     <<errno>>.
  229.  
  230.     The easiest way to report a BFD error to the user is to
  231.     use <<bfd_perror>>.
  232.  
  233. SUBSECTION
  234.     Type <<bfd_error_type>>
  235.  
  236.     The values returned by <<bfd_get_error>> are defined by the
  237.     enumerated type <<bfd_error_type>>.
  238.  
  239. CODE_FRAGMENT
  240. .
  241. .typedef enum bfd_error
  242. .{
  243. .  bfd_error_no_error = 0,
  244. .  bfd_error_system_call,
  245. .  bfd_error_invalid_target,
  246. .  bfd_error_wrong_format,
  247. .  bfd_error_invalid_operation,
  248. .  bfd_error_no_memory,
  249. .  bfd_error_no_symbols,
  250. .  bfd_error_no_armap,
  251. .  bfd_error_no_more_archived_files,
  252. .  bfd_error_malformed_archive,
  253. .  bfd_error_file_not_recognized,
  254. .  bfd_error_file_ambiguously_recognized,
  255. .  bfd_error_no_contents,
  256. .  bfd_error_nonrepresentable_section,
  257. .  bfd_error_no_debug_section,
  258. .  bfd_error_bad_value,
  259. .  bfd_error_file_truncated,
  260. .  bfd_error_file_too_big,
  261. .  bfd_error_invalid_error_code
  262. .} bfd_error_type;
  263. .
  264. */
  265.  
  266. #undef strerror
  267. extern char *strerror();
  268.  
  269. static bfd_error_type bfd_error = bfd_error_no_error;
  270.  
  271. CONST char *CONST bfd_errmsgs[] = {
  272.                         "No error",
  273.                         "System call error",
  274.                         "Invalid bfd target",
  275.                         "File in wrong format",
  276.                         "Invalid operation",
  277.                         "Memory exhausted",
  278.                         "No symbols",
  279.             "Archive has no index; run ranlib to add one",
  280.                         "No more archived files",
  281.                         "Malformed archive",
  282.                         "File format not recognized",
  283.                         "File format is ambiguous",
  284.                         "Section has no contents",
  285.                         "Nonrepresentable section on output",
  286.             "Symbol needs debug section which does not exist",
  287.             "Bad value",
  288.             "File truncated",
  289.             "File too big",
  290.                         "#<Invalid error code>"
  291.                        };
  292.  
  293. /*
  294. FUNCTION
  295.     bfd_get_error
  296.  
  297. SYNOPSIS
  298.     bfd_error_type bfd_get_error (void);
  299.  
  300. DESCRIPTION
  301.     Return the current BFD error condition.
  302. */
  303.  
  304. bfd_error_type
  305. bfd_get_error ()
  306. {
  307.   return bfd_error;
  308. }
  309.  
  310. /*
  311. FUNCTION
  312.     bfd_set_error
  313.  
  314. SYNOPSIS
  315.     void bfd_set_error (bfd_error_type error_tag);
  316.  
  317. DESCRIPTION
  318.     Set the BFD error condition to be @var{error_tag}.
  319. */
  320.  
  321. void
  322. bfd_set_error (error_tag)
  323.      bfd_error_type error_tag;
  324. {
  325.   bfd_error = error_tag;
  326. }
  327.  
  328. /*
  329. FUNCTION
  330.     bfd_errmsg
  331.  
  332. SYNOPSIS
  333.     CONST char *bfd_errmsg (bfd_error_type error_tag);
  334.  
  335. DESCRIPTION
  336.     Return a string describing the error @var{error_tag}, or
  337.     the system error if @var{error_tag} is <<bfd_error_system_call>>.
  338. */
  339.  
  340. CONST char *
  341. bfd_errmsg (error_tag)
  342.      bfd_error_type error_tag;
  343. {
  344. #ifndef errno
  345.   extern int errno;
  346. #endif
  347.   if (error_tag == bfd_error_system_call)
  348.     {
  349.       const char *errmsg;
  350.  
  351.       errmsg = strerror (errno);
  352.       if (errmsg == NULL)
  353.     {
  354.       static char buf[32];
  355.  
  356.       sprintf (buf, "Error %d", errno);
  357.       errmsg = buf;
  358.     }
  359.       return errmsg;
  360.     }
  361.  
  362.   if ((((int)error_tag <(int) bfd_error_no_error) ||
  363.        ((int)error_tag > (int)bfd_error_invalid_error_code)))
  364.     error_tag = bfd_error_invalid_error_code;/* sanity check */
  365.  
  366.   return bfd_errmsgs [(int)error_tag];
  367. }
  368.  
  369. /*
  370. FUNCTION
  371.     bfd_perror
  372.  
  373. SYNOPSIS
  374.     void bfd_perror (CONST char *message);
  375.  
  376. DESCRIPTION
  377.     Print to the standard error stream a string describing the
  378.     last BFD error that occurred, or the last system error if
  379.     the last BFD error was a system call failure.  If @var{message}
  380.     is non-NULL and non-empty, the error string printed is preceded
  381.     by @var{message}, a colon, and a space.  It is followed by a newline.
  382. */
  383.  
  384. void
  385. bfd_perror (message)
  386.      CONST char *message;
  387. {
  388.   if (bfd_get_error () == bfd_error_system_call)
  389.     perror((char *)message);            /* must be system error then... */
  390.   else {
  391.     if (message == NULL || *message == '\0')
  392.       fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
  393.     else
  394.       fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
  395.   }
  396. }
  397.  
  398. /*
  399. SUBSECTION
  400.     BFD error handler
  401.  
  402.     Some BFD functions want to print messages describing the
  403.     problem.  They call a BFD error handler function.  This
  404.     function may be overriden by the program.
  405.  
  406.     The BFD error handler acts like printf.
  407.  
  408. CODE_FRAGMENT
  409. .
  410. .typedef void (*bfd_error_handler_type) PARAMS ((const char *, ...));
  411. .
  412. */
  413.  
  414. /* The program name used when printing BFD error messages.  */
  415.  
  416. static const char *_bfd_error_program_name;
  417.  
  418. /* This is the default routine to handle BFD error messages.  */
  419.  
  420. #ifdef ANSI_PROTOTYPES
  421.  
  422. static void _bfd_default_error_handler PARAMS ((const char *s, ...));
  423.  
  424. static void
  425. _bfd_default_error_handler (const char *s, ...)
  426. {
  427.   va_list p;
  428.  
  429.   if (_bfd_error_program_name != NULL)
  430.     fprintf (stderr, "%s: ", _bfd_error_program_name);
  431.   else
  432.     fprintf (stderr, "BFD: ");
  433.  
  434.   va_start (p, s);
  435.  
  436.   vfprintf (stderr, s, p);
  437.  
  438.   va_end (p);
  439.  
  440.   fprintf (stderr, "\n");
  441. }
  442.  
  443. #else /* ! defined (ANSI_PROTOTYPES) */
  444.  
  445. static void _bfd_default_error_handler ();
  446.  
  447. static void
  448. _bfd_default_error_handler (va_alist)
  449.      va_dcl
  450. {
  451.   va_list p;
  452.   const char *s;
  453.  
  454.   if (_bfd_error_program_name != NULL)
  455.     fprintf (stderr, "%s: ", _bfd_error_program_name);
  456.   else
  457.     fprintf (stderr, "BFD: ");
  458.  
  459.   va_start (p);
  460.  
  461.   s = va_arg (p, const char *);
  462.   vfprintf (stderr, s, p);
  463.  
  464.   va_end (p);
  465.  
  466.   fprintf (stderr, "\n");
  467. }
  468.  
  469. #endif /* ! defined (ANSI_PROTOTYPES) */
  470.  
  471. /* This is a function pointer to the routine which should handle BFD
  472.    error messages.  It is called when a BFD routine encounters an
  473.    error for which it wants to print a message.  Going through a
  474.    function pointer permits a program linked against BFD to intercept
  475.    the messages and deal with them itself.  */
  476.  
  477. bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
  478.  
  479. /*
  480. FUNCTION
  481.     bfd_set_error_handler
  482.  
  483. SYNOPSIS
  484.     bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
  485.  
  486. DESCRIPTION
  487.     Set the BFD error handler function.  Returns the previous
  488.     function.
  489. */
  490.  
  491. bfd_error_handler_type
  492. bfd_set_error_handler (pnew)
  493.      bfd_error_handler_type pnew;
  494. {
  495.   bfd_error_handler_type pold;
  496.  
  497.   pold = _bfd_error_handler;
  498.   _bfd_error_handler = pnew;
  499.   return pold;
  500. }
  501.  
  502. /*
  503. FUNCTION
  504.     bfd_set_error_program_name
  505.  
  506. SYNOPSIS
  507.     void bfd_set_error_program_name (const char *);
  508.  
  509. DESCRIPTION
  510.     Set the program name to use when printing a BFD error.  This
  511.     is printed before the error message followed by a colon and
  512.     space.  The string must not be changed after it is passed to
  513.     this function.
  514. */
  515.  
  516. void
  517. bfd_set_error_program_name (name)
  518.      const char *name;
  519. {
  520.   _bfd_error_program_name = name;
  521. }
  522.  
  523. /*
  524. SECTION
  525.     Symbols
  526. */
  527.  
  528. /*
  529. FUNCTION
  530.     bfd_get_reloc_upper_bound
  531.  
  532. SYNOPSIS
  533.     long bfd_get_reloc_upper_bound(bfd *abfd, asection *sect);
  534.  
  535. DESCRIPTION
  536.     Return the number of bytes required to store the
  537.     relocation information associated with section @var{sect}
  538.     attached to bfd @var{abfd}.  If an error occurs, return -1.
  539.  
  540. */
  541.  
  542.  
  543. long
  544. bfd_get_reloc_upper_bound (abfd, asect)
  545.      bfd *abfd;
  546.      sec_ptr asect;
  547. {
  548.   if (abfd->format != bfd_object) {
  549.     bfd_set_error (bfd_error_invalid_operation);
  550.     return -1;
  551.   }
  552.  
  553.   return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
  554. }
  555.  
  556. /*
  557. FUNCTION
  558.     bfd_canonicalize_reloc
  559.  
  560. SYNOPSIS
  561.     long bfd_canonicalize_reloc
  562.             (bfd *abfd,
  563.         asection *sec,
  564.         arelent **loc,
  565.         asymbol    **syms);
  566.  
  567. DESCRIPTION
  568.     Call the back end associated with the open BFD
  569.     @var{abfd} and translate the external form of the relocation
  570.     information attached to @var{sec} into the internal canonical
  571.     form.  Place the table into memory at @var{loc}, which has
  572.     been preallocated, usually by a call to
  573.     <<bfd_get_reloc_upper_bound>>.  Returns the number of relocs, or
  574.     -1 on error.
  575.  
  576.     The @var{syms} table is also needed for horrible internal magic
  577.     reasons.
  578.  
  579.  
  580. */
  581. long
  582. bfd_canonicalize_reloc (abfd, asect, location, symbols)
  583.      bfd *abfd;
  584.      sec_ptr asect;
  585.      arelent **location;
  586.      asymbol **symbols;
  587. {
  588.   if (abfd->format != bfd_object) {
  589.     bfd_set_error (bfd_error_invalid_operation);
  590.     return -1;
  591.   }
  592.   return BFD_SEND (abfd, _bfd_canonicalize_reloc,
  593.            (abfd, asect, location, symbols));
  594. }
  595.  
  596. /*
  597. FUNCTION
  598.     bfd_set_reloc
  599.  
  600. SYNOPSIS
  601.     void bfd_set_reloc
  602.       (bfd *abfd, asection *sec, arelent **rel, unsigned int count)
  603.  
  604. DESCRIPTION
  605.     Set the relocation pointer and count within
  606.     section @var{sec} to the values @var{rel} and @var{count}.
  607.     The argument @var{abfd} is ignored.
  608.  
  609. */
  610. /*ARGSUSED*/
  611. void
  612. bfd_set_reloc (ignore_abfd, asect, location, count)
  613.      bfd *ignore_abfd;
  614.      sec_ptr asect;
  615.      arelent **location;
  616.      unsigned int count;
  617. {
  618.   asect->orelocation  = location;
  619.   asect->reloc_count = count;
  620. }
  621.  
  622. /*
  623. FUNCTION
  624.     bfd_set_file_flags
  625.  
  626. SYNOPSIS
  627.     boolean bfd_set_file_flags(bfd *abfd, flagword flags);
  628.  
  629. DESCRIPTION
  630.     Set the flag word in the BFD @var{abfd} to the value @var{flags}.
  631.  
  632.     Possible errors are:
  633.     o <<bfd_error_wrong_format>> - The target bfd was not of object format.
  634.     o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
  635.     o <<bfd_error_invalid_operation>> -
  636.     The flag word contained a bit which was not applicable to the
  637.     type of file.  E.g., an attempt was made to set the <<D_PAGED>> bit
  638.     on a BFD format which does not support demand paging.
  639.  
  640. */
  641.  
  642. boolean
  643. bfd_set_file_flags (abfd, flags)
  644.      bfd *abfd;
  645.      flagword flags;
  646. {
  647.   if (abfd->format != bfd_object) {
  648.     bfd_set_error (bfd_error_wrong_format);
  649.     return false;
  650.   }
  651.  
  652.   if (bfd_read_p (abfd)) {
  653.     bfd_set_error (bfd_error_invalid_operation);
  654.     return false;
  655.   }
  656.  
  657.   bfd_get_file_flags (abfd) = flags;
  658.   if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
  659.     bfd_set_error (bfd_error_invalid_operation);
  660.     return false;
  661.   }
  662.  
  663. return true;
  664. }
  665.  
  666. void
  667. bfd_assert (file, line)
  668.      const char *file;
  669.      int line;
  670. {
  671.   (*_bfd_error_handler) ("bfd assertion fail %s:%d", file, line);
  672. }
  673.  
  674.  
  675. /*
  676. FUNCTION
  677.     bfd_set_start_address
  678.  
  679. SYNOPSIS
  680.      boolean bfd_set_start_address(bfd *abfd, bfd_vma vma);
  681.  
  682. DESCRIPTION
  683.     Make @var{vma} the entry point of output BFD @var{abfd}.
  684.  
  685. RETURNS
  686.     Returns <<true>> on success, <<false>> otherwise.
  687. */
  688.  
  689. boolean
  690. bfd_set_start_address(abfd, vma)
  691. bfd *abfd;
  692. bfd_vma vma;
  693. {
  694.   abfd->start_address = vma;
  695.   return true;
  696. }
  697.  
  698.  
  699. /*
  700. FUNCTION
  701.     bfd_get_mtime
  702.  
  703. SYNOPSIS
  704.     long bfd_get_mtime(bfd *abfd);
  705.  
  706. DESCRIPTION
  707.     Return the file modification time (as read from the file system, or
  708.     from the archive header for archive members).
  709.  
  710. */
  711.  
  712. long
  713. bfd_get_mtime (abfd)
  714.      bfd *abfd;
  715. {
  716.   FILE *fp;
  717.   struct stat buf;
  718.  
  719.   if (abfd->mtime_set)
  720.     return abfd->mtime;
  721.  
  722.   fp = bfd_cache_lookup (abfd);
  723.   if (0 != fstat (fileno (fp), &buf))
  724.     return 0;
  725.  
  726.   abfd->mtime = buf.st_mtime;        /* Save value in case anyone wants it */
  727.   return buf.st_mtime;
  728. }
  729.  
  730. /*
  731. FUNCTION
  732.     bfd_get_size
  733.  
  734. SYNOPSIS
  735.     long bfd_get_size(bfd *abfd);
  736.  
  737. DESCRIPTION
  738.     Return the file size (as read from file system) for the file
  739.     associated with BFD @var{abfd}.
  740.  
  741.     The initial motivation for, and use of, this routine is not
  742.     so we can get the exact size of the object the BFD applies to, since
  743.     that might not be generally possible (archive members for example).
  744.     It would be ideal if someone could eventually modify
  745.     it so that such results were guaranteed.
  746.  
  747.     Instead, we want to ask questions like "is this NNN byte sized
  748.     object I'm about to try read from file offset YYY reasonable?"
  749.     As as example of where we might do this, some object formats
  750.     use string tables for which the first <<sizeof(long)>> bytes of the
  751.     table contain the size of the table itself, including the size bytes.
  752.     If an application tries to read what it thinks is one of these
  753.     string tables, without some way to validate the size, and for
  754.     some reason the size is wrong (byte swapping error, wrong location
  755.     for the string table, etc.), the only clue is likely to be a read
  756.     error when it tries to read the table, or a "virtual memory
  757.     exhausted" error when it tries to allocate 15 bazillon bytes
  758.     of space for the 15 bazillon byte table it is about to read.
  759.     This function at least allows us to answer the quesion, "is the
  760.     size reasonable?".
  761. */
  762.  
  763. long
  764. bfd_get_size (abfd)
  765.      bfd *abfd;
  766. {
  767.   FILE *fp;
  768.   struct stat buf;
  769.  
  770.   if ((abfd->flags & BFD_IN_MEMORY) != 0)
  771.     return ((struct bfd_in_memory *) abfd->iostream)->size;
  772.  
  773.   fp = bfd_cache_lookup (abfd);
  774.   if (0 != fstat (fileno (fp), &buf))
  775.     return 0;
  776.  
  777.   return buf.st_size;
  778. }
  779.  
  780. /*
  781. FUNCTION
  782.     bfd_get_gp_size
  783.  
  784. SYNOPSIS
  785.     int bfd_get_gp_size(bfd *abfd);
  786.  
  787. DESCRIPTION
  788.     Return the maximum size of objects to be optimized using the GP
  789.     register under MIPS ECOFF.  This is typically set by the <<-G>>
  790.     argument to the compiler, assembler or linker.
  791. */
  792.  
  793. int
  794. bfd_get_gp_size (abfd)
  795.      bfd *abfd;
  796. {
  797.   if (abfd->format == bfd_object)
  798.     {
  799.       if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  800.     return ecoff_data (abfd)->gp_size;
  801.       else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  802.     return elf_gp_size (abfd);
  803.     }
  804.   return 0;
  805. }
  806.  
  807. /*
  808. FUNCTION
  809.     bfd_set_gp_size
  810.  
  811. SYNOPSIS
  812.     void bfd_set_gp_size(bfd *abfd, int i);
  813.  
  814. DESCRIPTION
  815.     Set the maximum size of objects to be optimized using the GP
  816.     register under ECOFF or MIPS ELF.  This is typically set by
  817.     the <<-G>> argument to the compiler, assembler or linker.
  818. */
  819.  
  820. void
  821. bfd_set_gp_size (abfd, i)
  822.      bfd *abfd;
  823.      int i;
  824. {
  825.   /* Don't try to set GP size on an archive or core file! */
  826.   if (abfd->format != bfd_object)
  827.     return;
  828.   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  829.     ecoff_data (abfd)->gp_size = i;
  830.   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  831.     elf_gp_size (abfd) = i;
  832. }
  833.  
  834. /* Get the GP value.  This is an internal function used by some of the
  835.    relocation special_function routines on targets which support a GP
  836.    register.  */
  837.  
  838. bfd_vma
  839. _bfd_get_gp_value (abfd)
  840.      bfd *abfd;
  841. {
  842.   if (abfd->format == bfd_object)
  843.     {
  844.       if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  845.     return ecoff_data (abfd)->gp;
  846.       else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  847.     return elf_gp (abfd);
  848.     }
  849.   return 0;
  850. }
  851.  
  852. /* Set the GP value.  */
  853.  
  854. void
  855. _bfd_set_gp_value (abfd, v)
  856.      bfd *abfd;
  857.      bfd_vma v;
  858. {
  859.   if (abfd->format != bfd_object)
  860.     return;
  861.   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  862.     ecoff_data (abfd)->gp = v;
  863.   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  864.     elf_gp (abfd) = v;
  865. }
  866.  
  867. /*
  868. FUNCTION
  869.     bfd_scan_vma
  870.  
  871. SYNOPSIS
  872.     bfd_vma bfd_scan_vma(CONST char *string, CONST char **end, int base);
  873.  
  874. DESCRIPTION
  875.     Convert, like <<strtoul>>, a numerical expression
  876.     @var{string} into a <<bfd_vma>> integer, and return that integer.
  877.     (Though without as many bells and whistles as <<strtoul>>.)
  878.     The expression is assumed to be unsigned (i.e., positive).
  879.     If given a @var{base}, it is used as the base for conversion.
  880.     A base of 0 causes the function to interpret the string
  881.     in hex if a leading "0x" or "0X" is found, otherwise
  882.     in octal if a leading zero is found, otherwise in decimal.
  883.  
  884.     Overflow is not detected.
  885. */
  886.  
  887. bfd_vma
  888. bfd_scan_vma (string, end, base)
  889.      CONST char *string;
  890.      CONST char **end;
  891.      int base;
  892. {
  893.   bfd_vma value;
  894.   int digit;
  895.  
  896.   /* Let the host do it if possible.  */
  897.   if (sizeof(bfd_vma) <= sizeof(unsigned long))
  898.     return (bfd_vma) strtoul (string, (char **) end, base);
  899.  
  900.   /* A negative base makes no sense, and we only need to go as high as hex.  */
  901.   if ((base < 0) || (base > 16))
  902.     return (bfd_vma) 0;
  903.  
  904.   if (base == 0)
  905.     {
  906.       if (string[0] == '0')
  907.     {
  908.       if ((string[1] == 'x') || (string[1] == 'X'))
  909.         base = 16;
  910.       /* XXX should we also allow "0b" or "0B" to set base to 2? */
  911.       else
  912.         base = 8;
  913.     }
  914.       else
  915.     base = 10;
  916.     }
  917.   if ((base == 16) &&
  918.       (string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X')))
  919.     string += 2;
  920.   /* XXX should we also skip over "0b" or "0B" if base is 2? */
  921.     
  922. /* Speed could be improved with a table like hex_value[] in gas.  */
  923. #define HEX_VALUE(c) \
  924.   (isxdigit(c) ?                \
  925.     (isdigit(c) ?                \
  926.       (c - '0') :                \
  927.       (10 + c - (islower(c) ? 'a' : 'A'))) :    \
  928.     42)
  929.  
  930.   for (value = 0; (digit = HEX_VALUE(*string)) < base; string++)
  931.     {
  932.       value = value * base + digit;
  933.     }
  934.  
  935.   if (end)
  936.     *end = string;
  937.  
  938.   return value;
  939. }
  940.  
  941. /*
  942. FUNCTION
  943.     bfd_copy_private_bfd_data
  944.  
  945. SYNOPSIS
  946.     boolean bfd_copy_private_bfd_data(bfd *ibfd, bfd *obfd);
  947.  
  948. DESCRIPTION
  949.     Copy private BFD information from the BFD @var{ibfd} to the 
  950.     the BFD @var{obfd}.  Return <<true>> on success, <<false>> on error.
  951.     Possible error returns are:
  952.  
  953.     o <<bfd_error_no_memory>> -
  954.     Not enough memory exists to create private data for @var{obfd}.
  955.  
  956. .#define bfd_copy_private_bfd_data(ibfd, obfd) \
  957. .     BFD_SEND (ibfd, _bfd_copy_private_bfd_data, \
  958. .        (ibfd, obfd))
  959.  
  960. */
  961.  
  962. /*
  963. FUNCTION
  964.     bfd_merge_private_bfd_data
  965.  
  966. SYNOPSIS
  967.     boolean bfd_merge_private_bfd_data(bfd *ibfd, bfd *obfd);
  968.  
  969. DESCRIPTION
  970.     Merge private BFD information from the BFD @var{ibfd} to the 
  971.     the output file BFD @var{obfd} when linking.  Return <<true>>
  972.     on success, <<false>> on error.  Possible error returns are:
  973.  
  974.     o <<bfd_error_no_memory>> -
  975.     Not enough memory exists to create private data for @var{obfd}.
  976.  
  977. .#define bfd_merge_private_bfd_data(ibfd, obfd) \
  978. .     BFD_SEND (ibfd, _bfd_merge_private_bfd_data, \
  979. .        (ibfd, obfd))
  980.  
  981. */
  982.  
  983. /*
  984. FUNCTION
  985.     bfd_set_private_flags
  986.  
  987. SYNOPSIS
  988.     boolean bfd_set_private_flags(bfd *abfd, flagword flags);
  989.  
  990. DESCRIPTION
  991.     Set private BFD flag information in the BFD @var{abfd}.
  992.     Return <<true>> on success, <<false>> on error.  Possible error
  993.     returns are:
  994.  
  995.     o <<bfd_error_no_memory>> -
  996.     Not enough memory exists to create private data for @var{obfd}.
  997.  
  998. .#define bfd_set_private_flags(abfd, flags) \
  999. .     BFD_SEND (abfd, _bfd_set_private_flags, \
  1000. .        (abfd, flags))
  1001.  
  1002. */
  1003.  
  1004. /*
  1005. FUNCTION
  1006.     stuff
  1007.  
  1008. DESCRIPTION
  1009.     Stuff which should be documented:
  1010.  
  1011. .#define bfd_sizeof_headers(abfd, reloc) \
  1012. .     BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
  1013. .
  1014. .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
  1015. .     BFD_SEND (abfd, _bfd_find_nearest_line,  (abfd, sec, syms, off, file, func, line))
  1016. .
  1017. .       {* Do these three do anything useful at all, for any back end?  *}
  1018. .#define bfd_debug_info_start(abfd) \
  1019. .        BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
  1020. .
  1021. .#define bfd_debug_info_end(abfd) \
  1022. .        BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
  1023. .
  1024. .#define bfd_debug_info_accumulate(abfd, section) \
  1025. .        BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
  1026. .
  1027. .
  1028. .#define bfd_stat_arch_elt(abfd, stat) \
  1029. .        BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
  1030. .
  1031. .#define bfd_update_armap_timestamp(abfd) \
  1032. .        BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
  1033. .
  1034. .#define bfd_set_arch_mach(abfd, arch, mach)\
  1035. .        BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
  1036. .
  1037. .#define bfd_relax_section(abfd, section, link_info, again) \
  1038. .       BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
  1039. .
  1040. .#define bfd_link_hash_table_create(abfd) \
  1041. .    BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
  1042. .
  1043. .#define bfd_link_add_symbols(abfd, info) \
  1044. .    BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
  1045. .
  1046. .#define bfd_final_link(abfd, info) \
  1047. .    BFD_SEND (abfd, _bfd_final_link, (abfd, info))
  1048. .
  1049. .#define bfd_free_cached_info(abfd) \
  1050. .       BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
  1051. .
  1052. .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
  1053. .    BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
  1054. .
  1055. .#define bfd_print_private_bfd_data(abfd, file)\
  1056. .    BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
  1057. .
  1058. .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
  1059. .    BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
  1060. .
  1061. .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
  1062. .    BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
  1063. .
  1064. .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
  1065. .    BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
  1066. .
  1067. .extern bfd_byte *bfd_get_relocated_section_contents
  1068. .    PARAMS ((bfd *, struct bfd_link_info *,
  1069. .          struct bfd_link_order *, bfd_byte *,
  1070. .          boolean, asymbol **));
  1071. .
  1072.  
  1073. */
  1074.  
  1075. bfd_byte *
  1076. bfd_get_relocated_section_contents (abfd, link_info, link_order, data,
  1077.                     relocateable, symbols)
  1078.      bfd *abfd;
  1079.      struct bfd_link_info *link_info;
  1080.      struct bfd_link_order *link_order;
  1081.      bfd_byte *data;
  1082.      boolean relocateable;
  1083.      asymbol **symbols;
  1084. {
  1085.   bfd *abfd2;
  1086.   bfd_byte *(*fn) PARAMS ((bfd *, struct bfd_link_info *,
  1087.                struct bfd_link_order *, bfd_byte *, boolean,
  1088.                asymbol **));
  1089.  
  1090.   if (link_order->type == bfd_indirect_link_order)
  1091.     {
  1092.       abfd2 = link_order->u.indirect.section->owner;
  1093.       if (abfd2 == 0)
  1094.     abfd2 = abfd;
  1095.     }
  1096.   else
  1097.     abfd2 = abfd;
  1098.   fn = abfd2->xvec->_bfd_get_relocated_section_contents;
  1099.  
  1100.   return (*fn) (abfd, link_info, link_order, data, relocateable, symbols);
  1101. }
  1102.  
  1103. /* Record information about an ELF program header.  */
  1104.  
  1105. boolean
  1106. bfd_record_phdr (abfd, type, flags_valid, flags, at_valid, at,
  1107.          includes_filehdr, includes_phdrs, count, secs)
  1108.      bfd *abfd;
  1109.      unsigned long type;
  1110.      boolean flags_valid;
  1111.      flagword flags;
  1112.      boolean at_valid;
  1113.      bfd_vma at;
  1114.      boolean includes_filehdr;
  1115.      boolean includes_phdrs;
  1116.      unsigned int count;
  1117.      asection **secs;
  1118. {
  1119.   struct elf_segment_map *m, **pm;
  1120.  
  1121.   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  1122.     return true;
  1123.  
  1124.   m = ((struct elf_segment_map *)
  1125.        bfd_alloc (abfd,
  1126.           (sizeof (struct elf_segment_map)
  1127.            + (count - 1) * sizeof (asection *))));
  1128.   if (m == NULL)
  1129.     return false;
  1130.  
  1131.   m->next = NULL;
  1132.   m->p_type = type;
  1133.   m->p_flags = flags;
  1134.   m->p_paddr = at;
  1135.   m->p_flags_valid = flags_valid;
  1136.   m->p_paddr_valid = at_valid;
  1137.   m->includes_filehdr = includes_filehdr;
  1138.   m->includes_phdrs = includes_phdrs;
  1139.   m->count = count;
  1140.   if (count > 0)
  1141.     memcpy (m->sections, secs, count * sizeof (asection *));
  1142.  
  1143.   for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
  1144.     ;
  1145.   *pm = m;
  1146.  
  1147.   return true;
  1148. }
  1149.