* configure.tgt (i[3-7]86-*-solaris2*, i[3-7]86-*-solaris*): Correct
[binutils.git] / bfd / libbfd.c
blob57cfabcd460e478ba524ffc5f964b056e34ea702
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
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 bfd_boolean
107 _bfd_nocore_core_file_matches_executable_p
108 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
109 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
111 bfd_set_error (bfd_error_invalid_operation);
112 return FALSE;
115 /* Routine to handle core_file_failing_command entry point for targets
116 without core file support. */
118 char *
119 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
121 bfd_set_error (bfd_error_invalid_operation);
122 return NULL;
125 /* Routine to handle core_file_failing_signal entry point for targets
126 without core file support. */
129 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
131 bfd_set_error (bfd_error_invalid_operation);
132 return 0;
135 const bfd_target *
136 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
138 bfd_set_error (bfd_error_wrong_format);
139 return 0;
142 /* Allocate memory using malloc. */
144 void *
145 bfd_malloc (bfd_size_type size)
147 void *ptr;
149 if (size != (size_t) size)
151 bfd_set_error (bfd_error_no_memory);
152 return NULL;
155 ptr = malloc ((size_t) size);
156 if (ptr == NULL && (size_t) size != 0)
157 bfd_set_error (bfd_error_no_memory);
159 return ptr;
162 /* Allocate memory using malloc, nmemb * size with overflow checking. */
164 void *
165 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
167 void *ptr;
169 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
170 && size != 0
171 && nmemb > ~(bfd_size_type) 0 / size)
173 bfd_set_error (bfd_error_no_memory);
174 return NULL;
177 size *= nmemb;
179 if (size != (size_t) size)
181 bfd_set_error (bfd_error_no_memory);
182 return NULL;
185 ptr = malloc ((size_t) size);
186 if (ptr == NULL && (size_t) size != 0)
187 bfd_set_error (bfd_error_no_memory);
189 return ptr;
192 /* Reallocate memory using realloc. */
194 void *
195 bfd_realloc (void *ptr, bfd_size_type size)
197 void *ret;
199 if (size != (size_t) size)
201 bfd_set_error (bfd_error_no_memory);
202 return NULL;
205 if (ptr == NULL)
206 ret = malloc ((size_t) size);
207 else
208 ret = realloc (ptr, (size_t) size);
210 if (ret == NULL && (size_t) size != 0)
211 bfd_set_error (bfd_error_no_memory);
213 return ret;
216 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
218 void *
219 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
221 void *ret;
223 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
224 && size != 0
225 && nmemb > ~(bfd_size_type) 0 / size)
227 bfd_set_error (bfd_error_no_memory);
228 return NULL;
231 size *= nmemb;
233 if (size != (size_t) size)
235 bfd_set_error (bfd_error_no_memory);
236 return NULL;
239 if (ptr == NULL)
240 ret = malloc ((size_t) size);
241 else
242 ret = realloc (ptr, (size_t) size);
244 if (ret == NULL && (size_t) size != 0)
245 bfd_set_error (bfd_error_no_memory);
247 return ret;
250 /* Allocate memory using malloc and clear it. */
252 void *
253 bfd_zmalloc (bfd_size_type size)
255 void *ptr;
257 if (size != (size_t) size)
259 bfd_set_error (bfd_error_no_memory);
260 return NULL;
263 ptr = malloc ((size_t) size);
265 if ((size_t) size != 0)
267 if (ptr == NULL)
268 bfd_set_error (bfd_error_no_memory);
269 else
270 memset (ptr, 0, (size_t) size);
273 return ptr;
276 /* Allocate memory using malloc (nmemb * size) with overflow checking
277 and clear it. */
279 void *
280 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
282 void *ptr;
284 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
285 && size != 0
286 && nmemb > ~(bfd_size_type) 0 / size)
288 bfd_set_error (bfd_error_no_memory);
289 return NULL;
292 size *= nmemb;
294 if (size != (size_t) size)
296 bfd_set_error (bfd_error_no_memory);
297 return NULL;
300 ptr = malloc ((size_t) size);
302 if ((size_t) size != 0)
304 if (ptr == NULL)
305 bfd_set_error (bfd_error_no_memory);
306 else
307 memset (ptr, 0, (size_t) size);
310 return ptr;
314 INTERNAL_FUNCTION
315 bfd_write_bigendian_4byte_int
317 SYNOPSIS
318 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
320 DESCRIPTION
321 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
322 endian order regardless of what else is going on. This is useful in
323 archives.
326 bfd_boolean
327 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
329 bfd_byte buffer[4];
330 bfd_putb32 ((bfd_vma) i, buffer);
331 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
335 /** The do-it-yourself (byte) sex-change kit */
337 /* The middle letter e.g. get<b>short indicates Big or Little endian
338 target machine. It doesn't matter what the byte order of the host
339 machine is; these routines work for either. */
341 /* FIXME: Should these take a count argument?
342 Answer (gnu@cygnus.com): No, but perhaps they should be inline
343 functions in swap.h #ifdef __GNUC__.
344 Gprof them later and find out. */
347 FUNCTION
348 bfd_put_size
349 FUNCTION
350 bfd_get_size
352 DESCRIPTION
353 These macros as used for reading and writing raw data in
354 sections; each access (except for bytes) is vectored through
355 the target format of the BFD and mangled accordingly. The
356 mangling performs any necessary endian translations and
357 removes alignment restrictions. Note that types accepted and
358 returned by these macros are identical so they can be swapped
359 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
360 to either <<bfd_get_32>> or <<bfd_get_64>>.
362 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
363 system without prototypes, the caller is responsible for making
364 sure that is true, with a cast if necessary. We don't cast
365 them in the macro definitions because that would prevent <<lint>>
366 or <<gcc -Wall>> from detecting sins such as passing a pointer.
367 To detect calling these with less than a <<bfd_vma>>, use
368 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
371 .{* Byte swapping macros for user section data. *}
373 .#define bfd_put_8(abfd, val, ptr) \
374 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
375 .#define bfd_put_signed_8 \
376 . bfd_put_8
377 .#define bfd_get_8(abfd, ptr) \
378 . (*(unsigned char *) (ptr) & 0xff)
379 .#define bfd_get_signed_8(abfd, ptr) \
380 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
382 .#define bfd_put_16(abfd, val, ptr) \
383 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
384 .#define bfd_put_signed_16 \
385 . bfd_put_16
386 .#define bfd_get_16(abfd, ptr) \
387 . BFD_SEND (abfd, bfd_getx16, (ptr))
388 .#define bfd_get_signed_16(abfd, ptr) \
389 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
391 .#define bfd_put_32(abfd, val, ptr) \
392 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
393 .#define bfd_put_signed_32 \
394 . bfd_put_32
395 .#define bfd_get_32(abfd, ptr) \
396 . BFD_SEND (abfd, bfd_getx32, (ptr))
397 .#define bfd_get_signed_32(abfd, ptr) \
398 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
400 .#define bfd_put_64(abfd, val, ptr) \
401 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
402 .#define bfd_put_signed_64 \
403 . bfd_put_64
404 .#define bfd_get_64(abfd, ptr) \
405 . BFD_SEND (abfd, bfd_getx64, (ptr))
406 .#define bfd_get_signed_64(abfd, ptr) \
407 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
409 .#define bfd_get(bits, abfd, ptr) \
410 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
411 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
412 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
413 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
414 . : (abort (), (bfd_vma) - 1))
416 .#define bfd_put(bits, abfd, val, ptr) \
417 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
418 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
419 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
420 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
421 . : (abort (), (void) 0))
426 FUNCTION
427 bfd_h_put_size
428 bfd_h_get_size
430 DESCRIPTION
431 These macros have the same function as their <<bfd_get_x>>
432 brethren, except that they are used for removing information
433 for the header records of object files. Believe it or not,
434 some object files keep their header records in big endian
435 order and their data in little endian order.
437 .{* Byte swapping macros for file header data. *}
439 .#define bfd_h_put_8(abfd, val, ptr) \
440 . bfd_put_8 (abfd, val, ptr)
441 .#define bfd_h_put_signed_8(abfd, val, ptr) \
442 . bfd_put_8 (abfd, val, ptr)
443 .#define bfd_h_get_8(abfd, ptr) \
444 . bfd_get_8 (abfd, ptr)
445 .#define bfd_h_get_signed_8(abfd, ptr) \
446 . bfd_get_signed_8 (abfd, ptr)
448 .#define bfd_h_put_16(abfd, val, ptr) \
449 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
450 .#define bfd_h_put_signed_16 \
451 . bfd_h_put_16
452 .#define bfd_h_get_16(abfd, ptr) \
453 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
454 .#define bfd_h_get_signed_16(abfd, ptr) \
455 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
457 .#define bfd_h_put_32(abfd, val, ptr) \
458 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
459 .#define bfd_h_put_signed_32 \
460 . bfd_h_put_32
461 .#define bfd_h_get_32(abfd, ptr) \
462 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
463 .#define bfd_h_get_signed_32(abfd, ptr) \
464 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
466 .#define bfd_h_put_64(abfd, val, ptr) \
467 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
468 .#define bfd_h_put_signed_64 \
469 . bfd_h_put_64
470 .#define bfd_h_get_64(abfd, ptr) \
471 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
472 .#define bfd_h_get_signed_64(abfd, ptr) \
473 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
475 .{* Aliases for the above, which should eventually go away. *}
477 .#define H_PUT_64 bfd_h_put_64
478 .#define H_PUT_32 bfd_h_put_32
479 .#define H_PUT_16 bfd_h_put_16
480 .#define H_PUT_8 bfd_h_put_8
481 .#define H_PUT_S64 bfd_h_put_signed_64
482 .#define H_PUT_S32 bfd_h_put_signed_32
483 .#define H_PUT_S16 bfd_h_put_signed_16
484 .#define H_PUT_S8 bfd_h_put_signed_8
485 .#define H_GET_64 bfd_h_get_64
486 .#define H_GET_32 bfd_h_get_32
487 .#define H_GET_16 bfd_h_get_16
488 .#define H_GET_8 bfd_h_get_8
489 .#define H_GET_S64 bfd_h_get_signed_64
490 .#define H_GET_S32 bfd_h_get_signed_32
491 .#define H_GET_S16 bfd_h_get_signed_16
492 .#define H_GET_S8 bfd_h_get_signed_8
496 /* Sign extension to bfd_signed_vma. */
497 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
498 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
499 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
500 #define COERCE64(x) \
501 (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
503 bfd_vma
504 bfd_getb16 (const void *p)
506 const bfd_byte *addr = p;
507 return (addr[0] << 8) | addr[1];
510 bfd_vma
511 bfd_getl16 (const void *p)
513 const bfd_byte *addr = p;
514 return (addr[1] << 8) | addr[0];
517 bfd_signed_vma
518 bfd_getb_signed_16 (const void *p)
520 const bfd_byte *addr = p;
521 return COERCE16 ((addr[0] << 8) | addr[1]);
524 bfd_signed_vma
525 bfd_getl_signed_16 (const void *p)
527 const bfd_byte *addr = p;
528 return COERCE16 ((addr[1] << 8) | addr[0]);
531 void
532 bfd_putb16 (bfd_vma data, void *p)
534 bfd_byte *addr = p;
535 addr[0] = (data >> 8) & 0xff;
536 addr[1] = data & 0xff;
539 void
540 bfd_putl16 (bfd_vma data, void *p)
542 bfd_byte *addr = p;
543 addr[0] = data & 0xff;
544 addr[1] = (data >> 8) & 0xff;
547 bfd_vma
548 bfd_getb32 (const void *p)
550 const bfd_byte *addr = p;
551 unsigned long v;
553 v = (unsigned long) addr[0] << 24;
554 v |= (unsigned long) addr[1] << 16;
555 v |= (unsigned long) addr[2] << 8;
556 v |= (unsigned long) addr[3];
557 return v;
560 bfd_vma
561 bfd_getl32 (const void *p)
563 const bfd_byte *addr = p;
564 unsigned long v;
566 v = (unsigned long) addr[0];
567 v |= (unsigned long) addr[1] << 8;
568 v |= (unsigned long) addr[2] << 16;
569 v |= (unsigned long) addr[3] << 24;
570 return v;
573 bfd_signed_vma
574 bfd_getb_signed_32 (const void *p)
576 const bfd_byte *addr = p;
577 unsigned long v;
579 v = (unsigned long) addr[0] << 24;
580 v |= (unsigned long) addr[1] << 16;
581 v |= (unsigned long) addr[2] << 8;
582 v |= (unsigned long) addr[3];
583 return COERCE32 (v);
586 bfd_signed_vma
587 bfd_getl_signed_32 (const void *p)
589 const bfd_byte *addr = p;
590 unsigned long v;
592 v = (unsigned long) addr[0];
593 v |= (unsigned long) addr[1] << 8;
594 v |= (unsigned long) addr[2] << 16;
595 v |= (unsigned long) addr[3] << 24;
596 return COERCE32 (v);
599 bfd_uint64_t
600 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
602 #ifdef BFD_HOST_64_BIT
603 const bfd_byte *addr = p;
604 bfd_uint64_t v;
606 v = addr[0]; v <<= 8;
607 v |= addr[1]; v <<= 8;
608 v |= addr[2]; v <<= 8;
609 v |= addr[3]; v <<= 8;
610 v |= addr[4]; v <<= 8;
611 v |= addr[5]; v <<= 8;
612 v |= addr[6]; v <<= 8;
613 v |= addr[7];
615 return v;
616 #else
617 BFD_FAIL();
618 return 0;
619 #endif
622 bfd_uint64_t
623 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
625 #ifdef BFD_HOST_64_BIT
626 const bfd_byte *addr = p;
627 bfd_uint64_t v;
629 v = addr[7]; v <<= 8;
630 v |= addr[6]; v <<= 8;
631 v |= addr[5]; v <<= 8;
632 v |= addr[4]; v <<= 8;
633 v |= addr[3]; v <<= 8;
634 v |= addr[2]; v <<= 8;
635 v |= addr[1]; v <<= 8;
636 v |= addr[0];
638 return v;
639 #else
640 BFD_FAIL();
641 return 0;
642 #endif
646 bfd_int64_t
647 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
649 #ifdef BFD_HOST_64_BIT
650 const bfd_byte *addr = p;
651 bfd_uint64_t v;
653 v = addr[0]; v <<= 8;
654 v |= addr[1]; v <<= 8;
655 v |= addr[2]; v <<= 8;
656 v |= addr[3]; v <<= 8;
657 v |= addr[4]; v <<= 8;
658 v |= addr[5]; v <<= 8;
659 v |= addr[6]; v <<= 8;
660 v |= addr[7];
662 return COERCE64 (v);
663 #else
664 BFD_FAIL();
665 return 0;
666 #endif
669 bfd_int64_t
670 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
672 #ifdef BFD_HOST_64_BIT
673 const bfd_byte *addr = p;
674 bfd_uint64_t v;
676 v = addr[7]; v <<= 8;
677 v |= addr[6]; v <<= 8;
678 v |= addr[5]; v <<= 8;
679 v |= addr[4]; v <<= 8;
680 v |= addr[3]; v <<= 8;
681 v |= addr[2]; v <<= 8;
682 v |= addr[1]; v <<= 8;
683 v |= addr[0];
685 return COERCE64 (v);
686 #else
687 BFD_FAIL();
688 return 0;
689 #endif
692 void
693 bfd_putb32 (bfd_vma data, void *p)
695 bfd_byte *addr = p;
696 addr[0] = (data >> 24) & 0xff;
697 addr[1] = (data >> 16) & 0xff;
698 addr[2] = (data >> 8) & 0xff;
699 addr[3] = data & 0xff;
702 void
703 bfd_putl32 (bfd_vma data, void *p)
705 bfd_byte *addr = p;
706 addr[0] = data & 0xff;
707 addr[1] = (data >> 8) & 0xff;
708 addr[2] = (data >> 16) & 0xff;
709 addr[3] = (data >> 24) & 0xff;
712 void
713 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
715 #ifdef BFD_HOST_64_BIT
716 bfd_byte *addr = p;
717 addr[0] = (data >> (7*8)) & 0xff;
718 addr[1] = (data >> (6*8)) & 0xff;
719 addr[2] = (data >> (5*8)) & 0xff;
720 addr[3] = (data >> (4*8)) & 0xff;
721 addr[4] = (data >> (3*8)) & 0xff;
722 addr[5] = (data >> (2*8)) & 0xff;
723 addr[6] = (data >> (1*8)) & 0xff;
724 addr[7] = (data >> (0*8)) & 0xff;
725 #else
726 BFD_FAIL();
727 #endif
730 void
731 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
733 #ifdef BFD_HOST_64_BIT
734 bfd_byte *addr = p;
735 addr[7] = (data >> (7*8)) & 0xff;
736 addr[6] = (data >> (6*8)) & 0xff;
737 addr[5] = (data >> (5*8)) & 0xff;
738 addr[4] = (data >> (4*8)) & 0xff;
739 addr[3] = (data >> (3*8)) & 0xff;
740 addr[2] = (data >> (2*8)) & 0xff;
741 addr[1] = (data >> (1*8)) & 0xff;
742 addr[0] = (data >> (0*8)) & 0xff;
743 #else
744 BFD_FAIL();
745 #endif
748 void
749 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
751 bfd_byte *addr = p;
752 int i;
753 int bytes;
755 if (bits % 8 != 0)
756 abort ();
758 bytes = bits / 8;
759 for (i = 0; i < bytes; i++)
761 int index = big_p ? bytes - i - 1 : i;
763 addr[index] = data & 0xff;
764 data >>= 8;
768 bfd_uint64_t
769 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
771 const bfd_byte *addr = p;
772 bfd_uint64_t data;
773 int i;
774 int bytes;
776 if (bits % 8 != 0)
777 abort ();
779 data = 0;
780 bytes = bits / 8;
781 for (i = 0; i < bytes; i++)
783 int index = big_p ? i : bytes - i - 1;
785 data = (data << 8) | addr[index];
788 return data;
791 /* Default implementation */
793 bfd_boolean
794 _bfd_generic_get_section_contents (bfd *abfd,
795 sec_ptr section,
796 void *location,
797 file_ptr offset,
798 bfd_size_type count)
800 bfd_size_type sz;
801 if (count == 0)
802 return TRUE;
804 sz = section->rawsize ? section->rawsize : section->size;
805 if (offset + count > sz)
807 bfd_set_error (bfd_error_invalid_operation);
808 return FALSE;
811 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
812 || bfd_bread (location, count, abfd) != count)
813 return FALSE;
815 return TRUE;
818 bfd_boolean
819 _bfd_generic_get_section_contents_in_window
820 (bfd *abfd ATTRIBUTE_UNUSED,
821 sec_ptr section ATTRIBUTE_UNUSED,
822 bfd_window *w ATTRIBUTE_UNUSED,
823 file_ptr offset ATTRIBUTE_UNUSED,
824 bfd_size_type count ATTRIBUTE_UNUSED)
826 #ifdef USE_MMAP
827 bfd_size_type sz;
829 if (count == 0)
830 return TRUE;
831 if (abfd->xvec->_bfd_get_section_contents
832 != _bfd_generic_get_section_contents)
834 /* We don't know what changes the bfd's get_section_contents
835 method may have to make. So punt trying to map the file
836 window, and let get_section_contents do its thing. */
837 /* @@ FIXME : If the internal window has a refcount of 1 and was
838 allocated with malloc instead of mmap, just reuse it. */
839 bfd_free_window (w);
840 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
841 if (w->i == NULL)
842 return FALSE;
843 w->i->data = bfd_malloc (count);
844 if (w->i->data == NULL)
846 free (w->i);
847 w->i = NULL;
848 return FALSE;
850 w->i->mapped = 0;
851 w->i->refcount = 1;
852 w->size = w->i->size = count;
853 w->data = w->i->data;
854 return bfd_get_section_contents (abfd, section, w->data, offset, count);
856 sz = section->rawsize ? section->rawsize : section->size;
857 if (offset + count > sz
858 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
859 TRUE))
860 return FALSE;
861 return TRUE;
862 #else
863 abort ();
864 #endif
867 /* This generic function can only be used in implementations where creating
868 NEW sections is disallowed. It is useful in patching existing sections
869 in read-write files, though. See other set_section_contents functions
870 to see why it doesn't work for new sections. */
871 bfd_boolean
872 _bfd_generic_set_section_contents (bfd *abfd,
873 sec_ptr section,
874 const void *location,
875 file_ptr offset,
876 bfd_size_type count)
878 if (count == 0)
879 return TRUE;
881 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
882 || bfd_bwrite (location, count, abfd) != count)
883 return FALSE;
885 return TRUE;
889 INTERNAL_FUNCTION
890 bfd_log2
892 SYNOPSIS
893 unsigned int bfd_log2 (bfd_vma x);
895 DESCRIPTION
896 Return the log base 2 of the value supplied, rounded up. E.g., an
897 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
900 unsigned int
901 bfd_log2 (bfd_vma x)
903 unsigned int result = 0;
905 while ((x = (x >> 1)) != 0)
906 ++result;
907 return result;
910 bfd_boolean
911 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
913 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
915 return name[0] == locals_prefix;
918 /* Can be used from / for bfd_merge_private_bfd_data to check that
919 endianness matches between input and output file. Returns
920 TRUE for a match, otherwise returns FALSE and emits an error. */
921 bfd_boolean
922 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
924 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
925 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
926 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
928 const char *msg;
930 if (bfd_big_endian (ibfd))
931 msg = _("%B: compiled for a big endian system and target is little endian");
932 else
933 msg = _("%B: compiled for a little endian system and target is big endian");
935 (*_bfd_error_handler) (msg, ibfd);
937 bfd_set_error (bfd_error_wrong_format);
938 return FALSE;
941 return TRUE;
944 /* Give a warning at runtime if someone compiles code which calls
945 old routines. */
947 void
948 warn_deprecated (const char *what,
949 const char *file,
950 int line,
951 const char *func)
953 /* Poor man's tracking of functions we've already warned about. */
954 static size_t mask = 0;
956 if (~(size_t) func & ~mask)
958 /* Note: separate sentences in order to allow
959 for translation into other languages. */
960 if (func)
961 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
962 what, file, line, func);
963 else
964 fprintf (stderr, _("Deprecated %s called\n"), what);
965 mask |= ~(size_t) func;
969 /* Helper function for reading uleb128 encoded data. */
971 bfd_vma
972 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
973 bfd_byte *buf,
974 unsigned int *bytes_read_ptr)
976 bfd_vma result;
977 unsigned int num_read;
978 unsigned int shift;
979 unsigned char byte;
981 result = 0;
982 shift = 0;
983 num_read = 0;
986 byte = bfd_get_8 (abfd, buf);
987 buf++;
988 num_read++;
989 result |= (((bfd_vma) byte & 0x7f) << shift);
990 shift += 7;
992 while (byte & 0x80);
993 *bytes_read_ptr = num_read;
994 return result;
997 /* Helper function for reading sleb128 encoded data. */
999 bfd_signed_vma
1000 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1001 bfd_byte *buf,
1002 unsigned int *bytes_read_ptr)
1004 bfd_vma result;
1005 unsigned int shift;
1006 unsigned int num_read;
1007 unsigned char byte;
1009 result = 0;
1010 shift = 0;
1011 num_read = 0;
1014 byte = bfd_get_8 (abfd, buf);
1015 buf ++;
1016 num_read ++;
1017 result |= (((bfd_vma) byte & 0x7f) << shift);
1018 shift += 7;
1020 while (byte & 0x80);
1021 if (shift < 8 * sizeof (result) && (byte & 0x40))
1022 result |= (((bfd_vma) -1) << shift);
1023 *bytes_read_ptr = num_read;
1024 return result;
1027 bfd_boolean
1028 _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
1029 asymbol **symbols ATTRIBUTE_UNUSED,
1030 asymbol *symbol ATTRIBUTE_UNUSED,
1031 const char **filename_ptr ATTRIBUTE_UNUSED,
1032 unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
1034 return FALSE;
1037 bfd_boolean
1038 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1039 asection *isec ATTRIBUTE_UNUSED,
1040 bfd *obfd ATTRIBUTE_UNUSED,
1041 asection *osec ATTRIBUTE_UNUSED,
1042 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1044 return TRUE;