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