1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #ifndef HAVE_GETPAGESIZE
27 #define getpagesize() 2048
30 static int real_read PARAMS ((PTR, size_t, size_t, FILE *));
37 These routines are used within BFD.
38 They are not intended for export, but are documented here for
42 /* 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. */
60 bfd *ignore ATTRIBUTE_UNUSED;
65 /* A routine which is used in target vectors for unsupported
66 operations which return a pointer value. */
70 bfd_nullvoidptr (ignore)
71 bfd *ignore ATTRIBUTE_UNUSED;
73 bfd_set_error (bfd_error_invalid_operation);
80 bfd *ignore ATTRIBUTE_UNUSED;
88 bfd *ignore ATTRIBUTE_UNUSED;
96 bfd *ignore ATTRIBUTE_UNUSED;
101 /* A routine which is used in target vectors for unsupported
102 operations which return -1 on error. */
106 _bfd_n1 (ignore_abfd)
107 bfd *ignore_abfd ATTRIBUTE_UNUSED;
109 bfd_set_error (bfd_error_invalid_operation);
116 bfd *ignore ATTRIBUTE_UNUSED;
122 _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
123 bfd *ignore_core_bfd ATTRIBUTE_UNUSED;
124 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED;
126 bfd_set_error (bfd_error_invalid_operation);
130 /* Routine to handle core_file_failing_command entry point for targets
131 without core file support. */
135 _bfd_nocore_core_file_failing_command (ignore_abfd)
136 bfd *ignore_abfd ATTRIBUTE_UNUSED;
138 bfd_set_error (bfd_error_invalid_operation);
142 /* Routine to handle core_file_failing_signal entry point for targets
143 without core file support. */
147 _bfd_nocore_core_file_failing_signal (ignore_abfd)
148 bfd *ignore_abfd ATTRIBUTE_UNUSED;
150 bfd_set_error (bfd_error_invalid_operation);
156 _bfd_dummy_target (ignore_abfd)
157 bfd *ignore_abfd ATTRIBUTE_UNUSED;
159 bfd_set_error (bfd_error_wrong_format);
163 /* Allocate memory using malloc. */
171 ptr = (PTR) malloc (size);
172 if (ptr == NULL && size != 0)
173 bfd_set_error (bfd_error_no_memory);
177 /* Reallocate memory using realloc. */
180 bfd_realloc (ptr, size)
189 ret = realloc (ptr, size);
192 bfd_set_error (bfd_error_no_memory);
197 /* Allocate memory using malloc and clear it. */
205 ptr = (PTR) malloc (size);
210 bfd_set_error (bfd_error_no_memory);
212 memset (ptr, 0, size);
221 /* Note that archive entries don't have streams; they share their parent's.
222 This allows someone to play with the iostream behind BFD's back.
224 Also, note that the origin pointer points to the beginning of a file's
225 contents (0 for non-archive elements). For archive entries this is the
226 first octet in the file, NOT the beginning of the archive header. */
229 real_read (where, a,b, file)
235 /* FIXME - this looks like an optimization, but it's really to cover
236 up for a feature of some OSs (not solaris - sigh) that
237 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
238 internally and tries to link against them. BFD seems to be smart
239 enough to realize there are no symbol records in the "file" that
240 doesn't exist but attempts to read them anyway. On Solaris,
241 attempting to read zero bytes from a NULL file results in a core
242 dump, but on other platforms it just returns zero bytes read.
243 This makes it to something reasonable. - DJ */
244 if (a == 0 || b == 0)
247 #if defined (__VAX) && defined (VMS)
248 /* Apparently fread on Vax VMS does not keep the record length
250 return read (fileno (file), where, a * b);
252 return fread (where, a, b, file);
256 /* Return value is amount read (FIXME: how are errors and end of file dealt
257 with? We never call bfd_set_error, which is probably a mistake). */
260 bfd_read (ptr, size, nitems, abfd)
263 bfd_size_type nitems;
268 if ((abfd->flags & BFD_IN_MEMORY) != 0)
270 struct bfd_in_memory *bim;
273 bim = (struct bfd_in_memory *) abfd->iostream;
275 if (abfd->where + get > bim->size)
277 if (bim->size < (bfd_size_type) abfd->where)
280 get = bim->size - abfd->where;
281 bfd_set_error (bfd_error_file_truncated);
283 memcpy (ptr, bim->buffer + abfd->where, get);
288 nread = real_read (ptr, 1, (size_t)(size*nitems), bfd_cache_lookup(abfd));
290 abfd->where += nread;
292 /* Set bfd_error if we did not read as much data as we expected.
294 If the read failed due to an error set the bfd_error_system_call,
295 else set bfd_error_file_truncated.
297 A BFD backend may wish to override bfd_error_file_truncated to
298 provide something more useful (eg. no_symbols or wrong_format). */
299 if (nread != (int) (size * nitems))
301 if (ferror (bfd_cache_lookup (abfd)))
302 bfd_set_error (bfd_error_system_call);
304 bfd_set_error (bfd_error_file_truncated);
310 /* The window support stuff should probably be broken out into
312 /* The idea behind the next and refcount fields is that one mapped
313 region can suffice for multiple read-only windows or multiple
314 non-overlapping read-write windows. It's not implemented yet
316 struct _bfd_window_internal {
317 struct _bfd_window_internal *next;
320 int refcount : 31; /* should be enough... */
321 unsigned mapped : 1; /* 1 = mmap, 0 = malloc */
325 bfd_init_window (windowp)
333 /* Currently, if USE_MMAP is undefined, none if the window stuff is
334 used. Okay, so it's mis-named. At least the command-line option
335 "--without-mmap" is more obvious than "--without-windows" or some
339 #undef HAVE_MPROTECT /* code's not tested yet */
341 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
342 #include <sys/mman.h>
349 static int debug_windows;
352 bfd_free_window (windowp)
355 bfd_window_internal *i = windowp->i;
362 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
363 windowp, windowp->data, windowp->size, windowp->i);
364 if (i->refcount != 0)
370 munmap (i->data, i->size);
377 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
384 /* There should be no more references to i at this point. */
388 static int ok_to_map = 1;
391 bfd_get_file_window (abfd, offset, size, windowp, writable)
398 static size_t pagesize;
399 bfd_window_internal *i = windowp->i;
400 size_t size_to_alloc = size;
403 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
404 abfd, (long) offset, (long) size,
405 windowp, windowp->data, (unsigned long) windowp->size,
406 windowp->i, writable);
408 /* Make sure we know the page size, so we can be friendly to mmap. */
410 pagesize = getpagesize ();
416 windowp->i = i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
423 && (i->data == 0 || i->mapped == 1)
424 && (abfd->flags & BFD_IN_MEMORY) == 0)
426 file_ptr file_offset, offset2;
431 /* Find the real file and the real offset into it. */
432 while (abfd->my_archive != NULL)
434 offset += abfd->origin;
435 abfd = abfd->my_archive;
437 f = bfd_cache_lookup (abfd);
440 /* Compute offsets and size for mmap and for the user's data. */
441 offset2 = offset % pagesize;
444 file_offset = offset - offset2;
445 real_size = offset + size - file_offset;
446 real_size = real_size + pagesize - 1;
447 real_size -= real_size % pagesize;
449 /* If we're re-using a memory region, make sure it's big enough. */
450 if (i->data && i->size < size)
452 munmap (i->data, i->size);
455 i->data = mmap (i->data, real_size,
456 writable ? PROT_WRITE | PROT_READ : PROT_READ,
458 ? MAP_FILE | MAP_PRIVATE
459 : MAP_FILE | MAP_SHARED),
461 if (i->data == (PTR) -1)
463 /* An error happened. Report it, or try using malloc, or
465 bfd_set_error (bfd_error_system_call);
469 fprintf (stderr, "\t\tmmap failed!\n");
473 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
474 (long) real_size, i->data, (long) offset2);
476 windowp->data = (PTR) ((bfd_byte *) i->data + offset2);
477 windowp->size = size;
481 else if (debug_windows)
484 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
485 (unsigned long) i->data, (int) i->mapped);
487 fprintf (stderr, _("not mapping: env var not set\n"));
496 size_to_alloc += pagesize - 1;
497 size_to_alloc -= size_to_alloc % pagesize;
501 fprintf (stderr, "\n\t%s(%6ld)",
502 i->data ? "realloc" : " malloc", (long) size_to_alloc);
503 i->data = (PTR) bfd_realloc (i->data, size_to_alloc);
505 fprintf (stderr, "\t-> %p\n", i->data);
509 if (size_to_alloc == 0)
511 bfd_set_error (bfd_error_no_memory);
514 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
516 i->size = bfd_read (i->data, size, 1, abfd);
524 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
526 mprotect (i->data, i->size, PROT_READ);
529 windowp->data = i->data;
530 windowp->size = i->size;
534 #endif /* USE_MMAP */
537 bfd_write (ptr, size, nitems, abfd)
540 bfd_size_type nitems;
545 if ((abfd->flags & BFD_IN_MEMORY) != 0)
547 struct bfd_in_memory *bim = (struct bfd_in_memory *) (abfd->iostream);
549 if (abfd->where + size > bim->size)
551 long newsize, oldsize = (bim->size + 127) & ~127;
552 bim->size = abfd->where + size;
553 /* Round up to cut down on memory fragmentation */
554 newsize = (bim->size + 127) & ~127;
555 if (newsize > oldsize)
557 bim->buffer = bfd_realloc (bim->buffer, newsize);
558 if (bim->buffer == 0)
565 memcpy (bim->buffer + abfd->where, ptr, size);
570 nwrote = fwrite (ptr, 1, (size_t) (size * nitems),
571 bfd_cache_lookup (abfd));
573 abfd->where += nwrote;
574 if ((bfd_size_type) nwrote != size * nitems)
580 bfd_set_error (bfd_error_system_call);
587 bfd_write_bigendian_4byte_int
590 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
593 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
594 endian order regardless of what else is going on. This is useful in
599 bfd_write_bigendian_4byte_int (abfd, i)
604 bfd_putb32(i, buffer);
605 if (bfd_write((PTR)buffer, 4, 1, abfd) != 4)
615 if ((abfd->flags & BFD_IN_MEMORY) != 0)
618 ptr = ftell (bfd_cache_lookup(abfd));
620 if (abfd->my_archive)
630 if ((abfd->flags & BFD_IN_MEMORY) != 0)
632 return fflush (bfd_cache_lookup(abfd));
635 /* Returns 0 for success, negative value for failure (in which case
636 bfd_get_error can retrieve the error code). */
638 bfd_stat (abfd, statbuf)
640 struct stat *statbuf;
645 if ((abfd->flags & BFD_IN_MEMORY) != 0)
648 f = bfd_cache_lookup (abfd);
651 bfd_set_error (bfd_error_system_call);
654 result = fstat (fileno (f), statbuf);
656 bfd_set_error (bfd_error_system_call);
660 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
661 can retrieve the error code). */
664 bfd_seek (abfd, position, direction)
671 file_ptr file_position;
672 /* For the time being, a BFD may not seek to it's end. The problem
673 is that we don't easily have a way to recognize the end of an
674 element in an archive. */
676 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
678 if (direction == SEEK_CUR && position == 0)
681 if ((abfd->flags & BFD_IN_MEMORY) != 0)
683 struct bfd_in_memory *bim;
685 bim = (struct bfd_in_memory *) abfd->iostream;
687 if (direction == SEEK_SET)
688 abfd->where = position;
690 abfd->where += position;
692 if ((bfd_size_type) abfd->where > bim->size)
694 abfd->where = bim->size;
695 bfd_set_error (bfd_error_file_truncated);
702 if (abfd->format != bfd_archive && abfd->my_archive == 0)
705 /* Explanation for this code: I'm only about 95+% sure that the above
706 conditions are sufficient and that all i/o calls are properly
707 adjusting the `where' field. So this is sort of an `assert'
708 that the `where' field is correct. If we can go a while without
709 tripping the abort, we can probably safely disable this code,
710 so that the real optimizations happen. */
711 file_ptr where_am_i_now;
712 where_am_i_now = ftell (bfd_cache_lookup (abfd));
713 if (abfd->my_archive)
714 where_am_i_now -= abfd->origin;
715 if (where_am_i_now != abfd->where)
718 if (direction == SEEK_SET && position == abfd->where)
723 /* We need something smarter to optimize access to archives.
724 Currently, anything inside an archive is read via the file
725 handle for the archive. Which means that a bfd_seek on one
726 component affects the `current position' in the archive, as
727 well as in any other component.
729 It might be sufficient to put a spike through the cache
730 abstraction, and look to the archive for the file position,
731 but I think we should try for something cleaner.
733 In the meantime, no optimization for archives. */
736 f = bfd_cache_lookup (abfd);
737 file_position = position;
738 if (direction == SEEK_SET && abfd->my_archive != NULL)
739 file_position += abfd->origin;
741 result = fseek (f, file_position, direction);
745 int hold_errno = errno;
747 /* Force redetermination of `where' field. */
750 /* An EINVAL error probably means that the file offset was
752 if (hold_errno == EINVAL)
753 bfd_set_error (bfd_error_file_truncated);
756 bfd_set_error (bfd_error_system_call);
762 /* Adjust `where' field. */
763 if (direction == SEEK_SET)
764 abfd->where = position;
766 abfd->where += position;
771 /** The do-it-yourself (byte) sex-change kit */
773 /* The middle letter e.g. get<b>short indicates Big or Little endian
774 target machine. It doesn't matter what the byte order of the host
775 machine is; these routines work for either. */
777 /* FIXME: Should these take a count argument?
778 Answer (gnu@cygnus.com): No, but perhaps they should be inline
779 functions in swap.h #ifdef __GNUC__.
780 Gprof them later and find out. */
789 These macros as used for reading and writing raw data in
790 sections; each access (except for bytes) is vectored through
791 the target format of the BFD and mangled accordingly. The
792 mangling performs any necessary endian translations and
793 removes alignment restrictions. Note that types accepted and
794 returned by these macros are identical so they can be swapped
795 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
796 to either <<bfd_get_32>> or <<bfd_get_64>>.
798 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
799 system without prototypes, the caller is responsible for making
800 sure that is true, with a cast if necessary. We don't cast
801 them in the macro definitions because that would prevent <<lint>>
802 or <<gcc -Wall>> from detecting sins such as passing a pointer.
803 To detect calling these with less than a <<bfd_vma>>, use
804 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
807 .{* Byte swapping macros for user section data. *}
809 .#define bfd_put_8(abfd, val, ptr) \
810 . ((void) (*((unsigned char *)(ptr)) = (unsigned char)(val)))
811 .#define bfd_put_signed_8 \
813 .#define bfd_get_8(abfd, ptr) \
814 . (*(unsigned char *)(ptr))
815 .#define bfd_get_signed_8(abfd, ptr) \
816 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
818 .#define bfd_put_16(abfd, val, ptr) \
819 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
820 .#define bfd_put_signed_16 \
822 .#define bfd_get_16(abfd, ptr) \
823 . BFD_SEND(abfd, bfd_getx16, (ptr))
824 .#define bfd_get_signed_16(abfd, ptr) \
825 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
827 .#define bfd_put_32(abfd, val, ptr) \
828 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
829 .#define bfd_put_signed_32 \
831 .#define bfd_get_32(abfd, ptr) \
832 . BFD_SEND(abfd, bfd_getx32, (ptr))
833 .#define bfd_get_signed_32(abfd, ptr) \
834 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
836 .#define bfd_put_64(abfd, val, ptr) \
837 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
838 .#define bfd_put_signed_64 \
840 .#define bfd_get_64(abfd, ptr) \
841 . BFD_SEND(abfd, bfd_getx64, (ptr))
842 .#define bfd_get_signed_64(abfd, ptr) \
843 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
845 .#define bfd_get(bits, abfd, ptr) \
846 . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \
847 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
848 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
849 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
850 . : (abort (), (bfd_vma) - 1))
852 .#define bfd_put(bits, abfd, val, ptr) \
853 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
854 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
855 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
856 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
857 . : (abort (), (void) 0))
867 These macros have the same function as their <<bfd_get_x>>
868 bretheren, except that they are used for removing information
869 for the header records of object files. Believe it or not,
870 some object files keep their header records in big endian
871 order and their data in little endian order.
873 .{* Byte swapping macros for file header data. *}
875 .#define bfd_h_put_8(abfd, val, ptr) \
876 . bfd_put_8 (abfd, val, ptr)
877 .#define bfd_h_put_signed_8(abfd, val, ptr) \
878 . bfd_put_8 (abfd, val, ptr)
879 .#define bfd_h_get_8(abfd, ptr) \
880 . bfd_get_8 (abfd, ptr)
881 .#define bfd_h_get_signed_8(abfd, ptr) \
882 . bfd_get_signed_8 (abfd, ptr)
884 .#define bfd_h_put_16(abfd, val, ptr) \
885 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
886 .#define bfd_h_put_signed_16 \
888 .#define bfd_h_get_16(abfd, ptr) \
889 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
890 .#define bfd_h_get_signed_16(abfd, ptr) \
891 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
893 .#define bfd_h_put_32(abfd, val, ptr) \
894 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
895 .#define bfd_h_put_signed_32 \
897 .#define bfd_h_get_32(abfd, ptr) \
898 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
899 .#define bfd_h_get_signed_32(abfd, ptr) \
900 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
902 .#define bfd_h_put_64(abfd, val, ptr) \
903 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
904 .#define bfd_h_put_signed_64 \
906 .#define bfd_h_get_64(abfd, ptr) \
907 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
908 .#define bfd_h_get_signed_64(abfd, ptr) \
909 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
913 /* Sign extension to bfd_signed_vma. */
914 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
915 #define COERCE32(x) \
916 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000))
917 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
918 #define COERCE64(x) \
919 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
923 register const bfd_byte *addr;
925 return (addr[0] << 8) | addr[1];
930 register const bfd_byte *addr;
932 return (addr[1] << 8) | addr[0];
936 bfd_getb_signed_16 (addr)
937 register const bfd_byte *addr;
939 return COERCE16((addr[0] << 8) | addr[1]);
943 bfd_getl_signed_16 (addr)
944 register const bfd_byte *addr;
946 return COERCE16((addr[1] << 8) | addr[0]);
950 bfd_putb16 (data, addr)
952 register bfd_byte *addr;
954 addr[0] = (bfd_byte)(data >> 8);
955 addr[1] = (bfd_byte )data;
959 bfd_putl16 (data, addr)
961 register bfd_byte *addr;
963 addr[0] = (bfd_byte )data;
964 addr[1] = (bfd_byte)(data >> 8);
969 register const bfd_byte *addr;
973 v = (unsigned long) addr[0] << 24;
974 v |= (unsigned long) addr[1] << 16;
975 v |= (unsigned long) addr[2] << 8;
976 v |= (unsigned long) addr[3];
982 register const bfd_byte *addr;
986 v = (unsigned long) addr[0];
987 v |= (unsigned long) addr[1] << 8;
988 v |= (unsigned long) addr[2] << 16;
989 v |= (unsigned long) addr[3] << 24;
994 bfd_getb_signed_32 (addr)
995 register const bfd_byte *addr;
999 v = (unsigned long) addr[0] << 24;
1000 v |= (unsigned long) addr[1] << 16;
1001 v |= (unsigned long) addr[2] << 8;
1002 v |= (unsigned long) addr[3];
1003 return COERCE32 (v);
1007 bfd_getl_signed_32 (addr)
1008 register const bfd_byte *addr;
1012 v = (unsigned long) addr[0];
1013 v |= (unsigned long) addr[1] << 8;
1014 v |= (unsigned long) addr[2] << 16;
1015 v |= (unsigned long) addr[3] << 24;
1016 return COERCE32 (v);
1021 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1026 high= ((((((((addr[0]) << 8) |
1031 low = (((((((((bfd_vma)addr[4]) << 8) |
1036 return high << 32 | low;
1045 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1049 high= (((((((addr[7] << 8) |
1054 low = ((((((((bfd_vma)addr[3] << 8) |
1059 return high << 32 | low;
1068 bfd_getb_signed_64 (addr)
1069 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1074 high= ((((((((addr[0]) << 8) |
1079 low = (((((((((bfd_vma)addr[4]) << 8) |
1084 return COERCE64(high << 32 | low);
1092 bfd_getl_signed_64 (addr)
1093 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1097 high= (((((((addr[7] << 8) |
1102 low = ((((((((bfd_vma)addr[3] << 8) |
1107 return COERCE64(high << 32 | low);
1115 bfd_putb32 (data, addr)
1117 register bfd_byte *addr;
1119 addr[0] = (bfd_byte)(data >> 24);
1120 addr[1] = (bfd_byte)(data >> 16);
1121 addr[2] = (bfd_byte)(data >> 8);
1122 addr[3] = (bfd_byte)data;
1126 bfd_putl32 (data, addr)
1128 register bfd_byte *addr;
1130 addr[0] = (bfd_byte)data;
1131 addr[1] = (bfd_byte)(data >> 8);
1132 addr[2] = (bfd_byte)(data >> 16);
1133 addr[3] = (bfd_byte)(data >> 24);
1137 bfd_putb64 (data, addr)
1138 bfd_vma data ATTRIBUTE_UNUSED;
1139 register bfd_byte *addr ATTRIBUTE_UNUSED;
1142 addr[0] = (bfd_byte)(data >> (7*8));
1143 addr[1] = (bfd_byte)(data >> (6*8));
1144 addr[2] = (bfd_byte)(data >> (5*8));
1145 addr[3] = (bfd_byte)(data >> (4*8));
1146 addr[4] = (bfd_byte)(data >> (3*8));
1147 addr[5] = (bfd_byte)(data >> (2*8));
1148 addr[6] = (bfd_byte)(data >> (1*8));
1149 addr[7] = (bfd_byte)(data >> (0*8));
1156 bfd_putl64 (data, addr)
1157 bfd_vma data ATTRIBUTE_UNUSED;
1158 register bfd_byte *addr ATTRIBUTE_UNUSED;
1161 addr[7] = (bfd_byte)(data >> (7*8));
1162 addr[6] = (bfd_byte)(data >> (6*8));
1163 addr[5] = (bfd_byte)(data >> (5*8));
1164 addr[4] = (bfd_byte)(data >> (4*8));
1165 addr[3] = (bfd_byte)(data >> (3*8));
1166 addr[2] = (bfd_byte)(data >> (2*8));
1167 addr[1] = (bfd_byte)(data >> (1*8));
1168 addr[0] = (bfd_byte)(data >> (0*8));
1174 /* Default implementation */
1177 _bfd_generic_get_section_contents (abfd, section, location, offset, count)
1182 bfd_size_type count;
1187 if ((bfd_size_type) (offset + count) > section->_raw_size)
1189 bfd_set_error (bfd_error_invalid_operation);
1193 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1194 || bfd_read (location, (bfd_size_type) 1, count, abfd) != count)
1201 _bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count)
1202 bfd *abfd ATTRIBUTE_UNUSED;
1203 sec_ptr section ATTRIBUTE_UNUSED;
1204 bfd_window *w ATTRIBUTE_UNUSED;
1205 file_ptr offset ATTRIBUTE_UNUSED;
1206 bfd_size_type count ATTRIBUTE_UNUSED;
1211 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents)
1213 /* We don't know what changes the bfd's get_section_contents
1214 method may have to make. So punt trying to map the file
1215 window, and let get_section_contents do its thing. */
1216 /* @@ FIXME : If the internal window has a refcount of 1 and was
1217 allocated with malloc instead of mmap, just reuse it. */
1218 bfd_free_window (w);
1219 w->i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
1222 w->i->data = (PTR) bfd_malloc ((size_t) count);
1223 if (w->i->data == NULL)
1231 w->size = w->i->size = count;
1232 w->data = w->i->data;
1233 return bfd_get_section_contents (abfd, section, w->data, offset, count);
1235 if ((bfd_size_type) (offset+count) > section->_raw_size
1236 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true)
1245 /* This generic function can only be used in implementations where creating
1246 NEW sections is disallowed. It is useful in patching existing sections
1247 in read-write files, though. See other set_section_contents functions
1248 to see why it doesn't work for new sections. */
1250 _bfd_generic_set_section_contents (abfd, section, location, offset, count)
1255 bfd_size_type count;
1260 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
1261 || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
1272 unsigned int bfd_log2(bfd_vma x);
1275 Return the log base 2 of the value supplied, rounded up. E.g., an
1276 @var{x} of 1025 returns 11.
1283 unsigned int result = 0;
1285 while ((x = (x >> 1)) != 0)
1291 bfd_generic_is_local_label_name (abfd, name)
1295 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1297 return (name[0] == locals_prefix);
1300 /* Can be used from / for bfd_merge_private_bfd_data to check that
1301 endianness matches between input and output file. Returns
1302 true for a match, otherwise returns false and emits an error. */
1304 _bfd_generic_verify_endian_match (ibfd, obfd)
1308 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1309 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1311 (*_bfd_error_handler)
1312 ("%s: compiled for a %s endian system and target is %s endian",
1313 bfd_get_filename (ibfd),
1314 bfd_big_endian (ibfd) ? "big" : "little",
1315 bfd_big_endian (obfd) ? "big" : "little");
1317 bfd_set_error (bfd_error_wrong_format);