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