libbfd.c 30 KB

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