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