Initial revision
[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. */
7ed4093a 22#include <sysdep.h>
4a81b561
DHW
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
536b27a5
SC
32DEFUN(_bfd_dummy_new_section_hook,(ignore, ignore_newsect),
33 bfd *ignore AND
34 asection *ignore_newsect)
4a81b561
DHW
35{
36 return true;
37}
38
39boolean
536b27a5
SC
40DEFUN(bfd_false ,(ignore),
41 bfd *ignore)
4a81b561
DHW
42{
43 return false;
44}
45
46boolean
536b27a5
SC
47DEFUN(bfd_true,(ignore),
48 bfd *ignore)
4a81b561
DHW
49{
50 return true;
51}
52
d0ec7a8e 53PTR
536b27a5
SC
54DEFUN(bfd_nullvoidptr,(ignore),
55 bfd *ignore)
4a81b561 56{
d0ec7a8e 57 return (PTR)NULL;
4a81b561 58}
fc723380 59
4a81b561 60int
536b27a5
SC
61DEFUN(bfd_0,(ignore),
62 bfd *ignore)
4a81b561
DHW
63{
64 return 0;
65}
fc723380 66
4a81b561 67unsigned int
536b27a5
SC
68DEFUN(bfd_0u,(ignore),
69 bfd *ignore)
4a81b561
DHW
70{
71 return 0;
72}
73
74void
536b27a5
SC
75DEFUN(bfd_void,(ignore),
76 bfd *ignore)
4a81b561
DHW
77{
78}
79
80boolean
536b27a5
SC
81DEFUN(_bfd_dummy_core_file_matches_executable_p,(ignore_core_bfd, ignore_exec_bfd),
82 bfd *ignore_core_bfd AND
83 bfd *ignore_exec_bfd)
4a81b561
DHW
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 *
0f268757 92DEFUN(_bfd_dummy_core_file_failing_command,(ignore_abfd),
536b27a5 93 bfd *ignore_abfd)
4a81b561
DHW
94{
95 return (char *)NULL;
96}
97
98int
536b27a5
SC
99DEFUN(_bfd_dummy_core_file_failing_signal,(ignore_abfd),
100 bfd *ignore_abfd)
4a81b561
DHW
101{
102 return 0;
103}
104
105bfd_target *
536b27a5
SC
106DEFUN(_bfd_dummy_target,(ignore_abfd),
107 bfd *ignore_abfd)
4a81b561
DHW
108{
109 return 0;
110}
111\f
112/** zalloc -- allocate and clear storage */
113
114
115#ifndef zalloc
116char *
536b27a5
SC
117DEFUN(zalloc,(size),
118 bfd_size_type size)
4a81b561 119{
7ed4093a 120 char *ptr = (char *) malloc ((int)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
7ed4093a
SC
139static
140int DEFUN(real_read,(where, a,b, file),
141 PTR where AND
142 int a AND
143 int b AND
144 FILE *file)
145{
146 return fread(where, a,b,file);
147}
23b0b558 148bfd_size_type
7ed4093a
SC
149DEFUN(bfd_read,(ptr, size, nitems, abfd),
150 PTR ptr AND
151 bfd_size_type size AND
152 bfd_size_type nitems AND
153 bfd *abfd)
4a81b561 154{
7ed4093a 155 return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
4a81b561
DHW
156}
157
23b0b558 158bfd_size_type
7ed4093a
SC
159DEFUN(bfd_write,(ptr, size, nitems, abfd),
160 PTR ptr AND
161 bfd_size_type size AND
162 bfd_size_type nitems AND
163 bfd *abfd)
4a81b561 164{
7ed4093a 165 return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
4a81b561
DHW
166}
167
168int
7ed4093a
SC
169DEFUN(bfd_seek,(abfd, position, direction),
170 bfd * CONST abfd AND
171 CONST file_ptr position AND
172 CONST int direction)
4a81b561
DHW
173{
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. */
177
178 BFD_ASSERT(direction == SEEK_SET
179 || direction == SEEK_CUR);
180
181 if (direction == SEEK_SET && abfd->my_archive != NULL)
182 {
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,
187 direction));
188 }
189 else
190 {
191 return(fseek(bfd_cache_lookup(abfd), position, direction));
192 }
193}
194
195long
536b27a5
SC
196DEFUN(bfd_tell,(abfd),
197 bfd *abfd)
4a81b561
DHW
198{
199 file_ptr ptr;
200
201 ptr = ftell (bfd_cache_lookup(abfd));
202
203 if (abfd->my_archive)
204 ptr -= abfd->origin;
205 return ptr;
206}
207\f
208/** Make a string table */
209
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 */
213
214boolean
536b27a5
SC
215DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
216 char **table AND
536b27a5 217 char *new_string AND
5ad1d830
SC
218 unsigned int *table_length AND
219 char **free_ptr)
4a81b561
DHW
220{
221 size_t string_length = strlen (new_string) + 1; /* include null here */
222 char *base = *table;
223 size_t space_length = *table_length;
224 unsigned int offset = (base ? *free_ptr - base : 0);
225
226 if (base == NULL) {
227 /* Avoid a useless regrow if we can (but of course we still
228 take it next time */
229 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
230 DEFAULT_STRING_SPACE_SIZE : string_length+1);
231 base = zalloc (space_length);
232
233 if (base == NULL) {
234 bfd_error = no_memory;
235 return false;
236 }
237 }
238
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% */
243
244 base = (char *) realloc (base, space_length);
245 if (base == NULL) {
246 bfd_error = no_memory;
247 return false;
248 }
249
250 }
251
252 memcpy (base + offset, new_string, string_length);
253 *table = base;
254 *table_length = space_length;
255 *free_ptr = base + offset + string_length;
256
257 return true;
258}
259\f
260/** The do-it-yourself (byte) sex-change kit */
261
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. */
265
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. */
270
7ed4093a 271unsigned int
536b27a5
SC
272DEFUN(_do_getb16,(addr),
273 register bfd_byte *addr)
4a81b561
DHW
274{
275 return (addr[0] << 8) | addr[1];
276}
277
7ed4093a 278unsigned int
536b27a5
SC
279DEFUN(_do_getl16,(addr),
280 register bfd_byte *addr)
4a81b561
DHW
281{
282 return (addr[1] << 8) | addr[0];
283}
284
285void
536b27a5
SC
286DEFUN(_do_putb16,(data, addr),
287 int data AND
288 register bfd_byte *addr)
4a81b561
DHW
289{
290 addr[0] = (bfd_byte)(data >> 8);
291 addr[1] = (bfd_byte )data;
292}
293
294void
536b27a5
SC
295DEFUN(_do_putl16,(data, addr),
296 int data AND
297 register bfd_byte *addr)
4a81b561
DHW
298{
299 addr[0] = (bfd_byte )data;
300 addr[1] = (bfd_byte)(data >> 8);
301}
302
7ed4093a 303unsigned int
536b27a5
SC
304DEFUN(_do_getb32,(addr),
305 register bfd_byte *addr)
4a81b561
DHW
306{
307 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
308}
309
7ed4093a
SC
310unsigned int
311_do_getl32 (addr)
4a81b561
DHW
312 register bfd_byte *addr;
313{
314 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
315}
316
7ed4093a 317bfd_64_type
536b27a5
SC
318DEFUN(_do_getb64,(addr),
319 register bfd_byte *addr)
7ed4093a 320{
536b27a5 321#ifdef HOST_64_BIT
7ed4093a 322 bfd_64_type low, high;
536b27a5 323
7ed4093a
SC
324 high= ((((((((addr[0]) << 8) |
325 addr[1]) << 8) |
326 addr[2]) << 8) |
327 addr[3]) );
328
329 low = ((((((((addr[4]) << 8) |
330 addr[5]) << 8) |
331 addr[6]) << 8) |
332 addr[7]));
333
334 return high << 32 | low;
335#else
336 BFD_FAIL();
337#endif
338
339}
340
341bfd_64_type
5ad1d830 342DEFUN(_do_getl64,(addr),
536b27a5 343 register bfd_byte *addr)
7ed4093a
SC
344{
345 bfd_64_type low, high;
536b27a5 346#ifdef HOST_64_BIT
7ed4093a
SC
347 high= (((((((addr[7] << 8) |
348 addr[6]) << 8) |
349 addr[5]) << 8) |
350 addr[4]));
351
352 low = (((((((addr[3] << 8) |
353 addr[2]) << 8) |
354 addr[1]) << 8) |
355 addr[0]) );
356
357 return high << 32 | low;
358#else
359 BFD_FAIL();
360#endif
361}
362
4a81b561 363void
536b27a5
SC
364DEFUN(_do_putb32,(data, addr),
365 unsigned long data AND
366 register bfd_byte *addr)
4a81b561
DHW
367{
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;
372}
373
374void
536b27a5
SC
375DEFUN(_do_putl32,(data, addr),
376 unsigned long data AND
377 register bfd_byte *addr)
4a81b561
DHW
378{
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);
383}
7ed4093a 384void
536b27a5
SC
385DEFUN(_do_putb64,(data, addr),
386 bfd_64_type data AND
387 register bfd_byte *addr)
7ed4093a 388{
536b27a5
SC
389#ifdef HOST_64_BIT
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));
7ed4093a 398#else
536b27a5 399 BFD_FAIL();
7ed4093a
SC
400#endif
401
402}
403
404void
536b27a5
SC
405DEFUN(_do_putl64,(data, addr),
406 bfd_64_type data AND
407 register bfd_byte *addr)
7ed4093a 408{
536b27a5
SC
409#ifdef HOST_64_BIT
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));
7ed4093a 418#else
536b27a5 419 BFD_FAIL();
7ed4093a
SC
420#endif
421
422}
423
2203f786
JG
424\f
425/* Default implementation */
4a81b561 426
2203f786 427boolean
7ed4093a
SC
428DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
429 bfd *abfd AND
430 sec_ptr section AND
431 PTR location AND
432 file_ptr offset AND
433 bfd_size_type count)
2203f786
JG
434{
435 if (count == 0)
436 return true;
7ed4093a
SC
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)
2203f786
JG
440 return (false); /* on error */
441 return (true);
442}
This page took 0.072482 seconds and 4 git commands to generate.