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
)
101 _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd
, ignore_exec_bfd
)
102 bfd
*ignore_core_bfd
;
103 bfd
*ignore_exec_bfd
;
105 bfd_set_error (bfd_error_invalid_operation
);
109 /* of course you can't initialize a function to be the same as another, grr */
113 _bfd_dummy_core_file_failing_command (ignore_abfd
)
121 _bfd_dummy_core_file_failing_signal (ignore_abfd
)
129 _bfd_dummy_target (ignore_abfd
)
137 /* allocate and clear storage */
143 char *ptr
= (char *) malloc ((size_t) size
);
146 memset(ptr
, 0, (size_t) size
);
150 #endif /* bfd_zmalloc */
155 /* Note that archive entries don't have streams; they share their parent's.
156 This allows someone to play with the iostream behind BFD's back.
158 Also, note that the origin pointer points to the beginning of a file's
159 contents (0 for non-archive elements). For archive entries this is the
160 first octet in the file, NOT the beginning of the archive header. */
164 real_read (where
, a
,b
, file
)
170 return fread(where
, a
,b
,file
);
173 /* Return value is amount read (FIXME: how are errors and end of file dealt
174 with? We never call bfd_set_error, which is probably a mistake). */
177 bfd_read (ptr
, size
, nitems
, abfd
)
180 bfd_size_type nitems
;
184 nread
= real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
185 #ifdef FILE_OFFSET_IS_CHAR_INDEX
187 abfd
->where
+= nread
;
190 /* Set bfd_error if we did not read as much data as we expected.
192 If the read failed due to an error set the bfd_error_system_call,
193 else set bfd_error_file_truncated.
195 A BFD backend may wish to override bfd_error_file_truncated to
196 provide something more useful (eg. no_symbols or wrong_format). */
197 if (nread
< (int)(size
* nitems
))
199 if (ferror (bfd_cache_lookup (abfd
)))
200 bfd_set_error (bfd_error_system_call
);
202 bfd_set_error (bfd_error_file_truncated
);
209 bfd_write (ptr
, size
, nitems
, abfd
)
212 bfd_size_type nitems
;
215 int nwrote
= fwrite (ptr
, 1, (int) (size
* nitems
), bfd_cache_lookup (abfd
));
216 #ifdef FILE_OFFSET_IS_CHAR_INDEX
218 abfd
->where
+= nwrote
;
220 if (nwrote
!= size
* nitems
)
226 bfd_set_error (bfd_error_system_call
);
233 bfd_write_bigendian_4byte_int
236 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
239 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
240 endian order regardless of what else is going on. This is useful in
245 bfd_write_bigendian_4byte_int (abfd
, i
)
250 bfd_putb32(i
, buffer
);
251 if (bfd_write((PTR
)buffer
, 4, 1, abfd
) != 4)
261 ptr
= ftell (bfd_cache_lookup(abfd
));
263 if (abfd
->my_archive
)
273 return fflush (bfd_cache_lookup(abfd
));
277 bfd_stat (abfd
, statbuf
)
279 struct stat
*statbuf
;
281 return fstat (fileno(bfd_cache_lookup(abfd
)), statbuf
);
284 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
285 can retrieve the error code). */
288 bfd_seek (abfd
, position
, direction
)
290 CONST file_ptr position
;
295 file_ptr file_position
;
296 /* For the time being, a BFD may not seek to it's end. The problem
297 is that we don't easily have a way to recognize the end of an
298 element in an archive. */
300 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
302 if (direction
== SEEK_CUR
&& position
== 0)
304 #ifdef FILE_OFFSET_IS_CHAR_INDEX
305 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
308 /* Explanation for this code: I'm only about 95+% sure that the above
309 conditions are sufficient and that all i/o calls are properly
310 adjusting the `where' field. So this is sort of an `assert'
311 that the `where' field is correct. If we can go a while without
312 tripping the abort, we can probably safely disable this code,
313 so that the real optimizations happen. */
314 file_ptr where_am_i_now
;
315 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
316 if (abfd
->my_archive
)
317 where_am_i_now
-= abfd
->origin
;
318 if (where_am_i_now
!= abfd
->where
)
321 if (direction
== SEEK_SET
&& position
== abfd
->where
)
326 /* We need something smarter to optimize access to archives.
327 Currently, anything inside an archive is read via the file
328 handle for the archive. Which means that a bfd_seek on one
329 component affects the `current position' in the archive, as
330 well as in any other component.
332 It might be sufficient to put a spike through the cache
333 abstraction, and look to the archive for the file position,
334 but I think we should try for something cleaner.
336 In the meantime, no optimization for archives. */
340 f
= bfd_cache_lookup (abfd
);
341 file_position
= position
;
342 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
343 file_position
+= abfd
->origin
;
345 result
= fseek (f
, file_position
, direction
);
349 /* Force redetermination of `where' field. */
351 bfd_set_error (bfd_error_system_call
);
355 #ifdef FILE_OFFSET_IS_CHAR_INDEX
356 /* Adjust `where' field. */
357 if (direction
== SEEK_SET
)
358 abfd
->where
= position
;
360 abfd
->where
+= position
;
366 /** Make a string table */
369 Add string to table pointed to by table, at location starting with free_ptr.
370 resizes the table if necessary (if it's NULL, creates it, ignoring
371 table_length). Updates free_ptr, table, table_length */
374 bfd_add_to_string_table (table
, new_string
, table_length
, free_ptr
)
377 unsigned int *table_length
;
380 size_t string_length
= strlen (new_string
) + 1; /* include null here */
382 size_t space_length
= *table_length
;
383 unsigned int offset
= (base
? *free_ptr
- base
: 0);
386 /* Avoid a useless regrow if we can (but of course we still
387 take it next time). */
388 space_length
= (string_length
< DEFAULT_STRING_SPACE_SIZE
?
389 DEFAULT_STRING_SPACE_SIZE
: string_length
+1);
390 base
= bfd_zmalloc ((bfd_size_type
) space_length
);
393 bfd_set_error (bfd_error_no_memory
);
398 if ((size_t)(offset
+ string_length
) >= space_length
) {
399 /* Make sure we will have enough space */
400 while ((size_t)(offset
+ string_length
) >= space_length
)
401 space_length
+= space_length
/2; /* grow by 50% */
403 base
= (char *) realloc (base
, space_length
);
405 bfd_set_error (bfd_error_no_memory
);
411 memcpy (base
+ offset
, new_string
, string_length
);
413 *table_length
= space_length
;
414 *free_ptr
= base
+ offset
+ string_length
;
419 /** The do-it-yourself (byte) sex-change kit */
421 /* The middle letter e.g. get<b>short indicates Big or Little endian
422 target machine. It doesn't matter what the byte order of the host
423 machine is; these routines work for either. */
425 /* FIXME: Should these take a count argument?
426 Answer (gnu@cygnus.com): No, but perhaps they should be inline
427 functions in swap.h #ifdef __GNUC__.
428 Gprof them later and find out. */
437 These macros as used for reading and writing raw data in
438 sections; each access (except for bytes) is vectored through
439 the target format of the BFD and mangled accordingly. The
440 mangling performs any necessary endian translations and
441 removes alignment restrictions. Note that types accepted and
442 returned by these macros are identical so they can be swapped
443 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
444 to either <<bfd_get_32>> or <<bfd_get_64>>.
446 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
447 system without prototypes, the caller is responsible for making
448 sure that is true, with a cast if necessary. We don't cast
449 them in the macro definitions because that would prevent <<lint>>
450 or <<gcc -Wall>> from detecting sins such as passing a pointer.
451 To detect calling these with less than a <<bfd_vma>>, use
452 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
455 .{* Byte swapping macros for user section data. *}
457 .#define bfd_put_8(abfd, val, ptr) \
458 . (*((unsigned char *)(ptr)) = (unsigned char)(val))
459 .#define bfd_put_signed_8 \
461 .#define bfd_get_8(abfd, ptr) \
462 . (*(unsigned char *)(ptr))
463 .#define bfd_get_signed_8(abfd, ptr) \
464 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
466 .#define bfd_put_16(abfd, val, ptr) \
467 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
468 .#define bfd_put_signed_16 \
470 .#define bfd_get_16(abfd, ptr) \
471 . BFD_SEND(abfd, bfd_getx16, (ptr))
472 .#define bfd_get_signed_16(abfd, ptr) \
473 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
475 .#define bfd_put_32(abfd, val, ptr) \
476 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
477 .#define bfd_put_signed_32 \
479 .#define bfd_get_32(abfd, ptr) \
480 . BFD_SEND(abfd, bfd_getx32, (ptr))
481 .#define bfd_get_signed_32(abfd, ptr) \
482 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
484 .#define bfd_put_64(abfd, val, ptr) \
485 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
486 .#define bfd_put_signed_64 \
488 .#define bfd_get_64(abfd, ptr) \
489 . BFD_SEND(abfd, bfd_getx64, (ptr))
490 .#define bfd_get_signed_64(abfd, ptr) \
491 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
501 These macros have the same function as their <<bfd_get_x>>
502 bretheren, except that they are used for removing information
503 for the header records of object files. Believe it or not,
504 some object files keep their header records in big endian
505 order and their data in little endian order.
507 .{* Byte swapping macros for file header data. *}
509 .#define bfd_h_put_8(abfd, val, ptr) \
510 . bfd_put_8 (abfd, val, ptr)
511 .#define bfd_h_put_signed_8(abfd, val, ptr) \
512 . bfd_put_8 (abfd, val, ptr)
513 .#define bfd_h_get_8(abfd, ptr) \
514 . bfd_get_8 (abfd, ptr)
515 .#define bfd_h_get_signed_8(abfd, ptr) \
516 . bfd_get_signed_8 (abfd, ptr)
518 .#define bfd_h_put_16(abfd, val, ptr) \
519 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
520 .#define bfd_h_put_signed_16 \
522 .#define bfd_h_get_16(abfd, ptr) \
523 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
524 .#define bfd_h_get_signed_16(abfd, ptr) \
525 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
527 .#define bfd_h_put_32(abfd, val, ptr) \
528 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
529 .#define bfd_h_put_signed_32 \
531 .#define bfd_h_get_32(abfd, ptr) \
532 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
533 .#define bfd_h_get_signed_32(abfd, ptr) \
534 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
536 .#define bfd_h_put_64(abfd, val, ptr) \
537 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
538 .#define bfd_h_put_signed_64 \
540 .#define bfd_h_get_64(abfd, ptr) \
541 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
542 .#define bfd_h_get_signed_64(abfd, ptr) \
543 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
547 /* Sign extension to bfd_signed_vma. */
548 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
549 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
550 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
551 #define COERCE64(x) \
552 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
556 register const bfd_byte
*addr
;
558 return (addr
[0] << 8) | addr
[1];
563 register const bfd_byte
*addr
;
565 return (addr
[1] << 8) | addr
[0];
569 bfd_getb_signed_16 (addr
)
570 register const bfd_byte
*addr
;
572 return COERCE16((addr
[0] << 8) | addr
[1]);
576 bfd_getl_signed_16 (addr
)
577 register const bfd_byte
*addr
;
579 return COERCE16((addr
[1] << 8) | addr
[0]);
583 bfd_putb16 (data
, addr
)
585 register bfd_byte
*addr
;
587 addr
[0] = (bfd_byte
)(data
>> 8);
588 addr
[1] = (bfd_byte
)data
;
592 bfd_putl16 (data
, addr
)
594 register bfd_byte
*addr
;
596 addr
[0] = (bfd_byte
)data
;
597 addr
[1] = (bfd_byte
)(data
>> 8);
602 register const bfd_byte
*addr
;
604 return (((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
605 | addr
[2]) << 8 | addr
[3];
610 register const bfd_byte
*addr
;
612 return (((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
613 | addr
[1]) << 8 | addr
[0];
617 bfd_getb_signed_32 (addr
)
618 register const bfd_byte
*addr
;
620 return COERCE32((((((bfd_vma
)addr
[0] << 8) | addr
[1]) << 8)
621 | addr
[2]) << 8 | addr
[3]);
625 bfd_getl_signed_32 (addr
)
626 register const bfd_byte
*addr
;
628 return COERCE32((((((bfd_vma
)addr
[3] << 8) | addr
[2]) << 8)
629 | addr
[1]) << 8 | addr
[0]);
634 register const bfd_byte
*addr
;
639 high
= ((((((((addr
[0]) << 8) |
644 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
649 return high
<< 32 | low
;
658 register const bfd_byte
*addr
;
662 high
= (((((((addr
[7] << 8) |
667 low
= ((((((((bfd_vma
)addr
[3] << 8) |
672 return high
<< 32 | low
;
681 bfd_getb_signed_64 (addr
)
682 register const bfd_byte
*addr
;
687 high
= ((((((((addr
[0]) << 8) |
692 low
= (((((((((bfd_vma
)addr
[4]) << 8) |
697 return COERCE64(high
<< 32 | low
);
705 bfd_getl_signed_64 (addr
)
706 register const bfd_byte
*addr
;
710 high
= (((((((addr
[7] << 8) |
715 low
= ((((((((bfd_vma
)addr
[3] << 8) |
720 return COERCE64(high
<< 32 | low
);
728 bfd_putb32 (data
, addr
)
730 register bfd_byte
*addr
;
732 addr
[0] = (bfd_byte
)(data
>> 24);
733 addr
[1] = (bfd_byte
)(data
>> 16);
734 addr
[2] = (bfd_byte
)(data
>> 8);
735 addr
[3] = (bfd_byte
)data
;
739 bfd_putl32 (data
, addr
)
741 register bfd_byte
*addr
;
743 addr
[0] = (bfd_byte
)data
;
744 addr
[1] = (bfd_byte
)(data
>> 8);
745 addr
[2] = (bfd_byte
)(data
>> 16);
746 addr
[3] = (bfd_byte
)(data
>> 24);
750 bfd_putb64 (data
, addr
)
752 register bfd_byte
*addr
;
755 addr
[0] = (bfd_byte
)(data
>> (7*8));
756 addr
[1] = (bfd_byte
)(data
>> (6*8));
757 addr
[2] = (bfd_byte
)(data
>> (5*8));
758 addr
[3] = (bfd_byte
)(data
>> (4*8));
759 addr
[4] = (bfd_byte
)(data
>> (3*8));
760 addr
[5] = (bfd_byte
)(data
>> (2*8));
761 addr
[6] = (bfd_byte
)(data
>> (1*8));
762 addr
[7] = (bfd_byte
)(data
>> (0*8));
769 bfd_putl64 (data
, addr
)
771 register bfd_byte
*addr
;
774 addr
[7] = (bfd_byte
)(data
>> (7*8));
775 addr
[6] = (bfd_byte
)(data
>> (6*8));
776 addr
[5] = (bfd_byte
)(data
>> (5*8));
777 addr
[4] = (bfd_byte
)(data
>> (4*8));
778 addr
[3] = (bfd_byte
)(data
>> (3*8));
779 addr
[2] = (bfd_byte
)(data
>> (2*8));
780 addr
[1] = (bfd_byte
)(data
>> (1*8));
781 addr
[0] = (bfd_byte
)(data
>> (0*8));
787 /* Default implementation */
790 bfd_generic_get_section_contents (abfd
, section
, location
, offset
, count
)
799 if ((bfd_size_type
)(offset
+count
) > section
->_raw_size
800 || bfd_seek(abfd
, (file_ptr
)(section
->filepos
+ offset
), SEEK_SET
) == -1
801 || bfd_read(location
, (bfd_size_type
)1, count
, abfd
) != count
)
802 return (false); /* on error */
806 /* This generic function can only be used in implementations where creating
807 NEW sections is disallowed. It is useful in patching existing sections
808 in read-write files, though. See other set_section_contents functions
809 to see why it doesn't work for new sections. */
811 bfd_generic_set_section_contents (abfd
, section
, location
, offset
, count
)
821 if (bfd_seek (abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
) == -1
822 || bfd_write (location
, (bfd_size_type
) 1, count
, abfd
) != count
)
833 unsigned int bfd_log2(bfd_vma x);
836 Return the log base 2 of the value supplied, rounded up. E.g., an
837 @var{x} of 1025 returns 11.
845 while ( (bfd_vma
)(1<< result
) < x
)
851 bfd_generic_is_local_label (abfd
, sym
)
855 char locals_prefix
= (bfd_get_symbol_leading_char (abfd
) == '_') ? 'L' : '.';
857 return (sym
->name
[0] == locals_prefix
);