Back to index

cell-binutils  2.17cvs20070401
libbfd.c
Go to the documentation of this file.
00001 /* Assorted BFD support routines, only used internally.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
00003    2000, 2001, 2002, 2003, 2004, 2005, 2007
00004    Free Software Foundation, Inc.
00005    Written by Cygnus Support.
00006 
00007    This file is part of BFD, the Binary File Descriptor library.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libbfd.h"
00026 
00027 #ifndef HAVE_GETPAGESIZE
00028 #define getpagesize() 2048
00029 #endif
00030 
00031 /*
00032 SECTION
00033        Implementation details
00034 
00035 SUBSECTION
00036        Internal functions
00037 
00038 DESCRIPTION
00039        These routines are used within BFD.
00040        They are not intended for export, but are documented here for
00041        completeness.
00042 */
00043 
00044 /* A routine which is used in target vectors for unsupported
00045    operations.  */
00046 
00047 bfd_boolean
00048 bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
00049 {
00050   bfd_set_error (bfd_error_invalid_operation);
00051   return FALSE;
00052 }
00053 
00054 /* A routine which is used in target vectors for supported operations
00055    which do not actually do anything.  */
00056 
00057 bfd_boolean
00058 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
00059 {
00060   return TRUE;
00061 }
00062 
00063 /* A routine which is used in target vectors for unsupported
00064    operations which return a pointer value.  */
00065 
00066 void *
00067 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
00068 {
00069   bfd_set_error (bfd_error_invalid_operation);
00070   return NULL;
00071 }
00072 
00073 int
00074 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
00075 {
00076   return 0;
00077 }
00078 
00079 unsigned int
00080 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
00081 {
00082    return 0;
00083 }
00084 
00085 long
00086 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
00087 {
00088   return 0;
00089 }
00090 
00091 /* A routine which is used in target vectors for unsupported
00092    operations which return -1 on error.  */
00093 
00094 long
00095 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
00096 {
00097   bfd_set_error (bfd_error_invalid_operation);
00098   return -1;
00099 }
00100 
00101 void
00102 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
00103 {
00104 }
00105 
00106 long
00107 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
00108                                  asection *sec ATTRIBUTE_UNUSED)
00109 {
00110   return sizeof (arelent *);
00111 }
00112 
00113 long
00114 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
00115                               asection *sec ATTRIBUTE_UNUSED,
00116                               arelent **relptr,
00117                               asymbol **symbols ATTRIBUTE_UNUSED)
00118 {
00119   *relptr = NULL;
00120   return 0;
00121 }
00122 
00123 bfd_boolean
00124 _bfd_nocore_core_file_matches_executable_p
00125   (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
00126    bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
00127 {
00128   bfd_set_error (bfd_error_invalid_operation);
00129   return FALSE;
00130 }
00131 
00132 /* Routine to handle core_file_failing_command entry point for targets
00133    without core file support.  */
00134 
00135 char *
00136 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
00137 {
00138   bfd_set_error (bfd_error_invalid_operation);
00139   return NULL;
00140 }
00141 
00142 /* Routine to handle core_file_failing_signal entry point for targets
00143    without core file support.  */
00144 
00145 int
00146 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
00147 {
00148   bfd_set_error (bfd_error_invalid_operation);
00149   return 0;
00150 }
00151 
00152 const bfd_target *
00153 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
00154 {
00155   bfd_set_error (bfd_error_wrong_format);
00156   return 0;
00157 }
00158 
00159 /* Allocate memory using malloc.  */
00160 
00161 void *
00162 bfd_malloc (bfd_size_type size)
00163 {
00164   void *ptr;
00165 
00166   if (size != (size_t) size)
00167     {
00168       bfd_set_error (bfd_error_no_memory);
00169       return NULL;
00170     }
00171 
00172   ptr = malloc ((size_t) size);
00173   if (ptr == NULL && (size_t) size != 0)
00174     bfd_set_error (bfd_error_no_memory);
00175 
00176   return ptr;
00177 }
00178 
00179 /* Allocate memory using malloc, nmemb * size with overflow checking.  */
00180 
00181 void *
00182 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
00183 {
00184   void *ptr;
00185 
00186   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
00187       && size != 0
00188       && nmemb > ~(bfd_size_type) 0 / size)
00189     {
00190       bfd_set_error (bfd_error_no_memory);
00191       return NULL;
00192     }
00193 
00194   size *= nmemb;
00195 
00196   if (size != (size_t) size)
00197     {
00198       bfd_set_error (bfd_error_no_memory);
00199       return NULL;
00200     }
00201 
00202   ptr = malloc ((size_t) size);
00203   if (ptr == NULL && (size_t) size != 0)
00204     bfd_set_error (bfd_error_no_memory);
00205 
00206   return ptr;
00207 }
00208 
00209 /* Reallocate memory using realloc.  */
00210 
00211 void *
00212 bfd_realloc (void *ptr, bfd_size_type size)
00213 {
00214   void *ret;
00215 
00216   if (size != (size_t) size)
00217     {
00218       bfd_set_error (bfd_error_no_memory);
00219       return NULL;
00220     }
00221 
00222   if (ptr == NULL)
00223     ret = malloc ((size_t) size);
00224   else
00225     ret = realloc (ptr, (size_t) size);
00226 
00227   if (ret == NULL && (size_t) size != 0)
00228     bfd_set_error (bfd_error_no_memory);
00229 
00230   return ret;
00231 }
00232 
00233 /* Reallocate memory using realloc, nmemb * size with overflow checking.  */
00234 
00235 void *
00236 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
00237 {
00238   void *ret;
00239 
00240   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
00241       && size != 0
00242       && nmemb > ~(bfd_size_type) 0 / size)
00243     {
00244       bfd_set_error (bfd_error_no_memory);
00245       return NULL;
00246     }
00247 
00248   size *= nmemb;
00249 
00250   if (size != (size_t) size)
00251     {
00252       bfd_set_error (bfd_error_no_memory);
00253       return NULL;
00254     }
00255 
00256   if (ptr == NULL)
00257     ret = malloc ((size_t) size);
00258   else
00259     ret = realloc (ptr, (size_t) size);
00260 
00261   if (ret == NULL && (size_t) size != 0)
00262     bfd_set_error (bfd_error_no_memory);
00263 
00264   return ret;
00265 }
00266 
00267 /* Allocate memory using malloc and clear it.  */
00268 
00269 void *
00270 bfd_zmalloc (bfd_size_type size)
00271 {
00272   void *ptr;
00273 
00274   if (size != (size_t) size)
00275     {
00276       bfd_set_error (bfd_error_no_memory);
00277       return NULL;
00278     }
00279 
00280   ptr = malloc ((size_t) size);
00281 
00282   if ((size_t) size != 0)
00283     {
00284       if (ptr == NULL)
00285        bfd_set_error (bfd_error_no_memory);
00286       else
00287        memset (ptr, 0, (size_t) size);
00288     }
00289 
00290   return ptr;
00291 }
00292 
00293 /* Allocate memory using malloc (nmemb * size) with overflow checking
00294    and clear it.  */
00295 
00296 void *
00297 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
00298 {
00299   void *ptr;
00300 
00301   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
00302       && size != 0
00303       && nmemb > ~(bfd_size_type) 0 / size)
00304     {
00305       bfd_set_error (bfd_error_no_memory);
00306       return NULL;
00307     }
00308 
00309   size *= nmemb;
00310 
00311   if (size != (size_t) size)
00312     {
00313       bfd_set_error (bfd_error_no_memory);
00314       return NULL;
00315     }
00316 
00317   ptr = malloc ((size_t) size);
00318 
00319   if ((size_t) size != 0)
00320     {
00321       if (ptr == NULL)
00322        bfd_set_error (bfd_error_no_memory);
00323       else
00324        memset (ptr, 0, (size_t) size);
00325     }
00326 
00327   return ptr;
00328 }
00329 
00330 /*
00331 INTERNAL_FUNCTION
00332        bfd_write_bigendian_4byte_int
00333 
00334 SYNOPSIS
00335        bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
00336 
00337 DESCRIPTION
00338        Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
00339        endian order regardless of what else is going on.  This is useful in
00340        archives.
00341 
00342 */
00343 bfd_boolean
00344 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
00345 {
00346   bfd_byte buffer[4];
00347   bfd_putb32 ((bfd_vma) i, buffer);
00348   return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
00349 }
00350 
00351 
00354 /* The middle letter e.g. get<b>short indicates Big or Little endian
00355    target machine.  It doesn't matter what the byte order of the host
00356    machine is; these routines work for either.  */
00357 
00358 /* FIXME: Should these take a count argument?
00359    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
00360                              functions in swap.h #ifdef __GNUC__.
00361                              Gprof them later and find out.  */
00362 
00363 /*
00364 FUNCTION
00365        bfd_put_size
00366 FUNCTION
00367        bfd_get_size
00368 
00369 DESCRIPTION
00370        These macros as used for reading and writing raw data in
00371        sections; each access (except for bytes) is vectored through
00372        the target format of the BFD and mangled accordingly. The
00373        mangling performs any necessary endian translations and
00374        removes alignment restrictions.  Note that types accepted and
00375        returned by these macros are identical so they can be swapped
00376        around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
00377        to either <<bfd_get_32>> or <<bfd_get_64>>.
00378 
00379        In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
00380        system without prototypes, the caller is responsible for making
00381        sure that is true, with a cast if necessary.  We don't cast
00382        them in the macro definitions because that would prevent <<lint>>
00383        or <<gcc -Wall>> from detecting sins such as passing a pointer.
00384        To detect calling these with less than a <<bfd_vma>>, use
00385        <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
00386 
00387 .
00388 .{* Byte swapping macros for user section data.  *}
00389 .
00390 .#define bfd_put_8(abfd, val, ptr) \
00391 .  ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
00392 .#define bfd_put_signed_8 \
00393 .  bfd_put_8
00394 .#define bfd_get_8(abfd, ptr) \
00395 .  (*(unsigned char *) (ptr) & 0xff)
00396 .#define bfd_get_signed_8(abfd, ptr) \
00397 .  (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
00398 .
00399 .#define bfd_put_16(abfd, val, ptr) \
00400 .  BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
00401 .#define bfd_put_signed_16 \
00402 .  bfd_put_16
00403 .#define bfd_get_16(abfd, ptr) \
00404 .  BFD_SEND (abfd, bfd_getx16, (ptr))
00405 .#define bfd_get_signed_16(abfd, ptr) \
00406 .  BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
00407 .
00408 .#define bfd_put_32(abfd, val, ptr) \
00409 .  BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
00410 .#define bfd_put_signed_32 \
00411 .  bfd_put_32
00412 .#define bfd_get_32(abfd, ptr) \
00413 .  BFD_SEND (abfd, bfd_getx32, (ptr))
00414 .#define bfd_get_signed_32(abfd, ptr) \
00415 .  BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
00416 .
00417 .#define bfd_put_64(abfd, val, ptr) \
00418 .  BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
00419 .#define bfd_put_signed_64 \
00420 .  bfd_put_64
00421 .#define bfd_get_64(abfd, ptr) \
00422 .  BFD_SEND (abfd, bfd_getx64, (ptr))
00423 .#define bfd_get_signed_64(abfd, ptr) \
00424 .  BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
00425 .
00426 .#define bfd_get(bits, abfd, ptr)                \
00427 .  ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr)       \
00428 .   : (bits) == 16 ? bfd_get_16 (abfd, ptr)             \
00429 .   : (bits) == 32 ? bfd_get_32 (abfd, ptr)             \
00430 .   : (bits) == 64 ? bfd_get_64 (abfd, ptr)             \
00431 .   : (abort (), (bfd_vma) - 1))
00432 .
00433 .#define bfd_put(bits, abfd, val, ptr)                  \
00434 .  ((bits) == 8 ? bfd_put_8  (abfd, val, ptr)           \
00435 .   : (bits) == 16 ? bfd_put_16 (abfd, val, ptr)        \
00436 .   : (bits) == 32 ? bfd_put_32 (abfd, val, ptr)        \
00437 .   : (bits) == 64 ? bfd_put_64 (abfd, val, ptr)        \
00438 .   : (abort (), (void) 0))
00439 .
00440 */
00441 
00442 /*
00443 FUNCTION
00444        bfd_h_put_size
00445        bfd_h_get_size
00446 
00447 DESCRIPTION
00448        These macros have the same function as their <<bfd_get_x>>
00449        brethren, except that they are used for removing information
00450        for the header records of object files. Believe it or not,
00451        some object files keep their header records in big endian
00452        order and their data in little endian order.
00453 .
00454 .{* Byte swapping macros for file header data.  *}
00455 .
00456 .#define bfd_h_put_8(abfd, val, ptr) \
00457 .  bfd_put_8 (abfd, val, ptr)
00458 .#define bfd_h_put_signed_8(abfd, val, ptr) \
00459 .  bfd_put_8 (abfd, val, ptr)
00460 .#define bfd_h_get_8(abfd, ptr) \
00461 .  bfd_get_8 (abfd, ptr)
00462 .#define bfd_h_get_signed_8(abfd, ptr) \
00463 .  bfd_get_signed_8 (abfd, ptr)
00464 .
00465 .#define bfd_h_put_16(abfd, val, ptr) \
00466 .  BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
00467 .#define bfd_h_put_signed_16 \
00468 .  bfd_h_put_16
00469 .#define bfd_h_get_16(abfd, ptr) \
00470 .  BFD_SEND (abfd, bfd_h_getx16, (ptr))
00471 .#define bfd_h_get_signed_16(abfd, ptr) \
00472 .  BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
00473 .
00474 .#define bfd_h_put_32(abfd, val, ptr) \
00475 .  BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
00476 .#define bfd_h_put_signed_32 \
00477 .  bfd_h_put_32
00478 .#define bfd_h_get_32(abfd, ptr) \
00479 .  BFD_SEND (abfd, bfd_h_getx32, (ptr))
00480 .#define bfd_h_get_signed_32(abfd, ptr) \
00481 .  BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
00482 .
00483 .#define bfd_h_put_64(abfd, val, ptr) \
00484 .  BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
00485 .#define bfd_h_put_signed_64 \
00486 .  bfd_h_put_64
00487 .#define bfd_h_get_64(abfd, ptr) \
00488 .  BFD_SEND (abfd, bfd_h_getx64, (ptr))
00489 .#define bfd_h_get_signed_64(abfd, ptr) \
00490 .  BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
00491 .
00492 .{* Aliases for the above, which should eventually go away.  *}
00493 .
00494 .#define H_PUT_64  bfd_h_put_64
00495 .#define H_PUT_32  bfd_h_put_32
00496 .#define H_PUT_16  bfd_h_put_16
00497 .#define H_PUT_8   bfd_h_put_8
00498 .#define H_PUT_S64 bfd_h_put_signed_64
00499 .#define H_PUT_S32 bfd_h_put_signed_32
00500 .#define H_PUT_S16 bfd_h_put_signed_16
00501 .#define H_PUT_S8  bfd_h_put_signed_8
00502 .#define H_GET_64  bfd_h_get_64
00503 .#define H_GET_32  bfd_h_get_32
00504 .#define H_GET_16  bfd_h_get_16
00505 .#define H_GET_8   bfd_h_get_8
00506 .#define H_GET_S64 bfd_h_get_signed_64
00507 .#define H_GET_S32 bfd_h_get_signed_32
00508 .#define H_GET_S16 bfd_h_get_signed_16
00509 .#define H_GET_S8  bfd_h_get_signed_8
00510 .
00511 .*/
00512 
00513 /* Sign extension to bfd_signed_vma.  */
00514 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
00515 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
00516 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
00517 #define COERCE64(x) \
00518   (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
00519 
00520 bfd_vma
00521 bfd_getb16 (const void *p)
00522 {
00523   const bfd_byte *addr = p;
00524   return (addr[0] << 8) | addr[1];
00525 }
00526 
00527 bfd_vma
00528 bfd_getl16 (const void *p)
00529 {
00530   const bfd_byte *addr = p;
00531   return (addr[1] << 8) | addr[0];
00532 }
00533 
00534 bfd_signed_vma
00535 bfd_getb_signed_16 (const void *p)
00536 {
00537   const bfd_byte *addr = p;
00538   return COERCE16 ((addr[0] << 8) | addr[1]);
00539 }
00540 
00541 bfd_signed_vma
00542 bfd_getl_signed_16 (const void *p)
00543 {
00544   const bfd_byte *addr = p;
00545   return COERCE16 ((addr[1] << 8) | addr[0]);
00546 }
00547 
00548 void
00549 bfd_putb16 (bfd_vma data, void *p)
00550 {
00551   bfd_byte *addr = p;
00552   addr[0] = (data >> 8) & 0xff;
00553   addr[1] = data & 0xff;
00554 }
00555 
00556 void
00557 bfd_putl16 (bfd_vma data, void *p)
00558 {
00559   bfd_byte *addr = p;
00560   addr[0] = data & 0xff;
00561   addr[1] = (data >> 8) & 0xff;
00562 }
00563 
00564 bfd_vma
00565 bfd_getb32 (const void *p)
00566 {
00567   const bfd_byte *addr = p;
00568   unsigned long v;
00569 
00570   v = (unsigned long) addr[0] << 24;
00571   v |= (unsigned long) addr[1] << 16;
00572   v |= (unsigned long) addr[2] << 8;
00573   v |= (unsigned long) addr[3];
00574   return v;
00575 }
00576 
00577 bfd_vma
00578 bfd_getl32 (const void *p)
00579 {
00580   const bfd_byte *addr = p;
00581   unsigned long v;
00582 
00583   v = (unsigned long) addr[0];
00584   v |= (unsigned long) addr[1] << 8;
00585   v |= (unsigned long) addr[2] << 16;
00586   v |= (unsigned long) addr[3] << 24;
00587   return v;
00588 }
00589 
00590 bfd_signed_vma
00591 bfd_getb_signed_32 (const void *p)
00592 {
00593   const bfd_byte *addr = p;
00594   unsigned long v;
00595 
00596   v = (unsigned long) addr[0] << 24;
00597   v |= (unsigned long) addr[1] << 16;
00598   v |= (unsigned long) addr[2] << 8;
00599   v |= (unsigned long) addr[3];
00600   return COERCE32 (v);
00601 }
00602 
00603 bfd_signed_vma
00604 bfd_getl_signed_32 (const void *p)
00605 {
00606   const bfd_byte *addr = p;
00607   unsigned long v;
00608 
00609   v = (unsigned long) addr[0];
00610   v |= (unsigned long) addr[1] << 8;
00611   v |= (unsigned long) addr[2] << 16;
00612   v |= (unsigned long) addr[3] << 24;
00613   return COERCE32 (v);
00614 }
00615 
00616 bfd_uint64_t
00617 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
00618 {
00619 #ifdef BFD_HOST_64_BIT
00620   const bfd_byte *addr = p;
00621   bfd_uint64_t v;
00622 
00623   v  = addr[0]; v <<= 8;
00624   v |= addr[1]; v <<= 8;
00625   v |= addr[2]; v <<= 8;
00626   v |= addr[3]; v <<= 8;
00627   v |= addr[4]; v <<= 8;
00628   v |= addr[5]; v <<= 8;
00629   v |= addr[6]; v <<= 8;
00630   v |= addr[7];
00631 
00632   return v;
00633 #else
00634   BFD_FAIL();
00635   return 0;
00636 #endif
00637 }
00638 
00639 bfd_uint64_t
00640 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
00641 {
00642 #ifdef BFD_HOST_64_BIT
00643   const bfd_byte *addr = p;
00644   bfd_uint64_t v;
00645 
00646   v  = addr[7]; v <<= 8;
00647   v |= addr[6]; v <<= 8;
00648   v |= addr[5]; v <<= 8;
00649   v |= addr[4]; v <<= 8;
00650   v |= addr[3]; v <<= 8;
00651   v |= addr[2]; v <<= 8;
00652   v |= addr[1]; v <<= 8;
00653   v |= addr[0];
00654 
00655   return v;
00656 #else
00657   BFD_FAIL();
00658   return 0;
00659 #endif
00660 
00661 }
00662 
00663 bfd_int64_t
00664 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
00665 {
00666 #ifdef BFD_HOST_64_BIT
00667   const bfd_byte *addr = p;
00668   bfd_uint64_t v;
00669 
00670   v  = addr[0]; v <<= 8;
00671   v |= addr[1]; v <<= 8;
00672   v |= addr[2]; v <<= 8;
00673   v |= addr[3]; v <<= 8;
00674   v |= addr[4]; v <<= 8;
00675   v |= addr[5]; v <<= 8;
00676   v |= addr[6]; v <<= 8;
00677   v |= addr[7];
00678 
00679   return COERCE64 (v);
00680 #else
00681   BFD_FAIL();
00682   return 0;
00683 #endif
00684 }
00685 
00686 bfd_int64_t
00687 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
00688 {
00689 #ifdef BFD_HOST_64_BIT
00690   const bfd_byte *addr = p;
00691   bfd_uint64_t v;
00692 
00693   v  = addr[7]; v <<= 8;
00694   v |= addr[6]; v <<= 8;
00695   v |= addr[5]; v <<= 8;
00696   v |= addr[4]; v <<= 8;
00697   v |= addr[3]; v <<= 8;
00698   v |= addr[2]; v <<= 8;
00699   v |= addr[1]; v <<= 8;
00700   v |= addr[0];
00701 
00702   return COERCE64 (v);
00703 #else
00704   BFD_FAIL();
00705   return 0;
00706 #endif
00707 }
00708 
00709 void
00710 bfd_putb32 (bfd_vma data, void *p)
00711 {
00712   bfd_byte *addr = p;
00713   addr[0] = (data >> 24) & 0xff;
00714   addr[1] = (data >> 16) & 0xff;
00715   addr[2] = (data >>  8) & 0xff;
00716   addr[3] = data & 0xff;
00717 }
00718 
00719 void
00720 bfd_putl32 (bfd_vma data, void *p)
00721 {
00722   bfd_byte *addr = p;
00723   addr[0] = data & 0xff;
00724   addr[1] = (data >>  8) & 0xff;
00725   addr[2] = (data >> 16) & 0xff;
00726   addr[3] = (data >> 24) & 0xff;
00727 }
00728 
00729 void
00730 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
00731 {
00732 #ifdef BFD_HOST_64_BIT
00733   bfd_byte *addr = p;
00734   addr[0] = (data >> (7*8)) & 0xff;
00735   addr[1] = (data >> (6*8)) & 0xff;
00736   addr[2] = (data >> (5*8)) & 0xff;
00737   addr[3] = (data >> (4*8)) & 0xff;
00738   addr[4] = (data >> (3*8)) & 0xff;
00739   addr[5] = (data >> (2*8)) & 0xff;
00740   addr[6] = (data >> (1*8)) & 0xff;
00741   addr[7] = (data >> (0*8)) & 0xff;
00742 #else
00743   BFD_FAIL();
00744 #endif
00745 }
00746 
00747 void
00748 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
00749 {
00750 #ifdef BFD_HOST_64_BIT
00751   bfd_byte *addr = p;
00752   addr[7] = (data >> (7*8)) & 0xff;
00753   addr[6] = (data >> (6*8)) & 0xff;
00754   addr[5] = (data >> (5*8)) & 0xff;
00755   addr[4] = (data >> (4*8)) & 0xff;
00756   addr[3] = (data >> (3*8)) & 0xff;
00757   addr[2] = (data >> (2*8)) & 0xff;
00758   addr[1] = (data >> (1*8)) & 0xff;
00759   addr[0] = (data >> (0*8)) & 0xff;
00760 #else
00761   BFD_FAIL();
00762 #endif
00763 }
00764 
00765 void
00766 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
00767 {
00768   bfd_byte *addr = p;
00769   int i;
00770   int bytes;
00771 
00772   if (bits % 8 != 0)
00773     abort ();
00774 
00775   bytes = bits / 8;
00776   for (i = 0; i < bytes; i++)
00777     {
00778       int index = big_p ? bytes - i - 1 : i;
00779 
00780       addr[index] = data & 0xff;
00781       data >>= 8;
00782     }
00783 }
00784 
00785 bfd_uint64_t
00786 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
00787 {
00788   const bfd_byte *addr = p;
00789   bfd_uint64_t data;
00790   int i;
00791   int bytes;
00792 
00793   if (bits % 8 != 0)
00794     abort ();
00795 
00796   data = 0;
00797   bytes = bits / 8;
00798   for (i = 0; i < bytes; i++)
00799     {
00800       int index = big_p ? i : bytes - i - 1;
00801 
00802       data = (data << 8) | addr[index];
00803     }
00804 
00805   return data;
00806 }
00807 
00808 /* Default implementation */
00809 
00810 bfd_boolean
00811 _bfd_generic_get_section_contents (bfd *abfd,
00812                                sec_ptr section,
00813                                void *location,
00814                                file_ptr offset,
00815                                bfd_size_type count)
00816 {
00817   bfd_size_type sz;
00818   if (count == 0)
00819     return TRUE;
00820 
00821   sz = section->rawsize ? section->rawsize : section->size;
00822   if (offset + count > sz)
00823     {
00824       bfd_set_error (bfd_error_invalid_operation);
00825       return FALSE;
00826     }
00827 
00828   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
00829       || bfd_bread (location, count, abfd) != count)
00830     return FALSE;
00831 
00832   return TRUE;
00833 }
00834 
00835 bfd_boolean
00836 _bfd_generic_get_section_contents_in_window
00837   (bfd *abfd ATTRIBUTE_UNUSED,
00838    sec_ptr section ATTRIBUTE_UNUSED,
00839    bfd_window *w ATTRIBUTE_UNUSED,
00840    file_ptr offset ATTRIBUTE_UNUSED,
00841    bfd_size_type count ATTRIBUTE_UNUSED)
00842 {
00843 #ifdef USE_MMAP
00844   bfd_size_type sz;
00845 
00846   if (count == 0)
00847     return TRUE;
00848   if (abfd->xvec->_bfd_get_section_contents
00849       != _bfd_generic_get_section_contents)
00850     {
00851       /* We don't know what changes the bfd's get_section_contents
00852         method may have to make.  So punt trying to map the file
00853         window, and let get_section_contents do its thing.  */
00854       /* @@ FIXME : If the internal window has a refcount of 1 and was
00855         allocated with malloc instead of mmap, just reuse it.  */
00856       bfd_free_window (w);
00857       w->i = bfd_zmalloc (sizeof (bfd_window_internal));
00858       if (w->i == NULL)
00859        return FALSE;
00860       w->i->data = bfd_malloc (count);
00861       if (w->i->data == NULL)
00862        {
00863          free (w->i);
00864          w->i = NULL;
00865          return FALSE;
00866        }
00867       w->i->mapped = 0;
00868       w->i->refcount = 1;
00869       w->size = w->i->size = count;
00870       w->data = w->i->data;
00871       return bfd_get_section_contents (abfd, section, w->data, offset, count);
00872     }
00873   sz = section->rawsize ? section->rawsize : section->size;
00874   if (offset + count > sz
00875       || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
00876                             TRUE))
00877     return FALSE;
00878   return TRUE;
00879 #else
00880   abort ();
00881 #endif
00882 }
00883 
00884 /* This generic function can only be used in implementations where creating
00885    NEW sections is disallowed.  It is useful in patching existing sections
00886    in read-write files, though.  See other set_section_contents functions
00887    to see why it doesn't work for new sections.  */
00888 bfd_boolean
00889 _bfd_generic_set_section_contents (bfd *abfd,
00890                                sec_ptr section,
00891                                const void *location,
00892                                file_ptr offset,
00893                                bfd_size_type count)
00894 {
00895   if (count == 0)
00896     return TRUE;
00897 
00898   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
00899       || bfd_bwrite (location, count, abfd) != count)
00900     return FALSE;
00901 
00902   return TRUE;
00903 }
00904 
00905 /*
00906 INTERNAL_FUNCTION
00907        bfd_log2
00908 
00909 SYNOPSIS
00910        unsigned int bfd_log2 (bfd_vma x);
00911 
00912 DESCRIPTION
00913        Return the log base 2 of the value supplied, rounded up.  E.g., an
00914        @var{x} of 1025 returns 11.  A @var{x} of 0 returns 0.
00915 */
00916 
00917 unsigned int
00918 bfd_log2 (bfd_vma x)
00919 {
00920   unsigned int result = 0;
00921 
00922   while ((x = (x >> 1)) != 0)
00923     ++result;
00924   return result;
00925 }
00926 
00927 bfd_boolean
00928 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
00929 {
00930   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
00931 
00932   return name[0] == locals_prefix;
00933 }
00934 
00935 /*  Can be used from / for bfd_merge_private_bfd_data to check that
00936     endianness matches between input and output file.  Returns
00937     TRUE for a match, otherwise returns FALSE and emits an error.  */
00938 bfd_boolean
00939 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
00940 {
00941   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
00942       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
00943       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
00944     {
00945       const char *msg;
00946 
00947       if (bfd_big_endian (ibfd))
00948        msg = _("%B: compiled for a big endian system and target is little endian");
00949       else
00950        msg = _("%B: compiled for a little endian system and target is big endian");
00951 
00952       (*_bfd_error_handler) (msg, ibfd);
00953 
00954       bfd_set_error (bfd_error_wrong_format);
00955       return FALSE;
00956     }
00957 
00958   return TRUE;
00959 }
00960 
00961 /* Give a warning at runtime if someone compiles code which calls
00962    old routines.  */
00963 
00964 void
00965 warn_deprecated (const char *what,
00966                const char *file,
00967                int line,
00968                const char *func)
00969 {
00970   /* Poor man's tracking of functions we've already warned about.  */
00971   static size_t mask = 0;
00972 
00973   if (~(size_t) func & ~mask)
00974     {
00975       /* Note: separate sentences in order to allow
00976         for translation into other languages.  */
00977       if (func)
00978        fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
00979                what, file, line, func);
00980       else
00981        fprintf (stderr, _("Deprecated %s called\n"), what);
00982       mask |= ~(size_t) func;
00983     }
00984 }
00985 
00986 /* Helper function for reading uleb128 encoded data.  */
00987 
00988 bfd_vma
00989 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
00990                     bfd_byte *buf,
00991                     unsigned int *bytes_read_ptr)
00992 {
00993   bfd_vma result;
00994   unsigned int num_read;
00995   unsigned int shift;
00996   unsigned char byte;
00997 
00998   result = 0;
00999   shift = 0;
01000   num_read = 0;
01001   do
01002     {
01003       byte = bfd_get_8 (abfd, buf);
01004       buf++;
01005       num_read++;
01006       result |= (((bfd_vma) byte & 0x7f) << shift);
01007       shift += 7;
01008     }
01009   while (byte & 0x80);
01010   *bytes_read_ptr = num_read;
01011   return result;
01012 }
01013 
01014 /* Helper function for reading sleb128 encoded data.  */
01015 
01016 bfd_signed_vma
01017 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
01018                   bfd_byte *buf,
01019                   unsigned int *bytes_read_ptr)
01020 {
01021   bfd_vma result;
01022   unsigned int shift;
01023   unsigned int num_read;
01024   unsigned char byte;
01025 
01026   result = 0;
01027   shift = 0;
01028   num_read = 0;
01029   do
01030     {
01031       byte = bfd_get_8 (abfd, buf);
01032       buf ++;
01033       num_read ++;
01034       result |= (((bfd_vma) byte & 0x7f) << shift);
01035       shift += 7;
01036     }
01037   while (byte & 0x80);
01038   if (shift < 8 * sizeof (result) && (byte & 0x40))
01039     result |= (((bfd_vma) -1) << shift);
01040   *bytes_read_ptr = num_read;
01041   return result;
01042 }
01043 
01044 bfd_boolean
01045 _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
01046                      asymbol **symbols ATTRIBUTE_UNUSED,
01047                      asymbol *symbol ATTRIBUTE_UNUSED,
01048                      const char **filename_ptr ATTRIBUTE_UNUSED,
01049                      unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
01050 {
01051   return FALSE;
01052 }
01053 
01054 bfd_boolean
01055 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
01056                                    asection *isec ATTRIBUTE_UNUSED,
01057                                    bfd *obfd ATTRIBUTE_UNUSED,
01058                                    asection *osec ATTRIBUTE_UNUSED,
01059                                    struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
01060 {
01061   return TRUE;
01062 }