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