bfd.c revision 104834
133965Sjdp/* Generic BFD library interface and support routines.
278828Sobrien   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
391041Sobrien   2000, 2001, 2002
433965Sjdp   Free Software Foundation, Inc.
533965Sjdp   Written by Cygnus Support.
633965Sjdp
733965SjdpThis file is part of BFD, the Binary File Descriptor library.
833965Sjdp
933965SjdpThis program is free software; you can redistribute it and/or modify
1033965Sjdpit under the terms of the GNU General Public License as published by
1133965Sjdpthe Free Software Foundation; either version 2 of the License, or
1233965Sjdp(at your option) any later version.
1333965Sjdp
1433965SjdpThis program is distributed in the hope that it will be useful,
1533965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1633965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1733965SjdpGNU General Public License for more details.
1833965Sjdp
1933965SjdpYou should have received a copy of the GNU General Public License
2033965Sjdpalong with this program; if not, write to the Free Software
2133965SjdpFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2233965Sjdp
2333965Sjdp/*
2433965SjdpSECTION
2533965Sjdp	<<typedef bfd>>
2633965Sjdp
2733965Sjdp	A BFD has type <<bfd>>; objects of this type are the
2833965Sjdp	cornerstone of any application using BFD. Using BFD
2933965Sjdp	consists of making references though the BFD and to data in the BFD.
3033965Sjdp
3133965Sjdp	Here is the structure that defines the type <<bfd>>.  It
3233965Sjdp	contains the major data about the file and pointers
3333965Sjdp	to the rest of the data.
3433965Sjdp
3533965SjdpCODE_FRAGMENT
3633965Sjdp.
3777298Sobrien.struct _bfd
3833965Sjdp.{
3991041Sobrien.  {* The filename the application opened the BFD with.  *}
4091041Sobrien.  const char *filename;
4133965Sjdp.
4291041Sobrien.  {* A pointer to the target jump table.  *}
4391041Sobrien.  const struct bfd_target *xvec;
4433965Sjdp.
4591041Sobrien.  {* To avoid dragging too many header files into every file that
4691041Sobrien.     includes `<<bfd.h>>', IOSTREAM has been declared as a "char *",
4791041Sobrien.     and MTIME as a "long".  Their correct types, to which they
4891041Sobrien.     are cast when used, are "FILE *" and "time_t".    The iostream
4991041Sobrien.     is the result of an fopen on the filename.  However, if the
5091041Sobrien.     BFD_IN_MEMORY flag is set, then iostream is actually a pointer
5191041Sobrien.     to a bfd_in_memory struct.  *}
5291041Sobrien.  PTR iostream;
5333965Sjdp.
5491041Sobrien.  {* Is the file descriptor being cached?  That is, can it be closed as
5591041Sobrien.     needed, and re-opened when accessed later?  *}
5691041Sobrien.  boolean cacheable;
5733965Sjdp.
5891041Sobrien.  {* Marks whether there was a default target specified when the
5991041Sobrien.     BFD was opened. This is used to select which matching algorithm
6091041Sobrien.     to use to choose the back end.  *}
6191041Sobrien.  boolean target_defaulted;
6233965Sjdp.
6391041Sobrien.  {* The caching routines use these to maintain a
6491041Sobrien.     least-recently-used list of BFDs.  *}
6591041Sobrien.  struct _bfd *lru_prev, *lru_next;
6633965Sjdp.
6791041Sobrien.  {* When a file is closed by the caching routines, BFD retains
6891041Sobrien.     state information on the file here...  *}
6991041Sobrien.  ufile_ptr where;
7033965Sjdp.
7191041Sobrien.  {* ... and here: (``once'' means at least once).  *}
7291041Sobrien.  boolean opened_once;
7333965Sjdp.
7491041Sobrien.  {* Set if we have a locally maintained mtime value, rather than
7591041Sobrien.     getting it from the file each time.  *}
7691041Sobrien.  boolean mtime_set;
7733965Sjdp.
7891041Sobrien.  {* File modified time, if mtime_set is true.  *}
7991041Sobrien.  long mtime;
8033965Sjdp.
8191041Sobrien.  {* Reserved for an unimplemented file locking extension.  *}
8291041Sobrien.  int ifd;
8333965Sjdp.
8491041Sobrien.  {* The format which belongs to the BFD. (object, core, etc.)  *}
8591041Sobrien.  bfd_format format;
8633965Sjdp.
8791041Sobrien.  {* The direction with which the BFD was opened.  *}
8891041Sobrien.  enum bfd_direction
8991041Sobrien.    {
9091041Sobrien.      no_direction = 0,
9191041Sobrien.      read_direction = 1,
9291041Sobrien.      write_direction = 2,
9391041Sobrien.      both_direction = 3
9491041Sobrien.    }
9591041Sobrien.  direction;
9633965Sjdp.
9791041Sobrien.  {* Format_specific flags.  *}
9891041Sobrien.  flagword flags;
9933965Sjdp.
10091041Sobrien.  {* Currently my_archive is tested before adding origin to
10191041Sobrien.     anything. I believe that this can become always an add of
10291041Sobrien.     origin, with origin set to 0 for non archive files.  *}
10391041Sobrien.  ufile_ptr origin;
10433965Sjdp.
10591041Sobrien.  {* Remember when output has begun, to stop strange things
10691041Sobrien.     from happening.  *}
10791041Sobrien.  boolean output_has_begun;
10833965Sjdp.
10991041Sobrien.  {* A hash table for section names.  *}
11091041Sobrien.  struct bfd_hash_table section_htab;
11133965Sjdp.
11291041Sobrien.  {* Pointer to linked list of sections.  *}
11391041Sobrien.  struct sec *sections;
11433965Sjdp.
11591041Sobrien.  {* The place where we add to the section list.  *}
11691041Sobrien.  struct sec **section_tail;
11733965Sjdp.
11891041Sobrien.  {* The number of sections.  *}
11991041Sobrien.  unsigned int section_count;
12033965Sjdp.
12191041Sobrien.  {* Stuff only useful for object files:
12291041Sobrien.     The start address.  *}
12391041Sobrien.  bfd_vma start_address;
12433965Sjdp.
12591041Sobrien.  {* Used for input and output.  *}
12691041Sobrien.  unsigned int symcount;
12733965Sjdp.
12891041Sobrien.  {* Symbol table for output BFD (with symcount entries).  *}
12991041Sobrien.  struct symbol_cache_entry  **outsymbols;
13033965Sjdp.
131104834Sobrien.  {* Used for slurped dynamic symbol tables.  *}
132104834Sobrien.  unsigned int dynsymcount;
133104834Sobrien.
13491041Sobrien.  {* Pointer to structure which contains architecture information.  *}
13591041Sobrien.  const struct bfd_arch_info *arch_info;
13633965Sjdp.
13791041Sobrien.  {* Stuff only useful for archives.  *}
13891041Sobrien.  PTR arelt_data;
13991041Sobrien.  struct _bfd *my_archive;     {* The containing archive BFD.  *}
14091041Sobrien.  struct _bfd *next;           {* The next BFD in the archive.  *}
14191041Sobrien.  struct _bfd *archive_head;   {* The first BFD in the archive.  *}
14291041Sobrien.  boolean has_armap;
14333965Sjdp.
14491041Sobrien.  {* A chain of BFD structures involved in a link.  *}
14591041Sobrien.  struct _bfd *link_next;
14633965Sjdp.
14791041Sobrien.  {* A field used by _bfd_generic_link_add_archive_symbols.  This will
14891041Sobrien.     be used only for archive elements.  *}
14991041Sobrien.  int archive_pass;
15033965Sjdp.
15191041Sobrien.  {* Used by the back end to hold private data.  *}
15291041Sobrien.  union
15391041Sobrien.    {
15433965Sjdp.      struct aout_data_struct *aout_data;
15533965Sjdp.      struct artdata *aout_ar_data;
15633965Sjdp.      struct _oasys_data *oasys_obj_data;
15733965Sjdp.      struct _oasys_ar_data *oasys_ar_data;
15833965Sjdp.      struct coff_tdata *coff_obj_data;
15933965Sjdp.      struct pe_tdata *pe_obj_data;
16033965Sjdp.      struct xcoff_tdata *xcoff_obj_data;
16133965Sjdp.      struct ecoff_tdata *ecoff_obj_data;
16233965Sjdp.      struct ieee_data_struct *ieee_data;
16333965Sjdp.      struct ieee_ar_data_struct *ieee_ar_data;
16433965Sjdp.      struct srec_data_struct *srec_data;
16533965Sjdp.      struct ihex_data_struct *ihex_data;
16633965Sjdp.      struct tekhex_data_struct *tekhex_data;
16733965Sjdp.      struct elf_obj_tdata *elf_obj_data;
16833965Sjdp.      struct nlm_obj_tdata *nlm_obj_data;
16933965Sjdp.      struct bout_data_struct *bout_data;
17089857Sobrien.      struct mmo_data_struct *mmo_data;
17133965Sjdp.      struct sun_core_struct *sun_core_data;
17260484Sobrien.      struct sco5_core_struct *sco5_core_data;
17333965Sjdp.      struct trad_core_struct *trad_core_data;
17433965Sjdp.      struct som_data_struct *som_data;
17533965Sjdp.      struct hpux_core_struct *hpux_core_data;
17633965Sjdp.      struct hppabsd_core_struct *hppabsd_core_data;
17733965Sjdp.      struct sgi_core_struct *sgi_core_data;
17833965Sjdp.      struct lynx_core_struct *lynx_core_data;
17933965Sjdp.      struct osf_core_struct *osf_core_data;
18033965Sjdp.      struct cisco_core_struct *cisco_core_data;
18133965Sjdp.      struct versados_data_struct *versados_data;
18233965Sjdp.      struct netbsd_core_struct *netbsd_core_data;
18333965Sjdp.      PTR any;
18491041Sobrien.    }
18591041Sobrien.  tdata;
18677298Sobrien.
18791041Sobrien.  {* Used by the application to hold private data.  *}
18891041Sobrien.  PTR usrdata;
18933965Sjdp.
19033965Sjdp.  {* Where all the allocated stuff under this BFD goes.  This is a
19133965Sjdp.     struct objalloc *, but we use PTR to avoid requiring the inclusion of
19233965Sjdp.     objalloc.h.  *}
19391041Sobrien.  PTR memory;
19433965Sjdp.};
19533965Sjdp.
19633965Sjdp*/
19733965Sjdp
19833965Sjdp#include "bfd.h"
19933965Sjdp#include "sysdep.h"
20033965Sjdp
20133965Sjdp#ifdef ANSI_PROTOTYPES
20233965Sjdp#include <stdarg.h>
20333965Sjdp#else
20433965Sjdp#include <varargs.h>
20533965Sjdp#endif
20633965Sjdp
20733965Sjdp#include "libiberty.h"
20889857Sobrien#include "safe-ctype.h"
20933965Sjdp#include "bfdlink.h"
21033965Sjdp#include "libbfd.h"
21133965Sjdp#include "coff/internal.h"
21233965Sjdp#include "coff/sym.h"
21333965Sjdp#include "libcoff.h"
21433965Sjdp#include "libecoff.h"
21533965Sjdp#undef obj_symbols
21633965Sjdp#include "elf-bfd.h"
21733965Sjdp
21833965Sjdp/* provide storage for subsystem, stack and heap data which may have been
21933965Sjdp   passed in on the command line.  Ld puts this data into a bfd_link_info
22033965Sjdp   struct which ultimately gets passed in to the bfd.  When it arrives, copy
22133965Sjdp   it to the following struct so that the data will be available in coffcode.h
22233965Sjdp   where it is needed.  The typedef's used are defined in bfd.h */
22333965Sjdp
22433965Sjdp/*
22533965SjdpSECTION
22633965Sjdp	Error reporting
22733965Sjdp
22833965Sjdp	Most BFD functions return nonzero on success (check their
22933965Sjdp	individual documentation for precise semantics).  On an error,
23033965Sjdp	they call <<bfd_set_error>> to set an error condition that callers
23133965Sjdp	can check by calling <<bfd_get_error>>.
23233965Sjdp        If that returns <<bfd_error_system_call>>, then check
23333965Sjdp	<<errno>>.
23433965Sjdp
23533965Sjdp	The easiest way to report a BFD error to the user is to
23633965Sjdp	use <<bfd_perror>>.
23733965Sjdp
23833965SjdpSUBSECTION
23933965Sjdp	Type <<bfd_error_type>>
24033965Sjdp
24133965Sjdp	The values returned by <<bfd_get_error>> are defined by the
24233965Sjdp	enumerated type <<bfd_error_type>>.
24333965Sjdp
24433965SjdpCODE_FRAGMENT
24533965Sjdp.
24633965Sjdp.typedef enum bfd_error
24733965Sjdp.{
24833965Sjdp.  bfd_error_no_error = 0,
24933965Sjdp.  bfd_error_system_call,
25033965Sjdp.  bfd_error_invalid_target,
25133965Sjdp.  bfd_error_wrong_format,
25289857Sobrien.  bfd_error_wrong_object_format,
25333965Sjdp.  bfd_error_invalid_operation,
25433965Sjdp.  bfd_error_no_memory,
25533965Sjdp.  bfd_error_no_symbols,
25633965Sjdp.  bfd_error_no_armap,
25733965Sjdp.  bfd_error_no_more_archived_files,
25833965Sjdp.  bfd_error_malformed_archive,
25933965Sjdp.  bfd_error_file_not_recognized,
26033965Sjdp.  bfd_error_file_ambiguously_recognized,
26133965Sjdp.  bfd_error_no_contents,
26233965Sjdp.  bfd_error_nonrepresentable_section,
26333965Sjdp.  bfd_error_no_debug_section,
26433965Sjdp.  bfd_error_bad_value,
26533965Sjdp.  bfd_error_file_truncated,
26633965Sjdp.  bfd_error_file_too_big,
26733965Sjdp.  bfd_error_invalid_error_code
26891041Sobrien.}
26991041Sobrien.bfd_error_type;
27033965Sjdp.
27133965Sjdp*/
27233965Sjdp
27333965Sjdpstatic bfd_error_type bfd_error = bfd_error_no_error;
27433965Sjdp
27589857Sobrienconst char *const bfd_errmsgs[] =
27689857Sobrien{
27789857Sobrien  N_("No error"),
27889857Sobrien  N_("System call error"),
27989857Sobrien  N_("Invalid bfd target"),
28089857Sobrien  N_("File in wrong format"),
28189857Sobrien  N_("Archive object file in wrong format"),
28289857Sobrien  N_("Invalid operation"),
28389857Sobrien  N_("Memory exhausted"),
28489857Sobrien  N_("No symbols"),
28589857Sobrien  N_("Archive has no index; run ranlib to add one"),
28689857Sobrien  N_("No more archived files"),
28789857Sobrien  N_("Malformed archive"),
28889857Sobrien  N_("File format not recognized"),
28989857Sobrien  N_("File format is ambiguous"),
29089857Sobrien  N_("Section has no contents"),
29189857Sobrien  N_("Nonrepresentable section on output"),
29289857Sobrien  N_("Symbol needs debug section which does not exist"),
29389857Sobrien  N_("Bad value"),
29489857Sobrien  N_("File truncated"),
29589857Sobrien  N_("File too big"),
29689857Sobrien  N_("#<Invalid error code>")
29789857Sobrien};
29833965Sjdp
29933965Sjdp/*
30033965SjdpFUNCTION
30133965Sjdp	bfd_get_error
30233965Sjdp
30333965SjdpSYNOPSIS
30433965Sjdp	bfd_error_type bfd_get_error (void);
30533965Sjdp
30633965SjdpDESCRIPTION
30733965Sjdp	Return the current BFD error condition.
30833965Sjdp*/
30933965Sjdp
31033965Sjdpbfd_error_type
31133965Sjdpbfd_get_error ()
31233965Sjdp{
31333965Sjdp  return bfd_error;
31433965Sjdp}
31533965Sjdp
31633965Sjdp/*
31733965SjdpFUNCTION
31833965Sjdp	bfd_set_error
31933965Sjdp
32033965SjdpSYNOPSIS
32133965Sjdp	void bfd_set_error (bfd_error_type error_tag);
32233965Sjdp
32333965SjdpDESCRIPTION
32433965Sjdp	Set the BFD error condition to be @var{error_tag}.
32533965Sjdp*/
32633965Sjdp
32733965Sjdpvoid
32833965Sjdpbfd_set_error (error_tag)
32933965Sjdp     bfd_error_type error_tag;
33033965Sjdp{
33133965Sjdp  bfd_error = error_tag;
33233965Sjdp}
33333965Sjdp
33433965Sjdp/*
33533965SjdpFUNCTION
33633965Sjdp	bfd_errmsg
33733965Sjdp
33833965SjdpSYNOPSIS
33989857Sobrien	const char *bfd_errmsg (bfd_error_type error_tag);
34033965Sjdp
34133965SjdpDESCRIPTION
34233965Sjdp	Return a string describing the error @var{error_tag}, or
34333965Sjdp	the system error if @var{error_tag} is <<bfd_error_system_call>>.
34433965Sjdp*/
34533965Sjdp
34689857Sobrienconst char *
34733965Sjdpbfd_errmsg (error_tag)
34833965Sjdp     bfd_error_type error_tag;
34933965Sjdp{
35033965Sjdp#ifndef errno
35133965Sjdp  extern int errno;
35233965Sjdp#endif
35333965Sjdp  if (error_tag == bfd_error_system_call)
35433965Sjdp    return xstrerror (errno);
35533965Sjdp
35689857Sobrien  if ((((int) error_tag < (int) bfd_error_no_error) ||
35789857Sobrien       ((int) error_tag > (int) bfd_error_invalid_error_code)))
35833965Sjdp    error_tag = bfd_error_invalid_error_code;/* sanity check */
35933965Sjdp
36060484Sobrien  return _(bfd_errmsgs [(int)error_tag]);
36133965Sjdp}
36233965Sjdp
36333965Sjdp/*
36433965SjdpFUNCTION
36533965Sjdp	bfd_perror
36633965Sjdp
36733965SjdpSYNOPSIS
36889857Sobrien	void bfd_perror (const char *message);
36933965Sjdp
37033965SjdpDESCRIPTION
37133965Sjdp	Print to the standard error stream a string describing the
37233965Sjdp	last BFD error that occurred, or the last system error if
37333965Sjdp	the last BFD error was a system call failure.  If @var{message}
37433965Sjdp	is non-NULL and non-empty, the error string printed is preceded
37533965Sjdp	by @var{message}, a colon, and a space.  It is followed by a newline.
37633965Sjdp*/
37733965Sjdp
37833965Sjdpvoid
37933965Sjdpbfd_perror (message)
38089857Sobrien     const char *message;
38133965Sjdp{
38233965Sjdp  if (bfd_get_error () == bfd_error_system_call)
38389857Sobrien    /* Must be a system error then.  */
38489857Sobrien    perror ((char *)message);
38589857Sobrien  else
38689857Sobrien    {
38789857Sobrien      if (message == NULL || *message == '\0')
38889857Sobrien	fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
38989857Sobrien      else
39089857Sobrien	fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
39189857Sobrien    }
39233965Sjdp}
39333965Sjdp
39433965Sjdp/*
39533965SjdpSUBSECTION
39633965Sjdp	BFD error handler
39733965Sjdp
39833965Sjdp	Some BFD functions want to print messages describing the
39933965Sjdp	problem.  They call a BFD error handler function.  This
40033965Sjdp	function may be overriden by the program.
40133965Sjdp
40233965Sjdp	The BFD error handler acts like printf.
40333965Sjdp
40433965SjdpCODE_FRAGMENT
40533965Sjdp.
40633965Sjdp.typedef void (*bfd_error_handler_type) PARAMS ((const char *, ...));
40733965Sjdp.
40833965Sjdp*/
40933965Sjdp
41033965Sjdp/* The program name used when printing BFD error messages.  */
41133965Sjdp
41233965Sjdpstatic const char *_bfd_error_program_name;
41333965Sjdp
41433965Sjdp/* This is the default routine to handle BFD error messages.  */
41533965Sjdp
41633965Sjdpstatic void _bfd_default_error_handler PARAMS ((const char *s, ...));
41733965Sjdp
41833965Sjdpstatic void
41989857Sobrien_bfd_default_error_handler VPARAMS ((const char *s, ...))
42033965Sjdp{
42133965Sjdp  if (_bfd_error_program_name != NULL)
42233965Sjdp    fprintf (stderr, "%s: ", _bfd_error_program_name);
42333965Sjdp  else
42433965Sjdp    fprintf (stderr, "BFD: ");
42533965Sjdp
42689857Sobrien  VA_OPEN (p, s);
42789857Sobrien  VA_FIXEDARG (p, const char *, s);
42833965Sjdp  vfprintf (stderr, s, p);
42989857Sobrien  VA_CLOSE (p);
43033965Sjdp
43133965Sjdp  fprintf (stderr, "\n");
43233965Sjdp}
43333965Sjdp
43433965Sjdp/* This is a function pointer to the routine which should handle BFD
43533965Sjdp   error messages.  It is called when a BFD routine encounters an
43633965Sjdp   error for which it wants to print a message.  Going through a
43733965Sjdp   function pointer permits a program linked against BFD to intercept
43833965Sjdp   the messages and deal with them itself.  */
43933965Sjdp
44033965Sjdpbfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
44133965Sjdp
44233965Sjdp/*
44333965SjdpFUNCTION
44433965Sjdp	bfd_set_error_handler
44533965Sjdp
44633965SjdpSYNOPSIS
44733965Sjdp	bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
44833965Sjdp
44933965SjdpDESCRIPTION
45033965Sjdp	Set the BFD error handler function.  Returns the previous
45133965Sjdp	function.
45233965Sjdp*/
45333965Sjdp
45433965Sjdpbfd_error_handler_type
45533965Sjdpbfd_set_error_handler (pnew)
45633965Sjdp     bfd_error_handler_type pnew;
45733965Sjdp{
45833965Sjdp  bfd_error_handler_type pold;
45933965Sjdp
46033965Sjdp  pold = _bfd_error_handler;
46133965Sjdp  _bfd_error_handler = pnew;
46233965Sjdp  return pold;
46333965Sjdp}
46433965Sjdp
46533965Sjdp/*
46633965SjdpFUNCTION
46733965Sjdp	bfd_set_error_program_name
46833965Sjdp
46933965SjdpSYNOPSIS
47033965Sjdp	void bfd_set_error_program_name (const char *);
47133965Sjdp
47233965SjdpDESCRIPTION
47333965Sjdp	Set the program name to use when printing a BFD error.  This
47433965Sjdp	is printed before the error message followed by a colon and
47533965Sjdp	space.  The string must not be changed after it is passed to
47633965Sjdp	this function.
47733965Sjdp*/
47833965Sjdp
47933965Sjdpvoid
48033965Sjdpbfd_set_error_program_name (name)
48133965Sjdp     const char *name;
48233965Sjdp{
48333965Sjdp  _bfd_error_program_name = name;
48433965Sjdp}
48538889Sjdp
48638889Sjdp/*
48738889SjdpFUNCTION
48838889Sjdp	bfd_get_error_handler
48938889Sjdp
49038889SjdpSYNOPSIS
49138889Sjdp	bfd_error_handler_type bfd_get_error_handler (void);
49238889Sjdp
49338889SjdpDESCRIPTION
49438889Sjdp	Return the BFD error handler function.
49538889Sjdp*/
49638889Sjdp
49738889Sjdpbfd_error_handler_type
49838889Sjdpbfd_get_error_handler ()
49938889Sjdp{
50038889Sjdp  return _bfd_error_handler;
50138889Sjdp}
50289857Sobrien
50389857Sobrien/*
50489857SobrienFUNCTION
50589857Sobrien	bfd_archive_filename
50689857Sobrien
50789857SobrienSYNOPSIS
50889857Sobrien	const char *bfd_archive_filename (bfd *);
50989857Sobrien
51089857SobrienDESCRIPTION
51189857Sobrien	For a BFD that is a component of an archive, returns a string
51289857Sobrien	with both the archive name and file name.  For other BFDs, just
51389857Sobrien	returns the file name.
51489857Sobrien*/
51589857Sobrien
51689857Sobrienconst char *
51789857Sobrienbfd_archive_filename (abfd)
51889857Sobrien     bfd *abfd;
51989857Sobrien{
52089857Sobrien  if (abfd->my_archive)
52189857Sobrien    {
52289857Sobrien      static size_t curr = 0;
52389857Sobrien      static char *buf;
52489857Sobrien      size_t needed;
52589857Sobrien
52689857Sobrien      needed = (strlen (bfd_get_filename (abfd->my_archive))
52789857Sobrien		+ strlen (bfd_get_filename (abfd)) + 3);
52889857Sobrien      if (needed > curr)
52989857Sobrien	{
53089857Sobrien	  if (curr)
53189857Sobrien	    free (buf);
53289857Sobrien	  curr = needed + (needed >> 1);
53389857Sobrien	  buf = bfd_malloc ((bfd_size_type) curr);
53489857Sobrien	  /* If we can't malloc, fail safe by returning just the file
53589857Sobrien	     name. This function is only used when building error
53689857Sobrien	     messages.  */
53789857Sobrien	  if (!buf)
53889857Sobrien	    {
53989857Sobrien	      curr = 0;
54089857Sobrien	      return bfd_get_filename (abfd);
54189857Sobrien	    }
54289857Sobrien	}
54389857Sobrien      sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
54489857Sobrien	       bfd_get_filename (abfd));
54589857Sobrien      return buf;
54689857Sobrien    }
54789857Sobrien  else
54889857Sobrien    return bfd_get_filename (abfd);
54989857Sobrien}
55033965Sjdp
55133965Sjdp/*
55233965SjdpSECTION
55333965Sjdp	Symbols
55433965Sjdp*/
55533965Sjdp
55633965Sjdp/*
55733965SjdpFUNCTION
55833965Sjdp	bfd_get_reloc_upper_bound
55933965Sjdp
56033965SjdpSYNOPSIS
56133965Sjdp	long bfd_get_reloc_upper_bound(bfd *abfd, asection *sect);
56233965Sjdp
56333965SjdpDESCRIPTION
56433965Sjdp	Return the number of bytes required to store the
56533965Sjdp	relocation information associated with section @var{sect}
56633965Sjdp	attached to bfd @var{abfd}.  If an error occurs, return -1.
56733965Sjdp
56833965Sjdp*/
56933965Sjdp
57033965Sjdplong
57133965Sjdpbfd_get_reloc_upper_bound (abfd, asect)
57233965Sjdp     bfd *abfd;
57333965Sjdp     sec_ptr asect;
57433965Sjdp{
57589857Sobrien  if (abfd->format != bfd_object)
57689857Sobrien    {
57789857Sobrien      bfd_set_error (bfd_error_invalid_operation);
57889857Sobrien      return -1;
57989857Sobrien    }
58033965Sjdp
58133965Sjdp  return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
58233965Sjdp}
58333965Sjdp
58433965Sjdp/*
58533965SjdpFUNCTION
58633965Sjdp	bfd_canonicalize_reloc
58733965Sjdp
58833965SjdpSYNOPSIS
58933965Sjdp	long bfd_canonicalize_reloc
59033965Sjdp        	(bfd *abfd,
59133965Sjdp		asection *sec,
59233965Sjdp		arelent **loc,
59333965Sjdp		asymbol	**syms);
59433965Sjdp
59533965SjdpDESCRIPTION
59633965Sjdp	Call the back end associated with the open BFD
59733965Sjdp	@var{abfd} and translate the external form of the relocation
59833965Sjdp	information attached to @var{sec} into the internal canonical
59933965Sjdp	form.  Place the table into memory at @var{loc}, which has
60033965Sjdp	been preallocated, usually by a call to
60133965Sjdp	<<bfd_get_reloc_upper_bound>>.  Returns the number of relocs, or
60233965Sjdp	-1 on error.
60333965Sjdp
60433965Sjdp	The @var{syms} table is also needed for horrible internal magic
60533965Sjdp	reasons.
60633965Sjdp
60733965Sjdp*/
60833965Sjdplong
60933965Sjdpbfd_canonicalize_reloc (abfd, asect, location, symbols)
61033965Sjdp     bfd *abfd;
61133965Sjdp     sec_ptr asect;
61233965Sjdp     arelent **location;
61333965Sjdp     asymbol **symbols;
61433965Sjdp{
61589857Sobrien  if (abfd->format != bfd_object)
61689857Sobrien    {
61789857Sobrien      bfd_set_error (bfd_error_invalid_operation);
61889857Sobrien      return -1;
61989857Sobrien    }
62089857Sobrien
62133965Sjdp  return BFD_SEND (abfd, _bfd_canonicalize_reloc,
62233965Sjdp		   (abfd, asect, location, symbols));
62333965Sjdp}
62433965Sjdp
62533965Sjdp/*
62633965SjdpFUNCTION
62733965Sjdp	bfd_set_reloc
62833965Sjdp
62933965SjdpSYNOPSIS
63033965Sjdp	void bfd_set_reloc
63191041Sobrien	  (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
63233965Sjdp
63333965SjdpDESCRIPTION
63433965Sjdp	Set the relocation pointer and count within
63533965Sjdp	section @var{sec} to the values @var{rel} and @var{count}.
63633965Sjdp	The argument @var{abfd} is ignored.
63733965Sjdp
63833965Sjdp*/
63977298Sobrien
64033965Sjdpvoid
64133965Sjdpbfd_set_reloc (ignore_abfd, asect, location, count)
64260484Sobrien     bfd *ignore_abfd ATTRIBUTE_UNUSED;
64333965Sjdp     sec_ptr asect;
64433965Sjdp     arelent **location;
64533965Sjdp     unsigned int count;
64633965Sjdp{
64733965Sjdp  asect->orelocation = location;
64833965Sjdp  asect->reloc_count = count;
64933965Sjdp}
65033965Sjdp
65133965Sjdp/*
65233965SjdpFUNCTION
65333965Sjdp	bfd_set_file_flags
65433965Sjdp
65533965SjdpSYNOPSIS
65633965Sjdp	boolean bfd_set_file_flags(bfd *abfd, flagword flags);
65733965Sjdp
65833965SjdpDESCRIPTION
65933965Sjdp	Set the flag word in the BFD @var{abfd} to the value @var{flags}.
66033965Sjdp
66133965Sjdp	Possible errors are:
66233965Sjdp	o <<bfd_error_wrong_format>> - The target bfd was not of object format.
66333965Sjdp	o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
66433965Sjdp	o <<bfd_error_invalid_operation>> -
66533965Sjdp	The flag word contained a bit which was not applicable to the
66633965Sjdp	type of file.  E.g., an attempt was made to set the <<D_PAGED>> bit
66733965Sjdp	on a BFD format which does not support demand paging.
66833965Sjdp
66933965Sjdp*/
67033965Sjdp
67133965Sjdpboolean
67233965Sjdpbfd_set_file_flags (abfd, flags)
67333965Sjdp     bfd *abfd;
67433965Sjdp     flagword flags;
67533965Sjdp{
67689857Sobrien  if (abfd->format != bfd_object)
67789857Sobrien    {
67889857Sobrien      bfd_set_error (bfd_error_wrong_format);
67989857Sobrien      return false;
68089857Sobrien    }
68133965Sjdp
68289857Sobrien  if (bfd_read_p (abfd))
68389857Sobrien    {
68489857Sobrien      bfd_set_error (bfd_error_invalid_operation);
68589857Sobrien      return false;
68689857Sobrien    }
68733965Sjdp
68833965Sjdp  bfd_get_file_flags (abfd) = flags;
68989857Sobrien  if ((flags & bfd_applicable_file_flags (abfd)) != flags)
69089857Sobrien    {
69189857Sobrien      bfd_set_error (bfd_error_invalid_operation);
69289857Sobrien      return false;
69389857Sobrien    }
69433965Sjdp
69589857Sobrien  return true;
69633965Sjdp}
69733965Sjdp
69833965Sjdpvoid
69933965Sjdpbfd_assert (file, line)
70033965Sjdp     const char *file;
70133965Sjdp     int line;
70233965Sjdp{
70389857Sobrien  (*_bfd_error_handler) (_("BFD %s assertion fail %s:%d"),
70489857Sobrien			 BFD_VERSION_STRING, file, line);
70533965Sjdp}
70633965Sjdp
70760484Sobrien/* A more or less friendly abort message.  In libbfd.h abort is
70860484Sobrien   defined to call this function.  */
70933965Sjdp
71060484Sobrien#ifndef EXIT_FAILURE
71160484Sobrien#define EXIT_FAILURE 1
71260484Sobrien#endif
71360484Sobrien
71460484Sobrienvoid
71560484Sobrien_bfd_abort (file, line, fn)
71660484Sobrien     const char *file;
71760484Sobrien     int line;
71860484Sobrien     const char *fn;
71960484Sobrien{
72060484Sobrien  if (fn != NULL)
72160484Sobrien    (*_bfd_error_handler)
72289857Sobrien      (_("BFD %s internal error, aborting at %s line %d in %s\n"),
72389857Sobrien       BFD_VERSION_STRING, file, line, fn);
72460484Sobrien  else
72560484Sobrien    (*_bfd_error_handler)
72689857Sobrien      (_("BFD %s internal error, aborting at %s line %d\n"),
72789857Sobrien       BFD_VERSION_STRING, file, line);
72860484Sobrien  (*_bfd_error_handler) (_("Please report this bug.\n"));
72960484Sobrien  xexit (EXIT_FAILURE);
73060484Sobrien}
73160484Sobrien
73233965Sjdp/*
73333965SjdpFUNCTION
73477298Sobrien	bfd_get_arch_size
73577298Sobrien
73677298SobrienSYNOPSIS
73777298Sobrien 	int bfd_get_arch_size (bfd *abfd);
73877298Sobrien
73977298SobrienDESCRIPTION
74077298Sobrien	Returns the architecture address size, in bits, as determined
74177298Sobrien	by the object file's format.  For ELF, this information is
74277298Sobrien	included in the header.
74377298Sobrien
74477298SobrienRETURNS
74577298Sobrien	Returns the arch size in bits if known, <<-1>> otherwise.
74677298Sobrien*/
74777298Sobrien
74877298Sobrienint
74977298Sobrienbfd_get_arch_size (abfd)
75077298Sobrien     bfd *abfd;
75177298Sobrien{
75277298Sobrien  if (abfd->xvec->flavour == bfd_target_elf_flavour)
75377298Sobrien    return (get_elf_backend_data (abfd))->s->arch_size;
75477298Sobrien
75577298Sobrien  return -1;
75677298Sobrien}
75777298Sobrien
75877298Sobrien/*
75977298SobrienFUNCTION
76077298Sobrien	bfd_get_sign_extend_vma
76177298Sobrien
76277298SobrienSYNOPSIS
76377298Sobrien 	int bfd_get_sign_extend_vma (bfd *abfd);
76477298Sobrien
76577298SobrienDESCRIPTION
76677298Sobrien	Indicates if the target architecture "naturally" sign extends
76777298Sobrien	an address.  Some architectures implicitly sign extend address
76877298Sobrien	values when they are converted to types larger than the size
76977298Sobrien	of an address.  For instance, bfd_get_start_address() will
77077298Sobrien	return an address sign extended to fill a bfd_vma when this is
77177298Sobrien	the case.
77277298Sobrien
77377298SobrienRETURNS
77477298Sobrien	Returns <<1>> if the target architecture is known to sign
77577298Sobrien	extend addresses, <<0>> if the target architecture is known to
77677298Sobrien	not sign extend addresses, and <<-1>> otherwise.
77777298Sobrien*/
77877298Sobrien
77977298Sobrienint
78077298Sobrienbfd_get_sign_extend_vma (abfd)
78177298Sobrien     bfd *abfd;
78277298Sobrien{
78389857Sobrien  char *name;
78489857Sobrien
78577298Sobrien  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
78677298Sobrien    return (get_elf_backend_data (abfd)->sign_extend_vma);
78777298Sobrien
78889857Sobrien  name = bfd_get_target (abfd);
78989857Sobrien
79089857Sobrien  /* Return a proper value for DJGPP COFF (an x86 COFF variant).
79189857Sobrien     This function is required for DWARF2 support, but there is
79289857Sobrien     no place to store this information in the COFF back end.
79389857Sobrien     Should enough other COFF targets add support for DWARF2,
79489857Sobrien     a place will have to be found.  Until then, this hack will do.  */
79589857Sobrien  if (strncmp (name, "coff-go32", sizeof ("coff-go32") - 1) == 0)
79689857Sobrien    return 1;
79789857Sobrien
79877298Sobrien  bfd_set_error (bfd_error_wrong_format);
79977298Sobrien  return -1;
80077298Sobrien}
80177298Sobrien
80277298Sobrien/*
80377298SobrienFUNCTION
80433965Sjdp	bfd_set_start_address
80533965Sjdp
80633965SjdpSYNOPSIS
80733965Sjdp 	boolean bfd_set_start_address(bfd *abfd, bfd_vma vma);
80833965Sjdp
80933965SjdpDESCRIPTION
81033965Sjdp	Make @var{vma} the entry point of output BFD @var{abfd}.
81133965Sjdp
81233965SjdpRETURNS
81333965Sjdp	Returns <<true>> on success, <<false>> otherwise.
81433965Sjdp*/
81533965Sjdp
81633965Sjdpboolean
81789857Sobrienbfd_set_start_address (abfd, vma)
81889857Sobrien     bfd *abfd;
81989857Sobrien     bfd_vma vma;
82033965Sjdp{
82133965Sjdp  abfd->start_address = vma;
82233965Sjdp  return true;
82333965Sjdp}
82433965Sjdp
82533965Sjdp/*
82633965SjdpFUNCTION
82733965Sjdp	bfd_get_mtime
82833965Sjdp
82933965SjdpSYNOPSIS
83033965Sjdp	long bfd_get_mtime(bfd *abfd);
83133965Sjdp
83233965SjdpDESCRIPTION
83333965Sjdp	Return the file modification time (as read from the file system, or
83433965Sjdp	from the archive header for archive members).
83533965Sjdp
83633965Sjdp*/
83733965Sjdp
83833965Sjdplong
83933965Sjdpbfd_get_mtime (abfd)
84033965Sjdp     bfd *abfd;
84133965Sjdp{
84233965Sjdp  FILE *fp;
84333965Sjdp  struct stat buf;
84433965Sjdp
84533965Sjdp  if (abfd->mtime_set)
84633965Sjdp    return abfd->mtime;
84733965Sjdp
84833965Sjdp  fp = bfd_cache_lookup (abfd);
84933965Sjdp  if (0 != fstat (fileno (fp), &buf))
85033965Sjdp    return 0;
85133965Sjdp
85233965Sjdp  abfd->mtime = buf.st_mtime;		/* Save value in case anyone wants it */
85333965Sjdp  return buf.st_mtime;
85433965Sjdp}
85533965Sjdp
85633965Sjdp/*
85733965SjdpFUNCTION
85833965Sjdp	bfd_get_size
85933965Sjdp
86033965SjdpSYNOPSIS
86133965Sjdp	long bfd_get_size(bfd *abfd);
86233965Sjdp
86333965SjdpDESCRIPTION
86433965Sjdp	Return the file size (as read from file system) for the file
86533965Sjdp	associated with BFD @var{abfd}.
86633965Sjdp
86733965Sjdp	The initial motivation for, and use of, this routine is not
86833965Sjdp	so we can get the exact size of the object the BFD applies to, since
86933965Sjdp	that might not be generally possible (archive members for example).
87033965Sjdp	It would be ideal if someone could eventually modify
87133965Sjdp	it so that such results were guaranteed.
87233965Sjdp
87333965Sjdp	Instead, we want to ask questions like "is this NNN byte sized
87433965Sjdp	object I'm about to try read from file offset YYY reasonable?"
87533965Sjdp	As as example of where we might do this, some object formats
87677298Sobrien	use string tables for which the first <<sizeof (long)>> bytes of the
87733965Sjdp	table contain the size of the table itself, including the size bytes.
87833965Sjdp	If an application tries to read what it thinks is one of these
87933965Sjdp	string tables, without some way to validate the size, and for
88033965Sjdp	some reason the size is wrong (byte swapping error, wrong location
88133965Sjdp	for the string table, etc.), the only clue is likely to be a read
88233965Sjdp	error when it tries to read the table, or a "virtual memory
88333965Sjdp	exhausted" error when it tries to allocate 15 bazillon bytes
88433965Sjdp	of space for the 15 bazillon byte table it is about to read.
88533965Sjdp	This function at least allows us to answer the quesion, "is the
88633965Sjdp	size reasonable?".
88733965Sjdp*/
88833965Sjdp
88933965Sjdplong
89033965Sjdpbfd_get_size (abfd)
89133965Sjdp     bfd *abfd;
89233965Sjdp{
89333965Sjdp  FILE *fp;
89433965Sjdp  struct stat buf;
89533965Sjdp
89633965Sjdp  if ((abfd->flags & BFD_IN_MEMORY) != 0)
89733965Sjdp    return ((struct bfd_in_memory *) abfd->iostream)->size;
89833965Sjdp
89933965Sjdp  fp = bfd_cache_lookup (abfd);
90089857Sobrien  if (0 != fstat (fileno (fp), & buf))
90133965Sjdp    return 0;
90233965Sjdp
90333965Sjdp  return buf.st_size;
90433965Sjdp}
90533965Sjdp
90633965Sjdp/*
90733965SjdpFUNCTION
90833965Sjdp	bfd_get_gp_size
90933965Sjdp
91033965SjdpSYNOPSIS
91189857Sobrien	unsigned int bfd_get_gp_size(bfd *abfd);
91233965Sjdp
91333965SjdpDESCRIPTION
91433965Sjdp	Return the maximum size of objects to be optimized using the GP
91533965Sjdp	register under MIPS ECOFF.  This is typically set by the <<-G>>
91633965Sjdp	argument to the compiler, assembler or linker.
91733965Sjdp*/
91833965Sjdp
91989857Sobrienunsigned int
92033965Sjdpbfd_get_gp_size (abfd)
92133965Sjdp     bfd *abfd;
92233965Sjdp{
92333965Sjdp  if (abfd->format == bfd_object)
92433965Sjdp    {
92533965Sjdp      if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
92633965Sjdp	return ecoff_data (abfd)->gp_size;
92733965Sjdp      else if (abfd->xvec->flavour == bfd_target_elf_flavour)
92833965Sjdp	return elf_gp_size (abfd);
92933965Sjdp    }
93033965Sjdp  return 0;
93133965Sjdp}
93233965Sjdp
93333965Sjdp/*
93433965SjdpFUNCTION
93533965Sjdp	bfd_set_gp_size
93633965Sjdp
93733965SjdpSYNOPSIS
93889857Sobrien	void bfd_set_gp_size(bfd *abfd, unsigned int i);
93933965Sjdp
94033965SjdpDESCRIPTION
94133965Sjdp	Set the maximum size of objects to be optimized using the GP
94233965Sjdp	register under ECOFF or MIPS ELF.  This is typically set by
94333965Sjdp	the <<-G>> argument to the compiler, assembler or linker.
94433965Sjdp*/
94533965Sjdp
94633965Sjdpvoid
94733965Sjdpbfd_set_gp_size (abfd, i)
94833965Sjdp     bfd *abfd;
94989857Sobrien     unsigned int i;
95033965Sjdp{
95189857Sobrien  /* Don't try to set GP size on an archive or core file!  */
95233965Sjdp  if (abfd->format != bfd_object)
95333965Sjdp    return;
95489857Sobrien
95533965Sjdp  if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
95633965Sjdp    ecoff_data (abfd)->gp_size = i;
95733965Sjdp  else if (abfd->xvec->flavour == bfd_target_elf_flavour)
95833965Sjdp    elf_gp_size (abfd) = i;
95933965Sjdp}
96033965Sjdp
96133965Sjdp/* Get the GP value.  This is an internal function used by some of the
96233965Sjdp   relocation special_function routines on targets which support a GP
96333965Sjdp   register.  */
96433965Sjdp
96533965Sjdpbfd_vma
96633965Sjdp_bfd_get_gp_value (abfd)
96733965Sjdp     bfd *abfd;
96833965Sjdp{
96989857Sobrien  if (abfd->format != bfd_object)
97089857Sobrien    return 0;
97189857Sobrien
97289857Sobrien  if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
97389857Sobrien    return ecoff_data (abfd)->gp;
97489857Sobrien  else if (abfd->xvec->flavour == bfd_target_elf_flavour)
97589857Sobrien    return elf_gp (abfd);
97689857Sobrien
97733965Sjdp  return 0;
97833965Sjdp}
97933965Sjdp
98033965Sjdp/* Set the GP value.  */
98133965Sjdp
98233965Sjdpvoid
98333965Sjdp_bfd_set_gp_value (abfd, v)
98433965Sjdp     bfd *abfd;
98533965Sjdp     bfd_vma v;
98633965Sjdp{
98733965Sjdp  if (abfd->format != bfd_object)
98833965Sjdp    return;
98989857Sobrien
99033965Sjdp  if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
99133965Sjdp    ecoff_data (abfd)->gp = v;
99233965Sjdp  else if (abfd->xvec->flavour == bfd_target_elf_flavour)
99333965Sjdp    elf_gp (abfd) = v;
99433965Sjdp}
99533965Sjdp
99633965Sjdp/*
99733965SjdpFUNCTION
99833965Sjdp	bfd_scan_vma
99933965Sjdp
100033965SjdpSYNOPSIS
100189857Sobrien	bfd_vma bfd_scan_vma(const char *string, const char **end, int base);
100233965Sjdp
100333965SjdpDESCRIPTION
100433965Sjdp	Convert, like <<strtoul>>, a numerical expression
100533965Sjdp	@var{string} into a <<bfd_vma>> integer, and return that integer.
100633965Sjdp	(Though without as many bells and whistles as <<strtoul>>.)
100733965Sjdp	The expression is assumed to be unsigned (i.e., positive).
100833965Sjdp	If given a @var{base}, it is used as the base for conversion.
100933965Sjdp	A base of 0 causes the function to interpret the string
101033965Sjdp	in hex if a leading "0x" or "0X" is found, otherwise
101133965Sjdp	in octal if a leading zero is found, otherwise in decimal.
101233965Sjdp
1013104834Sobrien	If the value would overflow, the maximum <<bfd_vma>> value is
1014104834Sobrien	returned.
101533965Sjdp*/
101633965Sjdp
101733965Sjdpbfd_vma
101833965Sjdpbfd_scan_vma (string, end, base)
101989857Sobrien     const char *string;
102089857Sobrien     const char **end;
102133965Sjdp     int base;
102233965Sjdp{
102333965Sjdp  bfd_vma value;
1024104834Sobrien  bfd_vma cutoff;
1025104834Sobrien  unsigned int cutlim;
1026104834Sobrien  int overflow;
102733965Sjdp
102833965Sjdp  /* Let the host do it if possible.  */
102977298Sobrien  if (sizeof (bfd_vma) <= sizeof (unsigned long))
103033965Sjdp    return (bfd_vma) strtoul (string, (char **) end, base);
103133965Sjdp
103233965Sjdp  if (base == 0)
103333965Sjdp    {
103433965Sjdp      if (string[0] == '0')
103533965Sjdp	{
103633965Sjdp	  if ((string[1] == 'x') || (string[1] == 'X'))
103733965Sjdp	    base = 16;
103833965Sjdp	  else
103933965Sjdp	    base = 8;
104033965Sjdp	}
104133965Sjdp    }
104289857Sobrien
1043104834Sobrien  if ((base < 2) || (base > 36))
1044104834Sobrien    base = 10;
104577298Sobrien
1046104834Sobrien  if (base == 16
1047104834Sobrien      && string[0] == '0'
1048104834Sobrien      && (string[1] == 'x' || string[1] == 'X')
1049104834Sobrien      && ISXDIGIT (string[2]))
1050104834Sobrien    {
1051104834Sobrien      string += 2;
1052104834Sobrien    }
105333965Sjdp
1054104834Sobrien  cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
1055104834Sobrien  cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
1056104834Sobrien  value = 0;
1057104834Sobrien  overflow = 0;
1058104834Sobrien  while (1)
1059104834Sobrien    {
1060104834Sobrien      unsigned int digit;
106133965Sjdp
1062104834Sobrien      digit = *string;
1063104834Sobrien      if (ISDIGIT (digit))
1064104834Sobrien	digit = digit - '0';
1065104834Sobrien      else if (ISALPHA (digit))
1066104834Sobrien	digit = TOUPPER (digit) - 'A' + 10;
1067104834Sobrien      else
1068104834Sobrien	break;
1069104834Sobrien      if (digit >= (unsigned int) base)
1070104834Sobrien	break;
1071104834Sobrien      if (value > cutoff || (value == cutoff && digit > cutlim))
1072104834Sobrien	overflow = 1;
1073104834Sobrien      value = value * base + digit;
1074104834Sobrien      ++string;
1075104834Sobrien    }
107633965Sjdp
1077104834Sobrien  if (overflow)
1078104834Sobrien    value = ~ (bfd_vma) 0;
1079104834Sobrien
1080104834Sobrien  if (end != NULL)
1081104834Sobrien    *end = string;
1082104834Sobrien
108333965Sjdp  return value;
108433965Sjdp}
108533965Sjdp
108633965Sjdp/*
108733965SjdpFUNCTION
108833965Sjdp	bfd_copy_private_bfd_data
108933965Sjdp
109033965SjdpSYNOPSIS
109133965Sjdp	boolean bfd_copy_private_bfd_data(bfd *ibfd, bfd *obfd);
109233965Sjdp
109333965SjdpDESCRIPTION
109477298Sobrien	Copy private BFD information from the BFD @var{ibfd} to the
109533965Sjdp	the BFD @var{obfd}.  Return <<true>> on success, <<false>> on error.
109633965Sjdp	Possible error returns are:
109733965Sjdp
109833965Sjdp	o <<bfd_error_no_memory>> -
109933965Sjdp	Not enough memory exists to create private data for @var{obfd}.
110033965Sjdp
110133965Sjdp.#define bfd_copy_private_bfd_data(ibfd, obfd) \
110233965Sjdp.     BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
110333965Sjdp.		(ibfd, obfd))
110433965Sjdp
110533965Sjdp*/
110633965Sjdp
110733965Sjdp/*
110833965SjdpFUNCTION
110933965Sjdp	bfd_merge_private_bfd_data
111033965Sjdp
111133965SjdpSYNOPSIS
111233965Sjdp	boolean bfd_merge_private_bfd_data(bfd *ibfd, bfd *obfd);
111333965Sjdp
111433965SjdpDESCRIPTION
111577298Sobrien	Merge private BFD information from the BFD @var{ibfd} to the
111633965Sjdp	the output file BFD @var{obfd} when linking.  Return <<true>>
111733965Sjdp	on success, <<false>> on error.  Possible error returns are:
111833965Sjdp
111933965Sjdp	o <<bfd_error_no_memory>> -
112033965Sjdp	Not enough memory exists to create private data for @var{obfd}.
112133965Sjdp
112233965Sjdp.#define bfd_merge_private_bfd_data(ibfd, obfd) \
112333965Sjdp.     BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
112433965Sjdp.		(ibfd, obfd))
112533965Sjdp
112633965Sjdp*/
112733965Sjdp
112833965Sjdp/*
112933965SjdpFUNCTION
113033965Sjdp	bfd_set_private_flags
113133965Sjdp
113233965SjdpSYNOPSIS
113333965Sjdp	boolean bfd_set_private_flags(bfd *abfd, flagword flags);
113433965Sjdp
113533965SjdpDESCRIPTION
113633965Sjdp	Set private BFD flag information in the BFD @var{abfd}.
113733965Sjdp	Return <<true>> on success, <<false>> on error.  Possible error
113833965Sjdp	returns are:
113933965Sjdp
114033965Sjdp	o <<bfd_error_no_memory>> -
114133965Sjdp	Not enough memory exists to create private data for @var{obfd}.
114233965Sjdp
114333965Sjdp.#define bfd_set_private_flags(abfd, flags) \
114433965Sjdp.     BFD_SEND (abfd, _bfd_set_private_flags, \
114533965Sjdp.		(abfd, flags))
114633965Sjdp
114733965Sjdp*/
114833965Sjdp
114933965Sjdp/*
115033965SjdpFUNCTION
115133965Sjdp	stuff
115233965Sjdp
115333965SjdpDESCRIPTION
115433965Sjdp	Stuff which should be documented:
115533965Sjdp
115633965Sjdp.#define bfd_sizeof_headers(abfd, reloc) \
115733965Sjdp.     BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
115833965Sjdp.
115933965Sjdp.#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
116033965Sjdp.     BFD_SEND (abfd, _bfd_find_nearest_line,  (abfd, sec, syms, off, file, func, line))
116133965Sjdp.
116233965Sjdp.       {* Do these three do anything useful at all, for any back end?  *}
116333965Sjdp.#define bfd_debug_info_start(abfd) \
116433965Sjdp.        BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
116533965Sjdp.
116633965Sjdp.#define bfd_debug_info_end(abfd) \
116733965Sjdp.        BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
116833965Sjdp.
116933965Sjdp.#define bfd_debug_info_accumulate(abfd, section) \
117033965Sjdp.        BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
117133965Sjdp.
117233965Sjdp.
117333965Sjdp.#define bfd_stat_arch_elt(abfd, stat) \
117433965Sjdp.        BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
117533965Sjdp.
117633965Sjdp.#define bfd_update_armap_timestamp(abfd) \
117733965Sjdp.        BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
117833965Sjdp.
117933965Sjdp.#define bfd_set_arch_mach(abfd, arch, mach)\
118033965Sjdp.        BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
118133965Sjdp.
118233965Sjdp.#define bfd_relax_section(abfd, section, link_info, again) \
118333965Sjdp.       BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
118433965Sjdp.
118560484Sobrien.#define bfd_gc_sections(abfd, link_info) \
118660484Sobrien.	BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
118760484Sobrien.
118889857Sobrien.#define bfd_merge_sections(abfd, link_info) \
118989857Sobrien.	BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
119089857Sobrien.
1191104834Sobrien.#define bfd_discard_group(abfd, sec) \
1192104834Sobrien.	BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
1193104834Sobrien.
119433965Sjdp.#define bfd_link_hash_table_create(abfd) \
119533965Sjdp.	BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
119633965Sjdp.
1197104834Sobrien.#define bfd_link_hash_table_free(abfd, hash) \
1198104834Sobrien.	BFD_SEND (abfd, _bfd_link_hash_table_free, (hash))
1199104834Sobrien.
120033965Sjdp.#define bfd_link_add_symbols(abfd, info) \
120133965Sjdp.	BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
120233965Sjdp.
1203104834Sobrien.#define bfd_link_just_syms(sec, info) \
1204104834Sobrien.	BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
1205104834Sobrien.
120633965Sjdp.#define bfd_final_link(abfd, info) \
120733965Sjdp.	BFD_SEND (abfd, _bfd_final_link, (abfd, info))
120833965Sjdp.
120933965Sjdp.#define bfd_free_cached_info(abfd) \
121033965Sjdp.       BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
121133965Sjdp.
121233965Sjdp.#define bfd_get_dynamic_symtab_upper_bound(abfd) \
121333965Sjdp.	BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
121433965Sjdp.
121533965Sjdp.#define bfd_print_private_bfd_data(abfd, file)\
121633965Sjdp.	BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
121733965Sjdp.
121833965Sjdp.#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
121933965Sjdp.	BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
122033965Sjdp.
122133965Sjdp.#define bfd_get_dynamic_reloc_upper_bound(abfd) \
122233965Sjdp.	BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
122333965Sjdp.
122433965Sjdp.#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
122533965Sjdp.	BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
122633965Sjdp.
122733965Sjdp.extern bfd_byte *bfd_get_relocated_section_contents
122833965Sjdp.	PARAMS ((bfd *, struct bfd_link_info *,
122933965Sjdp.		  struct bfd_link_order *, bfd_byte *,
123033965Sjdp.		  boolean, asymbol **));
123133965Sjdp.
123233965Sjdp
123333965Sjdp*/
123433965Sjdp
123533965Sjdpbfd_byte *
123633965Sjdpbfd_get_relocated_section_contents (abfd, link_info, link_order, data,
123733965Sjdp				    relocateable, symbols)
123833965Sjdp     bfd *abfd;
123933965Sjdp     struct bfd_link_info *link_info;
124033965Sjdp     struct bfd_link_order *link_order;
124133965Sjdp     bfd_byte *data;
124233965Sjdp     boolean relocateable;
124333965Sjdp     asymbol **symbols;
124433965Sjdp{
124533965Sjdp  bfd *abfd2;
124633965Sjdp  bfd_byte *(*fn) PARAMS ((bfd *, struct bfd_link_info *,
124733965Sjdp			   struct bfd_link_order *, bfd_byte *, boolean,
124833965Sjdp			   asymbol **));
124933965Sjdp
125033965Sjdp  if (link_order->type == bfd_indirect_link_order)
125133965Sjdp    {
125233965Sjdp      abfd2 = link_order->u.indirect.section->owner;
125389857Sobrien      if (abfd2 == NULL)
125433965Sjdp	abfd2 = abfd;
125533965Sjdp    }
125633965Sjdp  else
125733965Sjdp    abfd2 = abfd;
125889857Sobrien
125933965Sjdp  fn = abfd2->xvec->_bfd_get_relocated_section_contents;
126033965Sjdp
126133965Sjdp  return (*fn) (abfd, link_info, link_order, data, relocateable, symbols);
126233965Sjdp}
126333965Sjdp
126433965Sjdp/* Record information about an ELF program header.  */
126533965Sjdp
126633965Sjdpboolean
126733965Sjdpbfd_record_phdr (abfd, type, flags_valid, flags, at_valid, at,
126833965Sjdp		 includes_filehdr, includes_phdrs, count, secs)
126933965Sjdp     bfd *abfd;
127033965Sjdp     unsigned long type;
127133965Sjdp     boolean flags_valid;
127233965Sjdp     flagword flags;
127333965Sjdp     boolean at_valid;
127433965Sjdp     bfd_vma at;
127533965Sjdp     boolean includes_filehdr;
127633965Sjdp     boolean includes_phdrs;
127733965Sjdp     unsigned int count;
127833965Sjdp     asection **secs;
127933965Sjdp{
128033965Sjdp  struct elf_segment_map *m, **pm;
128189857Sobrien  bfd_size_type amt;
128233965Sjdp
128333965Sjdp  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
128433965Sjdp    return true;
128533965Sjdp
128689857Sobrien  amt = sizeof (struct elf_segment_map);
128789857Sobrien  amt += ((bfd_size_type) count - 1) * sizeof (asection *);
128889857Sobrien  m = (struct elf_segment_map *) bfd_alloc (abfd, amt);
128933965Sjdp  if (m == NULL)
129033965Sjdp    return false;
129133965Sjdp
129233965Sjdp  m->next = NULL;
129333965Sjdp  m->p_type = type;
129433965Sjdp  m->p_flags = flags;
129533965Sjdp  m->p_paddr = at;
129633965Sjdp  m->p_flags_valid = flags_valid;
129733965Sjdp  m->p_paddr_valid = at_valid;
129833965Sjdp  m->includes_filehdr = includes_filehdr;
129933965Sjdp  m->includes_phdrs = includes_phdrs;
130033965Sjdp  m->count = count;
130133965Sjdp  if (count > 0)
130233965Sjdp    memcpy (m->sections, secs, count * sizeof (asection *));
130333965Sjdp
130433965Sjdp  for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
130533965Sjdp    ;
130633965Sjdp  *pm = m;
130733965Sjdp
130833965Sjdp  return true;
130933965Sjdp}
131089857Sobrien
131189857Sobrienvoid
131289857Sobrienbfd_sprintf_vma (abfd, buf, value)
131389857Sobrien     bfd *abfd;
131489857Sobrien     char *buf;
131589857Sobrien     bfd_vma value;
131689857Sobrien{
131789857Sobrien  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
131889857Sobrien    get_elf_backend_data (abfd)->elf_backend_sprintf_vma (abfd, buf, value);
131989857Sobrien  else
132089857Sobrien    sprintf_vma (buf, value);
132189857Sobrien}
132289857Sobrien
132389857Sobrienvoid
132489857Sobrienbfd_fprintf_vma (abfd, stream, value)
132589857Sobrien     bfd *abfd;
132689857Sobrien     PTR stream;
132789857Sobrien     bfd_vma value;
132889857Sobrien{
132989857Sobrien  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
133089857Sobrien    get_elf_backend_data (abfd)->elf_backend_fprintf_vma (abfd, stream, value);
133189857Sobrien  else
133289857Sobrien    fprintf_vma ((FILE *) stream, value);
133389857Sobrien}
133489857Sobrien
133589857Sobrien/*
133689857SobrienFUNCTION
133789857Sobrien	bfd_alt_mach_code
133889857Sobrien
133989857SobrienSYNOPSIS
1340104834Sobrien	boolean bfd_alt_mach_code(bfd *abfd, int alternative);
134189857Sobrien
134289857SobrienDESCRIPTION
134389857Sobrien
134489857Sobrien	When more than one machine code number is available for the
134589857Sobrien	same machine type, this function can be used to switch between
1346104834Sobrien	the preferred one (alternative == 0) and any others.  Currently,
134789857Sobrien	only ELF supports this feature, with up to two alternate
134889857Sobrien	machine codes.
134989857Sobrien*/
135089857Sobrien
135189857Sobrienboolean
1352104834Sobrienbfd_alt_mach_code (abfd, alternative)
135389857Sobrien     bfd *abfd;
1354104834Sobrien     int alternative;
135589857Sobrien{
135689857Sobrien  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
135789857Sobrien    {
135889857Sobrien      int code;
135989857Sobrien
1360104834Sobrien      switch (alternative)
136189857Sobrien	{
136289857Sobrien	case 0:
136389857Sobrien	  code = get_elf_backend_data (abfd)->elf_machine_code;
136489857Sobrien	  break;
136589857Sobrien
136689857Sobrien	case 1:
136789857Sobrien	  code = get_elf_backend_data (abfd)->elf_machine_alt1;
136889857Sobrien	  if (code == 0)
136989857Sobrien	    return false;
137089857Sobrien	  break;
137189857Sobrien
137289857Sobrien	case 2:
137389857Sobrien	  code = get_elf_backend_data (abfd)->elf_machine_alt2;
137489857Sobrien	  if (code == 0)
137589857Sobrien	    return false;
137689857Sobrien	  break;
137789857Sobrien
137889857Sobrien	default:
137989857Sobrien	  return false;
138089857Sobrien	}
138189857Sobrien
138289857Sobrien      elf_elfheader (abfd)->e_machine = code;
138389857Sobrien
138489857Sobrien      return true;
138589857Sobrien    }
138689857Sobrien
138789857Sobrien  return false;
138889857Sobrien}
1389