1 /* Assorted BFD support routines, only used internally.
2 Copyright (C) 1990-2020 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
26 #ifndef HAVE_GETPAGESIZE
27 #define getpagesize() 2048
32 Implementation details
38 These routines are used within BFD.
39 They are not intended for export, but are documented here for
44 _bfd_bool_bfd_false (bfd
*abfd ATTRIBUTE_UNUSED
)
50 _bfd_bool_bfd_asymbol_false (bfd
*abfd ATTRIBUTE_UNUSED
,
51 asymbol
*sym ATTRIBUTE_UNUSED
)
56 /* A routine which is used in target vectors for unsupported
60 _bfd_bool_bfd_false_error (bfd
*ignore ATTRIBUTE_UNUSED
)
62 bfd_set_error (bfd_error_invalid_operation
);
67 _bfd_bool_bfd_link_false_error (bfd
*abfd
,
68 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
70 return _bfd_bool_bfd_false_error (abfd
);
73 /* A routine which is used in target vectors for supported operations
74 which do not actually do anything. */
77 _bfd_bool_bfd_true (bfd
*ignore ATTRIBUTE_UNUSED
)
83 _bfd_bool_bfd_link_true (bfd
*abfd ATTRIBUTE_UNUSED
,
84 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
90 _bfd_bool_bfd_bfd_true (bfd
*ibfd ATTRIBUTE_UNUSED
,
91 bfd
*obfd ATTRIBUTE_UNUSED
)
97 _bfd_bool_bfd_uint_true (bfd
*abfd ATTRIBUTE_UNUSED
,
98 unsigned int flags ATTRIBUTE_UNUSED
)
104 _bfd_bool_bfd_asection_bfd_asection_true (bfd
*ibfd ATTRIBUTE_UNUSED
,
105 asection
*isec ATTRIBUTE_UNUSED
,
106 bfd
*obfd ATTRIBUTE_UNUSED
,
107 asection
*osec ATTRIBUTE_UNUSED
)
113 _bfd_bool_bfd_asymbol_bfd_asymbol_true (bfd
*ibfd ATTRIBUTE_UNUSED
,
114 asymbol
*isym ATTRIBUTE_UNUSED
,
115 bfd
*obfd ATTRIBUTE_UNUSED
,
116 asymbol
*osym ATTRIBUTE_UNUSED
)
122 _bfd_bool_bfd_ptr_true (bfd
*abfd ATTRIBUTE_UNUSED
,
123 void *ptr ATTRIBUTE_UNUSED
)
128 /* A routine which is used in target vectors for unsupported
129 operations which return a pointer value. */
132 _bfd_ptr_bfd_null_error (bfd
*ignore ATTRIBUTE_UNUSED
)
134 bfd_set_error (bfd_error_invalid_operation
);
139 _bfd_int_bfd_0 (bfd
*ignore ATTRIBUTE_UNUSED
)
145 _bfd_uint_bfd_0 (bfd
*ignore ATTRIBUTE_UNUSED
)
151 _bfd_long_bfd_0 (bfd
*ignore ATTRIBUTE_UNUSED
)
156 /* A routine which is used in target vectors for unsupported
157 operations which return -1 on error. */
160 _bfd_long_bfd_n1_error (bfd
*ignore_abfd ATTRIBUTE_UNUSED
)
162 bfd_set_error (bfd_error_invalid_operation
);
167 _bfd_void_bfd (bfd
*ignore ATTRIBUTE_UNUSED
)
172 _bfd_void_bfd_link (bfd
*abfd ATTRIBUTE_UNUSED
,
173 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
178 _bfd_void_bfd_asection (bfd
*abfd ATTRIBUTE_UNUSED
,
179 asection
*sec ATTRIBUTE_UNUSED
)
184 _bfd_norelocs_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
185 asection
*sec ATTRIBUTE_UNUSED
)
187 return sizeof (arelent
*);
191 _bfd_norelocs_canonicalize_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
192 asection
*sec ATTRIBUTE_UNUSED
,
194 asymbol
**symbols ATTRIBUTE_UNUSED
)
201 _bfd_norelocs_set_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
202 asection
*sec ATTRIBUTE_UNUSED
,
203 arelent
**relptr ATTRIBUTE_UNUSED
,
204 unsigned int count ATTRIBUTE_UNUSED
)
210 _bfd_nocore_core_file_matches_executable_p
211 (bfd
*ignore_core_bfd ATTRIBUTE_UNUSED
,
212 bfd
*ignore_exec_bfd ATTRIBUTE_UNUSED
)
214 bfd_set_error (bfd_error_invalid_operation
);
218 /* Routine to handle core_file_failing_command entry point for targets
219 without core file support. */
222 _bfd_nocore_core_file_failing_command (bfd
*ignore_abfd ATTRIBUTE_UNUSED
)
224 bfd_set_error (bfd_error_invalid_operation
);
228 /* Routine to handle core_file_failing_signal entry point for targets
229 without core file support. */
232 _bfd_nocore_core_file_failing_signal (bfd
*ignore_abfd ATTRIBUTE_UNUSED
)
234 bfd_set_error (bfd_error_invalid_operation
);
238 /* Routine to handle the core_file_pid entry point for targets without
239 core file support. */
242 _bfd_nocore_core_file_pid (bfd
*ignore_abfd ATTRIBUTE_UNUSED
)
244 bfd_set_error (bfd_error_invalid_operation
);
249 _bfd_dummy_target (bfd
*ignore_abfd ATTRIBUTE_UNUSED
)
251 bfd_set_error (bfd_error_wrong_format
);
255 /* Allocate memory using malloc. */
258 #define SSIZE_MAX ((size_t) -1 >> 1)
262 bfd_malloc (bfd_size_type size
)
265 size_t sz
= (size_t) size
;
268 /* This is to pacify memory checkers like valgrind. */
271 bfd_set_error (bfd_error_no_memory
);
276 if (ptr
== NULL
&& sz
!= 0)
277 bfd_set_error (bfd_error_no_memory
);
283 bfd_realloc (void *ptr
, bfd_size_type size
)
286 size_t sz
= (size_t) size
;
289 return bfd_malloc (size
);
292 /* This is to pacify memory checkers like valgrind. */
295 bfd_set_error (bfd_error_no_memory
);
299 ret
= realloc (ptr
, sz
);
301 if (ret
== NULL
&& sz
!= 0)
302 bfd_set_error (bfd_error_no_memory
);
307 /* Reallocate memory using realloc.
308 If this fails the pointer is freed before returning. */
311 bfd_realloc_or_free (void *ptr
, bfd_size_type size
)
313 void *ret
= bfd_realloc (ptr
, size
);
321 /* Allocate memory using malloc and clear it. */
324 bfd_zmalloc (bfd_size_type size
)
326 void *ptr
= bfd_malloc (size
);
328 if (ptr
!= NULL
&& size
> 0)
329 memset (ptr
, 0, (size_t) size
);
336 bfd_write_bigendian_4byte_int
339 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
342 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
343 endian order regardless of what else is going on. This is useful in
348 bfd_write_bigendian_4byte_int (bfd
*abfd
, unsigned int i
)
351 bfd_putb32 ((bfd_vma
) i
, buffer
);
352 return bfd_bwrite (buffer
, (bfd_size_type
) 4, abfd
) == 4;
356 /** The do-it-yourself (byte) sex-change kit */
358 /* The middle letter e.g. get<b>short indicates Big or Little endian
359 target machine. It doesn't matter what the byte order of the host
360 machine is; these routines work for either. */
362 /* FIXME: Should these take a count argument?
363 Answer (gnu@cygnus.com): No, but perhaps they should be inline
364 functions in swap.h #ifdef __GNUC__.
365 Gprof them later and find out. */
374 These macros as used for reading and writing raw data in
375 sections; each access (except for bytes) is vectored through
376 the target format of the BFD and mangled accordingly. The
377 mangling performs any necessary endian translations and
378 removes alignment restrictions. Note that types accepted and
379 returned by these macros are identical so they can be swapped
380 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
381 to either <<bfd_get_32>> or <<bfd_get_64>>.
383 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
384 system without prototypes, the caller is responsible for making
385 sure that is true, with a cast if necessary. We don't cast
386 them in the macro definitions because that would prevent <<lint>>
387 or <<gcc -Wall>> from detecting sins such as passing a pointer.
388 To detect calling these with less than a <<bfd_vma>>, use
389 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
392 .{* Byte swapping macros for user section data. *}
394 .#define bfd_put_8(abfd, val, ptr) \
395 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
396 .#define bfd_put_signed_8 \
398 .#define bfd_get_8(abfd, ptr) \
399 . ((bfd_vma) *(const unsigned char *) (ptr) & 0xff)
400 .#define bfd_get_signed_8(abfd, ptr) \
401 . ((((bfd_signed_vma) *(const unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
403 .#define bfd_put_16(abfd, val, ptr) \
404 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
405 .#define bfd_put_signed_16 \
407 .#define bfd_get_16(abfd, ptr) \
408 . BFD_SEND (abfd, bfd_getx16, (ptr))
409 .#define bfd_get_signed_16(abfd, ptr) \
410 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
412 .#define bfd_put_24(abfd, val, ptr) \
414 . if (bfd_big_endian (abfd)) \
415 . bfd_putb24 ((val), (ptr)); \
417 . bfd_putl24 ((val), (ptr)); \
420 .bfd_vma bfd_getb24 (const void *p);
421 .bfd_vma bfd_getl24 (const void *p);
423 .#define bfd_get_24(abfd, ptr) \
424 . (bfd_big_endian (abfd) ? bfd_getb24 (ptr) : bfd_getl24 (ptr))
426 .#define bfd_put_32(abfd, val, ptr) \
427 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
428 .#define bfd_put_signed_32 \
430 .#define bfd_get_32(abfd, ptr) \
431 . BFD_SEND (abfd, bfd_getx32, (ptr))
432 .#define bfd_get_signed_32(abfd, ptr) \
433 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
435 .#define bfd_put_64(abfd, val, ptr) \
436 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
437 .#define bfd_put_signed_64 \
439 .#define bfd_get_64(abfd, ptr) \
440 . BFD_SEND (abfd, bfd_getx64, (ptr))
441 .#define bfd_get_signed_64(abfd, ptr) \
442 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
444 .#define bfd_get(bits, abfd, ptr) \
445 . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \
446 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
447 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
448 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
449 . : (abort (), (bfd_vma) - 1))
451 .#define bfd_put(bits, abfd, val, ptr) \
452 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
453 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
454 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
455 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
456 . : (abort (), (void) 0))
466 These macros have the same function as their <<bfd_get_x>>
467 brethren, except that they are used for removing information
468 for the header records of object files. Believe it or not,
469 some object files keep their header records in big endian
470 order and their data in little endian order.
472 .{* Byte swapping macros for file header data. *}
474 .#define bfd_h_put_8(abfd, val, ptr) \
475 . bfd_put_8 (abfd, val, ptr)
476 .#define bfd_h_put_signed_8(abfd, val, ptr) \
477 . bfd_put_8 (abfd, val, ptr)
478 .#define bfd_h_get_8(abfd, ptr) \
479 . bfd_get_8 (abfd, ptr)
480 .#define bfd_h_get_signed_8(abfd, ptr) \
481 . bfd_get_signed_8 (abfd, ptr)
483 .#define bfd_h_put_16(abfd, val, ptr) \
484 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
485 .#define bfd_h_put_signed_16 \
487 .#define bfd_h_get_16(abfd, ptr) \
488 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
489 .#define bfd_h_get_signed_16(abfd, ptr) \
490 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
492 .#define bfd_h_put_32(abfd, val, ptr) \
493 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
494 .#define bfd_h_put_signed_32 \
496 .#define bfd_h_get_32(abfd, ptr) \
497 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
498 .#define bfd_h_get_signed_32(abfd, ptr) \
499 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
501 .#define bfd_h_put_64(abfd, val, ptr) \
502 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
503 .#define bfd_h_put_signed_64 \
505 .#define bfd_h_get_64(abfd, ptr) \
506 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
507 .#define bfd_h_get_signed_64(abfd, ptr) \
508 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
510 .{* Aliases for the above, which should eventually go away. *}
512 .#define H_PUT_64 bfd_h_put_64
513 .#define H_PUT_32 bfd_h_put_32
514 .#define H_PUT_16 bfd_h_put_16
515 .#define H_PUT_8 bfd_h_put_8
516 .#define H_PUT_S64 bfd_h_put_signed_64
517 .#define H_PUT_S32 bfd_h_put_signed_32
518 .#define H_PUT_S16 bfd_h_put_signed_16
519 .#define H_PUT_S8 bfd_h_put_signed_8
520 .#define H_GET_64 bfd_h_get_64
521 .#define H_GET_32 bfd_h_get_32
522 .#define H_GET_16 bfd_h_get_16
523 .#define H_GET_8 bfd_h_get_8
524 .#define H_GET_S64 bfd_h_get_signed_64
525 .#define H_GET_S32 bfd_h_get_signed_32
526 .#define H_GET_S16 bfd_h_get_signed_16
527 .#define H_GET_S8 bfd_h_get_signed_8
531 /* Sign extension to bfd_signed_vma. */
532 #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000)
533 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
534 #define COERCE64(x) \
535 (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63))
538 bfd_getb16 (const void *p
)
540 const bfd_byte
*addr
= (const bfd_byte
*) p
;
541 return (addr
[0] << 8) | addr
[1];
545 bfd_getl16 (const void *p
)
547 const bfd_byte
*addr
= (const bfd_byte
*) p
;
548 return (addr
[1] << 8) | addr
[0];
552 bfd_getb_signed_16 (const void *p
)
554 const bfd_byte
*addr
= (const bfd_byte
*) p
;
555 return COERCE16 ((addr
[0] << 8) | addr
[1]);
559 bfd_getl_signed_16 (const void *p
)
561 const bfd_byte
*addr
= (const bfd_byte
*) p
;
562 return COERCE16 ((addr
[1] << 8) | addr
[0]);
566 bfd_putb16 (bfd_vma data
, void *p
)
568 bfd_byte
*addr
= (bfd_byte
*) p
;
569 addr
[0] = (data
>> 8) & 0xff;
570 addr
[1] = data
& 0xff;
574 bfd_putl16 (bfd_vma data
, void *p
)
576 bfd_byte
*addr
= (bfd_byte
*) p
;
577 addr
[0] = data
& 0xff;
578 addr
[1] = (data
>> 8) & 0xff;
582 bfd_putb24 (bfd_vma data
, void *p
)
584 bfd_byte
*addr
= (bfd_byte
*) p
;
585 addr
[0] = (data
>> 16) & 0xff;
586 addr
[1] = (data
>> 8) & 0xff;
587 addr
[2] = data
& 0xff;
591 bfd_putl24 (bfd_vma data
, void *p
)
593 bfd_byte
*addr
= (bfd_byte
*) p
;
594 addr
[0] = data
& 0xff;
595 addr
[1] = (data
>> 8) & 0xff;
596 addr
[2] = (data
>> 16) & 0xff;
600 bfd_getb24 (const void *p
)
602 const bfd_byte
*addr
= (const bfd_byte
*) p
;
605 v
= (unsigned long) addr
[0] << 16;
606 v
|= (unsigned long) addr
[1] << 8;
607 v
|= (unsigned long) addr
[2];
612 bfd_getl24 (const void *p
)
614 const bfd_byte
*addr
= (const bfd_byte
*) p
;
617 v
= (unsigned long) addr
[0];
618 v
|= (unsigned long) addr
[1] << 8;
619 v
|= (unsigned long) addr
[2] << 16;
624 bfd_getb32 (const void *p
)
626 const bfd_byte
*addr
= (const bfd_byte
*) p
;
629 v
= (unsigned long) addr
[0] << 24;
630 v
|= (unsigned long) addr
[1] << 16;
631 v
|= (unsigned long) addr
[2] << 8;
632 v
|= (unsigned long) addr
[3];
637 bfd_getl32 (const void *p
)
639 const bfd_byte
*addr
= (const bfd_byte
*) p
;
642 v
= (unsigned long) addr
[0];
643 v
|= (unsigned long) addr
[1] << 8;
644 v
|= (unsigned long) addr
[2] << 16;
645 v
|= (unsigned long) addr
[3] << 24;
650 bfd_getb_signed_32 (const void *p
)
652 const bfd_byte
*addr
= (const bfd_byte
*) p
;
655 v
= (unsigned long) addr
[0] << 24;
656 v
|= (unsigned long) addr
[1] << 16;
657 v
|= (unsigned long) addr
[2] << 8;
658 v
|= (unsigned long) addr
[3];
663 bfd_getl_signed_32 (const void *p
)
665 const bfd_byte
*addr
= (const bfd_byte
*) p
;
668 v
= (unsigned long) addr
[0];
669 v
|= (unsigned long) addr
[1] << 8;
670 v
|= (unsigned long) addr
[2] << 16;
671 v
|= (unsigned long) addr
[3] << 24;
676 bfd_getb64 (const void *p ATTRIBUTE_UNUSED
)
678 #ifdef BFD_HOST_64_BIT
679 const bfd_byte
*addr
= (const bfd_byte
*) p
;
682 v
= addr
[0]; v
<<= 8;
683 v
|= addr
[1]; v
<<= 8;
684 v
|= addr
[2]; v
<<= 8;
685 v
|= addr
[3]; v
<<= 8;
686 v
|= addr
[4]; v
<<= 8;
687 v
|= addr
[5]; v
<<= 8;
688 v
|= addr
[6]; v
<<= 8;
699 bfd_getl64 (const void *p ATTRIBUTE_UNUSED
)
701 #ifdef BFD_HOST_64_BIT
702 const bfd_byte
*addr
= (const bfd_byte
*) p
;
705 v
= addr
[7]; v
<<= 8;
706 v
|= addr
[6]; v
<<= 8;
707 v
|= addr
[5]; v
<<= 8;
708 v
|= addr
[4]; v
<<= 8;
709 v
|= addr
[3]; v
<<= 8;
710 v
|= addr
[2]; v
<<= 8;
711 v
|= addr
[1]; v
<<= 8;
723 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED
)
725 #ifdef BFD_HOST_64_BIT
726 const bfd_byte
*addr
= (const bfd_byte
*) p
;
729 v
= addr
[0]; v
<<= 8;
730 v
|= addr
[1]; v
<<= 8;
731 v
|= addr
[2]; v
<<= 8;
732 v
|= addr
[3]; v
<<= 8;
733 v
|= addr
[4]; v
<<= 8;
734 v
|= addr
[5]; v
<<= 8;
735 v
|= addr
[6]; v
<<= 8;
746 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED
)
748 #ifdef BFD_HOST_64_BIT
749 const bfd_byte
*addr
= (const bfd_byte
*) p
;
752 v
= addr
[7]; v
<<= 8;
753 v
|= addr
[6]; v
<<= 8;
754 v
|= addr
[5]; v
<<= 8;
755 v
|= addr
[4]; v
<<= 8;
756 v
|= addr
[3]; v
<<= 8;
757 v
|= addr
[2]; v
<<= 8;
758 v
|= addr
[1]; v
<<= 8;
769 bfd_putb32 (bfd_vma data
, void *p
)
771 bfd_byte
*addr
= (bfd_byte
*) p
;
772 addr
[0] = (data
>> 24) & 0xff;
773 addr
[1] = (data
>> 16) & 0xff;
774 addr
[2] = (data
>> 8) & 0xff;
775 addr
[3] = data
& 0xff;
779 bfd_putl32 (bfd_vma data
, void *p
)
781 bfd_byte
*addr
= (bfd_byte
*) p
;
782 addr
[0] = data
& 0xff;
783 addr
[1] = (data
>> 8) & 0xff;
784 addr
[2] = (data
>> 16) & 0xff;
785 addr
[3] = (data
>> 24) & 0xff;
789 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED
, void *p ATTRIBUTE_UNUSED
)
791 #ifdef BFD_HOST_64_BIT
792 bfd_byte
*addr
= (bfd_byte
*) p
;
793 addr
[0] = (data
>> (7*8)) & 0xff;
794 addr
[1] = (data
>> (6*8)) & 0xff;
795 addr
[2] = (data
>> (5*8)) & 0xff;
796 addr
[3] = (data
>> (4*8)) & 0xff;
797 addr
[4] = (data
>> (3*8)) & 0xff;
798 addr
[5] = (data
>> (2*8)) & 0xff;
799 addr
[6] = (data
>> (1*8)) & 0xff;
800 addr
[7] = (data
>> (0*8)) & 0xff;
807 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED
, void *p ATTRIBUTE_UNUSED
)
809 #ifdef BFD_HOST_64_BIT
810 bfd_byte
*addr
= (bfd_byte
*) p
;
811 addr
[7] = (data
>> (7*8)) & 0xff;
812 addr
[6] = (data
>> (6*8)) & 0xff;
813 addr
[5] = (data
>> (5*8)) & 0xff;
814 addr
[4] = (data
>> (4*8)) & 0xff;
815 addr
[3] = (data
>> (3*8)) & 0xff;
816 addr
[2] = (data
>> (2*8)) & 0xff;
817 addr
[1] = (data
>> (1*8)) & 0xff;
818 addr
[0] = (data
>> (0*8)) & 0xff;
825 bfd_put_bits (bfd_uint64_t data
, void *p
, int bits
, bfd_boolean big_p
)
827 bfd_byte
*addr
= (bfd_byte
*) p
;
835 for (i
= 0; i
< bytes
; i
++)
837 int addr_index
= big_p
? bytes
- i
- 1 : i
;
839 addr
[addr_index
] = data
& 0xff;
845 bfd_get_bits (const void *p
, int bits
, bfd_boolean big_p
)
847 const bfd_byte
*addr
= (const bfd_byte
*) p
;
857 for (i
= 0; i
< bytes
; i
++)
859 int addr_index
= big_p
? i
: bytes
- i
- 1;
861 data
= (data
<< 8) | addr
[addr_index
];
867 /* Default implementation */
870 _bfd_generic_get_section_contents (bfd
*abfd
,
880 if (section
->compress_status
!= COMPRESS_SECTION_NONE
)
883 /* xgettext:c-format */
884 (_("%pB: unable to get decompressed section %pA"),
886 bfd_set_error (bfd_error_invalid_operation
);
890 /* We do allow reading of a section after bfd_final_link has
891 written the contents out to disk. In that situation, rawsize is
892 just a stale version of size, so ignore it. Otherwise we must be
893 reading an input section, where rawsize, if different to size,
894 is the on-disk size. */
895 if (abfd
->direction
!= write_direction
&& section
->rawsize
!= 0)
896 sz
= section
->rawsize
;
899 if (offset
+ count
< count
900 || offset
+ count
> sz
901 || (abfd
->my_archive
!= NULL
902 && !bfd_is_thin_archive (abfd
->my_archive
)
903 && ((ufile_ptr
) section
->filepos
+ offset
+ count
904 > arelt_size (abfd
))))
906 bfd_set_error (bfd_error_invalid_operation
);
910 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
911 || bfd_bread (location
, count
, abfd
) != count
)
918 _bfd_generic_get_section_contents_in_window
919 (bfd
*abfd ATTRIBUTE_UNUSED
,
920 sec_ptr section ATTRIBUTE_UNUSED
,
921 bfd_window
*w ATTRIBUTE_UNUSED
,
922 file_ptr offset ATTRIBUTE_UNUSED
,
923 bfd_size_type count ATTRIBUTE_UNUSED
)
930 if (abfd
->xvec
->_bfd_get_section_contents
931 != _bfd_generic_get_section_contents
)
933 /* We don't know what changes the bfd's get_section_contents
934 method may have to make. So punt trying to map the file
935 window, and let get_section_contents do its thing. */
936 /* @@ FIXME : If the internal window has a refcount of 1 and was
937 allocated with malloc instead of mmap, just reuse it. */
939 w
->i
= bfd_zmalloc (sizeof (bfd_window_internal
));
942 w
->i
->data
= bfd_malloc (count
);
943 if (w
->i
->data
== NULL
)
951 w
->size
= w
->i
->size
= count
;
952 w
->data
= w
->i
->data
;
953 return bfd_get_section_contents (abfd
, section
, w
->data
, offset
, count
);
955 if (abfd
->direction
!= write_direction
&& section
->rawsize
!= 0)
956 sz
= section
->rawsize
;
959 if (offset
+ count
< count
960 || offset
+ count
> sz
961 || (abfd
->my_archive
!= NULL
962 && !bfd_is_thin_archive (abfd
->my_archive
)
963 && ((ufile_ptr
) section
->filepos
+ offset
+ count
964 > arelt_size (abfd
)))
965 || ! bfd_get_file_window (abfd
, section
->filepos
+ offset
, count
, w
,
974 /* This generic function can only be used in implementations where creating
975 NEW sections is disallowed. It is useful in patching existing sections
976 in read-write files, though. See other set_section_contents functions
977 to see why it doesn't work for new sections. */
979 _bfd_generic_set_section_contents (bfd
*abfd
,
981 const void *location
,
988 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
989 || bfd_bwrite (location
, count
, abfd
) != count
)
1000 unsigned int bfd_log2 (bfd_vma x);
1003 Return the log base 2 of the value supplied, rounded up. E.g., an
1004 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
1008 bfd_log2 (bfd_vma x
)
1010 unsigned int result
= 0;
1017 while ((x
>>= 1) != 0);
1022 bfd_generic_is_local_label_name (bfd
*abfd
, const char *name
)
1024 char locals_prefix
= (bfd_get_symbol_leading_char (abfd
) == '_') ? 'L' : '.';
1026 return name
[0] == locals_prefix
;
1029 /* Give a warning at runtime if someone compiles code which calls
1033 _bfd_warn_deprecated (const char *what
,
1038 /* Poor man's tracking of functions we've already warned about. */
1039 static size_t mask
= 0;
1041 if (~(size_t) func
& ~mask
)
1044 /* Note: separate sentences in order to allow
1045 for translation into other languages. */
1047 /* xgettext:c-format */
1048 fprintf (stderr
, _("Deprecated %s called at %s line %d in %s\n"),
1049 what
, file
, line
, func
);
1051 fprintf (stderr
, _("Deprecated %s called\n"), what
);
1053 mask
|= ~(size_t) func
;
1057 /* Helper function for reading uleb128 encoded data. */
1060 _bfd_read_unsigned_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
1062 unsigned int *bytes_read_ptr
)
1065 unsigned int num_read
;
1074 byte
= bfd_get_8 (abfd
, buf
);
1077 result
|= (((bfd_vma
) byte
& 0x7f) << shift
);
1080 while (byte
& 0x80);
1081 *bytes_read_ptr
= num_read
;
1085 /* Read in a LEB128 encoded value from ABFD starting at DATA.
1086 If SIGN is true, return a signed LEB128 value.
1087 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
1088 No bytes will be read at address END or beyond. */
1091 _bfd_safe_read_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
1093 unsigned int *length_return
,
1095 const bfd_byte
* const end
)
1098 unsigned int num_read
= 0;
1099 unsigned int shift
= 0;
1100 unsigned char byte
= 0;
1104 byte
= bfd_get_8 (abfd
, data
);
1108 result
|= ((bfd_vma
) (byte
& 0x7f)) << shift
;
1111 if ((byte
& 0x80) == 0)
1115 if (length_return
!= NULL
)
1116 *length_return
= num_read
;
1118 if (sign
&& (shift
< 8 * sizeof (result
)) && (byte
& 0x40))
1119 result
|= -((bfd_vma
) 1 << shift
);
1124 /* Helper function for reading sleb128 encoded data. */
1127 _bfd_read_signed_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
1129 unsigned int *bytes_read_ptr
)
1133 unsigned int num_read
;
1141 byte
= bfd_get_8 (abfd
, buf
);
1144 result
|= (((bfd_vma
) byte
& 0x7f) << shift
);
1147 while (byte
& 0x80);
1148 if (shift
< 8 * sizeof (result
) && (byte
& 0x40))
1149 result
|= (((bfd_vma
) -1) << shift
);
1150 *bytes_read_ptr
= num_read
;
1154 /* Write VAL in uleb128 format to P.
1155 END indicates the last byte of allocated space for the uleb128 value to fit
1157 Return a pointer to the byte following the last byte that was written, or
1158 NULL if the uleb128 value does not fit in the allocated space between P and
1161 _bfd_write_unsigned_leb128 (bfd_byte
*p
, bfd_byte
*end
, bfd_vma val
)
1179 _bfd_generic_init_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
1180 asection
*isec ATTRIBUTE_UNUSED
,
1181 bfd
*obfd ATTRIBUTE_UNUSED
,
1182 asection
*osec ATTRIBUTE_UNUSED
,
1183 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
)