Subversion Repositories Kolibri OS

Rev

Rev 5197 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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