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 *) bfd_xmalloc (size
);
138 memset(ptr
,0, (size_t) size
);
142 #endif /* bfd_zmalloc */
147 /* Note that archive entries don't have streams; they share their parent's.
148 This allows someone to play with the iostream behind BFD's back.
150 Also, note that the origin pointer points to the beginning of a file's
151 contents (0 for non-archive elements). For archive entries this is the
152 first octet in the file, NOT the beginning of the archive header. */
156 real_read (where
, a
,b
, file
)
162 return fread(where
, a
,b
,file
);
166 bfd_read (ptr
, size
, nitems
, abfd
)
169 bfd_size_type nitems
;
173 nread
= real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
174 #ifdef FILE_OFFSET_IS_CHAR_INDEX
176 abfd
->where
+= nread
;
182 bfd_write (ptr
, size
, nitems
, abfd
)
185 bfd_size_type nitems
;
188 int nwrote
= fwrite (ptr
, 1, (int) (size
* nitems
), bfd_cache_lookup (abfd
));
189 #ifdef FILE_OFFSET_IS_CHAR_INDEX
191 abfd
->where
+= nwrote
;
193 if (nwrote
!= size
* nitems
)
199 bfd_error
= system_call_error
;
206 bfd_write_bigendian_4byte_int
209 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
212 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
213 endian order regardless of what else is going on. This is useful in
218 bfd_write_bigendian_4byte_int (abfd
, i
)
223 bfd_putb32(i
, buffer
);
224 bfd_write((PTR
)buffer
, 4, 1, abfd
);
233 ptr
= ftell (bfd_cache_lookup(abfd
));
235 if (abfd
->my_archive
)
245 return fflush (bfd_cache_lookup(abfd
));
249 bfd_stat (abfd
, statbuf
)
251 struct stat
*statbuf
;
253 return fstat (fileno(bfd_cache_lookup(abfd
)), statbuf
);
257 bfd_seek (abfd
, position
, direction
)
259 CONST file_ptr position
;
264 file_ptr file_position
;
265 /* For the time being, a BFD may not seek to it's end. The problem
266 is that we don't easily have a way to recognize the end of an
267 element in an archive. */
269 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
271 if (direction
== SEEK_CUR
&& position
== 0)
273 #ifdef FILE_OFFSET_IS_CHAR_INDEX
274 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
277 /* Explanation for this code: I'm only about 95+% sure that the above
278 conditions are sufficient and that all i/o calls are properly
279 adjusting the `where' field. So this is sort of an `assert'
280 that the `where' field is correct. If we can go a while without
281 tripping the abort, we can probably safely disable this code,
282 so that the real optimizations happen. */
283 file_ptr where_am_i_now
;
284 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
285 if (abfd
->my_archive
)
286 where_am_i_now
-= abfd
->origin
;
287 if (where_am_i_now
!= abfd
->where
)
290 if (direction
== SEEK_SET
&& position
== abfd
->where
)
295 /* We need something smarter to optimize access to archives.
296 Currently, anything inside an archive is read via the file
297 handle for the archive. Which means that a bfd_seek on one
298 component affects the `current position' in the archive, as
299 well as in any other component.
301 It might be sufficient to put a spike through the cache
302 abstraction, and look to the archive for the file position,
303 but I think we should try for something cleaner.
305 In the meantime, no optimization for archives. */
309 f
= bfd_cache_lookup (abfd
);
310 file_position
= position
;
311 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
312 file_position
+= abfd
->origin
;
314 result
= fseek (f
, file_position
, direction
);
318 /* Force redetermination of `where' field. */
320 bfd_error
= system_call_error
;
324 #ifdef FILE_OFFSET_IS_CHAR_INDEX
325 /* Adjust `where' field. */
326 if (direction
== SEEK_SET
)
327 abfd
->where
= position
;
329 abfd
->where
+= position
;
335 /** Make a string table */
338 Add string to table pointed to by table, at location starting with free_ptr.
339 resizes the table if necessary (if it's NULL, creates it, ignoring
340 table_length). Updates free_ptr, table, table_length */
343 bfd_add_to_string_table (table
, new_string
, table_length
, free_ptr
)
346 unsigned int *table_length
;
349 size_t string_length
= strlen (new_string
) + 1; /* include null here */
351 size_t space_length
= *table_length
;
352 unsigned int offset
= (base
? *free_ptr
- base
: 0);
355 /* Avoid a useless regrow if we can (but of course we still
356 take it next time). */
357 space_length
= (string_length
< DEFAULT_STRING_SPACE_SIZE
?
358 DEFAULT_STRING_SPACE_SIZE
: string_length
+1);
359 base
= bfd_zmalloc ((bfd_size_type
) space_length
);
362 bfd_error
= no_memory
;
367 if ((size_t)(offset
+ string_length
) >= space_length
) {
368 /* Make sure we will have enough space */
369 while ((size_t)(offset
+ string_length
) >= space_length
)
370 space_length
+= space_length
/2; /* grow by 50% */
372 base
= (char *) realloc (base
, space_length
);
374 bfd_error
= no_memory
;
380 memcpy (base
+ offset
, new_string
, string_length
);
382 *table_length
= space_length
;
383 *free_ptr
= base
+ offset
+ string_length
;
388 /** The do-it-yourself (byte) sex-change kit */
390 /* The middle letter e.g. get<b>short indicates Big or Little endian
391 target machine. It doesn't matter what the byte order of the host
392 machine is; these routines work for either. */
394 /* FIXME: Should these take a count argument?
395 Answer (gnu@cygnus.com): No, but perhaps they should be inline
396 functions in swap.h #ifdef __GNUC__.
397 Gprof them later and find out. */
406 These macros as used for reading and writing raw data in
407 sections; each access (except for bytes) is vectored through
408 the target format of the BFD and mangled accordingly. The
409 mangling performs any necessary endian translations and
410 removes alignment restrictions. Note that types accepted and
411 returned by these macros are identical so they can be swapped
412 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
413 to either <<bfd_get_32>> or <<bfd_get_64>>.
415 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
416 system without prototypes, the caller is responsible for making
417 sure that is true, with a cast if necessary. We don't cast
418 them in the macro definitions because that would prevent <<lint>>
419 or <<gcc -Wall>> from detecting sins such as passing a pointer.
420 To detect calling these with less than a <<bfd_vma>>, use
421 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
424 .{* Byte swapping macros for user section data. *}
426 .#define bfd_put_8(abfd, val, ptr) \
427 . (*((unsigned char *)(ptr)) = (unsigned char)(val))
428 .#define bfd_put_signed_8 \
430 .#define bfd_get_8(abfd, ptr) \
431 . (*(unsigned char *)(ptr))
432 .#define bfd_get_signed_8(abfd, ptr) \
433 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
435 .#define bfd_put_16(abfd, val, ptr) \
436 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
437 .#define bfd_put_signed_16 \
439 .#define bfd_get_16(abfd, ptr) \
440 . BFD_SEND(abfd, bfd_getx16, (ptr))
441 .#define bfd_get_signed_16(abfd, ptr) \
442 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
444 .#define bfd_put_32(abfd, val, ptr) \
445 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
446 .#define bfd_put_signed_32 \
448 .#define bfd_get_32(abfd, ptr) \
449 . BFD_SEND(abfd, bfd_getx32, (ptr))
450 .#define bfd_get_signed_32(abfd, ptr) \
451 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
453 .#define bfd_put_64(abfd, val, ptr) \
454 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
455 .#define bfd_put_signed_64 \
457 .#define bfd_get_64(abfd, ptr) \
458 . BFD_SEND(abfd, bfd_getx64, (ptr))
459 .#define bfd_get_signed_64(abfd, ptr) \
460 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
470 These macros have the same function as their <<bfd_get_x>>
471 bretheren, except that they are used for removing information
472 for the header records of object files. Believe it or not,
473 some object files keep their header records in big endian
474 order and their data in little endian order.
476 .{* Byte swapping macros for file header data. *}
478 .#define bfd_h_put_8(abfd, val, ptr) \
479 . bfd_put_8 (abfd, val, ptr)
480 .#define bfd_h_put_signed_8(abfd, val, ptr) \
481 . bfd_put_8 (abfd, val, ptr)
482 .#define bfd_h_get_8(abfd, ptr) \
483 . bfd_get_8 (abfd, ptr)
484 .#define bfd_h_get_signed_8(abfd, ptr) \
485 . bfd_get_signed_8 (abfd, ptr)
487 .#define bfd_h_put_16(abfd, val, ptr) \
488 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
489 .#define bfd_h_put_signed_16 \
491 .#define bfd_h_get_16(abfd, ptr) \
492 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
493 .#define bfd_h_get_signed_16(abfd, ptr) \
494 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
496 .#define bfd_h_put_32(abfd, val, ptr) \
497 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
498 .#define bfd_h_put_signed_32 \
500 .#define bfd_h_get_32(abfd, ptr) \
501 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
502 .#define bfd_h_get_signed_32(abfd, ptr) \
503 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
505 .#define bfd_h_put_64(abfd, val, ptr) \
506 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
507 .#define bfd_h_put_signed_64 \
509 .#define bfd_h_get_64(abfd, ptr) \
510 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
511 .#define bfd_h_get_signed_64(abfd, ptr) \
512 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
516 /* Sign extension to bfd_signed_vma. */
517 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
518 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
519 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
520 #define COERCE64(x) \
521 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
525 register const bfd_byte
*addr
;
527 return (addr
[0] << 8) | addr
[1];
532 register const bfd_byte
*addr
;
534 return (addr
[1] << 8) | addr
[0];
538 bfd_getb_signed_16 (addr
)
539 register const bfd_byte
*addr
;
541 return COERCE16((addr
[0] << 8) | addr
[1]);
545 bfd_getl_signed_16 (addr
)
546 register const bfd_byte
*addr
;
548 return COERCE16((addr
[1] << 8) | addr
[0]);
552 bfd_putb16 (data
, addr
)
554 register bfd_byte
*addr
;
556 addr
[0] = (bfd_byte
)(data
>> 8);
557 addr
[1] = (bfd_byte
)data
;
561 bfd_putl16 (data
, addr
)
563 register bfd_byte
*addr
;
565 addr
[0] = (bfd_byte
)data
;
566 addr
[1] = (bfd_byte
)(data
>> 8);
571 register const bfd_byte
*addr
;
573 return (((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
574 | addr
[2]) << 8 | addr
[3];
579 register const bfd_byte
*addr
;
581 return (((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
582 | addr
[1]) << 8 | addr
[0];
586 bfd_getb_signed_32 (addr
)
587 register const bfd_byte
*addr
;
589 return COERCE32((((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
590 | addr
[2]) << 8 | addr
[3]);
594 bfd_getl_signed_32 (addr
)
595 register const bfd_byte
*addr
;
597 return COERCE32((((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
598 | addr
[1]) << 8 | addr
[0]);
603 register const bfd_byte
*addr
;
608 high
= ((((((((addr
[0]) << 8) |
613 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
618 return high
<< 32 | low
;
627 register const bfd_byte
*addr
;
631 high
= (((((((addr
[7] << 8) |
636 low
= ((((((((bfd_vma
)addr
[3] << 8) |
641 return high
<< 32 | low
;
650 bfd_getb_signed_64 (addr
)
651 register const bfd_byte
*addr
;
656 high
= ((((((((addr
[0]) << 8) |
661 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
666 return COERCE64(high
<< 32 | low
);
674 bfd_getl_signed_64 (addr
)
675 register const bfd_byte
*addr
;
679 high
= (((((((addr
[7] << 8) |
684 low
= ((((((((bfd_vma
)addr
[3] << 8) |
689 return COERCE64(high
<< 32 | low
);
697 bfd_putb32 (data
, addr
)
699 register bfd_byte
*addr
;
701 addr
[0] = (bfd_byte
)(data
>> 24);
702 addr
[1] = (bfd_byte
)(data
>> 16);
703 addr
[2] = (bfd_byte
)(data
>> 8);
704 addr
[3] = (bfd_byte
)data
;
708 bfd_putl32 (data
, addr
)
710 register bfd_byte
*addr
;
712 addr
[0] = (bfd_byte
)data
;
713 addr
[1] = (bfd_byte
)(data
>> 8);
714 addr
[2] = (bfd_byte
)(data
>> 16);
715 addr
[3] = (bfd_byte
)(data
>> 24);
719 bfd_putb64 (data
, addr
)
721 register bfd_byte
*addr
;
724 addr
[0] = (bfd_byte
)(data
>> (7*8));
725 addr
[1] = (bfd_byte
)(data
>> (6*8));
726 addr
[2] = (bfd_byte
)(data
>> (5*8));
727 addr
[3] = (bfd_byte
)(data
>> (4*8));
728 addr
[4] = (bfd_byte
)(data
>> (3*8));
729 addr
[5] = (bfd_byte
)(data
>> (2*8));
730 addr
[6] = (bfd_byte
)(data
>> (1*8));
731 addr
[7] = (bfd_byte
)(data
>> (0*8));
738 bfd_putl64 (data
, addr
)
740 register bfd_byte
*addr
;
743 addr
[7] = (bfd_byte
)(data
>> (7*8));
744 addr
[6] = (bfd_byte
)(data
>> (6*8));
745 addr
[5] = (bfd_byte
)(data
>> (5*8));
746 addr
[4] = (bfd_byte
)(data
>> (4*8));
747 addr
[3] = (bfd_byte
)(data
>> (3*8));
748 addr
[2] = (bfd_byte
)(data
>> (2*8));
749 addr
[1] = (bfd_byte
)(data
>> (1*8));
750 addr
[0] = (bfd_byte
)(data
>> (0*8));
756 /* Default implementation */
759 bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
)
768 if ((bfd_size_type
)(offset
+count
) > section
->_raw_size
769 || bfd_seek(abfd
, (file_ptr
)(section
->filepos
+ offset
), SEEK_SET
) == -1
770 || bfd_read(location
, (bfd_size_type
)1, count
, abfd
) != count
)
771 return (false); /* on error */
775 /* This generic function can only be used in implementations where creating
776 NEW sections is disallowed. It is useful in patching existing sections
777 in read-write files, though. See other set_section_contents functions
778 to see why it doesn't work for new sections. */
780 bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
790 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) == -1
791 || bfd_write (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
802 unsigned int bfd_log2(bfd_vma x);
805 Return the log base 2 of the value supplied, rounded up. E.g., an
806 @var{x} of 1025 returns 11.
814 while ( (bfd_vma
)(1<< result
) < x
)