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