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)
81 _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
85 bfd_error = invalid_operation;
89 /* of course you can't initialize a function to be the same as another, grr */
92 _bfd_dummy_core_file_failing_command (ignore_abfd)
99 _bfd_dummy_core_file_failing_signal (ignore_abfd)
106 _bfd_dummy_target (ignore_abfd)
112 /** zalloc -- allocate and clear storage */
120 char *ptr = (char *) malloc (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 bfd_read (ptr, size, nitems, abfd)
143 bfd_size_type nitems;
146 return fread (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
150 bfd_write (ptr, size, nitems, abfd)
153 bfd_size_type nitems;
156 return fwrite (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
160 bfd_seek (abfd, position, direction)
162 CONST file_ptr position;
165 /* For the time being, a bfd may not seek to it's end. The
166 problem is that we don't easily have a way to recognize
167 the end of an element in an archive. */
169 BFD_ASSERT(direction == SEEK_SET
170 || direction == SEEK_CUR);
172 if (direction == SEEK_SET && abfd->my_archive != NULL)
174 /* This is a set within an archive, so we need to
175 add the base of the object within the archive */
176 return(fseek(bfd_cache_lookup(abfd),
177 position + abfd->origin,
182 return(fseek(bfd_cache_lookup(abfd), position, direction));
192 ptr = ftell (bfd_cache_lookup(abfd));
194 if (abfd->my_archive)
199 /** Make a string table */
201 /* Add string to table pointed to by table, at location starting with free_ptr.
202 resizes the table if necessary (if it's NULL, creates it, ignoring
203 table_length). Updates free_ptr, table, table_length */
206 bfd_add_to_string_table (table, new_string, table_length, free_ptr)
207 char **table, **free_ptr;
209 unsigned int *table_length;
211 size_t string_length = strlen (new_string) + 1; /* include null here */
213 size_t space_length = *table_length;
214 unsigned int offset = (base ? *free_ptr - base : 0);
217 /* Avoid a useless regrow if we can (but of course we still
219 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
220 DEFAULT_STRING_SPACE_SIZE : string_length+1);
221 base = zalloc (space_length);
224 bfd_error = no_memory;
229 if ((size_t)(offset + string_length) >= space_length) {
230 /* Make sure we will have enough space */
231 while ((size_t)(offset + string_length) >= space_length)
232 space_length += space_length/2; /* grow by 50% */
234 base = (char *) realloc (base, space_length);
236 bfd_error = no_memory;
242 memcpy (base + offset, new_string, string_length);
244 *table_length = space_length;
245 *free_ptr = base + offset + string_length;
250 /** The do-it-yourself (byte) sex-change kit */
252 /* The middle letter e.g. get<b>short indicates Big or Little endian
253 target machine. It doesn't matter what the byte order of the host
254 machine is; these routines work for either. */
256 /* FIXME: Should these take a count argument?
257 Answer (gnu@cygnus.com): No, but perhaps they should be inline
258 functions in swap.h #ifdef __GNUC__.
259 Gprof them later and find out. */
263 register bfd_byte *addr;
265 return (addr[0] << 8) | addr[1];
270 register bfd_byte *addr;
272 return (addr[1] << 8) | addr[0];
276 _do_putbshort (data, addr)
277 int data; /* Actually short, but ansi C sucks */
278 register bfd_byte *addr;
280 addr[0] = (bfd_byte)(data >> 8);
281 addr[1] = (bfd_byte )data;
285 _do_putlshort (data, addr)
286 int data; /* Actually short, but ansi C sucks */
287 register bfd_byte *addr;
289 addr[0] = (bfd_byte )data;
290 addr[1] = (bfd_byte)(data >> 8);
295 register bfd_byte *addr;
297 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
302 register bfd_byte *addr;
304 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
308 _do_putblong (data, addr)
310 register bfd_byte *addr;
312 addr[0] = (bfd_byte)(data >> 24);
313 addr[1] = (bfd_byte)(data >> 16);
314 addr[2] = (bfd_byte)(data >> 8);
315 addr[3] = (bfd_byte)data;
319 _do_putllong (data, addr)
321 register bfd_byte *addr;
323 addr[0] = (bfd_byte)data;
324 addr[1] = (bfd_byte)(data >> 8);
325 addr[2] = (bfd_byte)(data >> 16);
326 addr[3] = (bfd_byte)(data >> 24);