Merge in stuff accidently commited to sh3e branch
[deliverable/binutils-gdb.git] / bfd / libbfd.c
CommitLineData
0d552306 1/* Assorted BFD support routines, only used internally.
f4bd7a8f 2 Copyright 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
f58809fd 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 18along with this program; if not, write to the Free Software
4fe6d901 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
4a81b561 20
4a81b561 21#include "bfd.h"
f58809fd 22#include "sysdep.h"
4a81b561
DHW
23#include "libbfd.h"
24
4fe6d901
KR
25static int real_read PARAMS ((PTR, size_t, size_t, FILE *));
26
f8e01940
JG
27/*
28SECTION
17ecba1a 29 Internal functions
f8e01940
JG
30
31DESCRIPTION
c188b0be 32 These routines are used within BFD.
f8e01940
JG
33 They are not intended for export, but are documented here for
34 completeness.
35*/
4a81b561 36
6812b607
ILT
37/* A routine which is used in target vectors for unsupported
38 operations. */
4a81b561 39
728472f1 40/*ARGSUSED*/
4a81b561 41boolean
f4bd7a8f
DM
42bfd_false (ignore)
43 bfd *ignore;
4a81b561 44{
6812b607 45 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
46 return false;
47}
48
6812b607
ILT
49/* A routine which is used in target vectors for supported operations
50 which do not actually do anything. */
51
728472f1 52/*ARGSUSED*/
4a81b561 53boolean
f4bd7a8f
DM
54bfd_true (ignore)
55 bfd *ignore;
4a81b561
DHW
56{
57 return true;
58}
59
6812b607
ILT
60/* A routine which is used in target vectors for unsupported
61 operations which return a pointer value. */
62
728472f1 63/*ARGSUSED*/
d0ec7a8e 64PTR
f4bd7a8f
DM
65bfd_nullvoidptr (ignore)
66 bfd *ignore;
4a81b561 67{
6812b607
ILT
68 bfd_set_error (bfd_error_invalid_operation);
69 return NULL;
4a81b561 70}
fc723380 71
728472f1 72/*ARGSUSED*/
4a81b561 73int
f4bd7a8f
DM
74bfd_0 (ignore)
75 bfd *ignore;
4a81b561
DHW
76{
77 return 0;
78}
fc723380 79
728472f1 80/*ARGSUSED*/
4a81b561 81unsigned int
f4bd7a8f
DM
82bfd_0u (ignore)
83 bfd *ignore;
4a81b561
DHW
84{
85 return 0;
86}
87
326e32d7
ILT
88/*ARGUSED*/
89long
90bfd_0l (ignore)
91 bfd *ignore;
92{
93 return 0;
94}
95
6812b607
ILT
96/* A routine which is used in target vectors for unsupported
97 operations which return -1 on error. */
98
99/*ARGSUSED*/
100long
101_bfd_n1 (ignore_abfd)
102 bfd *ignore_abfd;
103{
104 bfd_set_error (bfd_error_invalid_operation);
105 return -1;
106}
107
728472f1 108/*ARGSUSED*/
4a81b561 109void
f4bd7a8f
DM
110bfd_void (ignore)
111 bfd *ignore;
4a81b561
DHW
112{
113}
114
728472f1 115/*ARGSUSED*/
4a81b561 116boolean
6812b607 117_bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
f4bd7a8f
DM
118 bfd *ignore_core_bfd;
119 bfd *ignore_exec_bfd;
4a81b561 120{
120f5bd9 121 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
122 return false;
123}
124
6812b607
ILT
125/* Routine to handle core_file_failing_command entry point for targets
126 without core file support. */
4a81b561 127
728472f1 128/*ARGSUSED*/
4a81b561 129char *
6812b607 130_bfd_nocore_core_file_failing_command (ignore_abfd)
f4bd7a8f 131 bfd *ignore_abfd;
4a81b561 132{
6812b607 133 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
134 return (char *)NULL;
135}
136
6812b607
ILT
137/* Routine to handle core_file_failing_signal entry point for targets
138 without core file support. */
139
728472f1 140/*ARGSUSED*/
4a81b561 141int
6812b607 142_bfd_nocore_core_file_failing_signal (ignore_abfd)
f4bd7a8f 143 bfd *ignore_abfd;
4a81b561 144{
6812b607 145 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
146 return 0;
147}
148
728472f1 149/*ARGSUSED*/
4fe6d901 150const bfd_target *
f4bd7a8f
DM
151_bfd_dummy_target (ignore_abfd)
152 bfd *ignore_abfd;
4a81b561 153{
4fe6d901 154 bfd_set_error (bfd_error_wrong_format);
4a81b561
DHW
155 return 0;
156}
157\f
4a81b561 158
f4bd7a8f
DM
159#ifndef bfd_zmalloc
160/* allocate and clear storage */
4a81b561 161
4a81b561 162char *
f4bd7a8f
DM
163bfd_zmalloc (size)
164 bfd_size_type size;
4a81b561 165{
120f5bd9 166 char *ptr = (char *) malloc ((size_t) size);
4a81b561 167
120f5bd9
JL
168 if (ptr && size)
169 memset(ptr, 0, (size_t) size);
4a81b561
DHW
170
171 return ptr;
172}
f4bd7a8f 173#endif /* bfd_zmalloc */
4a81b561
DHW
174\f
175/* Some IO code */
176
177
178/* Note that archive entries don't have streams; they share their parent's.
f58809fd 179 This allows someone to play with the iostream behind BFD's back.
4a81b561
DHW
180
181 Also, note that the origin pointer points to the beginning of a file's
182 contents (0 for non-archive elements). For archive entries this is the
183 first octet in the file, NOT the beginning of the archive header. */
184
4fe6d901 185static int
f4bd7a8f
DM
186real_read (where, a,b, file)
187 PTR where;
4fe6d901
KR
188 size_t a;
189 size_t b;
f4bd7a8f 190 FILE *file;
7ed4093a 191{
4fe6d901 192 return fread (where, a, b, file);
7ed4093a 193}
f4bd7a8f 194
120f5bd9
JL
195/* Return value is amount read (FIXME: how are errors and end of file dealt
196 with? We never call bfd_set_error, which is probably a mistake). */
197
23b0b558 198bfd_size_type
f4bd7a8f
DM
199bfd_read (ptr, size, nitems, abfd)
200 PTR ptr;
201 bfd_size_type size;
202 bfd_size_type nitems;
203 bfd *abfd;
4a81b561 204{
0d552306 205 int nread;
4fe6d901 206 nread = real_read (ptr, 1, (size_t)(size*nitems), bfd_cache_lookup(abfd));
0d552306
KR
207#ifdef FILE_OFFSET_IS_CHAR_INDEX
208 if (nread > 0)
209 abfd->where += nread;
210#endif
120f5bd9
JL
211
212 /* Set bfd_error if we did not read as much data as we expected.
213
214 If the read failed due to an error set the bfd_error_system_call,
215 else set bfd_error_file_truncated.
216
217 A BFD backend may wish to override bfd_error_file_truncated to
218 provide something more useful (eg. no_symbols or wrong_format). */
219 if (nread < (int)(size * nitems))
220 {
221 if (ferror (bfd_cache_lookup (abfd)))
222 bfd_set_error (bfd_error_system_call);
223 else
224 bfd_set_error (bfd_error_file_truncated);
225 }
226
0d552306 227 return nread;
4a81b561
DHW
228}
229
4fe6d901
KR
230/* The window support stuff should probably be broken out into
231 another file.... */
232/* The idea behind the next and refcount fields is that one mapped
233 region can suffice for multiple read-only windows or multiple
234 non-overlapping read-write windows. It's not implemented yet
235 though. */
236struct _bfd_window_internal {
237 struct _bfd_window_internal *next;
238 PTR data;
239 bfd_size_type size;
240 int refcount : 31; /* should be enough... */
241 unsigned mapped : 1; /* 1 = mmap, 0 = malloc */
242};
243
244void
245bfd_init_window (windowp)
246 bfd_window *windowp;
247{
248 windowp->data = 0;
249 windowp->i = 0;
250 windowp->size = 0;
251}
252
253#undef HAVE_MPROTECT /* code's not tested yet */
254
255#if HAVE_MMAP || HAVE_MPROTECT
256#include <sys/types.h>
257#include <sys/mman.h>
258#endif
259
260#ifndef MAP_FILE
261#define MAP_FILE 0
262#endif
263
264static int debug_windows;
265
266void
267bfd_free_window (windowp)
268 bfd_window *windowp;
269{
270 bfd_window_internal *i = windowp->i;
271 windowp->i = 0;
272 windowp->data = 0;
273 if (i == 0)
274 return;
275 i->refcount--;
276 if (debug_windows)
277 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
278 windowp, windowp->data, windowp->size, windowp->i);
279 if (i->refcount != 0)
280 return;
281
282 if (i->mapped)
283 {
284#ifdef HAVE_MMAP
285 munmap (i->data, i->size);
286 goto no_free;
287#else
288 abort ();
289#endif
290 }
291#ifdef HAVE_MPROTECT
292 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
293#endif
294 free (i->data);
295#ifdef HAVE_MMAP
296 no_free:
297#endif
298 i->data = 0;
299 /* There should be no more references to i at this point. */
300 free (i);
301}
302
303static int ok_to_map = 1;
304
092abcdf 305boolean
4fe6d901
KR
306bfd_get_file_window (abfd, offset, size, windowp, writable)
307 bfd *abfd;
308 file_ptr offset;
309 bfd_size_type size;
310 bfd_window *windowp;
0bb8ff19 311 boolean writable;
4fe6d901
KR
312{
313 static size_t pagesize;
314 bfd_window_internal *i = windowp->i;
315 size_t size_to_alloc = size;
316
317 if (debug_windows)
318 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
319 abfd, (long) offset, (long) size,
320 windowp, windowp->data, windowp->size, windowp->i,
321 writable);
322
323 /* Make sure we know the page size, so we can be friendly to mmap. */
324 if (pagesize == 0)
325 pagesize = getpagesize ();
326 if (pagesize == 0)
327 abort ();
328
329 if (i == 0)
330 {
331 windowp->i = i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
332 if (i == 0)
333 return false;
334 i->data = 0;
335 }
336#ifdef HAVE_MMAP
337 if (ok_to_map && (i->data == 0 || i->mapped == 1))
338 {
339 file_ptr file_offset, offset2;
340 size_t real_size;
341 int fd;
342 FILE *f;
343
344 /* Find the real file and the real offset into it. */
345 while (abfd->my_archive != NULL)
346 {
347 offset += abfd->origin;
348 abfd = abfd->my_archive;
349 }
350 f = bfd_cache_lookup (abfd);
351 fd = fileno (f);
352
353 /* Compute offsets and size for mmap and for the user's data. */
354 offset2 = offset % pagesize;
355 if (offset2 < 0)
356 abort ();
357 file_offset = offset - offset2;
358 real_size = offset + size - file_offset;
359 real_size = real_size + pagesize - 1;
360 real_size -= real_size % pagesize;
361
362 /* If we're re-using a memory region, make sure it's big enough. */
363 if (i->data && i->size < size)
364 {
365 munmap (i->data, i->size);
366 i->data = 0;
367 }
368 i->data = mmap (i->data, real_size,
369 writable ? PROT_WRITE | PROT_READ : PROT_READ,
0bb8ff19
ILT
370 (writable
371 ? MAP_FILE | MAP_PRIVATE
372 : MAP_FILE | MAP_SHARED),
4fe6d901
KR
373 fd, file_offset);
374 if (i->data == (PTR) -1)
375 {
376 /* An error happened. Report it, or try using malloc, or
377 something. */
378 bfd_set_error (bfd_error_system_call);
379 i->data = 0;
380 windowp->data = 0;
381 if (debug_windows)
382 fprintf (stderr, "\t\tmmap failed!\n");
383 return false;
384 }
385 if (debug_windows)
386 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
387 (long) real_size, i->data, (long) offset2);
388 i->size = real_size;
389 windowp->data = i->data + offset2;
390 windowp->size = size;
391 i->mapped = 1;
392 return true;
393 }
394 else if (debug_windows)
395 {
396 if (ok_to_map)
3b168da4
ILT
397 fprintf (stderr, "not mapping: data=%lx mapped=%d\n",
398 (unsigned long) i->data, (int) i->mapped);
4fe6d901
KR
399 else
400 fprintf (stderr, "not mapping: env var not set\n");
401 }
402#else
403 ok_to_map = 0;
404#endif
405
406#ifdef HAVE_MPROTECT
407 if (!writable)
408 {
409 size_to_alloc += pagesize - 1;
410 size_to_alloc -= size_to_alloc % pagesize;
411 }
412#endif
413 if (debug_windows)
414 fprintf (stderr, "\n\t%s(%6ld)",
415 i->data ? "realloc" : " malloc", (long) size_to_alloc);
416 if (i->data)
092abcdf 417 i->data = (PTR) realloc (i->data, size_to_alloc);
4fe6d901 418 else
092abcdf 419 i->data = (PTR) malloc (size_to_alloc);
4fe6d901
KR
420 if (debug_windows)
421 fprintf (stderr, "\t-> %p\n", i->data);
422 i->refcount = 1;
092abcdf
ILT
423 if (i->data == NULL)
424 {
425 if (size_to_alloc == 0)
426 return true;
427 bfd_set_error (bfd_error_no_memory);
428 return false;
429 }
4fe6d901
KR
430 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
431 return false;
432 i->size = bfd_read (i->data, size, 1, abfd);
433 if (i->size != size)
434 return false;
435 i->mapped = 0;
436#ifdef HAVE_MPROTECT
437 if (!writable)
438 {
439 if (debug_windows)
440 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
441 (long) i->size);
442 mprotect (i->data, i->size, PROT_READ);
443 }
444#endif
445 windowp->data = i->data;
446 windowp->size = i->size;
447 return true;
448}
449
23b0b558 450bfd_size_type
df34342b
ILT
451bfd_write (ptr, size, nitems, abfd)
452 CONST PTR ptr;
453 bfd_size_type size;
454 bfd_size_type nitems;
455 bfd *abfd;
4a81b561 456{
4fe6d901
KR
457 long nwrote = fwrite (ptr, 1, (size_t) (size * nitems),
458 bfd_cache_lookup (abfd));
0d552306
KR
459#ifdef FILE_OFFSET_IS_CHAR_INDEX
460 if (nwrote > 0)
461 abfd->where += nwrote;
462#endif
4fe6d901 463 if ((bfd_size_type) nwrote != size * nitems)
df34342b
ILT
464 {
465#ifdef ENOSPC
466 if (nwrote >= 0)
467 errno = ENOSPC;
468#endif
120f5bd9 469 bfd_set_error (bfd_error_system_call);
df34342b 470 }
0d552306 471 return nwrote;
4a81b561
DHW
472}
473
f8e01940
JG
474/*
475INTERNAL_FUNCTION
476 bfd_write_bigendian_4byte_int
477
478SYNOPSIS
479 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
480
481DESCRIPTION
c188b0be
DM
482 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
483 endian order regardless of what else is going on. This is useful in
f8e01940 484 archives.
1e310759 485
1e310759 486*/
f58809fd 487void
f4bd7a8f
DM
488bfd_write_bigendian_4byte_int (abfd, i)
489 bfd *abfd;
490 int i;
f58809fd 491{
1e310759 492 bfd_byte buffer[4];
b5b4294e 493 bfd_putb32(i, buffer);
4002f18a
ILT
494 if (bfd_write((PTR)buffer, 4, 1, abfd) != 4)
495 abort ();
f58809fd
SC
496}
497
0d552306 498long
f4bd7a8f
DM
499bfd_tell (abfd)
500 bfd *abfd;
0d552306
KR
501{
502 file_ptr ptr;
503
504 ptr = ftell (bfd_cache_lookup(abfd));
505
506 if (abfd->my_archive)
507 ptr -= abfd->origin;
508 abfd->where = ptr;
509 return ptr;
510}
511
b5b4294e 512int
f4bd7a8f
DM
513bfd_flush (abfd)
514 bfd *abfd;
b5b4294e
JG
515{
516 return fflush (bfd_cache_lookup(abfd));
517}
518
4fe6d901
KR
519/* Returns 0 for success, negative value for failure (in which case
520 bfd_get_error can retrieve the error code). */
b5b4294e 521int
f4bd7a8f
DM
522bfd_stat (abfd, statbuf)
523 bfd *abfd;
524 struct stat *statbuf;
b5b4294e 525{
4fe6d901
KR
526 FILE *f;
527 int result;
528 f = bfd_cache_lookup (abfd);
529 if (f == NULL)
530 {
531 bfd_set_error (bfd_error_system_call);
532 return -1;
533 }
534 result = fstat (fileno (f), statbuf);
535 if (result < 0)
536 bfd_set_error (bfd_error_system_call);
537 return result;
b5b4294e
JG
538}
539
120f5bd9
JL
540/* Returns 0 for success, nonzero for failure (in which case bfd_get_error
541 can retrieve the error code). */
542
4a81b561 543int
f4bd7a8f 544bfd_seek (abfd, position, direction)
4fe6d901
KR
545 bfd *abfd;
546 file_ptr position;
547 int direction;
4a81b561 548{
0d552306
KR
549 int result;
550 FILE *f;
e5b02860 551 file_ptr file_position;
0d552306
KR
552 /* For the time being, a BFD may not seek to it's end. The problem
553 is that we don't easily have a way to recognize the end of an
554 element in an archive. */
555
556 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
557
b31d06ca 558 if (direction == SEEK_CUR && position == 0)
0d552306
KR
559 return 0;
560#ifdef FILE_OFFSET_IS_CHAR_INDEX
b6090f4d
JG
561 if (abfd->format != bfd_archive && abfd->my_archive == 0)
562 {
4c3721d5 563#if 0
b6090f4d
JG
564 /* Explanation for this code: I'm only about 95+% sure that the above
565 conditions are sufficient and that all i/o calls are properly
566 adjusting the `where' field. So this is sort of an `assert'
567 that the `where' field is correct. If we can go a while without
568 tripping the abort, we can probably safely disable this code,
569 so that the real optimizations happen. */
570 file_ptr where_am_i_now;
571 where_am_i_now = ftell (bfd_cache_lookup (abfd));
572 if (abfd->my_archive)
573 where_am_i_now -= abfd->origin;
574 if (where_am_i_now != abfd->where)
575 abort ();
576#endif
577 if (direction == SEEK_SET && position == abfd->where)
578 return 0;
579 }
580 else
581 {
582 /* We need something smarter to optimize access to archives.
583 Currently, anything inside an archive is read via the file
584 handle for the archive. Which means that a bfd_seek on one
585 component affects the `current position' in the archive, as
586 well as in any other component.
587
588 It might be sufficient to put a spike through the cache
589 abstraction, and look to the archive for the file position,
590 but I think we should try for something cleaner.
591
592 In the meantime, no optimization for archives. */
593 }
0d552306 594#endif
4a81b561 595
0d552306 596 f = bfd_cache_lookup (abfd);
e5b02860 597 file_position = position;
0d552306 598 if (direction == SEEK_SET && abfd->my_archive != NULL)
e5b02860
KR
599 file_position += abfd->origin;
600
601 result = fseek (f, file_position, direction);
602
603 if (result != 0)
df34342b
ILT
604 {
605 /* Force redetermination of `where' field. */
606 bfd_tell (abfd);
120f5bd9 607 bfd_set_error (bfd_error_system_call);
df34342b 608 }
0d552306
KR
609 else
610 {
e5b02860
KR
611#ifdef FILE_OFFSET_IS_CHAR_INDEX
612 /* Adjust `where' field. */
613 if (direction == SEEK_SET)
614 abfd->where = position;
615 else
616 abfd->where += position;
617#endif
0d552306 618 }
e5b02860 619 return result;
4a81b561
DHW
620}
621\f
4a81b561
DHW
622/** The do-it-yourself (byte) sex-change kit */
623
624/* The middle letter e.g. get<b>short indicates Big or Little endian
625 target machine. It doesn't matter what the byte order of the host
626 machine is; these routines work for either. */
627
628/* FIXME: Should these take a count argument?
629 Answer (gnu@cygnus.com): No, but perhaps they should be inline
6f715d66
SC
630 functions in swap.h #ifdef __GNUC__.
631 Gprof them later and find out. */
632
f8e01940
JG
633/*
634FUNCTION
635 bfd_put_size
636FUNCTION
637 bfd_get_size
638
639DESCRIPTION
640 These macros as used for reading and writing raw data in
641 sections; each access (except for bytes) is vectored through
642 the target format of the BFD and mangled accordingly. The
643 mangling performs any necessary endian translations and
14e3c2e4
JK
644 removes alignment restrictions. Note that types accepted and
645 returned by these macros are identical so they can be swapped
17ecba1a
DM
646 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
647 to either <<bfd_get_32>> or <<bfd_get_64>>.
f8e01940 648
17ecba1a 649 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
7e4db254
JK
650 system without prototypes, the caller is responsible for making
651 sure that is true, with a cast if necessary. We don't cast
17ecba1a
DM
652 them in the macro definitions because that would prevent <<lint>>
653 or <<gcc -Wall>> from detecting sins such as passing a pointer.
654 To detect calling these with less than a <<bfd_vma>>, use
655 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
7e4db254 656
da3cd00a
KR
657.
658.{* Byte swapping macros for user section data. *}
659.
f8e01940 660.#define bfd_put_8(abfd, val, ptr) \
4c3721d5 661. (*((unsigned char *)(ptr)) = (unsigned char)(val))
da3cd00a
KR
662.#define bfd_put_signed_8 \
663. bfd_put_8
f8e01940 664.#define bfd_get_8(abfd, ptr) \
da3cd00a
KR
665. (*(unsigned char *)(ptr))
666.#define bfd_get_signed_8(abfd, ptr) \
667. ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
668.
f8e01940 669.#define bfd_put_16(abfd, val, ptr) \
7e4db254 670. BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
da3cd00a
KR
671.#define bfd_put_signed_16 \
672. bfd_put_16
f8e01940
JG
673.#define bfd_get_16(abfd, ptr) \
674. BFD_SEND(abfd, bfd_getx16, (ptr))
14e3c2e4
JK
675.#define bfd_get_signed_16(abfd, ptr) \
676. BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
da3cd00a 677.
f8e01940 678.#define bfd_put_32(abfd, val, ptr) \
7e4db254 679. BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
da3cd00a
KR
680.#define bfd_put_signed_32 \
681. bfd_put_32
f8e01940
JG
682.#define bfd_get_32(abfd, ptr) \
683. BFD_SEND(abfd, bfd_getx32, (ptr))
14e3c2e4
JK
684.#define bfd_get_signed_32(abfd, ptr) \
685. BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
da3cd00a 686.
f8e01940 687.#define bfd_put_64(abfd, val, ptr) \
7e4db254 688. BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
da3cd00a
KR
689.#define bfd_put_signed_64 \
690. bfd_put_64
f8e01940
JG
691.#define bfd_get_64(abfd, ptr) \
692. BFD_SEND(abfd, bfd_getx64, (ptr))
14e3c2e4
JK
693.#define bfd_get_signed_64(abfd, ptr) \
694. BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
da3cd00a 695.
f8e01940
JG
696*/
697
698/*
699FUNCTION
700 bfd_h_put_size
f8e01940
JG
701 bfd_h_get_size
702
703DESCRIPTION
704 These macros have the same function as their <<bfd_get_x>>
c188b0be 705 bretheren, except that they are used for removing information
f8e01940
JG
706 for the header records of object files. Believe it or not,
707 some object files keep their header records in big endian
c188b0be 708 order and their data in little endian order.
da3cd00a
KR
709.
710.{* Byte swapping macros for file header data. *}
711.
f8e01940 712.#define bfd_h_put_8(abfd, val, ptr) \
da3cd00a
KR
713. bfd_put_8 (abfd, val, ptr)
714.#define bfd_h_put_signed_8(abfd, val, ptr) \
715. bfd_put_8 (abfd, val, ptr)
f8e01940 716.#define bfd_h_get_8(abfd, ptr) \
da3cd00a
KR
717. bfd_get_8 (abfd, ptr)
718.#define bfd_h_get_signed_8(abfd, ptr) \
719. bfd_get_signed_8 (abfd, ptr)
720.
f8e01940
JG
721.#define bfd_h_put_16(abfd, val, ptr) \
722. BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
da3cd00a
KR
723.#define bfd_h_put_signed_16 \
724. bfd_h_put_16
f8e01940
JG
725.#define bfd_h_get_16(abfd, ptr) \
726. BFD_SEND(abfd, bfd_h_getx16,(ptr))
14e3c2e4
JK
727.#define bfd_h_get_signed_16(abfd, ptr) \
728. BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
da3cd00a 729.
f8e01940
JG
730.#define bfd_h_put_32(abfd, val, ptr) \
731. BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
da3cd00a
KR
732.#define bfd_h_put_signed_32 \
733. bfd_h_put_32
f8e01940
JG
734.#define bfd_h_get_32(abfd, ptr) \
735. BFD_SEND(abfd, bfd_h_getx32,(ptr))
14e3c2e4
JK
736.#define bfd_h_get_signed_32(abfd, ptr) \
737. BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
da3cd00a 738.
f8e01940
JG
739.#define bfd_h_put_64(abfd, val, ptr) \
740. BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
da3cd00a
KR
741.#define bfd_h_put_signed_64 \
742. bfd_h_put_64
f8e01940
JG
743.#define bfd_h_get_64(abfd, ptr) \
744. BFD_SEND(abfd, bfd_h_getx64,(ptr))
14e3c2e4
JK
745.#define bfd_h_get_signed_64(abfd, ptr) \
746. BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
da3cd00a 747.
f8e01940 748*/
4a81b561 749
14e3c2e4 750/* Sign extension to bfd_signed_vma. */
df34342b
ILT
751#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
752#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
96ad107b 753#define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
df34342b
ILT
754#define COERCE64(x) \
755 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
14e3c2e4 756
f58809fd 757bfd_vma
f4bd7a8f
DM
758bfd_getb16 (addr)
759 register const bfd_byte *addr;
4a81b561 760{
f4bd7a8f 761 return (addr[0] << 8) | addr[1];
4a81b561
DHW
762}
763
f58809fd 764bfd_vma
f4bd7a8f
DM
765bfd_getl16 (addr)
766 register const bfd_byte *addr;
4a81b561 767{
f4bd7a8f 768 return (addr[1] << 8) | addr[0];
4a81b561
DHW
769}
770
14e3c2e4 771bfd_signed_vma
f4bd7a8f
DM
772bfd_getb_signed_16 (addr)
773 register const bfd_byte *addr;
14e3c2e4 774{
f4bd7a8f 775 return COERCE16((addr[0] << 8) | addr[1]);
14e3c2e4
JK
776}
777
778bfd_signed_vma
f4bd7a8f
DM
779bfd_getl_signed_16 (addr)
780 register const bfd_byte *addr;
14e3c2e4 781{
f4bd7a8f 782 return COERCE16((addr[1] << 8) | addr[0]);
14e3c2e4
JK
783}
784
4a81b561 785void
f4bd7a8f
DM
786bfd_putb16 (data, addr)
787 bfd_vma data;
788 register bfd_byte *addr;
4a81b561 789{
f4bd7a8f
DM
790 addr[0] = (bfd_byte)(data >> 8);
791 addr[1] = (bfd_byte )data;
4a81b561
DHW
792}
793
794void
f4bd7a8f
DM
795bfd_putl16 (data, addr)
796 bfd_vma data;
797 register bfd_byte *addr;
4a81b561 798{
f4bd7a8f
DM
799 addr[0] = (bfd_byte )data;
800 addr[1] = (bfd_byte)(data >> 8);
4a81b561
DHW
801}
802
f58809fd 803bfd_vma
b5b4294e 804bfd_getb32 (addr)
f4bd7a8f 805 register const bfd_byte *addr;
4a81b561 806{
f4bd7a8f
DM
807 return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
808 | addr[2]) << 8 | addr[3];
4a81b561
DHW
809}
810
f58809fd 811bfd_vma
b5b4294e 812bfd_getl32 (addr)
f4bd7a8f 813 register const bfd_byte *addr;
4a81b561 814{
f4bd7a8f
DM
815 return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
816 | addr[1]) << 8 | addr[0];
4a81b561
DHW
817}
818
14e3c2e4 819bfd_signed_vma
b5b4294e 820bfd_getb_signed_32 (addr)
f4bd7a8f 821 register const bfd_byte *addr;
14e3c2e4 822{
f4bd7a8f
DM
823 return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
824 | addr[2]) << 8 | addr[3]);
14e3c2e4
JK
825}
826
827bfd_signed_vma
b5b4294e 828bfd_getl_signed_32 (addr)
f4bd7a8f 829 register const bfd_byte *addr;
14e3c2e4 830{
f4bd7a8f
DM
831 return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
832 | addr[1]) << 8 | addr[0]);
14e3c2e4
JK
833}
834
f58809fd 835bfd_vma
f4bd7a8f
DM
836bfd_getb64 (addr)
837 register const bfd_byte *addr;
7ed4093a 838{
b5b4294e 839#ifdef BFD64
b6090f4d 840 bfd_vma low, high;
536b27a5 841
7ed4093a 842 high= ((((((((addr[0]) << 8) |
6f715d66
SC
843 addr[1]) << 8) |
844 addr[2]) << 8) |
845 addr[3]) );
7ed4093a 846
df34342b 847 low = (((((((((bfd_vma)addr[4]) << 8) |
6f715d66
SC
848 addr[5]) << 8) |
849 addr[6]) << 8) |
850 addr[7]));
7ed4093a
SC
851
852 return high << 32 | low;
853#else
854 BFD_FAIL();
f58809fd 855 return 0;
7ed4093a 856#endif
7ed4093a
SC
857}
858
f58809fd 859bfd_vma
f4bd7a8f
DM
860bfd_getl64 (addr)
861 register const bfd_byte *addr;
7ed4093a 862{
b5b4294e 863#ifdef BFD64
b6090f4d 864 bfd_vma low, high;
7ed4093a 865 high= (((((((addr[7] << 8) |
6f715d66
SC
866 addr[6]) << 8) |
867 addr[5]) << 8) |
868 addr[4]));
7ed4093a 869
df34342b 870 low = ((((((((bfd_vma)addr[3] << 8) |
6f715d66
SC
871 addr[2]) << 8) |
872 addr[1]) << 8) |
873 addr[0]) );
7ed4093a
SC
874
875 return high << 32 | low;
876#else
877 BFD_FAIL();
f58809fd 878 return 0;
7ed4093a 879#endif
6f715d66 880
7ed4093a
SC
881}
882
14e3c2e4 883bfd_signed_vma
f4bd7a8f
DM
884bfd_getb_signed_64 (addr)
885 register const bfd_byte *addr;
14e3c2e4 886{
b5b4294e 887#ifdef BFD64
14e3c2e4
JK
888 bfd_vma low, high;
889
890 high= ((((((((addr[0]) << 8) |
891 addr[1]) << 8) |
892 addr[2]) << 8) |
893 addr[3]) );
894
df34342b 895 low = (((((((((bfd_vma)addr[4]) << 8) |
14e3c2e4
JK
896 addr[5]) << 8) |
897 addr[6]) << 8) |
898 addr[7]));
899
900 return COERCE64(high << 32 | low);
901#else
902 BFD_FAIL();
903 return 0;
904#endif
14e3c2e4
JK
905}
906
907bfd_signed_vma
f4bd7a8f
DM
908bfd_getl_signed_64 (addr)
909 register const bfd_byte *addr;
14e3c2e4 910{
b5b4294e 911#ifdef BFD64
14e3c2e4
JK
912 bfd_vma low, high;
913 high= (((((((addr[7] << 8) |
914 addr[6]) << 8) |
915 addr[5]) << 8) |
916 addr[4]));
917
df34342b 918 low = ((((((((bfd_vma)addr[3] << 8) |
14e3c2e4
JK
919 addr[2]) << 8) |
920 addr[1]) << 8) |
921 addr[0]) );
922
923 return COERCE64(high << 32 | low);
924#else
925 BFD_FAIL();
926 return 0;
927#endif
14e3c2e4
JK
928}
929
4a81b561 930void
f4bd7a8f
DM
931bfd_putb32 (data, addr)
932 bfd_vma data;
933 register bfd_byte *addr;
4a81b561 934{
6f715d66
SC
935 addr[0] = (bfd_byte)(data >> 24);
936 addr[1] = (bfd_byte)(data >> 16);
937 addr[2] = (bfd_byte)(data >> 8);
938 addr[3] = (bfd_byte)data;
4a81b561
DHW
939}
940
941void
f4bd7a8f
DM
942bfd_putl32 (data, addr)
943 bfd_vma data;
944 register bfd_byte *addr;
4a81b561 945{
6f715d66
SC
946 addr[0] = (bfd_byte)data;
947 addr[1] = (bfd_byte)(data >> 8);
948 addr[2] = (bfd_byte)(data >> 16);
949 addr[3] = (bfd_byte)(data >> 24);
4a81b561 950}
f4bd7a8f 951
7ed4093a 952void
f4bd7a8f
DM
953bfd_putb64 (data, addr)
954 bfd_vma data;
955 register bfd_byte *addr;
7ed4093a 956{
b5b4294e 957#ifdef BFD64
536b27a5
SC
958 addr[0] = (bfd_byte)(data >> (7*8));
959 addr[1] = (bfd_byte)(data >> (6*8));
960 addr[2] = (bfd_byte)(data >> (5*8));
961 addr[3] = (bfd_byte)(data >> (4*8));
962 addr[4] = (bfd_byte)(data >> (3*8));
963 addr[5] = (bfd_byte)(data >> (2*8));
964 addr[6] = (bfd_byte)(data >> (1*8));
965 addr[7] = (bfd_byte)(data >> (0*8));
7ed4093a 966#else
536b27a5 967 BFD_FAIL();
7ed4093a 968#endif
7ed4093a
SC
969}
970
971void
f4bd7a8f
DM
972bfd_putl64 (data, addr)
973 bfd_vma data;
974 register bfd_byte *addr;
7ed4093a 975{
b5b4294e 976#ifdef BFD64
536b27a5
SC
977 addr[7] = (bfd_byte)(data >> (7*8));
978 addr[6] = (bfd_byte)(data >> (6*8));
979 addr[5] = (bfd_byte)(data >> (5*8));
980 addr[4] = (bfd_byte)(data >> (4*8));
981 addr[3] = (bfd_byte)(data >> (3*8));
982 addr[2] = (bfd_byte)(data >> (2*8));
983 addr[1] = (bfd_byte)(data >> (1*8));
984 addr[0] = (bfd_byte)(data >> (0*8));
7ed4093a 985#else
536b27a5 986 BFD_FAIL();
7ed4093a 987#endif
7ed4093a 988}
2203f786
JG
989\f
990/* Default implementation */
4a81b561 991
2203f786 992boolean
6812b607 993_bfd_generic_get_section_contents (abfd, section, location, offset, count)
f4bd7a8f
DM
994 bfd *abfd;
995 sec_ptr section;
996 PTR location;
997 file_ptr offset;
998 bfd_size_type count;
2203f786
JG
999{
1000 if (count == 0)
6f715d66 1001 return true;
f8e01940
JG
1002 if ((bfd_size_type)(offset+count) > section->_raw_size
1003 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
6f715d66
SC
1004 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
1005 return (false); /* on error */
2203f786
JG
1006 return (true);
1007}
6f715d66 1008
4fe6d901
KR
1009boolean
1010_bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count)
1011 bfd *abfd;
1012 sec_ptr section;
1013 bfd_window *w;
1014 file_ptr offset;
1015 bfd_size_type count;
1016{
1017 if (count == 0)
1018 return true;
1019 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents)
1020 {
1021 /* We don't know what changes the bfd's get_section_contents
1022 method may have to make. So punt trying to map the file
1023 window, and let get_section_contents do its thing. */
1024 /* @@ FIXME : If the internal window has a refcount of 1 and was
1025 allocated with malloc instead of mmap, just reuse it. */
1026 bfd_free_window (w);
1027 w->i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
1028 if (w->i == NULL)
1029 return false;
1030 w->i->data = (PTR) malloc ((size_t) count);
1031 if (w->i->data == NULL)
1032 {
1033 free (w->i);
1034 w->i = NULL;
1035 return false;
1036 }
1037 w->i->mapped = 0;
1038 w->i->refcount = 1;
1039 w->size = w->i->size = count;
1040 w->data = w->i->data;
1041 return bfd_get_section_contents (abfd, section, w->data, offset, count);
1042 }
1043 if ((bfd_size_type) (offset+count) > section->_raw_size
0bb8ff19 1044 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true)
4fe6d901
KR
1045 == false))
1046 return false;
1047 return true;
1048}
1049
f58809fd
SC
1050/* This generic function can only be used in implementations where creating
1051 NEW sections is disallowed. It is useful in patching existing sections
1052 in read-write files, though. See other set_section_contents functions
1053 to see why it doesn't work for new sections. */
1054boolean
6812b607 1055_bfd_generic_set_section_contents (abfd, section, location, offset, count)
df34342b
ILT
1056 bfd *abfd;
1057 sec_ptr section;
1058 PTR location;
1059 file_ptr offset;
1060 bfd_size_type count;
f58809fd 1061{
df34342b
ILT
1062 if (count == 0)
1063 return true;
1064
df34342b
ILT
1065 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
1066 || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
1067 return false;
1068
1069 return true;
f58809fd
SC
1070}
1071
f8e01940
JG
1072/*
1073INTERNAL_FUNCTION
1074 bfd_log2
1075
17ecba1a
DM
1076SYNOPSIS
1077 unsigned int bfd_log2(bfd_vma x);
1078
f8e01940 1079DESCRIPTION
c188b0be
DM
1080 Return the log base 2 of the value supplied, rounded up. E.g., an
1081 @var{x} of 1025 returns 11.
f8e01940 1082*/
6f715d66 1083
b5b4294e
JG
1084unsigned
1085bfd_log2(x)
1086 bfd_vma x;
6f715d66 1087{
b5b4294e 1088 unsigned result = 0;
6f715d66
SC
1089 while ( (bfd_vma)(1<< result) < x)
1090 result++;
1091 return result;
1092}
120f5bd9
JL
1093
1094boolean
1095bfd_generic_is_local_label (abfd, sym)
1096 bfd *abfd;
1097 asymbol *sym;
1098{
1099 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1100
1101 return (sym->name[0] == locals_prefix);
1102}
1103
This page took 0.232419 seconds and 4 git commands to generate.