1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #ifndef HAVE_GETPAGESIZE
28 #define getpagesize() 2048
31 static int real_read PARAMS ((PTR, size_t, size_t, FILE *));
38 These routines are used within BFD.
39 They are not intended for export, but are documented here for
43 /* A routine which is used in target vectors for unsupported
48 bfd *ignore ATTRIBUTE_UNUSED;
50 bfd_set_error (bfd_error_invalid_operation);
54 /* A routine which is used in target vectors for supported operations
55 which do not actually do anything. */
59 bfd *ignore ATTRIBUTE_UNUSED;
64 /* A routine which is used in target vectors for unsupported
65 operations which return a pointer value. */
68 bfd_nullvoidptr (ignore)
69 bfd *ignore ATTRIBUTE_UNUSED;
71 bfd_set_error (bfd_error_invalid_operation);
77 bfd *ignore ATTRIBUTE_UNUSED;
84 bfd *ignore ATTRIBUTE_UNUSED;
91 bfd *ignore ATTRIBUTE_UNUSED;
96 /* A routine which is used in target vectors for unsupported
97 operations which return -1 on error. */
100 _bfd_n1 (ignore_abfd)
101 bfd *ignore_abfd ATTRIBUTE_UNUSED;
103 bfd_set_error (bfd_error_invalid_operation);
109 bfd *ignore ATTRIBUTE_UNUSED;
114 _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
115 bfd *ignore_core_bfd ATTRIBUTE_UNUSED;
116 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED;
118 bfd_set_error (bfd_error_invalid_operation);
122 /* Routine to handle core_file_failing_command entry point for targets
123 without core file support. */
126 _bfd_nocore_core_file_failing_command (ignore_abfd)
127 bfd *ignore_abfd ATTRIBUTE_UNUSED;
129 bfd_set_error (bfd_error_invalid_operation);
133 /* Routine to handle core_file_failing_signal entry point for targets
134 without core file support. */
137 _bfd_nocore_core_file_failing_signal (ignore_abfd)
138 bfd *ignore_abfd ATTRIBUTE_UNUSED;
140 bfd_set_error (bfd_error_invalid_operation);
145 _bfd_dummy_target (ignore_abfd)
146 bfd *ignore_abfd ATTRIBUTE_UNUSED;
148 bfd_set_error (bfd_error_wrong_format);
152 /* Allocate memory using malloc. */
160 ptr = (PTR) malloc (size);
161 if (ptr == NULL && size != 0)
162 bfd_set_error (bfd_error_no_memory);
166 /* Reallocate memory using realloc. */
169 bfd_realloc (ptr, size)
178 ret = realloc (ptr, size);
181 bfd_set_error (bfd_error_no_memory);
186 /* Allocate memory using malloc and clear it. */
194 ptr = (PTR) malloc (size);
199 bfd_set_error (bfd_error_no_memory);
201 memset (ptr, 0, size);
209 /* Note that archive entries don't have streams; they share their parent's.
210 This allows someone to play with the iostream behind BFD's back.
212 Also, note that the origin pointer points to the beginning of a file's
213 contents (0 for non-archive elements). For archive entries this is the
214 first octet in the file, NOT the beginning of the archive header. */
217 real_read (where, a,b, file)
223 /* FIXME - this looks like an optimization, but it's really to cover
224 up for a feature of some OSs (not solaris - sigh) that
225 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
226 internally and tries to link against them. BFD seems to be smart
227 enough to realize there are no symbol records in the "file" that
228 doesn't exist but attempts to read them anyway. On Solaris,
229 attempting to read zero bytes from a NULL file results in a core
230 dump, but on other platforms it just returns zero bytes read.
231 This makes it to something reasonable. - DJ */
232 if (a == 0 || b == 0)
235 #if defined (__VAX) && defined (VMS)
236 /* Apparently fread on Vax VMS does not keep the record length
238 return read (fileno (file), where, a * b);
240 return fread (where, a, b, file);
244 /* Return value is amount read (FIXME: how are errors and end of file dealt
245 with? We never call bfd_set_error, which is probably a mistake). */
248 bfd_read (ptr, size, nitems, abfd)
251 bfd_size_type nitems;
256 if ((abfd->flags & BFD_IN_MEMORY) != 0)
258 struct bfd_in_memory *bim;
261 bim = (struct bfd_in_memory *) abfd->iostream;
263 if (abfd->where + get > bim->size)
265 if (bim->size < (bfd_size_type) abfd->where)
268 get = bim->size - abfd->where;
269 bfd_set_error (bfd_error_file_truncated);
271 memcpy (ptr, bim->buffer + abfd->where, get);
276 nread = real_read (ptr, 1, (size_t) (size*nitems), bfd_cache_lookup(abfd));
278 abfd->where += nread;
280 /* Set bfd_error if we did not read as much data as we expected.
282 If the read failed due to an error set the bfd_error_system_call,
283 else set bfd_error_file_truncated.
285 A BFD backend may wish to override bfd_error_file_truncated to
286 provide something more useful (eg. no_symbols or wrong_format). */
287 if (nread != (int) (size * nitems))
289 if (ferror (bfd_cache_lookup (abfd)))
290 bfd_set_error (bfd_error_system_call);
292 bfd_set_error (bfd_error_file_truncated);
298 /* The window support stuff should probably be broken out into
300 /* The idea behind the next and refcount fields is that one mapped
301 region can suffice for multiple read-only windows or multiple
302 non-overlapping read-write windows. It's not implemented yet
304 struct _bfd_window_internal {
305 struct _bfd_window_internal *next;
308 int refcount : 31; /* should be enough... */
309 unsigned mapped : 1; /* 1 = mmap, 0 = malloc */
313 bfd_init_window (windowp)
321 /* Currently, if USE_MMAP is undefined, none if the window stuff is
322 used. Okay, so it's mis-named. At least the command-line option
323 "--without-mmap" is more obvious than "--without-windows" or some
327 #undef HAVE_MPROTECT /* code's not tested yet */
329 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
330 #include <sys/mman.h>
337 static int debug_windows;
340 bfd_free_window (windowp)
343 bfd_window_internal *i = windowp->i;
350 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
351 windowp, windowp->data, windowp->size, windowp->i);
352 if (i->refcount != 0)
358 munmap (i->data, i->size);
365 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
372 /* There should be no more references to i at this point. */
376 static int ok_to_map = 1;
379 bfd_get_file_window (abfd, offset, size, windowp, writable)
386 static size_t pagesize;
387 bfd_window_internal *i = windowp->i;
388 size_t size_to_alloc = size;
391 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
392 abfd, (long) offset, (long) size,
393 windowp, windowp->data, (unsigned long) windowp->size,
394 windowp->i, writable);
396 /* Make sure we know the page size, so we can be friendly to mmap. */
398 pagesize = getpagesize ();
404 windowp->i = i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
411 && (i->data == 0 || i->mapped == 1)
412 && (abfd->flags & BFD_IN_MEMORY) == 0)
414 file_ptr file_offset, offset2;
419 /* Find the real file and the real offset into it. */
420 while (abfd->my_archive != NULL)
422 offset += abfd->origin;
423 abfd = abfd->my_archive;
425 f = bfd_cache_lookup (abfd);
428 /* Compute offsets and size for mmap and for the user's data. */
429 offset2 = offset % pagesize;
432 file_offset = offset - offset2;
433 real_size = offset + size - file_offset;
434 real_size = real_size + pagesize - 1;
435 real_size -= real_size % pagesize;
437 /* If we're re-using a memory region, make sure it's big enough. */
438 if (i->data && i->size < size)
440 munmap (i->data, i->size);
443 i->data = mmap (i->data, real_size,
444 writable ? PROT_WRITE | PROT_READ : PROT_READ,
446 ? MAP_FILE | MAP_PRIVATE
447 : MAP_FILE | MAP_SHARED),
449 if (i->data == (PTR) -1)
451 /* An error happened. Report it, or try using malloc, or
453 bfd_set_error (bfd_error_system_call);
457 fprintf (stderr, "\t\tmmap failed!\n");
461 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
462 (long) real_size, i->data, (long) offset2);
464 windowp->data = (PTR) ((bfd_byte *) i->data + offset2);
465 windowp->size = size;
469 else if (debug_windows)
472 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
473 (unsigned long) i->data, (int) i->mapped);
475 fprintf (stderr, _("not mapping: env var not set\n"));
484 size_to_alloc += pagesize - 1;
485 size_to_alloc -= size_to_alloc % pagesize;
489 fprintf (stderr, "\n\t%s(%6ld)",
490 i->data ? "realloc" : " malloc", (long) size_to_alloc);
491 i->data = (PTR) bfd_realloc (i->data, size_to_alloc);
493 fprintf (stderr, "\t-> %p\n", i->data);
497 if (size_to_alloc == 0)
499 bfd_set_error (bfd_error_no_memory);
502 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
504 i->size = bfd_read (i->data, size, 1, abfd);
512 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
514 mprotect (i->data, i->size, PROT_READ);
517 windowp->data = i->data;
518 windowp->size = i->size;
522 #endif /* USE_MMAP */
525 bfd_write (ptr, size, nitems, abfd)
528 bfd_size_type nitems;
533 if ((abfd->flags & BFD_IN_MEMORY) != 0)
535 struct bfd_in_memory *bim = (struct bfd_in_memory *) (abfd->iostream);
537 if (abfd->where + size > bim->size)
539 long newsize, oldsize = (bim->size + 127) & ~127;
540 bim->size = abfd->where + size;
541 /* Round up to cut down on memory fragmentation */
542 newsize = (bim->size + 127) & ~127;
543 if (newsize > oldsize)
545 bim->buffer = bfd_realloc (bim->buffer, newsize);
546 if (bim->buffer == 0)
553 memcpy (bim->buffer + abfd->where, ptr, size);
558 nwrote = fwrite (ptr, 1, (size_t) (size * nitems),
559 bfd_cache_lookup (abfd));
561 abfd->where += nwrote;
562 if ((bfd_size_type) nwrote != size * nitems)
568 bfd_set_error (bfd_error_system_call);
575 bfd_write_bigendian_4byte_int
578 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
581 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
582 endian order regardless of what else is going on. This is useful in
587 bfd_write_bigendian_4byte_int (abfd, i)
592 bfd_putb32(i, buffer);
593 if (bfd_write((PTR)buffer, 4, 1, abfd) != 4)
603 if ((abfd->flags & BFD_IN_MEMORY) != 0)
606 ptr = ftell (bfd_cache_lookup(abfd));
608 if (abfd->my_archive)
618 if ((abfd->flags & BFD_IN_MEMORY) != 0)
620 return fflush (bfd_cache_lookup(abfd));
623 /* Returns 0 for success, negative value for failure (in which case
624 bfd_get_error can retrieve the error code). */
626 bfd_stat (abfd, statbuf)
628 struct stat *statbuf;
633 if ((abfd->flags & BFD_IN_MEMORY) != 0)
636 f = bfd_cache_lookup (abfd);
639 bfd_set_error (bfd_error_system_call);
642 result = fstat (fileno (f), statbuf);
644 bfd_set_error (bfd_error_system_call);
648 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
649 can retrieve the error code). */
652 bfd_seek (abfd, position, direction)
659 file_ptr file_position;
660 /* For the time being, a BFD may not seek to it's end. The problem
661 is that we don't easily have a way to recognize the end of an
662 element in an archive. */
664 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
666 if (direction == SEEK_CUR && position == 0)
669 if ((abfd->flags & BFD_IN_MEMORY) != 0)
671 struct bfd_in_memory *bim;
673 bim = (struct bfd_in_memory *) abfd->iostream;
675 if (direction == SEEK_SET)
676 abfd->where = position;
678 abfd->where += position;
680 if ((bfd_size_type) abfd->where > bim->size)
682 if ((abfd->direction == write_direction) ||
683 (abfd->direction == both_direction))
685 long newsize, oldsize = (bim->size + 127) & ~127;
686 bim->size = abfd->where;
687 /* Round up to cut down on memory fragmentation */
688 newsize = (bim->size + 127) & ~127;
689 if (newsize > oldsize)
691 bim->buffer = bfd_realloc (bim->buffer, newsize);
692 if (bim->buffer == 0)
695 bfd_set_error (bfd_error_no_memory);
702 abfd->where = bim->size;
703 bfd_set_error (bfd_error_file_truncated);
710 if (abfd->format != bfd_archive && abfd->my_archive == 0)
713 /* Explanation for this code: I'm only about 95+% sure that the above
714 conditions are sufficient and that all i/o calls are properly
715 adjusting the `where' field. So this is sort of an `assert'
716 that the `where' field is correct. If we can go a while without
717 tripping the abort, we can probably safely disable this code,
718 so that the real optimizations happen. */
719 file_ptr where_am_i_now;
720 where_am_i_now = ftell (bfd_cache_lookup (abfd));
721 if (abfd->my_archive)
722 where_am_i_now -= abfd->origin;
723 if (where_am_i_now != abfd->where)
726 if (direction == SEEK_SET && position == abfd->where)
731 /* We need something smarter to optimize access to archives.
732 Currently, anything inside an archive is read via the file
733 handle for the archive. Which means that a bfd_seek on one
734 component affects the `current position' in the archive, as
735 well as in any other component.
737 It might be sufficient to put a spike through the cache
738 abstraction, and look to the archive for the file position,
739 but I think we should try for something cleaner.
741 In the meantime, no optimization for archives. */
744 f = bfd_cache_lookup (abfd);
745 file_position = position;
746 if (direction == SEEK_SET && abfd->my_archive != NULL)
747 file_position += abfd->origin;
749 result = fseek (f, file_position, direction);
753 int hold_errno = errno;
755 /* Force redetermination of `where' field. */
758 /* An EINVAL error probably means that the file offset was
760 if (hold_errno == EINVAL)
761 bfd_set_error (bfd_error_file_truncated);
764 bfd_set_error (bfd_error_system_call);
770 /* Adjust `where' field. */
771 if (direction == SEEK_SET)
772 abfd->where = position;
774 abfd->where += position;
779 /** The do-it-yourself (byte) sex-change kit */
781 /* The middle letter e.g. get<b>short indicates Big or Little endian
782 target machine. It doesn't matter what the byte order of the host
783 machine is; these routines work for either. */
785 /* FIXME: Should these take a count argument?
786 Answer (gnu@cygnus.com): No, but perhaps they should be inline
787 functions in swap.h #ifdef __GNUC__.
788 Gprof them later and find out. */
797 These macros as used for reading and writing raw data in
798 sections; each access (except for bytes) is vectored through
799 the target format of the BFD and mangled accordingly. The
800 mangling performs any necessary endian translations and
801 removes alignment restrictions. Note that types accepted and
802 returned by these macros are identical so they can be swapped
803 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
804 to either <<bfd_get_32>> or <<bfd_get_64>>.
806 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
807 system without prototypes, the caller is responsible for making
808 sure that is true, with a cast if necessary. We don't cast
809 them in the macro definitions because that would prevent <<lint>>
810 or <<gcc -Wall>> from detecting sins such as passing a pointer.
811 To detect calling these with less than a <<bfd_vma>>, use
812 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
815 .{* Byte swapping macros for user section data. *}
817 .#define bfd_put_8(abfd, val, ptr) \
818 . ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val)))
819 .#define bfd_put_signed_8 \
821 .#define bfd_get_8(abfd, ptr) \
822 . (*(unsigned char *) (ptr))
823 .#define bfd_get_signed_8(abfd, ptr) \
824 . ((*(unsigned char *) (ptr) ^ 0x80) - 0x80)
826 .#define bfd_put_16(abfd, val, ptr) \
827 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
828 .#define bfd_put_signed_16 \
830 .#define bfd_get_16(abfd, ptr) \
831 . BFD_SEND(abfd, bfd_getx16, (ptr))
832 .#define bfd_get_signed_16(abfd, ptr) \
833 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
835 .#define bfd_put_32(abfd, val, ptr) \
836 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
837 .#define bfd_put_signed_32 \
839 .#define bfd_get_32(abfd, ptr) \
840 . BFD_SEND(abfd, bfd_getx32, (ptr))
841 .#define bfd_get_signed_32(abfd, ptr) \
842 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
844 .#define bfd_put_64(abfd, val, ptr) \
845 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
846 .#define bfd_put_signed_64 \
848 .#define bfd_get_64(abfd, ptr) \
849 . BFD_SEND(abfd, bfd_getx64, (ptr))
850 .#define bfd_get_signed_64(abfd, ptr) \
851 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
853 .#define bfd_get(bits, abfd, ptr) \
854 . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \
855 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
856 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
857 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
858 . : (abort (), (bfd_vma) - 1))
860 .#define bfd_put(bits, abfd, val, ptr) \
861 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
862 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
863 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
864 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
865 . : (abort (), (void) 0))
875 These macros have the same function as their <<bfd_get_x>>
876 bretheren, except that they are used for removing information
877 for the header records of object files. Believe it or not,
878 some object files keep their header records in big endian
879 order and their data in little endian order.
881 .{* Byte swapping macros for file header data. *}
883 .#define bfd_h_put_8(abfd, val, ptr) \
884 . bfd_put_8 (abfd, val, ptr)
885 .#define bfd_h_put_signed_8(abfd, val, ptr) \
886 . bfd_put_8 (abfd, val, ptr)
887 .#define bfd_h_get_8(abfd, ptr) \
888 . bfd_get_8 (abfd, ptr)
889 .#define bfd_h_get_signed_8(abfd, ptr) \
890 . bfd_get_signed_8 (abfd, ptr)
892 .#define bfd_h_put_16(abfd, val, ptr) \
893 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
894 .#define bfd_h_put_signed_16 \
896 .#define bfd_h_get_16(abfd, ptr) \
897 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
898 .#define bfd_h_get_signed_16(abfd, ptr) \
899 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
901 .#define bfd_h_put_32(abfd, val, ptr) \
902 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
903 .#define bfd_h_put_signed_32 \
905 .#define bfd_h_get_32(abfd, ptr) \
906 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
907 .#define bfd_h_get_signed_32(abfd, ptr) \
908 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
910 .#define bfd_h_put_64(abfd, val, ptr) \
911 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
912 .#define bfd_h_put_signed_64 \
914 .#define bfd_h_get_64(abfd, ptr) \
915 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
916 .#define bfd_h_get_signed_64(abfd, ptr) \
917 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
921 /* Sign extension to bfd_signed_vma. */
922 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
923 #define COERCE32(x) \
924 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000))
925 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
926 #define COERCE64(x) \
927 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
931 register const bfd_byte *addr;
933 return (addr[0] << 8) | addr[1];
938 register const bfd_byte *addr;
940 return (addr[1] << 8) | addr[0];
944 bfd_getb_signed_16 (addr)
945 register const bfd_byte *addr;
947 return COERCE16((addr[0] << 8) | addr[1]);
951 bfd_getl_signed_16 (addr)
952 register const bfd_byte *addr;
954 return COERCE16((addr[1] << 8) | addr[0]);
958 bfd_putb16 (data, addr)
960 register bfd_byte *addr;
962 addr[0] = (bfd_byte) (data >> 8);
963 addr[1] = (bfd_byte) data;
967 bfd_putl16 (data, addr)
969 register bfd_byte *addr;
971 addr[0] = (bfd_byte) data;
972 addr[1] = (bfd_byte) (data >> 8);
977 register const bfd_byte *addr;
981 v = (unsigned long) addr[0] << 24;
982 v |= (unsigned long) addr[1] << 16;
983 v |= (unsigned long) addr[2] << 8;
984 v |= (unsigned long) addr[3];
990 register const bfd_byte *addr;
994 v = (unsigned long) addr[0];
995 v |= (unsigned long) addr[1] << 8;
996 v |= (unsigned long) addr[2] << 16;
997 v |= (unsigned long) addr[3] << 24;
1002 bfd_getb_signed_32 (addr)
1003 register const bfd_byte *addr;
1007 v = (unsigned long) addr[0] << 24;
1008 v |= (unsigned long) addr[1] << 16;
1009 v |= (unsigned long) addr[2] << 8;
1010 v |= (unsigned long) addr[3];
1011 return COERCE32 (v);
1015 bfd_getl_signed_32 (addr)
1016 register const bfd_byte *addr;
1020 v = (unsigned long) addr[0];
1021 v |= (unsigned long) addr[1] << 8;
1022 v |= (unsigned long) addr[2] << 16;
1023 v |= (unsigned long) addr[3] << 24;
1024 return COERCE32 (v);
1029 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1034 high= ((((((((addr[0]) << 8) |
1039 low = (((((((((bfd_vma)addr[4]) << 8) |
1044 return high << 32 | low;
1053 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1057 high= (((((((addr[7] << 8) |
1062 low = ((((((((bfd_vma)addr[3] << 8) |
1067 return high << 32 | low;
1076 bfd_getb_signed_64 (addr)
1077 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1082 high= ((((((((addr[0]) << 8) |
1087 low = (((((((((bfd_vma)addr[4]) << 8) |
1092 return COERCE64(high << 32 | low);
1100 bfd_getl_signed_64 (addr)
1101 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1105 high= (((((((addr[7] << 8) |
1110 low = ((((((((bfd_vma)addr[3] << 8) |
1115 return COERCE64(high << 32 | low);
1123 bfd_putb32 (data, addr)
1125 register bfd_byte *addr;
1127 addr[0] = (bfd_byte) (data >> 24);
1128 addr[1] = (bfd_byte) (data >> 16);
1129 addr[2] = (bfd_byte) (data >> 8);
1130 addr[3] = (bfd_byte) data;
1134 bfd_putl32 (data, addr)
1136 register bfd_byte *addr;
1138 addr[0] = (bfd_byte) data;
1139 addr[1] = (bfd_byte) (data >> 8);
1140 addr[2] = (bfd_byte) (data >> 16);
1141 addr[3] = (bfd_byte) (data >> 24);
1145 bfd_putb64 (data, addr)
1146 bfd_vma data ATTRIBUTE_UNUSED;
1147 register bfd_byte *addr ATTRIBUTE_UNUSED;
1150 addr[0] = (bfd_byte) (data >> (7*8));
1151 addr[1] = (bfd_byte) (data >> (6*8));
1152 addr[2] = (bfd_byte) (data >> (5*8));
1153 addr[3] = (bfd_byte) (data >> (4*8));
1154 addr[4] = (bfd_byte) (data >> (3*8));
1155 addr[5] = (bfd_byte) (data >> (2*8));
1156 addr[6] = (bfd_byte) (data >> (1*8));
1157 addr[7] = (bfd_byte) (data >> (0*8));
1164 bfd_putl64 (data, addr)
1165 bfd_vma data ATTRIBUTE_UNUSED;
1166 register bfd_byte *addr ATTRIBUTE_UNUSED;
1169 addr[7] = (bfd_byte) (data >> (7*8));
1170 addr[6] = (bfd_byte) (data >> (6*8));
1171 addr[5] = (bfd_byte) (data >> (5*8));
1172 addr[4] = (bfd_byte) (data >> (4*8));
1173 addr[3] = (bfd_byte) (data >> (3*8));
1174 addr[2] = (bfd_byte) (data >> (2*8));
1175 addr[1] = (bfd_byte) (data >> (1*8));
1176 addr[0] = (bfd_byte) (data >> (0*8));
1183 bfd_put_bits (data, addr, bits, big_p)
1196 for (i = 0; i < bytes; i++)
1198 int index = big_p ? bytes - i - 1 : i;
1200 addr[index] = (bfd_byte) data;
1206 bfd_get_bits (addr, bits, big_p)
1220 for (i = 0; i < bytes; i++)
1222 int index = big_p ? i : bytes - i - 1;
1224 data = (data << 8) | addr[index];
1230 /* Default implementation */
1233 _bfd_generic_get_section_contents (abfd, section, location, offset, count)
1238 bfd_size_type count;
1243 if ((bfd_size_type) (offset + count) > section->_raw_size)
1245 bfd_set_error (bfd_error_invalid_operation);
1249 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1250 || bfd_read (location, (bfd_size_type) 1, count, abfd) != count)
1257 _bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count)
1258 bfd *abfd ATTRIBUTE_UNUSED;
1259 sec_ptr section ATTRIBUTE_UNUSED;
1260 bfd_window *w ATTRIBUTE_UNUSED;
1261 file_ptr offset ATTRIBUTE_UNUSED;
1262 bfd_size_type count ATTRIBUTE_UNUSED;
1267 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents)
1269 /* We don't know what changes the bfd's get_section_contents
1270 method may have to make. So punt trying to map the file
1271 window, and let get_section_contents do its thing. */
1272 /* @@ FIXME : If the internal window has a refcount of 1 and was
1273 allocated with malloc instead of mmap, just reuse it. */
1274 bfd_free_window (w);
1275 w->i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
1278 w->i->data = (PTR) bfd_malloc ((size_t) count);
1279 if (w->i->data == NULL)
1287 w->size = w->i->size = count;
1288 w->data = w->i->data;
1289 return bfd_get_section_contents (abfd, section, w->data, offset, count);
1291 if ((bfd_size_type) (offset+count) > section->_raw_size
1292 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true)
1301 /* This generic function can only be used in implementations where creating
1302 NEW sections is disallowed. It is useful in patching existing sections
1303 in read-write files, though. See other set_section_contents functions
1304 to see why it doesn't work for new sections. */
1306 _bfd_generic_set_section_contents (abfd, section, location, offset, count)
1311 bfd_size_type count;
1316 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
1317 || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
1328 unsigned int bfd_log2(bfd_vma x);
1331 Return the log base 2 of the value supplied, rounded up. E.g., an
1332 @var{x} of 1025 returns 11.
1339 unsigned int result = 0;
1341 while ((x = (x >> 1)) != 0)
1347 bfd_generic_is_local_label_name (abfd, name)
1351 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1353 return (name[0] == locals_prefix);
1356 /* Can be used from / for bfd_merge_private_bfd_data to check that
1357 endianness matches between input and output file. Returns
1358 true for a match, otherwise returns false and emits an error. */
1360 _bfd_generic_verify_endian_match (ibfd, obfd)
1364 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1365 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1366 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1370 if (bfd_big_endian (ibfd))
1371 msg = _("%s: compiled for a big endian system and target is little endian");
1373 msg = _("%s: compiled for a little endian system and target is big endian");
1375 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
1377 bfd_set_error (bfd_error_wrong_format);