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 size_t 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 if (size != (size_t) size)
162 bfd_set_error (bfd_error_no_memory);
166 ptr = (PTR) malloc ((size_t) size);
167 if (ptr == NULL && (size_t) size != 0)
168 bfd_set_error (bfd_error_no_memory);
173 /* Reallocate memory using realloc. */
176 bfd_realloc (ptr, size)
182 if (size != (size_t) size)
184 bfd_set_error (bfd_error_no_memory);
189 ret = malloc ((size_t) size);
191 ret = realloc (ptr, (size_t) size);
193 if (ret == NULL && (size_t) size != 0)
194 bfd_set_error (bfd_error_no_memory);
199 /* Allocate memory using malloc and clear it. */
207 if (size != (size_t) size)
209 bfd_set_error (bfd_error_no_memory);
213 ptr = (PTR) malloc ((size_t) size);
215 if ((size_t) size != 0)
218 bfd_set_error (bfd_error_no_memory);
220 memset (ptr, 0, (size_t) size);
228 /* Note that archive entries don't have streams; they share their parent's.
229 This allows someone to play with the iostream behind BFD's back.
231 Also, note that the origin pointer points to the beginning of a file's
232 contents (0 for non-archive elements). For archive entries this is the
233 first octet in the file, NOT the beginning of the archive header. */
236 real_read (where, a, b, file)
242 /* FIXME - this looks like an optimization, but it's really to cover
243 up for a feature of some OSs (not solaris - sigh) that
244 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
245 internally and tries to link against them. BFD seems to be smart
246 enough to realize there are no symbol records in the "file" that
247 doesn't exist but attempts to read them anyway. On Solaris,
248 attempting to read zero bytes from a NULL file results in a core
249 dump, but on other platforms it just returns zero bytes read.
250 This makes it to something reasonable. - DJ */
251 if (a == 0 || b == 0)
255 #if defined (__VAX) && defined (VMS)
256 /* Apparently fread on Vax VMS does not keep the record length
258 return read (fileno (file), where, a * b);
260 return fread (where, a, b, file);
264 /* Return value is amount read. */
267 bfd_bread (ptr, size, abfd)
274 if ((abfd->flags & BFD_IN_MEMORY) != 0)
276 struct bfd_in_memory *bim;
279 bim = (struct bfd_in_memory *) abfd->iostream;
281 if (abfd->where + get > bim->size)
283 if (bim->size < (bfd_size_type) abfd->where)
286 get = bim->size - abfd->where;
287 bfd_set_error (bfd_error_file_truncated);
289 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
294 nread = real_read (ptr, 1, (size_t) size, bfd_cache_lookup (abfd));
295 if (nread != (size_t) -1)
296 abfd->where += nread;
298 /* Set bfd_error if we did not read as much data as we expected.
300 If the read failed due to an error set the bfd_error_system_call,
301 else set bfd_error_file_truncated.
303 A BFD backend may wish to override bfd_error_file_truncated to
304 provide something more useful (eg. no_symbols or wrong_format). */
307 if (ferror (bfd_cache_lookup (abfd)))
308 bfd_set_error (bfd_error_system_call);
310 bfd_set_error (bfd_error_file_truncated);
316 /* The window support stuff should probably be broken out into
318 /* The idea behind the next and refcount fields is that one mapped
319 region can suffice for multiple read-only windows or multiple
320 non-overlapping read-write windows. It's not implemented yet
322 struct _bfd_window_internal {
323 struct _bfd_window_internal *next;
326 int refcount : 31; /* should be enough... */
327 unsigned mapped : 1; /* 1 = mmap, 0 = malloc */
331 bfd_init_window (windowp)
339 /* Currently, if USE_MMAP is undefined, none if the window stuff is
340 used. Okay, so it's mis-named. At least the command-line option
341 "--without-mmap" is more obvious than "--without-windows" or some
345 #undef HAVE_MPROTECT /* code's not tested yet */
347 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
348 #include <sys/mman.h>
355 static int debug_windows;
358 bfd_free_window (windowp)
361 bfd_window_internal *i = windowp->i;
368 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
369 windowp, windowp->data, windowp->size, windowp->i);
370 if (i->refcount != 0)
376 munmap (i->data, i->size);
383 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
390 /* There should be no more references to i at this point. */
394 static int ok_to_map = 1;
397 bfd_get_file_window (abfd, offset, size, windowp, writable)
404 static size_t pagesize;
405 bfd_window_internal *i = windowp->i;
406 bfd_size_type size_to_alloc = size;
409 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
410 abfd, (long) offset, (long) size,
411 windowp, windowp->data, (unsigned long) windowp->size,
412 windowp->i, writable);
414 /* Make sure we know the page size, so we can be friendly to mmap. */
416 pagesize = getpagesize ();
422 i = ((bfd_window_internal *)
423 bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal)));
431 && (i->data == 0 || i->mapped == 1)
432 && (abfd->flags & BFD_IN_MEMORY) == 0)
434 file_ptr file_offset, offset2;
439 /* Find the real file and the real offset into it. */
440 while (abfd->my_archive != NULL)
442 offset += abfd->origin;
443 abfd = abfd->my_archive;
445 f = bfd_cache_lookup (abfd);
448 /* Compute offsets and size for mmap and for the user's data. */
449 offset2 = offset % pagesize;
452 file_offset = offset - offset2;
453 real_size = offset + size - file_offset;
454 real_size = real_size + pagesize - 1;
455 real_size -= real_size % pagesize;
457 /* If we're re-using a memory region, make sure it's big enough. */
458 if (i->data && i->size < size)
460 munmap (i->data, i->size);
463 i->data = mmap (i->data, real_size,
464 writable ? PROT_WRITE | PROT_READ : PROT_READ,
466 ? MAP_FILE | MAP_PRIVATE
467 : MAP_FILE | MAP_SHARED),
469 if (i->data == (PTR) -1)
471 /* An error happened. Report it, or try using malloc, or
473 bfd_set_error (bfd_error_system_call);
477 fprintf (stderr, "\t\tmmap failed!\n");
481 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
482 (long) real_size, i->data, (long) offset2);
484 windowp->data = (PTR) ((bfd_byte *) i->data + offset2);
485 windowp->size = size;
489 else if (debug_windows)
492 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
493 (unsigned long) i->data, (int) i->mapped);
495 fprintf (stderr, _("not mapping: env var not set\n"));
504 size_to_alloc += pagesize - 1;
505 size_to_alloc -= size_to_alloc % pagesize;
509 fprintf (stderr, "\n\t%s(%6ld)",
510 i->data ? "realloc" : " malloc", (long) size_to_alloc);
511 i->data = (PTR) bfd_realloc (i->data, size_to_alloc);
513 fprintf (stderr, "\t-> %p\n", i->data);
517 if (size_to_alloc == 0)
521 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
523 i->size = bfd_bread (i->data, size, abfd);
531 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
533 mprotect (i->data, i->size, PROT_READ);
536 windowp->data = i->data;
537 windowp->size = i->size;
541 #endif /* USE_MMAP */
544 bfd_bwrite (ptr, size, abfd)
551 if ((abfd->flags & BFD_IN_MEMORY) != 0)
553 struct bfd_in_memory *bim = (struct bfd_in_memory *) (abfd->iostream);
554 size = (size_t) size;
555 if (abfd->where + size > bim->size)
557 bfd_size_type newsize, oldsize;
559 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
560 bim->size = abfd->where + size;
561 /* Round up to cut down on memory fragmentation */
562 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
563 if (newsize > oldsize)
565 bim->buffer = bfd_realloc (bim->buffer, newsize);
566 if (bim->buffer == 0)
573 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
578 nwrote = fwrite (ptr, 1, (size_t) size, bfd_cache_lookup (abfd));
579 if (nwrote != (size_t) -1)
580 abfd->where += nwrote;
587 bfd_set_error (bfd_error_system_call);
594 bfd_write_bigendian_4byte_int
597 void bfd_write_bigendian_4byte_int (bfd *, unsigned int);
600 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
601 endian order regardless of what else is going on. This is useful in
606 bfd_write_bigendian_4byte_int (abfd, i)
611 bfd_putb32 ((bfd_vma) i, buffer);
612 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) 4, abfd) != 4)
622 if ((abfd->flags & BFD_IN_MEMORY) != 0)
625 ptr = ftell (bfd_cache_lookup (abfd));
627 if (abfd->my_archive)
637 if ((abfd->flags & BFD_IN_MEMORY) != 0)
639 return fflush (bfd_cache_lookup(abfd));
642 /* Returns 0 for success, negative value for failure (in which case
643 bfd_get_error can retrieve the error code). */
645 bfd_stat (abfd, statbuf)
647 struct stat *statbuf;
652 if ((abfd->flags & BFD_IN_MEMORY) != 0)
655 f = bfd_cache_lookup (abfd);
658 bfd_set_error (bfd_error_system_call);
661 result = fstat (fileno (f), statbuf);
663 bfd_set_error (bfd_error_system_call);
667 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
668 can retrieve the error code). */
671 bfd_seek (abfd, position, direction)
679 /* For the time being, a BFD may not seek to it's end. The problem
680 is that we don't easily have a way to recognize the end of an
681 element in an archive. */
683 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
685 if (direction == SEEK_CUR && position == 0)
688 if ((abfd->flags & BFD_IN_MEMORY) != 0)
690 struct bfd_in_memory *bim;
692 bim = (struct bfd_in_memory *) abfd->iostream;
694 if (direction == SEEK_SET)
695 abfd->where = position;
697 abfd->where += position;
699 if (abfd->where > bim->size)
701 if ((abfd->direction == write_direction) ||
702 (abfd->direction == both_direction))
704 bfd_size_type newsize, oldsize;
705 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
706 bim->size = abfd->where;
707 /* Round up to cut down on memory fragmentation */
708 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
709 if (newsize > oldsize)
711 bim->buffer = bfd_realloc (bim->buffer, newsize);
712 if (bim->buffer == 0)
721 abfd->where = bim->size;
722 bfd_set_error (bfd_error_file_truncated);
729 if (abfd->format != bfd_archive && abfd->my_archive == 0)
732 /* Explanation for this code: I'm only about 95+% sure that the above
733 conditions are sufficient and that all i/o calls are properly
734 adjusting the `where' field. So this is sort of an `assert'
735 that the `where' field is correct. If we can go a while without
736 tripping the abort, we can probably safely disable this code,
737 so that the real optimizations happen. */
738 file_ptr where_am_i_now;
739 where_am_i_now = ftell (bfd_cache_lookup (abfd));
740 if (abfd->my_archive)
741 where_am_i_now -= abfd->origin;
742 if (where_am_i_now != abfd->where)
745 if (direction == SEEK_SET && (bfd_vma) position == abfd->where)
750 /* We need something smarter to optimize access to archives.
751 Currently, anything inside an archive is read via the file
752 handle for the archive. Which means that a bfd_seek on one
753 component affects the `current position' in the archive, as
754 well as in any other component.
756 It might be sufficient to put a spike through the cache
757 abstraction, and look to the archive for the file position,
758 but I think we should try for something cleaner.
760 In the meantime, no optimization for archives. */
763 f = bfd_cache_lookup (abfd);
764 file_position = position;
765 if (direction == SEEK_SET && abfd->my_archive != NULL)
766 file_position += abfd->origin;
768 result = fseek (f, file_position, direction);
771 int hold_errno = errno;
773 /* Force redetermination of `where' field. */
776 /* An EINVAL error probably means that the file offset was
778 if (hold_errno == EINVAL)
779 bfd_set_error (bfd_error_file_truncated);
782 bfd_set_error (bfd_error_system_call);
788 /* Adjust `where' field. */
789 if (direction == SEEK_SET)
790 abfd->where = position;
792 abfd->where += position;
797 /** The do-it-yourself (byte) sex-change kit */
799 /* The middle letter e.g. get<b>short indicates Big or Little endian
800 target machine. It doesn't matter what the byte order of the host
801 machine is; these routines work for either. */
803 /* FIXME: Should these take a count argument?
804 Answer (gnu@cygnus.com): No, but perhaps they should be inline
805 functions in swap.h #ifdef __GNUC__.
806 Gprof them later and find out. */
815 These macros as used for reading and writing raw data in
816 sections; each access (except for bytes) is vectored through
817 the target format of the BFD and mangled accordingly. The
818 mangling performs any necessary endian translations and
819 removes alignment restrictions. Note that types accepted and
820 returned by these macros are identical so they can be swapped
821 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
822 to either <<bfd_get_32>> or <<bfd_get_64>>.
824 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
825 system without prototypes, the caller is responsible for making
826 sure that is true, with a cast if necessary. We don't cast
827 them in the macro definitions because that would prevent <<lint>>
828 or <<gcc -Wall>> from detecting sins such as passing a pointer.
829 To detect calling these with less than a <<bfd_vma>>, use
830 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
833 .{* Byte swapping macros for user section data. *}
835 .#define bfd_put_8(abfd, val, ptr) \
836 . ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val)))
837 .#define bfd_put_signed_8 \
839 .#define bfd_get_8(abfd, ptr) \
840 . (*(unsigned char *) (ptr) & 0xff)
841 .#define bfd_get_signed_8(abfd, ptr) \
842 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
844 .#define bfd_put_16(abfd, val, ptr) \
845 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
846 .#define bfd_put_signed_16 \
848 .#define bfd_get_16(abfd, ptr) \
849 . BFD_SEND(abfd, bfd_getx16, (ptr))
850 .#define bfd_get_signed_16(abfd, ptr) \
851 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
853 .#define bfd_put_32(abfd, val, ptr) \
854 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
855 .#define bfd_put_signed_32 \
857 .#define bfd_get_32(abfd, ptr) \
858 . BFD_SEND(abfd, bfd_getx32, (ptr))
859 .#define bfd_get_signed_32(abfd, ptr) \
860 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
862 .#define bfd_put_64(abfd, val, ptr) \
863 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
864 .#define bfd_put_signed_64 \
866 .#define bfd_get_64(abfd, ptr) \
867 . BFD_SEND(abfd, bfd_getx64, (ptr))
868 .#define bfd_get_signed_64(abfd, ptr) \
869 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
871 .#define bfd_get(bits, abfd, ptr) \
872 . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \
873 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
874 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
875 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
876 . : (abort (), (bfd_vma) - 1))
878 .#define bfd_put(bits, abfd, val, ptr) \
879 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
880 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
881 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
882 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
883 . : (abort (), (void) 0))
893 These macros have the same function as their <<bfd_get_x>>
894 brethren, except that they are used for removing information
895 for the header records of object files. Believe it or not,
896 some object files keep their header records in big endian
897 order and their data in little endian order.
899 .{* Byte swapping macros for file header data. *}
901 .#define bfd_h_put_8(abfd, val, ptr) \
902 . bfd_put_8 (abfd, val, ptr)
903 .#define bfd_h_put_signed_8(abfd, val, ptr) \
904 . bfd_put_8 (abfd, val, ptr)
905 .#define bfd_h_get_8(abfd, ptr) \
906 . bfd_get_8 (abfd, ptr)
907 .#define bfd_h_get_signed_8(abfd, ptr) \
908 . bfd_get_signed_8 (abfd, ptr)
910 .#define bfd_h_put_16(abfd, val, ptr) \
911 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
912 .#define bfd_h_put_signed_16 \
914 .#define bfd_h_get_16(abfd, ptr) \
915 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
916 .#define bfd_h_get_signed_16(abfd, ptr) \
917 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
919 .#define bfd_h_put_32(abfd, val, ptr) \
920 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
921 .#define bfd_h_put_signed_32 \
923 .#define bfd_h_get_32(abfd, ptr) \
924 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
925 .#define bfd_h_get_signed_32(abfd, ptr) \
926 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
928 .#define bfd_h_put_64(abfd, val, ptr) \
929 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
930 .#define bfd_h_put_signed_64 \
932 .#define bfd_h_get_64(abfd, ptr) \
933 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
934 .#define bfd_h_get_signed_64(abfd, ptr) \
935 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
937 .{* Refinements on the above, which should eventually go away. Save
938 . cluttering the source with (bfd_vma) and (bfd_byte *) casts. *}
940 .#define H_PUT_64(abfd, val, where) \
941 . bfd_h_put_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
943 .#define H_PUT_32(abfd, val, where) \
944 . bfd_h_put_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
946 .#define H_PUT_16(abfd, val, where) \
947 . bfd_h_put_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
949 .#define H_PUT_8 bfd_h_put_8
951 .#define H_PUT_S64(abfd, val, where) \
952 . bfd_h_put_signed_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
954 .#define H_PUT_S32(abfd, val, where) \
955 . bfd_h_put_signed_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
957 .#define H_PUT_S16(abfd, val, where) \
958 . bfd_h_put_signed_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
960 .#define H_PUT_S8 bfd_h_put_signed_8
962 .#define H_GET_64(abfd, where) \
963 . bfd_h_get_64 ((abfd), (bfd_byte *) (where))
965 .#define H_GET_32(abfd, where) \
966 . bfd_h_get_32 ((abfd), (bfd_byte *) (where))
968 .#define H_GET_16(abfd, where) \
969 . bfd_h_get_16 ((abfd), (bfd_byte *) (where))
971 .#define H_GET_8 bfd_h_get_8
973 .#define H_GET_S64(abfd, where) \
974 . bfd_h_get_signed_64 ((abfd), (bfd_byte *) (where))
976 .#define H_GET_S32(abfd, where) \
977 . bfd_h_get_signed_32 ((abfd), (bfd_byte *) (where))
979 .#define H_GET_S16(abfd, where) \
980 . bfd_h_get_signed_16 ((abfd), (bfd_byte *) (where))
982 .#define H_GET_S8 bfd_h_get_signed_8
986 /* Sign extension to bfd_signed_vma. */
987 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
988 #define COERCE32(x) \
989 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000))
990 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
991 #define COERCE64(x) \
992 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
996 register const bfd_byte *addr;
998 return (addr[0] << 8) | addr[1];
1003 register const bfd_byte *addr;
1005 return (addr[1] << 8) | addr[0];
1009 bfd_getb_signed_16 (addr)
1010 register const bfd_byte *addr;
1012 return COERCE16((addr[0] << 8) | addr[1]);
1016 bfd_getl_signed_16 (addr)
1017 register const bfd_byte *addr;
1019 return COERCE16((addr[1] << 8) | addr[0]);
1023 bfd_putb16 (data, addr)
1025 register bfd_byte *addr;
1027 addr[0] = (bfd_byte) (data >> 8);
1028 addr[1] = (bfd_byte) data;
1032 bfd_putl16 (data, addr)
1034 register bfd_byte *addr;
1036 addr[0] = (bfd_byte) data;
1037 addr[1] = (bfd_byte) (data >> 8);
1042 register const bfd_byte *addr;
1046 v = (unsigned long) addr[0] << 24;
1047 v |= (unsigned long) addr[1] << 16;
1048 v |= (unsigned long) addr[2] << 8;
1049 v |= (unsigned long) addr[3];
1055 register const bfd_byte *addr;
1059 v = (unsigned long) addr[0];
1060 v |= (unsigned long) addr[1] << 8;
1061 v |= (unsigned long) addr[2] << 16;
1062 v |= (unsigned long) addr[3] << 24;
1067 bfd_getb_signed_32 (addr)
1068 register const bfd_byte *addr;
1072 v = (unsigned long) addr[0] << 24;
1073 v |= (unsigned long) addr[1] << 16;
1074 v |= (unsigned long) addr[2] << 8;
1075 v |= (unsigned long) addr[3];
1076 return COERCE32 (v);
1080 bfd_getl_signed_32 (addr)
1081 register const bfd_byte *addr;
1085 v = (unsigned long) addr[0];
1086 v |= (unsigned long) addr[1] << 8;
1087 v |= (unsigned long) addr[2] << 16;
1088 v |= (unsigned long) addr[3] << 24;
1089 return COERCE32 (v);
1094 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1099 high= ((((((((addr[0]) << 8) |
1104 low = (((((((((bfd_vma)addr[4]) << 8) |
1109 return high << 32 | low;
1118 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1122 high= (((((((addr[7] << 8) |
1127 low = ((((((((bfd_vma)addr[3] << 8) |
1132 return high << 32 | low;
1141 bfd_getb_signed_64 (addr)
1142 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1147 high= ((((((((addr[0]) << 8) |
1152 low = (((((((((bfd_vma)addr[4]) << 8) |
1157 return COERCE64(high << 32 | low);
1165 bfd_getl_signed_64 (addr)
1166 register const bfd_byte *addr ATTRIBUTE_UNUSED;
1170 high= (((((((addr[7] << 8) |
1175 low = ((((((((bfd_vma)addr[3] << 8) |
1180 return COERCE64(high << 32 | low);
1188 bfd_putb32 (data, addr)
1190 register bfd_byte *addr;
1192 addr[0] = (bfd_byte) (data >> 24);
1193 addr[1] = (bfd_byte) (data >> 16);
1194 addr[2] = (bfd_byte) (data >> 8);
1195 addr[3] = (bfd_byte) data;
1199 bfd_putl32 (data, addr)
1201 register bfd_byte *addr;
1203 addr[0] = (bfd_byte) data;
1204 addr[1] = (bfd_byte) (data >> 8);
1205 addr[2] = (bfd_byte) (data >> 16);
1206 addr[3] = (bfd_byte) (data >> 24);
1210 bfd_putb64 (data, addr)
1211 bfd_vma data ATTRIBUTE_UNUSED;
1212 register bfd_byte *addr ATTRIBUTE_UNUSED;
1215 addr[0] = (bfd_byte) (data >> (7*8));
1216 addr[1] = (bfd_byte) (data >> (6*8));
1217 addr[2] = (bfd_byte) (data >> (5*8));
1218 addr[3] = (bfd_byte) (data >> (4*8));
1219 addr[4] = (bfd_byte) (data >> (3*8));
1220 addr[5] = (bfd_byte) (data >> (2*8));
1221 addr[6] = (bfd_byte) (data >> (1*8));
1222 addr[7] = (bfd_byte) (data >> (0*8));
1229 bfd_putl64 (data, addr)
1230 bfd_vma data ATTRIBUTE_UNUSED;
1231 register bfd_byte *addr ATTRIBUTE_UNUSED;
1234 addr[7] = (bfd_byte) (data >> (7*8));
1235 addr[6] = (bfd_byte) (data >> (6*8));
1236 addr[5] = (bfd_byte) (data >> (5*8));
1237 addr[4] = (bfd_byte) (data >> (4*8));
1238 addr[3] = (bfd_byte) (data >> (3*8));
1239 addr[2] = (bfd_byte) (data >> (2*8));
1240 addr[1] = (bfd_byte) (data >> (1*8));
1241 addr[0] = (bfd_byte) (data >> (0*8));
1248 bfd_put_bits (data, addr, bits, big_p)
1261 for (i = 0; i < bytes; i++)
1263 int index = big_p ? bytes - i - 1 : i;
1265 addr[index] = (bfd_byte) data;
1271 bfd_get_bits (addr, bits, big_p)
1285 for (i = 0; i < bytes; i++)
1287 int index = big_p ? i : bytes - i - 1;
1289 data = (data << 8) | addr[index];
1295 /* Default implementation */
1298 _bfd_generic_get_section_contents (abfd, section, location, offset, count)
1303 bfd_size_type count;
1308 if (offset + count > section->_raw_size)
1310 bfd_set_error (bfd_error_invalid_operation);
1314 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1315 || bfd_bread (location, count, abfd) != count)
1322 _bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count)
1323 bfd *abfd ATTRIBUTE_UNUSED;
1324 sec_ptr section ATTRIBUTE_UNUSED;
1325 bfd_window *w ATTRIBUTE_UNUSED;
1326 file_ptr offset ATTRIBUTE_UNUSED;
1327 bfd_size_type count ATTRIBUTE_UNUSED;
1332 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents)
1334 /* We don't know what changes the bfd's get_section_contents
1335 method may have to make. So punt trying to map the file
1336 window, and let get_section_contents do its thing. */
1337 /* @@ FIXME : If the internal window has a refcount of 1 and was
1338 allocated with malloc instead of mmap, just reuse it. */
1339 bfd_free_window (w);
1340 w->i = ((bfd_window_internal *)
1341 bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal)));
1344 w->i->data = (PTR) bfd_malloc (count);
1345 if (w->i->data == NULL)
1353 w->size = w->i->size = count;
1354 w->data = w->i->data;
1355 return bfd_get_section_contents (abfd, section, w->data, offset, count);
1357 if (offset + count > section->_raw_size
1358 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true)
1367 /* This generic function can only be used in implementations where creating
1368 NEW sections is disallowed. It is useful in patching existing sections
1369 in read-write files, though. See other set_section_contents functions
1370 to see why it doesn't work for new sections. */
1372 _bfd_generic_set_section_contents (abfd, section, location, offset, count)
1377 bfd_size_type count;
1382 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1383 || bfd_bwrite (location, count, abfd) != count)
1394 unsigned int bfd_log2 (bfd_vma x);
1397 Return the log base 2 of the value supplied, rounded up. E.g., an
1398 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
1405 unsigned int result = 0;
1407 while ((x = (x >> 1)) != 0)
1413 bfd_generic_is_local_label_name (abfd, name)
1417 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1419 return (name[0] == locals_prefix);
1422 /* Can be used from / for bfd_merge_private_bfd_data to check that
1423 endianness matches between input and output file. Returns
1424 true for a match, otherwise returns false and emits an error. */
1426 _bfd_generic_verify_endian_match (ibfd, obfd)
1430 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1431 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1432 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1436 if (bfd_big_endian (ibfd))
1437 msg = _("%s: compiled for a big endian system and target is little endian");
1439 msg = _("%s: compiled for a little endian system and target is big endian");
1441 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
1443 bfd_set_error (bfd_error_wrong_format);
1450 /* Give a warning at runtime if someone compiles code which calls
1453 warn_deprecated (what, file, line, func)
1459 /* Poor man's tracking of functions we've already warned about. */
1460 static size_t mask = 0;
1462 if (~(size_t) func & ~mask)
1464 fprintf (stderr, _("Deprecated %s called"), what);
1466 fprintf (stderr, _(" at %s line %d in %s\n"), file, line, func);
1468 fprintf (stderr, "\n");
1469 mask |= ~(size_t) func;