1 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
3 This file is part of BFD, the Binary File Diddler.
5 BFD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 BFD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with BFD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /*** libbfd.c -- random bfd support routines used internally only. */
28 /** Dummies for targets that don't want or need to implement
32 DEFUN(_bfd_dummy_new_section_hook,(ignore, ignore_newsect),
34 asection *ignore_newsect)
40 DEFUN(bfd_false ,(ignore),
47 DEFUN(bfd_true,(ignore),
54 DEFUN(bfd_nullvoidptr,(ignore),
68 DEFUN(bfd_0u,(ignore),
75 DEFUN(bfd_void,(ignore),
81 DEFUN(_bfd_dummy_core_file_matches_executable_p,(ignore_core_bfd, ignore_exec_bfd),
82 bfd *ignore_core_bfd AND
85 bfd_error = invalid_operation;
89 /* of course you can't initialize a function to be the same as another, grr */
92 DEFUN(_bfd_dummy_core_file_failing_command,(ignore_abfd),
99 DEFUN(_bfd_dummy_core_file_failing_signal,(ignore_abfd),
106 DEFUN(_bfd_dummy_target,(ignore_abfd),
112 /** zalloc -- allocate and clear storage */
120 char *ptr = (char *) malloc ((int)size);
122 if ((ptr != NULL) && (size != 0))
132 /* Note that archive entries don't have streams; they share their parent's.
133 This allows someone to play with the iostream behind bfd's back.
135 Also, note that the origin pointer points to the beginning of a file's
136 contents (0 for non-archive elements). For archive entries this is the
137 first octet in the file, NOT the beginning of the archive header. */
140 int DEFUN(real_read,(where, a,b, file),
146 return fread(where, a,b,file);
149 DEFUN(bfd_read,(ptr, size, nitems, abfd),
151 bfd_size_type size AND
152 bfd_size_type nitems AND
155 return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
159 DEFUN(bfd_write,(ptr, size, nitems, abfd),
161 bfd_size_type size AND
162 bfd_size_type nitems AND
165 return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
169 DEFUN(bfd_seek,(abfd, position, direction),
171 CONST file_ptr position AND
174 /* For the time being, a bfd may not seek to it's end. The
175 problem is that we don't easily have a way to recognize
176 the end of an element in an archive. */
178 BFD_ASSERT(direction == SEEK_SET
179 || direction == SEEK_CUR);
181 if (direction == SEEK_SET && abfd->my_archive != NULL)
183 /* This is a set within an archive, so we need to
184 add the base of the object within the archive */
185 return(fseek(bfd_cache_lookup(abfd),
186 position + abfd->origin,
191 return(fseek(bfd_cache_lookup(abfd), position, direction));
196 DEFUN(bfd_tell,(abfd),
201 ptr = ftell (bfd_cache_lookup(abfd));
203 if (abfd->my_archive)
208 /** Make a string table */
211 Add string to table pointed to by table, at location starting with free_ptr.
212 resizes the table if necessary (if it's NULL, creates it, ignoring
213 table_length). Updates free_ptr, table, table_length */
216 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
219 unsigned int *table_length AND
222 size_t string_length = strlen (new_string) + 1; /* include null here */
224 size_t space_length = *table_length;
225 unsigned int offset = (base ? *free_ptr - base : 0);
228 /* Avoid a useless regrow if we can (but of course we still
230 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
231 DEFAULT_STRING_SPACE_SIZE : string_length+1);
232 base = zalloc (space_length);
235 bfd_error = no_memory;
240 if ((size_t)(offset + string_length) >= space_length) {
241 /* Make sure we will have enough space */
242 while ((size_t)(offset + string_length) >= space_length)
243 space_length += space_length/2; /* grow by 50% */
245 base = (char *) realloc (base, space_length);
247 bfd_error = no_memory;
253 memcpy (base + offset, new_string, string_length);
255 *table_length = space_length;
256 *free_ptr = base + offset + string_length;
261 /** The do-it-yourself (byte) sex-change kit */
263 /* The middle letter e.g. get<b>short indicates Big or Little endian
264 target machine. It doesn't matter what the byte order of the host
265 machine is; these routines work for either. */
267 /* FIXME: Should these take a count argument?
268 Answer (gnu@cygnus.com): No, but perhaps they should be inline
269 functions in swap.h #ifdef __GNUC__.
270 Gprof them later and find out. */
275 These macros as used for reading and writing raw data in sections;
276 each access (except for bytes) is vectored through the target format
277 of the bfd and mangled accordingly. The mangling performs any
278 necessary endian translations and removes alignment restrictions.
280 #define bfd_put_8(abfd, val, ptr) \
281 (*((char *)ptr) = (char)val)
282 #define bfd_get_8(abfd, ptr) \
284 #define bfd_put_16(abfd, val, ptr) \
285 BFD_SEND(abfd, bfd_putx16, (val,ptr))
286 #define bfd_get_16(abfd, ptr) \
287 BFD_SEND(abfd, bfd_getx16, (ptr))
288 #define bfd_put_32(abfd, val, ptr) \
289 BFD_SEND(abfd, bfd_putx32, (val,ptr))
290 #define bfd_get_32(abfd, ptr) \
291 BFD_SEND(abfd, bfd_getx32, (ptr))
292 #define bfd_put_64(abfd, val, ptr) \
293 BFD_SEND(abfd, bfd_putx64, (val, ptr))
294 #define bfd_get_64(abfd, ptr) \
295 BFD_SEND(abfd, bfd_getx64, (ptr))
302 These macros have the same function as their @code{bfd_get_x}
303 bretherin, except that they are used for removing information for the
304 header records of object files. Believe it or not, some object files
305 keep their header records in big endian order, and their data in little
308 #define bfd_h_put_8(abfd, val, ptr) \
309 (*((char *)ptr) = (char)val)
310 #define bfd_h_get_8(abfd, ptr) \
312 #define bfd_h_put_16(abfd, val, ptr) \
313 BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
314 #define bfd_h_get_16(abfd, ptr) \
315 BFD_SEND(abfd, bfd_h_getx16,(ptr))
316 #define bfd_h_put_32(abfd, val, ptr) \
317 BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
318 #define bfd_h_get_32(abfd, ptr) \
319 BFD_SEND(abfd, bfd_h_getx32,(ptr))
320 #define bfd_h_put_64(abfd, val, ptr) \
321 BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
322 #define bfd_h_get_64(abfd, ptr) \
323 BFD_SEND(abfd, bfd_h_getx64,(ptr))
328 DEFUN(_do_getb16,(addr),
329 register bfd_byte *addr)
331 return (addr[0] << 8) | addr[1];
335 DEFUN(_do_getl16,(addr),
336 register bfd_byte *addr)
338 return (addr[1] << 8) | addr[0];
342 DEFUN(_do_putb16,(data, addr),
344 register bfd_byte *addr)
346 addr[0] = (bfd_byte)(data >> 8);
347 addr[1] = (bfd_byte )data;
351 DEFUN(_do_putl16,(data, addr),
353 register bfd_byte *addr)
355 addr[0] = (bfd_byte )data;
356 addr[1] = (bfd_byte)(data >> 8);
360 DEFUN(_do_getb32,(addr),
361 register bfd_byte *addr)
363 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
368 register bfd_byte *addr;
370 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
374 DEFUN(_do_getb64,(addr),
375 register bfd_byte *addr)
378 bfd_64_type low, high;
380 high= ((((((((addr[0]) << 8) |
385 low = ((((((((addr[4]) << 8) |
390 return high << 32 | low;
400 DEFUN(_do_getl64,(addr),
401 register bfd_byte *addr)
405 bfd_64_type low, high;
406 high= (((((((addr[7] << 8) |
411 low = (((((((addr[3] << 8) |
416 return high << 32 | low;
426 DEFUN(_do_putb32,(data, addr),
427 unsigned long data AND
428 register bfd_byte *addr)
430 addr[0] = (bfd_byte)(data >> 24);
431 addr[1] = (bfd_byte)(data >> 16);
432 addr[2] = (bfd_byte)(data >> 8);
433 addr[3] = (bfd_byte)data;
437 DEFUN(_do_putl32,(data, addr),
438 unsigned long data AND
439 register bfd_byte *addr)
441 addr[0] = (bfd_byte)data;
442 addr[1] = (bfd_byte)(data >> 8);
443 addr[2] = (bfd_byte)(data >> 16);
444 addr[3] = (bfd_byte)(data >> 24);
447 DEFUN(_do_putb64,(data, addr),
449 register bfd_byte *addr)
452 addr[0] = (bfd_byte)(data >> (7*8));
453 addr[1] = (bfd_byte)(data >> (6*8));
454 addr[2] = (bfd_byte)(data >> (5*8));
455 addr[3] = (bfd_byte)(data >> (4*8));
456 addr[4] = (bfd_byte)(data >> (3*8));
457 addr[5] = (bfd_byte)(data >> (2*8));
458 addr[6] = (bfd_byte)(data >> (1*8));
459 addr[7] = (bfd_byte)(data >> (0*8));
467 DEFUN(_do_putl64,(data, addr),
469 register bfd_byte *addr)
472 addr[7] = (bfd_byte)(data >> (7*8));
473 addr[6] = (bfd_byte)(data >> (6*8));
474 addr[5] = (bfd_byte)(data >> (5*8));
475 addr[4] = (bfd_byte)(data >> (4*8));
476 addr[3] = (bfd_byte)(data >> (3*8));
477 addr[2] = (bfd_byte)(data >> (2*8));
478 addr[1] = (bfd_byte)(data >> (1*8));
479 addr[0] = (bfd_byte)(data >> (0*8));
487 /* Default implementation */
490 DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
499 if ((bfd_size_type)offset >= section->size
500 || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
501 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
502 return (false); /* on error */
508 Return the log base 2 of the value supplied, rounded up. eg an arg
509 of 1025 would return 11.
510 *; PROTO(bfd_vma, bfd_log2,(bfd_vma x));
517 while ( (bfd_vma)(1<< result) < x)