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 */
210 /* Add string to table pointed to by table, at location starting with free_ptr.
211 resizes the table if necessary (if it's NULL, creates it, ignoring
212 table_length). Updates free_ptr, table, table_length */
215 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
218 unsigned int *table_length AND
221 size_t string_length = strlen (new_string) + 1; /* include null here */
223 size_t space_length = *table_length;
224 unsigned int offset = (base ? *free_ptr - base : 0);
227 /* Avoid a useless regrow if we can (but of course we still
229 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
230 DEFAULT_STRING_SPACE_SIZE : string_length+1);
231 base = zalloc (space_length);
234 bfd_error = no_memory;
239 if ((size_t)(offset + string_length) >= space_length) {
240 /* Make sure we will have enough space */
241 while ((size_t)(offset + string_length) >= space_length)
242 space_length += space_length/2; /* grow by 50% */
244 base = (char *) realloc (base, space_length);
246 bfd_error = no_memory;
252 memcpy (base + offset, new_string, string_length);
254 *table_length = space_length;
255 *free_ptr = base + offset + string_length;
260 /** The do-it-yourself (byte) sex-change kit */
262 /* The middle letter e.g. get<b>short indicates Big or Little endian
263 target machine. It doesn't matter what the byte order of the host
264 machine is; these routines work for either. */
266 /* FIXME: Should these take a count argument?
267 Answer (gnu@cygnus.com): No, but perhaps they should be inline
268 functions in swap.h #ifdef __GNUC__.
269 Gprof them later and find out. */
272 DEFUN(_do_getb16,(addr),
273 register bfd_byte *addr)
275 return (addr[0] << 8) | addr[1];
279 DEFUN(_do_getl16,(addr),
280 register bfd_byte *addr)
282 return (addr[1] << 8) | addr[0];
286 DEFUN(_do_putb16,(data, addr),
288 register bfd_byte *addr)
290 addr[0] = (bfd_byte)(data >> 8);
291 addr[1] = (bfd_byte )data;
295 DEFUN(_do_putl16,(data, addr),
297 register bfd_byte *addr)
299 addr[0] = (bfd_byte )data;
300 addr[1] = (bfd_byte)(data >> 8);
304 DEFUN(_do_getb32,(addr),
305 register bfd_byte *addr)
307 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
312 register bfd_byte *addr;
314 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
318 DEFUN(_do_getb64,(addr),
319 register bfd_byte *addr)
322 bfd_64_type low, high;
324 high= ((((((((addr[0]) << 8) |
329 low = ((((((((addr[4]) << 8) |
334 return high << 32 | low;
342 DEFUN(_do_getl64,(addr),
343 register bfd_byte *addr)
345 bfd_64_type low, high;
347 high= (((((((addr[7] << 8) |
352 low = (((((((addr[3] << 8) |
357 return high << 32 | low;
364 DEFUN(_do_putb32,(data, addr),
365 unsigned long data AND
366 register bfd_byte *addr)
368 addr[0] = (bfd_byte)(data >> 24);
369 addr[1] = (bfd_byte)(data >> 16);
370 addr[2] = (bfd_byte)(data >> 8);
371 addr[3] = (bfd_byte)data;
375 DEFUN(_do_putl32,(data, addr),
376 unsigned long data AND
377 register bfd_byte *addr)
379 addr[0] = (bfd_byte)data;
380 addr[1] = (bfd_byte)(data >> 8);
381 addr[2] = (bfd_byte)(data >> 16);
382 addr[3] = (bfd_byte)(data >> 24);
385 DEFUN(_do_putb64,(data, addr),
387 register bfd_byte *addr)
390 addr[0] = (bfd_byte)(data >> (7*8));
391 addr[1] = (bfd_byte)(data >> (6*8));
392 addr[2] = (bfd_byte)(data >> (5*8));
393 addr[3] = (bfd_byte)(data >> (4*8));
394 addr[4] = (bfd_byte)(data >> (3*8));
395 addr[5] = (bfd_byte)(data >> (2*8));
396 addr[6] = (bfd_byte)(data >> (1*8));
397 addr[7] = (bfd_byte)(data >> (0*8));
405 DEFUN(_do_putl64,(data, addr),
407 register bfd_byte *addr)
410 addr[7] = (bfd_byte)(data >> (7*8));
411 addr[6] = (bfd_byte)(data >> (6*8));
412 addr[5] = (bfd_byte)(data >> (5*8));
413 addr[4] = (bfd_byte)(data >> (4*8));
414 addr[3] = (bfd_byte)(data >> (3*8));
415 addr[2] = (bfd_byte)(data >> (2*8));
416 addr[1] = (bfd_byte)(data >> (1*8));
417 addr[0] = (bfd_byte)(data >> (0*8));
425 /* Default implementation */
428 DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
437 if ((bfd_size_type)offset >= section->size
438 || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
439 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
440 return (false); /* on error */