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