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