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