Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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