libbfd.c revision 1.1.1.5 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 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 /* xgettext:c-format */
793 (_("%B: unable to get decompressed section %A"),
794 abfd, section);
795 bfd_set_error (bfd_error_invalid_operation);
796 return FALSE;
797 }
798
799 /* We do allow reading of a section after bfd_final_link has
800 written the contents out to disk. In that situation, rawsize is
801 just a stale version of size, so ignore it. Otherwise we must be
802 reading an input section, where rawsize, if different to size,
803 is the on-disk size. */
804 if (abfd->direction != write_direction && section->rawsize != 0)
805 sz = section->rawsize;
806 else
807 sz = section->size;
808 if (offset + count < count
809 || offset + count > sz)
810 {
811 bfd_set_error (bfd_error_invalid_operation);
812 return FALSE;
813 }
814
815 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
816 || bfd_bread (location, count, abfd) != count)
817 return FALSE;
818
819 return TRUE;
820 }
821
822 bfd_boolean
823 _bfd_generic_get_section_contents_in_window
824 (bfd *abfd ATTRIBUTE_UNUSED,
825 sec_ptr section ATTRIBUTE_UNUSED,
826 bfd_window *w ATTRIBUTE_UNUSED,
827 file_ptr offset ATTRIBUTE_UNUSED,
828 bfd_size_type count ATTRIBUTE_UNUSED)
829 {
830 #ifdef USE_MMAP
831 bfd_size_type sz;
832
833 if (count == 0)
834 return TRUE;
835 if (abfd->xvec->_bfd_get_section_contents
836 != _bfd_generic_get_section_contents)
837 {
838 /* We don't know what changes the bfd's get_section_contents
839 method may have to make. So punt trying to map the file
840 window, and let get_section_contents do its thing. */
841 /* @@ FIXME : If the internal window has a refcount of 1 and was
842 allocated with malloc instead of mmap, just reuse it. */
843 bfd_free_window (w);
844 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
845 if (w->i == NULL)
846 return FALSE;
847 w->i->data = bfd_malloc (count);
848 if (w->i->data == NULL)
849 {
850 free (w->i);
851 w->i = NULL;
852 return FALSE;
853 }
854 w->i->mapped = 0;
855 w->i->refcount = 1;
856 w->size = w->i->size = count;
857 w->data = w->i->data;
858 return bfd_get_section_contents (abfd, section, w->data, offset, count);
859 }
860 if (abfd->direction != write_direction && section->rawsize != 0)
861 sz = section->rawsize;
862 else
863 sz = section->size;
864 if (offset + count > sz
865 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
866 TRUE))
867 return FALSE;
868 return TRUE;
869 #else
870 abort ();
871 #endif
872 }
873
874 /* This generic function can only be used in implementations where creating
875 NEW sections is disallowed. It is useful in patching existing sections
876 in read-write files, though. See other set_section_contents functions
877 to see why it doesn't work for new sections. */
878 bfd_boolean
879 _bfd_generic_set_section_contents (bfd *abfd,
880 sec_ptr section,
881 const void *location,
882 file_ptr offset,
883 bfd_size_type count)
884 {
885 if (count == 0)
886 return TRUE;
887
888 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
889 || bfd_bwrite (location, count, abfd) != count)
890 return FALSE;
891
892 return TRUE;
893 }
894
895 /*
896 INTERNAL_FUNCTION
897 bfd_log2
898
899 SYNOPSIS
900 unsigned int bfd_log2 (bfd_vma x);
901
902 DESCRIPTION
903 Return the log base 2 of the value supplied, rounded up. E.g., an
904 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
905 */
906
907 unsigned int
908 bfd_log2 (bfd_vma x)
909 {
910 unsigned int result = 0;
911
912 if (x <= 1)
913 return result;
914 --x;
915 do
916 ++result;
917 while ((x >>= 1) != 0);
918 return result;
919 }
920
921 bfd_boolean
922 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
923 {
924 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
925
926 return name[0] == locals_prefix;
927 }
928
929 /* Give a warning at runtime if someone compiles code which calls
930 old routines. */
931
932 void
933 _bfd_warn_deprecated (const char *what,
934 const char *file,
935 int line,
936 const char *func)
937 {
938 /* Poor man's tracking of functions we've already warned about. */
939 static size_t mask = 0;
940
941 if (~(size_t) func & ~mask)
942 {
943 fflush (stdout);
944 /* Note: separate sentences in order to allow
945 for translation into other languages. */
946 if (func)
947 /* xgettext:c-format */
948 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
949 what, file, line, func);
950 else
951 fprintf (stderr, _("Deprecated %s called\n"), what);
952 fflush (stderr);
953 mask |= ~(size_t) func;
954 }
955 }
956
957 /* Helper function for reading uleb128 encoded data. */
958
959 bfd_vma
960 _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
961 bfd_byte *buf,
962 unsigned int *bytes_read_ptr)
963 {
964 bfd_vma result;
965 unsigned int num_read;
966 unsigned int shift;
967 unsigned char byte;
968
969 result = 0;
970 shift = 0;
971 num_read = 0;
972 do
973 {
974 byte = bfd_get_8 (abfd, buf);
975 buf++;
976 num_read++;
977 result |= (((bfd_vma) byte & 0x7f) << shift);
978 shift += 7;
979 }
980 while (byte & 0x80);
981 *bytes_read_ptr = num_read;
982 return result;
983 }
984
985 /* Read in a LEB128 encoded value from ABFD starting at DATA.
986 If SIGN is true, return a signed LEB128 value.
987 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
988 No bytes will be read at address END or beyond. */
989
990 bfd_vma
991 _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
992 bfd_byte *data,
993 unsigned int *length_return,
994 bfd_boolean sign,
995 const bfd_byte * const end)
996 {
997 bfd_vma result = 0;
998 unsigned int num_read = 0;
999 unsigned int shift = 0;
1000 unsigned char byte = 0;
1001
1002 while (data < end)
1003 {
1004 byte = bfd_get_8 (abfd, data);
1005 data++;
1006 num_read++;
1007
1008 result |= ((bfd_vma) (byte & 0x7f)) << shift;
1009
1010 shift += 7;
1011 if ((byte & 0x80) == 0)
1012 break;
1013 }
1014
1015 if (length_return != NULL)
1016 *length_return = num_read;
1017
1018 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
1019 result |= -((bfd_vma) 1 << shift);
1020
1021 return result;
1022 }
1023
1024 /* Helper function for reading sleb128 encoded data. */
1025
1026 bfd_signed_vma
1027 _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1028 bfd_byte *buf,
1029 unsigned int *bytes_read_ptr)
1030 {
1031 bfd_vma result;
1032 unsigned int shift;
1033 unsigned int num_read;
1034 unsigned char byte;
1035
1036 result = 0;
1037 shift = 0;
1038 num_read = 0;
1039 do
1040 {
1041 byte = bfd_get_8 (abfd, buf);
1042 buf ++;
1043 num_read ++;
1044 result |= (((bfd_vma) byte & 0x7f) << shift);
1045 shift += 7;
1046 }
1047 while (byte & 0x80);
1048 if (shift < 8 * sizeof (result) && (byte & 0x40))
1049 result |= (((bfd_vma) -1) << shift);
1050 *bytes_read_ptr = num_read;
1051 return result;
1052 }
1053
1054 bfd_boolean
1055 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1056 asection *isec ATTRIBUTE_UNUSED,
1057 bfd *obfd ATTRIBUTE_UNUSED,
1058 asection *osec ATTRIBUTE_UNUSED,
1059 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1060 {
1061 return TRUE;
1062 }
1063