Back to index

cell-binutils  2.17cvs20070401
bfd.c
Go to the documentation of this file.
00001 /* Generic BFD library interface and support routines.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
00003    2000, 2001, 2002, 2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    Written by Cygnus Support.
00006 
00007    This file is part of BFD, the Binary File Descriptor library.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00022 
00023 /*
00024 SECTION
00025        <<typedef bfd>>
00026 
00027        A BFD has type <<bfd>>; objects of this type are the
00028        cornerstone of any application using BFD. Using BFD
00029        consists of making references though the BFD and to data in the BFD.
00030 
00031        Here is the structure that defines the type <<bfd>>.  It
00032        contains the major data about the file and pointers
00033        to the rest of the data.
00034 
00035 CODE_FRAGMENT
00036 .
00037 .struct bfd
00038 .{
00039 .  {* A unique identifier of the BFD  *}
00040 .  unsigned int id;
00041 .
00042 .  {* The filename the application opened the BFD with.  *}
00043 .  const char *filename;
00044 .
00045 .  {* A pointer to the target jump table.  *}
00046 .  const struct bfd_target *xvec;
00047 .
00048 .  {* The IOSTREAM, and corresponding IO vector that provide access
00049 .     to the file backing the BFD.  *}
00050 .  void *iostream;
00051 .  const struct bfd_iovec *iovec;
00052 .
00053 .  {* Is the file descriptor being cached?  That is, can it be closed as
00054 .     needed, and re-opened when accessed later?  *}
00055 .  bfd_boolean cacheable;
00056 .
00057 .  {* Marks whether there was a default target specified when the
00058 .     BFD was opened. This is used to select which matching algorithm
00059 .     to use to choose the back end.  *}
00060 .  bfd_boolean target_defaulted;
00061 .
00062 .  {* The caching routines use these to maintain a
00063 .     least-recently-used list of BFDs.  *}
00064 .  struct bfd *lru_prev, *lru_next;
00065 .
00066 .  {* When a file is closed by the caching routines, BFD retains
00067 .     state information on the file here...  *}
00068 .  ufile_ptr where;
00069 .
00070 .  {* ... and here: (``once'' means at least once).  *}
00071 .  bfd_boolean opened_once;
00072 .
00073 .  {* Set if we have a locally maintained mtime value, rather than
00074 .     getting it from the file each time.  *}
00075 .  bfd_boolean mtime_set;
00076 .
00077 .  {* File modified time, if mtime_set is TRUE.  *}
00078 .  long mtime;
00079 .
00080 .  {* Reserved for an unimplemented file locking extension.  *}
00081 .  int ifd;
00082 .
00083 .  {* The format which belongs to the BFD. (object, core, etc.)  *}
00084 .  bfd_format format;
00085 .
00086 .  {* The direction with which the BFD was opened.  *}
00087 .  enum bfd_direction
00088 .    {
00089 .      no_direction = 0,
00090 .      read_direction = 1,
00091 .      write_direction = 2,
00092 .      both_direction = 3
00093 .    }
00094 .  direction;
00095 .
00096 .  {* Format_specific flags.  *}
00097 .  flagword flags;
00098 .
00099 .  {* Currently my_archive is tested before adding origin to
00100 .     anything. I believe that this can become always an add of
00101 .     origin, with origin set to 0 for non archive files.  *}
00102 .  ufile_ptr origin;
00103 .
00104 .  {* Remember when output has begun, to stop strange things
00105 .     from happening.  *}
00106 .  bfd_boolean output_has_begun;
00107 .
00108 .  {* A hash table for section names.  *}
00109 .  struct bfd_hash_table section_htab;
00110 .
00111 .  {* Pointer to linked list of sections.  *}
00112 .  struct bfd_section *sections;
00113 .
00114 .  {* The last section on the section list.  *}
00115 .  struct bfd_section *section_last;
00116 .
00117 .  {* The number of sections.  *}
00118 .  unsigned int section_count;
00119 .
00120 .  {* Stuff only useful for object files:
00121 .     The start address.  *}
00122 .  bfd_vma start_address;
00123 .
00124 .  {* Used for input and output.  *}
00125 .  unsigned int symcount;
00126 .
00127 .  {* Symbol table for output BFD (with symcount entries).  *}
00128 .  struct bfd_symbol  **outsymbols;
00129 .
00130 .  {* Used for slurped dynamic symbol tables.  *}
00131 .  unsigned int dynsymcount;
00132 .
00133 .  {* Pointer to structure which contains architecture information.  *}
00134 .  const struct bfd_arch_info *arch_info;
00135 .
00136 .  {* Flag set if symbols from this BFD should not be exported.  *}
00137 .  bfd_boolean no_export;
00138 .
00139 .  {* Stuff only useful for archives.  *}
00140 .  void *arelt_data;
00141 .  struct bfd *my_archive;      {* The containing archive BFD.  *}
00142 .  struct bfd *next;            {* The next BFD in the archive.  *}
00143 .  struct bfd *archive_head;    {* The first BFD in the archive.  *}
00144 .  bfd_boolean has_armap;
00145 .
00146 .  {* A chain of BFD structures involved in a link.  *}
00147 .  struct bfd *link_next;
00148 .
00149 .  {* A field used by _bfd_generic_link_add_archive_symbols.  This will
00150 .     be used only for archive elements.  *}
00151 .  int archive_pass;
00152 .
00153 .  {* Used by the back end to hold private data.  *}
00154 .  union
00155 .    {
00156 .      struct aout_data_struct *aout_data;
00157 .      struct artdata *aout_ar_data;
00158 .      struct _oasys_data *oasys_obj_data;
00159 .      struct _oasys_ar_data *oasys_ar_data;
00160 .      struct coff_tdata *coff_obj_data;
00161 .      struct pe_tdata *pe_obj_data;
00162 .      struct xcoff_tdata *xcoff_obj_data;
00163 .      struct ecoff_tdata *ecoff_obj_data;
00164 .      struct ieee_data_struct *ieee_data;
00165 .      struct ieee_ar_data_struct *ieee_ar_data;
00166 .      struct srec_data_struct *srec_data;
00167 .      struct ihex_data_struct *ihex_data;
00168 .      struct tekhex_data_struct *tekhex_data;
00169 .      struct elf_obj_tdata *elf_obj_data;
00170 .      struct nlm_obj_tdata *nlm_obj_data;
00171 .      struct bout_data_struct *bout_data;
00172 .      struct mmo_data_struct *mmo_data;
00173 .      struct sun_core_struct *sun_core_data;
00174 .      struct sco5_core_struct *sco5_core_data;
00175 .      struct trad_core_struct *trad_core_data;
00176 .      struct som_data_struct *som_data;
00177 .      struct hpux_core_struct *hpux_core_data;
00178 .      struct hppabsd_core_struct *hppabsd_core_data;
00179 .      struct sgi_core_struct *sgi_core_data;
00180 .      struct lynx_core_struct *lynx_core_data;
00181 .      struct osf_core_struct *osf_core_data;
00182 .      struct cisco_core_struct *cisco_core_data;
00183 .      struct versados_data_struct *versados_data;
00184 .      struct netbsd_core_struct *netbsd_core_data;
00185 .      struct mach_o_data_struct *mach_o_data;
00186 .      struct mach_o_fat_data_struct *mach_o_fat_data;
00187 .      struct bfd_pef_data_struct *pef_data;
00188 .      struct bfd_pef_xlib_data_struct *pef_xlib_data;
00189 .      struct bfd_sym_data_struct *sym_data;
00190 .      void *any;
00191 .    }
00192 .  tdata;
00193 .
00194 .  {* Used by the application to hold private data.  *}
00195 .  void *usrdata;
00196 .
00197 .  {* Where all the allocated stuff under this BFD goes.  This is a
00198 .     struct objalloc *, but we use void * to avoid requiring the inclusion
00199 .     of objalloc.h.  *}
00200 .  void *memory;
00201 .};
00202 .
00203 */
00204 
00205 #include "bfd.h"
00206 #include "bfdver.h"
00207 #include "sysdep.h"
00208 #include <stdarg.h>
00209 #include "libiberty.h"
00210 #include "safe-ctype.h"
00211 #include "bfdlink.h"
00212 #include "libbfd.h"
00213 #include "coff/internal.h"
00214 #include "coff/sym.h"
00215 #include "libcoff.h"
00216 #include "libecoff.h"
00217 #undef obj_symbols
00218 #include "elf-bfd.h"
00219 
00220 #ifndef EXIT_FAILURE
00221 #define EXIT_FAILURE 1
00222 #endif
00223 
00224 
00225 /* provide storage for subsystem, stack and heap data which may have been
00226    passed in on the command line.  Ld puts this data into a bfd_link_info
00227    struct which ultimately gets passed in to the bfd.  When it arrives, copy
00228    it to the following struct so that the data will be available in coffcode.h
00229    where it is needed.  The typedef's used are defined in bfd.h */
00230 
00231 /*
00232 SECTION
00233        Error reporting
00234 
00235        Most BFD functions return nonzero on success (check their
00236        individual documentation for precise semantics).  On an error,
00237        they call <<bfd_set_error>> to set an error condition that callers
00238        can check by calling <<bfd_get_error>>.
00239         If that returns <<bfd_error_system_call>>, then check
00240        <<errno>>.
00241 
00242        The easiest way to report a BFD error to the user is to
00243        use <<bfd_perror>>.
00244 
00245 SUBSECTION
00246        Type <<bfd_error_type>>
00247 
00248        The values returned by <<bfd_get_error>> are defined by the
00249        enumerated type <<bfd_error_type>>.
00250 
00251 CODE_FRAGMENT
00252 .
00253 .typedef enum bfd_error
00254 .{
00255 .  bfd_error_no_error = 0,
00256 .  bfd_error_system_call,
00257 .  bfd_error_invalid_target,
00258 .  bfd_error_wrong_format,
00259 .  bfd_error_wrong_object_format,
00260 .  bfd_error_invalid_operation,
00261 .  bfd_error_no_memory,
00262 .  bfd_error_no_symbols,
00263 .  bfd_error_no_armap,
00264 .  bfd_error_no_more_archived_files,
00265 .  bfd_error_malformed_archive,
00266 .  bfd_error_file_not_recognized,
00267 .  bfd_error_file_ambiguously_recognized,
00268 .  bfd_error_no_contents,
00269 .  bfd_error_nonrepresentable_section,
00270 .  bfd_error_no_debug_section,
00271 .  bfd_error_bad_value,
00272 .  bfd_error_file_truncated,
00273 .  bfd_error_file_too_big,
00274 .  bfd_error_on_input,
00275 .  bfd_error_invalid_error_code
00276 .}
00277 .bfd_error_type;
00278 .
00279 */
00280 
00281 static bfd_error_type bfd_error = bfd_error_no_error;
00282 static bfd *input_bfd = NULL;
00283 static bfd_error_type input_error = bfd_error_no_error;
00284 
00285 const char *const bfd_errmsgs[] =
00286 {
00287   N_("No error"),
00288   N_("System call error"),
00289   N_("Invalid bfd target"),
00290   N_("File in wrong format"),
00291   N_("Archive object file in wrong format"),
00292   N_("Invalid operation"),
00293   N_("Memory exhausted"),
00294   N_("No symbols"),
00295   N_("Archive has no index; run ranlib to add one"),
00296   N_("No more archived files"),
00297   N_("Malformed archive"),
00298   N_("File format not recognized"),
00299   N_("File format is ambiguous"),
00300   N_("Section has no contents"),
00301   N_("Nonrepresentable section on output"),
00302   N_("Symbol needs debug section which does not exist"),
00303   N_("Bad value"),
00304   N_("File truncated"),
00305   N_("File too big"),
00306   N_("Error reading %s: %s"),
00307   N_("#<Invalid error code>")
00308 };
00309 
00310 /*
00311 FUNCTION
00312        bfd_get_error
00313 
00314 SYNOPSIS
00315        bfd_error_type bfd_get_error (void);
00316 
00317 DESCRIPTION
00318        Return the current BFD error condition.
00319 */
00320 
00321 bfd_error_type
00322 bfd_get_error (void)
00323 {
00324   return bfd_error;
00325 }
00326 
00327 /*
00328 FUNCTION
00329        bfd_set_error
00330 
00331 SYNOPSIS
00332        void bfd_set_error (bfd_error_type error_tag, ...);
00333 
00334 DESCRIPTION
00335        Set the BFD error condition to be @var{error_tag}.
00336        If @var{error_tag} is bfd_error_on_input, then this function
00337        takes two more parameters, the input bfd where the error
00338        occurred, and the bfd_error_type error.
00339 */
00340 
00341 void
00342 bfd_set_error (bfd_error_type error_tag, ...)
00343 {
00344   bfd_error = error_tag;
00345   if (error_tag == bfd_error_on_input)
00346     {
00347       /* This is an error that occurred during bfd_close when
00348         writing an archive, but on one of the input files.  */
00349       va_list ap;
00350 
00351       va_start (ap, error_tag);
00352       input_bfd = va_arg (ap, bfd *);
00353       input_error = va_arg (ap, int);
00354       if (input_error >= bfd_error_on_input)
00355        abort ();
00356       va_end (ap);
00357     }
00358 }
00359 
00360 /*
00361 FUNCTION
00362        bfd_errmsg
00363 
00364 SYNOPSIS
00365        const char *bfd_errmsg (bfd_error_type error_tag);
00366 
00367 DESCRIPTION
00368        Return a string describing the error @var{error_tag}, or
00369        the system error if @var{error_tag} is <<bfd_error_system_call>>.
00370 */
00371 
00372 const char *
00373 bfd_errmsg (bfd_error_type error_tag)
00374 {
00375 #ifndef errno
00376   extern int errno;
00377 #endif
00378   if (error_tag == bfd_error_on_input)
00379     {
00380       char *buf;
00381       const char *msg = bfd_errmsg (input_error);
00382 
00383       if (asprintf (&buf, _(bfd_errmsgs [error_tag]), input_bfd->filename, msg)
00384          != -1)
00385        return buf;
00386 
00387       /* Ick, what to do on out of memory?  */
00388       return msg;
00389     }
00390 
00391   if (error_tag == bfd_error_system_call)
00392     return xstrerror (errno);
00393 
00394   if (error_tag > bfd_error_invalid_error_code)
00395     error_tag = bfd_error_invalid_error_code;    /* sanity check */
00396 
00397   return _(bfd_errmsgs [error_tag]);
00398 }
00399 
00400 /*
00401 FUNCTION
00402        bfd_perror
00403 
00404 SYNOPSIS
00405        void bfd_perror (const char *message);
00406 
00407 DESCRIPTION
00408        Print to the standard error stream a string describing the
00409        last BFD error that occurred, or the last system error if
00410        the last BFD error was a system call failure.  If @var{message}
00411        is non-NULL and non-empty, the error string printed is preceded
00412        by @var{message}, a colon, and a space.  It is followed by a newline.
00413 */
00414 
00415 void
00416 bfd_perror (const char *message)
00417 {
00418   if (message == NULL || *message == '\0')
00419     fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
00420   else
00421     fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
00422 }
00423 
00424 /*
00425 SUBSECTION
00426        BFD error handler
00427 
00428        Some BFD functions want to print messages describing the
00429        problem.  They call a BFD error handler function.  This
00430        function may be overridden by the program.
00431 
00432        The BFD error handler acts like printf.
00433 
00434 CODE_FRAGMENT
00435 .
00436 .typedef void (*bfd_error_handler_type) (const char *, ...);
00437 .
00438 */
00439 
00440 /* The program name used when printing BFD error messages.  */
00441 
00442 static const char *_bfd_error_program_name;
00443 
00444 /* This is the default routine to handle BFD error messages.
00445    Like fprintf (stderr, ...), but also handles some extra format specifiers.
00446 
00447    %A section name from section.  For group components, print group name too.
00448    %B file name from bfd.  For archive components, prints archive too.
00449 
00450    Note - because these two extra format specifiers require special handling
00451    they are scanned for and processed in this function, before calling
00452    vfprintf.  This means that the *arguments* for these format specifiers
00453    must be the first ones in the variable argument list, regardless of where
00454    the specifiers appear in the format string.  Thus for example calling
00455    this function with a format string of:
00456 
00457       "blah %s blah %A blah %d blah %B"
00458 
00459    would involve passing the arguments as:
00460 
00461       "blah %s blah %A blah %d blah %B",
00462         asection_for_the_%A,
00463        bfd_for_the_%B,
00464        string_for_the_%s,
00465        integer_for_the_%d);
00466  */
00467 
00468 void
00469 _bfd_default_error_handler (const char *fmt, ...)
00470 {
00471   va_list ap;
00472   char *bufp;
00473   const char *new_fmt, *p;
00474   size_t avail = 1000;
00475   char buf[1000];
00476 
00477   if (_bfd_error_program_name != NULL)
00478     fprintf (stderr, "%s: ", _bfd_error_program_name);
00479   else
00480     fprintf (stderr, "BFD: ");
00481 
00482   va_start (ap, fmt);
00483   new_fmt = fmt;
00484   bufp = buf;
00485 
00486   /* Reserve enough space for the existing format string.  */
00487   avail -= strlen (fmt) + 1;
00488   if (avail > 1000)
00489     _exit (EXIT_FAILURE);
00490 
00491   p = fmt;
00492   while (1)
00493     {
00494       char *q;
00495       size_t len, extra, trim;
00496 
00497       p = strchr (p, '%');
00498       if (p == NULL || p[1] == '\0')
00499        {
00500          if (new_fmt == buf)
00501            {
00502              len = strlen (fmt);
00503              memcpy (bufp, fmt, len + 1);
00504            }
00505          break;
00506        }
00507 
00508       if (p[1] == 'A' || p[1] == 'B')
00509        {
00510          len = p - fmt;
00511          memcpy (bufp, fmt, len);
00512          bufp += len;
00513          fmt = p + 2;
00514          new_fmt = buf;
00515 
00516          /* If we run out of space, tough, you lose your ridiculously
00517             long file or section name.  It's not safe to try to alloc
00518             memory here;  We might be printing an out of memory message.  */
00519          if (avail == 0)
00520            {
00521              *bufp++ = '*';
00522              *bufp++ = '*';
00523              *bufp = '\0';
00524            }
00525          else
00526            {
00527              if (p[1] == 'B')
00528               {
00529                 bfd *abfd = va_arg (ap, bfd *);
00530 
00531                 if (abfd == NULL)
00532                   /* Invoking %B with a null bfd pointer is an internal error.  */
00533                   abort ();
00534                 else if (abfd->my_archive)
00535                   snprintf (bufp, avail, "%s(%s)",
00536                            abfd->my_archive->filename, abfd->filename);
00537                 else
00538                   snprintf (bufp, avail, "%s", abfd->filename);
00539               }
00540              else
00541               {
00542                 asection *sec = va_arg (ap, asection *);
00543                 bfd *abfd;
00544                 const char *group = NULL;
00545                 struct coff_comdat_info *ci;
00546 
00547                 if (sec == NULL)
00548                   /* Invoking %A with a null section pointer is an internal error.  */
00549                   abort ();
00550                 abfd = sec->owner;
00551                 if (abfd != NULL
00552                     && bfd_get_flavour (abfd) == bfd_target_elf_flavour
00553                     && elf_next_in_group (sec) != NULL
00554                     && (sec->flags & SEC_GROUP) == 0)
00555                   group = elf_group_name (sec);
00556                 else if (abfd != NULL
00557                         && bfd_get_flavour (abfd) == bfd_target_coff_flavour
00558                         && (ci = bfd_coff_get_comdat_section (sec->owner,
00559                                                          sec)) != NULL)
00560                   group = ci->name;
00561                 if (group != NULL)
00562                   snprintf (bufp, avail, "%s[%s]", sec->name, group);
00563                 else
00564                   snprintf (bufp, avail, "%s", sec->name);
00565               }
00566              len = strlen (bufp);
00567              avail = avail - len + 2;
00568 
00569              /* We need to replace any '%' we printed by "%%".
00570                First count how many.  */
00571              q = bufp;
00572              bufp += len;
00573              extra = 0;
00574              while ((q = strchr (q, '%')) != NULL)
00575               {
00576                 ++q;
00577                 ++extra;
00578               }
00579 
00580              /* If there isn't room, trim off the end of the string.  */
00581              q = bufp;
00582              bufp += extra;
00583              if (extra > avail)
00584               {
00585                 trim = extra - avail;
00586                 bufp -= trim;
00587                 do
00588                   {
00589                     if (*--q == '%')
00590                      --extra;
00591                   }
00592                 while (--trim != 0);
00593                 *q = '\0';
00594                 avail = extra;
00595               }
00596              avail -= extra;
00597 
00598              /* Now double all '%' chars, shuffling the string as we go.  */
00599              while (extra != 0)
00600               {
00601                 while ((q[extra] = *q) != '%')
00602                   --q;
00603                 q[--extra] = '%';
00604                 --q;
00605               }
00606            }
00607        }
00608       p = p + 2;
00609     }
00610 
00611   vfprintf (stderr, new_fmt, ap);
00612   va_end (ap);
00613 
00614   putc ('\n', stderr);
00615 }
00616 
00617 /* This is a function pointer to the routine which should handle BFD
00618    error messages.  It is called when a BFD routine encounters an
00619    error for which it wants to print a message.  Going through a
00620    function pointer permits a program linked against BFD to intercept
00621    the messages and deal with them itself.  */
00622 
00623 bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
00624 
00625 /*
00626 FUNCTION
00627        bfd_set_error_handler
00628 
00629 SYNOPSIS
00630        bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
00631 
00632 DESCRIPTION
00633        Set the BFD error handler function.  Returns the previous
00634        function.
00635 */
00636 
00637 bfd_error_handler_type
00638 bfd_set_error_handler (bfd_error_handler_type pnew)
00639 {
00640   bfd_error_handler_type pold;
00641 
00642   pold = _bfd_error_handler;
00643   _bfd_error_handler = pnew;
00644   return pold;
00645 }
00646 
00647 /*
00648 FUNCTION
00649        bfd_set_error_program_name
00650 
00651 SYNOPSIS
00652        void bfd_set_error_program_name (const char *);
00653 
00654 DESCRIPTION
00655        Set the program name to use when printing a BFD error.  This
00656        is printed before the error message followed by a colon and
00657        space.  The string must not be changed after it is passed to
00658        this function.
00659 */
00660 
00661 void
00662 bfd_set_error_program_name (const char *name)
00663 {
00664   _bfd_error_program_name = name;
00665 }
00666 
00667 /*
00668 FUNCTION
00669        bfd_get_error_handler
00670 
00671 SYNOPSIS
00672        bfd_error_handler_type bfd_get_error_handler (void);
00673 
00674 DESCRIPTION
00675        Return the BFD error handler function.
00676 */
00677 
00678 bfd_error_handler_type
00679 bfd_get_error_handler (void)
00680 {
00681   return _bfd_error_handler;
00682 }
00683 
00684 /*
00685 SECTION
00686        Miscellaneous
00687 
00688 SUBSECTION
00689        Miscellaneous functions
00690 */
00691 
00692 /*
00693 FUNCTION
00694        bfd_get_reloc_upper_bound
00695 
00696 SYNOPSIS
00697        long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
00698 
00699 DESCRIPTION
00700        Return the number of bytes required to store the
00701        relocation information associated with section @var{sect}
00702        attached to bfd @var{abfd}.  If an error occurs, return -1.
00703 
00704 */
00705 
00706 long
00707 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
00708 {
00709   if (abfd->format != bfd_object)
00710     {
00711       bfd_set_error (bfd_error_invalid_operation);
00712       return -1;
00713     }
00714 
00715   return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
00716 }
00717 
00718 /*
00719 FUNCTION
00720        bfd_canonicalize_reloc
00721 
00722 SYNOPSIS
00723        long bfd_canonicalize_reloc
00724          (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
00725 
00726 DESCRIPTION
00727        Call the back end associated with the open BFD
00728        @var{abfd} and translate the external form of the relocation
00729        information attached to @var{sec} into the internal canonical
00730        form.  Place the table into memory at @var{loc}, which has
00731        been preallocated, usually by a call to
00732        <<bfd_get_reloc_upper_bound>>.  Returns the number of relocs, or
00733        -1 on error.
00734 
00735        The @var{syms} table is also needed for horrible internal magic
00736        reasons.
00737 
00738 */
00739 long
00740 bfd_canonicalize_reloc (bfd *abfd,
00741                      sec_ptr asect,
00742                      arelent **location,
00743                      asymbol **symbols)
00744 {
00745   if (abfd->format != bfd_object)
00746     {
00747       bfd_set_error (bfd_error_invalid_operation);
00748       return -1;
00749     }
00750 
00751   return BFD_SEND (abfd, _bfd_canonicalize_reloc,
00752                  (abfd, asect, location, symbols));
00753 }
00754 
00755 /*
00756 FUNCTION
00757        bfd_set_reloc
00758 
00759 SYNOPSIS
00760        void bfd_set_reloc
00761          (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
00762 
00763 DESCRIPTION
00764        Set the relocation pointer and count within
00765        section @var{sec} to the values @var{rel} and @var{count}.
00766        The argument @var{abfd} is ignored.
00767 
00768 */
00769 
00770 void
00771 bfd_set_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED,
00772               sec_ptr asect,
00773               arelent **location,
00774               unsigned int count)
00775 {
00776   asect->orelocation = location;
00777   asect->reloc_count = count;
00778 }
00779 
00780 /*
00781 FUNCTION
00782        bfd_set_file_flags
00783 
00784 SYNOPSIS
00785        bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags);
00786 
00787 DESCRIPTION
00788        Set the flag word in the BFD @var{abfd} to the value @var{flags}.
00789 
00790        Possible errors are:
00791        o <<bfd_error_wrong_format>> - The target bfd was not of object format.
00792        o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
00793        o <<bfd_error_invalid_operation>> -
00794        The flag word contained a bit which was not applicable to the
00795        type of file.  E.g., an attempt was made to set the <<D_PAGED>> bit
00796        on a BFD format which does not support demand paging.
00797 
00798 */
00799 
00800 bfd_boolean
00801 bfd_set_file_flags (bfd *abfd, flagword flags)
00802 {
00803   if (abfd->format != bfd_object)
00804     {
00805       bfd_set_error (bfd_error_wrong_format);
00806       return FALSE;
00807     }
00808 
00809   if (bfd_read_p (abfd))
00810     {
00811       bfd_set_error (bfd_error_invalid_operation);
00812       return FALSE;
00813     }
00814 
00815   bfd_get_file_flags (abfd) = flags;
00816   if ((flags & bfd_applicable_file_flags (abfd)) != flags)
00817     {
00818       bfd_set_error (bfd_error_invalid_operation);
00819       return FALSE;
00820     }
00821 
00822   return TRUE;
00823 }
00824 
00825 void
00826 bfd_assert (const char *file, int line)
00827 {
00828   (*_bfd_error_handler) (_("BFD %s assertion fail %s:%d"),
00829                       BFD_VERSION_STRING, file, line);
00830 }
00831 
00832 /* A more or less friendly abort message.  In libbfd.h abort is
00833    defined to call this function.  */
00834 
00835 void
00836 _bfd_abort (const char *file, int line, const char *fn)
00837 {
00838   if (fn != NULL)
00839     (*_bfd_error_handler)
00840       (_("BFD %s internal error, aborting at %s line %d in %s\n"),
00841        BFD_VERSION_STRING, file, line, fn);
00842   else
00843     (*_bfd_error_handler)
00844       (_("BFD %s internal error, aborting at %s line %d\n"),
00845        BFD_VERSION_STRING, file, line);
00846   (*_bfd_error_handler) (_("Please report this bug.\n"));
00847   _exit (EXIT_FAILURE);
00848 }
00849 
00850 /*
00851 FUNCTION
00852        bfd_get_arch_size
00853 
00854 SYNOPSIS
00855        int bfd_get_arch_size (bfd *abfd);
00856 
00857 DESCRIPTION
00858        Returns the architecture address size, in bits, as determined
00859        by the object file's format.  For ELF, this information is
00860        included in the header.
00861 
00862 RETURNS
00863        Returns the arch size in bits if known, <<-1>> otherwise.
00864 */
00865 
00866 int
00867 bfd_get_arch_size (bfd *abfd)
00868 {
00869   if (abfd->xvec->flavour == bfd_target_elf_flavour)
00870     return get_elf_backend_data (abfd)->s->arch_size;
00871 
00872   return -1;
00873 }
00874 
00875 /*
00876 FUNCTION
00877        bfd_get_sign_extend_vma
00878 
00879 SYNOPSIS
00880        int bfd_get_sign_extend_vma (bfd *abfd);
00881 
00882 DESCRIPTION
00883        Indicates if the target architecture "naturally" sign extends
00884        an address.  Some architectures implicitly sign extend address
00885        values when they are converted to types larger than the size
00886        of an address.  For instance, bfd_get_start_address() will
00887        return an address sign extended to fill a bfd_vma when this is
00888        the case.
00889 
00890 RETURNS
00891        Returns <<1>> if the target architecture is known to sign
00892        extend addresses, <<0>> if the target architecture is known to
00893        not sign extend addresses, and <<-1>> otherwise.
00894 */
00895 
00896 int
00897 bfd_get_sign_extend_vma (bfd *abfd)
00898 {
00899   char *name;
00900 
00901   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
00902     return get_elf_backend_data (abfd)->sign_extend_vma;
00903 
00904   name = bfd_get_target (abfd);
00905 
00906   /* Return a proper value for DJGPP & PE COFF.
00907      This function is required for DWARF2 support, but there is
00908      no place to store this information in the COFF back end.
00909      Should enough other COFF targets add support for DWARF2,
00910      a place will have to be found.  Until then, this hack will do.  */
00911   if (CONST_STRNEQ (name, "coff-go32")
00912       || strcmp (name, "pe-i386") == 0
00913       || strcmp (name, "pei-i386") == 0
00914       || strcmp (name, "pe-arm-wince-little") == 0
00915       || strcmp (name, "pei-arm-wince-little") == 0)
00916     return 1;
00917 
00918   bfd_set_error (bfd_error_wrong_format);
00919   return -1;
00920 }
00921 
00922 /*
00923 FUNCTION
00924        bfd_set_start_address
00925 
00926 SYNOPSIS
00927        bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma);
00928 
00929 DESCRIPTION
00930        Make @var{vma} the entry point of output BFD @var{abfd}.
00931 
00932 RETURNS
00933        Returns <<TRUE>> on success, <<FALSE>> otherwise.
00934 */
00935 
00936 bfd_boolean
00937 bfd_set_start_address (bfd *abfd, bfd_vma vma)
00938 {
00939   abfd->start_address = vma;
00940   return TRUE;
00941 }
00942 
00943 /*
00944 FUNCTION
00945        bfd_get_gp_size
00946 
00947 SYNOPSIS
00948        unsigned int bfd_get_gp_size (bfd *abfd);
00949 
00950 DESCRIPTION
00951        Return the maximum size of objects to be optimized using the GP
00952        register under MIPS ECOFF.  This is typically set by the <<-G>>
00953        argument to the compiler, assembler or linker.
00954 */
00955 
00956 unsigned int
00957 bfd_get_gp_size (bfd *abfd)
00958 {
00959   if (abfd->format == bfd_object)
00960     {
00961       if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
00962        return ecoff_data (abfd)->gp_size;
00963       else if (abfd->xvec->flavour == bfd_target_elf_flavour)
00964        return elf_gp_size (abfd);
00965     }
00966   return 0;
00967 }
00968 
00969 /*
00970 FUNCTION
00971        bfd_set_gp_size
00972 
00973 SYNOPSIS
00974        void bfd_set_gp_size (bfd *abfd, unsigned int i);
00975 
00976 DESCRIPTION
00977        Set the maximum size of objects to be optimized using the GP
00978        register under ECOFF or MIPS ELF.  This is typically set by
00979        the <<-G>> argument to the compiler, assembler or linker.
00980 */
00981 
00982 void
00983 bfd_set_gp_size (bfd *abfd, unsigned int i)
00984 {
00985   /* Don't try to set GP size on an archive or core file!  */
00986   if (abfd->format != bfd_object)
00987     return;
00988 
00989   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
00990     ecoff_data (abfd)->gp_size = i;
00991   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
00992     elf_gp_size (abfd) = i;
00993 }
00994 
00995 /* Get the GP value.  This is an internal function used by some of the
00996    relocation special_function routines on targets which support a GP
00997    register.  */
00998 
00999 bfd_vma
01000 _bfd_get_gp_value (bfd *abfd)
01001 {
01002   if (! abfd)
01003     return 0;
01004   if (abfd->format != bfd_object)
01005     return 0;
01006 
01007   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
01008     return ecoff_data (abfd)->gp;
01009   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
01010     return elf_gp (abfd);
01011 
01012   return 0;
01013 }
01014 
01015 /* Set the GP value.  */
01016 
01017 void
01018 _bfd_set_gp_value (bfd *abfd, bfd_vma v)
01019 {
01020   if (! abfd)
01021     BFD_FAIL ();
01022   if (abfd->format != bfd_object)
01023     return;
01024 
01025   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
01026     ecoff_data (abfd)->gp = v;
01027   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
01028     elf_gp (abfd) = v;
01029 }
01030 
01031 /*
01032 FUNCTION
01033        bfd_scan_vma
01034 
01035 SYNOPSIS
01036        bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
01037 
01038 DESCRIPTION
01039        Convert, like <<strtoul>>, a numerical expression
01040        @var{string} into a <<bfd_vma>> integer, and return that integer.
01041        (Though without as many bells and whistles as <<strtoul>>.)
01042        The expression is assumed to be unsigned (i.e., positive).
01043        If given a @var{base}, it is used as the base for conversion.
01044        A base of 0 causes the function to interpret the string
01045        in hex if a leading "0x" or "0X" is found, otherwise
01046        in octal if a leading zero is found, otherwise in decimal.
01047 
01048        If the value would overflow, the maximum <<bfd_vma>> value is
01049        returned.
01050 */
01051 
01052 bfd_vma
01053 bfd_scan_vma (const char *string, const char **end, int base)
01054 {
01055   bfd_vma value;
01056   bfd_vma cutoff;
01057   unsigned int cutlim;
01058   int overflow;
01059 
01060   /* Let the host do it if possible.  */
01061   if (sizeof (bfd_vma) <= sizeof (unsigned long))
01062     return strtoul (string, (char **) end, base);
01063 
01064 #ifdef HAVE_STRTOULL
01065   if (sizeof (bfd_vma) <= sizeof (unsigned long long))
01066     return strtoull (string, (char **) end, base);
01067 #endif
01068 
01069   if (base == 0)
01070     {
01071       if (string[0] == '0')
01072        {
01073          if ((string[1] == 'x') || (string[1] == 'X'))
01074            base = 16;
01075          else
01076            base = 8;
01077        }
01078     }
01079 
01080   if ((base < 2) || (base > 36))
01081     base = 10;
01082 
01083   if (base == 16
01084       && string[0] == '0'
01085       && (string[1] == 'x' || string[1] == 'X')
01086       && ISXDIGIT (string[2]))
01087     {
01088       string += 2;
01089     }
01090 
01091   cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
01092   cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
01093   value = 0;
01094   overflow = 0;
01095   while (1)
01096     {
01097       unsigned int digit;
01098 
01099       digit = *string;
01100       if (ISDIGIT (digit))
01101        digit = digit - '0';
01102       else if (ISALPHA (digit))
01103        digit = TOUPPER (digit) - 'A' + 10;
01104       else
01105        break;
01106       if (digit >= (unsigned int) base)
01107        break;
01108       if (value > cutoff || (value == cutoff && digit > cutlim))
01109        overflow = 1;
01110       value = value * base + digit;
01111       ++string;
01112     }
01113 
01114   if (overflow)
01115     value = ~ (bfd_vma) 0;
01116 
01117   if (end != NULL)
01118     *end = string;
01119 
01120   return value;
01121 }
01122 
01123 /*
01124 FUNCTION
01125        bfd_copy_private_header_data
01126 
01127 SYNOPSIS
01128        bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
01129 
01130 DESCRIPTION
01131        Copy private BFD header information from the BFD @var{ibfd} to the
01132        the BFD @var{obfd}.  This copies information that may require
01133        sections to exist, but does not require symbol tables.  Return
01134        <<true>> on success, <<false>> on error.
01135        Possible error returns are:
01136 
01137        o <<bfd_error_no_memory>> -
01138        Not enough memory exists to create private data for @var{obfd}.
01139 
01140 .#define bfd_copy_private_header_data(ibfd, obfd) \
01141 .     BFD_SEND (obfd, _bfd_copy_private_header_data, \
01142 .             (ibfd, obfd))
01143 
01144 */
01145 
01146 /*
01147 FUNCTION
01148        bfd_copy_private_bfd_data
01149 
01150 SYNOPSIS
01151        bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
01152 
01153 DESCRIPTION
01154        Copy private BFD information from the BFD @var{ibfd} to the
01155        the BFD @var{obfd}.  Return <<TRUE>> on success, <<FALSE>> on error.
01156        Possible error returns are:
01157 
01158        o <<bfd_error_no_memory>> -
01159        Not enough memory exists to create private data for @var{obfd}.
01160 
01161 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
01162 .     BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
01163 .             (ibfd, obfd))
01164 
01165 */
01166 
01167 /*
01168 FUNCTION
01169        bfd_merge_private_bfd_data
01170 
01171 SYNOPSIS
01172        bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd);
01173 
01174 DESCRIPTION
01175        Merge private BFD information from the BFD @var{ibfd} to the
01176        the output file BFD @var{obfd} when linking.  Return <<TRUE>>
01177        on success, <<FALSE>> on error.  Possible error returns are:
01178 
01179        o <<bfd_error_no_memory>> -
01180        Not enough memory exists to create private data for @var{obfd}.
01181 
01182 .#define bfd_merge_private_bfd_data(ibfd, obfd) \
01183 .     BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
01184 .             (ibfd, obfd))
01185 
01186 */
01187 
01188 /*
01189 FUNCTION
01190        bfd_set_private_flags
01191 
01192 SYNOPSIS
01193        bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags);
01194 
01195 DESCRIPTION
01196        Set private BFD flag information in the BFD @var{abfd}.
01197        Return <<TRUE>> on success, <<FALSE>> on error.  Possible error
01198        returns are:
01199 
01200        o <<bfd_error_no_memory>> -
01201        Not enough memory exists to create private data for @var{obfd}.
01202 
01203 .#define bfd_set_private_flags(abfd, flags) \
01204 .     BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
01205 
01206 */
01207 
01208 /*
01209 FUNCTION
01210        Other functions
01211 
01212 DESCRIPTION
01213        The following functions exist but have not yet been documented.
01214 
01215 .#define bfd_sizeof_headers(abfd, info) \
01216 .       BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
01217 .
01218 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
01219 .       BFD_SEND (abfd, _bfd_find_nearest_line, \
01220 .                 (abfd, sec, syms, off, file, func, line))
01221 .
01222 .#define bfd_find_line(abfd, syms, sym, file, line) \
01223 .       BFD_SEND (abfd, _bfd_find_line, \
01224 .                 (abfd, syms, sym, file, line))
01225 .
01226 .#define bfd_find_inliner_info(abfd, file, func, line) \
01227 .       BFD_SEND (abfd, _bfd_find_inliner_info, \
01228 .                 (abfd, file, func, line))
01229 .
01230 .#define bfd_debug_info_start(abfd) \
01231 .       BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
01232 .
01233 .#define bfd_debug_info_end(abfd) \
01234 .       BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
01235 .
01236 .#define bfd_debug_info_accumulate(abfd, section) \
01237 .       BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
01238 .
01239 .#define bfd_stat_arch_elt(abfd, stat) \
01240 .       BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
01241 .
01242 .#define bfd_update_armap_timestamp(abfd) \
01243 .       BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
01244 .
01245 .#define bfd_set_arch_mach(abfd, arch, mach)\
01246 .       BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
01247 .
01248 .#define bfd_relax_section(abfd, section, link_info, again) \
01249 .       BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
01250 .
01251 .#define bfd_gc_sections(abfd, link_info) \
01252 .      BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
01253 .
01254 .#define bfd_merge_sections(abfd, link_info) \
01255 .      BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
01256 .
01257 .#define bfd_is_group_section(abfd, sec) \
01258 .      BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
01259 .
01260 .#define bfd_discard_group(abfd, sec) \
01261 .      BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
01262 .
01263 .#define bfd_link_hash_table_create(abfd) \
01264 .      BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
01265 .
01266 .#define bfd_link_hash_table_free(abfd, hash) \
01267 .      BFD_SEND (abfd, _bfd_link_hash_table_free, (hash))
01268 .
01269 .#define bfd_link_add_symbols(abfd, info) \
01270 .      BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
01271 .
01272 .#define bfd_link_just_syms(abfd, sec, info) \
01273 .      BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
01274 .
01275 .#define bfd_final_link(abfd, info) \
01276 .      BFD_SEND (abfd, _bfd_final_link, (abfd, info))
01277 .
01278 .#define bfd_free_cached_info(abfd) \
01279 .       BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
01280 .
01281 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
01282 .      BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
01283 .
01284 .#define bfd_print_private_bfd_data(abfd, file)\
01285 .      BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
01286 .
01287 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
01288 .      BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
01289 .
01290 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
01291 .      BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
01292 .                                             dyncount, dynsyms, ret))
01293 .
01294 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
01295 .      BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
01296 .
01297 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
01298 .      BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
01299 .
01300 .extern bfd_byte *bfd_get_relocated_section_contents
01301 .  (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
01302 .   bfd_boolean, asymbol **);
01303 .
01304 
01305 */
01306 
01307 bfd_byte *
01308 bfd_get_relocated_section_contents (bfd *abfd,
01309                                 struct bfd_link_info *link_info,
01310                                 struct bfd_link_order *link_order,
01311                                 bfd_byte *data,
01312                                 bfd_boolean relocatable,
01313                                 asymbol **symbols)
01314 {
01315   bfd *abfd2;
01316   bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
01317                  bfd_byte *, bfd_boolean, asymbol **);
01318 
01319   if (link_order->type == bfd_indirect_link_order)
01320     {
01321       abfd2 = link_order->u.indirect.section->owner;
01322       if (abfd2 == NULL)
01323        abfd2 = abfd;
01324     }
01325   else
01326     abfd2 = abfd;
01327 
01328   fn = abfd2->xvec->_bfd_get_relocated_section_contents;
01329 
01330   return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
01331 }
01332 
01333 /* Record information about an ELF program header.  */
01334 
01335 bfd_boolean
01336 bfd_record_phdr (bfd *abfd,
01337                unsigned long type,
01338                bfd_boolean flags_valid,
01339                flagword flags,
01340                bfd_boolean at_valid,
01341                bfd_vma at,
01342                bfd_boolean includes_filehdr,
01343                bfd_boolean includes_phdrs,
01344                unsigned int count,
01345                asection **secs)
01346 {
01347   struct elf_segment_map *m, **pm;
01348   bfd_size_type amt;
01349 
01350   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
01351     return TRUE;
01352 
01353   amt = sizeof (struct elf_segment_map);
01354   amt += ((bfd_size_type) count - 1) * sizeof (asection *);
01355   m = bfd_zalloc (abfd, amt);
01356   if (m == NULL)
01357     return FALSE;
01358 
01359   m->p_type = type;
01360   m->p_flags = flags;
01361   m->p_paddr = at;
01362   m->p_flags_valid = flags_valid;
01363   m->p_paddr_valid = at_valid;
01364   m->includes_filehdr = includes_filehdr;
01365   m->includes_phdrs = includes_phdrs;
01366   m->count = count;
01367   if (count > 0)
01368     memcpy (m->sections, secs, count * sizeof (asection *));
01369 
01370   for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
01371     ;
01372   *pm = m;
01373 
01374   return TRUE;
01375 }
01376 
01377 void
01378 bfd_sprintf_vma (bfd *abfd, char *buf, bfd_vma value)
01379 {
01380   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
01381     get_elf_backend_data (abfd)->elf_backend_sprintf_vma (abfd, buf, value);
01382   else
01383     sprintf_vma (buf, value);
01384 }
01385 
01386 void
01387 bfd_fprintf_vma (bfd *abfd, void *stream, bfd_vma value)
01388 {
01389   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
01390     get_elf_backend_data (abfd)->elf_backend_fprintf_vma (abfd, stream, value);
01391   else
01392     fprintf_vma ((FILE *) stream, value);
01393 }
01394 
01395 /*
01396 FUNCTION
01397        bfd_alt_mach_code
01398 
01399 SYNOPSIS
01400        bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative);
01401 
01402 DESCRIPTION
01403 
01404        When more than one machine code number is available for the
01405        same machine type, this function can be used to switch between
01406        the preferred one (alternative == 0) and any others.  Currently,
01407        only ELF supports this feature, with up to two alternate
01408        machine codes.
01409 */
01410 
01411 bfd_boolean
01412 bfd_alt_mach_code (bfd *abfd, int alternative)
01413 {
01414   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
01415     {
01416       int code;
01417 
01418       switch (alternative)
01419        {
01420        case 0:
01421          code = get_elf_backend_data (abfd)->elf_machine_code;
01422          break;
01423 
01424        case 1:
01425          code = get_elf_backend_data (abfd)->elf_machine_alt1;
01426          if (code == 0)
01427            return FALSE;
01428          break;
01429 
01430        case 2:
01431          code = get_elf_backend_data (abfd)->elf_machine_alt2;
01432          if (code == 0)
01433            return FALSE;
01434          break;
01435 
01436        default:
01437          return FALSE;
01438        }
01439 
01440       elf_elfheader (abfd)->e_machine = code;
01441 
01442       return TRUE;
01443     }
01444 
01445   return FALSE;
01446 }
01447 
01448 /*
01449 CODE_FRAGMENT
01450 
01451 .struct bfd_preserve
01452 .{
01453 .  void *marker;
01454 .  void *tdata;
01455 .  flagword flags;
01456 .  const struct bfd_arch_info *arch_info;
01457 .  struct bfd_section *sections;
01458 .  struct bfd_section *section_last;
01459 .  unsigned int section_count;
01460 .  struct bfd_hash_table section_htab;
01461 .};
01462 .
01463 */
01464 
01465 /*
01466 FUNCTION
01467        bfd_preserve_save
01468 
01469 SYNOPSIS
01470        bfd_boolean bfd_preserve_save (bfd *, struct bfd_preserve *);
01471 
01472 DESCRIPTION
01473        When testing an object for compatibility with a particular
01474        target back-end, the back-end object_p function needs to set
01475        up certain fields in the bfd on successfully recognizing the
01476        object.  This typically happens in a piecemeal fashion, with
01477        failures possible at many points.  On failure, the bfd is
01478        supposed to be restored to its initial state, which is
01479        virtually impossible.  However, restoring a subset of the bfd
01480        state works in practice.  This function stores the subset and
01481        reinitializes the bfd.
01482 
01483 */
01484 
01485 bfd_boolean
01486 bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
01487 {
01488   preserve->tdata = abfd->tdata.any;
01489   preserve->arch_info = abfd->arch_info;
01490   preserve->flags = abfd->flags;
01491   preserve->sections = abfd->sections;
01492   preserve->section_last = abfd->section_last;
01493   preserve->section_count = abfd->section_count;
01494   preserve->section_htab = abfd->section_htab;
01495 
01496   if (! bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
01497                           sizeof (struct section_hash_entry)))
01498     return FALSE;
01499 
01500   abfd->tdata.any = NULL;
01501   abfd->arch_info = &bfd_default_arch_struct;
01502   abfd->flags &= BFD_IN_MEMORY;
01503   abfd->sections = NULL;
01504   abfd->section_last = NULL;
01505   abfd->section_count = 0;
01506 
01507   return TRUE;
01508 }
01509 
01510 /*
01511 FUNCTION
01512        bfd_preserve_restore
01513 
01514 SYNOPSIS
01515        void bfd_preserve_restore (bfd *, struct bfd_preserve *);
01516 
01517 DESCRIPTION
01518        This function restores bfd state saved by bfd_preserve_save.
01519        If MARKER is non-NULL in struct bfd_preserve then that block
01520        and all subsequently bfd_alloc'd memory is freed.
01521 
01522 */
01523 
01524 void
01525 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
01526 {
01527   bfd_hash_table_free (&abfd->section_htab);
01528 
01529   abfd->tdata.any = preserve->tdata;
01530   abfd->arch_info = preserve->arch_info;
01531   abfd->flags = preserve->flags;
01532   abfd->section_htab = preserve->section_htab;
01533   abfd->sections = preserve->sections;
01534   abfd->section_last = preserve->section_last;
01535   abfd->section_count = preserve->section_count;
01536 
01537   /* bfd_release frees all memory more recently bfd_alloc'd than
01538      its arg, as well as its arg.  */
01539   if (preserve->marker != NULL)
01540     {
01541       bfd_release (abfd, preserve->marker);
01542       preserve->marker = NULL;
01543     }
01544 }
01545 
01546 /*
01547 FUNCTION
01548        bfd_preserve_finish
01549 
01550 SYNOPSIS
01551        void bfd_preserve_finish (bfd *, struct bfd_preserve *);
01552 
01553 DESCRIPTION
01554        This function should be called when the bfd state saved by
01555        bfd_preserve_save is no longer needed.  ie. when the back-end
01556        object_p function returns with success.
01557 
01558 */
01559 
01560 void
01561 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
01562 {
01563   /* It would be nice to be able to free more memory here, eg. old
01564      tdata, but that's not possible since these blocks are sitting
01565      inside bfd_alloc'd memory.  The section hash is on a separate
01566      objalloc.  */
01567   bfd_hash_table_free (&preserve->section_htab);
01568 }
01569 
01570 /*
01571 FUNCTION
01572        bfd_emul_get_maxpagesize
01573 
01574 SYNOPSIS
01575        bfd_vma bfd_emul_get_maxpagesize (const char *);
01576 
01577 DESCRIPTION
01578        Returns the maximum page size, in bytes, as determined by
01579        emulation.
01580 
01581 RETURNS
01582        Returns the maximum page size in bytes for ELF, abort
01583        otherwise.
01584 */
01585 
01586 bfd_vma
01587 bfd_emul_get_maxpagesize (const char *emul)
01588 {
01589   const bfd_target *target;
01590 
01591   target = bfd_find_target (emul, NULL);
01592   if (target != NULL
01593       && target->flavour == bfd_target_elf_flavour)
01594     return xvec_get_elf_backend_data (target)->maxpagesize;
01595 
01596   abort ();
01597   return 0;
01598 }
01599 
01600 static void
01601 bfd_elf_set_pagesize (const bfd_target *target, bfd_vma size,
01602                     int offset, const bfd_target *orig_target)
01603 {
01604   if (target->flavour == bfd_target_elf_flavour)
01605     {
01606       const struct elf_backend_data *bed;
01607 
01608       bed = xvec_get_elf_backend_data (target);
01609       *((bfd_vma *) ((char *) bed + offset)) = size;
01610     }
01611 
01612   if (target->alternative_target
01613       && target->alternative_target != orig_target)
01614     bfd_elf_set_pagesize (target->alternative_target, size, offset,
01615                        orig_target);
01616 }
01617 
01618 /*
01619 FUNCTION
01620        bfd_emul_set_maxpagesize
01621 
01622 SYNOPSIS
01623        void bfd_emul_set_maxpagesize (const char *, bfd_vma);
01624 
01625 DESCRIPTION
01626        For ELF, set the maximum page size for the emulation.  It is
01627        a no-op for other formats.
01628 
01629 */
01630 
01631 void
01632 bfd_emul_set_maxpagesize (const char *emul, bfd_vma size)
01633 {
01634   const bfd_target *target;
01635 
01636   target = bfd_find_target (emul, NULL);
01637   if (target)
01638     bfd_elf_set_pagesize (target, size,
01639                        offsetof (struct elf_backend_data,
01640                                 maxpagesize), target);
01641 }
01642 
01643 /*
01644 FUNCTION
01645        bfd_emul_get_commonpagesize
01646 
01647 SYNOPSIS
01648        bfd_vma bfd_emul_get_commonpagesize (const char *);
01649 
01650 DESCRIPTION
01651        Returns the common page size, in bytes, as determined by
01652        emulation.
01653 
01654 RETURNS
01655        Returns the common page size in bytes for ELF, abort otherwise.
01656 */
01657 
01658 bfd_vma
01659 bfd_emul_get_commonpagesize (const char *emul)
01660 {
01661   const bfd_target *target;
01662 
01663   target = bfd_find_target (emul, NULL);
01664   if (target != NULL
01665       && target->flavour == bfd_target_elf_flavour)
01666     return xvec_get_elf_backend_data (target)->commonpagesize;
01667 
01668   abort ();
01669   return 0;
01670 }
01671 
01672 /*
01673 FUNCTION
01674        bfd_emul_set_commonpagesize
01675 
01676 SYNOPSIS
01677        void bfd_emul_set_commonpagesize (const char *, bfd_vma);
01678 
01679 DESCRIPTION
01680        For ELF, set the common page size for the emulation.  It is
01681        a no-op for other formats.
01682 
01683 */
01684 
01685 void
01686 bfd_emul_set_commonpagesize (const char *emul, bfd_vma size)
01687 {
01688   const bfd_target *target;
01689 
01690   target = bfd_find_target (emul, NULL);
01691   if (target)
01692     bfd_elf_set_pagesize (target, size,
01693                        offsetof (struct elf_backend_data,
01694                                 commonpagesize), target);
01695 }