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
47 bfd
*ignore ATTRIBUTE_UNUSED
;
49 bfd_set_error (bfd_error_invalid_operation
);
53 /* A routine which is used in target vectors for supported operations
54 which do not actually do anything. */
58 bfd
*ignore ATTRIBUTE_UNUSED
;
63 /* A routine which is used in target vectors for unsupported
64 operations which return a pointer value. */
67 bfd_nullvoidptr (ignore
)
68 bfd
*ignore ATTRIBUTE_UNUSED
;
70 bfd_set_error (bfd_error_invalid_operation
);
76 bfd
*ignore ATTRIBUTE_UNUSED
;
83 bfd
*ignore ATTRIBUTE_UNUSED
;
90 bfd
*ignore ATTRIBUTE_UNUSED
;
95 /* A routine which is used in target vectors for unsupported
96 operations which return -1 on error. */
100 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
102 bfd_set_error (bfd_error_invalid_operation
);
108 bfd
*ignore ATTRIBUTE_UNUSED
;
113 _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd
, ignore_exec_bfd
)
114 bfd
*ignore_core_bfd ATTRIBUTE_UNUSED
;
115 bfd
*ignore_exec_bfd ATTRIBUTE_UNUSED
;
117 bfd_set_error (bfd_error_invalid_operation
);
121 /* Routine to handle core_file_failing_command entry point for targets
122 without core file support. */
125 _bfd_nocore_core_file_failing_command (ignore_abfd
)
126 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
128 bfd_set_error (bfd_error_invalid_operation
);
132 /* Routine to handle core_file_failing_signal entry point for targets
133 without core file support. */
136 _bfd_nocore_core_file_failing_signal (ignore_abfd
)
137 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
139 bfd_set_error (bfd_error_invalid_operation
);
144 _bfd_dummy_target (ignore_abfd
)
145 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
147 bfd_set_error (bfd_error_wrong_format
);
151 /* Allocate memory using malloc. */
159 ptr
= (PTR
) malloc (size
);
160 if (ptr
== NULL
&& size
!= 0)
161 bfd_set_error (bfd_error_no_memory
);
165 /* Reallocate memory using realloc. */
168 bfd_realloc (ptr
, size
)
177 ret
= realloc (ptr
, size
);
180 bfd_set_error (bfd_error_no_memory
);
185 /* Allocate memory using malloc and clear it. */
193 ptr
= (PTR
) malloc (size
);
198 bfd_set_error (bfd_error_no_memory
);
200 memset (ptr
, 0, size
);
208 /* Note that archive entries don't have streams; they share their parent's.
209 This allows someone to play with the iostream behind BFD's back.
211 Also, note that the origin pointer points to the beginning of a file's
212 contents (0 for non-archive elements). For archive entries this is the
213 first octet in the file, NOT the beginning of the archive header. */
216 real_read (where
, a
,b
, file
)
222 /* FIXME - this looks like an optimization, but it's really to cover
223 up for a feature of some OSs (not solaris - sigh) that
224 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
225 internally and tries to link against them. BFD seems to be smart
226 enough to realize there are no symbol records in the "file" that
227 doesn't exist but attempts to read them anyway. On Solaris,
228 attempting to read zero bytes from a NULL file results in a core
229 dump, but on other platforms it just returns zero bytes read.
230 This makes it to something reasonable. - DJ */
231 if (a
== 0 || b
== 0)
234 #if defined (__VAX) && defined (VMS)
235 /* Apparently fread on Vax VMS does not keep the record length
237 return read (fileno (file
), where
, a
* b
);
239 return fread (where
, a
, b
, file
);
243 /* Return value is amount read (FIXME: how are errors and end of file dealt
244 with? We never call bfd_set_error, which is probably a mistake). */
247 bfd_read (ptr
, size
, nitems
, abfd
)
250 bfd_size_type nitems
;
255 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
257 struct bfd_in_memory
*bim
;
260 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
262 if (abfd
->where
+ get
> bim
->size
)
264 if (bim
->size
< (bfd_size_type
) abfd
->where
)
267 get
= bim
->size
- abfd
->where
;
268 bfd_set_error (bfd_error_file_truncated
);
270 memcpy (ptr
, bim
->buffer
+ abfd
->where
, get
);
275 nread
= real_read (ptr
, 1, (size_t) (size
*nitems
), bfd_cache_lookup(abfd
));
277 abfd
->where
+= nread
;
279 /* Set bfd_error if we did not read as much data as we expected.
281 If the read failed due to an error set the bfd_error_system_call,
282 else set bfd_error_file_truncated.
284 A BFD backend may wish to override bfd_error_file_truncated to
285 provide something more useful (eg. no_symbols or wrong_format). */
286 if (nread
!= (int) (size
* nitems
))
288 if (ferror (bfd_cache_lookup (abfd
)))
289 bfd_set_error (bfd_error_system_call
);
291 bfd_set_error (bfd_error_file_truncated
);
297 /* The window support stuff should probably be broken out into
299 /* The idea behind the next and refcount fields is that one mapped
300 region can suffice for multiple read-only windows or multiple
301 non-overlapping read-write windows. It's not implemented yet
303 struct _bfd_window_internal
{
304 struct _bfd_window_internal
*next
;
307 int refcount
: 31; /* should be enough... */
308 unsigned mapped
: 1; /* 1 = mmap, 0 = malloc */
312 bfd_init_window (windowp
)
320 /* Currently, if USE_MMAP is undefined, none if the window stuff is
321 used. Okay, so it's mis-named. At least the command-line option
322 "--without-mmap" is more obvious than "--without-windows" or some
326 #undef HAVE_MPROTECT /* code's not tested yet */
328 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
329 #include <sys/mman.h>
336 static int debug_windows
;
339 bfd_free_window (windowp
)
342 bfd_window_internal
*i
= windowp
->i
;
349 fprintf (stderr
, "freeing window @%p<%p,%lx,%p>\n",
350 windowp
, windowp
->data
, windowp
->size
, windowp
->i
);
351 if (i
->refcount
!= 0)
357 munmap (i
->data
, i
->size
);
364 mprotect (i
->data
, i
->size
, PROT_READ
| PROT_WRITE
);
371 /* There should be no more references to i at this point. */
375 static int ok_to_map
= 1;
378 bfd_get_file_window (abfd
, offset
, size
, windowp
, writable
)
385 static size_t pagesize
;
386 bfd_window_internal
*i
= windowp
->i
;
387 size_t size_to_alloc
= size
;
390 fprintf (stderr
, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
391 abfd
, (long) offset
, (long) size
,
392 windowp
, windowp
->data
, (unsigned long) windowp
->size
,
393 windowp
->i
, writable
);
395 /* Make sure we know the page size, so we can be friendly to mmap. */
397 pagesize
= getpagesize ();
403 windowp
->i
= i
= (bfd_window_internal
*) bfd_zmalloc (sizeof (bfd_window_internal
));
410 && (i
->data
== 0 || i
->mapped
== 1)
411 && (abfd
->flags
& BFD_IN_MEMORY
) == 0)
413 file_ptr file_offset
, offset2
;
418 /* Find the real file and the real offset into it. */
419 while (abfd
->my_archive
!= NULL
)
421 offset
+= abfd
->origin
;
422 abfd
= abfd
->my_archive
;
424 f
= bfd_cache_lookup (abfd
);
427 /* Compute offsets and size for mmap and for the user's data. */
428 offset2
= offset
% pagesize
;
431 file_offset
= offset
- offset2
;
432 real_size
= offset
+ size
- file_offset
;
433 real_size
= real_size
+ pagesize
- 1;
434 real_size
-= real_size
% pagesize
;
436 /* If we're re-using a memory region, make sure it's big enough. */
437 if (i
->data
&& i
->size
< size
)
439 munmap (i
->data
, i
->size
);
442 i
->data
= mmap (i
->data
, real_size
,
443 writable
? PROT_WRITE
| PROT_READ
: PROT_READ
,
445 ? MAP_FILE
| MAP_PRIVATE
446 : MAP_FILE
| MAP_SHARED
),
448 if (i
->data
== (PTR
) -1)
450 /* An error happened. Report it, or try using malloc, or
452 bfd_set_error (bfd_error_system_call
);
456 fprintf (stderr
, "\t\tmmap failed!\n");
460 fprintf (stderr
, "\n\tmapped %ld at %p, offset is %ld\n",
461 (long) real_size
, i
->data
, (long) offset2
);
463 windowp
->data
= (PTR
) ((bfd_byte
*) i
->data
+ offset2
);
464 windowp
->size
= size
;
468 else if (debug_windows
)
471 fprintf (stderr
, _("not mapping: data=%lx mapped=%d\n"),
472 (unsigned long) i
->data
, (int) i
->mapped
);
474 fprintf (stderr
, _("not mapping: env var not set\n"));
483 size_to_alloc
+= pagesize
- 1;
484 size_to_alloc
-= size_to_alloc
% pagesize
;
488 fprintf (stderr
, "\n\t%s(%6ld)",
489 i
->data
? "realloc" : " malloc", (long) size_to_alloc
);
490 i
->data
= (PTR
) bfd_realloc (i
->data
, size_to_alloc
);
492 fprintf (stderr
, "\t-> %p\n", i
->data
);
496 if (size_to_alloc
== 0)
498 bfd_set_error (bfd_error_no_memory
);
501 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
503 i
->size
= bfd_read (i
->data
, size
, 1, abfd
);
511 fprintf (stderr
, "\tmprotect (%p, %ld, PROT_READ)\n", i
->data
,
513 mprotect (i
->data
, i
->size
, PROT_READ
);
516 windowp
->data
= i
->data
;
517 windowp
->size
= i
->size
;
521 #endif /* USE_MMAP */
524 bfd_write (ptr
, size
, nitems
, abfd
)
527 bfd_size_type nitems
;
532 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
534 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) (abfd
->iostream
);
536 if (abfd
->where
+ size
> bim
->size
)
538 long newsize
, oldsize
= (bim
->size
+ 127) & ~127;
539 bim
->size
= abfd
->where
+ size
;
540 /* Round up to cut down on memory fragmentation */
541 newsize
= (bim
->size
+ 127) & ~127;
542 if (newsize
> oldsize
)
544 bim
->buffer
= bfd_realloc (bim
->buffer
, newsize
);
545 if (bim
->buffer
== 0)
552 memcpy (bim
->buffer
+ abfd
->where
, ptr
, size
);
557 nwrote
= fwrite (ptr
, 1, (size_t) (size
* nitems
),
558 bfd_cache_lookup (abfd
));
560 abfd
->where
+= nwrote
;
561 if ((bfd_size_type
) nwrote
!= size
* nitems
)
567 bfd_set_error (bfd_error_system_call
);
574 bfd_write_bigendian_4byte_int
577 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
580 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
581 endian order regardless of what else is going on. This is useful in
586 bfd_write_bigendian_4byte_int (abfd
, i
)
591 bfd_putb32(i
, buffer
);
592 if (bfd_write((PTR
)buffer
, 4, 1, abfd
) != 4)
602 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
605 ptr
= ftell (bfd_cache_lookup(abfd
));
607 if (abfd
->my_archive
)
617 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
619 return fflush (bfd_cache_lookup(abfd
));
622 /* Returns 0 for success, negative value for failure (in which case
623 bfd_get_error can retrieve the error code). */
625 bfd_stat (abfd
, statbuf
)
627 struct stat
*statbuf
;
632 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
635 f
= bfd_cache_lookup (abfd
);
638 bfd_set_error (bfd_error_system_call
);
641 result
= fstat (fileno (f
), statbuf
);
643 bfd_set_error (bfd_error_system_call
);
647 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
648 can retrieve the error code). */
651 bfd_seek (abfd
, position
, direction
)
658 file_ptr file_position
;
659 /* For the time being, a BFD may not seek to it's end. The problem
660 is that we don't easily have a way to recognize the end of an
661 element in an archive. */
663 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
665 if (direction
== SEEK_CUR
&& position
== 0)
668 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
670 struct bfd_in_memory
*bim
;
672 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
674 if (direction
== SEEK_SET
)
675 abfd
->where
= position
;
677 abfd
->where
+= position
;
679 if ((bfd_size_type
) abfd
->where
> bim
->size
)
681 if ((abfd
->direction
== write_direction
) ||
682 (abfd
->direction
== both_direction
))
684 long newsize
, oldsize
= (bim
->size
+ 127) & ~127;
685 bim
->size
= abfd
->where
;
686 /* Round up to cut down on memory fragmentation */
687 newsize
= (bim
->size
+ 127) & ~127;
688 if (newsize
> oldsize
)
690 bim
->buffer
= bfd_realloc (bim
->buffer
, newsize
);
691 if (bim
->buffer
== 0)
694 bfd_set_error (bfd_error_no_memory
);
701 abfd
->where
= bim
->size
;
702 bfd_set_error (bfd_error_file_truncated
);
709 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
712 /* Explanation for this code: I'm only about 95+% sure that the above
713 conditions are sufficient and that all i/o calls are properly
714 adjusting the `where' field. So this is sort of an `assert'
715 that the `where' field is correct. If we can go a while without
716 tripping the abort, we can probably safely disable this code,
717 so that the real optimizations happen. */
718 file_ptr where_am_i_now
;
719 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
720 if (abfd
->my_archive
)
721 where_am_i_now
-= abfd
->origin
;
722 if (where_am_i_now
!= abfd
->where
)
725 if (direction
== SEEK_SET
&& position
== abfd
->where
)
730 /* We need something smarter to optimize access to archives.
731 Currently, anything inside an archive is read via the file
732 handle for the archive. Which means that a bfd_seek on one
733 component affects the `current position' in the archive, as
734 well as in any other component.
736 It might be sufficient to put a spike through the cache
737 abstraction, and look to the archive for the file position,
738 but I think we should try for something cleaner.
740 In the meantime, no optimization for archives. */
743 f
= bfd_cache_lookup (abfd
);
744 file_position
= position
;
745 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
746 file_position
+= abfd
->origin
;
748 result
= fseek (f
, file_position
, direction
);
752 int hold_errno
= errno
;
754 /* Force redetermination of `where' field. */
757 /* An EINVAL error probably means that the file offset was
759 if (hold_errno
== EINVAL
)
760 bfd_set_error (bfd_error_file_truncated
);
763 bfd_set_error (bfd_error_system_call
);
769 /* Adjust `where' field. */
770 if (direction
== SEEK_SET
)
771 abfd
->where
= position
;
773 abfd
->where
+= position
;
778 /** The do-it-yourself (byte) sex-change kit */
780 /* The middle letter e.g. get<b>short indicates Big or Little endian
781 target machine. It doesn't matter what the byte order of the host
782 machine is; these routines work for either. */
784 /* FIXME: Should these take a count argument?
785 Answer (gnu@cygnus.com): No, but perhaps they should be inline
786 functions in swap.h #ifdef __GNUC__.
787 Gprof them later and find out. */
796 These macros as used for reading and writing raw data in
797 sections; each access (except for bytes) is vectored through
798 the target format of the BFD and mangled accordingly. The
799 mangling performs any necessary endian translations and
800 removes alignment restrictions. Note that types accepted and
801 returned by these macros are identical so they can be swapped
802 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
803 to either <<bfd_get_32>> or <<bfd_get_64>>.
805 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
806 system without prototypes, the caller is responsible for making
807 sure that is true, with a cast if necessary. We don't cast
808 them in the macro definitions because that would prevent <<lint>>
809 or <<gcc -Wall>> from detecting sins such as passing a pointer.
810 To detect calling these with less than a <<bfd_vma>>, use
811 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
814 .{* Byte swapping macros for user section data. *}
816 .#define bfd_put_8(abfd, val, ptr) \
817 . ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val)))
818 .#define bfd_put_signed_8 \
820 .#define bfd_get_8(abfd, ptr) \
821 . (*(unsigned char *) (ptr))
822 .#define bfd_get_signed_8(abfd, ptr) \
823 . ((*(unsigned char *) (ptr) ^ 0x80) - 0x80)
825 .#define bfd_put_16(abfd, val, ptr) \
826 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
827 .#define bfd_put_signed_16 \
829 .#define bfd_get_16(abfd, ptr) \
830 . BFD_SEND(abfd, bfd_getx16, (ptr))
831 .#define bfd_get_signed_16(abfd, ptr) \
832 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
834 .#define bfd_put_32(abfd, val, ptr) \
835 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
836 .#define bfd_put_signed_32 \
838 .#define bfd_get_32(abfd, ptr) \
839 . BFD_SEND(abfd, bfd_getx32, (ptr))
840 .#define bfd_get_signed_32(abfd, ptr) \
841 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
843 .#define bfd_put_64(abfd, val, ptr) \
844 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
845 .#define bfd_put_signed_64 \
847 .#define bfd_get_64(abfd, ptr) \
848 . BFD_SEND(abfd, bfd_getx64, (ptr))
849 .#define bfd_get_signed_64(abfd, ptr) \
850 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
852 .#define bfd_get(bits, abfd, ptr) \
853 . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \
854 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
855 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
856 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
857 . : (abort (), (bfd_vma) - 1))
859 .#define bfd_put(bits, abfd, val, ptr) \
860 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
861 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
862 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
863 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
864 . : (abort (), (void) 0))
874 These macros have the same function as their <<bfd_get_x>>
875 bretheren, except that they are used for removing information
876 for the header records of object files. Believe it or not,
877 some object files keep their header records in big endian
878 order and their data in little endian order.
880 .{* Byte swapping macros for file header data. *}
882 .#define bfd_h_put_8(abfd, val, ptr) \
883 . bfd_put_8 (abfd, val, ptr)
884 .#define bfd_h_put_signed_8(abfd, val, ptr) \
885 . bfd_put_8 (abfd, val, ptr)
886 .#define bfd_h_get_8(abfd, ptr) \
887 . bfd_get_8 (abfd, ptr)
888 .#define bfd_h_get_signed_8(abfd, ptr) \
889 . bfd_get_signed_8 (abfd, ptr)
891 .#define bfd_h_put_16(abfd, val, ptr) \
892 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
893 .#define bfd_h_put_signed_16 \
895 .#define bfd_h_get_16(abfd, ptr) \
896 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
897 .#define bfd_h_get_signed_16(abfd, ptr) \
898 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
900 .#define bfd_h_put_32(abfd, val, ptr) \
901 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
902 .#define bfd_h_put_signed_32 \
904 .#define bfd_h_get_32(abfd, ptr) \
905 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
906 .#define bfd_h_get_signed_32(abfd, ptr) \
907 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
909 .#define bfd_h_put_64(abfd, val, ptr) \
910 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
911 .#define bfd_h_put_signed_64 \
913 .#define bfd_h_get_64(abfd, ptr) \
914 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
915 .#define bfd_h_get_signed_64(abfd, ptr) \
916 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
920 /* Sign extension to bfd_signed_vma. */
921 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
922 #define COERCE32(x) \
923 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000))
924 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
925 #define COERCE64(x) \
926 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
930 register const bfd_byte
*addr
;
932 return (addr
[0] << 8) | addr
[1];
937 register const bfd_byte
*addr
;
939 return (addr
[1] << 8) | addr
[0];
943 bfd_getb_signed_16 (addr
)
944 register const bfd_byte
*addr
;
946 return COERCE16((addr
[0] << 8) | addr
[1]);
950 bfd_getl_signed_16 (addr
)
951 register const bfd_byte
*addr
;
953 return COERCE16((addr
[1] << 8) | addr
[0]);
957 bfd_putb16 (data
, addr
)
959 register bfd_byte
*addr
;
961 addr
[0] = (bfd_byte
) (data
>> 8);
962 addr
[1] = (bfd_byte
)data
;
966 bfd_putl16 (data
, addr
)
968 register bfd_byte
*addr
;
970 addr
[0] = (bfd_byte
)data
;
971 addr
[1] = (bfd_byte
) (data
>> 8);
976 register const bfd_byte
*addr
;
980 v
= (unsigned long) addr
[0] << 24;
981 v
|= (unsigned long) addr
[1] << 16;
982 v
|= (unsigned long) addr
[2] << 8;
983 v
|= (unsigned long) addr
[3];
989 register const bfd_byte
*addr
;
993 v
= (unsigned long) addr
[0];
994 v
|= (unsigned long) addr
[1] << 8;
995 v
|= (unsigned long) addr
[2] << 16;
996 v
|= (unsigned long) addr
[3] << 24;
1001 bfd_getb_signed_32 (addr
)
1002 register const bfd_byte
*addr
;
1006 v
= (unsigned long) addr
[0] << 24;
1007 v
|= (unsigned long) addr
[1] << 16;
1008 v
|= (unsigned long) addr
[2] << 8;
1009 v
|= (unsigned long) addr
[3];
1010 return COERCE32 (v
);
1014 bfd_getl_signed_32 (addr
)
1015 register const bfd_byte
*addr
;
1019 v
= (unsigned long) addr
[0];
1020 v
|= (unsigned long) addr
[1] << 8;
1021 v
|= (unsigned long) addr
[2] << 16;
1022 v
|= (unsigned long) addr
[3] << 24;
1023 return COERCE32 (v
);
1028 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
1033 high
= ((((((((addr
[0]) << 8) |
1038 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
1043 return high
<< 32 | low
;
1052 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
1056 high
= (((((((addr
[7] << 8) |
1061 low
= ((((((((bfd_vma
)addr
[3] << 8) |
1066 return high
<< 32 | low
;
1075 bfd_getb_signed_64 (addr
)
1076 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
1081 high
= ((((((((addr
[0]) << 8) |
1086 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
1091 return COERCE64(high
<< 32 | low
);
1099 bfd_getl_signed_64 (addr
)
1100 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
1104 high
= (((((((addr
[7] << 8) |
1109 low
= ((((((((bfd_vma
)addr
[3] << 8) |
1114 return COERCE64(high
<< 32 | low
);
1122 bfd_putb32 (data
, addr
)
1124 register bfd_byte
*addr
;
1126 addr
[0] = (bfd_byte
) (data
>> 24);
1127 addr
[1] = (bfd_byte
) (data
>> 16);
1128 addr
[2] = (bfd_byte
) (data
>> 8);
1129 addr
[3] = (bfd_byte
)data
;
1133 bfd_putl32 (data
, addr
)
1135 register bfd_byte
*addr
;
1137 addr
[0] = (bfd_byte
)data
;
1138 addr
[1] = (bfd_byte
) (data
>> 8);
1139 addr
[2] = (bfd_byte
) (data
>> 16);
1140 addr
[3] = (bfd_byte
) (data
>> 24);
1144 bfd_putb64 (data
, addr
)
1145 bfd_vma data ATTRIBUTE_UNUSED
;
1146 register bfd_byte
*addr ATTRIBUTE_UNUSED
;
1149 addr
[0] = (bfd_byte
) (data
>> (7*8));
1150 addr
[1] = (bfd_byte
) (data
>> (6*8));
1151 addr
[2] = (bfd_byte
) (data
>> (5*8));
1152 addr
[3] = (bfd_byte
) (data
>> (4*8));
1153 addr
[4] = (bfd_byte
) (data
>> (3*8));
1154 addr
[5] = (bfd_byte
) (data
>> (2*8));
1155 addr
[6] = (bfd_byte
) (data
>> (1*8));
1156 addr
[7] = (bfd_byte
) (data
>> (0*8));
1163 bfd_putl64 (data
, addr
)
1164 bfd_vma data ATTRIBUTE_UNUSED
;
1165 register bfd_byte
*addr ATTRIBUTE_UNUSED
;
1168 addr
[7] = (bfd_byte
) (data
>> (7*8));
1169 addr
[6] = (bfd_byte
) (data
>> (6*8));
1170 addr
[5] = (bfd_byte
) (data
>> (5*8));
1171 addr
[4] = (bfd_byte
) (data
>> (4*8));
1172 addr
[3] = (bfd_byte
) (data
>> (3*8));
1173 addr
[2] = (bfd_byte
) (data
>> (2*8));
1174 addr
[1] = (bfd_byte
) (data
>> (1*8));
1175 addr
[0] = (bfd_byte
) (data
>> (0*8));
1182 bfd_put_bits (data
, addr
, bits
, big_p
)
1195 for (i
= 0; i
< bytes
; i
++)
1197 int index
= big_p
? bytes
- i
- 1 : i
;
1199 addr
[index
] = (bfd_byte
) data
;
1205 bfd_get_bits (addr
, bits
, big_p
)
1219 for (i
= 0; i
< bytes
; i
++)
1221 int index
= big_p
? i
: bytes
- i
- 1;
1223 data
= (data
<< 8) | addr
[index
];
1229 /* Default implementation */
1232 _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
)
1237 bfd_size_type count
;
1242 if ((bfd_size_type
) (offset
+ count
) > section
->_raw_size
)
1244 bfd_set_error (bfd_error_invalid_operation
);
1248 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
1249 || bfd_read (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
1256 _bfd_generic_get_section_contents_in_window (abfd
, section
, w
, offset
, count
)
1257 bfd
*abfd ATTRIBUTE_UNUSED
;
1258 sec_ptr section ATTRIBUTE_UNUSED
;
1259 bfd_window
*w ATTRIBUTE_UNUSED
;
1260 file_ptr offset ATTRIBUTE_UNUSED
;
1261 bfd_size_type count ATTRIBUTE_UNUSED
;
1266 if (abfd
->xvec
->_bfd_get_section_contents
!= _bfd_generic_get_section_contents
)
1268 /* We don't know what changes the bfd's get_section_contents
1269 method may have to make. So punt trying to map the file
1270 window, and let get_section_contents do its thing. */
1271 /* @@ FIXME : If the internal window has a refcount of 1 and was
1272 allocated with malloc instead of mmap, just reuse it. */
1273 bfd_free_window (w
);
1274 w
->i
= (bfd_window_internal
*) bfd_zmalloc (sizeof (bfd_window_internal
));
1277 w
->i
->data
= (PTR
) bfd_malloc ((size_t) count
);
1278 if (w
->i
->data
== NULL
)
1286 w
->size
= w
->i
->size
= count
;
1287 w
->data
= w
->i
->data
;
1288 return bfd_get_section_contents (abfd
, section
, w
->data
, offset
, count
);
1290 if ((bfd_size_type
) (offset
+count
) > section
->_raw_size
1291 || (bfd_get_file_window (abfd
, section
->filepos
+ offset
, count
, w
, true)
1300 /* This generic function can only be used in implementations where creating
1301 NEW sections is disallowed. It is useful in patching existing sections
1302 in read-write files, though. See other set_section_contents functions
1303 to see why it doesn't work for new sections. */
1305 _bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
1310 bfd_size_type count
;
1315 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) == -1
1316 || bfd_write (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
1327 unsigned int bfd_log2(bfd_vma x);
1330 Return the log base 2 of the value supplied, rounded up. E.g., an
1331 @var{x} of 1025 returns 11.
1338 unsigned int result
= 0;
1340 while ((x
= (x
>> 1)) != 0)
1346 bfd_generic_is_local_label_name (abfd
, name
)
1350 char locals_prefix
= (bfd_get_symbol_leading_char (abfd
) == '_') ? 'L' : '.';
1352 return (name
[0] == locals_prefix
);
1355 /* Can be used from / for bfd_merge_private_bfd_data to check that
1356 endianness matches between input and output file. Returns
1357 true for a match, otherwise returns false and emits an error. */
1359 _bfd_generic_verify_endian_match (ibfd
, obfd
)
1363 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1364 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1365 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1369 if (bfd_big_endian (ibfd
))
1370 msg
= _("%s: compiled for a big endian system and target is little endian");
1372 msg
= _("%s: compiled for a little endian system and target is big endian");
1374 (*_bfd_error_handler
) (msg
, bfd_get_filename (ibfd
));
1376 bfd_set_error (bfd_error_wrong_format
);