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 This file contains various routines which 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 ((int)size);
126 if ((ptr != NULL) && (size != 0))
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);
159 write (2, no_memory_message, sizeof(no_memory_message)-1);
168 /* Note that archive entries don't have streams; they share their parent's.
169 This allows someone to play with the iostream behind BFD's back.
171 Also, note that the origin pointer points to the beginning of a file's
172 contents (0 for non-archive elements). For archive entries this is the
173 first octet in the file, NOT the beginning of the archive header. */
176 int DEFUN(real_read,(where, a,b, file),
182 return fread(where, a,b,file);
185 DEFUN(bfd_read,(ptr, size, nitems, abfd),
187 bfd_size_type size AND
188 bfd_size_type nitems AND
192 nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
193 #ifdef FILE_OFFSET_IS_CHAR_INDEX
195 abfd->where += nread;
201 DEFUN(bfd_write,(ptr, size, nitems, abfd),
203 bfd_size_type size AND
204 bfd_size_type nitems AND
207 int nwrote = fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
208 #ifdef FILE_OFFSET_IS_CHAR_INDEX
210 abfd->where += nwrote;
217 bfd_write_bigendian_4byte_int
220 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
223 Writes a 4 byte integer to the outputing bfd, in big endian
224 mode regardless of what else is going on. This is usefull in
229 DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
234 _do_putb32(i, buffer);
235 bfd_write((PTR)buffer, 4, 1, abfd);
239 DEFUN(bfd_tell,(abfd),
244 ptr = ftell (bfd_cache_lookup(abfd));
246 if (abfd->my_archive)
253 DEFUN(bfd_seek,(abfd, position, direction),
255 CONST file_ptr position AND
260 file_ptr file_position;
261 /* For the time being, a BFD may not seek to it's end. The problem
262 is that we don't easily have a way to recognize the end of an
263 element in an archive. */
265 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
267 if (direction == SEEK_CUR && position == 0)
269 #ifdef FILE_OFFSET_IS_CHAR_INDEX
270 if (abfd->format != bfd_archive && abfd->my_archive == 0)
273 /* Explanation for this code: I'm only about 95+% sure that the above
274 conditions are sufficient and that all i/o calls are properly
275 adjusting the `where' field. So this is sort of an `assert'
276 that the `where' field is correct. If we can go a while without
277 tripping the abort, we can probably safely disable this code,
278 so that the real optimizations happen. */
279 file_ptr where_am_i_now;
280 where_am_i_now = ftell (bfd_cache_lookup (abfd));
281 if (abfd->my_archive)
282 where_am_i_now -= abfd->origin;
283 if (where_am_i_now != abfd->where)
286 if (direction == SEEK_SET && position == abfd->where)
291 /* We need something smarter to optimize access to archives.
292 Currently, anything inside an archive is read via the file
293 handle for the archive. Which means that a bfd_seek on one
294 component affects the `current position' in the archive, as
295 well as in any other component.
297 It might be sufficient to put a spike through the cache
298 abstraction, and look to the archive for the file position,
299 but I think we should try for something cleaner.
301 In the meantime, no optimization for archives. */
305 f = bfd_cache_lookup (abfd);
306 file_position = position;
307 if (direction == SEEK_SET && abfd->my_archive != NULL)
308 file_position += abfd->origin;
310 result = fseek (f, file_position, direction);
313 /* Force redetermination of `where' field. */
317 #ifdef FILE_OFFSET_IS_CHAR_INDEX
318 /* Adjust `where' field. */
319 if (direction == SEEK_SET)
320 abfd->where = position;
322 abfd->where += position;
328 /** Make a string table */
331 Add string to table pointed to by table, at location starting with free_ptr.
332 resizes the table if necessary (if it's NULL, creates it, ignoring
333 table_length). Updates free_ptr, table, table_length */
336 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
339 unsigned int *table_length AND
342 size_t string_length = strlen (new_string) + 1; /* include null here */
344 size_t space_length = *table_length;
345 unsigned int offset = (base ? *free_ptr - base : 0);
348 /* Avoid a useless regrow if we can (but of course we still
350 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
351 DEFAULT_STRING_SPACE_SIZE : string_length+1);
352 base = zalloc (space_length);
355 bfd_error = no_memory;
360 if ((size_t)(offset + string_length) >= space_length) {
361 /* Make sure we will have enough space */
362 while ((size_t)(offset + string_length) >= space_length)
363 space_length += space_length/2; /* grow by 50% */
365 base = (char *) realloc (base, space_length);
367 bfd_error = no_memory;
373 memcpy (base + offset, new_string, string_length);
375 *table_length = space_length;
376 *free_ptr = base + offset + string_length;
381 /** The do-it-yourself (byte) sex-change kit */
383 /* The middle letter e.g. get<b>short indicates Big or Little endian
384 target machine. It doesn't matter what the byte order of the host
385 machine is; these routines work for either. */
387 /* FIXME: Should these take a count argument?
389 functions in swap.h #ifdef __GNUC__.
390 Gprof them later and find out. */
399 These macros as used for reading and writing raw data in
400 sections; each access (except for bytes) is vectored through
401 the target format of the BFD and mangled accordingly. The
402 mangling performs any necessary endian translations and
403 removes alignment restrictions. Note that types accepted and
404 returned by these macros are identical so they can be swapped
405 around in macros--for example libaout.h defines GET_WORD to
406 either bfd_get_32 or bfd_get_64.
408 In the put routines, val must be a bfd_vma. If we are on a
409 system without prototypes, the caller is responsible for making
410 sure that is true, with a cast if necessary. We don't cast
411 them in the macro definitions because that would prevent lint
412 or gcc -Wall from detecting sins such as passing a pointer.
413 To detect calling these with less than a bfd_vma, use gcc
414 -Wconversion on a host with 64 bit bfd_vma's.
416 .#define bfd_put_8(abfd, val, ptr) \
417 . (*((unsigned char *)ptr) = (unsigned char)val)
418 .#define bfd_put_signed_8(abfd, val, ptr) (*((char *)(ptr)) = (char)(val))
419 .#define bfd_get_8(abfd, ptr) \
420 . (*((unsigned char *)(ptr)))
421 .#define bfd_get_signed_8(abfd, ptr) (((*(char *)(ptr) ^ 0x80) & 0xff) - 0x80)
422 .#define bfd_put_16(abfd, val, ptr) \
423 . BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
424 .#define bfd_put_signed_16 bfd_put_16
425 .#define bfd_get_16(abfd, ptr) \
426 . BFD_SEND(abfd, bfd_getx16, (ptr))
427 .#define bfd_get_signed_16(abfd, ptr) \
428 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
429 .#define bfd_put_32(abfd, val, ptr) \
430 . BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
431 .#define bfd_put_signed_32 bfd_put_32
432 .#define bfd_get_32(abfd, ptr) \
433 . BFD_SEND(abfd, bfd_getx32, (ptr))
434 .#define bfd_get_signed_32(abfd, ptr) \
435 . BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
436 .#define bfd_put_64(abfd, val, ptr) \
437 . BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
438 .#define bfd_put_signed_64 bfd_put_64
439 .#define bfd_get_64(abfd, ptr) \
440 . BFD_SEND(abfd, bfd_getx64, (ptr))
441 .#define bfd_get_signed_64(abfd, ptr) \
442 . BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
453 These macros have the same function as their <<bfd_get_x>>
454 bretherin, except that they are used for removing information
455 for the header records of object files. Believe it or not,
456 some object files keep their header records in big endian
457 order, and their data in little endan order.
459 .#define bfd_h_put_8(abfd, val, ptr) \
460 . (*((unsigned char *)ptr) = (unsigned char)val)
461 .#define bfd_h_put_signed_8(abfd, val, ptr) (*((char *)(ptr)) = (char)(val))
462 .#define bfd_h_get_8(abfd, ptr) \
463 . (*((unsigned char *)(ptr)))
464 .#define bfd_h_get_signed_8 bfd_get_signed_8
465 .#define bfd_h_put_16(abfd, val, ptr) \
466 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
467 .#define bfd_h_put_signed_16 bfd_h_put_16
468 .#define bfd_h_get_16(abfd, ptr) \
469 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
470 .#define bfd_h_get_signed_16(abfd, ptr) \
471 . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
472 .#define bfd_h_put_32(abfd, val, ptr) \
473 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
474 .#define bfd_h_put_signed_32 bfd_h_put_32
475 .#define bfd_h_get_32(abfd, ptr) \
476 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
477 .#define bfd_h_get_signed_32(abfd, ptr) \
478 . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
479 .#define bfd_h_put_64(abfd, val, ptr) \
480 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
481 .#define bfd_h_put_signed_64 bfd_h_put_64
482 .#define bfd_h_get_64(abfd, ptr) \
483 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
484 .#define bfd_h_get_signed_64(abfd, ptr) \
485 . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
489 /* Sign extension to bfd_signed_vma. */
490 #define COERCE16(x) ((bfd_signed_vma) (((x) ^ 0x8000) - 0x8000))
491 #define COERCE32(x) ((bfd_signed_vma) (((x) ^ 0x80000000) - 0x80000000))
492 #define COERCE64(x) ((bfd_signed_vma)\
493 (((x) ^ 0x8000000000000000) - 0x8000000000000000))
496 DEFUN(_do_getb16,(addr),
497 register bfd_byte *addr)
499 return (addr[0] << 8) | addr[1];
503 DEFUN(_do_getl16,(addr),
504 register bfd_byte *addr)
506 return (addr[1] << 8) | addr[0];
510 DEFUN(_do_getb_signed_16,(addr),
511 register bfd_byte *addr)
513 return COERCE16((addr[0] << 8) | addr[1]);
517 DEFUN(_do_getl_signed_16,(addr),
518 register bfd_byte *addr)
520 return COERCE16((addr[1] << 8) | addr[0]);
524 DEFUN(_do_putb16,(data, addr),
526 register bfd_byte *addr)
528 addr[0] = (bfd_byte)(data >> 8);
529 addr[1] = (bfd_byte )data;
533 DEFUN(_do_putl16,(data, addr),
535 register bfd_byte *addr)
537 addr[0] = (bfd_byte )data;
538 addr[1] = (bfd_byte)(data >> 8);
543 register bfd_byte *addr;
545 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
550 register bfd_byte *addr;
552 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
556 _do_getb_signed_32 (addr)
557 register bfd_byte *addr;
559 return COERCE32(((((addr[0] << 8) | addr[1]) << 8)
560 | addr[2]) << 8 | addr[3]);
564 _do_getl_signed_32 (addr)
565 register bfd_byte *addr;
567 return COERCE32(((((addr[3] << 8) | addr[2]) << 8)
568 | addr[1]) << 8 | addr[0]);
572 DEFUN(_do_getb64,(addr),
573 register bfd_byte *addr)
578 high= ((((((((addr[0]) << 8) |
583 low = ((((((((addr[4]) << 8) |
588 return high << 32 | low;
597 DEFUN(_do_getl64,(addr),
598 register bfd_byte *addr)
603 high= (((((((addr[7] << 8) |
608 low = (((((((addr[3] << 8) |
613 return high << 32 | low;
622 DEFUN(_do_getb_signed_64,(addr),
623 register bfd_byte *addr)
628 high= ((((((((addr[0]) << 8) |
633 low = ((((((((addr[4]) << 8) |
638 return COERCE64(high << 32 | low);
647 DEFUN(_do_getl_signed_64,(addr),
648 register bfd_byte *addr)
653 high= (((((((addr[7] << 8) |
658 low = (((((((addr[3] << 8) |
663 return COERCE64(high << 32 | low);
672 DEFUN(_do_putb32,(data, addr),
674 register bfd_byte *addr)
676 addr[0] = (bfd_byte)(data >> 24);
677 addr[1] = (bfd_byte)(data >> 16);
678 addr[2] = (bfd_byte)(data >> 8);
679 addr[3] = (bfd_byte)data;
683 DEFUN(_do_putl32,(data, addr),
685 register bfd_byte *addr)
687 addr[0] = (bfd_byte)data;
688 addr[1] = (bfd_byte)(data >> 8);
689 addr[2] = (bfd_byte)(data >> 16);
690 addr[3] = (bfd_byte)(data >> 24);
693 DEFUN(_do_putb64,(data, addr),
695 register bfd_byte *addr)
698 addr[0] = (bfd_byte)(data >> (7*8));
699 addr[1] = (bfd_byte)(data >> (6*8));
700 addr[2] = (bfd_byte)(data >> (5*8));
701 addr[3] = (bfd_byte)(data >> (4*8));
702 addr[4] = (bfd_byte)(data >> (3*8));
703 addr[5] = (bfd_byte)(data >> (2*8));
704 addr[6] = (bfd_byte)(data >> (1*8));
705 addr[7] = (bfd_byte)(data >> (0*8));
713 DEFUN(_do_putl64,(data, addr),
715 register bfd_byte *addr)
718 addr[7] = (bfd_byte)(data >> (7*8));
719 addr[6] = (bfd_byte)(data >> (6*8));
720 addr[5] = (bfd_byte)(data >> (5*8));
721 addr[4] = (bfd_byte)(data >> (4*8));
722 addr[3] = (bfd_byte)(data >> (3*8));
723 addr[2] = (bfd_byte)(data >> (2*8));
724 addr[1] = (bfd_byte)(data >> (1*8));
725 addr[0] = (bfd_byte)(data >> (0*8));
733 /* Default implementation */
736 DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
745 if ((bfd_size_type)(offset+count) > section->_raw_size
746 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
747 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
748 return (false); /* on error */
752 /* This generic function can only be used in implementations where creating
753 NEW sections is disallowed. It is useful in patching existing sections
754 in read-write files, though. See other set_section_contents functions
755 to see why it doesn't work for new sections. */
757 DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
766 if ((bfd_size_type)(offset+count) > bfd_get_section_size_after_reloc(section)
767 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
768 || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
769 return (false); /* on error */
778 Return the log base 2 of the value supplied, rounded up. eg an
779 arg of 1025 would return 11.
782 bfd_vma bfd_log2(bfd_vma x);
789 while ( (bfd_vma)(1<< result) < x)