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