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