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_set_error (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);
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);
165 /* Return value is amount read (FIXME: how are errors and end of file dealt
166 with? We never call bfd_set_error, which is probably a mistake). */
169 bfd_read (ptr, size, nitems, abfd)
172 bfd_size_type nitems;
176 nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
177 #ifdef FILE_OFFSET_IS_CHAR_INDEX
179 abfd->where += nread;
182 /* Set bfd_error if we did not read as much data as we expected.
184 If the read failed due to an error set the bfd_error_system_call,
185 else set bfd_error_file_truncated.
187 A BFD backend may wish to override bfd_error_file_truncated to
188 provide something more useful (eg. no_symbols or wrong_format). */
189 if (nread < (int)(size * nitems))
191 if (ferror (bfd_cache_lookup (abfd)))
192 bfd_set_error (bfd_error_system_call);
194 bfd_set_error (bfd_error_file_truncated);
201 bfd_write (ptr, size, nitems, abfd)
204 bfd_size_type nitems;
207 int nwrote = fwrite (ptr, 1, (int) (size * nitems), bfd_cache_lookup (abfd));
208 #ifdef FILE_OFFSET_IS_CHAR_INDEX
210 abfd->where += nwrote;
212 if (nwrote != size * nitems)
218 bfd_set_error (bfd_error_system_call);
225 bfd_write_bigendian_4byte_int
228 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
231 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
232 endian order regardless of what else is going on. This is useful in
237 bfd_write_bigendian_4byte_int (abfd, i)
242 bfd_putb32(i, buffer);
243 bfd_write((PTR)buffer, 4, 1, abfd);
252 ptr = ftell (bfd_cache_lookup(abfd));
254 if (abfd->my_archive)
264 return fflush (bfd_cache_lookup(abfd));
268 bfd_stat (abfd, statbuf)
270 struct stat *statbuf;
272 return fstat (fileno(bfd_cache_lookup(abfd)), statbuf);
275 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
276 can retrieve the error code). */
279 bfd_seek (abfd, position, direction)
281 CONST file_ptr position;
286 file_ptr file_position;
287 /* For the time being, a BFD may not seek to it's end. The problem
288 is that we don't easily have a way to recognize the end of an
289 element in an archive. */
291 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
293 if (direction == SEEK_CUR && position == 0)
295 #ifdef FILE_OFFSET_IS_CHAR_INDEX
296 if (abfd->format != bfd_archive && abfd->my_archive == 0)
299 /* Explanation for this code: I'm only about 95+% sure that the above
300 conditions are sufficient and that all i/o calls are properly
301 adjusting the `where' field. So this is sort of an `assert'
302 that the `where' field is correct. If we can go a while without
303 tripping the abort, we can probably safely disable this code,
304 so that the real optimizations happen. */
305 file_ptr where_am_i_now;
306 where_am_i_now = ftell (bfd_cache_lookup (abfd));
307 if (abfd->my_archive)
308 where_am_i_now -= abfd->origin;
309 if (where_am_i_now != abfd->where)
312 if (direction == SEEK_SET && position == abfd->where)
317 /* We need something smarter to optimize access to archives.
318 Currently, anything inside an archive is read via the file
319 handle for the archive. Which means that a bfd_seek on one
320 component affects the `current position' in the archive, as
321 well as in any other component.
323 It might be sufficient to put a spike through the cache
324 abstraction, and look to the archive for the file position,
325 but I think we should try for something cleaner.
327 In the meantime, no optimization for archives. */
331 f = bfd_cache_lookup (abfd);
332 file_position = position;
333 if (direction == SEEK_SET && abfd->my_archive != NULL)
334 file_position += abfd->origin;
336 result = fseek (f, file_position, direction);
340 /* Force redetermination of `where' field. */
342 bfd_set_error (bfd_error_system_call);
346 #ifdef FILE_OFFSET_IS_CHAR_INDEX
347 /* Adjust `where' field. */
348 if (direction == SEEK_SET)
349 abfd->where = position;
351 abfd->where += position;
357 /** Make a string table */
360 Add string to table pointed to by table, at location starting with free_ptr.
361 resizes the table if necessary (if it's NULL, creates it, ignoring
362 table_length). Updates free_ptr, table, table_length */
365 bfd_add_to_string_table (table, new_string, table_length, free_ptr)
368 unsigned int *table_length;
371 size_t string_length = strlen (new_string) + 1; /* include null here */
373 size_t space_length = *table_length;
374 unsigned int offset = (base ? *free_ptr - base : 0);
377 /* Avoid a useless regrow if we can (but of course we still
378 take it next time). */
379 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
380 DEFAULT_STRING_SPACE_SIZE : string_length+1);
381 base = bfd_zmalloc ((bfd_size_type) space_length);
384 bfd_set_error (bfd_error_no_memory);
389 if ((size_t)(offset + string_length) >= space_length) {
390 /* Make sure we will have enough space */
391 while ((size_t)(offset + string_length) >= space_length)
392 space_length += space_length/2; /* grow by 50% */
394 base = (char *) realloc (base, space_length);
396 bfd_set_error (bfd_error_no_memory);
402 memcpy (base + offset, new_string, string_length);
404 *table_length = space_length;
405 *free_ptr = base + offset + string_length;
410 /** The do-it-yourself (byte) sex-change kit */
412 /* The middle letter e.g. get<b>short indicates Big or Little endian
413 target machine. It doesn't matter what the byte order of the host
414 machine is; these routines work for either. */
416 /* FIXME: Should these take a count argument?
418 functions in swap.h #ifdef __GNUC__.
419 Gprof them later and find out. */
428 These macros as used for reading and writing raw data in
429 sections; each access (except for bytes) is vectored through
430 the target format of the BFD and mangled accordingly. The
431 mangling performs any necessary endian translations and
432 removes alignment restrictions. Note that types accepted and
433 returned by these macros are identical so they can be swapped
434 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
435 to either <<bfd_get_32>> or <<bfd_get_64>>.
437 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
438 system without prototypes, the caller is responsible for making
439 sure that is true, with a cast if necessary. We don't cast
440 them in the macro definitions because that would prevent <<lint>>
441 or <<gcc -Wall>> from detecting sins such as passing a pointer.
442 To detect calling these with less than a <<bfd_vma>>, use
443 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
446 .{* Byte swapping macros for user section data. *}
448 .#define bfd_put_8(abfd, val, ptr) \
449 . (*((unsigned char *)(ptr)) = (unsigned char)(val))
450 .#define bfd_put_signed_8 \
452 .#define bfd_get_8(abfd, ptr) \
453 . (*(unsigned char *)(ptr))
454 .#define bfd_get_signed_8(abfd, ptr) \
455 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
457 .#define bfd_put_16(abfd, val, ptr) \
458 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
459 .#define bfd_put_signed_16 \
461 .#define bfd_get_16(abfd, ptr) \
462 . BFD_SEND(abfd, bfd_getx16, (ptr))
463 .#define bfd_get_signed_16(abfd, ptr) \
464 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
466 .#define bfd_put_32(abfd, val, ptr) \
467 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
468 .#define bfd_put_signed_32 \
470 .#define bfd_get_32(abfd, ptr) \
471 . BFD_SEND(abfd, bfd_getx32, (ptr))
472 .#define bfd_get_signed_32(abfd, ptr) \
473 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
475 .#define bfd_put_64(abfd, val, ptr) \
476 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
477 .#define bfd_put_signed_64 \
479 .#define bfd_get_64(abfd, ptr) \
480 . BFD_SEND(abfd, bfd_getx64, (ptr))
481 .#define bfd_get_signed_64(abfd, ptr) \
482 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
492 These macros have the same function as their <<bfd_get_x>>
493 bretheren, except that they are used for removing information
494 for the header records of object files. Believe it or not,
495 some object files keep their header records in big endian
496 order and their data in little endian order.
498 .{* Byte swapping macros for file header data. *}
500 .#define bfd_h_put_8(abfd, val, ptr) \
501 . bfd_put_8 (abfd, val, ptr)
502 .#define bfd_h_put_signed_8(abfd, val, ptr) \
503 . bfd_put_8 (abfd, val, ptr)
504 .#define bfd_h_get_8(abfd, ptr) \
505 . bfd_get_8 (abfd, ptr)
506 .#define bfd_h_get_signed_8(abfd, ptr) \
507 . bfd_get_signed_8 (abfd, ptr)
509 .#define bfd_h_put_16(abfd, val, ptr) \
510 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
511 .#define bfd_h_put_signed_16 \
513 .#define bfd_h_get_16(abfd, ptr) \
514 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
515 .#define bfd_h_get_signed_16(abfd, ptr) \
516 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
518 .#define bfd_h_put_32(abfd, val, ptr) \
519 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
520 .#define bfd_h_put_signed_32 \
522 .#define bfd_h_get_32(abfd, ptr) \
523 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
524 .#define bfd_h_get_signed_32(abfd, ptr) \
525 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
527 .#define bfd_h_put_64(abfd, val, ptr) \
528 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
529 .#define bfd_h_put_signed_64 \
531 .#define bfd_h_get_64(abfd, ptr) \
532 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
533 .#define bfd_h_get_signed_64(abfd, ptr) \
534 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
538 /* Sign extension to bfd_signed_vma. */
539 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
540 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
541 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
542 #define COERCE64(x) \
543 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
547 register const bfd_byte *addr;
549 return (addr[0] << 8) | addr[1];
554 register const bfd_byte *addr;
556 return (addr[1] << 8) | addr[0];
560 bfd_getb_signed_16 (addr)
561 register const bfd_byte *addr;
563 return COERCE16((addr[0] << 8) | addr[1]);
567 bfd_getl_signed_16 (addr)
568 register const bfd_byte *addr;
570 return COERCE16((addr[1] << 8) | addr[0]);
574 bfd_putb16 (data, addr)
576 register bfd_byte *addr;
578 addr[0] = (bfd_byte)(data >> 8);
579 addr[1] = (bfd_byte )data;
583 bfd_putl16 (data, addr)
585 register bfd_byte *addr;
587 addr[0] = (bfd_byte )data;
588 addr[1] = (bfd_byte)(data >> 8);
593 register const bfd_byte *addr;
595 return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
596 | addr[2]) << 8 | addr[3];
601 register const bfd_byte *addr;
603 return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
604 | addr[1]) << 8 | addr[0];
608 bfd_getb_signed_32 (addr)
609 register const bfd_byte *addr;
611 return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
612 | addr[2]) << 8 | addr[3]);
616 bfd_getl_signed_32 (addr)
617 register const bfd_byte *addr;
619 return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
620 | addr[1]) << 8 | addr[0]);
625 register const bfd_byte *addr;
630 high= ((((((((addr[0]) << 8) |
635 low = (((((((((bfd_vma)addr[4]) << 8) |
640 return high << 32 | low;
649 register const bfd_byte *addr;
653 high= (((((((addr[7] << 8) |
658 low = ((((((((bfd_vma)addr[3] << 8) |
663 return high << 32 | low;
672 bfd_getb_signed_64 (addr)
673 register const bfd_byte *addr;
678 high= ((((((((addr[0]) << 8) |
683 low = (((((((((bfd_vma)addr[4]) << 8) |
688 return COERCE64(high << 32 | low);
696 bfd_getl_signed_64 (addr)
697 register const bfd_byte *addr;
701 high= (((((((addr[7] << 8) |
706 low = ((((((((bfd_vma)addr[3] << 8) |
711 return COERCE64(high << 32 | low);
719 bfd_putb32 (data, addr)
721 register bfd_byte *addr;
723 addr[0] = (bfd_byte)(data >> 24);
724 addr[1] = (bfd_byte)(data >> 16);
725 addr[2] = (bfd_byte)(data >> 8);
726 addr[3] = (bfd_byte)data;
730 bfd_putl32 (data, addr)
732 register bfd_byte *addr;
734 addr[0] = (bfd_byte)data;
735 addr[1] = (bfd_byte)(data >> 8);
736 addr[2] = (bfd_byte)(data >> 16);
737 addr[3] = (bfd_byte)(data >> 24);
741 bfd_putb64 (data, addr)
743 register bfd_byte *addr;
746 addr[0] = (bfd_byte)(data >> (7*8));
747 addr[1] = (bfd_byte)(data >> (6*8));
748 addr[2] = (bfd_byte)(data >> (5*8));
749 addr[3] = (bfd_byte)(data >> (4*8));
750 addr[4] = (bfd_byte)(data >> (3*8));
751 addr[5] = (bfd_byte)(data >> (2*8));
752 addr[6] = (bfd_byte)(data >> (1*8));
753 addr[7] = (bfd_byte)(data >> (0*8));
760 bfd_putl64 (data, addr)
762 register bfd_byte *addr;
765 addr[7] = (bfd_byte)(data >> (7*8));
766 addr[6] = (bfd_byte)(data >> (6*8));
767 addr[5] = (bfd_byte)(data >> (5*8));
768 addr[4] = (bfd_byte)(data >> (4*8));
769 addr[3] = (bfd_byte)(data >> (3*8));
770 addr[2] = (bfd_byte)(data >> (2*8));
771 addr[1] = (bfd_byte)(data >> (1*8));
772 addr[0] = (bfd_byte)(data >> (0*8));
778 /* Default implementation */
781 bfd_generic_get_section_contents (abfd, section, location, offset, count)
790 if ((bfd_size_type)(offset+count) > section->_raw_size
791 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
792 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
793 return (false); /* on error */
797 /* This generic function can only be used in implementations where creating
798 NEW sections is disallowed. It is useful in patching existing sections
799 in read-write files, though. See other set_section_contents functions
800 to see why it doesn't work for new sections. */
802 bfd_generic_set_section_contents (abfd, section, location, offset, count)
812 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
813 || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
824 unsigned int bfd_log2(bfd_vma x);
827 Return the log base 2 of the value supplied, rounded up. E.g., an
828 @var{x} of 1025 returns 11.
836 while ( (bfd_vma)(1<< result) < x)
842 bfd_generic_is_local_label (abfd, sym)
846 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
848 return (sym->name[0] == locals_prefix);