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. */
27 /** Dummies for targets that don't want or need to implement
31 DEFUN(_bfd_dummy_new_section_hook,(ignore, ignore_newsect),
33 asection *ignore_newsect)
39 DEFUN(bfd_false ,(ignore),
46 DEFUN(bfd_true,(ignore),
53 DEFUN(bfd_nullvoidptr,(ignore),
67 DEFUN(bfd_0u,(ignore),
74 DEFUN(bfd_void,(ignore),
80 DEFUN(_bfd_dummy_core_file_matches_executable_p,(ignore_core_bfd, ignore_exec_bfd),
81 bfd *ignore_core_bfd AND
84 bfd_error = invalid_operation;
88 /* of course you can't initialize a function to be the same as another, grr */
91 DEFUN(_bfd_dummy_core_file_failing_command,(ignore_abfd),
98 DEFUN(_bfd_dummy_core_file_failing_signal,(ignore_abfd),
105 DEFUN(_bfd_dummy_target,(ignore_abfd),
111 /** zalloc -- allocate and clear storage */
119 char *ptr = (char *) malloc ((int)size);
121 if ((ptr != NULL) && (size != 0))
128 /*proto-internal* bfd_xmalloc
129 bfd_xmalloc -- Like malloc, but exit if no more memory.
130 *; PROTO(PTR, bfd_xmalloc,( bfd_size_type size));
132 /** There is major inconsistency in how running out of memory is handled.
133 Some routines return a NULL, and set bfd_error to no_memory.
134 However, obstack routines can't do this ... */
137 DEFUN(PTR bfd_xmalloc,(size),
140 static char no_memory_message[] = "Virtual memory exhausted!\n";
142 if (size == 0) size = 1;
143 ptr = (PTR)malloc(size);
147 write (2, no_memory_message, sizeof(no_memory_message)-1);
156 /* Note that archive entries don't have streams; they share their parent's.
157 This allows someone to play with the iostream behind BFD's back.
159 Also, note that the origin pointer points to the beginning of a file's
160 contents (0 for non-archive elements). For archive entries this is the
161 first octet in the file, NOT the beginning of the archive header. */
164 int DEFUN(real_read,(where, a,b, file),
170 return fread(where, a,b,file);
173 DEFUN(bfd_read,(ptr, size, nitems, abfd),
175 bfd_size_type size AND
176 bfd_size_type nitems AND
179 return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
183 DEFUN(bfd_write,(ptr, size, nitems, abfd),
185 bfd_size_type size AND
186 bfd_size_type nitems AND
189 return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
192 /*proto-internal* bfd_write_bigendian_4byte_int
194 *; PROTO(void, bfd_write_bigendian_4byte_int,( bfd *abfd, int i));
197 DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
202 _do_putb32(i, buffer);
203 bfd_write((PTR)buffer, 4, 1, abfd);
207 DEFUN(bfd_seek,(abfd, position, direction),
209 CONST file_ptr position AND
212 /* For the time being, a BFD may not seek to it's end. The
213 problem is that we don't easily have a way to recognize
214 the end of an element in an archive. */
216 BFD_ASSERT(direction == SEEK_SET
217 || direction == SEEK_CUR);
219 if (direction == SEEK_SET && abfd->my_archive != NULL)
221 /* This is a set within an archive, so we need to
222 add the base of the object within the archive */
223 return(fseek(bfd_cache_lookup(abfd),
224 position + abfd->origin,
229 return(fseek(bfd_cache_lookup(abfd), position, direction));
234 DEFUN(bfd_tell,(abfd),
239 ptr = ftell (bfd_cache_lookup(abfd));
241 if (abfd->my_archive)
246 /** Make a string table */
249 Add string to table pointed to by table, at location starting with free_ptr.
250 resizes the table if necessary (if it's NULL, creates it, ignoring
251 table_length). Updates free_ptr, table, table_length */
254 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
257 unsigned int *table_length AND
260 size_t string_length = strlen (new_string) + 1; /* include null here */
262 size_t space_length = *table_length;
263 unsigned int offset = (base ? *free_ptr - base : 0);
266 /* Avoid a useless regrow if we can (but of course we still
268 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
269 DEFAULT_STRING_SPACE_SIZE : string_length+1);
270 base = zalloc (space_length);
273 bfd_error = no_memory;
278 if ((size_t)(offset + string_length) >= space_length) {
279 /* Make sure we will have enough space */
280 while ((size_t)(offset + string_length) >= space_length)
281 space_length += space_length/2; /* grow by 50% */
283 base = (char *) realloc (base, space_length);
285 bfd_error = no_memory;
291 memcpy (base + offset, new_string, string_length);
293 *table_length = space_length;
294 *free_ptr = base + offset + string_length;
299 /** The do-it-yourself (byte) sex-change kit */
301 /* The middle letter e.g. get<b>short indicates Big or Little endian
302 target machine. It doesn't matter what the byte order of the host
303 machine is; these routines work for either. */
305 /* FIXME: Should these take a count argument?
307 functions in swap.h #ifdef __GNUC__.
308 Gprof them later and find out. */
313 These macros as used for reading and writing raw data in sections;
314 each access (except for bytes) is vectored through the target format
315 of the BFD and mangled accordingly. The mangling performs any
316 necessary endian translations and removes alignment restrictions.
318 #define bfd_put_8(abfd, val, ptr) \
319 (*((char *)ptr) = (char)val)
320 #define bfd_get_8(abfd, ptr) \
322 #define bfd_put_16(abfd, val, ptr) \
323 BFD_SEND(abfd, bfd_putx16, (val,ptr))
324 #define bfd_get_16(abfd, ptr) \
325 BFD_SEND(abfd, bfd_getx16, (ptr))
326 #define bfd_put_32(abfd, val, ptr) \
327 BFD_SEND(abfd, bfd_putx32, (val,ptr))
328 #define bfd_get_32(abfd, ptr) \
329 BFD_SEND(abfd, bfd_getx32, (ptr))
330 #define bfd_put_64(abfd, val, ptr) \
331 BFD_SEND(abfd, bfd_putx64, (val, ptr))
332 #define bfd_get_64(abfd, ptr) \
333 BFD_SEND(abfd, bfd_getx64, (ptr))
340 These macros have the same function as their @code{bfd_get_x}
341 bretherin, except that they are used for removing information for the
342 header records of object files. Believe it or not, some object files
343 keep their header records in big endian order, and their data in little
346 #define bfd_h_put_8(abfd, val, ptr) \
347 (*((char *)ptr) = (char)val)
348 #define bfd_h_get_8(abfd, ptr) \
350 #define bfd_h_put_16(abfd, val, ptr) \
351 BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
352 #define bfd_h_get_16(abfd, ptr) \
353 BFD_SEND(abfd, bfd_h_getx16,(ptr))
354 #define bfd_h_put_32(abfd, val, ptr) \
355 BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
356 #define bfd_h_get_32(abfd, ptr) \
357 BFD_SEND(abfd, bfd_h_getx32,(ptr))
358 #define bfd_h_put_64(abfd, val, ptr) \
359 BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
360 #define bfd_h_get_64(abfd, ptr) \
361 BFD_SEND(abfd, bfd_h_getx64,(ptr))
366 DEFUN(_do_getb16,(addr),
367 register bfd_byte *addr)
369 return (addr[0] << 8) | addr[1];
373 DEFUN(_do_getl16,(addr),
374 register bfd_byte *addr)
376 return (addr[1] << 8) | addr[0];
380 DEFUN(_do_putb16,(data, addr),
382 register bfd_byte *addr)
384 addr[0] = (bfd_byte)(data >> 8);
385 addr[1] = (bfd_byte )data;
389 DEFUN(_do_putl16,(data, addr),
391 register bfd_byte *addr)
393 addr[0] = (bfd_byte )data;
394 addr[1] = (bfd_byte)(data >> 8);
398 DEFUN(_do_getb32,(addr),
399 register bfd_byte *addr)
401 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
406 register bfd_byte *addr;
408 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
412 DEFUN(_do_getb64,(addr),
413 register bfd_byte *addr)
416 bfd_64_type low, high;
418 high= ((((((((addr[0]) << 8) |
423 low = ((((((((addr[4]) << 8) |
428 return high << 32 | low;
437 DEFUN(_do_getl64,(addr),
438 register bfd_byte *addr)
442 bfd_64_type low, high;
443 high= (((((((addr[7] << 8) |
448 low = (((((((addr[3] << 8) |
453 return high << 32 | low;
462 DEFUN(_do_putb32,(data, addr),
464 register bfd_byte *addr)
466 addr[0] = (bfd_byte)(data >> 24);
467 addr[1] = (bfd_byte)(data >> 16);
468 addr[2] = (bfd_byte)(data >> 8);
469 addr[3] = (bfd_byte)data;
473 DEFUN(_do_putl32,(data, addr),
475 register bfd_byte *addr)
477 addr[0] = (bfd_byte)data;
478 addr[1] = (bfd_byte)(data >> 8);
479 addr[2] = (bfd_byte)(data >> 16);
480 addr[3] = (bfd_byte)(data >> 24);
483 DEFUN(_do_putb64,(data, addr),
485 register bfd_byte *addr)
488 addr[0] = (bfd_byte)(data >> (7*8));
489 addr[1] = (bfd_byte)(data >> (6*8));
490 addr[2] = (bfd_byte)(data >> (5*8));
491 addr[3] = (bfd_byte)(data >> (4*8));
492 addr[4] = (bfd_byte)(data >> (3*8));
493 addr[5] = (bfd_byte)(data >> (2*8));
494 addr[6] = (bfd_byte)(data >> (1*8));
495 addr[7] = (bfd_byte)(data >> (0*8));
503 DEFUN(_do_putl64,(data, addr),
505 register bfd_byte *addr)
508 addr[7] = (bfd_byte)(data >> (7*8));
509 addr[6] = (bfd_byte)(data >> (6*8));
510 addr[5] = (bfd_byte)(data >> (5*8));
511 addr[4] = (bfd_byte)(data >> (4*8));
512 addr[3] = (bfd_byte)(data >> (3*8));
513 addr[2] = (bfd_byte)(data >> (2*8));
514 addr[1] = (bfd_byte)(data >> (1*8));
515 addr[0] = (bfd_byte)(data >> (0*8));
523 /* Default implementation */
526 DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
535 if ((bfd_size_type)(offset+count) > section->size
536 || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
537 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
538 return (false); /* on error */
542 /* This generic function can only be used in implementations where creating
543 NEW sections is disallowed. It is useful in patching existing sections
544 in read-write files, though. See other set_section_contents functions
545 to see why it doesn't work for new sections. */
547 DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
556 if ((bfd_size_type)(offset+count) > section->size
557 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
558 || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
559 return (false); /* on error */
565 Return the log base 2 of the value supplied, rounded up. eg an arg
566 of 1025 would return 11.
567 *; PROTO(bfd_vma, bfd_log2,(bfd_vma x));
574 while ( (bfd_vma)(1<< result) < x)