1 /* Assorted BFD support routines, only used internally.
2 Copyright (C) 1990-2019 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
);
282 /* Allocate memory using malloc, nmemb * size with overflow checking. */
285 bfd_malloc2 (bfd_size_type nmemb
, bfd_size_type size
)
287 if ((nmemb
| size
) >= HALF_BFD_SIZE_TYPE
289 && nmemb
> ~(bfd_size_type
) 0 / size
)
291 bfd_set_error (bfd_error_no_memory
);
295 return bfd_malloc (size
* nmemb
);
298 /* Reallocate memory using realloc. */
301 bfd_realloc (void *ptr
, bfd_size_type size
)
304 size_t sz
= (size_t) size
;
307 return bfd_malloc (size
);
310 /* This is to pacify memory checkers like valgrind. */
313 bfd_set_error (bfd_error_no_memory
);
317 ret
= realloc (ptr
, sz
);
319 if (ret
== NULL
&& sz
!= 0)
320 bfd_set_error (bfd_error_no_memory
);
325 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
328 bfd_realloc2 (void *ptr
, bfd_size_type nmemb
, bfd_size_type size
)
330 if ((nmemb
| size
) >= HALF_BFD_SIZE_TYPE
332 && nmemb
> ~(bfd_size_type
) 0 / size
)
334 bfd_set_error (bfd_error_no_memory
);
338 return bfd_realloc (ptr
, size
* nmemb
);
341 /* Reallocate memory using realloc.
342 If this fails the pointer is freed before returning. */
345 bfd_realloc_or_free (void *ptr
, bfd_size_type size
)
347 void *ret
= bfd_realloc (ptr
, size
);
349 if (ret
== NULL
&& ptr
!= NULL
)
355 /* Allocate memory using malloc and clear it. */
358 bfd_zmalloc (bfd_size_type size
)
360 void *ptr
= bfd_malloc (size
);
362 if (ptr
!= NULL
&& size
> 0)
363 memset (ptr
, 0, (size_t) size
);
368 /* Allocate memory using malloc (nmemb * size) with overflow checking
372 bfd_zmalloc2 (bfd_size_type nmemb
, bfd_size_type size
)
374 void *ptr
= bfd_malloc2 (nmemb
, size
);
378 size_t sz
= nmemb
* size
;
389 bfd_write_bigendian_4byte_int
392 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
395 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
396 endian order regardless of what else is going on. This is useful in
401 bfd_write_bigendian_4byte_int (bfd
*abfd
, unsigned int i
)
404 bfd_putb32 ((bfd_vma
) i
, buffer
);
405 return bfd_bwrite (buffer
, (bfd_size_type
) 4, abfd
) == 4;
409 /** The do-it-yourself (byte) sex-change kit */
411 /* The middle letter e.g. get<b>short indicates Big or Little endian
412 target machine. It doesn't matter what the byte order of the host
413 machine is; these routines work for either. */
415 /* FIXME: Should these take a count argument?
416 Answer (gnu@cygnus.com): No, but perhaps they should be inline
417 functions in swap.h #ifdef __GNUC__.
418 Gprof them later and find out. */
427 These macros as used for reading and writing raw data in
428 sections; each access (except for bytes) is vectored through
429 the target format of the BFD and mangled accordingly. The
430 mangling performs any necessary endian translations and
431 removes alignment restrictions. Note that types accepted and
432 returned by these macros are identical so they can be swapped
433 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
434 to either <<bfd_get_32>> or <<bfd_get_64>>.
436 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
437 system without prototypes, the caller is responsible for making
438 sure that is true, with a cast if necessary. We don't cast
439 them in the macro definitions because that would prevent <<lint>>
440 or <<gcc -Wall>> from detecting sins such as passing a pointer.
441 To detect calling these with less than a <<bfd_vma>>, use
442 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
445 .{* Byte swapping macros for user section data. *}
447 .#define bfd_put_8(abfd, val, ptr) \
448 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
449 .#define bfd_put_signed_8 \
451 .#define bfd_get_8(abfd, ptr) \
452 . (*(const unsigned char *) (ptr) & 0xff)
453 .#define bfd_get_signed_8(abfd, ptr) \
454 . (((*(const unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
456 .#define bfd_put_16(abfd, val, ptr) \
457 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
458 .#define bfd_put_signed_16 \
460 .#define bfd_get_16(abfd, ptr) \
461 . BFD_SEND (abfd, bfd_getx16, (ptr))
462 .#define bfd_get_signed_16(abfd, ptr) \
463 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
465 .#define bfd_put_24(abfd, val, ptr) \
467 . if (bfd_big_endian (abfd)) \
468 . bfd_putb24 ((val), (ptr)); \
470 . bfd_putl24 ((val), (ptr)); \
473 .bfd_vma bfd_getb24 (const void *p);
474 .bfd_vma bfd_getl24 (const void *p);
476 .#define bfd_get_24(abfd, ptr) \
477 . (bfd_big_endian (abfd) ? bfd_getb24 (ptr) : bfd_getl24 (ptr))
479 .#define bfd_put_32(abfd, val, ptr) \
480 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
481 .#define bfd_put_signed_32 \
483 .#define bfd_get_32(abfd, ptr) \
484 . BFD_SEND (abfd, bfd_getx32, (ptr))
485 .#define bfd_get_signed_32(abfd, ptr) \
486 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
488 .#define bfd_put_64(abfd, val, ptr) \
489 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
490 .#define bfd_put_signed_64 \
492 .#define bfd_get_64(abfd, ptr) \
493 . BFD_SEND (abfd, bfd_getx64, (ptr))
494 .#define bfd_get_signed_64(abfd, ptr) \
495 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
497 .#define bfd_get(bits, abfd, ptr) \
498 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
499 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
500 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
501 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
502 . : (abort (), (bfd_vma) - 1))
504 .#define bfd_put(bits, abfd, val, ptr) \
505 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
506 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
507 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
508 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
509 . : (abort (), (void) 0))
519 These macros have the same function as their <<bfd_get_x>>
520 brethren, except that they are used for removing information
521 for the header records of object files. Believe it or not,
522 some object files keep their header records in big endian
523 order and their data in little endian order.
525 .{* Byte swapping macros for file header data. *}
527 .#define bfd_h_put_8(abfd, val, ptr) \
528 . bfd_put_8 (abfd, val, ptr)
529 .#define bfd_h_put_signed_8(abfd, val, ptr) \
530 . bfd_put_8 (abfd, val, ptr)
531 .#define bfd_h_get_8(abfd, ptr) \
532 . bfd_get_8 (abfd, ptr)
533 .#define bfd_h_get_signed_8(abfd, ptr) \
534 . bfd_get_signed_8 (abfd, ptr)
536 .#define bfd_h_put_16(abfd, val, ptr) \
537 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
538 .#define bfd_h_put_signed_16 \
540 .#define bfd_h_get_16(abfd, ptr) \
541 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
542 .#define bfd_h_get_signed_16(abfd, ptr) \
543 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
545 .#define bfd_h_put_32(abfd, val, ptr) \
546 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
547 .#define bfd_h_put_signed_32 \
549 .#define bfd_h_get_32(abfd, ptr) \
550 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
551 .#define bfd_h_get_signed_32(abfd, ptr) \
552 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
554 .#define bfd_h_put_64(abfd, val, ptr) \
555 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
556 .#define bfd_h_put_signed_64 \
558 .#define bfd_h_get_64(abfd, ptr) \
559 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
560 .#define bfd_h_get_signed_64(abfd, ptr) \
561 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
563 .{* Aliases for the above, which should eventually go away. *}
565 .#define H_PUT_64 bfd_h_put_64
566 .#define H_PUT_32 bfd_h_put_32
567 .#define H_PUT_16 bfd_h_put_16
568 .#define H_PUT_8 bfd_h_put_8
569 .#define H_PUT_S64 bfd_h_put_signed_64
570 .#define H_PUT_S32 bfd_h_put_signed_32
571 .#define H_PUT_S16 bfd_h_put_signed_16
572 .#define H_PUT_S8 bfd_h_put_signed_8
573 .#define H_GET_64 bfd_h_get_64
574 .#define H_GET_32 bfd_h_get_32
575 .#define H_GET_16 bfd_h_get_16
576 .#define H_GET_8 bfd_h_get_8
577 .#define H_GET_S64 bfd_h_get_signed_64
578 .#define H_GET_S32 bfd_h_get_signed_32
579 .#define H_GET_S16 bfd_h_get_signed_16
580 .#define H_GET_S8 bfd_h_get_signed_8
584 /* Sign extension to bfd_signed_vma. */
585 #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000)
586 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
587 #define COERCE64(x) \
588 (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63))
591 bfd_getb16 (const void *p
)
593 const bfd_byte
*addr
= (const bfd_byte
*) p
;
594 return (addr
[0] << 8) | addr
[1];
598 bfd_getl16 (const void *p
)
600 const bfd_byte
*addr
= (const bfd_byte
*) p
;
601 return (addr
[1] << 8) | addr
[0];
605 bfd_getb_signed_16 (const void *p
)
607 const bfd_byte
*addr
= (const bfd_byte
*) p
;
608 return COERCE16 ((addr
[0] << 8) | addr
[1]);
612 bfd_getl_signed_16 (const void *p
)
614 const bfd_byte
*addr
= (const bfd_byte
*) p
;
615 return COERCE16 ((addr
[1] << 8) | addr
[0]);
619 bfd_putb16 (bfd_vma data
, void *p
)
621 bfd_byte
*addr
= (bfd_byte
*) p
;
622 addr
[0] = (data
>> 8) & 0xff;
623 addr
[1] = data
& 0xff;
627 bfd_putl16 (bfd_vma data
, void *p
)
629 bfd_byte
*addr
= (bfd_byte
*) p
;
630 addr
[0] = data
& 0xff;
631 addr
[1] = (data
>> 8) & 0xff;
635 bfd_putb24 (bfd_vma data
, void *p
)
637 bfd_byte
*addr
= (bfd_byte
*) p
;
638 addr
[0] = (data
>> 16) & 0xff;
639 addr
[1] = (data
>> 8) & 0xff;
640 addr
[2] = data
& 0xff;
644 bfd_putl24 (bfd_vma data
, void *p
)
646 bfd_byte
*addr
= (bfd_byte
*) p
;
647 addr
[0] = data
& 0xff;
648 addr
[1] = (data
>> 8) & 0xff;
649 addr
[2] = (data
>> 16) & 0xff;
653 bfd_getb24 (const void *p
)
655 const bfd_byte
*addr
= (const bfd_byte
*) p
;
658 v
= (unsigned long) addr
[0] << 16;
659 v
|= (unsigned long) addr
[1] << 8;
660 v
|= (unsigned long) addr
[2];
665 bfd_getl24 (const void *p
)
667 const bfd_byte
*addr
= (const bfd_byte
*) p
;
670 v
= (unsigned long) addr
[0];
671 v
|= (unsigned long) addr
[1] << 8;
672 v
|= (unsigned long) addr
[2] << 16;
677 bfd_getb32 (const void *p
)
679 const bfd_byte
*addr
= (const bfd_byte
*) p
;
682 v
= (unsigned long) addr
[0] << 24;
683 v
|= (unsigned long) addr
[1] << 16;
684 v
|= (unsigned long) addr
[2] << 8;
685 v
|= (unsigned long) addr
[3];
690 bfd_getl32 (const void *p
)
692 const bfd_byte
*addr
= (const bfd_byte
*) p
;
695 v
= (unsigned long) addr
[0];
696 v
|= (unsigned long) addr
[1] << 8;
697 v
|= (unsigned long) addr
[2] << 16;
698 v
|= (unsigned long) addr
[3] << 24;
703 bfd_getb_signed_32 (const void *p
)
705 const bfd_byte
*addr
= (const bfd_byte
*) p
;
708 v
= (unsigned long) addr
[0] << 24;
709 v
|= (unsigned long) addr
[1] << 16;
710 v
|= (unsigned long) addr
[2] << 8;
711 v
|= (unsigned long) addr
[3];
716 bfd_getl_signed_32 (const void *p
)
718 const bfd_byte
*addr
= (const bfd_byte
*) p
;
721 v
= (unsigned long) addr
[0];
722 v
|= (unsigned long) addr
[1] << 8;
723 v
|= (unsigned long) addr
[2] << 16;
724 v
|= (unsigned long) addr
[3] << 24;
729 bfd_getb64 (const void *p ATTRIBUTE_UNUSED
)
731 #ifdef BFD_HOST_64_BIT
732 const bfd_byte
*addr
= (const bfd_byte
*) p
;
735 v
= addr
[0]; v
<<= 8;
736 v
|= addr
[1]; v
<<= 8;
737 v
|= addr
[2]; v
<<= 8;
738 v
|= addr
[3]; v
<<= 8;
739 v
|= addr
[4]; v
<<= 8;
740 v
|= addr
[5]; v
<<= 8;
741 v
|= addr
[6]; v
<<= 8;
752 bfd_getl64 (const void *p ATTRIBUTE_UNUSED
)
754 #ifdef BFD_HOST_64_BIT
755 const bfd_byte
*addr
= (const bfd_byte
*) p
;
758 v
= addr
[7]; v
<<= 8;
759 v
|= addr
[6]; v
<<= 8;
760 v
|= addr
[5]; v
<<= 8;
761 v
|= addr
[4]; v
<<= 8;
762 v
|= addr
[3]; v
<<= 8;
763 v
|= addr
[2]; v
<<= 8;
764 v
|= addr
[1]; v
<<= 8;
776 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED
)
778 #ifdef BFD_HOST_64_BIT
779 const bfd_byte
*addr
= (const bfd_byte
*) p
;
782 v
= addr
[0]; v
<<= 8;
783 v
|= addr
[1]; v
<<= 8;
784 v
|= addr
[2]; v
<<= 8;
785 v
|= addr
[3]; v
<<= 8;
786 v
|= addr
[4]; v
<<= 8;
787 v
|= addr
[5]; v
<<= 8;
788 v
|= addr
[6]; v
<<= 8;
799 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED
)
801 #ifdef BFD_HOST_64_BIT
802 const bfd_byte
*addr
= (const bfd_byte
*) p
;
805 v
= addr
[7]; v
<<= 8;
806 v
|= addr
[6]; v
<<= 8;
807 v
|= addr
[5]; v
<<= 8;
808 v
|= addr
[4]; v
<<= 8;
809 v
|= addr
[3]; v
<<= 8;
810 v
|= addr
[2]; v
<<= 8;
811 v
|= addr
[1]; v
<<= 8;
822 bfd_putb32 (bfd_vma data
, void *p
)
824 bfd_byte
*addr
= (bfd_byte
*) p
;
825 addr
[0] = (data
>> 24) & 0xff;
826 addr
[1] = (data
>> 16) & 0xff;
827 addr
[2] = (data
>> 8) & 0xff;
828 addr
[3] = data
& 0xff;
832 bfd_putl32 (bfd_vma data
, void *p
)
834 bfd_byte
*addr
= (bfd_byte
*) p
;
835 addr
[0] = data
& 0xff;
836 addr
[1] = (data
>> 8) & 0xff;
837 addr
[2] = (data
>> 16) & 0xff;
838 addr
[3] = (data
>> 24) & 0xff;
842 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED
, void *p ATTRIBUTE_UNUSED
)
844 #ifdef BFD_HOST_64_BIT
845 bfd_byte
*addr
= (bfd_byte
*) p
;
846 addr
[0] = (data
>> (7*8)) & 0xff;
847 addr
[1] = (data
>> (6*8)) & 0xff;
848 addr
[2] = (data
>> (5*8)) & 0xff;
849 addr
[3] = (data
>> (4*8)) & 0xff;
850 addr
[4] = (data
>> (3*8)) & 0xff;
851 addr
[5] = (data
>> (2*8)) & 0xff;
852 addr
[6] = (data
>> (1*8)) & 0xff;
853 addr
[7] = (data
>> (0*8)) & 0xff;
860 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED
, void *p ATTRIBUTE_UNUSED
)
862 #ifdef BFD_HOST_64_BIT
863 bfd_byte
*addr
= (bfd_byte
*) p
;
864 addr
[7] = (data
>> (7*8)) & 0xff;
865 addr
[6] = (data
>> (6*8)) & 0xff;
866 addr
[5] = (data
>> (5*8)) & 0xff;
867 addr
[4] = (data
>> (4*8)) & 0xff;
868 addr
[3] = (data
>> (3*8)) & 0xff;
869 addr
[2] = (data
>> (2*8)) & 0xff;
870 addr
[1] = (data
>> (1*8)) & 0xff;
871 addr
[0] = (data
>> (0*8)) & 0xff;
878 bfd_put_bits (bfd_uint64_t data
, void *p
, int bits
, bfd_boolean big_p
)
880 bfd_byte
*addr
= (bfd_byte
*) p
;
888 for (i
= 0; i
< bytes
; i
++)
890 int addr_index
= big_p
? bytes
- i
- 1 : i
;
892 addr
[addr_index
] = data
& 0xff;
898 bfd_get_bits (const void *p
, int bits
, bfd_boolean big_p
)
900 const bfd_byte
*addr
= (const bfd_byte
*) p
;
910 for (i
= 0; i
< bytes
; i
++)
912 int addr_index
= big_p
? i
: bytes
- i
- 1;
914 data
= (data
<< 8) | addr
[addr_index
];
920 /* Default implementation */
923 _bfd_generic_get_section_contents (bfd
*abfd
,
933 if (section
->compress_status
!= COMPRESS_SECTION_NONE
)
936 /* xgettext:c-format */
937 (_("%pB: unable to get decompressed section %pA"),
939 bfd_set_error (bfd_error_invalid_operation
);
943 /* We do allow reading of a section after bfd_final_link has
944 written the contents out to disk. In that situation, rawsize is
945 just a stale version of size, so ignore it. Otherwise we must be
946 reading an input section, where rawsize, if different to size,
947 is the on-disk size. */
948 if (abfd
->direction
!= write_direction
&& section
->rawsize
!= 0)
949 sz
= section
->rawsize
;
952 if (offset
+ count
< count
953 || offset
+ count
> sz
954 || (abfd
->my_archive
!= NULL
955 && !bfd_is_thin_archive (abfd
->my_archive
)
956 && ((ufile_ptr
) section
->filepos
+ offset
+ count
957 > arelt_size (abfd
))))
959 bfd_set_error (bfd_error_invalid_operation
);
963 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
964 || bfd_bread (location
, count
, abfd
) != count
)
971 _bfd_generic_get_section_contents_in_window
972 (bfd
*abfd ATTRIBUTE_UNUSED
,
973 sec_ptr section ATTRIBUTE_UNUSED
,
974 bfd_window
*w ATTRIBUTE_UNUSED
,
975 file_ptr offset ATTRIBUTE_UNUSED
,
976 bfd_size_type count ATTRIBUTE_UNUSED
)
983 if (abfd
->xvec
->_bfd_get_section_contents
984 != _bfd_generic_get_section_contents
)
986 /* We don't know what changes the bfd's get_section_contents
987 method may have to make. So punt trying to map the file
988 window, and let get_section_contents do its thing. */
989 /* @@ FIXME : If the internal window has a refcount of 1 and was
990 allocated with malloc instead of mmap, just reuse it. */
992 w
->i
= bfd_zmalloc (sizeof (bfd_window_internal
));
995 w
->i
->data
= bfd_malloc (count
);
996 if (w
->i
->data
== NULL
)
1004 w
->size
= w
->i
->size
= count
;
1005 w
->data
= w
->i
->data
;
1006 return bfd_get_section_contents (abfd
, section
, w
->data
, offset
, count
);
1008 if (abfd
->direction
!= write_direction
&& section
->rawsize
!= 0)
1009 sz
= section
->rawsize
;
1012 if (offset
+ count
< count
1013 || offset
+ count
> sz
1014 || (abfd
->my_archive
!= NULL
1015 && !bfd_is_thin_archive (abfd
->my_archive
)
1016 && ((ufile_ptr
) section
->filepos
+ offset
+ count
1017 > arelt_size (abfd
)))
1018 || ! bfd_get_file_window (abfd
, section
->filepos
+ offset
, count
, w
,
1027 /* This generic function can only be used in implementations where creating
1028 NEW sections is disallowed. It is useful in patching existing sections
1029 in read-write files, though. See other set_section_contents functions
1030 to see why it doesn't work for new sections. */
1032 _bfd_generic_set_section_contents (bfd
*abfd
,
1034 const void *location
,
1036 bfd_size_type count
)
1041 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
1042 || bfd_bwrite (location
, count
, abfd
) != count
)
1053 unsigned int bfd_log2 (bfd_vma x);
1056 Return the log base 2 of the value supplied, rounded up. E.g., an
1057 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
1061 bfd_log2 (bfd_vma x
)
1063 unsigned int result
= 0;
1070 while ((x
>>= 1) != 0);
1075 bfd_generic_is_local_label_name (bfd
*abfd
, const char *name
)
1077 char locals_prefix
= (bfd_get_symbol_leading_char (abfd
) == '_') ? 'L' : '.';
1079 return name
[0] == locals_prefix
;
1082 /* Give a warning at runtime if someone compiles code which calls
1086 _bfd_warn_deprecated (const char *what
,
1091 /* Poor man's tracking of functions we've already warned about. */
1092 static size_t mask
= 0;
1094 if (~(size_t) func
& ~mask
)
1097 /* Note: separate sentences in order to allow
1098 for translation into other languages. */
1100 /* xgettext:c-format */
1101 fprintf (stderr
, _("Deprecated %s called at %s line %d in %s\n"),
1102 what
, file
, line
, func
);
1104 fprintf (stderr
, _("Deprecated %s called\n"), what
);
1106 mask
|= ~(size_t) func
;
1110 /* Helper function for reading uleb128 encoded data. */
1113 _bfd_read_unsigned_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
1115 unsigned int *bytes_read_ptr
)
1118 unsigned int num_read
;
1127 byte
= bfd_get_8 (abfd
, buf
);
1130 result
|= (((bfd_vma
) byte
& 0x7f) << shift
);
1133 while (byte
& 0x80);
1134 *bytes_read_ptr
= num_read
;
1138 /* Read in a LEB128 encoded value from ABFD starting at DATA.
1139 If SIGN is true, return a signed LEB128 value.
1140 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
1141 No bytes will be read at address END or beyond. */
1144 _bfd_safe_read_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
1146 unsigned int *length_return
,
1148 const bfd_byte
* const end
)
1151 unsigned int num_read
= 0;
1152 unsigned int shift
= 0;
1153 unsigned char byte
= 0;
1157 byte
= bfd_get_8 (abfd
, data
);
1161 result
|= ((bfd_vma
) (byte
& 0x7f)) << shift
;
1164 if ((byte
& 0x80) == 0)
1168 if (length_return
!= NULL
)
1169 *length_return
= num_read
;
1171 if (sign
&& (shift
< 8 * sizeof (result
)) && (byte
& 0x40))
1172 result
|= -((bfd_vma
) 1 << shift
);
1177 /* Helper function for reading sleb128 encoded data. */
1180 _bfd_read_signed_leb128 (bfd
*abfd ATTRIBUTE_UNUSED
,
1182 unsigned int *bytes_read_ptr
)
1186 unsigned int num_read
;
1194 byte
= bfd_get_8 (abfd
, buf
);
1197 result
|= (((bfd_vma
) byte
& 0x7f) << shift
);
1200 while (byte
& 0x80);
1201 if (shift
< 8 * sizeof (result
) && (byte
& 0x40))
1202 result
|= (((bfd_vma
) -1) << shift
);
1203 *bytes_read_ptr
= num_read
;
1208 _bfd_generic_init_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
1209 asection
*isec ATTRIBUTE_UNUSED
,
1210 bfd
*obfd ATTRIBUTE_UNUSED
,
1211 asection
*osec ATTRIBUTE_UNUSED
,
1212 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
)