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