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
36 These routines are used within BFD.
37 They are not intended for export, but are documented here for
41 /* A routine which is used in target vectors for unsupported
46 bfd
*ignore ATTRIBUTE_UNUSED
;
48 bfd_set_error (bfd_error_invalid_operation
);
52 /* A routine which is used in target vectors for supported operations
53 which do not actually do anything. */
57 bfd
*ignore ATTRIBUTE_UNUSED
;
62 /* A routine which is used in target vectors for unsupported
63 operations which return a pointer value. */
66 bfd_nullvoidptr (ignore
)
67 bfd
*ignore ATTRIBUTE_UNUSED
;
69 bfd_set_error (bfd_error_invalid_operation
);
75 bfd
*ignore ATTRIBUTE_UNUSED
;
82 bfd
*ignore ATTRIBUTE_UNUSED
;
89 bfd
*ignore ATTRIBUTE_UNUSED
;
94 /* A routine which is used in target vectors for unsupported
95 operations which return -1 on error. */
99 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
101 bfd_set_error (bfd_error_invalid_operation
);
107 bfd
*ignore ATTRIBUTE_UNUSED
;
112 _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd
, ignore_exec_bfd
)
113 bfd
*ignore_core_bfd ATTRIBUTE_UNUSED
;
114 bfd
*ignore_exec_bfd ATTRIBUTE_UNUSED
;
116 bfd_set_error (bfd_error_invalid_operation
);
120 /* Routine to handle core_file_failing_command entry point for targets
121 without core file support. */
124 _bfd_nocore_core_file_failing_command (ignore_abfd
)
125 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
127 bfd_set_error (bfd_error_invalid_operation
);
131 /* Routine to handle core_file_failing_signal entry point for targets
132 without core file support. */
135 _bfd_nocore_core_file_failing_signal (ignore_abfd
)
136 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
138 bfd_set_error (bfd_error_invalid_operation
);
143 _bfd_dummy_target (ignore_abfd
)
144 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
146 bfd_set_error (bfd_error_wrong_format
);
150 /* Allocate memory using malloc. */
158 if (size
!= (size_t) size
)
160 bfd_set_error (bfd_error_no_memory
);
164 ptr
= (PTR
) malloc ((size_t) size
);
165 if (ptr
== NULL
&& (size_t) size
!= 0)
166 bfd_set_error (bfd_error_no_memory
);
171 /* Reallocate memory using realloc. */
174 bfd_realloc (ptr
, size
)
180 if (size
!= (size_t) size
)
182 bfd_set_error (bfd_error_no_memory
);
187 ret
= (PTR
) malloc ((size_t) size
);
189 ret
= (PTR
) realloc (ptr
, (size_t) size
);
191 if (ret
== NULL
&& (size_t) size
!= 0)
192 bfd_set_error (bfd_error_no_memory
);
197 /* Allocate memory using malloc and clear it. */
205 if (size
!= (size_t) size
)
207 bfd_set_error (bfd_error_no_memory
);
211 ptr
= (PTR
) malloc ((size_t) size
);
213 if ((size_t) size
!= 0)
216 bfd_set_error (bfd_error_no_memory
);
218 memset (ptr
, 0, (size_t) size
);
225 bfd_write_bigendian_4byte_int
228 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
231 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
232 endian order regardless of what else is going on. This is useful in
237 bfd_write_bigendian_4byte_int (abfd
, i
)
242 bfd_putb32 ((bfd_vma
) i
, buffer
);
243 return bfd_bwrite ((PTR
) buffer
, (bfd_size_type
) 4, abfd
) == 4;
247 /** The do-it-yourself (byte) sex-change kit */
249 /* The middle letter e.g. get<b>short indicates Big or Little endian
250 target machine. It doesn't matter what the byte order of the host
251 machine is; these routines work for either. */
253 /* FIXME: Should these take a count argument?
254 Answer (gnu@cygnus.com): No, but perhaps they should be inline
255 functions in swap.h #ifdef __GNUC__.
256 Gprof them later and find out. */
265 These macros as used for reading and writing raw data in
266 sections; each access (except for bytes) is vectored through
267 the target format of the BFD and mangled accordingly. The
268 mangling performs any necessary endian translations and
269 removes alignment restrictions. Note that types accepted and
270 returned by these macros are identical so they can be swapped
271 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
272 to either <<bfd_get_32>> or <<bfd_get_64>>.
274 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
275 system without prototypes, the caller is responsible for making
276 sure that is true, with a cast if necessary. We don't cast
277 them in the macro definitions because that would prevent <<lint>>
278 or <<gcc -Wall>> from detecting sins such as passing a pointer.
279 To detect calling these with less than a <<bfd_vma>>, use
280 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
283 .{* Byte swapping macros for user section data. *}
285 .#define bfd_put_8(abfd, val, ptr) \
286 . ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val)))
287 .#define bfd_put_signed_8 \
289 .#define bfd_get_8(abfd, ptr) \
290 . (*(unsigned char *) (ptr) & 0xff)
291 .#define bfd_get_signed_8(abfd, ptr) \
292 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
294 .#define bfd_put_16(abfd, val, ptr) \
295 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
296 .#define bfd_put_signed_16 \
298 .#define bfd_get_16(abfd, ptr) \
299 . BFD_SEND(abfd, bfd_getx16, (ptr))
300 .#define bfd_get_signed_16(abfd, ptr) \
301 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
303 .#define bfd_put_32(abfd, val, ptr) \
304 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
305 .#define bfd_put_signed_32 \
307 .#define bfd_get_32(abfd, ptr) \
308 . BFD_SEND(abfd, bfd_getx32, (ptr))
309 .#define bfd_get_signed_32(abfd, ptr) \
310 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
312 .#define bfd_put_64(abfd, val, ptr) \
313 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
314 .#define bfd_put_signed_64 \
316 .#define bfd_get_64(abfd, ptr) \
317 . BFD_SEND(abfd, bfd_getx64, (ptr))
318 .#define bfd_get_signed_64(abfd, ptr) \
319 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
321 .#define bfd_get(bits, abfd, ptr) \
322 . ( (bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
323 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
324 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
325 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
326 . : (abort (), (bfd_vma) - 1))
328 .#define bfd_put(bits, abfd, val, ptr) \
329 . ( (bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
330 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
331 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
332 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
333 . : (abort (), (void) 0))
343 These macros have the same function as their <<bfd_get_x>>
344 brethren, except that they are used for removing information
345 for the header records of object files. Believe it or not,
346 some object files keep their header records in big endian
347 order and their data in little endian order.
349 .{* Byte swapping macros for file header data. *}
351 .#define bfd_h_put_8(abfd, val, ptr) \
352 . bfd_put_8 (abfd, val, ptr)
353 .#define bfd_h_put_signed_8(abfd, val, ptr) \
354 . bfd_put_8 (abfd, val, ptr)
355 .#define bfd_h_get_8(abfd, ptr) \
356 . bfd_get_8 (abfd, ptr)
357 .#define bfd_h_get_signed_8(abfd, ptr) \
358 . bfd_get_signed_8 (abfd, ptr)
360 .#define bfd_h_put_16(abfd, val, ptr) \
361 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
362 .#define bfd_h_put_signed_16 \
364 .#define bfd_h_get_16(abfd, ptr) \
365 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
366 .#define bfd_h_get_signed_16(abfd, ptr) \
367 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
369 .#define bfd_h_put_32(abfd, val, ptr) \
370 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
371 .#define bfd_h_put_signed_32 \
373 .#define bfd_h_get_32(abfd, ptr) \
374 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
375 .#define bfd_h_get_signed_32(abfd, ptr) \
376 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
378 .#define bfd_h_put_64(abfd, val, ptr) \
379 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
380 .#define bfd_h_put_signed_64 \
382 .#define bfd_h_get_64(abfd, ptr) \
383 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
384 .#define bfd_h_get_signed_64(abfd, ptr) \
385 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
387 .{* Refinements on the above, which should eventually go away. Save
388 . cluttering the source with (bfd_vma) and (bfd_byte *) casts. *}
390 .#define H_PUT_64(abfd, val, where) \
391 . bfd_h_put_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
393 .#define H_PUT_32(abfd, val, where) \
394 . bfd_h_put_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
396 .#define H_PUT_16(abfd, val, where) \
397 . bfd_h_put_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
399 .#define H_PUT_8 bfd_h_put_8
401 .#define H_PUT_S64(abfd, val, where) \
402 . bfd_h_put_signed_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
404 .#define H_PUT_S32(abfd, val, where) \
405 . bfd_h_put_signed_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
407 .#define H_PUT_S16(abfd, val, where) \
408 . bfd_h_put_signed_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
410 .#define H_PUT_S8 bfd_h_put_signed_8
412 .#define H_GET_64(abfd, where) \
413 . bfd_h_get_64 ((abfd), (bfd_byte *) (where))
415 .#define H_GET_32(abfd, where) \
416 . bfd_h_get_32 ((abfd), (bfd_byte *) (where))
418 .#define H_GET_16(abfd, where) \
419 . bfd_h_get_16 ((abfd), (bfd_byte *) (where))
421 .#define H_GET_8 bfd_h_get_8
423 .#define H_GET_S64(abfd, where) \
424 . bfd_h_get_signed_64 ((abfd), (bfd_byte *) (where))
426 .#define H_GET_S32(abfd, where) \
427 . bfd_h_get_signed_32 ((abfd), (bfd_byte *) (where))
429 .#define H_GET_S16(abfd, where) \
430 . bfd_h_get_signed_16 ((abfd), (bfd_byte *) (where))
432 .#define H_GET_S8 bfd_h_get_signed_8
436 /* Sign extension to bfd_signed_vma. */
437 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
438 #define COERCE32(x) \
439 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000))
440 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
441 #define COERCE64(x) \
442 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
446 register const bfd_byte
*addr
;
448 return (addr
[0] << 8) | addr
[1];
453 register const bfd_byte
*addr
;
455 return (addr
[1] << 8) | addr
[0];
459 bfd_getb_signed_16 (addr
)
460 register const bfd_byte
*addr
;
462 return COERCE16((addr
[0] << 8) | addr
[1]);
466 bfd_getl_signed_16 (addr
)
467 register const bfd_byte
*addr
;
469 return COERCE16((addr
[1] << 8) | addr
[0]);
473 bfd_putb16 (data
, addr
)
475 register bfd_byte
*addr
;
477 addr
[0] = (bfd_byte
) (data
>> 8);
478 addr
[1] = (bfd_byte
) data
;
482 bfd_putl16 (data
, addr
)
484 register bfd_byte
*addr
;
486 addr
[0] = (bfd_byte
) data
;
487 addr
[1] = (bfd_byte
) (data
>> 8);
492 register const bfd_byte
*addr
;
496 v
= (unsigned long) addr
[0] << 24;
497 v
|= (unsigned long) addr
[1] << 16;
498 v
|= (unsigned long) addr
[2] << 8;
499 v
|= (unsigned long) addr
[3];
505 register const bfd_byte
*addr
;
509 v
= (unsigned long) addr
[0];
510 v
|= (unsigned long) addr
[1] << 8;
511 v
|= (unsigned long) addr
[2] << 16;
512 v
|= (unsigned long) addr
[3] << 24;
517 bfd_getb_signed_32 (addr
)
518 register const bfd_byte
*addr
;
522 v
= (unsigned long) addr
[0] << 24;
523 v
|= (unsigned long) addr
[1] << 16;
524 v
|= (unsigned long) addr
[2] << 8;
525 v
|= (unsigned long) addr
[3];
530 bfd_getl_signed_32 (addr
)
531 register const bfd_byte
*addr
;
535 v
= (unsigned long) addr
[0];
536 v
|= (unsigned long) addr
[1] << 8;
537 v
|= (unsigned long) addr
[2] << 16;
538 v
|= (unsigned long) addr
[3] << 24;
544 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
549 high
= ((((((((addr
[0]) << 8) |
554 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
559 return high
<< 32 | low
;
568 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
572 high
= (((((((addr
[7] << 8) |
577 low
= ((((((((bfd_vma
)addr
[3] << 8) |
582 return high
<< 32 | low
;
591 bfd_getb_signed_64 (addr
)
592 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
597 high
= ((((((((addr
[0]) << 8) |
602 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
607 return COERCE64(high
<< 32 | low
);
615 bfd_getl_signed_64 (addr
)
616 register const bfd_byte
*addr ATTRIBUTE_UNUSED
;
620 high
= (((((((addr
[7] << 8) |
625 low
= ((((((((bfd_vma
)addr
[3] << 8) |
630 return COERCE64(high
<< 32 | low
);
638 bfd_putb32 (data
, addr
)
640 register bfd_byte
*addr
;
642 addr
[0] = (bfd_byte
) (data
>> 24);
643 addr
[1] = (bfd_byte
) (data
>> 16);
644 addr
[2] = (bfd_byte
) (data
>> 8);
645 addr
[3] = (bfd_byte
) data
;
649 bfd_putl32 (data
, addr
)
651 register bfd_byte
*addr
;
653 addr
[0] = (bfd_byte
) data
;
654 addr
[1] = (bfd_byte
) (data
>> 8);
655 addr
[2] = (bfd_byte
) (data
>> 16);
656 addr
[3] = (bfd_byte
) (data
>> 24);
660 bfd_putb64 (data
, addr
)
661 bfd_vma data ATTRIBUTE_UNUSED
;
662 register bfd_byte
*addr ATTRIBUTE_UNUSED
;
665 addr
[0] = (bfd_byte
) (data
>> (7*8));
666 addr
[1] = (bfd_byte
) (data
>> (6*8));
667 addr
[2] = (bfd_byte
) (data
>> (5*8));
668 addr
[3] = (bfd_byte
) (data
>> (4*8));
669 addr
[4] = (bfd_byte
) (data
>> (3*8));
670 addr
[5] = (bfd_byte
) (data
>> (2*8));
671 addr
[6] = (bfd_byte
) (data
>> (1*8));
672 addr
[7] = (bfd_byte
) (data
>> (0*8));
679 bfd_putl64 (data
, addr
)
680 bfd_vma data ATTRIBUTE_UNUSED
;
681 register bfd_byte
*addr ATTRIBUTE_UNUSED
;
684 addr
[7] = (bfd_byte
) (data
>> (7*8));
685 addr
[6] = (bfd_byte
) (data
>> (6*8));
686 addr
[5] = (bfd_byte
) (data
>> (5*8));
687 addr
[4] = (bfd_byte
) (data
>> (4*8));
688 addr
[3] = (bfd_byte
) (data
>> (3*8));
689 addr
[2] = (bfd_byte
) (data
>> (2*8));
690 addr
[1] = (bfd_byte
) (data
>> (1*8));
691 addr
[0] = (bfd_byte
) (data
>> (0*8));
698 bfd_put_bits (data
, addr
, bits
, big_p
)
711 for (i
= 0; i
< bytes
; i
++)
713 int index
= big_p
? bytes
- i
- 1 : i
;
715 addr
[index
] = (bfd_byte
) data
;
721 bfd_get_bits (addr
, bits
, big_p
)
735 for (i
= 0; i
< bytes
; i
++)
737 int index
= big_p
? i
: bytes
- i
- 1;
739 data
= (data
<< 8) | addr
[index
];
745 /* Default implementation */
748 _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
)
758 if (offset
+ count
> section
->_raw_size
)
760 bfd_set_error (bfd_error_invalid_operation
);
764 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
765 || bfd_bread (location
, count
, abfd
) != count
)
772 _bfd_generic_get_section_contents_in_window (abfd
, section
, w
, offset
, count
)
773 bfd
*abfd ATTRIBUTE_UNUSED
;
774 sec_ptr section ATTRIBUTE_UNUSED
;
775 bfd_window
*w ATTRIBUTE_UNUSED
;
776 file_ptr offset ATTRIBUTE_UNUSED
;
777 bfd_size_type count ATTRIBUTE_UNUSED
;
782 if (abfd
->xvec
->_bfd_get_section_contents
!= _bfd_generic_get_section_contents
)
784 /* We don't know what changes the bfd's get_section_contents
785 method may have to make. So punt trying to map the file
786 window, and let get_section_contents do its thing. */
787 /* @@ FIXME : If the internal window has a refcount of 1 and was
788 allocated with malloc instead of mmap, just reuse it. */
790 w
->i
= ((bfd_window_internal
*)
791 bfd_zmalloc ((bfd_size_type
) sizeof (bfd_window_internal
)));
794 w
->i
->data
= (PTR
) bfd_malloc (count
);
795 if (w
->i
->data
== NULL
)
803 w
->size
= w
->i
->size
= count
;
804 w
->data
= w
->i
->data
;
805 return bfd_get_section_contents (abfd
, section
, w
->data
, offset
, count
);
807 if (offset
+ count
> section
->_raw_size
808 || ! bfd_get_file_window (abfd
, section
->filepos
+ offset
, count
, w
,
817 /* This generic function can only be used in implementations where creating
818 NEW sections is disallowed. It is useful in patching existing sections
819 in read-write files, though. See other set_section_contents functions
820 to see why it doesn't work for new sections. */
822 _bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
832 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
833 || bfd_bwrite (location
, count
, abfd
) != count
)
844 unsigned int bfd_log2 (bfd_vma x);
847 Return the log base 2 of the value supplied, rounded up. E.g., an
848 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
855 unsigned int result
= 0;
857 while ((x
= (x
>> 1)) != 0)
863 bfd_generic_is_local_label_name (abfd
, name
)
867 char locals_prefix
= (bfd_get_symbol_leading_char (abfd
) == '_') ? 'L' : '.';
869 return name
[0] == locals_prefix
;
872 /* Can be used from / for bfd_merge_private_bfd_data to check that
873 endianness matches between input and output file. Returns
874 TRUE for a match, otherwise returns FALSE and emits an error. */
876 _bfd_generic_verify_endian_match (ibfd
, obfd
)
880 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
881 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
882 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
886 if (bfd_big_endian (ibfd
))
887 msg
= _("%s: compiled for a big endian system and target is little endian");
889 msg
= _("%s: compiled for a little endian system and target is big endian");
891 (*_bfd_error_handler
) (msg
, bfd_archive_filename (ibfd
));
893 bfd_set_error (bfd_error_wrong_format
);
900 /* Give a warning at runtime if someone compiles code which calls
904 warn_deprecated (what
, file
, line
, func
)
910 /* Poor man's tracking of functions we've already warned about. */
911 static size_t mask
= 0;
913 if (~(size_t) func
& ~mask
)
915 /* Note: separate sentences in order to allow
916 for translation into other languages. */
918 fprintf (stderr
, _("Deprecated %s called at %s line %d in %s\n"),
919 what
, file
, line
, func
);
921 fprintf (stderr
, _("Deprecated %s called\n"), what
);
922 mask
|= ~(size_t) func
;