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