daily update
[binutils.git] / bfd / libbfd.c
blob5146d9525e1cc0b7b18d6c7943edf3317aba1e97
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, 2009
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 /* Routine to handle the core_file_pid entry point for targets without
154 core file support. */
157 _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
159 bfd_set_error (bfd_error_invalid_operation);
160 return 0;
163 const bfd_target *
164 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
166 bfd_set_error (bfd_error_wrong_format);
167 return 0;
170 /* Allocate memory using malloc. */
172 void *
173 bfd_malloc (bfd_size_type size)
175 void *ptr;
177 if (size != (size_t) size)
179 bfd_set_error (bfd_error_no_memory);
180 return NULL;
183 ptr = malloc ((size_t) size);
184 if (ptr == NULL && (size_t) size != 0)
185 bfd_set_error (bfd_error_no_memory);
187 return ptr;
190 /* Allocate memory using malloc, nmemb * size with overflow checking. */
192 void *
193 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
195 void *ptr;
197 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
198 && size != 0
199 && nmemb > ~(bfd_size_type) 0 / size)
201 bfd_set_error (bfd_error_no_memory);
202 return NULL;
205 size *= nmemb;
207 if (size != (size_t) size)
209 bfd_set_error (bfd_error_no_memory);
210 return NULL;
213 ptr = malloc ((size_t) size);
214 if (ptr == NULL && (size_t) size != 0)
215 bfd_set_error (bfd_error_no_memory);
217 return ptr;
220 /* Reallocate memory using realloc. */
222 void *
223 bfd_realloc (void *ptr, bfd_size_type size)
225 void *ret;
227 if (size != (size_t) size)
229 bfd_set_error (bfd_error_no_memory);
230 return NULL;
233 if (ptr == NULL)
234 ret = malloc ((size_t) size);
235 else
236 ret = realloc (ptr, (size_t) size);
238 if (ret == NULL && (size_t) size != 0)
239 bfd_set_error (bfd_error_no_memory);
241 return ret;
244 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
246 void *
247 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
249 void *ret;
251 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
252 && size != 0
253 && nmemb > ~(bfd_size_type) 0 / size)
255 bfd_set_error (bfd_error_no_memory);
256 return NULL;
259 size *= nmemb;
261 if (size != (size_t) size)
263 bfd_set_error (bfd_error_no_memory);
264 return NULL;
267 if (ptr == NULL)
268 ret = malloc ((size_t) size);
269 else
270 ret = realloc (ptr, (size_t) size);
272 if (ret == NULL && (size_t) size != 0)
273 bfd_set_error (bfd_error_no_memory);
275 return ret;
278 /* Reallocate memory using realloc.
279 If this fails the pointer is freed before returning. */
281 void *
282 bfd_realloc_or_free (void *ptr, bfd_size_type size)
284 size_t amount = (size_t) size;
285 void *ret;
287 if (size != amount)
288 ret = NULL;
289 else if (ptr == NULL)
290 ret = malloc (amount);
291 else
292 ret = realloc (ptr, amount);
294 if (ret == NULL)
296 if (amount > 0)
297 bfd_set_error (bfd_error_no_memory);
299 if (ptr != NULL)
300 free (ptr);
303 return ret;
306 /* Allocate memory using malloc and clear it. */
308 void *
309 bfd_zmalloc (bfd_size_type size)
311 void *ptr;
313 if (size != (size_t) size)
315 bfd_set_error (bfd_error_no_memory);
316 return NULL;
319 ptr = malloc ((size_t) size);
321 if ((size_t) size != 0)
323 if (ptr == NULL)
324 bfd_set_error (bfd_error_no_memory);
325 else
326 memset (ptr, 0, (size_t) size);
329 return ptr;
332 /* Allocate memory using malloc (nmemb * size) with overflow checking
333 and clear it. */
335 void *
336 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
338 void *ptr;
340 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
341 && size != 0
342 && nmemb > ~(bfd_size_type) 0 / size)
344 bfd_set_error (bfd_error_no_memory);
345 return NULL;
348 size *= nmemb;
350 if (size != (size_t) size)
352 bfd_set_error (bfd_error_no_memory);
353 return NULL;
356 ptr = malloc ((size_t) size);
358 if ((size_t) size != 0)
360 if (ptr == NULL)
361 bfd_set_error (bfd_error_no_memory);
362 else
363 memset (ptr, 0, (size_t) size);
366 return ptr;
370 INTERNAL_FUNCTION
371 bfd_write_bigendian_4byte_int
373 SYNOPSIS
374 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
376 DESCRIPTION
377 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
378 endian order regardless of what else is going on. This is useful in
379 archives.
382 bfd_boolean
383 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
385 bfd_byte buffer[4];
386 bfd_putb32 ((bfd_vma) i, buffer);
387 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
391 /** The do-it-yourself (byte) sex-change kit */
393 /* The middle letter e.g. get<b>short indicates Big or Little endian
394 target machine. It doesn't matter what the byte order of the host
395 machine is; these routines work for either. */
397 /* FIXME: Should these take a count argument?
398 Answer (gnu@cygnus.com): No, but perhaps they should be inline
399 functions in swap.h #ifdef __GNUC__.
400 Gprof them later and find out. */
403 FUNCTION
404 bfd_put_size
405 FUNCTION
406 bfd_get_size
408 DESCRIPTION
409 These macros as used for reading and writing raw data in
410 sections; each access (except for bytes) is vectored through
411 the target format of the BFD and mangled accordingly. The
412 mangling performs any necessary endian translations and
413 removes alignment restrictions. Note that types accepted and
414 returned by these macros are identical so they can be swapped
415 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
416 to either <<bfd_get_32>> or <<bfd_get_64>>.
418 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
419 system without prototypes, the caller is responsible for making
420 sure that is true, with a cast if necessary. We don't cast
421 them in the macro definitions because that would prevent <<lint>>
422 or <<gcc -Wall>> from detecting sins such as passing a pointer.
423 To detect calling these with less than a <<bfd_vma>>, use
424 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
427 .{* Byte swapping macros for user section data. *}
429 .#define bfd_put_8(abfd, val, ptr) \
430 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
431 .#define bfd_put_signed_8 \
432 . bfd_put_8
433 .#define bfd_get_8(abfd, ptr) \
434 . (*(unsigned char *) (ptr) & 0xff)
435 .#define bfd_get_signed_8(abfd, ptr) \
436 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
438 .#define bfd_put_16(abfd, val, ptr) \
439 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
440 .#define bfd_put_signed_16 \
441 . bfd_put_16
442 .#define bfd_get_16(abfd, ptr) \
443 . BFD_SEND (abfd, bfd_getx16, (ptr))
444 .#define bfd_get_signed_16(abfd, ptr) \
445 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
447 .#define bfd_put_32(abfd, val, ptr) \
448 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
449 .#define bfd_put_signed_32 \
450 . bfd_put_32
451 .#define bfd_get_32(abfd, ptr) \
452 . BFD_SEND (abfd, bfd_getx32, (ptr))
453 .#define bfd_get_signed_32(abfd, ptr) \
454 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
456 .#define bfd_put_64(abfd, val, ptr) \
457 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
458 .#define bfd_put_signed_64 \
459 . bfd_put_64
460 .#define bfd_get_64(abfd, ptr) \
461 . BFD_SEND (abfd, bfd_getx64, (ptr))
462 .#define bfd_get_signed_64(abfd, ptr) \
463 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
465 .#define bfd_get(bits, abfd, ptr) \
466 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
467 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
468 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
469 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
470 . : (abort (), (bfd_vma) - 1))
472 .#define bfd_put(bits, abfd, val, ptr) \
473 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
474 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
475 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
476 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
477 . : (abort (), (void) 0))
482 FUNCTION
483 bfd_h_put_size
484 bfd_h_get_size
486 DESCRIPTION
487 These macros have the same function as their <<bfd_get_x>>
488 brethren, except that they are used for removing information
489 for the header records of object files. Believe it or not,
490 some object files keep their header records in big endian
491 order and their data in little endian order.
493 .{* Byte swapping macros for file header data. *}
495 .#define bfd_h_put_8(abfd, val, ptr) \
496 . bfd_put_8 (abfd, val, ptr)
497 .#define bfd_h_put_signed_8(abfd, val, ptr) \
498 . bfd_put_8 (abfd, val, ptr)
499 .#define bfd_h_get_8(abfd, ptr) \
500 . bfd_get_8 (abfd, ptr)
501 .#define bfd_h_get_signed_8(abfd, ptr) \
502 . bfd_get_signed_8 (abfd, ptr)
504 .#define bfd_h_put_16(abfd, val, ptr) \
505 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
506 .#define bfd_h_put_signed_16 \
507 . bfd_h_put_16
508 .#define bfd_h_get_16(abfd, ptr) \
509 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
510 .#define bfd_h_get_signed_16(abfd, ptr) \
511 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
513 .#define bfd_h_put_32(abfd, val, ptr) \
514 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
515 .#define bfd_h_put_signed_32 \
516 . bfd_h_put_32
517 .#define bfd_h_get_32(abfd, ptr) \
518 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
519 .#define bfd_h_get_signed_32(abfd, ptr) \
520 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
522 .#define bfd_h_put_64(abfd, val, ptr) \
523 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
524 .#define bfd_h_put_signed_64 \
525 . bfd_h_put_64
526 .#define bfd_h_get_64(abfd, ptr) \
527 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
528 .#define bfd_h_get_signed_64(abfd, ptr) \
529 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
531 .{* Aliases for the above, which should eventually go away. *}
533 .#define H_PUT_64 bfd_h_put_64
534 .#define H_PUT_32 bfd_h_put_32
535 .#define H_PUT_16 bfd_h_put_16
536 .#define H_PUT_8 bfd_h_put_8
537 .#define H_PUT_S64 bfd_h_put_signed_64
538 .#define H_PUT_S32 bfd_h_put_signed_32
539 .#define H_PUT_S16 bfd_h_put_signed_16
540 .#define H_PUT_S8 bfd_h_put_signed_8
541 .#define H_GET_64 bfd_h_get_64
542 .#define H_GET_32 bfd_h_get_32
543 .#define H_GET_16 bfd_h_get_16
544 .#define H_GET_8 bfd_h_get_8
545 .#define H_GET_S64 bfd_h_get_signed_64
546 .#define H_GET_S32 bfd_h_get_signed_32
547 .#define H_GET_S16 bfd_h_get_signed_16
548 .#define H_GET_S8 bfd_h_get_signed_8
552 /* Sign extension to bfd_signed_vma. */
553 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
554 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
555 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
556 #define COERCE64(x) \
557 (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
559 bfd_vma
560 bfd_getb16 (const void *p)
562 const bfd_byte *addr = (const bfd_byte *) p;
563 return (addr[0] << 8) | addr[1];
566 bfd_vma
567 bfd_getl16 (const void *p)
569 const bfd_byte *addr = (const bfd_byte *) p;
570 return (addr[1] << 8) | addr[0];
573 bfd_signed_vma
574 bfd_getb_signed_16 (const void *p)
576 const bfd_byte *addr = (const bfd_byte *) p;
577 return COERCE16 ((addr[0] << 8) | addr[1]);
580 bfd_signed_vma
581 bfd_getl_signed_16 (const void *p)
583 const bfd_byte *addr = (const bfd_byte *) p;
584 return COERCE16 ((addr[1] << 8) | addr[0]);
587 void
588 bfd_putb16 (bfd_vma data, void *p)
590 bfd_byte *addr = (bfd_byte *) p;
591 addr[0] = (data >> 8) & 0xff;
592 addr[1] = data & 0xff;
595 void
596 bfd_putl16 (bfd_vma data, void *p)
598 bfd_byte *addr = (bfd_byte *) p;
599 addr[0] = data & 0xff;
600 addr[1] = (data >> 8) & 0xff;
603 bfd_vma
604 bfd_getb32 (const void *p)
606 const bfd_byte *addr = (const bfd_byte *) p;
607 unsigned long v;
609 v = (unsigned long) addr[0] << 24;
610 v |= (unsigned long) addr[1] << 16;
611 v |= (unsigned long) addr[2] << 8;
612 v |= (unsigned long) addr[3];
613 return v;
616 bfd_vma
617 bfd_getl32 (const void *p)
619 const bfd_byte *addr = (const bfd_byte *) p;
620 unsigned long v;
622 v = (unsigned long) addr[0];
623 v |= (unsigned long) addr[1] << 8;
624 v |= (unsigned long) addr[2] << 16;
625 v |= (unsigned long) addr[3] << 24;
626 return v;
629 bfd_signed_vma
630 bfd_getb_signed_32 (const void *p)
632 const bfd_byte *addr = (const bfd_byte *) p;
633 unsigned long v;
635 v = (unsigned long) addr[0] << 24;
636 v |= (unsigned long) addr[1] << 16;
637 v |= (unsigned long) addr[2] << 8;
638 v |= (unsigned long) addr[3];
639 return COERCE32 (v);
642 bfd_signed_vma
643 bfd_getl_signed_32 (const void *p)
645 const bfd_byte *addr = (const bfd_byte *) p;
646 unsigned long v;
648 v = (unsigned long) addr[0];
649 v |= (unsigned long) addr[1] << 8;
650 v |= (unsigned long) addr[2] << 16;
651 v |= (unsigned long) addr[3] << 24;
652 return COERCE32 (v);
655 bfd_uint64_t
656 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
658 #ifdef BFD_HOST_64_BIT
659 const bfd_byte *addr = (const bfd_byte *) p;
660 bfd_uint64_t v;
662 v = addr[0]; v <<= 8;
663 v |= addr[1]; v <<= 8;
664 v |= addr[2]; v <<= 8;
665 v |= addr[3]; v <<= 8;
666 v |= addr[4]; v <<= 8;
667 v |= addr[5]; v <<= 8;
668 v |= addr[6]; v <<= 8;
669 v |= addr[7];
671 return v;
672 #else
673 BFD_FAIL();
674 return 0;
675 #endif
678 bfd_uint64_t
679 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
681 #ifdef BFD_HOST_64_BIT
682 const bfd_byte *addr = (const bfd_byte *) p;
683 bfd_uint64_t v;
685 v = addr[7]; v <<= 8;
686 v |= addr[6]; v <<= 8;
687 v |= addr[5]; v <<= 8;
688 v |= addr[4]; v <<= 8;
689 v |= addr[3]; v <<= 8;
690 v |= addr[2]; v <<= 8;
691 v |= addr[1]; v <<= 8;
692 v |= addr[0];
694 return v;
695 #else
696 BFD_FAIL();
697 return 0;
698 #endif
702 bfd_int64_t
703 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
705 #ifdef BFD_HOST_64_BIT
706 const bfd_byte *addr = (const bfd_byte *) p;
707 bfd_uint64_t v;
709 v = addr[0]; v <<= 8;
710 v |= addr[1]; v <<= 8;
711 v |= addr[2]; v <<= 8;
712 v |= addr[3]; v <<= 8;
713 v |= addr[4]; v <<= 8;
714 v |= addr[5]; v <<= 8;
715 v |= addr[6]; v <<= 8;
716 v |= addr[7];
718 return COERCE64 (v);
719 #else
720 BFD_FAIL();
721 return 0;
722 #endif
725 bfd_int64_t
726 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
728 #ifdef BFD_HOST_64_BIT
729 const bfd_byte *addr = (const bfd_byte *) p;
730 bfd_uint64_t v;
732 v = addr[7]; v <<= 8;
733 v |= addr[6]; v <<= 8;
734 v |= addr[5]; v <<= 8;
735 v |= addr[4]; v <<= 8;
736 v |= addr[3]; v <<= 8;
737 v |= addr[2]; v <<= 8;
738 v |= addr[1]; v <<= 8;
739 v |= addr[0];
741 return COERCE64 (v);
742 #else
743 BFD_FAIL();
744 return 0;
745 #endif
748 void
749 bfd_putb32 (bfd_vma data, void *p)
751 bfd_byte *addr = (bfd_byte *) p;
752 addr[0] = (data >> 24) & 0xff;
753 addr[1] = (data >> 16) & 0xff;
754 addr[2] = (data >> 8) & 0xff;
755 addr[3] = data & 0xff;
758 void
759 bfd_putl32 (bfd_vma data, void *p)
761 bfd_byte *addr = (bfd_byte *) p;
762 addr[0] = data & 0xff;
763 addr[1] = (data >> 8) & 0xff;
764 addr[2] = (data >> 16) & 0xff;
765 addr[3] = (data >> 24) & 0xff;
768 void
769 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
771 #ifdef BFD_HOST_64_BIT
772 bfd_byte *addr = (bfd_byte *) p;
773 addr[0] = (data >> (7*8)) & 0xff;
774 addr[1] = (data >> (6*8)) & 0xff;
775 addr[2] = (data >> (5*8)) & 0xff;
776 addr[3] = (data >> (4*8)) & 0xff;
777 addr[4] = (data >> (3*8)) & 0xff;
778 addr[5] = (data >> (2*8)) & 0xff;
779 addr[6] = (data >> (1*8)) & 0xff;
780 addr[7] = (data >> (0*8)) & 0xff;
781 #else
782 BFD_FAIL();
783 #endif
786 void
787 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
789 #ifdef BFD_HOST_64_BIT
790 bfd_byte *addr = (bfd_byte *) p;
791 addr[7] = (data >> (7*8)) & 0xff;
792 addr[6] = (data >> (6*8)) & 0xff;
793 addr[5] = (data >> (5*8)) & 0xff;
794 addr[4] = (data >> (4*8)) & 0xff;
795 addr[3] = (data >> (3*8)) & 0xff;
796 addr[2] = (data >> (2*8)) & 0xff;
797 addr[1] = (data >> (1*8)) & 0xff;
798 addr[0] = (data >> (0*8)) & 0xff;
799 #else
800 BFD_FAIL();
801 #endif
804 void
805 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
807 bfd_byte *addr = (bfd_byte *) p;
808 int i;
809 int bytes;
811 if (bits % 8 != 0)
812 abort ();
814 bytes = bits / 8;
815 for (i = 0; i < bytes; i++)
817 int addr_index = big_p ? bytes - i - 1 : i;
819 addr[addr_index] = data & 0xff;
820 data >>= 8;
824 bfd_uint64_t
825 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
827 const bfd_byte *addr = (const bfd_byte *) p;
828 bfd_uint64_t data;
829 int i;
830 int bytes;
832 if (bits % 8 != 0)
833 abort ();
835 data = 0;
836 bytes = bits / 8;
837 for (i = 0; i < bytes; i++)
839 int addr_index = big_p ? i : bytes - i - 1;
841 data = (data << 8) | addr[addr_index];
844 return data;
847 /* Default implementation */
849 bfd_boolean
850 _bfd_generic_get_section_contents (bfd *abfd,
851 sec_ptr section,
852 void *location,
853 file_ptr offset,
854 bfd_size_type count)
856 bfd_size_type sz;
857 if (count == 0)
858 return TRUE;
860 sz = section->rawsize ? section->rawsize : section->size;
861 if (offset + count < count
862 || offset + count > sz)
864 bfd_set_error (bfd_error_invalid_operation);
865 return FALSE;
868 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
869 || bfd_bread (location, count, abfd) != count)
870 return FALSE;
872 return TRUE;
875 bfd_boolean
876 _bfd_generic_get_section_contents_in_window
877 (bfd *abfd ATTRIBUTE_UNUSED,
878 sec_ptr section ATTRIBUTE_UNUSED,
879 bfd_window *w ATTRIBUTE_UNUSED,
880 file_ptr offset ATTRIBUTE_UNUSED,
881 bfd_size_type count ATTRIBUTE_UNUSED)
883 #ifdef USE_MMAP
884 bfd_size_type sz;
886 if (count == 0)
887 return TRUE;
888 if (abfd->xvec->_bfd_get_section_contents
889 != _bfd_generic_get_section_contents)
891 /* We don't know what changes the bfd's get_section_contents
892 method may have to make. So punt trying to map the file
893 window, and let get_section_contents do its thing. */
894 /* @@ FIXME : If the internal window has a refcount of 1 and was
895 allocated with malloc instead of mmap, just reuse it. */
896 bfd_free_window (w);
897 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
898 if (w->i == NULL)
899 return FALSE;
900 w->i->data = bfd_malloc (count);
901 if (w->i->data == NULL)
903 free (w->i);
904 w->i = NULL;
905 return FALSE;
907 w->i->mapped = 0;
908 w->i->refcount = 1;
909 w->size = w->i->size = count;
910 w->data = w->i->data;
911 return bfd_get_section_contents (abfd, section, w->data, offset, count);
913 sz = section->rawsize ? section->rawsize : section->size;
914 if (offset + count > sz
915 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
916 TRUE))
917 return FALSE;
918 return TRUE;
919 #else
920 abort ();
921 #endif
924 /* This generic function can only be used in implementations where creating
925 NEW sections is disallowed. It is useful in patching existing sections
926 in read-write files, though. See other set_section_contents functions
927 to see why it doesn't work for new sections. */
928 bfd_boolean
929 _bfd_generic_set_section_contents (bfd *abfd,
930 sec_ptr section,
931 const void *location,
932 file_ptr offset,
933 bfd_size_type count)
935 if (count == 0)
936 return TRUE;
938 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
939 || bfd_bwrite (location, count, abfd) != count)
940 return FALSE;
942 return TRUE;
946 INTERNAL_FUNCTION
947 bfd_log2
949 SYNOPSIS
950 unsigned int bfd_log2 (bfd_vma x);
952 DESCRIPTION
953 Return the log base 2 of the value supplied, rounded up. E.g., an
954 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
957 unsigned int
958 bfd_log2 (bfd_vma x)
960 unsigned int result = 0;
962 while ((x = (x >> 1)) != 0)
963 ++result;
964 return result;
967 bfd_boolean
968 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
970 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
972 return name[0] == locals_prefix;
975 /* Can be used from / for bfd_merge_private_bfd_data to check that
976 endianness matches between input and output file. Returns
977 TRUE for a match, otherwise returns FALSE and emits an error. */
978 bfd_boolean
979 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
981 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
982 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
983 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
985 const char *msg;
987 if (bfd_big_endian (ibfd))
988 msg = _("%B: compiled for a big endian system and target is little endian");
989 else
990 msg = _("%B: compiled for a little endian system and target is big endian");
992 (*_bfd_error_handler) (msg, ibfd);
994 bfd_set_error (bfd_error_wrong_format);
995 return FALSE;
998 return TRUE;
1001 /* Give a warning at runtime if someone compiles code which calls
1002 old routines. */
1004 void
1005 warn_deprecated (const char *what,
1006 const char *file,
1007 int line,
1008 const char *func)
1010 /* Poor man's tracking of functions we've already warned about. */
1011 static size_t mask = 0;
1013 if (~(size_t) func & ~mask)
1015 /* Note: separate sentences in order to allow
1016 for translation into other languages. */
1017 if (func)
1018 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
1019 what, file, line, func);
1020 else
1021 fprintf (stderr, _("Deprecated %s called\n"), what);
1022 mask |= ~(size_t) func;
1026 /* Helper function for reading uleb128 encoded data. */
1028 bfd_vma
1029 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1030 bfd_byte *buf,
1031 unsigned int *bytes_read_ptr)
1033 bfd_vma result;
1034 unsigned int num_read;
1035 unsigned int shift;
1036 unsigned char byte;
1038 result = 0;
1039 shift = 0;
1040 num_read = 0;
1043 byte = bfd_get_8 (abfd, buf);
1044 buf++;
1045 num_read++;
1046 result |= (((bfd_vma) byte & 0x7f) << shift);
1047 shift += 7;
1049 while (byte & 0x80);
1050 *bytes_read_ptr = num_read;
1051 return result;
1054 /* Helper function for reading sleb128 encoded data. */
1056 bfd_signed_vma
1057 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1058 bfd_byte *buf,
1059 unsigned int *bytes_read_ptr)
1061 bfd_vma result;
1062 unsigned int shift;
1063 unsigned int num_read;
1064 unsigned char byte;
1066 result = 0;
1067 shift = 0;
1068 num_read = 0;
1071 byte = bfd_get_8 (abfd, buf);
1072 buf ++;
1073 num_read ++;
1074 result |= (((bfd_vma) byte & 0x7f) << shift);
1075 shift += 7;
1077 while (byte & 0x80);
1078 if (shift < 8 * sizeof (result) && (byte & 0x40))
1079 result |= (((bfd_vma) -1) << shift);
1080 *bytes_read_ptr = num_read;
1081 return result;
1084 bfd_boolean
1085 _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
1086 asymbol **symbols ATTRIBUTE_UNUSED,
1087 asymbol *symbol ATTRIBUTE_UNUSED,
1088 const char **filename_ptr ATTRIBUTE_UNUSED,
1089 unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
1091 return FALSE;
1094 bfd_boolean
1095 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1096 asection *isec ATTRIBUTE_UNUSED,
1097 bfd *obfd ATTRIBUTE_UNUSED,
1098 asection *osec ATTRIBUTE_UNUSED,
1099 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1101 return TRUE;