Thu May 9 11:00:45 1991 Steve Chamberlain (steve at cygint.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / libbfd.c
CommitLineData
4a81b561
DHW
1/* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
2
3This file is part of BFD, the Binary File Diddler.
4
5BFD is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 1, or (at your option)
8any later version.
9
10BFD is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BFD; see the file COPYING. If not, write to
17the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19/* $Id$ */
20
21/*** libbfd.c -- random bfd support routines used internally only. */
22#include "sysdep.h"
23#include "bfd.h"
24#include "libbfd.h"
25
26
27\f
28/** Dummies for targets that don't want or need to implement
29 certain operations */
30
31boolean
32_bfd_dummy_new_section_hook (ignore, ignore_newsect)
33 bfd *ignore;
34 asection *ignore_newsect;
35{
36 return true;
37}
38
39boolean
40bfd_false (ignore)
41 bfd *ignore;
42{
43 return false;
44}
45
46boolean
47bfd_true (ignore)
48 bfd *ignore;
49{
50 return true;
51}
52
d0ec7a8e 53PTR
4a81b561
DHW
54bfd_nullvoidptr(ignore)
55bfd *ignore;
56{
d0ec7a8e 57 return (PTR)NULL;
4a81b561 58}
fc723380 59
4a81b561
DHW
60int
61bfd_0(ignore)
62bfd *ignore;
63{
64 return 0;
65}
fc723380 66
4a81b561
DHW
67unsigned int
68bfd_0u(ignore)
69bfd *ignore;
70{
71 return 0;
72}
73
74void
75bfd_void(ignore)
76bfd *ignore;
77{
78}
79
80boolean
81_bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
82 bfd *ignore_core_bfd;
83bfd *ignore_exec_bfd;
84{
85 bfd_error = invalid_operation;
86 return false;
87}
88
89/* of course you can't initialize a function to be the same as another, grr */
90
91char *
92_bfd_dummy_core_file_failing_command (ignore_abfd)
93 bfd *ignore_abfd;
94{
95 return (char *)NULL;
96}
97
98int
99_bfd_dummy_core_file_failing_signal (ignore_abfd)
100 bfd *ignore_abfd;
101{
102 return 0;
103}
104
105bfd_target *
106_bfd_dummy_target (ignore_abfd)
107 bfd *ignore_abfd;
108{
109 return 0;
110}
111\f
112/** zalloc -- allocate and clear storage */
113
114
115#ifndef zalloc
116char *
117zalloc (size)
ede87e29 118 bfd_size_type size;
4a81b561 119{
9846338e 120 char *ptr = (char *) malloc (size);
4a81b561
DHW
121
122 if ((ptr != NULL) && (size != 0))
301dfc71 123 memset(ptr,0, size);
4a81b561
DHW
124
125 return ptr;
126}
127#endif
128\f
129/* Some IO code */
130
131
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.
134
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. */
138
23b0b558 139bfd_size_type
4a81b561 140bfd_read (ptr, size, nitems, abfd)
9846338e 141 PTR ptr;
23b0b558
JG
142 bfd_size_type size;
143 bfd_size_type nitems;
4a81b561
DHW
144 bfd *abfd;
145{
146 return fread (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
147}
148
23b0b558 149bfd_size_type
4a81b561 150bfd_write (ptr, size, nitems, abfd)
9846338e 151 PTR ptr;
23b0b558
JG
152 bfd_size_type size;
153 bfd_size_type nitems;
4a81b561
DHW
154 bfd *abfd;
155{
9846338e 156 return fwrite (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
4a81b561
DHW
157}
158
159int
160bfd_seek (abfd, position, direction)
9846338e
SC
161bfd * CONST abfd;
162CONST file_ptr position;
163CONST int direction;
4a81b561
DHW
164{
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. */
168
169 BFD_ASSERT(direction == SEEK_SET
170 || direction == SEEK_CUR);
171
172 if (direction == SEEK_SET && abfd->my_archive != NULL)
173 {
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,
178 direction));
179 }
180 else
181 {
182 return(fseek(bfd_cache_lookup(abfd), position, direction));
183 }
184}
185
186long
187bfd_tell (abfd)
188 bfd *abfd;
189{
190 file_ptr ptr;
191
192 ptr = ftell (bfd_cache_lookup(abfd));
193
194 if (abfd->my_archive)
195 ptr -= abfd->origin;
196 return ptr;
197}
198\f
199/** Make a string table */
200
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 */
204
205boolean
206bfd_add_to_string_table (table, new_string, table_length, free_ptr)
207 char **table, **free_ptr;
208 char *new_string;
209 unsigned int *table_length;
210{
211 size_t string_length = strlen (new_string) + 1; /* include null here */
212 char *base = *table;
213 size_t space_length = *table_length;
214 unsigned int offset = (base ? *free_ptr - base : 0);
215
216 if (base == NULL) {
217 /* Avoid a useless regrow if we can (but of course we still
218 take it next time */
219 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
220 DEFAULT_STRING_SPACE_SIZE : string_length+1);
221 base = zalloc (space_length);
222
223 if (base == NULL) {
224 bfd_error = no_memory;
225 return false;
226 }
227 }
228
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% */
233
234 base = (char *) realloc (base, space_length);
235 if (base == NULL) {
236 bfd_error = no_memory;
237 return false;
238 }
239
240 }
241
242 memcpy (base + offset, new_string, string_length);
243 *table = base;
244 *table_length = space_length;
245 *free_ptr = base + offset + string_length;
246
247 return true;
248}
249\f
250/** The do-it-yourself (byte) sex-change kit */
251
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. */
255
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. */
260
261short
262_do_getbshort (addr)
263 register bfd_byte *addr;
264{
265 return (addr[0] << 8) | addr[1];
266}
267
268short
269_do_getlshort (addr)
270 register bfd_byte *addr;
271{
272 return (addr[1] << 8) | addr[0];
273}
274
275void
276_do_putbshort (data, addr)
277 int data; /* Actually short, but ansi C sucks */
278 register bfd_byte *addr;
279{
280 addr[0] = (bfd_byte)(data >> 8);
281 addr[1] = (bfd_byte )data;
282}
283
284void
285_do_putlshort (data, addr)
286 int data; /* Actually short, but ansi C sucks */
287 register bfd_byte *addr;
288{
289 addr[0] = (bfd_byte )data;
290 addr[1] = (bfd_byte)(data >> 8);
291}
292
293long
294_do_getblong (addr)
295 register bfd_byte *addr;
296{
297 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
298}
299
300long
301_do_getllong (addr)
302 register bfd_byte *addr;
303{
304 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
305}
306
307void
308_do_putblong (data, addr)
309 unsigned long data;
310 register bfd_byte *addr;
311{
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;
316}
317
318void
319_do_putllong (data, addr)
320 unsigned long data;
321 register bfd_byte *addr;
322{
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);
327}
328
329
330
331
332
333
334
This page took 0.041597 seconds and 4 git commands to generate.