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 _bfd_dummy_new_section_hook (ignore, ignore_newsect)
34 asection *ignore_newsect;
54 bfd_nullvoidptr(ignore)
79 _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
83 bfd_error = invalid_operation;
87 /* of course you can't initialize a function to be the same as another, grr */
90 _bfd_dummy_core_file_failing_command (ignore_abfd)
97 _bfd_dummy_core_file_failing_signal (ignore_abfd)
104 _bfd_dummy_target (ignore_abfd)
110 /** zalloc -- allocate and clear storage */
118 char *ptr = (char *) malloc (size);
120 if ((ptr != NULL) && (size != 0))
130 /* Note that archive entries don't have streams; they share their parent's.
131 This allows someone to play with the iostream behind bfd's back.
133 Also, note that the origin pointer points to the beginning of a file's
134 contents (0 for non-archive elements). For archive entries this is the
135 first octet in the file, NOT the beginning of the archive header. */
138 bfd_read (ptr, size, nitems, abfd)
144 return fread (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
148 bfd_write (ptr, size, nitems, abfd)
154 return fwrite (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
158 bfd_seek (abfd, position, direction)
160 CONST file_ptr position;
163 /* For the time being, a bfd may not seek to it's end. The
164 problem is that we don't easily have a way to recognize
165 the end of an element in an archive. */
167 BFD_ASSERT(direction == SEEK_SET
168 || direction == SEEK_CUR);
170 if (direction == SEEK_SET && abfd->my_archive != NULL)
172 /* This is a set within an archive, so we need to
173 add the base of the object within the archive */
174 return(fseek(bfd_cache_lookup(abfd),
175 position + abfd->origin,
180 return(fseek(bfd_cache_lookup(abfd), position, direction));
190 ptr = ftell (bfd_cache_lookup(abfd));
192 if (abfd->my_archive)
197 /** Make a string table */
199 /* Add string to table pointed to by table, at location starting with free_ptr.
200 resizes the table if necessary (if it's NULL, creates it, ignoring
201 table_length). Updates free_ptr, table, table_length */
204 bfd_add_to_string_table (table, new_string, table_length, free_ptr)
205 char **table, **free_ptr;
207 unsigned int *table_length;
209 size_t string_length = strlen (new_string) + 1; /* include null here */
211 size_t space_length = *table_length;
212 unsigned int offset = (base ? *free_ptr - base : 0);
215 /* Avoid a useless regrow if we can (but of course we still
217 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
218 DEFAULT_STRING_SPACE_SIZE : string_length+1);
219 base = zalloc (space_length);
222 bfd_error = no_memory;
227 if ((size_t)(offset + string_length) >= space_length) {
228 /* Make sure we will have enough space */
229 while ((size_t)(offset + string_length) >= space_length)
230 space_length += space_length/2; /* grow by 50% */
232 base = (char *) realloc (base, space_length);
234 bfd_error = no_memory;
240 memcpy (base + offset, new_string, string_length);
242 *table_length = space_length;
243 *free_ptr = base + offset + string_length;
248 /** The do-it-yourself (byte) sex-change kit */
250 /* The middle letter e.g. get<b>short indicates Big or Little endian
251 target machine. It doesn't matter what the byte order of the host
252 machine is; these routines work for either. */
254 /* FIXME: Should these take a count argument?
255 Answer (gnu@cygnus.com): No, but perhaps they should be inline
256 functions in swap.h #ifdef __GNUC__.
257 Gprof them later and find out. */
261 register bfd_byte *addr;
263 return (addr[0] << 8) | addr[1];
268 register bfd_byte *addr;
270 return (addr[1] << 8) | addr[0];
274 _do_putbshort (data, addr)
275 int data; /* Actually short, but ansi C sucks */
276 register bfd_byte *addr;
278 addr[0] = (bfd_byte)(data >> 8);
279 addr[1] = (bfd_byte )data;
283 _do_putlshort (data, addr)
284 int data; /* Actually short, but ansi C sucks */
285 register bfd_byte *addr;
287 addr[0] = (bfd_byte )data;
288 addr[1] = (bfd_byte)(data >> 8);
293 register bfd_byte *addr;
295 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
300 register bfd_byte *addr;
302 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
306 _do_putblong (data, addr)
308 register bfd_byte *addr;
310 addr[0] = (bfd_byte)(data >> 24);
311 addr[1] = (bfd_byte)(data >> 16);
312 addr[2] = (bfd_byte)(data >> 8);
313 addr[3] = (bfd_byte)data;
317 _do_putllong (data, addr)
319 register bfd_byte *addr;
321 addr[0] = (bfd_byte)data;
322 addr[1] = (bfd_byte)(data >> 8);
323 addr[2] = (bfd_byte)(data >> 16);
324 addr[3] = (bfd_byte)(data >> 24);