1 /* Assorted BFD support routines, only used internally.
2 Copyright (C) 1990-2018 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
43 /* A routine which is used in target vectors for unsupported
47 bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
49 bfd_set_error (bfd_error_invalid_operation);
53 /* Like bfd_false, but accepts an arbitrary number of arguments. */
56 bfd_false_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
58 bfd_set_error (bfd_error_invalid_operation);
62 /* A routine which is used in target vectors for supported operations
63 which do not actually do anything. */
66 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
71 /* Like bfd_true, but accepts an arbitrary number of arguments. */
74 bfd_true_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
79 /* A routine which is used in target vectors for unsupported
80 operations which return a pointer value. */
83 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
85 bfd_set_error (bfd_error_invalid_operation);
89 /* Like bfd_nullvoidptr, but accepts an arbitrary number of arguments. */
92 bfd_nullvoidptr_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
94 bfd_set_error (bfd_error_invalid_operation);
99 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
104 /* Like bfd_0, but accepts an arbitrary number of arguments. */
107 bfd_0_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
113 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
118 /* Like bfd_0u, but accepts an arbitrary number of arguments. */
121 bfd_0u_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
127 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
132 /* Like bfd_0l, but accepts an arbitrary number of arguments. */
135 bfd_0l_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
140 /* A routine which is used in target vectors for unsupported
141 operations which return -1 on error. */
144 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
146 bfd_set_error (bfd_error_invalid_operation);
150 /* Like bfd_n1, but accepts an arbitrary number of arguments. */
153 _bfd_n1_any (bfd *ignore_abfd ATTRIBUTE_UNUSED, ...)
155 bfd_set_error (bfd_error_invalid_operation);
160 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
164 /* Like bfd_void, but accepts an arbitrary number of arguments. */
167 bfd_void_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
172 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
173 asection *sec ATTRIBUTE_UNUSED)
175 return sizeof (arelent *);
179 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
180 asection *sec ATTRIBUTE_UNUSED,
182 asymbol **symbols ATTRIBUTE_UNUSED)
189 _bfd_norelocs_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
190 asection *sec ATTRIBUTE_UNUSED,
191 arelent **relptr ATTRIBUTE_UNUSED,
192 unsigned int count ATTRIBUTE_UNUSED)
198 _bfd_nocore_core_file_matches_executable_p
199 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
200 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
202 bfd_set_error (bfd_error_invalid_operation);
206 /* Routine to handle core_file_failing_command entry point for targets
207 without core file support. */
210 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
212 bfd_set_error (bfd_error_invalid_operation);
216 /* Routine to handle core_file_failing_signal entry point for targets
217 without core file support. */
220 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
222 bfd_set_error (bfd_error_invalid_operation);
226 /* Routine to handle the core_file_pid entry point for targets without
227 core file support. */
230 _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
232 bfd_set_error (bfd_error_invalid_operation);
237 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
239 bfd_set_error (bfd_error_wrong_format);
243 /* Allocate memory using malloc. */
246 bfd_malloc (bfd_size_type size)
249 size_t sz = (size_t) size;
252 /* This is to pacify memory checkers like valgrind. */
253 || ((signed long) sz) < 0)
255 bfd_set_error (bfd_error_no_memory);
260 if (ptr == NULL && sz != 0)
261 bfd_set_error (bfd_error_no_memory);
266 /* Allocate memory using malloc, nmemb * size with overflow checking. */
269 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
271 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
273 && nmemb > ~(bfd_size_type) 0 / size)
275 bfd_set_error (bfd_error_no_memory);
279 return bfd_malloc (size * nmemb);
282 /* Reallocate memory using realloc. */
285 bfd_realloc (void *ptr, bfd_size_type size)
288 size_t sz = (size_t) size;
291 return bfd_malloc (size);
294 /* This is to pacify memory checkers like valgrind. */
295 || ((signed long) sz) < 0)
297 bfd_set_error (bfd_error_no_memory);
301 ret = realloc (ptr, sz);
303 if (ret == NULL && sz != 0)
304 bfd_set_error (bfd_error_no_memory);
309 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
312 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
314 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
316 && nmemb > ~(bfd_size_type) 0 / size)
318 bfd_set_error (bfd_error_no_memory);
322 return bfd_realloc (ptr, size * nmemb);
325 /* Reallocate memory using realloc.
326 If this fails the pointer is freed before returning. */
329 bfd_realloc_or_free (void *ptr, bfd_size_type size)
331 void *ret = bfd_realloc (ptr, size);
333 if (ret == NULL && ptr != NULL)
339 /* Allocate memory using malloc and clear it. */
342 bfd_zmalloc (bfd_size_type size)
344 void *ptr = bfd_malloc (size);
346 if (ptr != NULL && size > 0)
347 memset (ptr, 0, (size_t) size);
352 /* Allocate memory using malloc (nmemb * size) with overflow checking
356 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
358 void *ptr = bfd_malloc2 (nmemb, size);
362 size_t sz = nmemb * size;
373 bfd_write_bigendian_4byte_int
376 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
379 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
380 endian order regardless of what else is going on. This is useful in
385 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
388 bfd_putb32 ((bfd_vma) i, buffer);
389 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
393 /** The do-it-yourself (byte) sex-change kit */
395 /* The middle letter e.g. get<b>short indicates Big or Little endian
396 target machine. It doesn't matter what the byte order of the host
397 machine is; these routines work for either. */
399 /* FIXME: Should these take a count argument?
400 Answer (gnu@cygnus.com): No, but perhaps they should be inline
401 functions in swap.h #ifdef __GNUC__.
402 Gprof them later and find out. */
411 These macros as used for reading and writing raw data in
412 sections; each access (except for bytes) is vectored through
413 the target format of the BFD and mangled accordingly. The
414 mangling performs any necessary endian translations and
415 removes alignment restrictions. Note that types accepted and
416 returned by these macros are identical so they can be swapped
417 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
418 to either <<bfd_get_32>> or <<bfd_get_64>>.
420 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
421 system without prototypes, the caller is responsible for making
422 sure that is true, with a cast if necessary. We don't cast
423 them in the macro definitions because that would prevent <<lint>>
424 or <<gcc -Wall>> from detecting sins such as passing a pointer.
425 To detect calling these with less than a <<bfd_vma>>, use
426 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
429 .{* Byte swapping macros for user section data. *}
431 .#define bfd_put_8(abfd, val, ptr) \
432 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
433 .#define bfd_put_signed_8 \
435 .#define bfd_get_8(abfd, ptr) \
436 . (*(const unsigned char *) (ptr) & 0xff)
437 .#define bfd_get_signed_8(abfd, ptr) \
438 . (((*(const unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
440 .#define bfd_put_16(abfd, val, ptr) \
441 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
442 .#define bfd_put_signed_16 \
444 .#define bfd_get_16(abfd, ptr) \
445 . BFD_SEND (abfd, bfd_getx16, (ptr))
446 .#define bfd_get_signed_16(abfd, ptr) \
447 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
449 .#define bfd_put_32(abfd, val, ptr) \
450 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
451 .#define bfd_put_signed_32 \
453 .#define bfd_get_32(abfd, ptr) \
454 . BFD_SEND (abfd, bfd_getx32, (ptr))
455 .#define bfd_get_signed_32(abfd, ptr) \
456 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
458 .#define bfd_put_64(abfd, val, ptr) \
459 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
460 .#define bfd_put_signed_64 \
462 .#define bfd_get_64(abfd, ptr) \
463 . BFD_SEND (abfd, bfd_getx64, (ptr))
464 .#define bfd_get_signed_64(abfd, ptr) \
465 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
467 .#define bfd_get(bits, abfd, ptr) \
468 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
469 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
470 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
471 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
472 . : (abort (), (bfd_vma) - 1))
474 .#define bfd_put(bits, abfd, val, ptr) \
475 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
476 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
477 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
478 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
479 . : (abort (), (void) 0))
489 These macros have the same function as their <<bfd_get_x>>
490 brethren, except that they are used for removing information
491 for the header records of object files. Believe it or not,
492 some object files keep their header records in big endian
493 order and their data in little endian order.
495 .{* Byte swapping macros for file header data. *}
497 .#define bfd_h_put_8(abfd, val, ptr) \
498 . bfd_put_8 (abfd, val, ptr)
499 .#define bfd_h_put_signed_8(abfd, val, ptr) \
500 . bfd_put_8 (abfd, val, ptr)
501 .#define bfd_h_get_8(abfd, ptr) \
502 . bfd_get_8 (abfd, ptr)
503 .#define bfd_h_get_signed_8(abfd, ptr) \
504 . bfd_get_signed_8 (abfd, ptr)
506 .#define bfd_h_put_16(abfd, val, ptr) \
507 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
508 .#define bfd_h_put_signed_16 \
510 .#define bfd_h_get_16(abfd, ptr) \
511 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
512 .#define bfd_h_get_signed_16(abfd, ptr) \
513 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
515 .#define bfd_h_put_32(abfd, val, ptr) \
516 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
517 .#define bfd_h_put_signed_32 \
519 .#define bfd_h_get_32(abfd, ptr) \
520 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
521 .#define bfd_h_get_signed_32(abfd, ptr) \
522 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
524 .#define bfd_h_put_64(abfd, val, ptr) \
525 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
526 .#define bfd_h_put_signed_64 \
528 .#define bfd_h_get_64(abfd, ptr) \
529 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
530 .#define bfd_h_get_signed_64(abfd, ptr) \
531 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
533 .{* Aliases for the above, which should eventually go away. *}
535 .#define H_PUT_64 bfd_h_put_64
536 .#define H_PUT_32 bfd_h_put_32
537 .#define H_PUT_16 bfd_h_put_16
538 .#define H_PUT_8 bfd_h_put_8
539 .#define H_PUT_S64 bfd_h_put_signed_64
540 .#define H_PUT_S32 bfd_h_put_signed_32
541 .#define H_PUT_S16 bfd_h_put_signed_16
542 .#define H_PUT_S8 bfd_h_put_signed_8
543 .#define H_GET_64 bfd_h_get_64
544 .#define H_GET_32 bfd_h_get_32
545 .#define H_GET_16 bfd_h_get_16
546 .#define H_GET_8 bfd_h_get_8
547 .#define H_GET_S64 bfd_h_get_signed_64
548 .#define H_GET_S32 bfd_h_get_signed_32
549 .#define H_GET_S16 bfd_h_get_signed_16
550 .#define H_GET_S8 bfd_h_get_signed_8
554 /* Sign extension to bfd_signed_vma. */
555 #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000)
556 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
557 #define COERCE64(x) \
558 (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63))
561 bfd_getb16 (const void *p)
563 const bfd_byte *addr = (const bfd_byte *) p;
564 return (addr[0] << 8) | addr[1];
568 bfd_getl16 (const void *p)
570 const bfd_byte *addr = (const bfd_byte *) p;
571 return (addr[1] << 8) | addr[0];
575 bfd_getb_signed_16 (const void *p)
577 const bfd_byte *addr = (const bfd_byte *) p;
578 return COERCE16 ((addr[0] << 8) | addr[1]);
582 bfd_getl_signed_16 (const void *p)
584 const bfd_byte *addr = (const bfd_byte *) p;
585 return COERCE16 ((addr[1] << 8) | addr[0]);
589 bfd_putb16 (bfd_vma data, void *p)
591 bfd_byte *addr = (bfd_byte *) p;
592 addr[0] = (data >> 8) & 0xff;
593 addr[1] = data & 0xff;
597 bfd_putl16 (bfd_vma data, void *p)
599 bfd_byte *addr = (bfd_byte *) p;
600 addr[0] = data & 0xff;
601 addr[1] = (data >> 8) & 0xff;
605 bfd_getb32 (const void *p)
607 const bfd_byte *addr = (const bfd_byte *) p;
610 v = (unsigned long) addr[0] << 24;
611 v |= (unsigned long) addr[1] << 16;
612 v |= (unsigned long) addr[2] << 8;
613 v |= (unsigned long) addr[3];
618 bfd_getl32 (const void *p)
620 const bfd_byte *addr = (const bfd_byte *) p;
623 v = (unsigned long) addr[0];
624 v |= (unsigned long) addr[1] << 8;
625 v |= (unsigned long) addr[2] << 16;
626 v |= (unsigned long) addr[3] << 24;
631 bfd_getb_signed_32 (const void *p)
633 const bfd_byte *addr = (const bfd_byte *) p;
636 v = (unsigned long) addr[0] << 24;
637 v |= (unsigned long) addr[1] << 16;
638 v |= (unsigned long) addr[2] << 8;
639 v |= (unsigned long) addr[3];
644 bfd_getl_signed_32 (const void *p)
646 const bfd_byte *addr = (const bfd_byte *) p;
649 v = (unsigned long) addr[0];
650 v |= (unsigned long) addr[1] << 8;
651 v |= (unsigned long) addr[2] << 16;
652 v |= (unsigned long) addr[3] << 24;
657 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
659 #ifdef BFD_HOST_64_BIT
660 const bfd_byte *addr = (const bfd_byte *) p;
663 v = addr[0]; v <<= 8;
664 v |= addr[1]; v <<= 8;
665 v |= addr[2]; v <<= 8;
666 v |= addr[3]; v <<= 8;
667 v |= addr[4]; v <<= 8;
668 v |= addr[5]; v <<= 8;
669 v |= addr[6]; v <<= 8;
680 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
682 #ifdef BFD_HOST_64_BIT
683 const bfd_byte *addr = (const bfd_byte *) p;
686 v = addr[7]; v <<= 8;
687 v |= addr[6]; v <<= 8;
688 v |= addr[5]; v <<= 8;
689 v |= addr[4]; v <<= 8;
690 v |= addr[3]; v <<= 8;
691 v |= addr[2]; v <<= 8;
692 v |= addr[1]; v <<= 8;
704 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
706 #ifdef BFD_HOST_64_BIT
707 const bfd_byte *addr = (const bfd_byte *) p;
710 v = addr[0]; v <<= 8;
711 v |= addr[1]; v <<= 8;
712 v |= addr[2]; v <<= 8;
713 v |= addr[3]; v <<= 8;
714 v |= addr[4]; v <<= 8;
715 v |= addr[5]; v <<= 8;
716 v |= addr[6]; v <<= 8;
727 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
729 #ifdef BFD_HOST_64_BIT
730 const bfd_byte *addr = (const bfd_byte *) p;
733 v = addr[7]; v <<= 8;
734 v |= addr[6]; v <<= 8;
735 v |= addr[5]; v <<= 8;
736 v |= addr[4]; v <<= 8;
737 v |= addr[3]; v <<= 8;
738 v |= addr[2]; v <<= 8;
739 v |= addr[1]; v <<= 8;
750 bfd_putb32 (bfd_vma data, void *p)
752 bfd_byte *addr = (bfd_byte *) p;
753 addr[0] = (data >> 24) & 0xff;
754 addr[1] = (data >> 16) & 0xff;
755 addr[2] = (data >> 8) & 0xff;
756 addr[3] = data & 0xff;
760 bfd_putl32 (bfd_vma data, void *p)
762 bfd_byte *addr = (bfd_byte *) p;
763 addr[0] = data & 0xff;
764 addr[1] = (data >> 8) & 0xff;
765 addr[2] = (data >> 16) & 0xff;
766 addr[3] = (data >> 24) & 0xff;
770 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
772 #ifdef BFD_HOST_64_BIT
773 bfd_byte *addr = (bfd_byte *) p;
774 addr[0] = (data >> (7*8)) & 0xff;
775 addr[1] = (data >> (6*8)) & 0xff;
776 addr[2] = (data >> (5*8)) & 0xff;
777 addr[3] = (data >> (4*8)) & 0xff;
778 addr[4] = (data >> (3*8)) & 0xff;
779 addr[5] = (data >> (2*8)) & 0xff;
780 addr[6] = (data >> (1*8)) & 0xff;
781 addr[7] = (data >> (0*8)) & 0xff;
788 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
790 #ifdef BFD_HOST_64_BIT
791 bfd_byte *addr = (bfd_byte *) p;
792 addr[7] = (data >> (7*8)) & 0xff;
793 addr[6] = (data >> (6*8)) & 0xff;
794 addr[5] = (data >> (5*8)) & 0xff;
795 addr[4] = (data >> (4*8)) & 0xff;
796 addr[3] = (data >> (3*8)) & 0xff;
797 addr[2] = (data >> (2*8)) & 0xff;
798 addr[1] = (data >> (1*8)) & 0xff;
799 addr[0] = (data >> (0*8)) & 0xff;
806 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
808 bfd_byte *addr = (bfd_byte *) p;
816 for (i = 0; i < bytes; i++)
818 int addr_index = big_p ? bytes - i - 1 : i;
820 addr[addr_index] = data & 0xff;
826 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
828 const bfd_byte *addr = (const bfd_byte *) p;
838 for (i = 0; i < bytes; i++)
840 int addr_index = big_p ? i : bytes - i - 1;
842 data = (data << 8) | addr[addr_index];
848 /* Default implementation */
851 _bfd_generic_get_section_contents (bfd *abfd,
861 if (section->compress_status != COMPRESS_SECTION_NONE)
864 /* xgettext:c-format */
865 (_("%B: unable to get decompressed section %A"),
867 bfd_set_error (bfd_error_invalid_operation);
871 /* We do allow reading of a section after bfd_final_link has
872 written the contents out to disk. In that situation, rawsize is
873 just a stale version of size, so ignore it. Otherwise we must be
874 reading an input section, where rawsize, if different to size,
875 is the on-disk size. */
876 if (abfd->direction != write_direction && section->rawsize != 0)
877 sz = section->rawsize;
880 if (offset + count < count
881 || offset + count > sz
882 || (abfd->my_archive != NULL
883 && !bfd_is_thin_archive (abfd->my_archive)
884 && ((ufile_ptr) section->filepos + offset + count
885 > arelt_size (abfd))))
887 bfd_set_error (bfd_error_invalid_operation);
891 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
892 || bfd_bread (location, count, abfd) != count)
899 _bfd_generic_get_section_contents_in_window
900 (bfd *abfd ATTRIBUTE_UNUSED,
901 sec_ptr section ATTRIBUTE_UNUSED,
902 bfd_window *w ATTRIBUTE_UNUSED,
903 file_ptr offset ATTRIBUTE_UNUSED,
904 bfd_size_type count ATTRIBUTE_UNUSED)
911 if (abfd->xvec->_bfd_get_section_contents
912 != _bfd_generic_get_section_contents)
914 /* We don't know what changes the bfd's get_section_contents
915 method may have to make. So punt trying to map the file
916 window, and let get_section_contents do its thing. */
917 /* @@ FIXME : If the internal window has a refcount of 1 and was
918 allocated with malloc instead of mmap, just reuse it. */
920 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
923 w->i->data = bfd_malloc (count);
924 if (w->i->data == NULL)
932 w->size = w->i->size = count;
933 w->data = w->i->data;
934 return bfd_get_section_contents (abfd, section, w->data, offset, count);
936 if (abfd->direction != write_direction && section->rawsize != 0)
937 sz = section->rawsize;
940 if (offset + count < count
941 || offset + count > sz
942 || (abfd->my_archive != NULL
943 && !bfd_is_thin_archive (abfd->my_archive)
944 && ((ufile_ptr) section->filepos + offset + count
945 > arelt_size (abfd)))
946 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
955 /* This generic function can only be used in implementations where creating
956 NEW sections is disallowed. It is useful in patching existing sections
957 in read-write files, though. See other set_section_contents functions
958 to see why it doesn't work for new sections. */
960 _bfd_generic_set_section_contents (bfd *abfd,
962 const void *location,
969 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
970 || bfd_bwrite (location, count, abfd) != count)
981 unsigned int bfd_log2 (bfd_vma x);
984 Return the log base 2 of the value supplied, rounded up. E.g., an
985 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
991 unsigned int result = 0;
998 while ((x >>= 1) != 0);
1003 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
1005 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1007 return name[0] == locals_prefix;
1010 /* Give a warning at runtime if someone compiles code which calls
1014 _bfd_warn_deprecated (const char *what,
1019 /* Poor man's tracking of functions we've already warned about. */
1020 static size_t mask = 0;
1022 if (~(size_t) func & ~mask)
1025 /* Note: separate sentences in order to allow
1026 for translation into other languages. */
1028 /* xgettext:c-format */
1029 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
1030 what, file, line, func);
1032 fprintf (stderr, _("Deprecated %s called\n"), what);
1034 mask |= ~(size_t) func;
1038 /* Helper function for reading uleb128 encoded data. */
1041 _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1043 unsigned int *bytes_read_ptr)
1046 unsigned int num_read;
1055 byte = bfd_get_8 (abfd, buf);
1058 result |= (((bfd_vma) byte & 0x7f) << shift);
1061 while (byte & 0x80);
1062 *bytes_read_ptr = num_read;
1066 /* Read in a LEB128 encoded value from ABFD starting at DATA.
1067 If SIGN is true, return a signed LEB128 value.
1068 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
1069 No bytes will be read at address END or beyond. */
1072 _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1074 unsigned int *length_return,
1076 const bfd_byte * const end)
1079 unsigned int num_read = 0;
1080 unsigned int shift = 0;
1081 unsigned char byte = 0;
1085 byte = bfd_get_8 (abfd, data);
1089 result |= ((bfd_vma) (byte & 0x7f)) << shift;
1092 if ((byte & 0x80) == 0)
1096 if (length_return != NULL)
1097 *length_return = num_read;
1099 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
1100 result |= -((bfd_vma) 1 << shift);
1105 /* Helper function for reading sleb128 encoded data. */
1108 _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1110 unsigned int *bytes_read_ptr)
1114 unsigned int num_read;
1122 byte = bfd_get_8 (abfd, buf);
1125 result |= (((bfd_vma) byte & 0x7f) << shift);
1128 while (byte & 0x80);
1129 if (shift < 8 * sizeof (result) && (byte & 0x40))
1130 result |= (((bfd_vma) -1) << shift);
1131 *bytes_read_ptr = num_read;
1136 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1137 asection *isec ATTRIBUTE_UNUSED,
1138 bfd *obfd ATTRIBUTE_UNUSED,
1139 asection *osec ATTRIBUTE_UNUSED,
1140 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)