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