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