1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 1991, 1992 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
36 DEFUN(_bfd_dummy_new_section_hook
,(ignore
, ignore_newsect
),
38 asection
*ignore_newsect
)
44 DEFUN(bfd_false
,(ignore
),
51 DEFUN(bfd_true
,(ignore
),
58 DEFUN(bfd_nullvoidptr
,(ignore
),
72 DEFUN(bfd_0u
,(ignore
),
79 DEFUN(bfd_void
,(ignore
),
85 DEFUN(_bfd_dummy_core_file_matches_executable_p
,(ignore_core_bfd
, ignore_exec_bfd
),
86 bfd
*ignore_core_bfd AND
89 bfd_error
= invalid_operation
;
93 /* of course you can't initialize a function to be the same as another, grr */
96 DEFUN(_bfd_dummy_core_file_failing_command
,(ignore_abfd
),
103 DEFUN(_bfd_dummy_core_file_failing_signal
,(ignore_abfd
),
110 DEFUN(_bfd_dummy_target
,(ignore_abfd
),
116 /** zalloc -- allocate and clear storage */
124 char *ptr
= (char *) malloc ((size_t)size
);
126 if ((ptr
!= NULL
) && (size
!= 0))
127 memset(ptr
,0, (size_t) size
);
138 PTR bfd_xmalloc (bfd_size_type size);
141 Like <<malloc>>, but exit if no more memory.
145 /** There is major inconsistency in how running out of memory is handled.
146 Some routines return a NULL, and set bfd_error to no_memory.
147 However, obstack routines can't do this ... */
150 DEFUN(PTR bfd_xmalloc
,(size
),
153 static CONST
char no_memory_message
[] = "Virtual memory exhausted!\n";
155 if (size
== 0) size
= 1;
156 ptr
= (PTR
)malloc((size_t) size
);
159 write (2, no_memory_message
, sizeof(no_memory_message
)-1);
167 bfd_xmalloc_by_size_t
170 PTR bfd_xmalloc_by_size_t (size_t size);
173 Like <<malloc>>, but exit if no more memory.
174 Uses <<size_t>>, so it's suitable for use as <<obstack_chunk_alloc>>.
177 DEFUN(bfd_xmalloc_by_size_t
, (size
),
180 return bfd_xmalloc ((bfd_size_type
) size
);
186 /* Note that archive entries don't have streams; they share their parent's.
187 This allows someone to play with the iostream behind BFD's back.
189 Also, note that the origin pointer points to the beginning of a file's
190 contents (0 for non-archive elements). For archive entries this is the
191 first octet in the file, NOT the beginning of the archive header. */
194 int DEFUN(real_read
,(where
, a
,b
, file
),
200 return fread(where
, a
,b
,file
);
203 DEFUN(bfd_read
,(ptr
, size
, nitems
, abfd
),
205 bfd_size_type size AND
206 bfd_size_type nitems AND
210 nread
= real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
211 #ifdef FILE_OFFSET_IS_CHAR_INDEX
213 abfd
->where
+= nread
;
219 bfd_write (ptr
, size
, nitems
, abfd
)
222 bfd_size_type nitems
;
225 int nwrote
= fwrite (ptr
, 1, (int) (size
* nitems
), bfd_cache_lookup (abfd
));
226 #ifdef FILE_OFFSET_IS_CHAR_INDEX
228 abfd
->where
+= nwrote
;
230 if (nwrote
!= size
* nitems
)
236 bfd_error
= system_call_error
;
243 bfd_write_bigendian_4byte_int
246 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
249 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
250 endian order regardless of what else is going on. This is useful in
255 DEFUN(bfd_write_bigendian_4byte_int
,(abfd
, i
),
260 bfd_putb32(i
, buffer
);
261 bfd_write((PTR
)buffer
, 4, 1, abfd
);
265 DEFUN(bfd_tell
,(abfd
),
270 ptr
= ftell (bfd_cache_lookup(abfd
));
272 if (abfd
->my_archive
)
279 DEFUN(bfd_flush
,(abfd
),
282 return fflush (bfd_cache_lookup(abfd
));
286 DEFUN(bfd_stat
,(abfd
, statbuf
),
288 struct stat
*statbuf
)
290 return fstat (fileno(bfd_cache_lookup(abfd
)), statbuf
);
294 DEFUN(bfd_seek
,(abfd
, position
, direction
),
296 CONST file_ptr position AND
301 file_ptr file_position
;
302 /* For the time being, a BFD may not seek to it's end. The problem
303 is that we don't easily have a way to recognize the end of an
304 element in an archive. */
306 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
308 if (direction
== SEEK_CUR
&& position
== 0)
310 #ifdef FILE_OFFSET_IS_CHAR_INDEX
311 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
314 /* Explanation for this code: I'm only about 95+% sure that the above
315 conditions are sufficient and that all i/o calls are properly
316 adjusting the `where' field. So this is sort of an `assert'
317 that the `where' field is correct. If we can go a while without
318 tripping the abort, we can probably safely disable this code,
319 so that the real optimizations happen. */
320 file_ptr where_am_i_now
;
321 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
322 if (abfd
->my_archive
)
323 where_am_i_now
-= abfd
->origin
;
324 if (where_am_i_now
!= abfd
->where
)
327 if (direction
== SEEK_SET
&& position
== abfd
->where
)
332 /* We need something smarter to optimize access to archives.
333 Currently, anything inside an archive is read via the file
334 handle for the archive. Which means that a bfd_seek on one
335 component affects the `current position' in the archive, as
336 well as in any other component.
338 It might be sufficient to put a spike through the cache
339 abstraction, and look to the archive for the file position,
340 but I think we should try for something cleaner.
342 In the meantime, no optimization for archives. */
346 f
= bfd_cache_lookup (abfd
);
347 file_position
= position
;
348 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
349 file_position
+= abfd
->origin
;
351 result
= fseek (f
, file_position
, direction
);
355 /* Force redetermination of `where' field. */
357 bfd_error
= system_call_error
;
361 #ifdef FILE_OFFSET_IS_CHAR_INDEX
362 /* Adjust `where' field. */
363 if (direction
== SEEK_SET
)
364 abfd
->where
= position
;
366 abfd
->where
+= position
;
372 /** Make a string table */
375 Add string to table pointed to by table, at location starting with free_ptr.
376 resizes the table if necessary (if it's NULL, creates it, ignoring
377 table_length). Updates free_ptr, table, table_length */
380 DEFUN(bfd_add_to_string_table
,(table
, new_string
, table_length
, free_ptr
),
383 unsigned int *table_length AND
386 size_t string_length
= strlen (new_string
) + 1; /* include null here */
388 size_t space_length
= *table_length
;
389 unsigned int offset
= (base
? *free_ptr
- base
: 0);
392 /* Avoid a useless regrow if we can (but of course we still
394 space_length
= (string_length
< DEFAULT_STRING_SPACE_SIZE
?
395 DEFAULT_STRING_SPACE_SIZE
: string_length
+1);
396 base
= zalloc ((bfd_size_type
) space_length
);
399 bfd_error
= no_memory
;
404 if ((size_t)(offset
+ string_length
) >= space_length
) {
405 /* Make sure we will have enough space */
406 while ((size_t)(offset
+ string_length
) >= space_length
)
407 space_length
+= space_length
/2; /* grow by 50% */
409 base
= (char *) realloc (base
, space_length
);
411 bfd_error
= no_memory
;
417 memcpy (base
+ offset
, new_string
, string_length
);
419 *table_length
= space_length
;
420 *free_ptr
= base
+ offset
+ string_length
;
425 /** The do-it-yourself (byte) sex-change kit */
427 /* The middle letter e.g. get<b>short indicates Big or Little endian
428 target machine. It doesn't matter what the byte order of the host
429 machine is; these routines work for either. */
431 /* FIXME: Should these take a count argument?
432 Answer (gnu@cygnus.com): No, but perhaps they should be inline
433 functions in swap.h #ifdef __GNUC__.
434 Gprof them later and find out. */
443 These macros as used for reading and writing raw data in
444 sections; each access (except for bytes) is vectored through
445 the target format of the BFD and mangled accordingly. The
446 mangling performs any necessary endian translations and
447 removes alignment restrictions. Note that types accepted and
448 returned by these macros are identical so they can be swapped
449 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
450 to either <<bfd_get_32>> or <<bfd_get_64>>.
452 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
453 system without prototypes, the caller is responsible for making
454 sure that is true, with a cast if necessary. We don't cast
455 them in the macro definitions because that would prevent <<lint>>
456 or <<gcc -Wall>> from detecting sins such as passing a pointer.
457 To detect calling these with less than a <<bfd_vma>>, use
458 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
461 .{* Byte swapping macros for user section data. *}
463 .#define bfd_put_8(abfd, val, ptr) \
464 . (*((unsigned char *)(ptr)) = (unsigned char)val)
465 .#define bfd_put_signed_8 \
467 .#define bfd_get_8(abfd, ptr) \
468 . (*(unsigned char *)(ptr))
469 .#define bfd_get_signed_8(abfd, ptr) \
470 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
472 .#define bfd_put_16(abfd, val, ptr) \
473 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
474 .#define bfd_put_signed_16 \
476 .#define bfd_get_16(abfd, ptr) \
477 . BFD_SEND(abfd, bfd_getx16, (ptr))
478 .#define bfd_get_signed_16(abfd, ptr) \
479 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
481 .#define bfd_put_32(abfd, val, ptr) \
482 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
483 .#define bfd_put_signed_32 \
485 .#define bfd_get_32(abfd, ptr) \
486 . BFD_SEND(abfd, bfd_getx32, (ptr))
487 .#define bfd_get_signed_32(abfd, ptr) \
488 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
490 .#define bfd_put_64(abfd, val, ptr) \
491 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
492 .#define bfd_put_signed_64 \
494 .#define bfd_get_64(abfd, ptr) \
495 . BFD_SEND(abfd, bfd_getx64, (ptr))
496 .#define bfd_get_signed_64(abfd, ptr) \
497 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
507 These macros have the same function as their <<bfd_get_x>>
508 bretheren, except that they are used for removing information
509 for the header records of object files. Believe it or not,
510 some object files keep their header records in big endian
511 order and their data in little endian order.
513 .{* Byte swapping macros for file header data. *}
515 .#define bfd_h_put_8(abfd, val, ptr) \
516 . bfd_put_8 (abfd, val, ptr)
517 .#define bfd_h_put_signed_8(abfd, val, ptr) \
518 . bfd_put_8 (abfd, val, ptr)
519 .#define bfd_h_get_8(abfd, ptr) \
520 . bfd_get_8 (abfd, ptr)
521 .#define bfd_h_get_signed_8(abfd, ptr) \
522 . bfd_get_signed_8 (abfd, ptr)
524 .#define bfd_h_put_16(abfd, val, ptr) \
525 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
526 .#define bfd_h_put_signed_16 \
528 .#define bfd_h_get_16(abfd, ptr) \
529 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
530 .#define bfd_h_get_signed_16(abfd, ptr) \
531 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
533 .#define bfd_h_put_32(abfd, val, ptr) \
534 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
535 .#define bfd_h_put_signed_32 \
537 .#define bfd_h_get_32(abfd, ptr) \
538 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
539 .#define bfd_h_get_signed_32(abfd, ptr) \
540 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
542 .#define bfd_h_put_64(abfd, val, ptr) \
543 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
544 .#define bfd_h_put_signed_64 \
546 .#define bfd_h_get_64(abfd, ptr) \
547 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
548 .#define bfd_h_get_signed_64(abfd, ptr) \
549 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
553 /* Sign extension to bfd_signed_vma. */
554 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
555 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
556 #define EIGHT_GAZILLION (((HOST_64_BIT)0x80000000) << 32)
557 #define COERCE64(x) \
558 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
561 DEFUN(bfd_getb16
,(addr
),
562 register bfd_byte
*addr
)
564 return (addr
[0] << 8) | addr
[1];
568 DEFUN(bfd_getl16
,(addr
),
569 register bfd_byte
*addr
)
571 return (addr
[1] << 8) | addr
[0];
575 DEFUN(bfd_getb_signed_16
,(addr
),
576 register bfd_byte
*addr
)
578 return COERCE16((addr
[0] << 8) | addr
[1]);
582 DEFUN(bfd_getl_signed_16
,(addr
),
583 register bfd_byte
*addr
)
585 return COERCE16((addr
[1] << 8) | addr
[0]);
589 DEFUN(bfd_putb16
,(data
, addr
),
591 register bfd_byte
*addr
)
593 addr
[0] = (bfd_byte
)(data
>> 8);
594 addr
[1] = (bfd_byte
)data
;
598 DEFUN(bfd_putl16
,(data
, addr
),
600 register bfd_byte
*addr
)
602 addr
[0] = (bfd_byte
)data
;
603 addr
[1] = (bfd_byte
)(data
>> 8);
608 register bfd_byte
*addr
;
610 return (((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
611 | addr
[2]) << 8 | addr
[3];
616 register bfd_byte
*addr
;
618 return (((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
619 | addr
[1]) << 8 | addr
[0];
623 bfd_getb_signed_32 (addr
)
624 register bfd_byte
*addr
;
626 return COERCE32((((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
627 | addr
[2]) << 8 | addr
[3]);
631 bfd_getl_signed_32 (addr
)
632 register bfd_byte
*addr
;
634 return COERCE32((((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
635 | addr
[1]) << 8 | addr
[0]);
639 DEFUN(bfd_getb64
,(addr
),
640 register bfd_byte
*addr
)
645 high
= ((((((((addr
[0]) << 8) |
650 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
655 return high
<< 32 | low
;
664 DEFUN(bfd_getl64
,(addr
),
665 register bfd_byte
*addr
)
670 high
= (((((((addr
[7] << 8) |
675 low
= ((((((((bfd_vma
)addr
[3] << 8) |
680 return high
<< 32 | low
;
689 DEFUN(bfd_getb_signed_64
,(addr
),
690 register bfd_byte
*addr
)
695 high
= ((((((((addr
[0]) << 8) |
700 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
705 return COERCE64(high
<< 32 | low
);
714 DEFUN(bfd_getl_signed_64
,(addr
),
715 register bfd_byte
*addr
)
720 high
= (((((((addr
[7] << 8) |
725 low
= ((((((((bfd_vma
)addr
[3] << 8) |
730 return COERCE64(high
<< 32 | low
);
739 DEFUN(bfd_putb32
,(data
, addr
),
741 register bfd_byte
*addr
)
743 addr
[0] = (bfd_byte
)(data
>> 24);
744 addr
[1] = (bfd_byte
)(data
>> 16);
745 addr
[2] = (bfd_byte
)(data
>> 8);
746 addr
[3] = (bfd_byte
)data
;
750 DEFUN(bfd_putl32
,(data
, addr
),
752 register bfd_byte
*addr
)
754 addr
[0] = (bfd_byte
)data
;
755 addr
[1] = (bfd_byte
)(data
>> 8);
756 addr
[2] = (bfd_byte
)(data
>> 16);
757 addr
[3] = (bfd_byte
)(data
>> 24);
760 DEFUN(bfd_putb64
,(data
, addr
),
762 register bfd_byte
*addr
)
765 addr
[0] = (bfd_byte
)(data
>> (7*8));
766 addr
[1] = (bfd_byte
)(data
>> (6*8));
767 addr
[2] = (bfd_byte
)(data
>> (5*8));
768 addr
[3] = (bfd_byte
)(data
>> (4*8));
769 addr
[4] = (bfd_byte
)(data
>> (3*8));
770 addr
[5] = (bfd_byte
)(data
>> (2*8));
771 addr
[6] = (bfd_byte
)(data
>> (1*8));
772 addr
[7] = (bfd_byte
)(data
>> (0*8));
780 DEFUN(bfd_putl64
,(data
, addr
),
782 register bfd_byte
*addr
)
785 addr
[7] = (bfd_byte
)(data
>> (7*8));
786 addr
[6] = (bfd_byte
)(data
>> (6*8));
787 addr
[5] = (bfd_byte
)(data
>> (5*8));
788 addr
[4] = (bfd_byte
)(data
>> (4*8));
789 addr
[3] = (bfd_byte
)(data
>> (3*8));
790 addr
[2] = (bfd_byte
)(data
>> (2*8));
791 addr
[1] = (bfd_byte
)(data
>> (1*8));
792 addr
[0] = (bfd_byte
)(data
>> (0*8));
800 /* Default implementation */
803 DEFUN(bfd_generic_get_section_contents
, (abfd
, section
, location
, offset
, count
),
812 if ((bfd_size_type
)(offset
+count
) > section
->_raw_size
813 || bfd_seek(abfd
, (file_ptr
)(section
->filepos
+ offset
), SEEK_SET
) == -1
814 || bfd_read(location
, (bfd_size_type
)1, count
, abfd
) != count
)
815 return (false); /* on error */
819 /* This generic function can only be used in implementations where creating
820 NEW sections is disallowed. It is useful in patching existing sections
821 in read-write files, though. See other set_section_contents functions
822 to see why it doesn't work for new sections. */
824 bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
834 if (offset
+ count
> bfd_get_section_size_after_reloc (section
))
836 bfd_error
= bad_value
;
840 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) == -1
841 || bfd_write (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
852 unsigned int bfd_log2(bfd_vma x);
855 Return the log base 2 of the value supplied, rounded up. E.g., an
856 @var{x} of 1025 returns 11.
864 while ( (bfd_vma
)(1<< result
) < x
)