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 bfd_write((PTR)buffer, 4, 1, abfd);
260 ptr = ftell (bfd_cache_lookup(abfd));
262 if (abfd->my_archive)
272 return fflush (bfd_cache_lookup(abfd));
276 bfd_stat (abfd, statbuf)
278 struct stat *statbuf;
280 return fstat (fileno(bfd_cache_lookup(abfd)), statbuf);
283 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
284 can retrieve the error code). */
287 bfd_seek (abfd, position, direction)
289 CONST file_ptr position;
294 file_ptr file_position;
295 /* For the time being, a BFD may not seek to it's end. The problem
296 is that we don't easily have a way to recognize the end of an
297 element in an archive. */
299 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
301 if (direction == SEEK_CUR && position == 0)
303 #ifdef FILE_OFFSET_IS_CHAR_INDEX
304 if (abfd->format != bfd_archive && abfd->my_archive == 0)
307 /* Explanation for this code: I'm only about 95+% sure that the above
308 conditions are sufficient and that all i/o calls are properly
309 adjusting the `where' field. So this is sort of an `assert'
310 that the `where' field is correct. If we can go a while without
311 tripping the abort, we can probably safely disable this code,
312 so that the real optimizations happen. */
313 file_ptr where_am_i_now;
314 where_am_i_now = ftell (bfd_cache_lookup (abfd));
315 if (abfd->my_archive)
316 where_am_i_now -= abfd->origin;
317 if (where_am_i_now != abfd->where)
320 if (direction == SEEK_SET && position == abfd->where)
325 /* We need something smarter to optimize access to archives.
326 Currently, anything inside an archive is read via the file
327 handle for the archive. Which means that a bfd_seek on one
328 component affects the `current position' in the archive, as
329 well as in any other component.
331 It might be sufficient to put a spike through the cache
332 abstraction, and look to the archive for the file position,
333 but I think we should try for something cleaner.
335 In the meantime, no optimization for archives. */
339 f = bfd_cache_lookup (abfd);
340 file_position = position;
341 if (direction == SEEK_SET && abfd->my_archive != NULL)
342 file_position += abfd->origin;
344 result = fseek (f, file_position, direction);
348 /* Force redetermination of `where' field. */
350 bfd_set_error (bfd_error_system_call);
354 #ifdef FILE_OFFSET_IS_CHAR_INDEX
355 /* Adjust `where' field. */
356 if (direction == SEEK_SET)
357 abfd->where = position;
359 abfd->where += position;
365 /** Make a string table */
368 Add string to table pointed to by table, at location starting with free_ptr.
369 resizes the table if necessary (if it's NULL, creates it, ignoring
370 table_length). Updates free_ptr, table, table_length */
373 bfd_add_to_string_table (table, new_string, table_length, free_ptr)
376 unsigned int *table_length;
379 size_t string_length = strlen (new_string) + 1; /* include null here */
381 size_t space_length = *table_length;
382 unsigned int offset = (base ? *free_ptr - base : 0);
385 /* Avoid a useless regrow if we can (but of course we still
386 take it next time). */
387 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
388 DEFAULT_STRING_SPACE_SIZE : string_length+1);
389 base = bfd_zmalloc ((bfd_size_type) space_length);
392 bfd_set_error (bfd_error_no_memory);
397 if ((size_t)(offset + string_length) >= space_length) {
398 /* Make sure we will have enough space */
399 while ((size_t)(offset + string_length) >= space_length)
400 space_length += space_length/2; /* grow by 50% */
402 base = (char *) realloc (base, space_length);
404 bfd_set_error (bfd_error_no_memory);
410 memcpy (base + offset, new_string, string_length);
412 *table_length = space_length;
413 *free_ptr = base + offset + string_length;
418 /** The do-it-yourself (byte) sex-change kit */
420 /* The middle letter e.g. get<b>short indicates Big or Little endian
421 target machine. It doesn't matter what the byte order of the host
422 machine is; these routines work for either. */
424 /* FIXME: Should these take a count argument?
425 Answer (gnu@cygnus.com): No, but perhaps they should be inline
426 functions in swap.h #ifdef __GNUC__.
427 Gprof them later and find out. */
436 These macros as used for reading and writing raw data in
437 sections; each access (except for bytes) is vectored through
438 the target format of the BFD and mangled accordingly. The
439 mangling performs any necessary endian translations and
440 removes alignment restrictions. Note that types accepted and
441 returned by these macros are identical so they can be swapped
442 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
443 to either <<bfd_get_32>> or <<bfd_get_64>>.
445 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
446 system without prototypes, the caller is responsible for making
447 sure that is true, with a cast if necessary. We don't cast
448 them in the macro definitions because that would prevent <<lint>>
449 or <<gcc -Wall>> from detecting sins such as passing a pointer.
450 To detect calling these with less than a <<bfd_vma>>, use
451 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
454 .{* Byte swapping macros for user section data. *}
456 .#define bfd_put_8(abfd, val, ptr) \
457 . (*((unsigned char *)(ptr)) = (unsigned char)(val))
458 .#define bfd_put_signed_8 \
460 .#define bfd_get_8(abfd, ptr) \
461 . (*(unsigned char *)(ptr))
462 .#define bfd_get_signed_8(abfd, ptr) \
463 . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
465 .#define bfd_put_16(abfd, val, ptr) \
466 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
467 .#define bfd_put_signed_16 \
469 .#define bfd_get_16(abfd, ptr) \
470 . BFD_SEND(abfd, bfd_getx16, (ptr))
471 .#define bfd_get_signed_16(abfd, ptr) \
472 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
474 .#define bfd_put_32(abfd, val, ptr) \
475 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
476 .#define bfd_put_signed_32 \
478 .#define bfd_get_32(abfd, ptr) \
479 . BFD_SEND(abfd, bfd_getx32, (ptr))
480 .#define bfd_get_signed_32(abfd, ptr) \
481 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
483 .#define bfd_put_64(abfd, val, ptr) \
484 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
485 .#define bfd_put_signed_64 \
487 .#define bfd_get_64(abfd, ptr) \
488 . BFD_SEND(abfd, bfd_getx64, (ptr))
489 .#define bfd_get_signed_64(abfd, ptr) \
490 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
500 These macros have the same function as their <<bfd_get_x>>
501 bretheren, except that they are used for removing information
502 for the header records of object files. Believe it or not,
503 some object files keep their header records in big endian
504 order and their data in little endian order.
506 .{* Byte swapping macros for file header data. *}
508 .#define bfd_h_put_8(abfd, val, ptr) \
509 . bfd_put_8 (abfd, val, ptr)
510 .#define bfd_h_put_signed_8(abfd, val, ptr) \
511 . bfd_put_8 (abfd, val, ptr)
512 .#define bfd_h_get_8(abfd, ptr) \
513 . bfd_get_8 (abfd, ptr)
514 .#define bfd_h_get_signed_8(abfd, ptr) \
515 . bfd_get_signed_8 (abfd, ptr)
517 .#define bfd_h_put_16(abfd, val, ptr) \
518 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
519 .#define bfd_h_put_signed_16 \
521 .#define bfd_h_get_16(abfd, ptr) \
522 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
523 .#define bfd_h_get_signed_16(abfd, ptr) \
524 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
526 .#define bfd_h_put_32(abfd, val, ptr) \
527 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
528 .#define bfd_h_put_signed_32 \
530 .#define bfd_h_get_32(abfd, ptr) \
531 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
532 .#define bfd_h_get_signed_32(abfd, ptr) \
533 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
535 .#define bfd_h_put_64(abfd, val, ptr) \
536 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
537 .#define bfd_h_put_signed_64 \
539 .#define bfd_h_get_64(abfd, ptr) \
540 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
541 .#define bfd_h_get_signed_64(abfd, ptr) \
542 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
546 /* Sign extension to bfd_signed_vma. */
547 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
548 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
549 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
550 #define COERCE64(x) \
551 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
555 register const bfd_byte *addr;
557 return (addr[0] << 8) | addr[1];
562 register const bfd_byte *addr;
564 return (addr[1] << 8) | addr[0];
568 bfd_getb_signed_16 (addr)
569 register const bfd_byte *addr;
571 return COERCE16((addr[0] << 8) | addr[1]);
575 bfd_getl_signed_16 (addr)
576 register const bfd_byte *addr;
578 return COERCE16((addr[1] << 8) | addr[0]);
582 bfd_putb16 (data, addr)
584 register bfd_byte *addr;
586 addr[0] = (bfd_byte)(data >> 8);
587 addr[1] = (bfd_byte )data;
591 bfd_putl16 (data, addr)
593 register bfd_byte *addr;
595 addr[0] = (bfd_byte )data;
596 addr[1] = (bfd_byte)(data >> 8);
601 register const bfd_byte *addr;
603 return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
604 | addr[2]) << 8 | addr[3];
609 register const bfd_byte *addr;
611 return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
612 | addr[1]) << 8 | addr[0];
616 bfd_getb_signed_32 (addr)
617 register const bfd_byte *addr;
619 return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
620 | addr[2]) << 8 | addr[3]);
624 bfd_getl_signed_32 (addr)
625 register const bfd_byte *addr;
627 return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
628 | addr[1]) << 8 | addr[0]);
633 register const bfd_byte *addr;
638 high= ((((((((addr[0]) << 8) |
643 low = (((((((((bfd_vma)addr[4]) << 8) |
648 return high << 32 | low;
657 register const bfd_byte *addr;
661 high= (((((((addr[7] << 8) |
666 low = ((((((((bfd_vma)addr[3] << 8) |
671 return high << 32 | low;
680 bfd_getb_signed_64 (addr)
681 register const bfd_byte *addr;
686 high= ((((((((addr[0]) << 8) |
691 low = (((((((((bfd_vma)addr[4]) << 8) |
696 return COERCE64(high << 32 | low);
704 bfd_getl_signed_64 (addr)
705 register const bfd_byte *addr;
709 high= (((((((addr[7] << 8) |
714 low = ((((((((bfd_vma)addr[3] << 8) |
719 return COERCE64(high << 32 | low);
727 bfd_putb32 (data, addr)
729 register bfd_byte *addr;
731 addr[0] = (bfd_byte)(data >> 24);
732 addr[1] = (bfd_byte)(data >> 16);
733 addr[2] = (bfd_byte)(data >> 8);
734 addr[3] = (bfd_byte)data;
738 bfd_putl32 (data, addr)
740 register bfd_byte *addr;
742 addr[0] = (bfd_byte)data;
743 addr[1] = (bfd_byte)(data >> 8);
744 addr[2] = (bfd_byte)(data >> 16);
745 addr[3] = (bfd_byte)(data >> 24);
749 bfd_putb64 (data, addr)
751 register bfd_byte *addr;
754 addr[0] = (bfd_byte)(data >> (7*8));
755 addr[1] = (bfd_byte)(data >> (6*8));
756 addr[2] = (bfd_byte)(data >> (5*8));
757 addr[3] = (bfd_byte)(data >> (4*8));
758 addr[4] = (bfd_byte)(data >> (3*8));
759 addr[5] = (bfd_byte)(data >> (2*8));
760 addr[6] = (bfd_byte)(data >> (1*8));
761 addr[7] = (bfd_byte)(data >> (0*8));
768 bfd_putl64 (data, addr)
770 register bfd_byte *addr;
773 addr[7] = (bfd_byte)(data >> (7*8));
774 addr[6] = (bfd_byte)(data >> (6*8));
775 addr[5] = (bfd_byte)(data >> (5*8));
776 addr[4] = (bfd_byte)(data >> (4*8));
777 addr[3] = (bfd_byte)(data >> (3*8));
778 addr[2] = (bfd_byte)(data >> (2*8));
779 addr[1] = (bfd_byte)(data >> (1*8));
780 addr[0] = (bfd_byte)(data >> (0*8));
786 /* Default implementation */
789 bfd_generic_get_section_contents (abfd, section, location, offset, count)
798 if ((bfd_size_type)(offset+count) > section->_raw_size
799 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
800 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
801 return (false); /* on error */
805 /* This generic function can only be used in implementations where creating
806 NEW sections is disallowed. It is useful in patching existing sections
807 in read-write files, though. See other set_section_contents functions
808 to see why it doesn't work for new sections. */
810 bfd_generic_set_section_contents (abfd, section, location, offset, count)
820 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
821 || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
832 unsigned int bfd_log2(bfd_vma x);
835 Return the log base 2 of the value supplied, rounded up. E.g., an
836 @var{x} of 1025 returns 11.
844 while ( (bfd_vma)(1<< result) < x)
850 bfd_generic_is_local_label (abfd, sym)
854 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
856 return (sym->name[0] == locals_prefix);