Home | History | Annotate | Line # | Download | only in bfd
      1 /* Assorted BFD support routines, only used internally.
      2    Copyright (C) 1990-2024 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 "elf-bfd.h"
     25 #include "libbfd.h"
     26 #include "objalloc.h"
     27 
     28 #ifndef HAVE_GETPAGESIZE
     29 #define getpagesize() 2048
     30 #endif
     31 
     32 /*
     33 SECTION
     34 	Implementation details
     35 
     36 SUBSECTION
     37 	Internal functions
     38 
     39 DESCRIPTION
     40 	These routines are used within BFD.
     41 	They are not intended for export, but are documented here for
     42 	completeness.
     43 */
     44 
     45 bool
     46 _bfd_bool_bfd_false (bfd *abfd ATTRIBUTE_UNUSED)
     47 {
     48   return false;
     49 }
     50 
     51 bool
     52 _bfd_bool_bfd_asymbol_false (bfd *abfd ATTRIBUTE_UNUSED,
     53 			     asymbol *sym ATTRIBUTE_UNUSED)
     54 {
     55   return false;
     56 }
     57 
     58 /* A routine which is used in target vectors for unsupported
     59    operations.  */
     60 
     61 bool
     62 _bfd_bool_bfd_false_error (bfd *ignore ATTRIBUTE_UNUSED)
     63 {
     64   bfd_set_error (bfd_error_invalid_operation);
     65   return false;
     66 }
     67 
     68 bool
     69 _bfd_bool_bfd_link_false_error (bfd *abfd,
     70 				struct bfd_link_info *info ATTRIBUTE_UNUSED)
     71 {
     72   return _bfd_bool_bfd_false_error (abfd);
     73 }
     74 
     75 /* A routine which is used in target vectors for supported operations
     76    which do not actually do anything.  */
     77 
     78 bool
     79 _bfd_bool_bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
     80 {
     81   return true;
     82 }
     83 
     84 bool
     85 _bfd_bool_bfd_link_true (bfd *abfd ATTRIBUTE_UNUSED,
     86 			 struct bfd_link_info *info ATTRIBUTE_UNUSED)
     87 {
     88   return true;
     89 }
     90 
     91 bool
     92 _bfd_bool_bfd_bfd_true (bfd *ibfd ATTRIBUTE_UNUSED,
     93 			bfd *obfd ATTRIBUTE_UNUSED)
     94 {
     95   return true;
     96 }
     97 
     98 bool
     99 _bfd_bool_bfd_uint_true (bfd *abfd ATTRIBUTE_UNUSED,
    100 			 unsigned int flags ATTRIBUTE_UNUSED)
    101 {
    102   return true;
    103 }
    104 
    105 bool
    106 _bfd_bool_bfd_asection_bfd_asection_true (bfd *ibfd ATTRIBUTE_UNUSED,
    107 					  asection *isec ATTRIBUTE_UNUSED,
    108 					  bfd *obfd ATTRIBUTE_UNUSED,
    109 					  asection *osec ATTRIBUTE_UNUSED)
    110 {
    111   return true;
    112 }
    113 
    114 bool
    115 _bfd_bool_bfd_asymbol_bfd_asymbol_true (bfd *ibfd ATTRIBUTE_UNUSED,
    116 					asymbol *isym ATTRIBUTE_UNUSED,
    117 					bfd *obfd ATTRIBUTE_UNUSED,
    118 					asymbol *osym ATTRIBUTE_UNUSED)
    119 {
    120   return true;
    121 }
    122 
    123 bool
    124 _bfd_bool_bfd_ptr_true (bfd *abfd ATTRIBUTE_UNUSED,
    125 			void *ptr ATTRIBUTE_UNUSED)
    126 {
    127   return true;
    128 }
    129 
    130 /* A routine which is used in target vectors for unsupported
    131    operations which return a pointer value.  */
    132 
    133 void *
    134 _bfd_ptr_bfd_null_error (bfd *ignore ATTRIBUTE_UNUSED)
    135 {
    136   bfd_set_error (bfd_error_invalid_operation);
    137   return NULL;
    138 }
    139 
    140 int
    141 _bfd_int_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
    142 {
    143   return 0;
    144 }
    145 
    146 unsigned int
    147 _bfd_uint_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
    148 {
    149    return 0;
    150 }
    151 
    152 long
    153 _bfd_long_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
    154 {
    155   return 0;
    156 }
    157 
    158 /* A routine which is used in target vectors for unsupported
    159    operations which return -1 on error.  */
    160 
    161 long
    162 _bfd_long_bfd_n1_error (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    163 {
    164   bfd_set_error (bfd_error_invalid_operation);
    165   return -1;
    166 }
    167 
    168 void
    169 _bfd_void_bfd (bfd *ignore ATTRIBUTE_UNUSED)
    170 {
    171 }
    172 
    173 void
    174 _bfd_void_bfd_link (bfd *abfd ATTRIBUTE_UNUSED,
    175 		    struct bfd_link_info *info ATTRIBUTE_UNUSED)
    176 {
    177 }
    178 
    179 void
    180 _bfd_void_bfd_asection (bfd *abfd ATTRIBUTE_UNUSED,
    181 			asection *sec ATTRIBUTE_UNUSED)
    182 {
    183 }
    184 
    185 long
    186 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
    187 				     asection *sec ATTRIBUTE_UNUSED)
    188 {
    189   return sizeof (arelent *);
    190 }
    191 
    192 long
    193 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
    194 				  asection *sec ATTRIBUTE_UNUSED,
    195 				  arelent **relptr,
    196 				  asymbol **symbols ATTRIBUTE_UNUSED)
    197 {
    198   *relptr = NULL;
    199   return 0;
    200 }
    201 
    202 void
    203 _bfd_norelocs_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
    204 			 asection *sec ATTRIBUTE_UNUSED,
    205 			 arelent **relptr ATTRIBUTE_UNUSED,
    206 			 unsigned int count ATTRIBUTE_UNUSED)
    207 {
    208   /* Do nothing.  */
    209 }
    210 
    211 bool
    212 _bfd_nocore_core_file_matches_executable_p
    213   (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
    214    bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
    215 {
    216   bfd_set_error (bfd_error_invalid_operation);
    217   return false;
    218 }
    219 
    220 /* Routine to handle core_file_failing_command entry point for targets
    221    without core file support.  */
    222 
    223 char *
    224 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    225 {
    226   bfd_set_error (bfd_error_invalid_operation);
    227   return NULL;
    228 }
    229 
    230 /* Routine to handle core_file_failing_signal entry point for targets
    231    without core file support.  */
    232 
    233 int
    234 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    235 {
    236   bfd_set_error (bfd_error_invalid_operation);
    237   return 0;
    238 }
    239 
    240 /* Routine to handle the core_file_pid entry point for targets without
    241    core file support.  */
    242 
    243 int
    244 _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    245 {
    246   bfd_set_error (bfd_error_invalid_operation);
    247   return 0;
    248 }
    249 
    250 bfd_cleanup
    251 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
    252 {
    253   bfd_set_error (bfd_error_wrong_format);
    254   return 0;
    255 }
    256 
    257 /* Allocate memory using malloc.  */
    259 
    260 #ifndef SSIZE_MAX
    261 #define SSIZE_MAX ((size_t) -1 >> 1)
    262 #endif
    263 
    264 /*
    265 INTERNAL_FUNCTION
    266 	bfd_malloc
    267 
    268 SYNOPSIS
    269 	void *bfd_malloc (bfd_size_type {*size*});
    270 
    271 DESCRIPTION
    272 	Returns a pointer to an allocated block of memory that is at least
    273 	SIZE bytes long.  If SIZE is 0 then it will be treated as if it were
    274 	1.  If SIZE is too big then NULL will be returned.
    275 
    276 	Returns NULL upon error and sets bfd_error.
    277 */
    278 void *
    279 bfd_malloc (bfd_size_type size)
    280 {
    281   void *ptr;
    282   size_t sz = (size_t) size;
    283 
    284   if (size != sz
    285       /* This is to pacify memory checkers like valgrind.  */
    286       || sz > SSIZE_MAX)
    287     {
    288       bfd_set_error (bfd_error_no_memory);
    289       return NULL;
    290     }
    291 
    292   ptr = malloc (sz ? sz : 1);
    293   if (ptr == NULL)
    294     bfd_set_error (bfd_error_no_memory);
    295 
    296   return ptr;
    297 }
    298 
    299 /*
    300 INTERNAL_FUNCTION
    301 	bfd_realloc
    302 
    303 SYNOPSIS
    304 	void *bfd_realloc (void *{*mem*}, bfd_size_type {*size*});
    305 
    306 DESCRIPTION
    307 	Returns a pointer to an allocated block of memory that is at least
    308 	SIZE bytes long.  If SIZE is 0 then it will be treated as if it were
    309 	1.  If SIZE is too big then NULL will be returned.
    310 
    311 	If MEM is not NULL then it must point to an allocated block of memory.
    312 	If this block is large enough then MEM may be used as the return
    313 	value for this function, but this is not guaranteed.
    314 
    315 	If MEM is not returned then the first N bytes in the returned block
    316 	will be identical to the first N bytes in region pointed to by MEM,
    317 	where N is the lessor of SIZE and the length of the region of memory
    318 	currently addressed by MEM.
    319 
    320 	Returns NULL upon error and sets bfd_error.
    321 */
    322 void *
    323 bfd_realloc (void *ptr, bfd_size_type size)
    324 {
    325   void *ret;
    326   size_t sz = (size_t) size;
    327 
    328   if (ptr == NULL)
    329     return bfd_malloc (size);
    330 
    331   if (size != sz
    332       /* This is to pacify memory checkers like valgrind.  */
    333       || sz > SSIZE_MAX)
    334     {
    335       bfd_set_error (bfd_error_no_memory);
    336       return NULL;
    337     }
    338 
    339   /* The behaviour of realloc(0) is implementation defined,
    340      but for this function we always allocate memory.  */
    341   ret = realloc (ptr, sz ? sz : 1);
    342 
    343   if (ret == NULL)
    344     bfd_set_error (bfd_error_no_memory);
    345 
    346   return ret;
    347 }
    348 
    349 /*
    350 INTERNAL_FUNCTION
    351 	bfd_realloc_or_free
    352 
    353 SYNOPSIS
    354 	void *bfd_realloc_or_free (void *{*mem*}, bfd_size_type {*size*});
    355 
    356 DESCRIPTION
    357 	Returns a pointer to an allocated block of memory that is at least
    358 	SIZE bytes long.  If SIZE is 0 then no memory will be allocated,
    359 	MEM will be freed, and NULL will be returned.  This will not cause
    360 	bfd_error to be set.
    361 
    362 	If SIZE is too big then NULL will be returned and bfd_error will be
    363 	set.
    364 
    365 	If MEM is not NULL then it must point to an allocated block of memory.
    366 	If this block is large enough then MEM may be used as the return
    367 	value for this function, but this is not guaranteed.
    368 
    369 	If MEM is not returned then the first N bytes in the returned block
    370 	will be identical to the first N bytes in region pointed to by MEM,
    371 	where N is the lessor of SIZE and the length of the region of memory
    372 	currently addressed by MEM.
    373 */
    374 void *
    375 bfd_realloc_or_free (void *ptr, bfd_size_type size)
    376 {
    377   void *ret;
    378 
    379   /* The behaviour of realloc(0) is implementation defined, but
    380      for this function we treat it is always freeing the memory.  */
    381   if (size == 0)
    382     {
    383       free (ptr);
    384       return NULL;
    385     }
    386 
    387   ret = bfd_realloc (ptr, size);
    388   if (ret == NULL)
    389     free (ptr);
    390 
    391   return ret;
    392 }
    393 
    394 /*
    395 INTERNAL_FUNCTION
    396 	bfd_zmalloc
    397 
    398 SYNOPSIS
    399 	void *bfd_zmalloc (bfd_size_type {*size*});
    400 
    401 DESCRIPTION
    402 	Returns a pointer to an allocated block of memory that is at least
    403 	SIZE bytes long.  If SIZE is 0 then it will be treated as if it were
    404 	1.  If SIZE is too big then NULL will be returned.
    405 
    406 	Returns NULL upon error and sets bfd_error.
    407 
    408 	If NULL is not returned then the allocated block of memory will
    409 	have been cleared.
    410 */
    411 void *
    412 bfd_zmalloc (bfd_size_type size)
    413 {
    414   void *ptr = bfd_malloc (size);
    415 
    416   if (ptr != NULL)
    417     memset (ptr, 0, size ? (size_t) size : 1);
    418 
    419   return ptr;
    420 }
    421 
    422 /*
    423 FUNCTION
    424 	bfd_alloc
    425 
    426 SYNOPSIS
    427 	void *bfd_alloc (bfd *abfd, bfd_size_type wanted);
    428 
    429 DESCRIPTION
    430 	Allocate a block of @var{wanted} bytes of memory attached to
    431 	<<abfd>> and return a pointer to it.
    432 */
    433 
    434 void *
    435 bfd_alloc (bfd *abfd, bfd_size_type size)
    436 {
    437   void *ret;
    438   unsigned long ul_size = (unsigned long) size;
    439 
    440   if (size != ul_size
    441       /* Note - although objalloc_alloc takes an unsigned long as its
    442 	 argument, internally the size is treated as a signed long.  This can
    443 	 lead to problems where, for example, a request to allocate -1 bytes
    444 	 can result in just 1 byte being allocated, rather than
    445 	 ((unsigned long) -1) bytes.  Also memory checkers will often
    446 	 complain about attempts to allocate a negative amount of memory.
    447 	 So to stop these problems we fail if the size is negative.  */
    448       || ((signed long) ul_size) < 0)
    449     {
    450       bfd_set_error (bfd_error_no_memory);
    451       return NULL;
    452     }
    453 
    454   ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size);
    455   if (ret == NULL)
    456     bfd_set_error (bfd_error_no_memory);
    457   else
    458     abfd->alloc_size += size;
    459   return ret;
    460 }
    461 
    462 /*
    463 FUNCTION
    464 	bfd_zalloc
    465 
    466 SYNOPSIS
    467 	void *bfd_zalloc (bfd *abfd, bfd_size_type wanted);
    468 
    469 DESCRIPTION
    470 	Allocate a block of @var{wanted} bytes of zeroed memory
    471 	attached to <<abfd>> and return a pointer to it.
    472 */
    473 
    474 void *
    475 bfd_zalloc (bfd *abfd, bfd_size_type size)
    476 {
    477   void *res;
    478 
    479   res = bfd_alloc (abfd, size);
    480   if (res)
    481     memset (res, 0, (size_t) size);
    482   return res;
    483 }
    484 
    485 /*
    486 FUNCTION
    487 	bfd_release
    488 
    489 SYNOPSIS
    490 	void bfd_release (bfd *, void *);
    491 
    492 DESCRIPTION
    493 	Free a block allocated for a BFD.
    494 	Note: Also frees all more recently allocated blocks!
    495 */
    496 
    497 void
    498 bfd_release (bfd *abfd, void *block)
    499 {
    500   objalloc_free_block ((struct objalloc *) abfd->memory, block);
    501 }
    502 
    503 /*
    504 INTERNAL_FUNCTION
    505 	bfd_write_bigendian_4byte_int
    506 
    507 SYNOPSIS
    508 	bool bfd_write_bigendian_4byte_int (bfd *, unsigned int);
    509 
    510 DESCRIPTION
    511 	Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
    512 	endian order regardless of what else is going on.  This is useful in
    513 	archives.
    514 
    515 */
    516 bool
    517 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
    518 {
    519   bfd_byte buffer[4];
    520   bfd_putb32 (i, buffer);
    521   return bfd_write (buffer, 4, abfd) == 4;
    522 }
    523 
    524 
    525 /** The do-it-yourself (byte) sex-change kit */
    527 
    528 /* The middle letter e.g. get<b>short indicates Big or Little endian
    529    target machine.  It doesn't matter what the byte order of the host
    530    machine is; these routines work for either.  */
    531 
    532 /* FIXME: Should these take a count argument?
    533    Answer (gnu (at) cygnus.com):  No, but perhaps they should be inline
    534 			     functions in swap.h #ifdef __GNUC__.
    535 			     Gprof them later and find out.  */
    536 
    537 /*
    538 FUNCTION
    539 	bfd_put_size
    540 FUNCTION
    541 	bfd_get_size
    542 
    543 DESCRIPTION
    544 	These macros as used for reading and writing raw data in
    545 	sections; each access (except for bytes) is vectored through
    546 	the target format of the BFD and mangled accordingly. The
    547 	mangling performs any necessary endian translations and
    548 	removes alignment restrictions.  Note that types accepted and
    549 	returned by these macros are identical so they can be swapped
    550 	around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
    551 	to either <<bfd_get_32>> or <<bfd_get_64>>.
    552 
    553 	In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
    554 	system without prototypes, the caller is responsible for making
    555 	sure that is true, with a cast if necessary.  We don't cast
    556 	them in the macro definitions because that would prevent <<lint>>
    557 	or <<gcc -Wall>> from detecting sins such as passing a pointer.
    558 	To detect calling these with less than a <<bfd_vma>>, use
    559 	<<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
    560 
    561 .
    562 .{* Byte swapping macros for user section data.  *}
    563 .
    564 .#define bfd_put_8(abfd, val, ptr) \
    565 .  ((void) (*((bfd_byte *) (ptr)) = (val) & 0xff))
    566 .#define bfd_put_signed_8 \
    567 .  bfd_put_8
    568 .#define bfd_get_8(abfd, ptr) \
    569 .  ((bfd_vma) *(const bfd_byte *) (ptr) & 0xff)
    570 .#define bfd_get_signed_8(abfd, ptr) \
    571 .  ((((bfd_signed_vma) *(const bfd_byte *) (ptr) & 0xff) ^ 0x80) - 0x80)
    572 .
    573 .#define bfd_put_16(abfd, val, ptr) \
    574 .  BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
    575 .#define bfd_put_signed_16 \
    576 .  bfd_put_16
    577 .#define bfd_get_16(abfd, ptr) \
    578 .  BFD_SEND (abfd, bfd_getx16, (ptr))
    579 .#define bfd_get_signed_16(abfd, ptr) \
    580 .  BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
    581 .
    582 .#define bfd_put_24(abfd, val, ptr) \
    583 .  do					\
    584 .    if (bfd_big_endian (abfd))		\
    585 .      bfd_putb24 ((val), (ptr));	\
    586 .    else				\
    587 .      bfd_putl24 ((val), (ptr));	\
    588 .  while (0)
    589 .
    590 .bfd_vma bfd_getb24 (const void *p);
    591 .bfd_vma bfd_getl24 (const void *p);
    592 .
    593 .#define bfd_get_24(abfd, ptr) \
    594 .  (bfd_big_endian (abfd) ? bfd_getb24 (ptr) : bfd_getl24 (ptr))
    595 .
    596 .#define bfd_put_32(abfd, val, ptr) \
    597 .  BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
    598 .#define bfd_put_signed_32 \
    599 .  bfd_put_32
    600 .#define bfd_get_32(abfd, ptr) \
    601 .  BFD_SEND (abfd, bfd_getx32, (ptr))
    602 .#define bfd_get_signed_32(abfd, ptr) \
    603 .  BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
    604 .
    605 .#define bfd_put_64(abfd, val, ptr) \
    606 .  BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
    607 .#define bfd_put_signed_64 \
    608 .  bfd_put_64
    609 .#define bfd_get_64(abfd, ptr) \
    610 .  BFD_SEND (abfd, bfd_getx64, (ptr))
    611 .#define bfd_get_signed_64(abfd, ptr) \
    612 .  BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
    613 .
    614 .#define bfd_get(bits, abfd, ptr)			\
    615 .  ((bits) == 8 ? bfd_get_8 (abfd, ptr)			\
    616 .   : (bits) == 16 ? bfd_get_16 (abfd, ptr)		\
    617 .   : (bits) == 32 ? bfd_get_32 (abfd, ptr)		\
    618 .   : (bits) == 64 ? bfd_get_64 (abfd, ptr)		\
    619 .   : (abort (), (bfd_vma) - 1))
    620 .
    621 .#define bfd_put(bits, abfd, val, ptr)			\
    622 .  ((bits) == 8 ? bfd_put_8  (abfd, val, ptr)		\
    623 .   : (bits) == 16 ? bfd_put_16 (abfd, val, ptr)	\
    624 .   : (bits) == 32 ? bfd_put_32 (abfd, val, ptr)	\
    625 .   : (bits) == 64 ? bfd_put_64 (abfd, val, ptr)	\
    626 .   : (abort (), (void) 0))
    627 .
    628 */
    629 
    630 /*
    631 FUNCTION
    632 	bfd_h_put_size
    633 	bfd_h_get_size
    634 
    635 DESCRIPTION
    636 	These macros have the same function as their <<bfd_get_x>>
    637 	brethren, except that they are used for removing information
    638 	for the header records of object files. Believe it or not,
    639 	some object files keep their header records in big endian
    640 	order and their data in little endian order.
    641 .
    642 .{* Byte swapping macros for file header data.  *}
    643 .
    644 .#define bfd_h_put_8(abfd, val, ptr) \
    645 .  bfd_put_8 (abfd, val, ptr)
    646 .#define bfd_h_put_signed_8(abfd, val, ptr) \
    647 .  bfd_put_8 (abfd, val, ptr)
    648 .#define bfd_h_get_8(abfd, ptr) \
    649 .  bfd_get_8 (abfd, ptr)
    650 .#define bfd_h_get_signed_8(abfd, ptr) \
    651 .  bfd_get_signed_8 (abfd, ptr)
    652 .
    653 .#define bfd_h_put_16(abfd, val, ptr) \
    654 .  BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
    655 .#define bfd_h_put_signed_16 \
    656 .  bfd_h_put_16
    657 .#define bfd_h_get_16(abfd, ptr) \
    658 .  BFD_SEND (abfd, bfd_h_getx16, (ptr))
    659 .#define bfd_h_get_signed_16(abfd, ptr) \
    660 .  BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
    661 .
    662 .#define bfd_h_put_32(abfd, val, ptr) \
    663 .  BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
    664 .#define bfd_h_put_signed_32 \
    665 .  bfd_h_put_32
    666 .#define bfd_h_get_32(abfd, ptr) \
    667 .  BFD_SEND (abfd, bfd_h_getx32, (ptr))
    668 .#define bfd_h_get_signed_32(abfd, ptr) \
    669 .  BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
    670 .
    671 .#define bfd_h_put_64(abfd, val, ptr) \
    672 .  BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
    673 .#define bfd_h_put_signed_64 \
    674 .  bfd_h_put_64
    675 .#define bfd_h_get_64(abfd, ptr) \
    676 .  BFD_SEND (abfd, bfd_h_getx64, (ptr))
    677 .#define bfd_h_get_signed_64(abfd, ptr) \
    678 .  BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
    679 .
    680 .{* Aliases for the above, which should eventually go away.  *}
    681 .
    682 .#define H_PUT_64  bfd_h_put_64
    683 .#define H_PUT_32  bfd_h_put_32
    684 .#define H_PUT_16  bfd_h_put_16
    685 .#define H_PUT_8   bfd_h_put_8
    686 .#define H_PUT_S64 bfd_h_put_signed_64
    687 .#define H_PUT_S32 bfd_h_put_signed_32
    688 .#define H_PUT_S16 bfd_h_put_signed_16
    689 .#define H_PUT_S8  bfd_h_put_signed_8
    690 .#define H_GET_64  bfd_h_get_64
    691 .#define H_GET_32  bfd_h_get_32
    692 .#define H_GET_16  bfd_h_get_16
    693 .#define H_GET_8   bfd_h_get_8
    694 .#define H_GET_S64 bfd_h_get_signed_64
    695 .#define H_GET_S32 bfd_h_get_signed_32
    696 .#define H_GET_S16 bfd_h_get_signed_16
    697 .#define H_GET_S8  bfd_h_get_signed_8
    698 .
    699 .*/
    700 
    701 /* Sign extension to bfd_signed_vma.  */
    702 #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000)
    703 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
    704 #define COERCE64(x) \
    705   (((uint64_t) (x) ^ ((uint64_t) 1 << 63)) - ((uint64_t) 1 << 63))
    706 
    707 /*
    708 FUNCTION
    709 	Byte swapping routines.
    710 
    711 SYNOPSIS
    712 	uint64_t bfd_getb64 (const void *);
    713 	uint64_t bfd_getl64 (const void *);
    714 	int64_t bfd_getb_signed_64 (const void *);
    715 	int64_t bfd_getl_signed_64 (const void *);
    716 	bfd_vma bfd_getb32 (const void *);
    717 	bfd_vma bfd_getl32 (const void *);
    718 	bfd_signed_vma bfd_getb_signed_32 (const void *);
    719 	bfd_signed_vma bfd_getl_signed_32 (const void *);
    720 	bfd_vma bfd_getb16 (const void *);
    721 	bfd_vma bfd_getl16 (const void *);
    722 	bfd_signed_vma bfd_getb_signed_16 (const void *);
    723 	bfd_signed_vma bfd_getl_signed_16 (const void *);
    724 	void bfd_putb64 (uint64_t, void *);
    725 	void bfd_putl64 (uint64_t, void *);
    726 	void bfd_putb32 (bfd_vma, void *);
    727 	void bfd_putl32 (bfd_vma, void *);
    728 	void bfd_putb24 (bfd_vma, void *);
    729 	void bfd_putl24 (bfd_vma, void *);
    730 	void bfd_putb16 (bfd_vma, void *);
    731 	void bfd_putl16 (bfd_vma, void *);
    732 	uint64_t bfd_get_bits (const void *, int, bool);
    733 	void bfd_put_bits (uint64_t, void *, int, bool);
    734 
    735 DESCRIPTION
    736 	Read and write integers in a particular endian order.  getb
    737 	and putb functions handle big-endian, getl and putl handle
    738 	little-endian.  bfd_get_bits and bfd_put_bits specify
    739 	big-endian by passing TRUE in the last parameter,
    740 	little-endian by passing FALSE.
    741 */
    742 
    743 bfd_vma
    744 bfd_getb16 (const void *p)
    745 {
    746   const bfd_byte *addr = (const bfd_byte *) p;
    747   return (addr[0] << 8) | addr[1];
    748 }
    749 
    750 bfd_vma
    751 bfd_getl16 (const void *p)
    752 {
    753   const bfd_byte *addr = (const bfd_byte *) p;
    754   return (addr[1] << 8) | addr[0];
    755 }
    756 
    757 bfd_signed_vma
    758 bfd_getb_signed_16 (const void *p)
    759 {
    760   const bfd_byte *addr = (const bfd_byte *) p;
    761   return COERCE16 ((addr[0] << 8) | addr[1]);
    762 }
    763 
    764 bfd_signed_vma
    765 bfd_getl_signed_16 (const void *p)
    766 {
    767   const bfd_byte *addr = (const bfd_byte *) p;
    768   return COERCE16 ((addr[1] << 8) | addr[0]);
    769 }
    770 
    771 void
    772 bfd_putb16 (bfd_vma data, void *p)
    773 {
    774   bfd_byte *addr = (bfd_byte *) p;
    775   addr[0] = (data >> 8) & 0xff;
    776   addr[1] = data & 0xff;
    777 }
    778 
    779 void
    780 bfd_putl16 (bfd_vma data, void *p)
    781 {
    782   bfd_byte *addr = (bfd_byte *) p;
    783   addr[0] = data & 0xff;
    784   addr[1] = (data >> 8) & 0xff;
    785 }
    786 
    787 void
    788 bfd_putb24 (bfd_vma data, void *p)
    789 {
    790   bfd_byte *addr = (bfd_byte *) p;
    791   addr[0] = (data >> 16) & 0xff;
    792   addr[1] = (data >> 8) & 0xff;
    793   addr[2] = data & 0xff;
    794 }
    795 
    796 void
    797 bfd_putl24 (bfd_vma data, void *p)
    798 {
    799   bfd_byte *addr = (bfd_byte *) p;
    800   addr[0] = data & 0xff;
    801   addr[1] = (data >> 8) & 0xff;
    802   addr[2] = (data >> 16) & 0xff;
    803 }
    804 
    805 bfd_vma
    806 bfd_getb24 (const void *p)
    807 {
    808   const bfd_byte *addr = (const bfd_byte *) p;
    809   uint32_t v;
    810 
    811   v =  (uint32_t) addr[0] << 16;
    812   v |= (uint32_t) addr[1] << 8;
    813   v |= (uint32_t) addr[2];
    814   return v;
    815 }
    816 
    817 bfd_vma
    818 bfd_getl24 (const void *p)
    819 {
    820   const bfd_byte *addr = (const bfd_byte *) p;
    821   uint32_t v;
    822 
    823   v = (uint32_t) addr[0];
    824   v |= (uint32_t) addr[1] << 8;
    825   v |= (uint32_t) addr[2] << 16;
    826   return v;
    827 }
    828 
    829 bfd_vma
    830 bfd_getb32 (const void *p)
    831 {
    832   const bfd_byte *addr = (const bfd_byte *) p;
    833   uint32_t v;
    834 
    835   v = (uint32_t) addr[0] << 24;
    836   v |= (uint32_t) addr[1] << 16;
    837   v |= (uint32_t) addr[2] << 8;
    838   v |= (uint32_t) addr[3];
    839   return v;
    840 }
    841 
    842 bfd_vma
    843 bfd_getl32 (const void *p)
    844 {
    845   const bfd_byte *addr = (const bfd_byte *) p;
    846   uint32_t v;
    847 
    848   v = (uint32_t) addr[0];
    849   v |= (uint32_t) addr[1] << 8;
    850   v |= (uint32_t) addr[2] << 16;
    851   v |= (uint32_t) addr[3] << 24;
    852   return v;
    853 }
    854 
    855 bfd_signed_vma
    856 bfd_getb_signed_32 (const void *p)
    857 {
    858   const bfd_byte *addr = (const bfd_byte *) p;
    859   uint32_t v;
    860 
    861   v = (uint32_t) addr[0] << 24;
    862   v |= (uint32_t) addr[1] << 16;
    863   v |= (uint32_t) addr[2] << 8;
    864   v |= (uint32_t) addr[3];
    865   return COERCE32 (v);
    866 }
    867 
    868 bfd_signed_vma
    869 bfd_getl_signed_32 (const void *p)
    870 {
    871   const bfd_byte *addr = (const bfd_byte *) p;
    872   uint32_t v;
    873 
    874   v = (uint32_t) addr[0];
    875   v |= (uint32_t) addr[1] << 8;
    876   v |= (uint32_t) addr[2] << 16;
    877   v |= (uint32_t) addr[3] << 24;
    878   return COERCE32 (v);
    879 }
    880 
    881 uint64_t
    882 bfd_getb64 (const void *p)
    883 {
    884   const bfd_byte *addr = (const bfd_byte *) p;
    885   uint64_t v;
    886 
    887   v  = addr[0]; v <<= 8;
    888   v |= addr[1]; v <<= 8;
    889   v |= addr[2]; v <<= 8;
    890   v |= addr[3]; v <<= 8;
    891   v |= addr[4]; v <<= 8;
    892   v |= addr[5]; v <<= 8;
    893   v |= addr[6]; v <<= 8;
    894   v |= addr[7];
    895 
    896   return v;
    897 }
    898 
    899 uint64_t
    900 bfd_getl64 (const void *p)
    901 {
    902   const bfd_byte *addr = (const bfd_byte *) p;
    903   uint64_t v;
    904 
    905   v  = addr[7]; v <<= 8;
    906   v |= addr[6]; v <<= 8;
    907   v |= addr[5]; v <<= 8;
    908   v |= addr[4]; v <<= 8;
    909   v |= addr[3]; v <<= 8;
    910   v |= addr[2]; v <<= 8;
    911   v |= addr[1]; v <<= 8;
    912   v |= addr[0];
    913 
    914   return v;
    915 }
    916 
    917 int64_t
    918 bfd_getb_signed_64 (const void *p)
    919 {
    920   const bfd_byte *addr = (const bfd_byte *) p;
    921   uint64_t v;
    922 
    923   v  = addr[0]; v <<= 8;
    924   v |= addr[1]; v <<= 8;
    925   v |= addr[2]; v <<= 8;
    926   v |= addr[3]; v <<= 8;
    927   v |= addr[4]; v <<= 8;
    928   v |= addr[5]; v <<= 8;
    929   v |= addr[6]; v <<= 8;
    930   v |= addr[7];
    931 
    932   return COERCE64 (v);
    933 }
    934 
    935 int64_t
    936 bfd_getl_signed_64 (const void *p)
    937 {
    938   const bfd_byte *addr = (const bfd_byte *) p;
    939   uint64_t v;
    940 
    941   v  = addr[7]; v <<= 8;
    942   v |= addr[6]; v <<= 8;
    943   v |= addr[5]; v <<= 8;
    944   v |= addr[4]; v <<= 8;
    945   v |= addr[3]; v <<= 8;
    946   v |= addr[2]; v <<= 8;
    947   v |= addr[1]; v <<= 8;
    948   v |= addr[0];
    949 
    950   return COERCE64 (v);
    951 }
    952 
    953 void
    954 bfd_putb32 (bfd_vma data, void *p)
    955 {
    956   bfd_byte *addr = (bfd_byte *) p;
    957   addr[0] = (data >> 24) & 0xff;
    958   addr[1] = (data >> 16) & 0xff;
    959   addr[2] = (data >>  8) & 0xff;
    960   addr[3] = data & 0xff;
    961 }
    962 
    963 void
    964 bfd_putl32 (bfd_vma data, void *p)
    965 {
    966   bfd_byte *addr = (bfd_byte *) p;
    967   addr[0] = data & 0xff;
    968   addr[1] = (data >>  8) & 0xff;
    969   addr[2] = (data >> 16) & 0xff;
    970   addr[3] = (data >> 24) & 0xff;
    971 }
    972 
    973 void
    974 bfd_putb64 (uint64_t data, void *p)
    975 {
    976   bfd_byte *addr = (bfd_byte *) p;
    977   addr[0] = (data >> (7*8)) & 0xff;
    978   addr[1] = (data >> (6*8)) & 0xff;
    979   addr[2] = (data >> (5*8)) & 0xff;
    980   addr[3] = (data >> (4*8)) & 0xff;
    981   addr[4] = (data >> (3*8)) & 0xff;
    982   addr[5] = (data >> (2*8)) & 0xff;
    983   addr[6] = (data >> (1*8)) & 0xff;
    984   addr[7] = (data >> (0*8)) & 0xff;
    985 }
    986 
    987 void
    988 bfd_putl64 (uint64_t data, void *p)
    989 {
    990   bfd_byte *addr = (bfd_byte *) p;
    991   addr[7] = (data >> (7*8)) & 0xff;
    992   addr[6] = (data >> (6*8)) & 0xff;
    993   addr[5] = (data >> (5*8)) & 0xff;
    994   addr[4] = (data >> (4*8)) & 0xff;
    995   addr[3] = (data >> (3*8)) & 0xff;
    996   addr[2] = (data >> (2*8)) & 0xff;
    997   addr[1] = (data >> (1*8)) & 0xff;
    998   addr[0] = (data >> (0*8)) & 0xff;
    999 }
   1000 
   1001 void
   1002 bfd_put_bits (uint64_t data, void *p, int bits, bool big_p)
   1003 {
   1004   bfd_byte *addr = (bfd_byte *) p;
   1005   int i;
   1006   int bytes;
   1007 
   1008   if (bits % 8 != 0)
   1009     abort ();
   1010 
   1011   bytes = bits / 8;
   1012   for (i = 0; i < bytes; i++)
   1013     {
   1014       int addr_index = big_p ? bytes - i - 1 : i;
   1015 
   1016       addr[addr_index] = data & 0xff;
   1017       data >>= 8;
   1018     }
   1019 }
   1020 
   1021 uint64_t
   1022 bfd_get_bits (const void *p, int bits, bool big_p)
   1023 {
   1024   const bfd_byte *addr = (const bfd_byte *) p;
   1025   uint64_t data;
   1026   int i;
   1027   int bytes;
   1028 
   1029   if (bits % 8 != 0)
   1030     abort ();
   1031 
   1032   data = 0;
   1033   bytes = bits / 8;
   1034   for (i = 0; i < bytes; i++)
   1035     {
   1036       int addr_index = big_p ? i : bytes - i - 1;
   1037 
   1038       data = (data << 8) | addr[addr_index];
   1039     }
   1040 
   1041   return data;
   1042 }
   1043 
   1044 #ifdef USE_MMAP
   1046 /* Allocate a page to track mmapped memory and return the page and
   1047    the first entry.  Return NULL if mmap fails.  */
   1048 
   1049 static struct bfd_mmapped *
   1050 bfd_allocate_mmapped_page (bfd *abfd, struct bfd_mmapped_entry **entry)
   1051 {
   1052   struct bfd_mmapped * mmapped
   1053     = (struct bfd_mmapped *) mmap (NULL, _bfd_pagesize,
   1054 				   PROT_READ | PROT_WRITE,
   1055 				   MAP_PRIVATE | MAP_ANONYMOUS,
   1056 				   -1, 0);
   1057   if (mmapped == MAP_FAILED)
   1058     return NULL;
   1059 
   1060   mmapped->next = abfd->mmapped;
   1061   mmapped->max_entry
   1062     = ((_bfd_pagesize - offsetof (struct bfd_mmapped, entries))
   1063        / sizeof (struct bfd_mmapped_entry));
   1064   mmapped->next_entry = 1;
   1065   abfd->mmapped = mmapped;
   1066   *entry = mmapped->entries;
   1067   return mmapped;
   1068 }
   1069 
   1070 /* Mmap a memory region of RSIZE bytes at the current file offset.
   1071    Return mmap address and size in MAP_ADDR and MAP_SIZE.  Return NULL
   1072    on invalid input and MAP_FAILED for mmap failure.  */
   1073 
   1074 static void *
   1075 bfd_mmap_local (bfd *abfd, size_t rsize, void **map_addr, size_t *map_size)
   1076 {
   1077   /* We mmap on the underlying file.  In an archive it might be nice
   1078      to limit RSIZE to the element size, but that can be fuzzed and
   1079      the offset returned by bfd_tell is relative to the start of the
   1080      element.  Therefore to reliably stop access beyond the end of a
   1081      file (and resulting bus errors) we must work with the underlying
   1082      file offset and size, and trust that callers will limit access to
   1083      within an archive element.  */
   1084   while (abfd->my_archive != NULL
   1085 	 && !bfd_is_thin_archive (abfd->my_archive))
   1086     abfd = abfd->my_archive;
   1087 
   1088   ufile_ptr filesize = bfd_get_size (abfd);
   1089   ufile_ptr offset = bfd_tell (abfd);
   1090   if (filesize < offset || filesize - offset < rsize)
   1091     {
   1092       bfd_set_error (bfd_error_file_truncated);
   1093       return NULL;
   1094     }
   1095 
   1096   void *mem;
   1097   mem = bfd_mmap (abfd, NULL, rsize, PROT_READ | PROT_WRITE, MAP_PRIVATE,
   1098 		  offset, map_addr, map_size);
   1099   return mem;
   1100 }
   1101 
   1102 /* Mmap a memory region of RSIZE bytes at the current offset.
   1103    Return mmap address and size in MAP_ADDR and MAP_SIZE.  Return NULL
   1104    on invalid input and MAP_FAILED for mmap failure.  */
   1105 
   1106 void *
   1107 _bfd_mmap_temporary (bfd *abfd, size_t rsize, void **map_addr,
   1108 		     size_t *map_size)
   1109 {
   1110   /* Use mmap only if section size >= the minimum mmap section size.  */
   1111   if (rsize < _bfd_minimum_mmap_size)
   1112     {
   1113       void *mem = _bfd_malloc_and_read (abfd, rsize, rsize);
   1114       /* NB: Set *MAP_ADDR to MEM and *MAP_SIZE to 0 to indicate that
   1115 	 _bfd_malloc_and_read is called.  */
   1116       *map_addr = mem;
   1117       *map_size = 0;
   1118       return mem;
   1119     }
   1120 
   1121   return bfd_mmap_local (abfd, rsize, map_addr, map_size);
   1122 }
   1123 
   1124 /* Munmap RSIZE bytes at PTR.  */
   1125 
   1126 void
   1127 _bfd_munmap_temporary (void *ptr, size_t rsize)
   1128 {
   1129   /* NB: Since _bfd_munmap_temporary is called like free, PTR may be
   1130      NULL.  Otherwise, PTR and RSIZE must be valid.  If RSIZE is 0,
   1131      free is called.  */
   1132   if (ptr == NULL)
   1133     return;
   1134   if (rsize != 0)
   1135     {
   1136       if (munmap (ptr, rsize) != 0)
   1137 	abort ();
   1138     }
   1139   else
   1140     free (ptr);
   1141 }
   1142 
   1143 /* Mmap a memory region of RSIZE bytes at the current offset.
   1144    Return NULL on invalid input or mmap failure.  */
   1145 
   1146 void *
   1147 _bfd_mmap_persistent (bfd *abfd, size_t rsize)
   1148 {
   1149   /* Use mmap only if section size >= the minimum mmap section size.  */
   1150   if (rsize < _bfd_minimum_mmap_size)
   1151     return _bfd_alloc_and_read (abfd, rsize, rsize);
   1152 
   1153   void *mem, *map_addr;
   1154   size_t map_size;
   1155   mem = bfd_mmap_local (abfd, rsize, &map_addr, &map_size);
   1156   if (mem == NULL)
   1157     return mem;
   1158   if (mem == MAP_FAILED)
   1159     return _bfd_alloc_and_read (abfd, rsize, rsize);
   1160 
   1161   struct bfd_mmapped_entry *entry;
   1162   unsigned int next_entry;
   1163   struct bfd_mmapped *mmapped = abfd->mmapped;
   1164   if (mmapped != NULL
   1165       && (next_entry = mmapped->next_entry) < mmapped->max_entry)
   1166     {
   1167       entry = &mmapped->entries[next_entry];
   1168       mmapped->next_entry++;
   1169     }
   1170   else
   1171     {
   1172       mmapped = bfd_allocate_mmapped_page (abfd, &entry);
   1173       if (mmapped == NULL)
   1174 	{
   1175 	  munmap (map_addr, map_size);
   1176 	  return NULL;
   1177 	}
   1178     }
   1179 
   1180   entry->addr = map_addr;
   1181   entry->size = map_size;
   1182 
   1183   return mem;
   1184 }
   1185 #endif
   1186 
   1187 /* Attempt to read *SIZE_P bytes from ABFD's iostream to *DATA_P.
   1188    Return true if the full the amount has been read.  If *DATA_P is
   1189    NULL, mmap should be used, return the memory address at the
   1190    current offset in *DATA_P as well as return mmap address and size
   1191    in *MMAP_BASE and *SIZE_P.  Otherwise, return NULL in *MMAP_BASE
   1192    and 0 in *SIZE_P.  If FINAL_LINK is true, this is called from
   1193    elf_link_read_relocs_from_section.  */
   1194 
   1195 bool
   1196 _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
   1197 			  void **mmap_base, bfd *abfd,
   1198 			  bool final_link ATTRIBUTE_UNUSED)
   1199 {
   1200   void *data = *data_p;
   1201   size_t size = *size_p;
   1202 
   1203 #ifdef USE_MMAP
   1204   /* NB: When FINAL_LINK is true, the size of the preallocated buffer
   1205      is _bfd_minimum_mmap_size and use mmap if the data size >=
   1206      _bfd_minimum_mmap_size.  Otherwise, use mmap if ABFD isn't an IR
   1207      input or the data size >= _bfd_minimum_mmap_size.  */
   1208   bool use_mmmap;
   1209   bool mmap_size = size >= _bfd_minimum_mmap_size;
   1210   if (final_link)
   1211     use_mmmap = mmap_size;
   1212   else
   1213     use_mmmap = (mmap_size
   1214 		 && data == NULL
   1215 		 && (abfd->flags & BFD_PLUGIN) == 0);
   1216   if (use_mmmap)
   1217     {
   1218       void *mmaped = _bfd_mmap_temporary (abfd, size, mmap_base, size_p);
   1219       /* MAP_FAILED is returned when called from GDB on an object with
   1220 	 opncls_iovec.  Use bfd_read in this case.  */
   1221       if (mmaped != MAP_FAILED)
   1222 	{
   1223 	  if (mmaped == NULL)
   1224 	    abort ();
   1225 	  *data_p = mmaped;
   1226 	  return true;
   1227 	}
   1228     }
   1229 #endif
   1230 
   1231   if (data == NULL)
   1232     {
   1233       data = bfd_malloc (size);
   1234       if (data == NULL)
   1235 	return false;
   1236       *data_p = data;
   1237       /* NB: _bfd_munmap_temporary will free *MMAP_BASE if *SIZE_P == 0.  */
   1238       *mmap_base = data;
   1239     }
   1240   else
   1241     *mmap_base = NULL;
   1242   *size_p = 0;
   1243   return bfd_read (data, size, abfd) == size;
   1244 }
   1245 
   1246 /* Default implementation */
   1247 
   1248 bool
   1249 _bfd_generic_get_section_contents (bfd *abfd,
   1250 				   sec_ptr section,
   1251 				   void *location,
   1252 				   file_ptr offset,
   1253 				   bfd_size_type count)
   1254 {
   1255   bfd_size_type sz;
   1256   if (count == 0)
   1257     return true;
   1258 
   1259   if (section->compress_status != COMPRESS_SECTION_NONE)
   1260     {
   1261       _bfd_error_handler
   1262 	/* xgettext:c-format */
   1263 	(_("%pB: unable to get decompressed section %pA"),
   1264 	 abfd, section);
   1265       bfd_set_error (bfd_error_invalid_operation);
   1266       return false;
   1267     }
   1268 
   1269 #ifdef USE_MMAP
   1270   if (section->mmapped_p
   1271       && (section->contents != NULL || location != NULL))
   1272     {
   1273       _bfd_error_handler
   1274 	/* xgettext:c-format */
   1275 	(_("%pB: mapped section %pA has non-NULL buffer"),
   1276 	 abfd, section);
   1277       bfd_set_error (bfd_error_invalid_operation);
   1278       return false;
   1279     }
   1280 #endif
   1281 
   1282   sz = bfd_get_section_limit_octets (abfd, section);
   1283   if (offset + count < count
   1284       || offset + count > sz
   1285       || (abfd->my_archive != NULL
   1286 	  && !bfd_is_thin_archive (abfd->my_archive)
   1287 	  && ((ufile_ptr) section->filepos + offset + count
   1288 	      > arelt_size (abfd))))
   1289     {
   1290       bfd_set_error (bfd_error_invalid_operation);
   1291       return false;
   1292     }
   1293 
   1294   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
   1295     return false;
   1296 
   1297 #ifdef USE_MMAP
   1298   if (section->mmapped_p)
   1299     {
   1300       if (location != 0
   1301 	  || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
   1302 	abort ();
   1303 
   1304       location = bfd_mmap_local (abfd, count,
   1305 				 &elf_section_data (section)->contents_addr,
   1306 				 &elf_section_data (section)->contents_size);
   1307 
   1308       if (location == NULL)
   1309 	return false;
   1310 
   1311       /* Check for iovec not supporting mmap.  */
   1312       if (location != MAP_FAILED)
   1313 	{
   1314 	  section->contents = location;
   1315 	  return true;
   1316 	}
   1317 
   1318       /* Malloc the buffer and call bfd_read.  */
   1319       location = (bfd_byte *) bfd_malloc (count);
   1320       if (location == NULL)
   1321 	{
   1322 	  if (bfd_get_error () == bfd_error_no_memory)
   1323 	    _bfd_error_handler
   1324 	      /* xgettext:c-format */
   1325 	      (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
   1326 	       abfd, section, (uint64_t) count);
   1327 	  return false;
   1328 	}
   1329       section->contents = location;
   1330     }
   1331 #endif
   1332 
   1333   if (bfd_read (location, count, abfd) != count)
   1334     return false;
   1335 
   1336   return true;
   1337 }
   1338 
   1339 /* This generic function can only be used in implementations where creating
   1340    NEW sections is disallowed.  It is useful in patching existing sections
   1341    in read-write files, though.  See other set_section_contents functions
   1342    to see why it doesn't work for new sections.  */
   1343 bool
   1344 _bfd_generic_set_section_contents (bfd *abfd,
   1345 				   sec_ptr section,
   1346 				   const void *location,
   1347 				   file_ptr offset,
   1348 				   bfd_size_type count)
   1349 {
   1350   if (count == 0)
   1351     return true;
   1352 
   1353   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
   1354       || bfd_write (location, count, abfd) != count)
   1355     return false;
   1356 
   1357   return true;
   1358 }
   1359 
   1360 /*
   1361 INTERNAL_FUNCTION
   1362 	bfd_log2
   1363 
   1364 SYNOPSIS
   1365 	unsigned int bfd_log2 (bfd_vma x);
   1366 
   1367 DESCRIPTION
   1368 	Return the log base 2 of the value supplied, rounded up.  E.g., an
   1369 	@var{x} of 1025 returns 11.  A @var{x} of 0 returns 0.
   1370 */
   1371 
   1372 unsigned int
   1373 bfd_log2 (bfd_vma x)
   1374 {
   1375   unsigned int result = 0;
   1376 
   1377   if (x <= 1)
   1378     return result;
   1379   --x;
   1380   do
   1381     ++result;
   1382   while ((x >>= 1) != 0);
   1383   return result;
   1384 }
   1385 
   1386 bool
   1387 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
   1388 {
   1389   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
   1390 
   1391   return name[0] == locals_prefix;
   1392 }
   1393 
   1394 /* Helper function for reading uleb128 encoded data.  */
   1395 
   1396 bfd_vma
   1397 _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
   1398 			   bfd_byte *buf,
   1399 			   unsigned int *bytes_read_ptr)
   1400 {
   1401   bfd_vma result;
   1402   unsigned int num_read;
   1403   unsigned int shift;
   1404   bfd_byte byte;
   1405 
   1406   result = 0;
   1407   shift = 0;
   1408   num_read = 0;
   1409   do
   1410     {
   1411       byte = bfd_get_8 (abfd, buf);
   1412       buf++;
   1413       num_read++;
   1414       if (shift < 8 * sizeof (result))
   1415 	{
   1416 	  result |= (((bfd_vma) byte & 0x7f) << shift);
   1417 	  shift += 7;
   1418 	}
   1419     }
   1420   while (byte & 0x80);
   1421   *bytes_read_ptr = num_read;
   1422   return result;
   1423 }
   1424 
   1425 /* Read in a LEB128 encoded value from ABFD starting at *PTR.
   1426    If SIGN is true, return a signed LEB128 value.
   1427    *PTR is incremented by the number of bytes read.
   1428    No bytes will be read at address END or beyond.  */
   1429 
   1430 bfd_vma
   1431 _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
   1432 		       bfd_byte **ptr,
   1433 		       bool sign,
   1434 		       const bfd_byte * const end)
   1435 {
   1436   bfd_vma result = 0;
   1437   unsigned int shift = 0;
   1438   bfd_byte byte = 0;
   1439   bfd_byte *data = *ptr;
   1440 
   1441   while (data < end)
   1442     {
   1443       byte = bfd_get_8 (abfd, data);
   1444       data++;
   1445       if (shift < 8 * sizeof (result))
   1446 	{
   1447 	  result |= ((bfd_vma) (byte & 0x7f)) << shift;
   1448 	  shift += 7;
   1449 	}
   1450       if ((byte & 0x80) == 0)
   1451 	break;
   1452     }
   1453 
   1454   *ptr = data;
   1455 
   1456   if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
   1457     result |= -((bfd_vma) 1 << shift);
   1458 
   1459   return result;
   1460 }
   1461 
   1462 /* Helper function for reading sleb128 encoded data.  */
   1463 
   1464 bfd_signed_vma
   1465 _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
   1466 			 bfd_byte *buf,
   1467 			 unsigned int *bytes_read_ptr)
   1468 {
   1469   bfd_vma result;
   1470   unsigned int shift;
   1471   unsigned int num_read;
   1472   bfd_byte byte;
   1473 
   1474   result = 0;
   1475   shift = 0;
   1476   num_read = 0;
   1477   do
   1478     {
   1479       byte = bfd_get_8 (abfd, buf);
   1480       buf ++;
   1481       num_read ++;
   1482       if (shift < 8 * sizeof (result))
   1483 	{
   1484 	  result |= (((bfd_vma) byte & 0x7f) << shift);
   1485 	  shift += 7;
   1486 	}
   1487     }
   1488   while (byte & 0x80);
   1489   if (shift < 8 * sizeof (result) && (byte & 0x40))
   1490     result |= (((bfd_vma) -1) << shift);
   1491   *bytes_read_ptr = num_read;
   1492   return result;
   1493 }
   1494 
   1495 /* Write VAL in uleb128 format to P.
   1496    END indicates the last byte of allocated space for the uleb128 value to fit
   1497    in.
   1498    Return a pointer to the byte following the last byte that was written, or
   1499    NULL if the uleb128 value does not fit in the allocated space between P and
   1500    END.  */
   1501 bfd_byte *
   1502 _bfd_write_unsigned_leb128 (bfd_byte *p, bfd_byte *end, bfd_vma val)
   1503 {
   1504   bfd_byte c;
   1505   do
   1506     {
   1507       if (p > end)
   1508 	return NULL;
   1509       c = val & 0x7f;
   1510       val >>= 7;
   1511       if (val)
   1512 	c |= 0x80;
   1513       *(p++) = c;
   1514     }
   1515   while (val);
   1516   return p;
   1517 }
   1518 
   1519 bool
   1520 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
   1521 					asection *isec ATTRIBUTE_UNUSED,
   1522 					bfd *obfd ATTRIBUTE_UNUSED,
   1523 					asection *osec ATTRIBUTE_UNUSED,
   1524 					struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
   1525 {
   1526   return true;
   1527 }
   1528 
   1529 #ifdef HAVE_MMAP
   1530 uintptr_t _bfd_pagesize;
   1531 uintptr_t _bfd_pagesize_m1;
   1532 uintptr_t _bfd_minimum_mmap_size;
   1533 
   1534 __attribute__ ((unused, constructor))
   1535 static void
   1536 bfd_init_pagesize (void)
   1537 {
   1538   _bfd_pagesize = getpagesize ();
   1539   if (_bfd_pagesize == 0)
   1540     abort ();
   1541   _bfd_pagesize_m1 = _bfd_pagesize - 1;
   1542   /* The minimum section size to use mmap.  */
   1543   _bfd_minimum_mmap_size = _bfd_pagesize * 4;
   1544 }
   1545 #endif
   1546