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)
236 #if defined (__VAX) && defined (VMS)
237 /* Apparently fread on Vax VMS does not keep the record length
239 return read (fileno (file
), where
, a
* b
);
241 return fread (where
, a
, b
, file
);
245 /* Return value is amount read (FIXME: how are errors and end of file dealt
246 with? We never call bfd_set_error, which is probably a mistake). */
249 bfd_read (ptr
, size
, nitems
, abfd
)
252 bfd_size_type nitems
;
257 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
259 struct bfd_in_memory
*bim
;
262 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
264 if (abfd
->where
+ get
> bim
->size
)
266 if (bim
->size
< (bfd_size_type
) abfd
->where
)
269 get
= bim
->size
- abfd
->where
;
270 bfd_set_error (bfd_error_file_truncated
);
272 memcpy (ptr
, bim
->buffer
+ abfd
->where
, get
);
277 nread
= real_read (ptr
, 1, (size_t) (size
*nitems
), bfd_cache_lookup(abfd
));
279 abfd
->where
+= nread
;
281 /* Set bfd_error if we did not read as much data as we expected.
283 If the read failed due to an error set the bfd_error_system_call,
284 else set bfd_error_file_truncated.
286 A BFD backend may wish to override bfd_error_file_truncated to
287 provide something more useful (eg. no_symbols or wrong_format). */
288 if (nread
!= (int) (size
* nitems
))
290 if (ferror (bfd_cache_lookup (abfd
)))
291 bfd_set_error (bfd_error_system_call
);
293 bfd_set_error (bfd_error_file_truncated
);
299 /* The window support stuff should probably be broken out into
301 /* The idea behind the next and refcount fields is that one mapped
302 region can suffice for multiple read-only windows or multiple
303 non-overlapping read-write windows. It's not implemented yet
305 struct _bfd_window_internal
{
306 struct _bfd_window_internal
*next
;
309 int refcount
: 31; /* should be enough... */
310 unsigned mapped
: 1; /* 1 = mmap, 0 = malloc */
314 bfd_init_window (windowp
)
322 /* Currently, if USE_MMAP is undefined, none if the window stuff is
323 used. Okay, so it's mis-named. At least the command-line option
324 "--without-mmap" is more obvious than "--without-windows" or some
328 #undef HAVE_MPROTECT /* code's not tested yet */
330 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
331 #include <sys/mman.h>
338 static int debug_windows
;
341 bfd_free_window (windowp
)
344 bfd_window_internal
*i
= windowp
->i
;
351 fprintf (stderr
, "freeing window @%p<%p,%lx,%p>\n",
352 windowp
, windowp
->data
, windowp
->size
, windowp
->i
);
353 if (i
->refcount
!= 0)
359 munmap (i
->data
, i
->size
);
366 mprotect (i
->data
, i
->size
, PROT_READ
| PROT_WRITE
);
373 /* There should be no more references to i at this point. */
377 static int ok_to_map
= 1;
380 bfd_get_file_window (abfd
, offset
, size
, windowp
, writable
)
387 static size_t pagesize
;
388 bfd_window_internal
*i
= windowp
->i
;
389 size_t size_to_alloc
= size
;
392 fprintf (stderr
, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
393 abfd
, (long) offset
, (long) size
,
394 windowp
, windowp
->data
, (unsigned long) windowp
->size
,
395 windowp
->i
, writable
);
397 /* Make sure we know the page size, so we can be friendly to mmap. */
399 pagesize
= getpagesize ();
405 windowp
->i
= i
= (bfd_window_internal
*) bfd_zmalloc (sizeof (bfd_window_internal
));
412 && (i
->data
== 0 || i
->mapped
== 1)
413 && (abfd
->flags
& BFD_IN_MEMORY
) == 0)
415 file_ptr file_offset
, offset2
;
420 /* Find the real file and the real offset into it. */
421 while (abfd
->my_archive
!= NULL
)
423 offset
+= abfd
->origin
;
424 abfd
= abfd
->my_archive
;
426 f
= bfd_cache_lookup (abfd
);
429 /* Compute offsets and size for mmap and for the user's data. */
430 offset2
= offset
% pagesize
;
433 file_offset
= offset
- offset2
;
434 real_size
= offset
+ size
- file_offset
;
435 real_size
= real_size
+ pagesize
- 1;
436 real_size
-= real_size
% pagesize
;
438 /* If we're re-using a memory region, make sure it's big enough. */
439 if (i
->data
&& i
->size
< size
)
441 munmap (i
->data
, i
->size
);
444 i
->data
= mmap (i
->data
, real_size
,
445 writable
? PROT_WRITE
| PROT_READ
: PROT_READ
,
447 ? MAP_FILE
| MAP_PRIVATE
448 : MAP_FILE
| MAP_SHARED
),
450 if (i
->data
== (PTR
) -1)
452 /* An error happened. Report it, or try using malloc, or
454 bfd_set_error (bfd_error_system_call
);
458 fprintf (stderr
, "\t\tmmap failed!\n");
462 fprintf (stderr
, "\n\tmapped %ld at %p, offset is %ld\n",
463 (long) real_size
, i
->data
, (long) offset2
);
465 windowp
->data
= (PTR
) ((bfd_byte
*) i
->data
+ offset2
);
466 windowp
->size
= size
;
470 else if (debug_windows
)
473 fprintf (stderr
, _("not mapping: data=%lx mapped=%d\n"),
474 (unsigned long) i
->data
, (int) i
->mapped
);
476 fprintf (stderr
, _("not mapping: env var not set\n"));
485 size_to_alloc
+= pagesize
- 1;
486 size_to_alloc
-= size_to_alloc
% pagesize
;
490 fprintf (stderr
, "\n\t%s(%6ld)",
491 i
->data
? "realloc" : " malloc", (long) size_to_alloc
);
492 i
->data
= (PTR
) bfd_realloc (i
->data
, size_to_alloc
);
494 fprintf (stderr
, "\t-> %p\n", i
->data
);
498 if (size_to_alloc
== 0)
500 bfd_set_error (bfd_error_no_memory
);
503 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
505 i
->size
= bfd_read (i
->data
, size
, 1, abfd
);
513 fprintf (stderr
, "\tmprotect (%p, %ld, PROT_READ)\n", i
->data
,
515 mprotect (i
->data
, i
->size
, PROT_READ
);
518 windowp
->data
= i
->data
;
519 windowp
->size
= i
->size
;
523 #endif /* USE_MMAP */
526 bfd_write (ptr
, size
, nitems
, abfd
)
529 bfd_size_type nitems
;
534 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
536 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) (abfd
->iostream
);
538 if (abfd
->where
+ size
> bim
->size
)
540 long newsize
, oldsize
= (bim
->size
+ 127) & ~127;
541 bim
->size
= abfd
->where
+ size
;
542 /* Round up to cut down on memory fragmentation */
543 newsize
= (bim
->size
+ 127) & ~127;
544 if (newsize
> oldsize
)
546 bim
->buffer
= bfd_realloc (bim
->buffer
, newsize
);
547 if (bim
->buffer
== 0)
554 memcpy (bim
->buffer
+ abfd
->where
, ptr
, size
);
559 nwrote
= fwrite (ptr
, 1, (size_t) (size
* nitems
),
560 bfd_cache_lookup (abfd
));
562 abfd
->where
+= nwrote
;
563 if ((bfd_size_type
) nwrote
!= size
* nitems
)
569 bfd_set_error (bfd_error_system_call
);
576 bfd_write_bigendian_4byte_int
579 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
582 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
583 endian order regardless of what else is going on. This is useful in
588 bfd_write_bigendian_4byte_int (abfd
, i
)
593 bfd_putb32(i
, buffer
);
594 if (bfd_write((PTR
)buffer
, 4, 1, abfd
) != 4)
604 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
607 ptr
= ftell (bfd_cache_lookup(abfd
));
609 if (abfd
->my_archive
)
619 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
621 return fflush (bfd_cache_lookup(abfd
));
624 /* Returns 0 for success, negative value for failure (in which case
625 bfd_get_error can retrieve the error code). */
627 bfd_stat (abfd
, statbuf
)
629 struct stat
*statbuf
;
634 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
637 f
= bfd_cache_lookup (abfd
);
640 bfd_set_error (bfd_error_system_call
);
643 result
= fstat (fileno (f
), statbuf
);
645 bfd_set_error (bfd_error_system_call
);
649 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
650 can retrieve the error code). */
653 bfd_seek (abfd
, position
, direction
)
660 file_ptr file_position
;
661 /* For the time being, a BFD may not seek to it's end. The problem
662 is that we don't easily have a way to recognize the end of an
663 element in an archive. */
665 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
667 if (direction
== SEEK_CUR
&& position
== 0)
670 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
672 struct bfd_in_memory
*bim
;
674 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
676 if (direction
== SEEK_SET
)
677 abfd
->where
= position
;
679 abfd
->where
+= position
;
681 if ((bfd_size_type
) abfd
->where
> bim
->size
)
683 if ((abfd
->direction
== write_direction
) ||
684 (abfd
->direction
== both_direction
))
686 long newsize
, oldsize
= (bim
->size
+ 127) & ~127;
687 bim
->size
= abfd
->where
;
688 /* Round up to cut down on memory fragmentation */
689 newsize
= (bim
->size
+ 127) & ~127;
690 if (newsize
> oldsize
)
692 bim
->buffer
= bfd_realloc (bim
->buffer
, newsize
);
693 if (bim
->buffer
== 0)
696 bfd_set_error (bfd_error_no_memory
);
703 abfd
->where
= bim
->size
;
704 bfd_set_error (bfd_error_file_truncated
);
711 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
714 /* Explanation for this code: I'm only about 95+% sure that the above
715 conditions are sufficient and that all i/o calls are properly
716 adjusting the `where' field. So this is sort of an `assert'
717 that the `where' field is correct. If we can go a while without
718 tripping the abort, we can probably safely disable this code,
719 so that the real optimizations happen. */
720 file_ptr where_am_i_now
;
721 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
722 if (abfd
->my_archive
)
723 where_am_i_now
-= abfd
->origin
;
724 if (where_am_i_now
!= abfd
->where
)
727 if (direction
== SEEK_SET
&& position
== abfd
->where
)
732 /* We need something smarter to optimize access to archives.
733 Currently, anything inside an archive is read via the file
734 handle for the archive. Which means that a bfd_seek on one
735 component affects the `current position' in the archive, as
736 well as in any other component.
738 It might be sufficient to put a spike through the cache
739 abstraction, and look to the archive for the file position,
740 but I think we should try for something cleaner.
742 In the meantime, no optimization for archives. */
745 f
= bfd_cache_lookup (abfd
);
746 file_position
= position
;
747 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
748 file_position
+= abfd
->origin
;
750 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
);