* Makefile.in (MAKEOVERRIDES): Define to be empty.
[deliverable/binutils-gdb.git] / bfd / libbfd.c
CommitLineData
f58809fd
SC
1/* libbfd.c -- random BFD support routines, only used internally.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4a81b561 4
f58809fd 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
f58809fd 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
f58809fd
SC
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
f58809fd 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
f58809fd
SC
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4a81b561 20
4a81b561 21#include "bfd.h"
f58809fd 22#include "sysdep.h"
4a81b561
DHW
23#include "libbfd.h"
24
f8e01940
JG
25/*
26SECTION
27 libbfd
28
29DESCRIPTION
30 This file contains various routines which are used within BFD.
31 They are not intended for export, but are documented here for
32 completeness.
33*/
4a81b561
DHW
34
35boolean
536b27a5
SC
36DEFUN(_bfd_dummy_new_section_hook,(ignore, ignore_newsect),
37 bfd *ignore AND
38 asection *ignore_newsect)
4a81b561
DHW
39{
40 return true;
41}
42
43boolean
536b27a5
SC
44DEFUN(bfd_false ,(ignore),
45 bfd *ignore)
4a81b561
DHW
46{
47 return false;
48}
49
50boolean
536b27a5
SC
51DEFUN(bfd_true,(ignore),
52 bfd *ignore)
4a81b561
DHW
53{
54 return true;
55}
56
d0ec7a8e 57PTR
536b27a5
SC
58DEFUN(bfd_nullvoidptr,(ignore),
59 bfd *ignore)
4a81b561 60{
d0ec7a8e 61 return (PTR)NULL;
4a81b561 62}
fc723380 63
4a81b561 64int
536b27a5
SC
65DEFUN(bfd_0,(ignore),
66 bfd *ignore)
4a81b561
DHW
67{
68 return 0;
69}
fc723380 70
4a81b561 71unsigned int
536b27a5
SC
72DEFUN(bfd_0u,(ignore),
73 bfd *ignore)
4a81b561
DHW
74{
75 return 0;
76}
77
78void
536b27a5
SC
79DEFUN(bfd_void,(ignore),
80 bfd *ignore)
4a81b561
DHW
81{
82}
83
84boolean
536b27a5
SC
85DEFUN(_bfd_dummy_core_file_matches_executable_p,(ignore_core_bfd, ignore_exec_bfd),
86 bfd *ignore_core_bfd AND
87 bfd *ignore_exec_bfd)
4a81b561
DHW
88{
89 bfd_error = invalid_operation;
90 return false;
91}
92
93/* of course you can't initialize a function to be the same as another, grr */
94
95char *
0f268757 96DEFUN(_bfd_dummy_core_file_failing_command,(ignore_abfd),
536b27a5 97 bfd *ignore_abfd)
4a81b561
DHW
98{
99 return (char *)NULL;
100}
101
102int
536b27a5
SC
103DEFUN(_bfd_dummy_core_file_failing_signal,(ignore_abfd),
104 bfd *ignore_abfd)
4a81b561
DHW
105{
106 return 0;
107}
108
109bfd_target *
536b27a5
SC
110DEFUN(_bfd_dummy_target,(ignore_abfd),
111 bfd *ignore_abfd)
4a81b561
DHW
112{
113 return 0;
114}
115\f
116/** zalloc -- allocate and clear storage */
117
118
119#ifndef zalloc
120char *
536b27a5
SC
121DEFUN(zalloc,(size),
122 bfd_size_type size)
4a81b561 123{
7ed4093a 124 char *ptr = (char *) malloc ((int)size);
4a81b561
DHW
125
126 if ((ptr != NULL) && (size != 0))
301dfc71 127 memset(ptr,0, size);
4a81b561
DHW
128
129 return ptr;
130}
131#endif
1e310759 132
f8e01940
JG
133/*
134INTERNAL_FUNCTION
135 bfd_xmalloc
136
137SYNOPSIS
138 PTR bfd_xmalloc( bfd_size_type size);
139
140DESCRIPTION
141 Like malloc, but exit if no more memory.
142
1e310759 143*/
f8e01940 144
1e310759
JG
145/** There is major inconsistency in how running out of memory is handled.
146 Some routines return a NULL, and set bfd_error to no_memory.
147 However, obstack routines can't do this ... */
148
149
150DEFUN(PTR bfd_xmalloc,(size),
151 bfd_size_type size)
152{
f8e01940 153 static CONST char no_memory_message[] = "Virtual memory exhausted!\n";
1e310759
JG
154 PTR ptr;
155 if (size == 0) size = 1;
156 ptr = (PTR)malloc(size);
1e310759
JG
157 if (!ptr)
158 {
159 write (2, no_memory_message, sizeof(no_memory_message)-1);
160 exit (-1);
161 }
162 return ptr;
163}
4a81b561
DHW
164\f
165/* Some IO code */
166
167
168/* Note that archive entries don't have streams; they share their parent's.
f58809fd 169 This allows someone to play with the iostream behind BFD's back.
4a81b561
DHW
170
171 Also, note that the origin pointer points to the beginning of a file's
172 contents (0 for non-archive elements). For archive entries this is the
173 first octet in the file, NOT the beginning of the archive header. */
174
7ed4093a
SC
175static
176int DEFUN(real_read,(where, a,b, file),
6f715d66
SC
177 PTR where AND
178 int a AND
179 int b AND
180 FILE *file)
7ed4093a
SC
181{
182 return fread(where, a,b,file);
183}
23b0b558 184bfd_size_type
7ed4093a
SC
185DEFUN(bfd_read,(ptr, size, nitems, abfd),
186 PTR ptr AND
187 bfd_size_type size AND
188 bfd_size_type nitems AND
189 bfd *abfd)
4a81b561 190{
7ed4093a 191 return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
4a81b561
DHW
192}
193
23b0b558 194bfd_size_type
7ed4093a 195DEFUN(bfd_write,(ptr, size, nitems, abfd),
f58809fd 196 CONST PTR ptr AND
7ed4093a
SC
197 bfd_size_type size AND
198 bfd_size_type nitems AND
199 bfd *abfd)
4a81b561 200{
7ed4093a 201 return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
4a81b561
DHW
202}
203
f8e01940
JG
204/*
205INTERNAL_FUNCTION
206 bfd_write_bigendian_4byte_int
207
208SYNOPSIS
209 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
210
211DESCRIPTION
212 Writes a 4 byte integer to the outputing bfd, in big endian
213 mode regardless of what else is going on. This is usefull in
214 archives.
1e310759 215
1e310759 216*/
f58809fd
SC
217void
218DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
219 bfd *abfd AND
220 int i)
221{
1e310759 222 bfd_byte buffer[4];
f58809fd 223 _do_putb32(i, buffer);
1e310759 224 bfd_write((PTR)buffer, 4, 1, abfd);
f58809fd
SC
225}
226
4a81b561 227int
7ed4093a
SC
228DEFUN(bfd_seek,(abfd, position, direction),
229 bfd * CONST abfd AND
230 CONST file_ptr position AND
231 CONST int direction)
4a81b561 232{
f58809fd 233 /* For the time being, a BFD may not seek to it's end. The
6f715d66
SC
234 problem is that we don't easily have a way to recognize
235 the end of an element in an archive. */
236
237 BFD_ASSERT(direction == SEEK_SET
238 || direction == SEEK_CUR);
239
240 if (direction == SEEK_SET && abfd->my_archive != NULL)
241 {
242 /* This is a set within an archive, so we need to
243 add the base of the object within the archive */
244 return(fseek(bfd_cache_lookup(abfd),
245 position + abfd->origin,
246 direction));
247 }
248 else
249 {
250 return(fseek(bfd_cache_lookup(abfd), position, direction));
251 }
4a81b561
DHW
252}
253
254long
536b27a5
SC
255DEFUN(bfd_tell,(abfd),
256 bfd *abfd)
4a81b561 257{
6f715d66 258 file_ptr ptr;
4a81b561 259
6f715d66 260 ptr = ftell (bfd_cache_lookup(abfd));
4a81b561 261
6f715d66
SC
262 if (abfd->my_archive)
263 ptr -= abfd->origin;
264 return ptr;
4a81b561
DHW
265}
266\f
267/** Make a string table */
268
6f715d66
SC
269/*>bfd.h<
270 Add string to table pointed to by table, at location starting with free_ptr.
4a81b561
DHW
271 resizes the table if necessary (if it's NULL, creates it, ignoring
272 table_length). Updates free_ptr, table, table_length */
273
274boolean
536b27a5
SC
275DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
276 char **table AND
536b27a5 277 char *new_string AND
5ad1d830
SC
278 unsigned int *table_length AND
279 char **free_ptr)
4a81b561
DHW
280{
281 size_t string_length = strlen (new_string) + 1; /* include null here */
282 char *base = *table;
283 size_t space_length = *table_length;
284 unsigned int offset = (base ? *free_ptr - base : 0);
285
286 if (base == NULL) {
287 /* Avoid a useless regrow if we can (but of course we still
288 take it next time */
289 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
6f715d66 290 DEFAULT_STRING_SPACE_SIZE : string_length+1);
4a81b561
DHW
291 base = zalloc (space_length);
292
293 if (base == NULL) {
294 bfd_error = no_memory;
295 return false;
296 }
297 }
298
299 if ((size_t)(offset + string_length) >= space_length) {
300 /* Make sure we will have enough space */
301 while ((size_t)(offset + string_length) >= space_length)
302 space_length += space_length/2; /* grow by 50% */
303
304 base = (char *) realloc (base, space_length);
305 if (base == NULL) {
306 bfd_error = no_memory;
307 return false;
308 }
309
310 }
311
312 memcpy (base + offset, new_string, string_length);
313 *table = base;
314 *table_length = space_length;
315 *free_ptr = base + offset + string_length;
316
317 return true;
318}
319\f
320/** The do-it-yourself (byte) sex-change kit */
321
322/* The middle letter e.g. get<b>short indicates Big or Little endian
323 target machine. It doesn't matter what the byte order of the host
324 machine is; these routines work for either. */
325
326/* FIXME: Should these take a count argument?
327 Answer (gnu@cygnus.com): No, but perhaps they should be inline
6f715d66
SC
328 functions in swap.h #ifdef __GNUC__.
329 Gprof them later and find out. */
330
f8e01940
JG
331/*
332FUNCTION
333 bfd_put_size
334FUNCTION
335 bfd_get_size
336
337DESCRIPTION
338 These macros as used for reading and writing raw data in
339 sections; each access (except for bytes) is vectored through
340 the target format of the BFD and mangled accordingly. The
341 mangling performs any necessary endian translations and
342 removes alignment restrictions.
343
344.#define bfd_put_8(abfd, val, ptr) \
345. (*((char *)ptr) = (char)val)
346.#define bfd_get_8(abfd, ptr) \
347. (*((char *)ptr))
348.#define bfd_put_16(abfd, val, ptr) \
349. BFD_SEND(abfd, bfd_putx16, (val,ptr))
350.#define bfd_get_16(abfd, ptr) \
351. BFD_SEND(abfd, bfd_getx16, (ptr))
352.#define bfd_put_32(abfd, val, ptr) \
353. BFD_SEND(abfd, bfd_putx32, (val,ptr))
354.#define bfd_get_32(abfd, ptr) \
355. BFD_SEND(abfd, bfd_getx32, (ptr))
356.#define bfd_put_64(abfd, val, ptr) \
357. BFD_SEND(abfd, bfd_putx64, (val, ptr))
358.#define bfd_get_64(abfd, ptr) \
359. BFD_SEND(abfd, bfd_getx64, (ptr))
360
361*/
362
363/*
364FUNCTION
365 bfd_h_put_size
366FUNCTION
367 bfd_h_get_size
368
369DESCRIPTION
370 These macros have the same function as their <<bfd_get_x>>
371 bretherin, except that they are used for removing information
372 for the header records of object files. Believe it or not,
373 some object files keep their header records in big endian
374 order, and their data in little endan order.
375
376.#define bfd_h_put_8(abfd, val, ptr) \
377. (*((char *)ptr) = (char)val)
378.#define bfd_h_get_8(abfd, ptr) \
379. (*((char *)ptr))
380.#define bfd_h_put_16(abfd, val, ptr) \
381. BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
382.#define bfd_h_get_16(abfd, ptr) \
383. BFD_SEND(abfd, bfd_h_getx16,(ptr))
384.#define bfd_h_put_32(abfd, val, ptr) \
385. BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
386.#define bfd_h_get_32(abfd, ptr) \
387. BFD_SEND(abfd, bfd_h_getx32,(ptr))
388.#define bfd_h_put_64(abfd, val, ptr) \
389. BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
390.#define bfd_h_get_64(abfd, ptr) \
391. BFD_SEND(abfd, bfd_h_getx64,(ptr))
392
393*/
4a81b561 394
f58809fd 395bfd_vma
536b27a5
SC
396DEFUN(_do_getb16,(addr),
397 register bfd_byte *addr)
4a81b561 398{
6f715d66 399 return (addr[0] << 8) | addr[1];
4a81b561
DHW
400}
401
f58809fd 402bfd_vma
536b27a5
SC
403DEFUN(_do_getl16,(addr),
404 register bfd_byte *addr)
4a81b561 405{
6f715d66 406 return (addr[1] << 8) | addr[0];
4a81b561
DHW
407}
408
409void
536b27a5 410DEFUN(_do_putb16,(data, addr),
f58809fd 411 bfd_vma data AND
536b27a5 412 register bfd_byte *addr)
4a81b561 413{
6f715d66
SC
414 addr[0] = (bfd_byte)(data >> 8);
415 addr[1] = (bfd_byte )data;
4a81b561
DHW
416}
417
418void
536b27a5 419DEFUN(_do_putl16,(data, addr),
f58809fd 420 bfd_vma data AND
536b27a5 421 register bfd_byte *addr)
4a81b561 422{
6f715d66
SC
423 addr[0] = (bfd_byte )data;
424 addr[1] = (bfd_byte)(data >> 8);
4a81b561
DHW
425}
426
f58809fd 427bfd_vma
536b27a5
SC
428DEFUN(_do_getb32,(addr),
429 register bfd_byte *addr)
4a81b561 430{
6f715d66 431 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
4a81b561
DHW
432}
433
f58809fd 434bfd_vma
7ed4093a 435_do_getl32 (addr)
6f715d66 436 register bfd_byte *addr;
4a81b561 437{
6f715d66 438 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
4a81b561
DHW
439}
440
f58809fd 441bfd_vma
536b27a5
SC
442DEFUN(_do_getb64,(addr),
443 register bfd_byte *addr)
7ed4093a 444{
536b27a5 445#ifdef HOST_64_BIT
7ed4093a 446 bfd_64_type low, high;
536b27a5 447
7ed4093a 448 high= ((((((((addr[0]) << 8) |
6f715d66
SC
449 addr[1]) << 8) |
450 addr[2]) << 8) |
451 addr[3]) );
7ed4093a
SC
452
453 low = ((((((((addr[4]) << 8) |
6f715d66
SC
454 addr[5]) << 8) |
455 addr[6]) << 8) |
456 addr[7]));
7ed4093a
SC
457
458 return high << 32 | low;
459#else
460 BFD_FAIL();
f58809fd 461 return 0;
7ed4093a
SC
462#endif
463
464}
465
f58809fd 466bfd_vma
5ad1d830 467DEFUN(_do_getl64,(addr),
536b27a5 468 register bfd_byte *addr)
7ed4093a 469{
6f715d66 470
536b27a5 471#ifdef HOST_64_BIT
6f715d66 472 bfd_64_type low, high;
7ed4093a 473 high= (((((((addr[7] << 8) |
6f715d66
SC
474 addr[6]) << 8) |
475 addr[5]) << 8) |
476 addr[4]));
7ed4093a
SC
477
478 low = (((((((addr[3] << 8) |
6f715d66
SC
479 addr[2]) << 8) |
480 addr[1]) << 8) |
481 addr[0]) );
7ed4093a
SC
482
483 return high << 32 | low;
484#else
485 BFD_FAIL();
f58809fd 486 return 0;
7ed4093a 487#endif
6f715d66 488
7ed4093a
SC
489}
490
4a81b561 491void
536b27a5 492DEFUN(_do_putb32,(data, addr),
f58809fd 493 bfd_vma data AND
536b27a5 494 register bfd_byte *addr)
4a81b561 495{
6f715d66
SC
496 addr[0] = (bfd_byte)(data >> 24);
497 addr[1] = (bfd_byte)(data >> 16);
498 addr[2] = (bfd_byte)(data >> 8);
499 addr[3] = (bfd_byte)data;
4a81b561
DHW
500}
501
502void
536b27a5 503DEFUN(_do_putl32,(data, addr),
f58809fd 504 bfd_vma data AND
536b27a5 505 register bfd_byte *addr)
4a81b561 506{
6f715d66
SC
507 addr[0] = (bfd_byte)data;
508 addr[1] = (bfd_byte)(data >> 8);
509 addr[2] = (bfd_byte)(data >> 16);
510 addr[3] = (bfd_byte)(data >> 24);
4a81b561 511}
7ed4093a 512void
536b27a5 513DEFUN(_do_putb64,(data, addr),
f58809fd 514 bfd_vma data AND
6f715d66 515 register bfd_byte *addr)
7ed4093a 516{
536b27a5
SC
517#ifdef HOST_64_BIT
518 addr[0] = (bfd_byte)(data >> (7*8));
519 addr[1] = (bfd_byte)(data >> (6*8));
520 addr[2] = (bfd_byte)(data >> (5*8));
521 addr[3] = (bfd_byte)(data >> (4*8));
522 addr[4] = (bfd_byte)(data >> (3*8));
523 addr[5] = (bfd_byte)(data >> (2*8));
524 addr[6] = (bfd_byte)(data >> (1*8));
525 addr[7] = (bfd_byte)(data >> (0*8));
7ed4093a 526#else
536b27a5 527 BFD_FAIL();
7ed4093a
SC
528#endif
529
530}
531
532void
536b27a5 533DEFUN(_do_putl64,(data, addr),
f58809fd 534 bfd_vma data AND
536b27a5 535 register bfd_byte *addr)
7ed4093a 536{
536b27a5
SC
537#ifdef HOST_64_BIT
538 addr[7] = (bfd_byte)(data >> (7*8));
539 addr[6] = (bfd_byte)(data >> (6*8));
540 addr[5] = (bfd_byte)(data >> (5*8));
541 addr[4] = (bfd_byte)(data >> (4*8));
542 addr[3] = (bfd_byte)(data >> (3*8));
543 addr[2] = (bfd_byte)(data >> (2*8));
544 addr[1] = (bfd_byte)(data >> (1*8));
545 addr[0] = (bfd_byte)(data >> (0*8));
7ed4093a 546#else
536b27a5 547 BFD_FAIL();
7ed4093a
SC
548#endif
549
550}
551
2203f786
JG
552\f
553/* Default implementation */
4a81b561 554
2203f786 555boolean
7ed4093a
SC
556DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
557 bfd *abfd AND
558 sec_ptr section AND
559 PTR location AND
560 file_ptr offset AND
561 bfd_size_type count)
2203f786
JG
562{
563 if (count == 0)
6f715d66 564 return true;
f8e01940
JG
565 if ((bfd_size_type)(offset+count) > section->_raw_size
566 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
6f715d66
SC
567 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
568 return (false); /* on error */
2203f786
JG
569 return (true);
570}
6f715d66 571
f58809fd
SC
572/* This generic function can only be used in implementations where creating
573 NEW sections is disallowed. It is useful in patching existing sections
574 in read-write files, though. See other set_section_contents functions
575 to see why it doesn't work for new sections. */
576boolean
577DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
578 bfd *abfd AND
579 sec_ptr section AND
580 PTR location AND
581 file_ptr offset AND
582 bfd_size_type count)
583{
584 if (count == 0)
585 return true;
f8e01940 586 if ((bfd_size_type)(offset+count) > bfd_get_section_size_after_reloc(section)
f58809fd
SC
587 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
588 || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
589 return (false); /* on error */
590 return (true);
591}
592
f8e01940
JG
593/*
594INTERNAL_FUNCTION
595 bfd_log2
596
597DESCRIPTION
598 Return the log base 2 of the value supplied, rounded up. eg an
599 arg of 1025 would return 11.
600
601SYNOPSIS
602 bfd_vma bfd_log2(bfd_vma x);
603*/
6f715d66
SC
604
605bfd_vma bfd_log2(x)
606bfd_vma x;
607{
608 bfd_vma result = 0;
609 while ( (bfd_vma)(1<< result) < x)
610 result++;
611 return result;
612}
This page took 0.093882 seconds and 4 git commands to generate.