* elf32-ppc.c (ppc_elf_check_relocs): Set pointer_equality_needed
[binutils.git] / bfd / libbfd.c
bloba225397c3e780fd18b39c940d19f78edd3fad542
1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libbfd.h"
28 #ifndef HAVE_GETPAGESIZE
29 #define getpagesize() 2048
30 #endif
33 SECTION
34 Implementation details
36 SUBSECTION
37 Internal functions
39 DESCRIPTION
40 These routines are used within BFD.
41 They are not intended for export, but are documented here for
42 completeness.
45 /* A routine which is used in target vectors for unsupported
46 operations. */
48 bfd_boolean
49 bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
51 bfd_set_error (bfd_error_invalid_operation);
52 return FALSE;
55 /* A routine which is used in target vectors for supported operations
56 which do not actually do anything. */
58 bfd_boolean
59 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
61 return TRUE;
64 /* A routine which is used in target vectors for unsupported
65 operations which return a pointer value. */
67 void *
68 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
70 bfd_set_error (bfd_error_invalid_operation);
71 return NULL;
74 int
75 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
77 return 0;
80 unsigned int
81 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
83 return 0;
86 long
87 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
89 return 0;
92 /* A routine which is used in target vectors for unsupported
93 operations which return -1 on error. */
95 long
96 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
98 bfd_set_error (bfd_error_invalid_operation);
99 return -1;
102 void
103 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
107 long
108 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
109 asection *sec ATTRIBUTE_UNUSED)
111 return sizeof (arelent *);
114 long
115 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
116 asection *sec ATTRIBUTE_UNUSED,
117 arelent **relptr,
118 asymbol **symbols ATTRIBUTE_UNUSED)
120 *relptr = NULL;
121 return 0;
124 bfd_boolean
125 _bfd_nocore_core_file_matches_executable_p
126 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
127 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
129 bfd_set_error (bfd_error_invalid_operation);
130 return FALSE;
133 /* Routine to handle core_file_failing_command entry point for targets
134 without core file support. */
136 char *
137 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
139 bfd_set_error (bfd_error_invalid_operation);
140 return NULL;
143 /* Routine to handle core_file_failing_signal entry point for targets
144 without core file support. */
147 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
149 bfd_set_error (bfd_error_invalid_operation);
150 return 0;
153 const bfd_target *
154 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
156 bfd_set_error (bfd_error_wrong_format);
157 return 0;
160 /* Allocate memory using malloc. */
162 void *
163 bfd_malloc (bfd_size_type size)
165 void *ptr;
167 if (size != (size_t) size)
169 bfd_set_error (bfd_error_no_memory);
170 return NULL;
173 ptr = malloc ((size_t) size);
174 if (ptr == NULL && (size_t) size != 0)
175 bfd_set_error (bfd_error_no_memory);
177 return ptr;
180 /* Allocate memory using malloc, nmemb * size with overflow checking. */
182 void *
183 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
185 void *ptr;
187 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
188 && size != 0
189 && nmemb > ~(bfd_size_type) 0 / size)
191 bfd_set_error (bfd_error_no_memory);
192 return NULL;
195 size *= nmemb;
197 if (size != (size_t) size)
199 bfd_set_error (bfd_error_no_memory);
200 return NULL;
203 ptr = malloc ((size_t) size);
204 if (ptr == NULL && (size_t) size != 0)
205 bfd_set_error (bfd_error_no_memory);
207 return ptr;
210 /* Reallocate memory using realloc. */
212 void *
213 bfd_realloc (void *ptr, bfd_size_type size)
215 void *ret;
217 if (size != (size_t) size)
219 bfd_set_error (bfd_error_no_memory);
220 return NULL;
223 if (ptr == NULL)
224 ret = malloc ((size_t) size);
225 else
226 ret = realloc (ptr, (size_t) size);
228 if (ret == NULL && (size_t) size != 0)
229 bfd_set_error (bfd_error_no_memory);
231 return ret;
234 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
236 void *
237 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
239 void *ret;
241 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
242 && size != 0
243 && nmemb > ~(bfd_size_type) 0 / size)
245 bfd_set_error (bfd_error_no_memory);
246 return NULL;
249 size *= nmemb;
251 if (size != (size_t) size)
253 bfd_set_error (bfd_error_no_memory);
254 return NULL;
257 if (ptr == NULL)
258 ret = malloc ((size_t) size);
259 else
260 ret = realloc (ptr, (size_t) size);
262 if (ret == NULL && (size_t) size != 0)
263 bfd_set_error (bfd_error_no_memory);
265 return ret;
268 /* Reallocate memory using realloc.
269 If this fails the pointer is freed before returning. */
271 void *
272 bfd_realloc_or_free (void *ptr, bfd_size_type size)
274 size_t amount = (size_t) size;
275 void *ret;
277 if (size != amount)
278 ret = NULL;
279 else if (ptr == NULL)
280 ret = malloc (amount);
281 else
282 ret = realloc (ptr, amount);
284 if (ret == NULL)
286 if (amount > 0)
287 bfd_set_error (bfd_error_no_memory);
289 if (ptr != NULL)
290 free (ptr);
293 return ret;
296 /* Allocate memory using malloc and clear it. */
298 void *
299 bfd_zmalloc (bfd_size_type size)
301 void *ptr;
303 if (size != (size_t) size)
305 bfd_set_error (bfd_error_no_memory);
306 return NULL;
309 ptr = malloc ((size_t) size);
311 if ((size_t) size != 0)
313 if (ptr == NULL)
314 bfd_set_error (bfd_error_no_memory);
315 else
316 memset (ptr, 0, (size_t) size);
319 return ptr;
322 /* Allocate memory using malloc (nmemb * size) with overflow checking
323 and clear it. */
325 void *
326 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
328 void *ptr;
330 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
331 && size != 0
332 && nmemb > ~(bfd_size_type) 0 / size)
334 bfd_set_error (bfd_error_no_memory);
335 return NULL;
338 size *= nmemb;
340 if (size != (size_t) size)
342 bfd_set_error (bfd_error_no_memory);
343 return NULL;
346 ptr = malloc ((size_t) size);
348 if ((size_t) size != 0)
350 if (ptr == NULL)
351 bfd_set_error (bfd_error_no_memory);
352 else
353 memset (ptr, 0, (size_t) size);
356 return ptr;
360 INTERNAL_FUNCTION
361 bfd_write_bigendian_4byte_int
363 SYNOPSIS
364 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
366 DESCRIPTION
367 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
368 endian order regardless of what else is going on. This is useful in
369 archives.
372 bfd_boolean
373 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
375 bfd_byte buffer[4];
376 bfd_putb32 ((bfd_vma) i, buffer);
377 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
381 /** The do-it-yourself (byte) sex-change kit */
383 /* The middle letter e.g. get<b>short indicates Big or Little endian
384 target machine. It doesn't matter what the byte order of the host
385 machine is; these routines work for either. */
387 /* FIXME: Should these take a count argument?
388 Answer (gnu@cygnus.com): No, but perhaps they should be inline
389 functions in swap.h #ifdef __GNUC__.
390 Gprof them later and find out. */
393 FUNCTION
394 bfd_put_size
395 FUNCTION
396 bfd_get_size
398 DESCRIPTION
399 These macros as used for reading and writing raw data in
400 sections; each access (except for bytes) is vectored through
401 the target format of the BFD and mangled accordingly. The
402 mangling performs any necessary endian translations and
403 removes alignment restrictions. Note that types accepted and
404 returned by these macros are identical so they can be swapped
405 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
406 to either <<bfd_get_32>> or <<bfd_get_64>>.
408 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
409 system without prototypes, the caller is responsible for making
410 sure that is true, with a cast if necessary. We don't cast
411 them in the macro definitions because that would prevent <<lint>>
412 or <<gcc -Wall>> from detecting sins such as passing a pointer.
413 To detect calling these with less than a <<bfd_vma>>, use
414 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
417 .{* Byte swapping macros for user section data. *}
419 .#define bfd_put_8(abfd, val, ptr) \
420 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
421 .#define bfd_put_signed_8 \
422 . bfd_put_8
423 .#define bfd_get_8(abfd, ptr) \
424 . (*(unsigned char *) (ptr) & 0xff)
425 .#define bfd_get_signed_8(abfd, ptr) \
426 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
428 .#define bfd_put_16(abfd, val, ptr) \
429 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
430 .#define bfd_put_signed_16 \
431 . bfd_put_16
432 .#define bfd_get_16(abfd, ptr) \
433 . BFD_SEND (abfd, bfd_getx16, (ptr))
434 .#define bfd_get_signed_16(abfd, ptr) \
435 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
437 .#define bfd_put_32(abfd, val, ptr) \
438 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
439 .#define bfd_put_signed_32 \
440 . bfd_put_32
441 .#define bfd_get_32(abfd, ptr) \
442 . BFD_SEND (abfd, bfd_getx32, (ptr))
443 .#define bfd_get_signed_32(abfd, ptr) \
444 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
446 .#define bfd_put_64(abfd, val, ptr) \
447 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
448 .#define bfd_put_signed_64 \
449 . bfd_put_64
450 .#define bfd_get_64(abfd, ptr) \
451 . BFD_SEND (abfd, bfd_getx64, (ptr))
452 .#define bfd_get_signed_64(abfd, ptr) \
453 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
455 .#define bfd_get(bits, abfd, ptr) \
456 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
457 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
458 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
459 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
460 . : (abort (), (bfd_vma) - 1))
462 .#define bfd_put(bits, abfd, val, ptr) \
463 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
464 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
465 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
466 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
467 . : (abort (), (void) 0))
472 FUNCTION
473 bfd_h_put_size
474 bfd_h_get_size
476 DESCRIPTION
477 These macros have the same function as their <<bfd_get_x>>
478 brethren, except that they are used for removing information
479 for the header records of object files. Believe it or not,
480 some object files keep their header records in big endian
481 order and their data in little endian order.
483 .{* Byte swapping macros for file header data. *}
485 .#define bfd_h_put_8(abfd, val, ptr) \
486 . bfd_put_8 (abfd, val, ptr)
487 .#define bfd_h_put_signed_8(abfd, val, ptr) \
488 . bfd_put_8 (abfd, val, ptr)
489 .#define bfd_h_get_8(abfd, ptr) \
490 . bfd_get_8 (abfd, ptr)
491 .#define bfd_h_get_signed_8(abfd, ptr) \
492 . bfd_get_signed_8 (abfd, ptr)
494 .#define bfd_h_put_16(abfd, val, ptr) \
495 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
496 .#define bfd_h_put_signed_16 \
497 . bfd_h_put_16
498 .#define bfd_h_get_16(abfd, ptr) \
499 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
500 .#define bfd_h_get_signed_16(abfd, ptr) \
501 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
503 .#define bfd_h_put_32(abfd, val, ptr) \
504 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
505 .#define bfd_h_put_signed_32 \
506 . bfd_h_put_32
507 .#define bfd_h_get_32(abfd, ptr) \
508 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
509 .#define bfd_h_get_signed_32(abfd, ptr) \
510 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
512 .#define bfd_h_put_64(abfd, val, ptr) \
513 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
514 .#define bfd_h_put_signed_64 \
515 . bfd_h_put_64
516 .#define bfd_h_get_64(abfd, ptr) \
517 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
518 .#define bfd_h_get_signed_64(abfd, ptr) \
519 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
521 .{* Aliases for the above, which should eventually go away. *}
523 .#define H_PUT_64 bfd_h_put_64
524 .#define H_PUT_32 bfd_h_put_32
525 .#define H_PUT_16 bfd_h_put_16
526 .#define H_PUT_8 bfd_h_put_8
527 .#define H_PUT_S64 bfd_h_put_signed_64
528 .#define H_PUT_S32 bfd_h_put_signed_32
529 .#define H_PUT_S16 bfd_h_put_signed_16
530 .#define H_PUT_S8 bfd_h_put_signed_8
531 .#define H_GET_64 bfd_h_get_64
532 .#define H_GET_32 bfd_h_get_32
533 .#define H_GET_16 bfd_h_get_16
534 .#define H_GET_8 bfd_h_get_8
535 .#define H_GET_S64 bfd_h_get_signed_64
536 .#define H_GET_S32 bfd_h_get_signed_32
537 .#define H_GET_S16 bfd_h_get_signed_16
538 .#define H_GET_S8 bfd_h_get_signed_8
542 /* Sign extension to bfd_signed_vma. */
543 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
544 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
545 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
546 #define COERCE64(x) \
547 (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
549 bfd_vma
550 bfd_getb16 (const void *p)
552 const bfd_byte *addr = p;
553 return (addr[0] << 8) | addr[1];
556 bfd_vma
557 bfd_getl16 (const void *p)
559 const bfd_byte *addr = p;
560 return (addr[1] << 8) | addr[0];
563 bfd_signed_vma
564 bfd_getb_signed_16 (const void *p)
566 const bfd_byte *addr = p;
567 return COERCE16 ((addr[0] << 8) | addr[1]);
570 bfd_signed_vma
571 bfd_getl_signed_16 (const void *p)
573 const bfd_byte *addr = p;
574 return COERCE16 ((addr[1] << 8) | addr[0]);
577 void
578 bfd_putb16 (bfd_vma data, void *p)
580 bfd_byte *addr = p;
581 addr[0] = (data >> 8) & 0xff;
582 addr[1] = data & 0xff;
585 void
586 bfd_putl16 (bfd_vma data, void *p)
588 bfd_byte *addr = p;
589 addr[0] = data & 0xff;
590 addr[1] = (data >> 8) & 0xff;
593 bfd_vma
594 bfd_getb32 (const void *p)
596 const bfd_byte *addr = p;
597 unsigned long v;
599 v = (unsigned long) addr[0] << 24;
600 v |= (unsigned long) addr[1] << 16;
601 v |= (unsigned long) addr[2] << 8;
602 v |= (unsigned long) addr[3];
603 return v;
606 bfd_vma
607 bfd_getl32 (const void *p)
609 const bfd_byte *addr = p;
610 unsigned long v;
612 v = (unsigned long) addr[0];
613 v |= (unsigned long) addr[1] << 8;
614 v |= (unsigned long) addr[2] << 16;
615 v |= (unsigned long) addr[3] << 24;
616 return v;
619 bfd_signed_vma
620 bfd_getb_signed_32 (const void *p)
622 const bfd_byte *addr = p;
623 unsigned long v;
625 v = (unsigned long) addr[0] << 24;
626 v |= (unsigned long) addr[1] << 16;
627 v |= (unsigned long) addr[2] << 8;
628 v |= (unsigned long) addr[3];
629 return COERCE32 (v);
632 bfd_signed_vma
633 bfd_getl_signed_32 (const void *p)
635 const bfd_byte *addr = p;
636 unsigned long v;
638 v = (unsigned long) addr[0];
639 v |= (unsigned long) addr[1] << 8;
640 v |= (unsigned long) addr[2] << 16;
641 v |= (unsigned long) addr[3] << 24;
642 return COERCE32 (v);
645 bfd_uint64_t
646 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
648 #ifdef BFD_HOST_64_BIT
649 const bfd_byte *addr = p;
650 bfd_uint64_t v;
652 v = addr[0]; v <<= 8;
653 v |= addr[1]; v <<= 8;
654 v |= addr[2]; v <<= 8;
655 v |= addr[3]; v <<= 8;
656 v |= addr[4]; v <<= 8;
657 v |= addr[5]; v <<= 8;
658 v |= addr[6]; v <<= 8;
659 v |= addr[7];
661 return v;
662 #else
663 BFD_FAIL();
664 return 0;
665 #endif
668 bfd_uint64_t
669 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
671 #ifdef BFD_HOST_64_BIT
672 const bfd_byte *addr = p;
673 bfd_uint64_t v;
675 v = addr[7]; v <<= 8;
676 v |= addr[6]; v <<= 8;
677 v |= addr[5]; v <<= 8;
678 v |= addr[4]; v <<= 8;
679 v |= addr[3]; v <<= 8;
680 v |= addr[2]; v <<= 8;
681 v |= addr[1]; v <<= 8;
682 v |= addr[0];
684 return v;
685 #else
686 BFD_FAIL();
687 return 0;
688 #endif
692 bfd_int64_t
693 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
695 #ifdef BFD_HOST_64_BIT
696 const bfd_byte *addr = p;
697 bfd_uint64_t v;
699 v = addr[0]; v <<= 8;
700 v |= addr[1]; v <<= 8;
701 v |= addr[2]; v <<= 8;
702 v |= addr[3]; v <<= 8;
703 v |= addr[4]; v <<= 8;
704 v |= addr[5]; v <<= 8;
705 v |= addr[6]; v <<= 8;
706 v |= addr[7];
708 return COERCE64 (v);
709 #else
710 BFD_FAIL();
711 return 0;
712 #endif
715 bfd_int64_t
716 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
718 #ifdef BFD_HOST_64_BIT
719 const bfd_byte *addr = p;
720 bfd_uint64_t v;
722 v = addr[7]; v <<= 8;
723 v |= addr[6]; v <<= 8;
724 v |= addr[5]; v <<= 8;
725 v |= addr[4]; v <<= 8;
726 v |= addr[3]; v <<= 8;
727 v |= addr[2]; v <<= 8;
728 v |= addr[1]; v <<= 8;
729 v |= addr[0];
731 return COERCE64 (v);
732 #else
733 BFD_FAIL();
734 return 0;
735 #endif
738 void
739 bfd_putb32 (bfd_vma data, void *p)
741 bfd_byte *addr = p;
742 addr[0] = (data >> 24) & 0xff;
743 addr[1] = (data >> 16) & 0xff;
744 addr[2] = (data >> 8) & 0xff;
745 addr[3] = data & 0xff;
748 void
749 bfd_putl32 (bfd_vma data, void *p)
751 bfd_byte *addr = p;
752 addr[0] = data & 0xff;
753 addr[1] = (data >> 8) & 0xff;
754 addr[2] = (data >> 16) & 0xff;
755 addr[3] = (data >> 24) & 0xff;
758 void
759 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
761 #ifdef BFD_HOST_64_BIT
762 bfd_byte *addr = p;
763 addr[0] = (data >> (7*8)) & 0xff;
764 addr[1] = (data >> (6*8)) & 0xff;
765 addr[2] = (data >> (5*8)) & 0xff;
766 addr[3] = (data >> (4*8)) & 0xff;
767 addr[4] = (data >> (3*8)) & 0xff;
768 addr[5] = (data >> (2*8)) & 0xff;
769 addr[6] = (data >> (1*8)) & 0xff;
770 addr[7] = (data >> (0*8)) & 0xff;
771 #else
772 BFD_FAIL();
773 #endif
776 void
777 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
779 #ifdef BFD_HOST_64_BIT
780 bfd_byte *addr = p;
781 addr[7] = (data >> (7*8)) & 0xff;
782 addr[6] = (data >> (6*8)) & 0xff;
783 addr[5] = (data >> (5*8)) & 0xff;
784 addr[4] = (data >> (4*8)) & 0xff;
785 addr[3] = (data >> (3*8)) & 0xff;
786 addr[2] = (data >> (2*8)) & 0xff;
787 addr[1] = (data >> (1*8)) & 0xff;
788 addr[0] = (data >> (0*8)) & 0xff;
789 #else
790 BFD_FAIL();
791 #endif
794 void
795 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
797 bfd_byte *addr = p;
798 int i;
799 int bytes;
801 if (bits % 8 != 0)
802 abort ();
804 bytes = bits / 8;
805 for (i = 0; i < bytes; i++)
807 int index = big_p ? bytes - i - 1 : i;
809 addr[index] = data & 0xff;
810 data >>= 8;
814 bfd_uint64_t
815 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
817 const bfd_byte *addr = p;
818 bfd_uint64_t data;
819 int i;
820 int bytes;
822 if (bits % 8 != 0)
823 abort ();
825 data = 0;
826 bytes = bits / 8;
827 for (i = 0; i < bytes; i++)
829 int index = big_p ? i : bytes - i - 1;
831 data = (data << 8) | addr[index];
834 return data;
837 /* Default implementation */
839 bfd_boolean
840 _bfd_generic_get_section_contents (bfd *abfd,
841 sec_ptr section,
842 void *location,
843 file_ptr offset,
844 bfd_size_type count)
846 bfd_size_type sz;
847 if (count == 0)
848 return TRUE;
850 sz = section->rawsize ? section->rawsize : section->size;
851 if (offset + count < count
852 || offset + count > sz)
854 bfd_set_error (bfd_error_invalid_operation);
855 return FALSE;
858 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
859 || bfd_bread (location, count, abfd) != count)
860 return FALSE;
862 return TRUE;
865 bfd_boolean
866 _bfd_generic_get_section_contents_in_window
867 (bfd *abfd ATTRIBUTE_UNUSED,
868 sec_ptr section ATTRIBUTE_UNUSED,
869 bfd_window *w ATTRIBUTE_UNUSED,
870 file_ptr offset ATTRIBUTE_UNUSED,
871 bfd_size_type count ATTRIBUTE_UNUSED)
873 #ifdef USE_MMAP
874 bfd_size_type sz;
876 if (count == 0)
877 return TRUE;
878 if (abfd->xvec->_bfd_get_section_contents
879 != _bfd_generic_get_section_contents)
881 /* We don't know what changes the bfd's get_section_contents
882 method may have to make. So punt trying to map the file
883 window, and let get_section_contents do its thing. */
884 /* @@ FIXME : If the internal window has a refcount of 1 and was
885 allocated with malloc instead of mmap, just reuse it. */
886 bfd_free_window (w);
887 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
888 if (w->i == NULL)
889 return FALSE;
890 w->i->data = bfd_malloc (count);
891 if (w->i->data == NULL)
893 free (w->i);
894 w->i = NULL;
895 return FALSE;
897 w->i->mapped = 0;
898 w->i->refcount = 1;
899 w->size = w->i->size = count;
900 w->data = w->i->data;
901 return bfd_get_section_contents (abfd, section, w->data, offset, count);
903 sz = section->rawsize ? section->rawsize : section->size;
904 if (offset + count > sz
905 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
906 TRUE))
907 return FALSE;
908 return TRUE;
909 #else
910 abort ();
911 #endif
914 /* This generic function can only be used in implementations where creating
915 NEW sections is disallowed. It is useful in patching existing sections
916 in read-write files, though. See other set_section_contents functions
917 to see why it doesn't work for new sections. */
918 bfd_boolean
919 _bfd_generic_set_section_contents (bfd *abfd,
920 sec_ptr section,
921 const void *location,
922 file_ptr offset,
923 bfd_size_type count)
925 if (count == 0)
926 return TRUE;
928 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
929 || bfd_bwrite (location, count, abfd) != count)
930 return FALSE;
932 return TRUE;
936 INTERNAL_FUNCTION
937 bfd_log2
939 SYNOPSIS
940 unsigned int bfd_log2 (bfd_vma x);
942 DESCRIPTION
943 Return the log base 2 of the value supplied, rounded up. E.g., an
944 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
947 unsigned int
948 bfd_log2 (bfd_vma x)
950 unsigned int result = 0;
952 while ((x = (x >> 1)) != 0)
953 ++result;
954 return result;
957 bfd_boolean
958 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
960 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
962 return name[0] == locals_prefix;
965 /* Can be used from / for bfd_merge_private_bfd_data to check that
966 endianness matches between input and output file. Returns
967 TRUE for a match, otherwise returns FALSE and emits an error. */
968 bfd_boolean
969 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
971 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
972 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
973 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
975 const char *msg;
977 if (bfd_big_endian (ibfd))
978 msg = _("%B: compiled for a big endian system and target is little endian");
979 else
980 msg = _("%B: compiled for a little endian system and target is big endian");
982 (*_bfd_error_handler) (msg, ibfd);
984 bfd_set_error (bfd_error_wrong_format);
985 return FALSE;
988 return TRUE;
991 /* Give a warning at runtime if someone compiles code which calls
992 old routines. */
994 void
995 warn_deprecated (const char *what,
996 const char *file,
997 int line,
998 const char *func)
1000 /* Poor man's tracking of functions we've already warned about. */
1001 static size_t mask = 0;
1003 if (~(size_t) func & ~mask)
1005 /* Note: separate sentences in order to allow
1006 for translation into other languages. */
1007 if (func)
1008 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
1009 what, file, line, func);
1010 else
1011 fprintf (stderr, _("Deprecated %s called\n"), what);
1012 mask |= ~(size_t) func;
1016 /* Helper function for reading uleb128 encoded data. */
1018 bfd_vma
1019 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1020 bfd_byte *buf,
1021 unsigned int *bytes_read_ptr)
1023 bfd_vma result;
1024 unsigned int num_read;
1025 unsigned int shift;
1026 unsigned char byte;
1028 result = 0;
1029 shift = 0;
1030 num_read = 0;
1033 byte = bfd_get_8 (abfd, buf);
1034 buf++;
1035 num_read++;
1036 result |= (((bfd_vma) byte & 0x7f) << shift);
1037 shift += 7;
1039 while (byte & 0x80);
1040 *bytes_read_ptr = num_read;
1041 return result;
1044 /* Helper function for reading sleb128 encoded data. */
1046 bfd_signed_vma
1047 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1048 bfd_byte *buf,
1049 unsigned int *bytes_read_ptr)
1051 bfd_vma result;
1052 unsigned int shift;
1053 unsigned int num_read;
1054 unsigned char byte;
1056 result = 0;
1057 shift = 0;
1058 num_read = 0;
1061 byte = bfd_get_8 (abfd, buf);
1062 buf ++;
1063 num_read ++;
1064 result |= (((bfd_vma) byte & 0x7f) << shift);
1065 shift += 7;
1067 while (byte & 0x80);
1068 if (shift < 8 * sizeof (result) && (byte & 0x40))
1069 result |= (((bfd_vma) -1) << shift);
1070 *bytes_read_ptr = num_read;
1071 return result;
1074 bfd_boolean
1075 _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
1076 asymbol **symbols ATTRIBUTE_UNUSED,
1077 asymbol *symbol ATTRIBUTE_UNUSED,
1078 const char **filename_ptr ATTRIBUTE_UNUSED,
1079 unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
1081 return FALSE;
1084 bfd_boolean
1085 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1086 asection *isec ATTRIBUTE_UNUSED,
1087 bfd *obfd ATTRIBUTE_UNUSED,
1088 asection *osec ATTRIBUTE_UNUSED,
1089 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1091 return TRUE;