1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 1991, 1992, 1993 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 DEFUN(_bfd_dummy_new_section_hook
,(ignore
, ignore_newsect
),
39 asection
*ignore_newsect
)
46 DEFUN(bfd_false
,(ignore
),
54 DEFUN(bfd_true
,(ignore
),
62 DEFUN(bfd_nullvoidptr
,(ignore
),
78 DEFUN(bfd_0u
,(ignore
),
86 DEFUN(bfd_void
,(ignore
),
93 DEFUN(_bfd_dummy_core_file_matches_executable_p
,(ignore_core_bfd
, ignore_exec_bfd
),
94 bfd
*ignore_core_bfd AND
97 bfd_error
= invalid_operation
;
101 /* of course you can't initialize a function to be the same as another, grr */
105 DEFUN(_bfd_dummy_core_file_failing_command
,(ignore_abfd
),
113 DEFUN(_bfd_dummy_core_file_failing_signal
,(ignore_abfd
),
121 DEFUN(_bfd_dummy_target
,(ignore_abfd
),
127 /** zalloc -- 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
);
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 ... */
161 DEFUN(PTR bfd_xmalloc
,(size
),
164 static CONST
char no_memory_message
[] = "Virtual memory exhausted!\n";
166 if (size
== 0) size
= 1;
167 ptr
= (PTR
)malloc((size_t) size
);
170 write (2, no_memory_message
, sizeof(no_memory_message
)-1);
178 bfd_xmalloc_by_size_t
181 PTR bfd_xmalloc_by_size_t (size_t size);
184 Like <<malloc>>, but exit if no more memory.
185 Uses <<size_t>>, so it's suitable for use as <<obstack_chunk_alloc>>.
188 DEFUN(bfd_xmalloc_by_size_t
, (size
),
191 return bfd_xmalloc ((bfd_size_type
) size
);
197 /* Note that archive entries don't have streams; they share their parent's.
198 This allows someone to play with the iostream behind BFD's back.
200 Also, note that the origin pointer points to the beginning of a file's
201 contents (0 for non-archive elements). For archive entries this is the
202 first octet in the file, NOT the beginning of the archive header. */
205 int DEFUN(real_read
,(where
, a
,b
, file
),
211 return fread(where
, a
,b
,file
);
214 DEFUN(bfd_read
,(ptr
, size
, nitems
, abfd
),
216 bfd_size_type size AND
217 bfd_size_type nitems AND
221 nread
= real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
222 #ifdef FILE_OFFSET_IS_CHAR_INDEX
224 abfd
->where
+= nread
;
230 bfd_write (ptr
, size
, nitems
, abfd
)
233 bfd_size_type nitems
;
236 int nwrote
= fwrite (ptr
, 1, (int) (size
* nitems
), bfd_cache_lookup (abfd
));
237 #ifdef FILE_OFFSET_IS_CHAR_INDEX
239 abfd
->where
+= nwrote
;
241 if (nwrote
!= size
* nitems
)
247 bfd_error
= system_call_error
;
254 bfd_write_bigendian_4byte_int
257 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
260 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
261 endian order regardless of what else is going on. This is useful in
266 DEFUN(bfd_write_bigendian_4byte_int
,(abfd
, i
),
271 bfd_putb32(i
, buffer
);
272 bfd_write((PTR
)buffer
, 4, 1, abfd
);
276 DEFUN(bfd_tell
,(abfd
),
281 ptr
= ftell (bfd_cache_lookup(abfd
));
283 if (abfd
->my_archive
)
290 DEFUN(bfd_flush
,(abfd
),
293 return fflush (bfd_cache_lookup(abfd
));
297 DEFUN(bfd_stat
,(abfd
, statbuf
),
299 struct stat
*statbuf
)
301 return fstat (fileno(bfd_cache_lookup(abfd
)), statbuf
);
305 DEFUN(bfd_seek
,(abfd
, position
, direction
),
307 CONST file_ptr position AND
312 file_ptr file_position
;
313 /* For the time being, a BFD may not seek to it's end. The problem
314 is that we don't easily have a way to recognize the end of an
315 element in an archive. */
317 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
319 if (direction
== SEEK_CUR
&& position
== 0)
321 #ifdef FILE_OFFSET_IS_CHAR_INDEX
322 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
325 /* Explanation for this code: I'm only about 95+% sure that the above
326 conditions are sufficient and that all i/o calls are properly
327 adjusting the `where' field. So this is sort of an `assert'
328 that the `where' field is correct. If we can go a while without
329 tripping the abort, we can probably safely disable this code,
330 so that the real optimizations happen. */
331 file_ptr where_am_i_now
;
332 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
333 if (abfd
->my_archive
)
334 where_am_i_now
-= abfd
->origin
;
335 if (where_am_i_now
!= abfd
->where
)
338 if (direction
== SEEK_SET
&& position
== abfd
->where
)
343 /* We need something smarter to optimize access to archives.
344 Currently, anything inside an archive is read via the file
345 handle for the archive. Which means that a bfd_seek on one
346 component affects the `current position' in the archive, as
347 well as in any other component.
349 It might be sufficient to put a spike through the cache
350 abstraction, and look to the archive for the file position,
351 but I think we should try for something cleaner.
353 In the meantime, no optimization for archives. */
357 f
= bfd_cache_lookup (abfd
);
358 file_position
= position
;
359 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
360 file_position
+= abfd
->origin
;
362 result
= fseek (f
, file_position
, direction
);
366 /* Force redetermination of `where' field. */
368 bfd_error
= system_call_error
;
372 #ifdef FILE_OFFSET_IS_CHAR_INDEX
373 /* Adjust `where' field. */
374 if (direction
== SEEK_SET
)
375 abfd
->where
= position
;
377 abfd
->where
+= position
;
383 /** Make a string table */
386 Add string to table pointed to by table, at location starting with free_ptr.
387 resizes the table if necessary (if it's NULL, creates it, ignoring
388 table_length). Updates free_ptr, table, table_length */
391 DEFUN(bfd_add_to_string_table
,(table
, new_string
, table_length
, free_ptr
),
394 unsigned int *table_length AND
397 size_t string_length
= strlen (new_string
) + 1; /* include null here */
399 size_t space_length
= *table_length
;
400 unsigned int offset
= (base
? *free_ptr
- base
: 0);
403 /* Avoid a useless regrow if we can (but of course we still
405 space_length
= (string_length
< DEFAULT_STRING_SPACE_SIZE
?
406 DEFAULT_STRING_SPACE_SIZE
: string_length
+1);
407 base
= zalloc ((bfd_size_type
) space_length
);
410 bfd_error
= no_memory
;
415 if ((size_t)(offset
+ string_length
) >= space_length
) {
416 /* Make sure we will have enough space */
417 while ((size_t)(offset
+ string_length
) >= space_length
)
418 space_length
+= space_length
/2; /* grow by 50% */
420 base
= (char *) realloc (base
, space_length
);
422 bfd_error
= no_memory
;
428 memcpy (base
+ offset
, new_string
, string_length
);
430 *table_length
= space_length
;
431 *free_ptr
= base
+ offset
+ string_length
;
436 /** The do-it-yourself (byte) sex-change kit */
438 /* The middle letter e.g. get<b>short indicates Big or Little endian
439 target machine. It doesn't matter what the byte order of the host
440 machine is; these routines work for either. */
442 /* FIXME: Should these take a count argument?
443 Answer (gnu@cygnus.com): No, but perhaps they should be inline
444 functions in swap.h #ifdef __GNUC__.
445 Gprof them later and find out. */
454 These macros as used for reading and writing raw data in
455 sections; each access (except for bytes) is vectored through
456 the target format of the BFD and mangled accordingly. The
457 mangling performs any necessary endian translations and
458 removes alignment restrictions. Note that types accepted and
459 returned by these macros are identical so they can be swapped
460 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
461 to either <<bfd_get_32>> or <<bfd_get_64>>.
463 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
464 system without prototypes, the caller is responsible for making
465 sure that is true, with a cast if necessary. We don't cast
466 them in the macro definitions because that would prevent <<lint>>
467 or <<gcc -Wall>> from detecting sins such as passing a pointer.
468 To detect calling these with less than a <<bfd_vma>>, use
469 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
472 .{* Byte swapping macros for user section data. *}
474 .#define bfd_put_8(abfd, val, ptr) \
475 . (*((unsigned char *)(ptr)) = (unsigned char)(val))
476 .#define bfd_put_signed_8 \
478 .#define bfd_get_8(abfd, ptr) \
479 . (*(unsigned char *)(ptr))
480 .#define bfd_get_signed_8(abfd, ptr) \
481 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
483 .#define bfd_put_16(abfd, val, ptr) \
484 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
485 .#define bfd_put_signed_16 \
487 .#define bfd_get_16(abfd, ptr) \
488 . BFD_SEND(abfd, bfd_getx16, (ptr))
489 .#define bfd_get_signed_16(abfd, ptr) \
490 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
492 .#define bfd_put_32(abfd, val, ptr) \
493 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
494 .#define bfd_put_signed_32 \
496 .#define bfd_get_32(abfd, ptr) \
497 . BFD_SEND(abfd, bfd_getx32, (ptr))
498 .#define bfd_get_signed_32(abfd, ptr) \
499 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
501 .#define bfd_put_64(abfd, val, ptr) \
502 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
503 .#define bfd_put_signed_64 \
505 .#define bfd_get_64(abfd, ptr) \
506 . BFD_SEND(abfd, bfd_getx64, (ptr))
507 .#define bfd_get_signed_64(abfd, ptr) \
508 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
518 These macros have the same function as their <<bfd_get_x>>
519 bretheren, except that they are used for removing information
520 for the header records of object files. Believe it or not,
521 some object files keep their header records in big endian
522 order and their data in little endian order.
524 .{* Byte swapping macros for file header data. *}
526 .#define bfd_h_put_8(abfd, val, ptr) \
527 . bfd_put_8 (abfd, val, ptr)
528 .#define bfd_h_put_signed_8(abfd, val, ptr) \
529 . bfd_put_8 (abfd, val, ptr)
530 .#define bfd_h_get_8(abfd, ptr) \
531 . bfd_get_8 (abfd, ptr)
532 .#define bfd_h_get_signed_8(abfd, ptr) \
533 . bfd_get_signed_8 (abfd, ptr)
535 .#define bfd_h_put_16(abfd, val, ptr) \
536 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
537 .#define bfd_h_put_signed_16 \
539 .#define bfd_h_get_16(abfd, ptr) \
540 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
541 .#define bfd_h_get_signed_16(abfd, ptr) \
542 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
544 .#define bfd_h_put_32(abfd, val, ptr) \
545 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
546 .#define bfd_h_put_signed_32 \
548 .#define bfd_h_get_32(abfd, ptr) \
549 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
550 .#define bfd_h_get_signed_32(abfd, ptr) \
551 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
553 .#define bfd_h_put_64(abfd, val, ptr) \
554 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
555 .#define bfd_h_put_signed_64 \
557 .#define bfd_h_get_64(abfd, ptr) \
558 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
559 .#define bfd_h_get_signed_64(abfd, ptr) \
560 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
564 /* Sign extension to bfd_signed_vma. */
565 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
566 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
567 #define EIGHT_GAZILLION (((HOST_64_BIT)0x80000000) << 32)
568 #define COERCE64(x) \
569 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
572 DEFUN(bfd_getb16
,(addr
),
573 register bfd_byte
*addr
)
575 return (addr
[0] << 8) | addr
[1];
579 DEFUN(bfd_getl16
,(addr
),
580 register bfd_byte
*addr
)
582 return (addr
[1] << 8) | addr
[0];
586 DEFUN(bfd_getb_signed_16
,(addr
),
587 register bfd_byte
*addr
)
589 return COERCE16((addr
[0] << 8) | addr
[1]);
593 DEFUN(bfd_getl_signed_16
,(addr
),
594 register bfd_byte
*addr
)
596 return COERCE16((addr
[1] << 8) | addr
[0]);
600 DEFUN(bfd_putb16
,(data
, addr
),
602 register bfd_byte
*addr
)
604 addr
[0] = (bfd_byte
)(data
>> 8);
605 addr
[1] = (bfd_byte
)data
;
609 DEFUN(bfd_putl16
,(data
, addr
),
611 register bfd_byte
*addr
)
613 addr
[0] = (bfd_byte
)data
;
614 addr
[1] = (bfd_byte
)(data
>> 8);
619 register bfd_byte
*addr
;
621 return (((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
622 | addr
[2]) << 8 | addr
[3];
627 register bfd_byte
*addr
;
629 return (((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
630 | addr
[1]) << 8 | addr
[0];
634 bfd_getb_signed_32 (addr
)
635 register bfd_byte
*addr
;
637 return COERCE32((((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
638 | addr
[2]) << 8 | addr
[3]);
642 bfd_getl_signed_32 (addr
)
643 register bfd_byte
*addr
;
645 return COERCE32((((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
646 | addr
[1]) << 8 | addr
[0]);
650 DEFUN(bfd_getb64
,(addr
),
651 register bfd_byte
*addr
)
656 high
= ((((((((addr
[0]) << 8) |
661 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
666 return high
<< 32 | low
;
675 DEFUN(bfd_getl64
,(addr
),
676 register bfd_byte
*addr
)
681 high
= (((((((addr
[7] << 8) |
686 low
= ((((((((bfd_vma
)addr
[3] << 8) |
691 return high
<< 32 | low
;
700 DEFUN(bfd_getb_signed_64
,(addr
),
701 register bfd_byte
*addr
)
706 high
= ((((((((addr
[0]) << 8) |
711 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
716 return COERCE64(high
<< 32 | low
);
725 DEFUN(bfd_getl_signed_64
,(addr
),
726 register bfd_byte
*addr
)
731 high
= (((((((addr
[7] << 8) |
736 low
= ((((((((bfd_vma
)addr
[3] << 8) |
741 return COERCE64(high
<< 32 | low
);
750 DEFUN(bfd_putb32
,(data
, addr
),
752 register bfd_byte
*addr
)
754 addr
[0] = (bfd_byte
)(data
>> 24);
755 addr
[1] = (bfd_byte
)(data
>> 16);
756 addr
[2] = (bfd_byte
)(data
>> 8);
757 addr
[3] = (bfd_byte
)data
;
761 DEFUN(bfd_putl32
,(data
, addr
),
763 register bfd_byte
*addr
)
765 addr
[0] = (bfd_byte
)data
;
766 addr
[1] = (bfd_byte
)(data
>> 8);
767 addr
[2] = (bfd_byte
)(data
>> 16);
768 addr
[3] = (bfd_byte
)(data
>> 24);
771 DEFUN(bfd_putb64
,(data
, addr
),
773 register bfd_byte
*addr
)
776 addr
[0] = (bfd_byte
)(data
>> (7*8));
777 addr
[1] = (bfd_byte
)(data
>> (6*8));
778 addr
[2] = (bfd_byte
)(data
>> (5*8));
779 addr
[3] = (bfd_byte
)(data
>> (4*8));
780 addr
[4] = (bfd_byte
)(data
>> (3*8));
781 addr
[5] = (bfd_byte
)(data
>> (2*8));
782 addr
[6] = (bfd_byte
)(data
>> (1*8));
783 addr
[7] = (bfd_byte
)(data
>> (0*8));
791 DEFUN(bfd_putl64
,(data
, addr
),
793 register bfd_byte
*addr
)
796 addr
[7] = (bfd_byte
)(data
>> (7*8));
797 addr
[6] = (bfd_byte
)(data
>> (6*8));
798 addr
[5] = (bfd_byte
)(data
>> (5*8));
799 addr
[4] = (bfd_byte
)(data
>> (4*8));
800 addr
[3] = (bfd_byte
)(data
>> (3*8));
801 addr
[2] = (bfd_byte
)(data
>> (2*8));
802 addr
[1] = (bfd_byte
)(data
>> (1*8));
803 addr
[0] = (bfd_byte
)(data
>> (0*8));
811 /* Default implementation */
814 DEFUN(bfd_generic_get_section_contents
, (abfd
, section
, location
, offset
, count
),
823 if ((bfd_size_type
)(offset
+count
) > section
->_raw_size
824 || bfd_seek(abfd
, (file_ptr
)(section
->filepos
+ offset
), SEEK_SET
) == -1
825 || bfd_read(location
, (bfd_size_type
)1, count
, abfd
) != count
)
826 return (false); /* on error */
830 /* This generic function can only be used in implementations where creating
831 NEW sections is disallowed. It is useful in patching existing sections
832 in read-write files, though. See other set_section_contents functions
833 to see why it doesn't work for new sections. */
835 bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
845 if (offset
+ count
> bfd_get_section_size_after_reloc (section
))
847 bfd_error
= bad_value
;
851 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) == -1
852 || bfd_write (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
863 unsigned int bfd_log2(bfd_vma x);
866 Return the log base 2 of the value supplied, rounded up. E.g., an
867 @var{x} of 1025 returns 11.
875 while ( (bfd_vma
)(1<< result
) < x
)