1 /* libbfd.c -- random BFD support routines, only used internally.
2 Copyright (C) 1990-1991 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
191 return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
195 DEFUN(bfd_write,(ptr, size, nitems, abfd),
197 bfd_size_type size AND
198 bfd_size_type nitems AND
201 return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
206 bfd_write_bigendian_4byte_int
209 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
212 Writes a 4 byte integer to the outputing bfd, in big endian
213 mode regardless of what else is going on. This is usefull in
218 DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
223 _do_putb32(i, buffer);
224 bfd_write((PTR)buffer, 4, 1, abfd);
228 DEFUN(bfd_seek,(abfd, position, direction),
230 CONST file_ptr position AND
233 /* For the time being, a BFD may not seek to it's end. The
234 problem is that we don't easily have a way to recognize
235 the end of an element in an archive. */
237 BFD_ASSERT(direction == SEEK_SET
238 || direction == SEEK_CUR);
240 if (direction == SEEK_SET && abfd->my_archive != NULL)
242 /* This is a set within an archive, so we need to
243 add the base of the object within the archive */
244 return(fseek(bfd_cache_lookup(abfd),
245 position + abfd->origin,
250 return(fseek(bfd_cache_lookup(abfd), position, direction));
255 DEFUN(bfd_tell,(abfd),
260 ptr = ftell (bfd_cache_lookup(abfd));
262 if (abfd->my_archive)
267 /** Make a string table */
270 Add string to table pointed to by table, at location starting with free_ptr.
271 resizes the table if necessary (if it's NULL, creates it, ignoring
272 table_length). Updates free_ptr, table, table_length */
275 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
278 unsigned int *table_length AND
281 size_t string_length = strlen (new_string) + 1; /* include null here */
283 size_t space_length = *table_length;
284 unsigned int offset = (base ? *free_ptr - base : 0);
287 /* Avoid a useless regrow if we can (but of course we still
289 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
290 DEFAULT_STRING_SPACE_SIZE : string_length+1);
291 base = zalloc (space_length);
294 bfd_error = no_memory;
299 if ((size_t)(offset + string_length) >= space_length) {
300 /* Make sure we will have enough space */
301 while ((size_t)(offset + string_length) >= space_length)
302 space_length += space_length/2; /* grow by 50% */
304 base = (char *) realloc (base, space_length);
306 bfd_error = no_memory;
312 memcpy (base + offset, new_string, string_length);
314 *table_length = space_length;
315 *free_ptr = base + offset + string_length;
320 /** The do-it-yourself (byte) sex-change kit */
322 /* The middle letter e.g. get<b>short indicates Big or Little endian
323 target machine. It doesn't matter what the byte order of the host
324 machine is; these routines work for either. */
326 /* FIXME: Should these take a count argument?
328 functions in swap.h #ifdef __GNUC__.
329 Gprof them later and find out. */
338 These macros as used for reading and writing raw data in
339 sections; each access (except for bytes) is vectored through
340 the target format of the BFD and mangled accordingly. The
341 mangling performs any necessary endian translations and
342 removes alignment restrictions.
344 .#define bfd_put_8(abfd, val, ptr) \
345 . (*((char *)ptr) = (char)val)
346 .#define bfd_get_8(abfd, ptr) \
348 .#define bfd_put_16(abfd, val, ptr) \
349 . BFD_SEND(abfd, bfd_putx16, (val,ptr))
350 .#define bfd_get_16(abfd, ptr) \
351 . BFD_SEND(abfd, bfd_getx16, (ptr))
352 .#define bfd_put_32(abfd, val, ptr) \
353 . BFD_SEND(abfd, bfd_putx32, (val,ptr))
354 .#define bfd_get_32(abfd, ptr) \
355 . BFD_SEND(abfd, bfd_getx32, (ptr))
356 .#define bfd_put_64(abfd, val, ptr) \
357 . BFD_SEND(abfd, bfd_putx64, (val, ptr))
358 .#define bfd_get_64(abfd, ptr) \
359 . BFD_SEND(abfd, bfd_getx64, (ptr))
370 These macros have the same function as their <<bfd_get_x>>
371 bretherin, except that they are used for removing information
372 for the header records of object files. Believe it or not,
373 some object files keep their header records in big endian
374 order, and their data in little endan order.
376 .#define bfd_h_put_8(abfd, val, ptr) \
377 . (*((char *)ptr) = (char)val)
378 .#define bfd_h_get_8(abfd, ptr) \
380 .#define bfd_h_put_16(abfd, val, ptr) \
381 . BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
382 .#define bfd_h_get_16(abfd, ptr) \
383 . BFD_SEND(abfd, bfd_h_getx16,(ptr))
384 .#define bfd_h_put_32(abfd, val, ptr) \
385 . BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
386 .#define bfd_h_get_32(abfd, ptr) \
387 . BFD_SEND(abfd, bfd_h_getx32,(ptr))
388 .#define bfd_h_put_64(abfd, val, ptr) \
389 . BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
390 .#define bfd_h_get_64(abfd, ptr) \
391 . BFD_SEND(abfd, bfd_h_getx64,(ptr))
396 DEFUN(_do_getb16,(addr),
397 register bfd_byte *addr)
399 return (addr[0] << 8) | addr[1];
403 DEFUN(_do_getl16,(addr),
404 register bfd_byte *addr)
406 return (addr[1] << 8) | addr[0];
410 DEFUN(_do_putb16,(data, addr),
412 register bfd_byte *addr)
414 addr[0] = (bfd_byte)(data >> 8);
415 addr[1] = (bfd_byte )data;
419 DEFUN(_do_putl16,(data, addr),
421 register bfd_byte *addr)
423 addr[0] = (bfd_byte )data;
424 addr[1] = (bfd_byte)(data >> 8);
428 DEFUN(_do_getb32,(addr),
429 register bfd_byte *addr)
431 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
436 register bfd_byte *addr;
438 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
442 DEFUN(_do_getb64,(addr),
443 register bfd_byte *addr)
446 bfd_64_type low, high;
448 high= ((((((((addr[0]) << 8) |
453 low = ((((((((addr[4]) << 8) |
458 return high << 32 | low;
467 DEFUN(_do_getl64,(addr),
468 register bfd_byte *addr)
472 bfd_64_type low, high;
473 high= (((((((addr[7] << 8) |
478 low = (((((((addr[3] << 8) |
483 return high << 32 | low;
492 DEFUN(_do_putb32,(data, addr),
494 register bfd_byte *addr)
496 addr[0] = (bfd_byte)(data >> 24);
497 addr[1] = (bfd_byte)(data >> 16);
498 addr[2] = (bfd_byte)(data >> 8);
499 addr[3] = (bfd_byte)data;
503 DEFUN(_do_putl32,(data, addr),
505 register bfd_byte *addr)
507 addr[0] = (bfd_byte)data;
508 addr[1] = (bfd_byte)(data >> 8);
509 addr[2] = (bfd_byte)(data >> 16);
510 addr[3] = (bfd_byte)(data >> 24);
513 DEFUN(_do_putb64,(data, addr),
515 register bfd_byte *addr)
518 addr[0] = (bfd_byte)(data >> (7*8));
519 addr[1] = (bfd_byte)(data >> (6*8));
520 addr[2] = (bfd_byte)(data >> (5*8));
521 addr[3] = (bfd_byte)(data >> (4*8));
522 addr[4] = (bfd_byte)(data >> (3*8));
523 addr[5] = (bfd_byte)(data >> (2*8));
524 addr[6] = (bfd_byte)(data >> (1*8));
525 addr[7] = (bfd_byte)(data >> (0*8));
533 DEFUN(_do_putl64,(data, addr),
535 register bfd_byte *addr)
538 addr[7] = (bfd_byte)(data >> (7*8));
539 addr[6] = (bfd_byte)(data >> (6*8));
540 addr[5] = (bfd_byte)(data >> (5*8));
541 addr[4] = (bfd_byte)(data >> (4*8));
542 addr[3] = (bfd_byte)(data >> (3*8));
543 addr[2] = (bfd_byte)(data >> (2*8));
544 addr[1] = (bfd_byte)(data >> (1*8));
545 addr[0] = (bfd_byte)(data >> (0*8));
553 /* Default implementation */
556 DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
565 if ((bfd_size_type)(offset+count) > section->_raw_size
566 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
567 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
568 return (false); /* on error */
572 /* This generic function can only be used in implementations where creating
573 NEW sections is disallowed. It is useful in patching existing sections
574 in read-write files, though. See other set_section_contents functions
575 to see why it doesn't work for new sections. */
577 DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
586 if ((bfd_size_type)(offset+count) > bfd_get_section_size_after_reloc(section)
587 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
588 || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
589 return (false); /* on error */
598 Return the log base 2 of the value supplied, rounded up. eg an
599 arg of 1025 would return 11.
602 bfd_vma bfd_log2(bfd_vma x);
609 while ( (bfd_vma)(1<< result) < x)