1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
30 These routines are used within BFD.
31 They are not intended for export, but are documented here for
35 /* A routine which is used in target vectors for unsupported
43 bfd_set_error (bfd_error_invalid_operation
);
47 /* A routine which is used in target vectors for supported operations
48 which do not actually do anything. */
58 /* A routine which is used in target vectors for unsupported
59 operations which return a pointer value. */
63 bfd_nullvoidptr (ignore
)
66 bfd_set_error (bfd_error_invalid_operation
);
94 /* A routine which is used in target vectors for unsupported
95 operations which return -1 on error. */
102 bfd_set_error (bfd_error_invalid_operation
);
115 _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd
, ignore_exec_bfd
)
116 bfd
*ignore_core_bfd
;
117 bfd
*ignore_exec_bfd
;
119 bfd_set_error (bfd_error_invalid_operation
);
123 /* Routine to handle core_file_failing_command entry point for targets
124 without core file support. */
128 _bfd_nocore_core_file_failing_command (ignore_abfd
)
131 bfd_set_error (bfd_error_invalid_operation
);
135 /* Routine to handle core_file_failing_signal entry point for targets
136 without core file support. */
140 _bfd_nocore_core_file_failing_signal (ignore_abfd
)
143 bfd_set_error (bfd_error_invalid_operation
);
149 _bfd_dummy_target (ignore_abfd
)
157 /* allocate and clear storage */
163 char *ptr
= (char *) malloc ((size_t) size
);
166 memset(ptr
, 0, (size_t) size
);
170 #endif /* bfd_zmalloc */
175 /* Note that archive entries don't have streams; they share their parent's.
176 This allows someone to play with the iostream behind BFD's back.
178 Also, note that the origin pointer points to the beginning of a file's
179 contents (0 for non-archive elements). For archive entries this is the
180 first octet in the file, NOT the beginning of the archive header. */
184 real_read (where
, a
,b
, file
)
190 return fread(where
, a
,b
,file
);
193 /* Return value is amount read (FIXME: how are errors and end of file dealt
194 with? We never call bfd_set_error, which is probably a mistake). */
197 bfd_read (ptr
, size
, nitems
, abfd
)
200 bfd_size_type nitems
;
204 nread
= real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
205 #ifdef FILE_OFFSET_IS_CHAR_INDEX
207 abfd
->where
+= nread
;
210 /* Set bfd_error if we did not read as much data as we expected.
212 If the read failed due to an error set the bfd_error_system_call,
213 else set bfd_error_file_truncated.
215 A BFD backend may wish to override bfd_error_file_truncated to
216 provide something more useful (eg. no_symbols or wrong_format). */
217 if (nread
< (int)(size
* nitems
))
219 if (ferror (bfd_cache_lookup (abfd
)))
220 bfd_set_error (bfd_error_system_call
);
222 bfd_set_error (bfd_error_file_truncated
);
229 bfd_write (ptr
, size
, nitems
, abfd
)
232 bfd_size_type nitems
;
235 int nwrote
= fwrite (ptr
, 1, (int) (size
* nitems
), bfd_cache_lookup (abfd
));
236 #ifdef FILE_OFFSET_IS_CHAR_INDEX
238 abfd
->where
+= nwrote
;
240 if (nwrote
!= size
* nitems
)
246 bfd_set_error (bfd_error_system_call
);
253 bfd_write_bigendian_4byte_int
256 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
259 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
260 endian order regardless of what else is going on. This is useful in
265 bfd_write_bigendian_4byte_int (abfd
, i
)
270 bfd_putb32(i
, buffer
);
271 if (bfd_write((PTR
)buffer
, 4, 1, abfd
) != 4)
281 ptr
= ftell (bfd_cache_lookup(abfd
));
283 if (abfd
->my_archive
)
293 return fflush (bfd_cache_lookup(abfd
));
297 bfd_stat (abfd
, statbuf
)
299 struct stat
*statbuf
;
301 return fstat (fileno(bfd_cache_lookup(abfd
)), statbuf
);
304 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
305 can retrieve the error code). */
308 bfd_seek (abfd
, position
, direction
)
310 CONST file_ptr position
;
315 file_ptr file_position
;
316 /* For the time being, a BFD may not seek to it's end. The problem
317 is that we don't easily have a way to recognize the end of an
318 element in an archive. */
320 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
322 if (direction
== SEEK_CUR
&& position
== 0)
324 #ifdef FILE_OFFSET_IS_CHAR_INDEX
325 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
328 /* Explanation for this code: I'm only about 95+% sure that the above
329 conditions are sufficient and that all i/o calls are properly
330 adjusting the `where' field. So this is sort of an `assert'
331 that the `where' field is correct. If we can go a while without
332 tripping the abort, we can probably safely disable this code,
333 so that the real optimizations happen. */
334 file_ptr where_am_i_now
;
335 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
336 if (abfd
->my_archive
)
337 where_am_i_now
-= abfd
->origin
;
338 if (where_am_i_now
!= abfd
->where
)
341 if (direction
== SEEK_SET
&& position
== abfd
->where
)
346 /* We need something smarter to optimize access to archives.
347 Currently, anything inside an archive is read via the file
348 handle for the archive. Which means that a bfd_seek on one
349 component affects the `current position' in the archive, as
350 well as in any other component.
352 It might be sufficient to put a spike through the cache
353 abstraction, and look to the archive for the file position,
354 but I think we should try for something cleaner.
356 In the meantime, no optimization for archives. */
360 f
= bfd_cache_lookup (abfd
);
361 file_position
= position
;
362 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
363 file_position
+= abfd
->origin
;
365 result
= fseek (f
, file_position
, direction
);
369 /* Force redetermination of `where' field. */
371 bfd_set_error (bfd_error_system_call
);
375 #ifdef FILE_OFFSET_IS_CHAR_INDEX
376 /* Adjust `where' field. */
377 if (direction
== SEEK_SET
)
378 abfd
->where
= position
;
380 abfd
->where
+= position
;
386 /** Make a string table */
389 Add string to table pointed to by table, at location starting with free_ptr.
390 resizes the table if necessary (if it's NULL, creates it, ignoring
391 table_length). Updates free_ptr, table, table_length */
394 bfd_add_to_string_table (table
, new_string
, table_length
, free_ptr
)
397 unsigned int *table_length
;
400 size_t string_length
= strlen (new_string
) + 1; /* include null here */
402 size_t space_length
= *table_length
;
403 unsigned int offset
= (base
? *free_ptr
- base
: 0);
406 /* Avoid a useless regrow if we can (but of course we still
407 take it next time). */
408 space_length
= (string_length
< DEFAULT_STRING_SPACE_SIZE
?
409 DEFAULT_STRING_SPACE_SIZE
: string_length
+1);
410 base
= bfd_zmalloc ((bfd_size_type
) space_length
);
413 bfd_set_error (bfd_error_no_memory
);
418 if ((size_t)(offset
+ string_length
) >= space_length
) {
419 /* Make sure we will have enough space */
420 while ((size_t)(offset
+ string_length
) >= space_length
)
421 space_length
+= space_length
/2; /* grow by 50% */
423 base
= (char *) realloc (base
, space_length
);
425 bfd_set_error (bfd_error_no_memory
);
431 memcpy (base
+ offset
, new_string
, string_length
);
433 *table_length
= space_length
;
434 *free_ptr
= base
+ offset
+ string_length
;
439 /** The do-it-yourself (byte) sex-change kit */
441 /* The middle letter e.g. get<b>short indicates Big or Little endian
442 target machine. It doesn't matter what the byte order of the host
443 machine is; these routines work for either. */
445 /* FIXME: Should these take a count argument?
446 Answer (gnu@cygnus.com): No, but perhaps they should be inline
447 functions in swap.h #ifdef __GNUC__.
448 Gprof them later and find out. */
457 These macros as used for reading and writing raw data in
458 sections; each access (except for bytes) is vectored through
459 the target format of the BFD and mangled accordingly. The
460 mangling performs any necessary endian translations and
461 removes alignment restrictions. Note that types accepted and
462 returned by these macros are identical so they can be swapped
463 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
464 to either <<bfd_get_32>> or <<bfd_get_64>>.
466 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
467 system without prototypes, the caller is responsible for making
468 sure that is true, with a cast if necessary. We don't cast
469 them in the macro definitions because that would prevent <<lint>>
470 or <<gcc -Wall>> from detecting sins such as passing a pointer.
471 To detect calling these with less than a <<bfd_vma>>, use
472 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
475 .{* Byte swapping macros for user section data. *}
477 .#define bfd_put_8(abfd, val, ptr) \
478 . (*((unsigned char *)(ptr)) = (unsigned char)(val))
479 .#define bfd_put_signed_8 \
481 .#define bfd_get_8(abfd, ptr) \
482 . (*(unsigned char *)(ptr))
483 .#define bfd_get_signed_8(abfd, ptr) \
484 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
486 .#define bfd_put_16(abfd, val, ptr) \
487 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
488 .#define bfd_put_signed_16 \
490 .#define bfd_get_16(abfd, ptr) \
491 . BFD_SEND(abfd, bfd_getx16, (ptr))
492 .#define bfd_get_signed_16(abfd, ptr) \
493 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
495 .#define bfd_put_32(abfd, val, ptr) \
496 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
497 .#define bfd_put_signed_32 \
499 .#define bfd_get_32(abfd, ptr) \
500 . BFD_SEND(abfd, bfd_getx32, (ptr))
501 .#define bfd_get_signed_32(abfd, ptr) \
502 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
504 .#define bfd_put_64(abfd, val, ptr) \
505 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
506 .#define bfd_put_signed_64 \
508 .#define bfd_get_64(abfd, ptr) \
509 . BFD_SEND(abfd, bfd_getx64, (ptr))
510 .#define bfd_get_signed_64(abfd, ptr) \
511 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
521 These macros have the same function as their <<bfd_get_x>>
522 bretheren, except that they are used for removing information
523 for the header records of object files. Believe it or not,
524 some object files keep their header records in big endian
525 order and their data in little endian order.
527 .{* Byte swapping macros for file header data. *}
529 .#define bfd_h_put_8(abfd, val, ptr) \
530 . bfd_put_8 (abfd, val, ptr)
531 .#define bfd_h_put_signed_8(abfd, val, ptr) \
532 . bfd_put_8 (abfd, val, ptr)
533 .#define bfd_h_get_8(abfd, ptr) \
534 . bfd_get_8 (abfd, ptr)
535 .#define bfd_h_get_signed_8(abfd, ptr) \
536 . bfd_get_signed_8 (abfd, ptr)
538 .#define bfd_h_put_16(abfd, val, ptr) \
539 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
540 .#define bfd_h_put_signed_16 \
542 .#define bfd_h_get_16(abfd, ptr) \
543 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
544 .#define bfd_h_get_signed_16(abfd, ptr) \
545 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
547 .#define bfd_h_put_32(abfd, val, ptr) \
548 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
549 .#define bfd_h_put_signed_32 \
551 .#define bfd_h_get_32(abfd, ptr) \
552 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
553 .#define bfd_h_get_signed_32(abfd, ptr) \
554 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
556 .#define bfd_h_put_64(abfd, val, ptr) \
557 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
558 .#define bfd_h_put_signed_64 \
560 .#define bfd_h_get_64(abfd, ptr) \
561 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
562 .#define bfd_h_get_signed_64(abfd, ptr) \
563 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
567 /* Sign extension to bfd_signed_vma. */
568 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
569 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
570 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
571 #define COERCE64(x) \
572 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
576 register const bfd_byte
*addr
;
578 return (addr
[0] << 8) | addr
[1];
583 register const bfd_byte
*addr
;
585 return (addr
[1] << 8) | addr
[0];
589 bfd_getb_signed_16 (addr
)
590 register const bfd_byte
*addr
;
592 return COERCE16((addr
[0] << 8) | addr
[1]);
596 bfd_getl_signed_16 (addr
)
597 register const bfd_byte
*addr
;
599 return COERCE16((addr
[1] << 8) | addr
[0]);
603 bfd_putb16 (data
, addr
)
605 register bfd_byte
*addr
;
607 addr
[0] = (bfd_byte
)(data
>> 8);
608 addr
[1] = (bfd_byte
)data
;
612 bfd_putl16 (data
, addr
)
614 register bfd_byte
*addr
;
616 addr
[0] = (bfd_byte
)data
;
617 addr
[1] = (bfd_byte
)(data
>> 8);
622 register const bfd_byte
*addr
;
624 return (((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
625 | addr
[2]) << 8 | addr
[3];
630 register const bfd_byte
*addr
;
632 return (((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
633 | addr
[1]) << 8 | addr
[0];
637 bfd_getb_signed_32 (addr
)
638 register const bfd_byte
*addr
;
640 return COERCE32((((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
641 | addr
[2]) << 8 | addr
[3]);
645 bfd_getl_signed_32 (addr
)
646 register const bfd_byte
*addr
;
648 return COERCE32((((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
649 | addr
[1]) << 8 | addr
[0]);
654 register const bfd_byte
*addr
;
659 high
= ((((((((addr
[0]) << 8) |
664 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
669 return high
<< 32 | low
;
678 register const bfd_byte
*addr
;
682 high
= (((((((addr
[7] << 8) |
687 low
= ((((((((bfd_vma
)addr
[3] << 8) |
692 return high
<< 32 | low
;
701 bfd_getb_signed_64 (addr
)
702 register const bfd_byte
*addr
;
707 high
= ((((((((addr
[0]) << 8) |
712 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
717 return COERCE64(high
<< 32 | low
);
725 bfd_getl_signed_64 (addr
)
726 register const bfd_byte
*addr
;
730 high
= (((((((addr
[7] << 8) |
735 low
= ((((((((bfd_vma
)addr
[3] << 8) |
740 return COERCE64(high
<< 32 | low
);
748 bfd_putb32 (data
, addr
)
750 register bfd_byte
*addr
;
752 addr
[0] = (bfd_byte
)(data
>> 24);
753 addr
[1] = (bfd_byte
)(data
>> 16);
754 addr
[2] = (bfd_byte
)(data
>> 8);
755 addr
[3] = (bfd_byte
)data
;
759 bfd_putl32 (data
, addr
)
761 register bfd_byte
*addr
;
763 addr
[0] = (bfd_byte
)data
;
764 addr
[1] = (bfd_byte
)(data
>> 8);
765 addr
[2] = (bfd_byte
)(data
>> 16);
766 addr
[3] = (bfd_byte
)(data
>> 24);
770 bfd_putb64 (data
, addr
)
772 register bfd_byte
*addr
;
775 addr
[0] = (bfd_byte
)(data
>> (7*8));
776 addr
[1] = (bfd_byte
)(data
>> (6*8));
777 addr
[2] = (bfd_byte
)(data
>> (5*8));
778 addr
[3] = (bfd_byte
)(data
>> (4*8));
779 addr
[4] = (bfd_byte
)(data
>> (3*8));
780 addr
[5] = (bfd_byte
)(data
>> (2*8));
781 addr
[6] = (bfd_byte
)(data
>> (1*8));
782 addr
[7] = (bfd_byte
)(data
>> (0*8));
789 bfd_putl64 (data
, addr
)
791 register bfd_byte
*addr
;
794 addr
[7] = (bfd_byte
)(data
>> (7*8));
795 addr
[6] = (bfd_byte
)(data
>> (6*8));
796 addr
[5] = (bfd_byte
)(data
>> (5*8));
797 addr
[4] = (bfd_byte
)(data
>> (4*8));
798 addr
[3] = (bfd_byte
)(data
>> (3*8));
799 addr
[2] = (bfd_byte
)(data
>> (2*8));
800 addr
[1] = (bfd_byte
)(data
>> (1*8));
801 addr
[0] = (bfd_byte
)(data
>> (0*8));
807 /* Default implementation */
810 _bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
)
819 if ((bfd_size_type
)(offset
+count
) > section
->_raw_size
820 || bfd_seek(abfd
, (file_ptr
)(section
->filepos
+ offset
), SEEK_SET
) == -1
821 || bfd_read(location
, (bfd_size_type
)1, count
, abfd
) != count
)
822 return (false); /* on error */
826 /* This generic function can only be used in implementations where creating
827 NEW sections is disallowed. It is useful in patching existing sections
828 in read-write files, though. See other set_section_contents functions
829 to see why it doesn't work for new sections. */
831 _bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
841 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) == -1
842 || bfd_write (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
853 unsigned int bfd_log2(bfd_vma x);
856 Return the log base 2 of the value supplied, rounded up. E.g., an
857 @var{x} of 1025 returns 11.
865 while ( (bfd_vma
)(1<< result
) < x
)
871 bfd_generic_is_local_label (abfd
, sym
)
875 char locals_prefix
= (bfd_get_symbol_leading_char (abfd
) == '_') ? 'L' : '.';
877 return (sym
->name
[0] == locals_prefix
);