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