* libbfd-in.h (_bfd_norelocs_get_reloc_upper_bound): Don't define,
[binutils.git] / bfd / libbfd.c
blob2a36d947e3e89630ef6f7612a8dd1166f20c22ed
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
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 2 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, MA 02110-1301, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
27 #ifndef HAVE_GETPAGESIZE
28 #define getpagesize() 2048
29 #endif
32 SECTION
33 Implementation details
35 SUBSECTION
36 Internal functions
38 DESCRIPTION
39 These routines are used within BFD.
40 They are not intended for export, but are documented here for
41 completeness.
44 /* A routine which is used in target vectors for unsupported
45 operations. */
47 bfd_boolean
48 bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
50 bfd_set_error (bfd_error_invalid_operation);
51 return FALSE;
54 /* A routine which is used in target vectors for supported operations
55 which do not actually do anything. */
57 bfd_boolean
58 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
60 return TRUE;
63 /* A routine which is used in target vectors for unsupported
64 operations which return a pointer value. */
66 void *
67 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
69 bfd_set_error (bfd_error_invalid_operation);
70 return NULL;
73 int
74 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
76 return 0;
79 unsigned int
80 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
82 return 0;
85 long
86 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
88 return 0;
91 /* A routine which is used in target vectors for unsupported
92 operations which return -1 on error. */
94 long
95 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
97 bfd_set_error (bfd_error_invalid_operation);
98 return -1;
101 void
102 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
106 long
107 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
108 asection *sec ATTRIBUTE_UNUSED)
110 return sizeof (arelent *);
113 long
114 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
115 asection *sec ATTRIBUTE_UNUSED,
116 arelent **relptr,
117 asymbol **symbols ATTRIBUTE_UNUSED)
119 *relptr = NULL;
120 return 0;
123 bfd_boolean
124 _bfd_nocore_core_file_matches_executable_p
125 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
126 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
128 bfd_set_error (bfd_error_invalid_operation);
129 return FALSE;
132 /* Routine to handle core_file_failing_command entry point for targets
133 without core file support. */
135 char *
136 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
138 bfd_set_error (bfd_error_invalid_operation);
139 return NULL;
142 /* Routine to handle core_file_failing_signal entry point for targets
143 without core file support. */
146 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
148 bfd_set_error (bfd_error_invalid_operation);
149 return 0;
152 const bfd_target *
153 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
155 bfd_set_error (bfd_error_wrong_format);
156 return 0;
159 /* Allocate memory using malloc. */
161 void *
162 bfd_malloc (bfd_size_type size)
164 void *ptr;
166 if (size != (size_t) size)
168 bfd_set_error (bfd_error_no_memory);
169 return NULL;
172 ptr = malloc ((size_t) size);
173 if (ptr == NULL && (size_t) size != 0)
174 bfd_set_error (bfd_error_no_memory);
176 return ptr;
179 /* Allocate memory using malloc, nmemb * size with overflow checking. */
181 void *
182 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
184 void *ptr;
186 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
187 && size != 0
188 && nmemb > ~(bfd_size_type) 0 / size)
190 bfd_set_error (bfd_error_no_memory);
191 return NULL;
194 size *= nmemb;
196 if (size != (size_t) size)
198 bfd_set_error (bfd_error_no_memory);
199 return NULL;
202 ptr = malloc ((size_t) size);
203 if (ptr == NULL && (size_t) size != 0)
204 bfd_set_error (bfd_error_no_memory);
206 return ptr;
209 /* Reallocate memory using realloc. */
211 void *
212 bfd_realloc (void *ptr, bfd_size_type size)
214 void *ret;
216 if (size != (size_t) size)
218 bfd_set_error (bfd_error_no_memory);
219 return NULL;
222 if (ptr == NULL)
223 ret = malloc ((size_t) size);
224 else
225 ret = realloc (ptr, (size_t) size);
227 if (ret == NULL && (size_t) size != 0)
228 bfd_set_error (bfd_error_no_memory);
230 return ret;
233 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
235 void *
236 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
238 void *ret;
240 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
241 && size != 0
242 && nmemb > ~(bfd_size_type) 0 / size)
244 bfd_set_error (bfd_error_no_memory);
245 return NULL;
248 size *= nmemb;
250 if (size != (size_t) size)
252 bfd_set_error (bfd_error_no_memory);
253 return NULL;
256 if (ptr == NULL)
257 ret = malloc ((size_t) size);
258 else
259 ret = realloc (ptr, (size_t) size);
261 if (ret == NULL && (size_t) size != 0)
262 bfd_set_error (bfd_error_no_memory);
264 return ret;
267 /* Allocate memory using malloc and clear it. */
269 void *
270 bfd_zmalloc (bfd_size_type size)
272 void *ptr;
274 if (size != (size_t) size)
276 bfd_set_error (bfd_error_no_memory);
277 return NULL;
280 ptr = malloc ((size_t) size);
282 if ((size_t) size != 0)
284 if (ptr == NULL)
285 bfd_set_error (bfd_error_no_memory);
286 else
287 memset (ptr, 0, (size_t) size);
290 return ptr;
293 /* Allocate memory using malloc (nmemb * size) with overflow checking
294 and clear it. */
296 void *
297 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
299 void *ptr;
301 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
302 && size != 0
303 && nmemb > ~(bfd_size_type) 0 / size)
305 bfd_set_error (bfd_error_no_memory);
306 return NULL;
309 size *= nmemb;
311 if (size != (size_t) size)
313 bfd_set_error (bfd_error_no_memory);
314 return NULL;
317 ptr = malloc ((size_t) size);
319 if ((size_t) size != 0)
321 if (ptr == NULL)
322 bfd_set_error (bfd_error_no_memory);
323 else
324 memset (ptr, 0, (size_t) size);
327 return ptr;
331 INTERNAL_FUNCTION
332 bfd_write_bigendian_4byte_int
334 SYNOPSIS
335 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
337 DESCRIPTION
338 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
339 endian order regardless of what else is going on. This is useful in
340 archives.
343 bfd_boolean
344 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
346 bfd_byte buffer[4];
347 bfd_putb32 ((bfd_vma) i, buffer);
348 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
352 /** The do-it-yourself (byte) sex-change kit */
354 /* The middle letter e.g. get<b>short indicates Big or Little endian
355 target machine. It doesn't matter what the byte order of the host
356 machine is; these routines work for either. */
358 /* FIXME: Should these take a count argument?
359 Answer (gnu@cygnus.com): No, but perhaps they should be inline
360 functions in swap.h #ifdef __GNUC__.
361 Gprof them later and find out. */
364 FUNCTION
365 bfd_put_size
366 FUNCTION
367 bfd_get_size
369 DESCRIPTION
370 These macros as used for reading and writing raw data in
371 sections; each access (except for bytes) is vectored through
372 the target format of the BFD and mangled accordingly. The
373 mangling performs any necessary endian translations and
374 removes alignment restrictions. Note that types accepted and
375 returned by these macros are identical so they can be swapped
376 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
377 to either <<bfd_get_32>> or <<bfd_get_64>>.
379 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
380 system without prototypes, the caller is responsible for making
381 sure that is true, with a cast if necessary. We don't cast
382 them in the macro definitions because that would prevent <<lint>>
383 or <<gcc -Wall>> from detecting sins such as passing a pointer.
384 To detect calling these with less than a <<bfd_vma>>, use
385 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
388 .{* Byte swapping macros for user section data. *}
390 .#define bfd_put_8(abfd, val, ptr) \
391 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
392 .#define bfd_put_signed_8 \
393 . bfd_put_8
394 .#define bfd_get_8(abfd, ptr) \
395 . (*(unsigned char *) (ptr) & 0xff)
396 .#define bfd_get_signed_8(abfd, ptr) \
397 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
399 .#define bfd_put_16(abfd, val, ptr) \
400 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
401 .#define bfd_put_signed_16 \
402 . bfd_put_16
403 .#define bfd_get_16(abfd, ptr) \
404 . BFD_SEND (abfd, bfd_getx16, (ptr))
405 .#define bfd_get_signed_16(abfd, ptr) \
406 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
408 .#define bfd_put_32(abfd, val, ptr) \
409 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
410 .#define bfd_put_signed_32 \
411 . bfd_put_32
412 .#define bfd_get_32(abfd, ptr) \
413 . BFD_SEND (abfd, bfd_getx32, (ptr))
414 .#define bfd_get_signed_32(abfd, ptr) \
415 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
417 .#define bfd_put_64(abfd, val, ptr) \
418 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
419 .#define bfd_put_signed_64 \
420 . bfd_put_64
421 .#define bfd_get_64(abfd, ptr) \
422 . BFD_SEND (abfd, bfd_getx64, (ptr))
423 .#define bfd_get_signed_64(abfd, ptr) \
424 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
426 .#define bfd_get(bits, abfd, ptr) \
427 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
428 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
429 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
430 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
431 . : (abort (), (bfd_vma) - 1))
433 .#define bfd_put(bits, abfd, val, ptr) \
434 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
435 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
436 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
437 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
438 . : (abort (), (void) 0))
443 FUNCTION
444 bfd_h_put_size
445 bfd_h_get_size
447 DESCRIPTION
448 These macros have the same function as their <<bfd_get_x>>
449 brethren, except that they are used for removing information
450 for the header records of object files. Believe it or not,
451 some object files keep their header records in big endian
452 order and their data in little endian order.
454 .{* Byte swapping macros for file header data. *}
456 .#define bfd_h_put_8(abfd, val, ptr) \
457 . bfd_put_8 (abfd, val, ptr)
458 .#define bfd_h_put_signed_8(abfd, val, ptr) \
459 . bfd_put_8 (abfd, val, ptr)
460 .#define bfd_h_get_8(abfd, ptr) \
461 . bfd_get_8 (abfd, ptr)
462 .#define bfd_h_get_signed_8(abfd, ptr) \
463 . bfd_get_signed_8 (abfd, ptr)
465 .#define bfd_h_put_16(abfd, val, ptr) \
466 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
467 .#define bfd_h_put_signed_16 \
468 . bfd_h_put_16
469 .#define bfd_h_get_16(abfd, ptr) \
470 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
471 .#define bfd_h_get_signed_16(abfd, ptr) \
472 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
474 .#define bfd_h_put_32(abfd, val, ptr) \
475 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
476 .#define bfd_h_put_signed_32 \
477 . bfd_h_put_32
478 .#define bfd_h_get_32(abfd, ptr) \
479 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
480 .#define bfd_h_get_signed_32(abfd, ptr) \
481 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
483 .#define bfd_h_put_64(abfd, val, ptr) \
484 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
485 .#define bfd_h_put_signed_64 \
486 . bfd_h_put_64
487 .#define bfd_h_get_64(abfd, ptr) \
488 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
489 .#define bfd_h_get_signed_64(abfd, ptr) \
490 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
492 .{* Aliases for the above, which should eventually go away. *}
494 .#define H_PUT_64 bfd_h_put_64
495 .#define H_PUT_32 bfd_h_put_32
496 .#define H_PUT_16 bfd_h_put_16
497 .#define H_PUT_8 bfd_h_put_8
498 .#define H_PUT_S64 bfd_h_put_signed_64
499 .#define H_PUT_S32 bfd_h_put_signed_32
500 .#define H_PUT_S16 bfd_h_put_signed_16
501 .#define H_PUT_S8 bfd_h_put_signed_8
502 .#define H_GET_64 bfd_h_get_64
503 .#define H_GET_32 bfd_h_get_32
504 .#define H_GET_16 bfd_h_get_16
505 .#define H_GET_8 bfd_h_get_8
506 .#define H_GET_S64 bfd_h_get_signed_64
507 .#define H_GET_S32 bfd_h_get_signed_32
508 .#define H_GET_S16 bfd_h_get_signed_16
509 .#define H_GET_S8 bfd_h_get_signed_8
513 /* Sign extension to bfd_signed_vma. */
514 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
515 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
516 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
517 #define COERCE64(x) \
518 (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
520 bfd_vma
521 bfd_getb16 (const void *p)
523 const bfd_byte *addr = p;
524 return (addr[0] << 8) | addr[1];
527 bfd_vma
528 bfd_getl16 (const void *p)
530 const bfd_byte *addr = p;
531 return (addr[1] << 8) | addr[0];
534 bfd_signed_vma
535 bfd_getb_signed_16 (const void *p)
537 const bfd_byte *addr = p;
538 return COERCE16 ((addr[0] << 8) | addr[1]);
541 bfd_signed_vma
542 bfd_getl_signed_16 (const void *p)
544 const bfd_byte *addr = p;
545 return COERCE16 ((addr[1] << 8) | addr[0]);
548 void
549 bfd_putb16 (bfd_vma data, void *p)
551 bfd_byte *addr = p;
552 addr[0] = (data >> 8) & 0xff;
553 addr[1] = data & 0xff;
556 void
557 bfd_putl16 (bfd_vma data, void *p)
559 bfd_byte *addr = p;
560 addr[0] = data & 0xff;
561 addr[1] = (data >> 8) & 0xff;
564 bfd_vma
565 bfd_getb32 (const void *p)
567 const bfd_byte *addr = p;
568 unsigned long v;
570 v = (unsigned long) addr[0] << 24;
571 v |= (unsigned long) addr[1] << 16;
572 v |= (unsigned long) addr[2] << 8;
573 v |= (unsigned long) addr[3];
574 return v;
577 bfd_vma
578 bfd_getl32 (const void *p)
580 const bfd_byte *addr = p;
581 unsigned long v;
583 v = (unsigned long) addr[0];
584 v |= (unsigned long) addr[1] << 8;
585 v |= (unsigned long) addr[2] << 16;
586 v |= (unsigned long) addr[3] << 24;
587 return v;
590 bfd_signed_vma
591 bfd_getb_signed_32 (const void *p)
593 const bfd_byte *addr = p;
594 unsigned long v;
596 v = (unsigned long) addr[0] << 24;
597 v |= (unsigned long) addr[1] << 16;
598 v |= (unsigned long) addr[2] << 8;
599 v |= (unsigned long) addr[3];
600 return COERCE32 (v);
603 bfd_signed_vma
604 bfd_getl_signed_32 (const void *p)
606 const bfd_byte *addr = p;
607 unsigned long v;
609 v = (unsigned long) addr[0];
610 v |= (unsigned long) addr[1] << 8;
611 v |= (unsigned long) addr[2] << 16;
612 v |= (unsigned long) addr[3] << 24;
613 return COERCE32 (v);
616 bfd_uint64_t
617 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
619 #ifdef BFD_HOST_64_BIT
620 const bfd_byte *addr = p;
621 bfd_uint64_t v;
623 v = addr[0]; v <<= 8;
624 v |= addr[1]; v <<= 8;
625 v |= addr[2]; v <<= 8;
626 v |= addr[3]; v <<= 8;
627 v |= addr[4]; v <<= 8;
628 v |= addr[5]; v <<= 8;
629 v |= addr[6]; v <<= 8;
630 v |= addr[7];
632 return v;
633 #else
634 BFD_FAIL();
635 return 0;
636 #endif
639 bfd_uint64_t
640 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
642 #ifdef BFD_HOST_64_BIT
643 const bfd_byte *addr = p;
644 bfd_uint64_t v;
646 v = addr[7]; v <<= 8;
647 v |= addr[6]; v <<= 8;
648 v |= addr[5]; v <<= 8;
649 v |= addr[4]; v <<= 8;
650 v |= addr[3]; v <<= 8;
651 v |= addr[2]; v <<= 8;
652 v |= addr[1]; v <<= 8;
653 v |= addr[0];
655 return v;
656 #else
657 BFD_FAIL();
658 return 0;
659 #endif
663 bfd_int64_t
664 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
666 #ifdef BFD_HOST_64_BIT
667 const bfd_byte *addr = p;
668 bfd_uint64_t v;
670 v = addr[0]; v <<= 8;
671 v |= addr[1]; v <<= 8;
672 v |= addr[2]; v <<= 8;
673 v |= addr[3]; v <<= 8;
674 v |= addr[4]; v <<= 8;
675 v |= addr[5]; v <<= 8;
676 v |= addr[6]; v <<= 8;
677 v |= addr[7];
679 return COERCE64 (v);
680 #else
681 BFD_FAIL();
682 return 0;
683 #endif
686 bfd_int64_t
687 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
689 #ifdef BFD_HOST_64_BIT
690 const bfd_byte *addr = p;
691 bfd_uint64_t v;
693 v = addr[7]; v <<= 8;
694 v |= addr[6]; v <<= 8;
695 v |= addr[5]; v <<= 8;
696 v |= addr[4]; v <<= 8;
697 v |= addr[3]; v <<= 8;
698 v |= addr[2]; v <<= 8;
699 v |= addr[1]; v <<= 8;
700 v |= addr[0];
702 return COERCE64 (v);
703 #else
704 BFD_FAIL();
705 return 0;
706 #endif
709 void
710 bfd_putb32 (bfd_vma data, void *p)
712 bfd_byte *addr = p;
713 addr[0] = (data >> 24) & 0xff;
714 addr[1] = (data >> 16) & 0xff;
715 addr[2] = (data >> 8) & 0xff;
716 addr[3] = data & 0xff;
719 void
720 bfd_putl32 (bfd_vma data, void *p)
722 bfd_byte *addr = p;
723 addr[0] = data & 0xff;
724 addr[1] = (data >> 8) & 0xff;
725 addr[2] = (data >> 16) & 0xff;
726 addr[3] = (data >> 24) & 0xff;
729 void
730 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
732 #ifdef BFD_HOST_64_BIT
733 bfd_byte *addr = p;
734 addr[0] = (data >> (7*8)) & 0xff;
735 addr[1] = (data >> (6*8)) & 0xff;
736 addr[2] = (data >> (5*8)) & 0xff;
737 addr[3] = (data >> (4*8)) & 0xff;
738 addr[4] = (data >> (3*8)) & 0xff;
739 addr[5] = (data >> (2*8)) & 0xff;
740 addr[6] = (data >> (1*8)) & 0xff;
741 addr[7] = (data >> (0*8)) & 0xff;
742 #else
743 BFD_FAIL();
744 #endif
747 void
748 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
750 #ifdef BFD_HOST_64_BIT
751 bfd_byte *addr = p;
752 addr[7] = (data >> (7*8)) & 0xff;
753 addr[6] = (data >> (6*8)) & 0xff;
754 addr[5] = (data >> (5*8)) & 0xff;
755 addr[4] = (data >> (4*8)) & 0xff;
756 addr[3] = (data >> (3*8)) & 0xff;
757 addr[2] = (data >> (2*8)) & 0xff;
758 addr[1] = (data >> (1*8)) & 0xff;
759 addr[0] = (data >> (0*8)) & 0xff;
760 #else
761 BFD_FAIL();
762 #endif
765 void
766 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
768 bfd_byte *addr = p;
769 int i;
770 int bytes;
772 if (bits % 8 != 0)
773 abort ();
775 bytes = bits / 8;
776 for (i = 0; i < bytes; i++)
778 int index = big_p ? bytes - i - 1 : i;
780 addr[index] = data & 0xff;
781 data >>= 8;
785 bfd_uint64_t
786 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
788 const bfd_byte *addr = p;
789 bfd_uint64_t data;
790 int i;
791 int bytes;
793 if (bits % 8 != 0)
794 abort ();
796 data = 0;
797 bytes = bits / 8;
798 for (i = 0; i < bytes; i++)
800 int index = big_p ? i : bytes - i - 1;
802 data = (data << 8) | addr[index];
805 return data;
808 /* Default implementation */
810 bfd_boolean
811 _bfd_generic_get_section_contents (bfd *abfd,
812 sec_ptr section,
813 void *location,
814 file_ptr offset,
815 bfd_size_type count)
817 bfd_size_type sz;
818 if (count == 0)
819 return TRUE;
821 sz = section->rawsize ? section->rawsize : section->size;
822 if (offset + count > sz)
824 bfd_set_error (bfd_error_invalid_operation);
825 return FALSE;
828 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
829 || bfd_bread (location, count, abfd) != count)
830 return FALSE;
832 return TRUE;
835 bfd_boolean
836 _bfd_generic_get_section_contents_in_window
837 (bfd *abfd ATTRIBUTE_UNUSED,
838 sec_ptr section ATTRIBUTE_UNUSED,
839 bfd_window *w ATTRIBUTE_UNUSED,
840 file_ptr offset ATTRIBUTE_UNUSED,
841 bfd_size_type count ATTRIBUTE_UNUSED)
843 #ifdef USE_MMAP
844 bfd_size_type sz;
846 if (count == 0)
847 return TRUE;
848 if (abfd->xvec->_bfd_get_section_contents
849 != _bfd_generic_get_section_contents)
851 /* We don't know what changes the bfd's get_section_contents
852 method may have to make. So punt trying to map the file
853 window, and let get_section_contents do its thing. */
854 /* @@ FIXME : If the internal window has a refcount of 1 and was
855 allocated with malloc instead of mmap, just reuse it. */
856 bfd_free_window (w);
857 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
858 if (w->i == NULL)
859 return FALSE;
860 w->i->data = bfd_malloc (count);
861 if (w->i->data == NULL)
863 free (w->i);
864 w->i = NULL;
865 return FALSE;
867 w->i->mapped = 0;
868 w->i->refcount = 1;
869 w->size = w->i->size = count;
870 w->data = w->i->data;
871 return bfd_get_section_contents (abfd, section, w->data, offset, count);
873 sz = section->rawsize ? section->rawsize : section->size;
874 if (offset + count > sz
875 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
876 TRUE))
877 return FALSE;
878 return TRUE;
879 #else
880 abort ();
881 #endif
884 /* This generic function can only be used in implementations where creating
885 NEW sections is disallowed. It is useful in patching existing sections
886 in read-write files, though. See other set_section_contents functions
887 to see why it doesn't work for new sections. */
888 bfd_boolean
889 _bfd_generic_set_section_contents (bfd *abfd,
890 sec_ptr section,
891 const void *location,
892 file_ptr offset,
893 bfd_size_type count)
895 if (count == 0)
896 return TRUE;
898 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
899 || bfd_bwrite (location, count, abfd) != count)
900 return FALSE;
902 return TRUE;
906 INTERNAL_FUNCTION
907 bfd_log2
909 SYNOPSIS
910 unsigned int bfd_log2 (bfd_vma x);
912 DESCRIPTION
913 Return the log base 2 of the value supplied, rounded up. E.g., an
914 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
917 unsigned int
918 bfd_log2 (bfd_vma x)
920 unsigned int result = 0;
922 while ((x = (x >> 1)) != 0)
923 ++result;
924 return result;
927 bfd_boolean
928 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
930 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
932 return name[0] == locals_prefix;
935 /* Can be used from / for bfd_merge_private_bfd_data to check that
936 endianness matches between input and output file. Returns
937 TRUE for a match, otherwise returns FALSE and emits an error. */
938 bfd_boolean
939 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
941 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
942 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
943 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
945 const char *msg;
947 if (bfd_big_endian (ibfd))
948 msg = _("%B: compiled for a big endian system and target is little endian");
949 else
950 msg = _("%B: compiled for a little endian system and target is big endian");
952 (*_bfd_error_handler) (msg, ibfd);
954 bfd_set_error (bfd_error_wrong_format);
955 return FALSE;
958 return TRUE;
961 /* Give a warning at runtime if someone compiles code which calls
962 old routines. */
964 void
965 warn_deprecated (const char *what,
966 const char *file,
967 int line,
968 const char *func)
970 /* Poor man's tracking of functions we've already warned about. */
971 static size_t mask = 0;
973 if (~(size_t) func & ~mask)
975 /* Note: separate sentences in order to allow
976 for translation into other languages. */
977 if (func)
978 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
979 what, file, line, func);
980 else
981 fprintf (stderr, _("Deprecated %s called\n"), what);
982 mask |= ~(size_t) func;
986 /* Helper function for reading uleb128 encoded data. */
988 bfd_vma
989 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
990 bfd_byte *buf,
991 unsigned int *bytes_read_ptr)
993 bfd_vma result;
994 unsigned int num_read;
995 unsigned int shift;
996 unsigned char byte;
998 result = 0;
999 shift = 0;
1000 num_read = 0;
1003 byte = bfd_get_8 (abfd, buf);
1004 buf++;
1005 num_read++;
1006 result |= (((bfd_vma) byte & 0x7f) << shift);
1007 shift += 7;
1009 while (byte & 0x80);
1010 *bytes_read_ptr = num_read;
1011 return result;
1014 /* Helper function for reading sleb128 encoded data. */
1016 bfd_signed_vma
1017 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1018 bfd_byte *buf,
1019 unsigned int *bytes_read_ptr)
1021 bfd_vma result;
1022 unsigned int shift;
1023 unsigned int num_read;
1024 unsigned char byte;
1026 result = 0;
1027 shift = 0;
1028 num_read = 0;
1031 byte = bfd_get_8 (abfd, buf);
1032 buf ++;
1033 num_read ++;
1034 result |= (((bfd_vma) byte & 0x7f) << shift);
1035 shift += 7;
1037 while (byte & 0x80);
1038 if (shift < 8 * sizeof (result) && (byte & 0x40))
1039 result |= (((bfd_vma) -1) << shift);
1040 *bytes_read_ptr = num_read;
1041 return result;
1044 bfd_boolean
1045 _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
1046 asymbol **symbols ATTRIBUTE_UNUSED,
1047 asymbol *symbol ATTRIBUTE_UNUSED,
1048 const char **filename_ptr ATTRIBUTE_UNUSED,
1049 unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
1051 return FALSE;
1054 bfd_boolean
1055 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1056 asection *isec ATTRIBUTE_UNUSED,
1057 bfd *obfd ATTRIBUTE_UNUSED,
1058 asection *osec ATTRIBUTE_UNUSED,
1059 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1061 return TRUE;