Merge branch 'vendor/TCPDUMP' (early part)
[dragonfly.git] / contrib / binutils-2.17 / bfd / libbfd.c
1 /* Assorted BFD support routines, only used internally.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5    Written by Cygnus Support.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26
27 #ifndef HAVE_GETPAGESIZE
28 #define getpagesize() 2048
29 #endif
30
31 /*
32 SECTION
33         Implementation details
34
35 SUBSECTION
36         Internal functions
37
38 DESCRIPTION
39         These routines are used within BFD.
40         They are not intended for export, but are documented here for
41         completeness.
42 */
43
44 /* A routine which is used in target vectors for unsupported
45    operations.  */
46
47 bfd_boolean
48 bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
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 bfd_boolean
58 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
59 {
60   return TRUE;
61 }
62
63 /* A routine which is used in target vectors for unsupported
64    operations which return a pointer value.  */
65
66 void *
67 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
68 {
69   bfd_set_error (bfd_error_invalid_operation);
70   return NULL;
71 }
72
73 int
74 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
75 {
76   return 0;
77 }
78
79 unsigned int
80 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
81 {
82    return 0;
83 }
84
85 long
86 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
87 {
88   return 0;
89 }
90
91 /* A routine which is used in target vectors for unsupported
92    operations which return -1 on error.  */
93
94 long
95 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
96 {
97   bfd_set_error (bfd_error_invalid_operation);
98   return -1;
99 }
100
101 void
102 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
103 {
104 }
105
106 bfd_boolean
107 _bfd_nocore_core_file_matches_executable_p
108   (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
109    bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
110 {
111   bfd_set_error (bfd_error_invalid_operation);
112   return FALSE;
113 }
114
115 /* Routine to handle core_file_failing_command entry point for targets
116    without core file support.  */
117
118 char *
119 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
120 {
121   bfd_set_error (bfd_error_invalid_operation);
122   return NULL;
123 }
124
125 /* Routine to handle core_file_failing_signal entry point for targets
126    without core file support.  */
127
128 int
129 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
130 {
131   bfd_set_error (bfd_error_invalid_operation);
132   return 0;
133 }
134
135 const bfd_target *
136 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
137 {
138   bfd_set_error (bfd_error_wrong_format);
139   return 0;
140 }
141 \f
142 /* Allocate memory using malloc.  */
143
144 void *
145 bfd_malloc (bfd_size_type size)
146 {
147   void *ptr;
148
149   if (size != (size_t) size)
150     {
151       bfd_set_error (bfd_error_no_memory);
152       return NULL;
153     }
154
155   ptr = malloc ((size_t) size);
156   if (ptr == NULL && (size_t) size != 0)
157     bfd_set_error (bfd_error_no_memory);
158
159   return ptr;
160 }
161
162 /* Allocate memory using malloc, nmemb * size with overflow checking.  */
163
164 void *
165 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
166 {
167   void *ptr;
168
169   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
170       && size != 0
171       && nmemb > ~(bfd_size_type) 0 / size)
172     {
173       bfd_set_error (bfd_error_no_memory);
174       return NULL;
175     }
176
177   size *= nmemb;
178
179   if (size != (size_t) size)
180     {
181       bfd_set_error (bfd_error_no_memory);
182       return NULL;
183     }
184
185   ptr = malloc ((size_t) size);
186   if (ptr == NULL && (size_t) size != 0)
187     bfd_set_error (bfd_error_no_memory);
188
189   return ptr;
190 }
191
192 /* Reallocate memory using realloc.  */
193
194 void *
195 bfd_realloc (void *ptr, bfd_size_type size)
196 {
197   void *ret;
198
199   if (size != (size_t) size)
200     {
201       bfd_set_error (bfd_error_no_memory);
202       return NULL;
203     }
204
205   if (ptr == NULL)
206     ret = malloc ((size_t) size);
207   else
208     ret = realloc (ptr, (size_t) size);
209
210   if (ret == NULL && (size_t) size != 0)
211     bfd_set_error (bfd_error_no_memory);
212
213   return ret;
214 }
215
216 /* Reallocate memory using realloc, nmemb * size with overflow checking.  */
217
218 void *
219 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
220 {
221   void *ret;
222
223   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
224       && size != 0
225       && nmemb > ~(bfd_size_type) 0 / size)
226     {
227       bfd_set_error (bfd_error_no_memory);
228       return NULL;
229     }
230
231   size *= nmemb;
232
233   if (size != (size_t) size)
234     {
235       bfd_set_error (bfd_error_no_memory);
236       return NULL;
237     }
238
239   if (ptr == NULL)
240     ret = malloc ((size_t) size);
241   else
242     ret = realloc (ptr, (size_t) size);
243
244   if (ret == NULL && (size_t) size != 0)
245     bfd_set_error (bfd_error_no_memory);
246
247   return ret;
248 }
249
250 /* Allocate memory using malloc and clear it.  */
251
252 void *
253 bfd_zmalloc (bfd_size_type size)
254 {
255   void *ptr;
256
257   if (size != (size_t) size)
258     {
259       bfd_set_error (bfd_error_no_memory);
260       return NULL;
261     }
262
263   ptr = malloc ((size_t) size);
264
265   if ((size_t) size != 0)
266     {
267       if (ptr == NULL)
268         bfd_set_error (bfd_error_no_memory);
269       else
270         memset (ptr, 0, (size_t) size);
271     }
272
273   return ptr;
274 }
275
276 /* Allocate memory using malloc (nmemb * size) with overflow checking
277    and clear it.  */
278
279 void *
280 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
281 {
282   void *ptr;
283
284   if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
285       && size != 0
286       && nmemb > ~(bfd_size_type) 0 / size)
287     {
288       bfd_set_error (bfd_error_no_memory);
289       return NULL;
290     }
291
292   size *= nmemb;
293
294   if (size != (size_t) size)
295     {
296       bfd_set_error (bfd_error_no_memory);
297       return NULL;
298     }
299
300   ptr = malloc ((size_t) size);
301
302   if ((size_t) size != 0)
303     {
304       if (ptr == NULL)
305         bfd_set_error (bfd_error_no_memory);
306       else
307         memset (ptr, 0, (size_t) size);
308     }
309
310   return ptr;
311 }
312
313 /*
314 INTERNAL_FUNCTION
315         bfd_write_bigendian_4byte_int
316
317 SYNOPSIS
318         bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
319
320 DESCRIPTION
321         Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
322         endian order regardless of what else is going on.  This is useful in
323         archives.
324
325 */
326 bfd_boolean
327 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
328 {
329   bfd_byte buffer[4];
330   bfd_putb32 ((bfd_vma) i, buffer);
331   return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
332 }
333
334 \f
335 /** The do-it-yourself (byte) sex-change kit */
336
337 /* The middle letter e.g. get<b>short indicates Big or Little endian
338    target machine.  It doesn't matter what the byte order of the host
339    machine is; these routines work for either.  */
340
341 /* FIXME: Should these take a count argument?
342    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
343                              functions in swap.h #ifdef __GNUC__.
344                              Gprof them later and find out.  */
345
346 /*
347 FUNCTION
348         bfd_put_size
349 FUNCTION
350         bfd_get_size
351
352 DESCRIPTION
353         These macros as used for reading and writing raw data in
354         sections; each access (except for bytes) is vectored through
355         the target format of the BFD and mangled accordingly. The
356         mangling performs any necessary endian translations and
357         removes alignment restrictions.  Note that types accepted and
358         returned by these macros are identical so they can be swapped
359         around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
360         to either <<bfd_get_32>> or <<bfd_get_64>>.
361
362         In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
363         system without prototypes, the caller is responsible for making
364         sure that is true, with a cast if necessary.  We don't cast
365         them in the macro definitions because that would prevent <<lint>>
366         or <<gcc -Wall>> from detecting sins such as passing a pointer.
367         To detect calling these with less than a <<bfd_vma>>, use
368         <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
369
370 .
371 .{* Byte swapping macros for user section data.  *}
372 .
373 .#define bfd_put_8(abfd, val, ptr) \
374 .  ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
375 .#define bfd_put_signed_8 \
376 .  bfd_put_8
377 .#define bfd_get_8(abfd, ptr) \
378 .  (*(unsigned char *) (ptr) & 0xff)
379 .#define bfd_get_signed_8(abfd, ptr) \
380 .  (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
381 .
382 .#define bfd_put_16(abfd, val, ptr) \
383 .  BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
384 .#define bfd_put_signed_16 \
385 .  bfd_put_16
386 .#define bfd_get_16(abfd, ptr) \
387 .  BFD_SEND (abfd, bfd_getx16, (ptr))
388 .#define bfd_get_signed_16(abfd, ptr) \
389 .  BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
390 .
391 .#define bfd_put_32(abfd, val, ptr) \
392 .  BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
393 .#define bfd_put_signed_32 \
394 .  bfd_put_32
395 .#define bfd_get_32(abfd, ptr) \
396 .  BFD_SEND (abfd, bfd_getx32, (ptr))
397 .#define bfd_get_signed_32(abfd, ptr) \
398 .  BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
399 .
400 .#define bfd_put_64(abfd, val, ptr) \
401 .  BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
402 .#define bfd_put_signed_64 \
403 .  bfd_put_64
404 .#define bfd_get_64(abfd, ptr) \
405 .  BFD_SEND (abfd, bfd_getx64, (ptr))
406 .#define bfd_get_signed_64(abfd, ptr) \
407 .  BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
408 .
409 .#define bfd_get(bits, abfd, ptr)                       \
410 .  ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr)       \
411 .   : (bits) == 16 ? bfd_get_16 (abfd, ptr)             \
412 .   : (bits) == 32 ? bfd_get_32 (abfd, ptr)             \
413 .   : (bits) == 64 ? bfd_get_64 (abfd, ptr)             \
414 .   : (abort (), (bfd_vma) - 1))
415 .
416 .#define bfd_put(bits, abfd, val, ptr)                  \
417 .  ((bits) == 8 ? bfd_put_8  (abfd, val, ptr)           \
418 .   : (bits) == 16 ? bfd_put_16 (abfd, val, ptr)                \
419 .   : (bits) == 32 ? bfd_put_32 (abfd, val, ptr)                \
420 .   : (bits) == 64 ? bfd_put_64 (abfd, val, ptr)                \
421 .   : (abort (), (void) 0))
422 .
423 */
424
425 /*
426 FUNCTION
427         bfd_h_put_size
428         bfd_h_get_size
429
430 DESCRIPTION
431         These macros have the same function as their <<bfd_get_x>>
432         brethren, except that they are used for removing information
433         for the header records of object files. Believe it or not,
434         some object files keep their header records in big endian
435         order and their data in little endian order.
436 .
437 .{* Byte swapping macros for file header data.  *}
438 .
439 .#define bfd_h_put_8(abfd, val, ptr) \
440 .  bfd_put_8 (abfd, val, ptr)
441 .#define bfd_h_put_signed_8(abfd, val, ptr) \
442 .  bfd_put_8 (abfd, val, ptr)
443 .#define bfd_h_get_8(abfd, ptr) \
444 .  bfd_get_8 (abfd, ptr)
445 .#define bfd_h_get_signed_8(abfd, ptr) \
446 .  bfd_get_signed_8 (abfd, ptr)
447 .
448 .#define bfd_h_put_16(abfd, val, ptr) \
449 .  BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
450 .#define bfd_h_put_signed_16 \
451 .  bfd_h_put_16
452 .#define bfd_h_get_16(abfd, ptr) \
453 .  BFD_SEND (abfd, bfd_h_getx16, (ptr))
454 .#define bfd_h_get_signed_16(abfd, ptr) \
455 .  BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
456 .
457 .#define bfd_h_put_32(abfd, val, ptr) \
458 .  BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
459 .#define bfd_h_put_signed_32 \
460 .  bfd_h_put_32
461 .#define bfd_h_get_32(abfd, ptr) \
462 .  BFD_SEND (abfd, bfd_h_getx32, (ptr))
463 .#define bfd_h_get_signed_32(abfd, ptr) \
464 .  BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
465 .
466 .#define bfd_h_put_64(abfd, val, ptr) \
467 .  BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
468 .#define bfd_h_put_signed_64 \
469 .  bfd_h_put_64
470 .#define bfd_h_get_64(abfd, ptr) \
471 .  BFD_SEND (abfd, bfd_h_getx64, (ptr))
472 .#define bfd_h_get_signed_64(abfd, ptr) \
473 .  BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
474 .
475 .{* Aliases for the above, which should eventually go away.  *}
476 .
477 .#define H_PUT_64  bfd_h_put_64
478 .#define H_PUT_32  bfd_h_put_32
479 .#define H_PUT_16  bfd_h_put_16
480 .#define H_PUT_8   bfd_h_put_8
481 .#define H_PUT_S64 bfd_h_put_signed_64
482 .#define H_PUT_S32 bfd_h_put_signed_32
483 .#define H_PUT_S16 bfd_h_put_signed_16
484 .#define H_PUT_S8  bfd_h_put_signed_8
485 .#define H_GET_64  bfd_h_get_64
486 .#define H_GET_32  bfd_h_get_32
487 .#define H_GET_16  bfd_h_get_16
488 .#define H_GET_8   bfd_h_get_8
489 .#define H_GET_S64 bfd_h_get_signed_64
490 .#define H_GET_S32 bfd_h_get_signed_32
491 .#define H_GET_S16 bfd_h_get_signed_16
492 .#define H_GET_S8  bfd_h_get_signed_8
493 .
494 .*/
495
496 /* Sign extension to bfd_signed_vma.  */
497 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
498 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
499 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
500 #define COERCE64(x) \
501   (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
502
503 bfd_vma
504 bfd_getb16 (const void *p)
505 {
506   const bfd_byte *addr = p;
507   return (addr[0] << 8) | addr[1];
508 }
509
510 bfd_vma
511 bfd_getl16 (const void *p)
512 {
513   const bfd_byte *addr = p;
514   return (addr[1] << 8) | addr[0];
515 }
516
517 bfd_signed_vma
518 bfd_getb_signed_16 (const void *p)
519 {
520   const bfd_byte *addr = p;
521   return COERCE16 ((addr[0] << 8) | addr[1]);
522 }
523
524 bfd_signed_vma
525 bfd_getl_signed_16 (const void *p)
526 {
527   const bfd_byte *addr = p;
528   return COERCE16 ((addr[1] << 8) | addr[0]);
529 }
530
531 void
532 bfd_putb16 (bfd_vma data, void *p)
533 {
534   bfd_byte *addr = p;
535   addr[0] = (data >> 8) & 0xff;
536   addr[1] = data & 0xff;
537 }
538
539 void
540 bfd_putl16 (bfd_vma data, void *p)
541 {
542   bfd_byte *addr = p;
543   addr[0] = data & 0xff;
544   addr[1] = (data >> 8) & 0xff;
545 }
546
547 bfd_vma
548 bfd_getb32 (const void *p)
549 {
550   const bfd_byte *addr = p;
551   unsigned long v;
552
553   v = (unsigned long) addr[0] << 24;
554   v |= (unsigned long) addr[1] << 16;
555   v |= (unsigned long) addr[2] << 8;
556   v |= (unsigned long) addr[3];
557   return v;
558 }
559
560 bfd_vma
561 bfd_getl32 (const void *p)
562 {
563   const bfd_byte *addr = p;
564   unsigned long v;
565
566   v = (unsigned long) addr[0];
567   v |= (unsigned long) addr[1] << 8;
568   v |= (unsigned long) addr[2] << 16;
569   v |= (unsigned long) addr[3] << 24;
570   return v;
571 }
572
573 bfd_signed_vma
574 bfd_getb_signed_32 (const void *p)
575 {
576   const bfd_byte *addr = p;
577   unsigned long v;
578
579   v = (unsigned long) addr[0] << 24;
580   v |= (unsigned long) addr[1] << 16;
581   v |= (unsigned long) addr[2] << 8;
582   v |= (unsigned long) addr[3];
583   return COERCE32 (v);
584 }
585
586 bfd_signed_vma
587 bfd_getl_signed_32 (const void *p)
588 {
589   const bfd_byte *addr = p;
590   unsigned long v;
591
592   v = (unsigned long) addr[0];
593   v |= (unsigned long) addr[1] << 8;
594   v |= (unsigned long) addr[2] << 16;
595   v |= (unsigned long) addr[3] << 24;
596   return COERCE32 (v);
597 }
598
599 bfd_uint64_t
600 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
601 {
602 #ifdef BFD_HOST_64_BIT
603   const bfd_byte *addr = p;
604   bfd_uint64_t v;
605
606   v  = addr[0]; v <<= 8;
607   v |= addr[1]; v <<= 8;
608   v |= addr[2]; v <<= 8;
609   v |= addr[3]; v <<= 8;
610   v |= addr[4]; v <<= 8;
611   v |= addr[5]; v <<= 8;
612   v |= addr[6]; v <<= 8;
613   v |= addr[7];
614
615   return v;
616 #else
617   BFD_FAIL();
618   return 0;
619 #endif
620 }
621
622 bfd_uint64_t
623 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
624 {
625 #ifdef BFD_HOST_64_BIT
626   const bfd_byte *addr = p;
627   bfd_uint64_t v;
628
629   v  = addr[7]; v <<= 8;
630   v |= addr[6]; v <<= 8;
631   v |= addr[5]; v <<= 8;
632   v |= addr[4]; v <<= 8;
633   v |= addr[3]; v <<= 8;
634   v |= addr[2]; v <<= 8;
635   v |= addr[1]; v <<= 8;
636   v |= addr[0];
637
638   return v;
639 #else
640   BFD_FAIL();
641   return 0;
642 #endif
643
644 }
645
646 bfd_int64_t
647 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
648 {
649 #ifdef BFD_HOST_64_BIT
650   const bfd_byte *addr = p;
651   bfd_uint64_t v;
652
653   v  = addr[0]; v <<= 8;
654   v |= addr[1]; v <<= 8;
655   v |= addr[2]; v <<= 8;
656   v |= addr[3]; v <<= 8;
657   v |= addr[4]; v <<= 8;
658   v |= addr[5]; v <<= 8;
659   v |= addr[6]; v <<= 8;
660   v |= addr[7];
661
662   return COERCE64 (v);
663 #else
664   BFD_FAIL();
665   return 0;
666 #endif
667 }
668
669 bfd_int64_t
670 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
671 {
672 #ifdef BFD_HOST_64_BIT
673   const bfd_byte *addr = p;
674   bfd_uint64_t v;
675
676   v  = addr[7]; v <<= 8;
677   v |= addr[6]; v <<= 8;
678   v |= addr[5]; v <<= 8;
679   v |= addr[4]; v <<= 8;
680   v |= addr[3]; v <<= 8;
681   v |= addr[2]; v <<= 8;
682   v |= addr[1]; v <<= 8;
683   v |= addr[0];
684
685   return COERCE64 (v);
686 #else
687   BFD_FAIL();
688   return 0;
689 #endif
690 }
691
692 void
693 bfd_putb32 (bfd_vma data, void *p)
694 {
695   bfd_byte *addr = p;
696   addr[0] = (data >> 24) & 0xff;
697   addr[1] = (data >> 16) & 0xff;
698   addr[2] = (data >>  8) & 0xff;
699   addr[3] = data & 0xff;
700 }
701
702 void
703 bfd_putl32 (bfd_vma data, void *p)
704 {
705   bfd_byte *addr = p;
706   addr[0] = data & 0xff;
707   addr[1] = (data >>  8) & 0xff;
708   addr[2] = (data >> 16) & 0xff;
709   addr[3] = (data >> 24) & 0xff;
710 }
711
712 void
713 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
714 {
715 #ifdef BFD_HOST_64_BIT
716   bfd_byte *addr = p;
717   addr[0] = (data >> (7*8)) & 0xff;
718   addr[1] = (data >> (6*8)) & 0xff;
719   addr[2] = (data >> (5*8)) & 0xff;
720   addr[3] = (data >> (4*8)) & 0xff;
721   addr[4] = (data >> (3*8)) & 0xff;
722   addr[5] = (data >> (2*8)) & 0xff;
723   addr[6] = (data >> (1*8)) & 0xff;
724   addr[7] = (data >> (0*8)) & 0xff;
725 #else
726   BFD_FAIL();
727 #endif
728 }
729
730 void
731 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
732 {
733 #ifdef BFD_HOST_64_BIT
734   bfd_byte *addr = p;
735   addr[7] = (data >> (7*8)) & 0xff;
736   addr[6] = (data >> (6*8)) & 0xff;
737   addr[5] = (data >> (5*8)) & 0xff;
738   addr[4] = (data >> (4*8)) & 0xff;
739   addr[3] = (data >> (3*8)) & 0xff;
740   addr[2] = (data >> (2*8)) & 0xff;
741   addr[1] = (data >> (1*8)) & 0xff;
742   addr[0] = (data >> (0*8)) & 0xff;
743 #else
744   BFD_FAIL();
745 #endif
746 }
747
748 void
749 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
750 {
751   bfd_byte *addr = p;
752   int i;
753   int bytes;
754
755   if (bits % 8 != 0)
756     abort ();
757
758   bytes = bits / 8;
759   for (i = 0; i < bytes; i++)
760     {
761       int index = big_p ? bytes - i - 1 : i;
762
763       addr[index] = data & 0xff;
764       data >>= 8;
765     }
766 }
767
768 bfd_uint64_t
769 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
770 {
771   const bfd_byte *addr = p;
772   bfd_uint64_t data;
773   int i;
774   int bytes;
775
776   if (bits % 8 != 0)
777     abort ();
778
779   data = 0;
780   bytes = bits / 8;
781   for (i = 0; i < bytes; i++)
782     {
783       int index = big_p ? i : bytes - i - 1;
784
785       data = (data << 8) | addr[index];
786     }
787
788   return data;
789 }
790 \f
791 /* Default implementation */
792
793 bfd_boolean
794 _bfd_generic_get_section_contents (bfd *abfd,
795                                    sec_ptr section,
796                                    void *location,
797                                    file_ptr offset,
798                                    bfd_size_type count)
799 {
800   bfd_size_type sz;
801   if (count == 0)
802     return TRUE;
803
804   sz = section->rawsize ? section->rawsize : section->size;
805   if (offset + count > sz)
806     {
807       bfd_set_error (bfd_error_invalid_operation);
808       return FALSE;
809     }
810
811   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
812       || bfd_bread (location, count, abfd) != count)
813     return FALSE;
814
815   return TRUE;
816 }
817
818 bfd_boolean
819 _bfd_generic_get_section_contents_in_window
820   (bfd *abfd ATTRIBUTE_UNUSED,
821    sec_ptr section ATTRIBUTE_UNUSED,
822    bfd_window *w ATTRIBUTE_UNUSED,
823    file_ptr offset ATTRIBUTE_UNUSED,
824    bfd_size_type count ATTRIBUTE_UNUSED)
825 {
826 #ifdef USE_MMAP
827   bfd_size_type sz;
828
829   if (count == 0)
830     return TRUE;
831   if (abfd->xvec->_bfd_get_section_contents
832       != _bfd_generic_get_section_contents)
833     {
834       /* We don't know what changes the bfd's get_section_contents
835          method may have to make.  So punt trying to map the file
836          window, and let get_section_contents do its thing.  */
837       /* @@ FIXME : If the internal window has a refcount of 1 and was
838          allocated with malloc instead of mmap, just reuse it.  */
839       bfd_free_window (w);
840       w->i = bfd_zmalloc (sizeof (bfd_window_internal));
841       if (w->i == NULL)
842         return FALSE;
843       w->i->data = bfd_malloc (count);
844       if (w->i->data == NULL)
845         {
846           free (w->i);
847           w->i = NULL;
848           return FALSE;
849         }
850       w->i->mapped = 0;
851       w->i->refcount = 1;
852       w->size = w->i->size = count;
853       w->data = w->i->data;
854       return bfd_get_section_contents (abfd, section, w->data, offset, count);
855     }
856   sz = section->rawsize ? section->rawsize : section->size;
857   if (offset + count > sz
858       || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
859                                 TRUE))
860     return FALSE;
861   return TRUE;
862 #else
863   abort ();
864 #endif
865 }
866
867 /* This generic function can only be used in implementations where creating
868    NEW sections is disallowed.  It is useful in patching existing sections
869    in read-write files, though.  See other set_section_contents functions
870    to see why it doesn't work for new sections.  */
871 bfd_boolean
872 _bfd_generic_set_section_contents (bfd *abfd,
873                                    sec_ptr section,
874                                    const void *location,
875                                    file_ptr offset,
876                                    bfd_size_type count)
877 {
878   if (count == 0)
879     return TRUE;
880
881   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
882       || bfd_bwrite (location, count, abfd) != count)
883     return FALSE;
884
885   return TRUE;
886 }
887
888 /*
889 INTERNAL_FUNCTION
890         bfd_log2
891
892 SYNOPSIS
893         unsigned int bfd_log2 (bfd_vma x);
894
895 DESCRIPTION
896         Return the log base 2 of the value supplied, rounded up.  E.g., an
897         @var{x} of 1025 returns 11.  A @var{x} of 0 returns 0.
898 */
899
900 unsigned int
901 bfd_log2 (bfd_vma x)
902 {
903   unsigned int result = 0;
904
905   while ((x = (x >> 1)) != 0)
906     ++result;
907   return result;
908 }
909
910 bfd_boolean
911 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
912 {
913   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
914
915   return name[0] == locals_prefix;
916 }
917
918 /*  Can be used from / for bfd_merge_private_bfd_data to check that
919     endianness matches between input and output file.  Returns
920     TRUE for a match, otherwise returns FALSE and emits an error.  */
921 bfd_boolean
922 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
923 {
924   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
925       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
926       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
927     {
928       const char *msg;
929
930       if (bfd_big_endian (ibfd))
931         msg = _("%B: compiled for a big endian system and target is little endian");
932       else
933         msg = _("%B: compiled for a little endian system and target is big endian");
934
935       (*_bfd_error_handler) (msg, ibfd);
936
937       bfd_set_error (bfd_error_wrong_format);
938       return FALSE;
939     }
940
941   return TRUE;
942 }
943
944 /* Give a warning at runtime if someone compiles code which calls
945    old routines.  */
946
947 void
948 warn_deprecated (const char *what,
949                  const char *file,
950                  int line,
951                  const char *func)
952 {
953   /* Poor man's tracking of functions we've already warned about.  */
954   static size_t mask = 0;
955
956   if (~(size_t) func & ~mask)
957     {
958       /* Note: separate sentences in order to allow
959          for translation into other languages.  */
960       if (func)
961         fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
962                  what, file, line, func);
963       else
964         fprintf (stderr, _("Deprecated %s called\n"), what);
965       mask |= ~(size_t) func;
966     }
967 }
968
969 /* Helper function for reading uleb128 encoded data.  */
970
971 bfd_vma
972 read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
973                       bfd_byte *buf,
974                       unsigned int *bytes_read_ptr)
975 {
976   bfd_vma result;
977   unsigned int num_read;
978   unsigned int shift;
979   unsigned char byte;
980
981   result = 0;
982   shift = 0;
983   num_read = 0;
984   do
985     {
986       byte = bfd_get_8 (abfd, buf);
987       buf++;
988       num_read++;
989       result |= (((bfd_vma) byte & 0x7f) << shift);
990       shift += 7;
991     }
992   while (byte & 0x80);
993   *bytes_read_ptr = num_read;
994   return result;
995 }
996
997 /* Helper function for reading sleb128 encoded data.  */
998
999 bfd_signed_vma
1000 read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1001                     bfd_byte *buf,
1002                     unsigned int *bytes_read_ptr)
1003 {
1004   bfd_vma result;
1005   unsigned int shift;
1006   unsigned int num_read;
1007   unsigned char byte;
1008
1009   result = 0;
1010   shift = 0;
1011   num_read = 0;
1012   do
1013     {
1014       byte = bfd_get_8 (abfd, buf);
1015       buf ++;
1016       num_read ++;
1017       result |= (((bfd_vma) byte & 0x7f) << shift);
1018       shift += 7;
1019     }
1020   while (byte & 0x80);
1021   if (shift < 8 * sizeof (result) && (byte & 0x40))
1022     result |= (((bfd_vma) -1) << shift);
1023   *bytes_read_ptr = num_read;
1024   return result;
1025 }
1026
1027 bfd_boolean
1028 _bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
1029                        asymbol **symbols ATTRIBUTE_UNUSED,
1030                        asymbol *symbol ATTRIBUTE_UNUSED,
1031                        const char **filename_ptr ATTRIBUTE_UNUSED,
1032                        unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
1033 {
1034   return FALSE;
1035 }
1036
1037 bfd_boolean
1038 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1039                                         asection *isec ATTRIBUTE_UNUSED,
1040                                         bfd *obfd ATTRIBUTE_UNUSED,
1041                                         asection *osec ATTRIBUTE_UNUSED,
1042                                         struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1043 {
1044   return TRUE;
1045 }