Home | History | Annotate | Line # | Download | only in bfd
libbfd.c revision 1.1
      1  1.1  christos /* Assorted BFD support routines, only used internally.
      2  1.1  christos    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
      3  1.1  christos    2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
      4  1.1  christos    Free Software Foundation, Inc.
      5  1.1  christos    Written by Cygnus Support.
      6  1.1  christos 
      7  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      8  1.1  christos 
      9  1.1  christos    This program is free software; you can redistribute it and/or modify
     10  1.1  christos    it under the terms of the GNU General Public License as published by
     11  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     12  1.1  christos    (at your option) any later version.
     13  1.1  christos 
     14  1.1  christos    This program is distributed in the hope that it will be useful,
     15  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  1.1  christos    GNU General Public License for more details.
     18  1.1  christos 
     19  1.1  christos    You should have received a copy of the GNU General Public License
     20  1.1  christos    along with this program; if not, write to the Free Software
     21  1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     22  1.1  christos    MA 02110-1301, USA.  */
     23  1.1  christos 
     24  1.1  christos #include "sysdep.h"
     25  1.1  christos #include "bfd.h"
     26  1.1  christos #include "libbfd.h"
     27  1.1  christos 
     28  1.1  christos #ifndef HAVE_GETPAGESIZE
     29  1.1  christos #define getpagesize() 2048
     30  1.1  christos #endif
     31  1.1  christos 
     32  1.1  christos /*
     33  1.1  christos SECTION
     34  1.1  christos 	Implementation details
     35  1.1  christos 
     36  1.1  christos SUBSECTION
     37  1.1  christos 	Internal functions
     38  1.1  christos 
     39  1.1  christos DESCRIPTION
     40  1.1  christos 	These routines are used within BFD.
     41  1.1  christos 	They are not intended for export, but are documented here for
     42  1.1  christos 	completeness.
     43  1.1  christos */
     44  1.1  christos 
     45  1.1  christos /* A routine which is used in target vectors for unsupported
     46  1.1  christos    operations.  */
     47  1.1  christos 
     48  1.1  christos bfd_boolean
     49  1.1  christos bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
     50  1.1  christos {
     51  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
     52  1.1  christos   return FALSE;
     53  1.1  christos }
     54  1.1  christos 
     55  1.1  christos /* A routine which is used in target vectors for supported operations
     56  1.1  christos    which do not actually do anything.  */
     57  1.1  christos 
     58  1.1  christos bfd_boolean
     59  1.1  christos bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
     60  1.1  christos {
     61  1.1  christos   return TRUE;
     62  1.1  christos }
     63  1.1  christos 
     64  1.1  christos /* A routine which is used in target vectors for unsupported
     65  1.1  christos    operations which return a pointer value.  */
     66  1.1  christos 
     67  1.1  christos void *
     68  1.1  christos bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
     69  1.1  christos {
     70  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
     71  1.1  christos   return NULL;
     72  1.1  christos }
     73  1.1  christos 
     74  1.1  christos int
     75  1.1  christos bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
     76  1.1  christos {
     77  1.1  christos   return 0;
     78  1.1  christos }
     79  1.1  christos 
     80  1.1  christos unsigned int
     81  1.1  christos bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
     82  1.1  christos {
     83  1.1  christos    return 0;
     84  1.1  christos }
     85  1.1  christos 
     86  1.1  christos long
     87  1.1  christos bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
     88  1.1  christos {
     89  1.1  christos   return 0;
     90  1.1  christos }
     91  1.1  christos 
     92  1.1  christos /* A routine which is used in target vectors for unsupported
     93  1.1  christos    operations which return -1 on error.  */
     94  1.1  christos 
     95  1.1  christos long
     96  1.1  christos _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
     97  1.1  christos {
     98  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
     99  1.1  christos   return -1;
    100  1.1  christos }
    101  1.1  christos 
    102  1.1  christos void
    103  1.1  christos bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
    104  1.1  christos {
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos long
    108  1.1  christos _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
    109  1.1  christos 				     asection *sec ATTRIBUTE_UNUSED)
    110  1.1  christos {
    111  1.1  christos   return sizeof (arelent *);
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos long
    115  1.1  christos _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
    116  1.1  christos 				  asection *sec ATTRIBUTE_UNUSED,
    117  1.1  christos 				  arelent **relptr,
    118  1.1  christos 				  asymbol **symbols ATTRIBUTE_UNUSED)
    119  1.1  christos {
    120  1.1  christos   *relptr = NULL;
    121  1.1  christos   return 0;
    122  1.1  christos }
    123  1.1  christos 
    124  1.1  christos bfd_boolean
    125  1.1  christos _bfd_nocore_core_file_matches_executable_p
    126  1.1  christos   (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
    127  1.1  christos    bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
    128  1.1  christos {
    129  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
    130  1.1  christos   return FALSE;
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos /* Routine to handle core_file_failing_command entry point for targets
    134  1.1  christos    without core file support.  */
    135  1.1  christos 
    136  1.1  christos char *
    137  1.1  christos _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    138  1.1  christos {
    139  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
    140  1.1  christos   return NULL;
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos /* Routine to handle core_file_failing_signal entry point for targets
    144  1.1  christos    without core file support.  */
    145  1.1  christos 
    146  1.1  christos int
    147  1.1  christos _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    148  1.1  christos {
    149  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
    150  1.1  christos   return 0;
    151  1.1  christos }
    152  1.1  christos 
    153  1.1  christos /* Routine to handle the core_file_pid entry point for targets without
    154  1.1  christos    core file support.  */
    155  1.1  christos 
    156  1.1  christos int
    157  1.1  christos _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    158  1.1  christos {
    159  1.1  christos   bfd_set_error (bfd_error_invalid_operation);
    160  1.1  christos   return 0;
    161  1.1  christos }
    162  1.1  christos 
    163  1.1  christos const bfd_target *
    164  1.1  christos _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    165  1.1  christos {
    166  1.1  christos   bfd_set_error (bfd_error_wrong_format);
    167  1.1  christos   return 0;
    168  1.1  christos }
    169  1.1  christos 
    170  1.1  christos /* Allocate memory using malloc.  */
    172  1.1  christos 
    173  1.1  christos void *
    174  1.1  christos bfd_malloc (bfd_size_type size)
    175  1.1  christos {
    176  1.1  christos   void *ptr;
    177  1.1  christos 
    178  1.1  christos   if (size != (size_t) size)
    179  1.1  christos     {
    180  1.1  christos       bfd_set_error (bfd_error_no_memory);
    181  1.1  christos       return NULL;
    182  1.1  christos     }
    183  1.1  christos 
    184  1.1  christos   ptr = malloc ((size_t) size);
    185  1.1  christos   if (ptr == NULL && (size_t) size != 0)
    186  1.1  christos     bfd_set_error (bfd_error_no_memory);
    187  1.1  christos 
    188  1.1  christos   return ptr;
    189  1.1  christos }
    190  1.1  christos 
    191  1.1  christos /* Allocate memory using malloc, nmemb * size with overflow checking.  */
    192  1.1  christos 
    193  1.1  christos void *
    194  1.1  christos bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
    195  1.1  christos {
    196  1.1  christos   void *ptr;
    197  1.1  christos 
    198  1.1  christos   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
    199  1.1  christos       && size != 0
    200  1.1  christos       && nmemb > ~(bfd_size_type) 0 / size)
    201  1.1  christos     {
    202  1.1  christos       bfd_set_error (bfd_error_no_memory);
    203  1.1  christos       return NULL;
    204  1.1  christos     }
    205  1.1  christos 
    206  1.1  christos   size *= nmemb;
    207  1.1  christos 
    208  1.1  christos   if (size != (size_t) size)
    209  1.1  christos     {
    210  1.1  christos       bfd_set_error (bfd_error_no_memory);
    211  1.1  christos       return NULL;
    212  1.1  christos     }
    213  1.1  christos 
    214  1.1  christos   ptr = malloc ((size_t) size);
    215  1.1  christos   if (ptr == NULL && (size_t) size != 0)
    216  1.1  christos     bfd_set_error (bfd_error_no_memory);
    217  1.1  christos 
    218  1.1  christos   return ptr;
    219  1.1  christos }
    220  1.1  christos 
    221  1.1  christos /* Reallocate memory using realloc.  */
    222  1.1  christos 
    223  1.1  christos void *
    224  1.1  christos bfd_realloc (void *ptr, bfd_size_type size)
    225  1.1  christos {
    226  1.1  christos   void *ret;
    227  1.1  christos 
    228  1.1  christos   if (size != (size_t) size)
    229  1.1  christos     {
    230  1.1  christos       bfd_set_error (bfd_error_no_memory);
    231  1.1  christos       return NULL;
    232  1.1  christos     }
    233  1.1  christos 
    234  1.1  christos   if (ptr == NULL)
    235  1.1  christos     ret = malloc ((size_t) size);
    236  1.1  christos   else
    237  1.1  christos     ret = realloc (ptr, (size_t) size);
    238  1.1  christos 
    239  1.1  christos   if (ret == NULL && (size_t) size != 0)
    240  1.1  christos     bfd_set_error (bfd_error_no_memory);
    241  1.1  christos 
    242  1.1  christos   return ret;
    243  1.1  christos }
    244  1.1  christos 
    245  1.1  christos /* Reallocate memory using realloc, nmemb * size with overflow checking.  */
    246  1.1  christos 
    247  1.1  christos void *
    248  1.1  christos bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
    249  1.1  christos {
    250  1.1  christos   void *ret;
    251  1.1  christos 
    252  1.1  christos   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
    253  1.1  christos       && size != 0
    254  1.1  christos       && nmemb > ~(bfd_size_type) 0 / size)
    255  1.1  christos     {
    256  1.1  christos       bfd_set_error (bfd_error_no_memory);
    257  1.1  christos       return NULL;
    258  1.1  christos     }
    259  1.1  christos 
    260  1.1  christos   size *= nmemb;
    261  1.1  christos 
    262  1.1  christos   if (size != (size_t) size)
    263  1.1  christos     {
    264  1.1  christos       bfd_set_error (bfd_error_no_memory);
    265  1.1  christos       return NULL;
    266  1.1  christos     }
    267  1.1  christos 
    268  1.1  christos   if (ptr == NULL)
    269  1.1  christos     ret = malloc ((size_t) size);
    270  1.1  christos   else
    271  1.1  christos     ret = realloc (ptr, (size_t) size);
    272  1.1  christos 
    273  1.1  christos   if (ret == NULL && (size_t) size != 0)
    274  1.1  christos     bfd_set_error (bfd_error_no_memory);
    275  1.1  christos 
    276  1.1  christos   return ret;
    277  1.1  christos }
    278  1.1  christos 
    279  1.1  christos /* Reallocate memory using realloc.
    280  1.1  christos    If this fails the pointer is freed before returning.  */
    281  1.1  christos 
    282  1.1  christos void *
    283  1.1  christos bfd_realloc_or_free (void *ptr, bfd_size_type size)
    284  1.1  christos {
    285  1.1  christos   size_t amount = (size_t) size;
    286  1.1  christos   void *ret;
    287  1.1  christos 
    288  1.1  christos   if (size != amount)
    289  1.1  christos     ret = NULL;
    290  1.1  christos   else if (ptr == NULL)
    291  1.1  christos     ret = malloc (amount);
    292  1.1  christos   else
    293  1.1  christos     ret = realloc (ptr, amount);
    294  1.1  christos 
    295  1.1  christos   if (ret == NULL)
    296  1.1  christos     {
    297  1.1  christos       if (amount > 0)
    298  1.1  christos 	bfd_set_error (bfd_error_no_memory);
    299  1.1  christos 
    300  1.1  christos       if (ptr != NULL)
    301  1.1  christos 	free (ptr);
    302  1.1  christos     }
    303  1.1  christos 
    304  1.1  christos   return ret;
    305  1.1  christos }
    306  1.1  christos 
    307  1.1  christos /* Allocate memory using malloc and clear it.  */
    308  1.1  christos 
    309  1.1  christos void *
    310  1.1  christos bfd_zmalloc (bfd_size_type size)
    311  1.1  christos {
    312  1.1  christos   void *ptr;
    313  1.1  christos 
    314  1.1  christos   if (size != (size_t) size)
    315  1.1  christos     {
    316  1.1  christos       bfd_set_error (bfd_error_no_memory);
    317  1.1  christos       return NULL;
    318  1.1  christos     }
    319  1.1  christos 
    320  1.1  christos   ptr = malloc ((size_t) size);
    321  1.1  christos 
    322  1.1  christos   if ((size_t) size != 0)
    323  1.1  christos     {
    324  1.1  christos       if (ptr == NULL)
    325  1.1  christos 	bfd_set_error (bfd_error_no_memory);
    326  1.1  christos       else
    327  1.1  christos 	memset (ptr, 0, (size_t) size);
    328  1.1  christos     }
    329  1.1  christos 
    330  1.1  christos   return ptr;
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos /* Allocate memory using malloc (nmemb * size) with overflow checking
    334  1.1  christos    and clear it.  */
    335  1.1  christos 
    336  1.1  christos void *
    337  1.1  christos bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
    338  1.1  christos {
    339  1.1  christos   void *ptr;
    340  1.1  christos 
    341  1.1  christos   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
    342  1.1  christos       && size != 0
    343  1.1  christos       && nmemb > ~(bfd_size_type) 0 / size)
    344  1.1  christos     {
    345  1.1  christos       bfd_set_error (bfd_error_no_memory);
    346  1.1  christos       return NULL;
    347  1.1  christos     }
    348  1.1  christos 
    349  1.1  christos   size *= nmemb;
    350  1.1  christos 
    351  1.1  christos   if (size != (size_t) size)
    352  1.1  christos     {
    353  1.1  christos       bfd_set_error (bfd_error_no_memory);
    354  1.1  christos       return NULL;
    355  1.1  christos     }
    356  1.1  christos 
    357  1.1  christos   ptr = malloc ((size_t) size);
    358  1.1  christos 
    359  1.1  christos   if ((size_t) size != 0)
    360  1.1  christos     {
    361  1.1  christos       if (ptr == NULL)
    362  1.1  christos 	bfd_set_error (bfd_error_no_memory);
    363  1.1  christos       else
    364  1.1  christos 	memset (ptr, 0, (size_t) size);
    365  1.1  christos     }
    366  1.1  christos 
    367  1.1  christos   return ptr;
    368  1.1  christos }
    369  1.1  christos 
    370  1.1  christos /*
    371  1.1  christos INTERNAL_FUNCTION
    372  1.1  christos 	bfd_write_bigendian_4byte_int
    373  1.1  christos 
    374  1.1  christos SYNOPSIS
    375  1.1  christos 	bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
    376  1.1  christos 
    377  1.1  christos DESCRIPTION
    378  1.1  christos 	Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
    379  1.1  christos 	endian order regardless of what else is going on.  This is useful in
    380  1.1  christos 	archives.
    381  1.1  christos 
    382  1.1  christos */
    383  1.1  christos bfd_boolean
    384  1.1  christos bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
    385  1.1  christos {
    386  1.1  christos   bfd_byte buffer[4];
    387  1.1  christos   bfd_putb32 ((bfd_vma) i, buffer);
    388  1.1  christos   return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
    389  1.1  christos }
    390  1.1  christos 
    391  1.1  christos 
    392  1.1  christos /** The do-it-yourself (byte) sex-change kit */
    394  1.1  christos 
    395  1.1  christos /* The middle letter e.g. get<b>short indicates Big or Little endian
    396  1.1  christos    target machine.  It doesn't matter what the byte order of the host
    397  1.1  christos    machine is; these routines work for either.  */
    398  1.1  christos 
    399  1.1  christos /* FIXME: Should these take a count argument?
    400  1.1  christos    Answer (gnu (at) cygnus.com):  No, but perhaps they should be inline
    401  1.1  christos                              functions in swap.h #ifdef __GNUC__.
    402  1.1  christos                              Gprof them later and find out.  */
    403  1.1  christos 
    404  1.1  christos /*
    405  1.1  christos FUNCTION
    406  1.1  christos 	bfd_put_size
    407  1.1  christos FUNCTION
    408  1.1  christos 	bfd_get_size
    409  1.1  christos 
    410  1.1  christos DESCRIPTION
    411  1.1  christos 	These macros as used for reading and writing raw data in
    412  1.1  christos 	sections; each access (except for bytes) is vectored through
    413  1.1  christos 	the target format of the BFD and mangled accordingly. The
    414  1.1  christos 	mangling performs any necessary endian translations and
    415  1.1  christos 	removes alignment restrictions.  Note that types accepted and
    416  1.1  christos 	returned by these macros are identical so they can be swapped
    417  1.1  christos 	around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
    418  1.1  christos 	to either <<bfd_get_32>> or <<bfd_get_64>>.
    419  1.1  christos 
    420  1.1  christos 	In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
    421  1.1  christos 	system without prototypes, the caller is responsible for making
    422  1.1  christos 	sure that is true, with a cast if necessary.  We don't cast
    423  1.1  christos 	them in the macro definitions because that would prevent <<lint>>
    424  1.1  christos 	or <<gcc -Wall>> from detecting sins such as passing a pointer.
    425  1.1  christos 	To detect calling these with less than a <<bfd_vma>>, use
    426  1.1  christos 	<<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
    427  1.1  christos 
    428  1.1  christos .
    429  1.1  christos .{* Byte swapping macros for user section data.  *}
    430  1.1  christos .
    431  1.1  christos .#define bfd_put_8(abfd, val, ptr) \
    432  1.1  christos .  ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
    433  1.1  christos .#define bfd_put_signed_8 \
    434  1.1  christos .  bfd_put_8
    435  1.1  christos .#define bfd_get_8(abfd, ptr) \
    436  1.1  christos .  (*(unsigned char *) (ptr) & 0xff)
    437  1.1  christos .#define bfd_get_signed_8(abfd, ptr) \
    438  1.1  christos .  (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
    439  1.1  christos .
    440  1.1  christos .#define bfd_put_16(abfd, val, ptr) \
    441  1.1  christos .  BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
    442  1.1  christos .#define bfd_put_signed_16 \
    443  1.1  christos .  bfd_put_16
    444  1.1  christos .#define bfd_get_16(abfd, ptr) \
    445  1.1  christos .  BFD_SEND (abfd, bfd_getx16, (ptr))
    446  1.1  christos .#define bfd_get_signed_16(abfd, ptr) \
    447  1.1  christos .  BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
    448  1.1  christos .
    449  1.1  christos .#define bfd_put_32(abfd, val, ptr) \
    450  1.1  christos .  BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
    451  1.1  christos .#define bfd_put_signed_32 \
    452  1.1  christos .  bfd_put_32
    453  1.1  christos .#define bfd_get_32(abfd, ptr) \
    454  1.1  christos .  BFD_SEND (abfd, bfd_getx32, (ptr))
    455  1.1  christos .#define bfd_get_signed_32(abfd, ptr) \
    456  1.1  christos .  BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
    457  1.1  christos .
    458  1.1  christos .#define bfd_put_64(abfd, val, ptr) \
    459  1.1  christos .  BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
    460  1.1  christos .#define bfd_put_signed_64 \
    461  1.1  christos .  bfd_put_64
    462  1.1  christos .#define bfd_get_64(abfd, ptr) \
    463  1.1  christos .  BFD_SEND (abfd, bfd_getx64, (ptr))
    464  1.1  christos .#define bfd_get_signed_64(abfd, ptr) \
    465  1.1  christos .  BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
    466  1.1  christos .
    467  1.1  christos .#define bfd_get(bits, abfd, ptr)			\
    468  1.1  christos .  ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr)	\
    469  1.1  christos .   : (bits) == 16 ? bfd_get_16 (abfd, ptr)		\
    470  1.1  christos .   : (bits) == 32 ? bfd_get_32 (abfd, ptr)		\
    471  1.1  christos .   : (bits) == 64 ? bfd_get_64 (abfd, ptr)		\
    472  1.1  christos .   : (abort (), (bfd_vma) - 1))
    473  1.1  christos .
    474  1.1  christos .#define bfd_put(bits, abfd, val, ptr)			\
    475  1.1  christos .  ((bits) == 8 ? bfd_put_8  (abfd, val, ptr)		\
    476  1.1  christos .   : (bits) == 16 ? bfd_put_16 (abfd, val, ptr)		\
    477  1.1  christos .   : (bits) == 32 ? bfd_put_32 (abfd, val, ptr)		\
    478  1.1  christos .   : (bits) == 64 ? bfd_put_64 (abfd, val, ptr)		\
    479  1.1  christos .   : (abort (), (void) 0))
    480  1.1  christos .
    481  1.1  christos */
    482  1.1  christos 
    483  1.1  christos /*
    484  1.1  christos FUNCTION
    485  1.1  christos 	bfd_h_put_size
    486  1.1  christos 	bfd_h_get_size
    487  1.1  christos 
    488  1.1  christos DESCRIPTION
    489  1.1  christos 	These macros have the same function as their <<bfd_get_x>>
    490  1.1  christos 	brethren, except that they are used for removing information
    491  1.1  christos 	for the header records of object files. Believe it or not,
    492  1.1  christos 	some object files keep their header records in big endian
    493  1.1  christos 	order and their data in little endian order.
    494  1.1  christos .
    495  1.1  christos .{* Byte swapping macros for file header data.  *}
    496  1.1  christos .
    497  1.1  christos .#define bfd_h_put_8(abfd, val, ptr) \
    498  1.1  christos .  bfd_put_8 (abfd, val, ptr)
    499  1.1  christos .#define bfd_h_put_signed_8(abfd, val, ptr) \
    500  1.1  christos .  bfd_put_8 (abfd, val, ptr)
    501  1.1  christos .#define bfd_h_get_8(abfd, ptr) \
    502  1.1  christos .  bfd_get_8 (abfd, ptr)
    503  1.1  christos .#define bfd_h_get_signed_8(abfd, ptr) \
    504  1.1  christos .  bfd_get_signed_8 (abfd, ptr)
    505  1.1  christos .
    506  1.1  christos .#define bfd_h_put_16(abfd, val, ptr) \
    507  1.1  christos .  BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
    508  1.1  christos .#define bfd_h_put_signed_16 \
    509  1.1  christos .  bfd_h_put_16
    510  1.1  christos .#define bfd_h_get_16(abfd, ptr) \
    511  1.1  christos .  BFD_SEND (abfd, bfd_h_getx16, (ptr))
    512  1.1  christos .#define bfd_h_get_signed_16(abfd, ptr) \
    513  1.1  christos .  BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
    514  1.1  christos .
    515  1.1  christos .#define bfd_h_put_32(abfd, val, ptr) \
    516  1.1  christos .  BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
    517  1.1  christos .#define bfd_h_put_signed_32 \
    518  1.1  christos .  bfd_h_put_32
    519  1.1  christos .#define bfd_h_get_32(abfd, ptr) \
    520  1.1  christos .  BFD_SEND (abfd, bfd_h_getx32, (ptr))
    521  1.1  christos .#define bfd_h_get_signed_32(abfd, ptr) \
    522  1.1  christos .  BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
    523  1.1  christos .
    524  1.1  christos .#define bfd_h_put_64(abfd, val, ptr) \
    525  1.1  christos .  BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
    526  1.1  christos .#define bfd_h_put_signed_64 \
    527  1.1  christos .  bfd_h_put_64
    528  1.1  christos .#define bfd_h_get_64(abfd, ptr) \
    529  1.1  christos .  BFD_SEND (abfd, bfd_h_getx64, (ptr))
    530  1.1  christos .#define bfd_h_get_signed_64(abfd, ptr) \
    531  1.1  christos .  BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
    532  1.1  christos .
    533  1.1  christos .{* Aliases for the above, which should eventually go away.  *}
    534  1.1  christos .
    535  1.1  christos .#define H_PUT_64  bfd_h_put_64
    536  1.1  christos .#define H_PUT_32  bfd_h_put_32
    537  1.1  christos .#define H_PUT_16  bfd_h_put_16
    538  1.1  christos .#define H_PUT_8   bfd_h_put_8
    539  1.1  christos .#define H_PUT_S64 bfd_h_put_signed_64
    540  1.1  christos .#define H_PUT_S32 bfd_h_put_signed_32
    541  1.1  christos .#define H_PUT_S16 bfd_h_put_signed_16
    542  1.1  christos .#define H_PUT_S8  bfd_h_put_signed_8
    543  1.1  christos .#define H_GET_64  bfd_h_get_64
    544  1.1  christos .#define H_GET_32  bfd_h_get_32
    545  1.1  christos .#define H_GET_16  bfd_h_get_16
    546  1.1  christos .#define H_GET_8   bfd_h_get_8
    547  1.1  christos .#define H_GET_S64 bfd_h_get_signed_64
    548  1.1  christos .#define H_GET_S32 bfd_h_get_signed_32
    549  1.1  christos .#define H_GET_S16 bfd_h_get_signed_16
    550  1.1  christos .#define H_GET_S8  bfd_h_get_signed_8
    551  1.1  christos .
    552  1.1  christos .*/
    553  1.1  christos 
    554  1.1  christos /* Sign extension to bfd_signed_vma.  */
    555  1.1  christos #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
    556  1.1  christos #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
    557  1.1  christos #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
    558  1.1  christos #define COERCE64(x) \
    559  1.1  christos   (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
    560  1.1  christos 
    561  1.1  christos bfd_vma
    562  1.1  christos bfd_getb16 (const void *p)
    563  1.1  christos {
    564  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    565  1.1  christos   return (addr[0] << 8) | addr[1];
    566  1.1  christos }
    567  1.1  christos 
    568  1.1  christos bfd_vma
    569  1.1  christos bfd_getl16 (const void *p)
    570  1.1  christos {
    571  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    572  1.1  christos   return (addr[1] << 8) | addr[0];
    573  1.1  christos }
    574  1.1  christos 
    575  1.1  christos bfd_signed_vma
    576  1.1  christos bfd_getb_signed_16 (const void *p)
    577  1.1  christos {
    578  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    579  1.1  christos   return COERCE16 ((addr[0] << 8) | addr[1]);
    580  1.1  christos }
    581  1.1  christos 
    582  1.1  christos bfd_signed_vma
    583  1.1  christos bfd_getl_signed_16 (const void *p)
    584  1.1  christos {
    585  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    586  1.1  christos   return COERCE16 ((addr[1] << 8) | addr[0]);
    587  1.1  christos }
    588  1.1  christos 
    589  1.1  christos void
    590  1.1  christos bfd_putb16 (bfd_vma data, void *p)
    591  1.1  christos {
    592  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    593  1.1  christos   addr[0] = (data >> 8) & 0xff;
    594  1.1  christos   addr[1] = data & 0xff;
    595  1.1  christos }
    596  1.1  christos 
    597  1.1  christos void
    598  1.1  christos bfd_putl16 (bfd_vma data, void *p)
    599  1.1  christos {
    600  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    601  1.1  christos   addr[0] = data & 0xff;
    602  1.1  christos   addr[1] = (data >> 8) & 0xff;
    603  1.1  christos }
    604  1.1  christos 
    605  1.1  christos bfd_vma
    606  1.1  christos bfd_getb32 (const void *p)
    607  1.1  christos {
    608  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    609  1.1  christos   unsigned long v;
    610  1.1  christos 
    611  1.1  christos   v = (unsigned long) addr[0] << 24;
    612  1.1  christos   v |= (unsigned long) addr[1] << 16;
    613  1.1  christos   v |= (unsigned long) addr[2] << 8;
    614  1.1  christos   v |= (unsigned long) addr[3];
    615  1.1  christos   return v;
    616  1.1  christos }
    617  1.1  christos 
    618  1.1  christos bfd_vma
    619  1.1  christos bfd_getl32 (const void *p)
    620  1.1  christos {
    621  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    622  1.1  christos   unsigned long v;
    623  1.1  christos 
    624  1.1  christos   v = (unsigned long) addr[0];
    625  1.1  christos   v |= (unsigned long) addr[1] << 8;
    626  1.1  christos   v |= (unsigned long) addr[2] << 16;
    627  1.1  christos   v |= (unsigned long) addr[3] << 24;
    628  1.1  christos   return v;
    629  1.1  christos }
    630  1.1  christos 
    631  1.1  christos bfd_signed_vma
    632  1.1  christos bfd_getb_signed_32 (const void *p)
    633  1.1  christos {
    634  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    635  1.1  christos   unsigned long v;
    636  1.1  christos 
    637  1.1  christos   v = (unsigned long) addr[0] << 24;
    638  1.1  christos   v |= (unsigned long) addr[1] << 16;
    639  1.1  christos   v |= (unsigned long) addr[2] << 8;
    640  1.1  christos   v |= (unsigned long) addr[3];
    641  1.1  christos   return COERCE32 (v);
    642  1.1  christos }
    643  1.1  christos 
    644  1.1  christos bfd_signed_vma
    645  1.1  christos bfd_getl_signed_32 (const void *p)
    646  1.1  christos {
    647  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    648  1.1  christos   unsigned long v;
    649  1.1  christos 
    650  1.1  christos   v = (unsigned long) addr[0];
    651  1.1  christos   v |= (unsigned long) addr[1] << 8;
    652  1.1  christos   v |= (unsigned long) addr[2] << 16;
    653  1.1  christos   v |= (unsigned long) addr[3] << 24;
    654  1.1  christos   return COERCE32 (v);
    655  1.1  christos }
    656  1.1  christos 
    657  1.1  christos bfd_uint64_t
    658  1.1  christos bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
    659  1.1  christos {
    660  1.1  christos #ifdef BFD_HOST_64_BIT
    661  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    662  1.1  christos   bfd_uint64_t v;
    663  1.1  christos 
    664  1.1  christos   v  = addr[0]; v <<= 8;
    665  1.1  christos   v |= addr[1]; v <<= 8;
    666  1.1  christos   v |= addr[2]; v <<= 8;
    667  1.1  christos   v |= addr[3]; v <<= 8;
    668  1.1  christos   v |= addr[4]; v <<= 8;
    669  1.1  christos   v |= addr[5]; v <<= 8;
    670  1.1  christos   v |= addr[6]; v <<= 8;
    671  1.1  christos   v |= addr[7];
    672  1.1  christos 
    673  1.1  christos   return v;
    674  1.1  christos #else
    675  1.1  christos   BFD_FAIL();
    676  1.1  christos   return 0;
    677  1.1  christos #endif
    678  1.1  christos }
    679  1.1  christos 
    680  1.1  christos bfd_uint64_t
    681  1.1  christos bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
    682  1.1  christos {
    683  1.1  christos #ifdef BFD_HOST_64_BIT
    684  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    685  1.1  christos   bfd_uint64_t v;
    686  1.1  christos 
    687  1.1  christos   v  = addr[7]; v <<= 8;
    688  1.1  christos   v |= addr[6]; v <<= 8;
    689  1.1  christos   v |= addr[5]; v <<= 8;
    690  1.1  christos   v |= addr[4]; v <<= 8;
    691  1.1  christos   v |= addr[3]; v <<= 8;
    692  1.1  christos   v |= addr[2]; v <<= 8;
    693  1.1  christos   v |= addr[1]; v <<= 8;
    694  1.1  christos   v |= addr[0];
    695  1.1  christos 
    696  1.1  christos   return v;
    697  1.1  christos #else
    698  1.1  christos   BFD_FAIL();
    699  1.1  christos   return 0;
    700  1.1  christos #endif
    701  1.1  christos 
    702  1.1  christos }
    703  1.1  christos 
    704  1.1  christos bfd_int64_t
    705  1.1  christos bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
    706  1.1  christos {
    707  1.1  christos #ifdef BFD_HOST_64_BIT
    708  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    709  1.1  christos   bfd_uint64_t v;
    710  1.1  christos 
    711  1.1  christos   v  = addr[0]; v <<= 8;
    712  1.1  christos   v |= addr[1]; v <<= 8;
    713  1.1  christos   v |= addr[2]; v <<= 8;
    714  1.1  christos   v |= addr[3]; v <<= 8;
    715  1.1  christos   v |= addr[4]; v <<= 8;
    716  1.1  christos   v |= addr[5]; v <<= 8;
    717  1.1  christos   v |= addr[6]; v <<= 8;
    718  1.1  christos   v |= addr[7];
    719  1.1  christos 
    720  1.1  christos   return COERCE64 (v);
    721  1.1  christos #else
    722  1.1  christos   BFD_FAIL();
    723  1.1  christos   return 0;
    724  1.1  christos #endif
    725  1.1  christos }
    726  1.1  christos 
    727  1.1  christos bfd_int64_t
    728  1.1  christos bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
    729  1.1  christos {
    730  1.1  christos #ifdef BFD_HOST_64_BIT
    731  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    732  1.1  christos   bfd_uint64_t v;
    733  1.1  christos 
    734  1.1  christos   v  = addr[7]; v <<= 8;
    735  1.1  christos   v |= addr[6]; v <<= 8;
    736  1.1  christos   v |= addr[5]; v <<= 8;
    737  1.1  christos   v |= addr[4]; v <<= 8;
    738  1.1  christos   v |= addr[3]; v <<= 8;
    739  1.1  christos   v |= addr[2]; v <<= 8;
    740  1.1  christos   v |= addr[1]; v <<= 8;
    741  1.1  christos   v |= addr[0];
    742  1.1  christos 
    743  1.1  christos   return COERCE64 (v);
    744  1.1  christos #else
    745  1.1  christos   BFD_FAIL();
    746  1.1  christos   return 0;
    747  1.1  christos #endif
    748  1.1  christos }
    749  1.1  christos 
    750  1.1  christos void
    751  1.1  christos bfd_putb32 (bfd_vma data, void *p)
    752  1.1  christos {
    753  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    754  1.1  christos   addr[0] = (data >> 24) & 0xff;
    755  1.1  christos   addr[1] = (data >> 16) & 0xff;
    756  1.1  christos   addr[2] = (data >>  8) & 0xff;
    757  1.1  christos   addr[3] = data & 0xff;
    758  1.1  christos }
    759  1.1  christos 
    760  1.1  christos void
    761  1.1  christos bfd_putl32 (bfd_vma data, void *p)
    762  1.1  christos {
    763  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    764  1.1  christos   addr[0] = data & 0xff;
    765  1.1  christos   addr[1] = (data >>  8) & 0xff;
    766  1.1  christos   addr[2] = (data >> 16) & 0xff;
    767  1.1  christos   addr[3] = (data >> 24) & 0xff;
    768  1.1  christos }
    769  1.1  christos 
    770  1.1  christos void
    771  1.1  christos bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
    772  1.1  christos {
    773  1.1  christos #ifdef BFD_HOST_64_BIT
    774  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    775  1.1  christos   addr[0] = (data >> (7*8)) & 0xff;
    776  1.1  christos   addr[1] = (data >> (6*8)) & 0xff;
    777  1.1  christos   addr[2] = (data >> (5*8)) & 0xff;
    778  1.1  christos   addr[3] = (data >> (4*8)) & 0xff;
    779  1.1  christos   addr[4] = (data >> (3*8)) & 0xff;
    780  1.1  christos   addr[5] = (data >> (2*8)) & 0xff;
    781  1.1  christos   addr[6] = (data >> (1*8)) & 0xff;
    782  1.1  christos   addr[7] = (data >> (0*8)) & 0xff;
    783  1.1  christos #else
    784  1.1  christos   BFD_FAIL();
    785  1.1  christos #endif
    786  1.1  christos }
    787  1.1  christos 
    788  1.1  christos void
    789  1.1  christos bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
    790  1.1  christos {
    791  1.1  christos #ifdef BFD_HOST_64_BIT
    792  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    793  1.1  christos   addr[7] = (data >> (7*8)) & 0xff;
    794  1.1  christos   addr[6] = (data >> (6*8)) & 0xff;
    795  1.1  christos   addr[5] = (data >> (5*8)) & 0xff;
    796  1.1  christos   addr[4] = (data >> (4*8)) & 0xff;
    797  1.1  christos   addr[3] = (data >> (3*8)) & 0xff;
    798  1.1  christos   addr[2] = (data >> (2*8)) & 0xff;
    799  1.1  christos   addr[1] = (data >> (1*8)) & 0xff;
    800  1.1  christos   addr[0] = (data >> (0*8)) & 0xff;
    801  1.1  christos #else
    802  1.1  christos   BFD_FAIL();
    803  1.1  christos #endif
    804  1.1  christos }
    805  1.1  christos 
    806  1.1  christos void
    807  1.1  christos bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
    808  1.1  christos {
    809  1.1  christos   bfd_byte *addr = (bfd_byte *) p;
    810  1.1  christos   int i;
    811  1.1  christos   int bytes;
    812  1.1  christos 
    813  1.1  christos   if (bits % 8 != 0)
    814  1.1  christos     abort ();
    815  1.1  christos 
    816  1.1  christos   bytes = bits / 8;
    817  1.1  christos   for (i = 0; i < bytes; i++)
    818  1.1  christos     {
    819  1.1  christos       int addr_index = big_p ? bytes - i - 1 : i;
    820  1.1  christos 
    821  1.1  christos       addr[addr_index] = data & 0xff;
    822  1.1  christos       data >>= 8;
    823  1.1  christos     }
    824  1.1  christos }
    825  1.1  christos 
    826  1.1  christos bfd_uint64_t
    827  1.1  christos bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
    828  1.1  christos {
    829  1.1  christos   const bfd_byte *addr = (const bfd_byte *) p;
    830  1.1  christos   bfd_uint64_t data;
    831  1.1  christos   int i;
    832  1.1  christos   int bytes;
    833  1.1  christos 
    834  1.1  christos   if (bits % 8 != 0)
    835  1.1  christos     abort ();
    836  1.1  christos 
    837  1.1  christos   data = 0;
    838  1.1  christos   bytes = bits / 8;
    839  1.1  christos   for (i = 0; i < bytes; i++)
    840  1.1  christos     {
    841  1.1  christos       int addr_index = big_p ? i : bytes - i - 1;
    842  1.1  christos 
    843  1.1  christos       data = (data << 8) | addr[addr_index];
    844  1.1  christos     }
    845  1.1  christos 
    846  1.1  christos   return data;
    847  1.1  christos }
    848  1.1  christos 
    849  1.1  christos /* Default implementation */
    851  1.1  christos 
    852  1.1  christos bfd_boolean
    853  1.1  christos _bfd_generic_get_section_contents (bfd *abfd,
    854  1.1  christos 				   sec_ptr section,
    855  1.1  christos 				   void *location,
    856  1.1  christos 				   file_ptr offset,
    857  1.1  christos 				   bfd_size_type count)
    858  1.1  christos {
    859  1.1  christos   bfd_size_type sz;
    860  1.1  christos   if (count == 0)
    861  1.1  christos     return TRUE;
    862  1.1  christos 
    863  1.1  christos   if (section->compress_status != COMPRESS_SECTION_NONE)
    864  1.1  christos     {
    865  1.1  christos       (*_bfd_error_handler)
    866  1.1  christos 	(_("%B: unable to get decompressed section %A"),
    867  1.1  christos 	 abfd, section);
    868  1.1  christos       bfd_set_error (bfd_error_invalid_operation);
    869  1.1  christos       return FALSE;
    870  1.1  christos     }
    871  1.1  christos 
    872  1.1  christos   sz = section->rawsize ? section->rawsize : section->size;
    873  1.1  christos   if (offset + count < count
    874  1.1  christos       || offset + count > sz)
    875  1.1  christos     {
    876  1.1  christos       bfd_set_error (bfd_error_invalid_operation);
    877  1.1  christos       return FALSE;
    878  1.1  christos     }
    879  1.1  christos 
    880  1.1  christos   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
    881  1.1  christos       || bfd_bread (location, count, abfd) != count)
    882  1.1  christos     return FALSE;
    883  1.1  christos 
    884  1.1  christos   return TRUE;
    885  1.1  christos }
    886  1.1  christos 
    887  1.1  christos bfd_boolean
    888  1.1  christos _bfd_generic_get_section_contents_in_window
    889  1.1  christos   (bfd *abfd ATTRIBUTE_UNUSED,
    890  1.1  christos    sec_ptr section ATTRIBUTE_UNUSED,
    891  1.1  christos    bfd_window *w ATTRIBUTE_UNUSED,
    892  1.1  christos    file_ptr offset ATTRIBUTE_UNUSED,
    893  1.1  christos    bfd_size_type count ATTRIBUTE_UNUSED)
    894  1.1  christos {
    895  1.1  christos #ifdef USE_MMAP
    896  1.1  christos   bfd_size_type sz;
    897  1.1  christos 
    898  1.1  christos   if (count == 0)
    899  1.1  christos     return TRUE;
    900  1.1  christos   if (abfd->xvec->_bfd_get_section_contents
    901  1.1  christos       != _bfd_generic_get_section_contents)
    902  1.1  christos     {
    903  1.1  christos       /* We don't know what changes the bfd's get_section_contents
    904  1.1  christos 	 method may have to make.  So punt trying to map the file
    905  1.1  christos 	 window, and let get_section_contents do its thing.  */
    906  1.1  christos       /* @@ FIXME : If the internal window has a refcount of 1 and was
    907  1.1  christos 	 allocated with malloc instead of mmap, just reuse it.  */
    908  1.1  christos       bfd_free_window (w);
    909  1.1  christos       w->i = bfd_zmalloc (sizeof (bfd_window_internal));
    910  1.1  christos       if (w->i == NULL)
    911  1.1  christos 	return FALSE;
    912  1.1  christos       w->i->data = bfd_malloc (count);
    913  1.1  christos       if (w->i->data == NULL)
    914  1.1  christos 	{
    915  1.1  christos 	  free (w->i);
    916  1.1  christos 	  w->i = NULL;
    917  1.1  christos 	  return FALSE;
    918  1.1  christos 	}
    919  1.1  christos       w->i->mapped = 0;
    920  1.1  christos       w->i->refcount = 1;
    921  1.1  christos       w->size = w->i->size = count;
    922  1.1  christos       w->data = w->i->data;
    923  1.1  christos       return bfd_get_section_contents (abfd, section, w->data, offset, count);
    924  1.1  christos     }
    925  1.1  christos   sz = section->rawsize ? section->rawsize : section->size;
    926  1.1  christos   if (offset + count > sz
    927  1.1  christos       || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
    928  1.1  christos 				TRUE))
    929  1.1  christos     return FALSE;
    930  1.1  christos   return TRUE;
    931  1.1  christos #else
    932  1.1  christos   abort ();
    933  1.1  christos #endif
    934  1.1  christos }
    935  1.1  christos 
    936  1.1  christos /* This generic function can only be used in implementations where creating
    937  1.1  christos    NEW sections is disallowed.  It is useful in patching existing sections
    938  1.1  christos    in read-write files, though.  See other set_section_contents functions
    939  1.1  christos    to see why it doesn't work for new sections.  */
    940  1.1  christos bfd_boolean
    941  1.1  christos _bfd_generic_set_section_contents (bfd *abfd,
    942  1.1  christos 				   sec_ptr section,
    943  1.1  christos 				   const void *location,
    944  1.1  christos 				   file_ptr offset,
    945  1.1  christos 				   bfd_size_type count)
    946  1.1  christos {
    947  1.1  christos   if (count == 0)
    948  1.1  christos     return TRUE;
    949  1.1  christos 
    950  1.1  christos   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
    951  1.1  christos       || bfd_bwrite (location, count, abfd) != count)
    952  1.1  christos     return FALSE;
    953  1.1  christos 
    954  1.1  christos   return TRUE;
    955  1.1  christos }
    956  1.1  christos 
    957  1.1  christos /*
    958  1.1  christos INTERNAL_FUNCTION
    959  1.1  christos 	bfd_log2
    960  1.1  christos 
    961  1.1  christos SYNOPSIS
    962  1.1  christos 	unsigned int bfd_log2 (bfd_vma x);
    963  1.1  christos 
    964  1.1  christos DESCRIPTION
    965  1.1  christos 	Return the log base 2 of the value supplied, rounded up.  E.g., an
    966  1.1  christos 	@var{x} of 1025 returns 11.  A @var{x} of 0 returns 0.
    967  1.1  christos */
    968  1.1  christos 
    969  1.1  christos unsigned int
    970  1.1  christos bfd_log2 (bfd_vma x)
    971  1.1  christos {
    972  1.1  christos   unsigned int result = 0;
    973  1.1  christos 
    974  1.1  christos   while ((x = (x >> 1)) != 0)
    975  1.1  christos     ++result;
    976  1.1  christos   return result;
    977  1.1  christos }
    978  1.1  christos 
    979  1.1  christos bfd_boolean
    980  1.1  christos bfd_generic_is_local_label_name (bfd *abfd, const char *name)
    981  1.1  christos {
    982  1.1  christos   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
    983  1.1  christos 
    984  1.1  christos   return name[0] == locals_prefix;
    985  1.1  christos }
    986  1.1  christos 
    987  1.1  christos /*  Can be used from / for bfd_merge_private_bfd_data to check that
    988  1.1  christos     endianness matches between input and output file.  Returns
    989  1.1  christos     TRUE for a match, otherwise returns FALSE and emits an error.  */
    990  1.1  christos bfd_boolean
    991  1.1  christos _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
    992  1.1  christos {
    993  1.1  christos   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
    994  1.1  christos       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
    995  1.1  christos       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
    996  1.1  christos     {
    997  1.1  christos       const char *msg;
    998  1.1  christos 
    999  1.1  christos       if (bfd_big_endian (ibfd))
   1000  1.1  christos 	msg = _("%B: compiled for a big endian system and target is little endian");
   1001  1.1  christos       else
   1002  1.1  christos 	msg = _("%B: compiled for a little endian system and target is big endian");
   1003  1.1  christos 
   1004  1.1  christos       (*_bfd_error_handler) (msg, ibfd);
   1005  1.1  christos 
   1006  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   1007  1.1  christos       return FALSE;
   1008  1.1  christos     }
   1009  1.1  christos 
   1010  1.1  christos   return TRUE;
   1011  1.1  christos }
   1012  1.1  christos 
   1013  1.1  christos /* Give a warning at runtime if someone compiles code which calls
   1014  1.1  christos    old routines.  */
   1015  1.1  christos 
   1016  1.1  christos void
   1017  1.1  christos warn_deprecated (const char *what,
   1018  1.1  christos 		 const char *file,
   1019  1.1  christos 		 int line,
   1020  1.1  christos 		 const char *func)
   1021  1.1  christos {
   1022  1.1  christos   /* Poor man's tracking of functions we've already warned about.  */
   1023  1.1  christos   static size_t mask = 0;
   1024  1.1  christos 
   1025  1.1  christos   if (~(size_t) func & ~mask)
   1026  1.1  christos     {
   1027  1.1  christos       fflush (stdout);
   1028  1.1  christos       /* Note: separate sentences in order to allow
   1029  1.1  christos 	 for translation into other languages.  */
   1030  1.1  christos       if (func)
   1031  1.1  christos 	fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
   1032  1.1  christos 		 what, file, line, func);
   1033  1.1  christos       else
   1034  1.1  christos 	fprintf (stderr, _("Deprecated %s called\n"), what);
   1035  1.1  christos       fflush (stderr);
   1036  1.1  christos       mask |= ~(size_t) func;
   1037  1.1  christos     }
   1038  1.1  christos }
   1039  1.1  christos 
   1040  1.1  christos /* Helper function for reading uleb128 encoded data.  */
   1041  1.1  christos 
   1042  1.1  christos bfd_vma
   1043  1.1  christos read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
   1044  1.1  christos 		      bfd_byte *buf,
   1045  1.1  christos 		      unsigned int *bytes_read_ptr)
   1046  1.1  christos {
   1047  1.1  christos   bfd_vma result;
   1048  1.1  christos   unsigned int num_read;
   1049  1.1  christos   unsigned int shift;
   1050  1.1  christos   unsigned char byte;
   1051  1.1  christos 
   1052  1.1  christos   result = 0;
   1053  1.1  christos   shift = 0;
   1054  1.1  christos   num_read = 0;
   1055  1.1  christos   do
   1056  1.1  christos     {
   1057  1.1  christos       byte = bfd_get_8 (abfd, buf);
   1058  1.1  christos       buf++;
   1059  1.1  christos       num_read++;
   1060  1.1  christos       result |= (((bfd_vma) byte & 0x7f) << shift);
   1061  1.1  christos       shift += 7;
   1062  1.1  christos     }
   1063  1.1  christos   while (byte & 0x80);
   1064  1.1  christos   *bytes_read_ptr = num_read;
   1065  1.1  christos   return result;
   1066  1.1  christos }
   1067  1.1  christos 
   1068  1.1  christos /* Helper function for reading sleb128 encoded data.  */
   1069  1.1  christos 
   1070  1.1  christos bfd_signed_vma
   1071  1.1  christos read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
   1072  1.1  christos 		    bfd_byte *buf,
   1073  1.1  christos 		    unsigned int *bytes_read_ptr)
   1074  1.1  christos {
   1075  1.1  christos   bfd_vma result;
   1076  1.1  christos   unsigned int shift;
   1077  1.1  christos   unsigned int num_read;
   1078  1.1  christos   unsigned char byte;
   1079  1.1  christos 
   1080  1.1  christos   result = 0;
   1081  1.1  christos   shift = 0;
   1082  1.1  christos   num_read = 0;
   1083  1.1  christos   do
   1084  1.1  christos     {
   1085  1.1  christos       byte = bfd_get_8 (abfd, buf);
   1086  1.1  christos       buf ++;
   1087  1.1  christos       num_read ++;
   1088  1.1  christos       result |= (((bfd_vma) byte & 0x7f) << shift);
   1089  1.1  christos       shift += 7;
   1090  1.1  christos     }
   1091  1.1  christos   while (byte & 0x80);
   1092  1.1  christos   if (shift < 8 * sizeof (result) && (byte & 0x40))
   1093  1.1  christos     result |= (((bfd_vma) -1) << shift);
   1094  1.1  christos   *bytes_read_ptr = num_read;
   1095  1.1  christos   return result;
   1096  1.1  christos }
   1097  1.1  christos 
   1098  1.1  christos bfd_boolean
   1099  1.1  christos _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
   1100  1.1  christos 		       asymbol **symbols ATTRIBUTE_UNUSED,
   1101  1.1  christos 		       asymbol *symbol ATTRIBUTE_UNUSED,
   1102  1.1  christos 		       const char **filename_ptr ATTRIBUTE_UNUSED,
   1103  1.1  christos 		       unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
   1104  1.1  christos {
   1105  1.1  christos   return FALSE;
   1106  1.1  christos }
   1107  1.1  christos 
   1108  1.1  christos bfd_boolean
   1109  1.1  christos _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
   1110  1.1  christos 					asection *isec ATTRIBUTE_UNUSED,
   1111  1.1  christos 					bfd *obfd ATTRIBUTE_UNUSED,
   1112  1.1  christos 					asection *osec ATTRIBUTE_UNUSED,
   1113  1.1  christos 					struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   1114                {
   1115                  return TRUE;
   1116                }
   1117