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
37 _bfd_dummy_new_section_hook (ignore
, ignore_newsect
)
39 asection
*ignore_newsect
;
62 bfd_nullvoidptr (ignore
)
93 _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd
, ignore_exec_bfd
)
97 bfd_error
= invalid_operation
;
101 /* of course you can't initialize a function to be the same as another, grr */
105 _bfd_dummy_core_file_failing_command (ignore_abfd
)
113 _bfd_dummy_core_file_failing_signal (ignore_abfd
)
121 _bfd_dummy_target (ignore_abfd
)
129 /* allocate and clear storage */
135 char *ptr
= (char *) malloc ((size_t)size
);
137 if ((ptr
!= NULL
) && (size
!= 0))
138 memset(ptr
,0, (size_t) size
);
142 #endif /* bfd_zmalloc */
149 PTR bfd_xmalloc (bfd_size_type size);
152 Like <<malloc>>, but exit if no more memory.
156 /** There is major inconsistency in how running out of memory is handled.
157 Some routines return a NULL, and set bfd_error to no_memory.
158 However, obstack routines can't do this ... */
165 static CONST
char no_memory_message
[] = "Virtual memory exhausted!\n";
167 if (size
== 0) size
= 1;
168 ptr
= (PTR
)malloc((size_t) size
);
171 write (2, no_memory_message
, sizeof(no_memory_message
)-1);
179 bfd_xmalloc_by_size_t
182 PTR bfd_xmalloc_by_size_t (size_t size);
185 Like <<malloc>>, but exit if no more memory.
186 Uses <<size_t>>, so it's suitable for use as <<obstack_chunk_alloc>>.
189 bfd_xmalloc_by_size_t (size
)
192 return bfd_xmalloc ((bfd_size_type
) size
);
198 /* Note that archive entries don't have streams; they share their parent's.
199 This allows someone to play with the iostream behind BFD's back.
201 Also, note that the origin pointer points to the beginning of a file's
202 contents (0 for non-archive elements). For archive entries this is the
203 first octet in the file, NOT the beginning of the archive header. */
207 real_read (where
, a
,b
, file
)
213 return fread(where
, a
,b
,file
);
217 bfd_read (ptr
, size
, nitems
, abfd
)
220 bfd_size_type nitems
;
224 nread
= real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
225 #ifdef FILE_OFFSET_IS_CHAR_INDEX
227 abfd
->where
+= nread
;
233 bfd_write (ptr
, size
, nitems
, abfd
)
236 bfd_size_type nitems
;
239 int nwrote
= fwrite (ptr
, 1, (int) (size
* nitems
), bfd_cache_lookup (abfd
));
240 #ifdef FILE_OFFSET_IS_CHAR_INDEX
242 abfd
->where
+= nwrote
;
244 if (nwrote
!= size
* nitems
)
250 bfd_error
= system_call_error
;
257 bfd_write_bigendian_4byte_int
260 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
263 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
264 endian order regardless of what else is going on. This is useful in
269 bfd_write_bigendian_4byte_int (abfd
, i
)
274 bfd_putb32(i
, buffer
);
275 bfd_write((PTR
)buffer
, 4, 1, abfd
);
284 ptr
= ftell (bfd_cache_lookup(abfd
));
286 if (abfd
->my_archive
)
296 return fflush (bfd_cache_lookup(abfd
));
300 bfd_stat (abfd
, statbuf
)
302 struct stat
*statbuf
;
304 return fstat (fileno(bfd_cache_lookup(abfd
)), statbuf
);
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_error
= system_call_error
;
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_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_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 (((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
)