Fix compile time warning messages from gcc version 8 about cast between incompatible...
[deliverable/binutils-gdb.git] / bfd / libbfd.c
1 /* Assorted BFD support routines, only used internally.
2 Copyright (C) 1990-2018 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 /* Like bfd_false, but accepts an arbitrary number of arguments. */
54
55 bfd_boolean
56 bfd_false_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
57 {
58 bfd_set_error (bfd_error_invalid_operation);
59 return FALSE;
60 }
61
62 /* A routine which is used in target vectors for supported operations
63 which do not actually do anything. */
64
65 bfd_boolean
66 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
67 {
68 return TRUE;
69 }
70
71 /* Like bfd_true, but accepts an arbitrary number of arguments. */
72
73 bfd_boolean
74 bfd_true_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
75 {
76 return TRUE;
77 }
78
79 /* A routine which is used in target vectors for unsupported
80 operations which return a pointer value. */
81
82 void *
83 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
84 {
85 bfd_set_error (bfd_error_invalid_operation);
86 return NULL;
87 }
88
89 /* Like bfd_nullvoidptr, but accepts an arbitrary number of arguments. */
90
91 void *
92 bfd_nullvoidptr_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
93 {
94 bfd_set_error (bfd_error_invalid_operation);
95 return NULL;
96 }
97
98 int
99 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
100 {
101 return 0;
102 }
103
104 /* Like bfd_0, but accepts an arbitrary number of arguments. */
105
106 int
107 bfd_0_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
108 {
109 return 0;
110 }
111
112 unsigned int
113 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
114 {
115 return 0;
116 }
117
118 /* Like bfd_0u, but accepts an arbitrary number of arguments. */
119
120 unsigned int
121 bfd_0u_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
122 {
123 return 0;
124 }
125
126 long
127 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
128 {
129 return 0;
130 }
131
132 /* Like bfd_0l, but accepts an arbitrary number of arguments. */
133
134 long
135 bfd_0l_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
136 {
137 return 0;
138 }
139
140 /* A routine which is used in target vectors for unsupported
141 operations which return -1 on error. */
142
143 long
144 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
145 {
146 bfd_set_error (bfd_error_invalid_operation);
147 return -1;
148 }
149
150 /* Like bfd_n1, but accepts an arbitrary number of arguments. */
151
152 long
153 _bfd_n1_any (bfd *ignore_abfd ATTRIBUTE_UNUSED, ...)
154 {
155 bfd_set_error (bfd_error_invalid_operation);
156 return -1;
157 }
158
159 void
160 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
161 {
162 }
163
164 /* Like bfd_void, but accepts an arbitrary number of arguments. */
165
166 void
167 bfd_void_any (bfd *ignore ATTRIBUTE_UNUSED, ...)
168 {
169 }
170
171 long
172 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
173 asection *sec ATTRIBUTE_UNUSED)
174 {
175 return sizeof (arelent *);
176 }
177
178 long
179 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
180 asection *sec ATTRIBUTE_UNUSED,
181 arelent **relptr,
182 asymbol **symbols ATTRIBUTE_UNUSED)
183 {
184 *relptr = NULL;
185 return 0;
186 }
187
188 void
189 _bfd_norelocs_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
190 asection *sec ATTRIBUTE_UNUSED,
191 arelent **relptr ATTRIBUTE_UNUSED,
192 unsigned int count ATTRIBUTE_UNUSED)
193 {
194 /* Do nothing. */
195 }
196
197 bfd_boolean
198 _bfd_nocore_core_file_matches_executable_p
199 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
200 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
201 {
202 bfd_set_error (bfd_error_invalid_operation);
203 return FALSE;
204 }
205
206 /* Routine to handle core_file_failing_command entry point for targets
207 without core file support. */
208
209 char *
210 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
211 {
212 bfd_set_error (bfd_error_invalid_operation);
213 return NULL;
214 }
215
216 /* Routine to handle core_file_failing_signal entry point for targets
217 without core file support. */
218
219 int
220 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
221 {
222 bfd_set_error (bfd_error_invalid_operation);
223 return 0;
224 }
225
226 /* Routine to handle the core_file_pid entry point for targets without
227 core file support. */
228
229 int
230 _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
231 {
232 bfd_set_error (bfd_error_invalid_operation);
233 return 0;
234 }
235
236 const bfd_target *
237 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
238 {
239 bfd_set_error (bfd_error_wrong_format);
240 return 0;
241 }
242 \f
243 /* Allocate memory using malloc. */
244
245 void *
246 bfd_malloc (bfd_size_type size)
247 {
248 void *ptr;
249 size_t sz = (size_t) size;
250
251 if (size != sz
252 /* This is to pacify memory checkers like valgrind. */
253 || ((signed long) sz) < 0)
254 {
255 bfd_set_error (bfd_error_no_memory);
256 return NULL;
257 }
258
259 ptr = malloc (sz);
260 if (ptr == NULL && sz != 0)
261 bfd_set_error (bfd_error_no_memory);
262
263 return ptr;
264 }
265
266 /* Allocate memory using malloc, nmemb * size with overflow checking. */
267
268 void *
269 bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
270 {
271 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
272 && size != 0
273 && nmemb > ~(bfd_size_type) 0 / size)
274 {
275 bfd_set_error (bfd_error_no_memory);
276 return NULL;
277 }
278
279 return bfd_malloc (size * nmemb);
280 }
281
282 /* Reallocate memory using realloc. */
283
284 void *
285 bfd_realloc (void *ptr, bfd_size_type size)
286 {
287 void *ret;
288 size_t sz = (size_t) size;
289
290 if (ptr == NULL)
291 return bfd_malloc (size);
292
293 if (size != sz
294 /* This is to pacify memory checkers like valgrind. */
295 || ((signed long) sz) < 0)
296 {
297 bfd_set_error (bfd_error_no_memory);
298 return NULL;
299 }
300
301 ret = realloc (ptr, sz);
302
303 if (ret == NULL && sz != 0)
304 bfd_set_error (bfd_error_no_memory);
305
306 return ret;
307 }
308
309 /* Reallocate memory using realloc, nmemb * size with overflow checking. */
310
311 void *
312 bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
313 {
314 if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
315 && size != 0
316 && nmemb > ~(bfd_size_type) 0 / size)
317 {
318 bfd_set_error (bfd_error_no_memory);
319 return NULL;
320 }
321
322 return bfd_realloc (ptr, size * nmemb);
323 }
324
325 /* Reallocate memory using realloc.
326 If this fails the pointer is freed before returning. */
327
328 void *
329 bfd_realloc_or_free (void *ptr, bfd_size_type size)
330 {
331 void *ret = bfd_realloc (ptr, size);
332
333 if (ret == NULL && ptr != NULL)
334 free (ptr);
335
336 return ret;
337 }
338
339 /* Allocate memory using malloc and clear it. */
340
341 void *
342 bfd_zmalloc (bfd_size_type size)
343 {
344 void *ptr = bfd_malloc (size);
345
346 if (ptr != NULL && size > 0)
347 memset (ptr, 0, (size_t) size);
348
349 return ptr;
350 }
351
352 /* Allocate memory using malloc (nmemb * size) with overflow checking
353 and clear it. */
354
355 void *
356 bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
357 {
358 void *ptr = bfd_malloc2 (nmemb, size);
359
360 if (ptr != NULL)
361 {
362 size_t sz = nmemb * size;
363
364 if (sz > 0)
365 memset (ptr, 0, sz);
366 }
367
368 return ptr;
369 }
370
371 /*
372 INTERNAL_FUNCTION
373 bfd_write_bigendian_4byte_int
374
375 SYNOPSIS
376 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
377
378 DESCRIPTION
379 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
380 endian order regardless of what else is going on. This is useful in
381 archives.
382
383 */
384 bfd_boolean
385 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
386 {
387 bfd_byte buffer[4];
388 bfd_putb32 ((bfd_vma) i, buffer);
389 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
390 }
391
392 \f
393 /** 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@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 . (*(const unsigned char *) (ptr) & 0xff)
437 .#define bfd_get_signed_8(abfd, ptr) \
438 . (((*(const 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_vma) (x) ^ 0x8000) - 0x8000)
556 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
557 #define COERCE64(x) \
558 (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63))
559
560 bfd_vma
561 bfd_getb16 (const void *p)
562 {
563 const bfd_byte *addr = (const bfd_byte *) p;
564 return (addr[0] << 8) | addr[1];
565 }
566
567 bfd_vma
568 bfd_getl16 (const void *p)
569 {
570 const bfd_byte *addr = (const bfd_byte *) p;
571 return (addr[1] << 8) | addr[0];
572 }
573
574 bfd_signed_vma
575 bfd_getb_signed_16 (const void *p)
576 {
577 const bfd_byte *addr = (const bfd_byte *) p;
578 return COERCE16 ((addr[0] << 8) | addr[1]);
579 }
580
581 bfd_signed_vma
582 bfd_getl_signed_16 (const void *p)
583 {
584 const bfd_byte *addr = (const bfd_byte *) p;
585 return COERCE16 ((addr[1] << 8) | addr[0]);
586 }
587
588 void
589 bfd_putb16 (bfd_vma data, void *p)
590 {
591 bfd_byte *addr = (bfd_byte *) p;
592 addr[0] = (data >> 8) & 0xff;
593 addr[1] = data & 0xff;
594 }
595
596 void
597 bfd_putl16 (bfd_vma data, void *p)
598 {
599 bfd_byte *addr = (bfd_byte *) p;
600 addr[0] = data & 0xff;
601 addr[1] = (data >> 8) & 0xff;
602 }
603
604 bfd_vma
605 bfd_getb32 (const void *p)
606 {
607 const bfd_byte *addr = (const bfd_byte *) p;
608 unsigned long v;
609
610 v = (unsigned long) addr[0] << 24;
611 v |= (unsigned long) addr[1] << 16;
612 v |= (unsigned long) addr[2] << 8;
613 v |= (unsigned long) addr[3];
614 return v;
615 }
616
617 bfd_vma
618 bfd_getl32 (const void *p)
619 {
620 const bfd_byte *addr = (const bfd_byte *) p;
621 unsigned long v;
622
623 v = (unsigned long) addr[0];
624 v |= (unsigned long) addr[1] << 8;
625 v |= (unsigned long) addr[2] << 16;
626 v |= (unsigned long) addr[3] << 24;
627 return v;
628 }
629
630 bfd_signed_vma
631 bfd_getb_signed_32 (const void *p)
632 {
633 const bfd_byte *addr = (const bfd_byte *) p;
634 unsigned long v;
635
636 v = (unsigned long) addr[0] << 24;
637 v |= (unsigned long) addr[1] << 16;
638 v |= (unsigned long) addr[2] << 8;
639 v |= (unsigned long) addr[3];
640 return COERCE32 (v);
641 }
642
643 bfd_signed_vma
644 bfd_getl_signed_32 (const void *p)
645 {
646 const bfd_byte *addr = (const bfd_byte *) p;
647 unsigned long v;
648
649 v = (unsigned long) addr[0];
650 v |= (unsigned long) addr[1] << 8;
651 v |= (unsigned long) addr[2] << 16;
652 v |= (unsigned long) addr[3] << 24;
653 return COERCE32 (v);
654 }
655
656 bfd_uint64_t
657 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
658 {
659 #ifdef BFD_HOST_64_BIT
660 const bfd_byte *addr = (const bfd_byte *) p;
661 bfd_uint64_t v;
662
663 v = addr[0]; v <<= 8;
664 v |= addr[1]; v <<= 8;
665 v |= addr[2]; v <<= 8;
666 v |= addr[3]; v <<= 8;
667 v |= addr[4]; v <<= 8;
668 v |= addr[5]; v <<= 8;
669 v |= addr[6]; v <<= 8;
670 v |= addr[7];
671
672 return v;
673 #else
674 BFD_FAIL();
675 return 0;
676 #endif
677 }
678
679 bfd_uint64_t
680 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
681 {
682 #ifdef BFD_HOST_64_BIT
683 const bfd_byte *addr = (const bfd_byte *) p;
684 bfd_uint64_t v;
685
686 v = addr[7]; v <<= 8;
687 v |= addr[6]; v <<= 8;
688 v |= addr[5]; v <<= 8;
689 v |= addr[4]; v <<= 8;
690 v |= addr[3]; v <<= 8;
691 v |= addr[2]; v <<= 8;
692 v |= addr[1]; v <<= 8;
693 v |= addr[0];
694
695 return v;
696 #else
697 BFD_FAIL();
698 return 0;
699 #endif
700
701 }
702
703 bfd_int64_t
704 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
705 {
706 #ifdef BFD_HOST_64_BIT
707 const bfd_byte *addr = (const bfd_byte *) p;
708 bfd_uint64_t v;
709
710 v = addr[0]; v <<= 8;
711 v |= addr[1]; v <<= 8;
712 v |= addr[2]; v <<= 8;
713 v |= addr[3]; v <<= 8;
714 v |= addr[4]; v <<= 8;
715 v |= addr[5]; v <<= 8;
716 v |= addr[6]; v <<= 8;
717 v |= addr[7];
718
719 return COERCE64 (v);
720 #else
721 BFD_FAIL();
722 return 0;
723 #endif
724 }
725
726 bfd_int64_t
727 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
728 {
729 #ifdef BFD_HOST_64_BIT
730 const bfd_byte *addr = (const bfd_byte *) p;
731 bfd_uint64_t v;
732
733 v = addr[7]; v <<= 8;
734 v |= addr[6]; v <<= 8;
735 v |= addr[5]; v <<= 8;
736 v |= addr[4]; v <<= 8;
737 v |= addr[3]; v <<= 8;
738 v |= addr[2]; v <<= 8;
739 v |= addr[1]; v <<= 8;
740 v |= addr[0];
741
742 return COERCE64 (v);
743 #else
744 BFD_FAIL();
745 return 0;
746 #endif
747 }
748
749 void
750 bfd_putb32 (bfd_vma data, void *p)
751 {
752 bfd_byte *addr = (bfd_byte *) p;
753 addr[0] = (data >> 24) & 0xff;
754 addr[1] = (data >> 16) & 0xff;
755 addr[2] = (data >> 8) & 0xff;
756 addr[3] = data & 0xff;
757 }
758
759 void
760 bfd_putl32 (bfd_vma data, void *p)
761 {
762 bfd_byte *addr = (bfd_byte *) p;
763 addr[0] = data & 0xff;
764 addr[1] = (data >> 8) & 0xff;
765 addr[2] = (data >> 16) & 0xff;
766 addr[3] = (data >> 24) & 0xff;
767 }
768
769 void
770 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
771 {
772 #ifdef BFD_HOST_64_BIT
773 bfd_byte *addr = (bfd_byte *) p;
774 addr[0] = (data >> (7*8)) & 0xff;
775 addr[1] = (data >> (6*8)) & 0xff;
776 addr[2] = (data >> (5*8)) & 0xff;
777 addr[3] = (data >> (4*8)) & 0xff;
778 addr[4] = (data >> (3*8)) & 0xff;
779 addr[5] = (data >> (2*8)) & 0xff;
780 addr[6] = (data >> (1*8)) & 0xff;
781 addr[7] = (data >> (0*8)) & 0xff;
782 #else
783 BFD_FAIL();
784 #endif
785 }
786
787 void
788 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
789 {
790 #ifdef BFD_HOST_64_BIT
791 bfd_byte *addr = (bfd_byte *) p;
792 addr[7] = (data >> (7*8)) & 0xff;
793 addr[6] = (data >> (6*8)) & 0xff;
794 addr[5] = (data >> (5*8)) & 0xff;
795 addr[4] = (data >> (4*8)) & 0xff;
796 addr[3] = (data >> (3*8)) & 0xff;
797 addr[2] = (data >> (2*8)) & 0xff;
798 addr[1] = (data >> (1*8)) & 0xff;
799 addr[0] = (data >> (0*8)) & 0xff;
800 #else
801 BFD_FAIL();
802 #endif
803 }
804
805 void
806 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
807 {
808 bfd_byte *addr = (bfd_byte *) p;
809 int i;
810 int bytes;
811
812 if (bits % 8 != 0)
813 abort ();
814
815 bytes = bits / 8;
816 for (i = 0; i < bytes; i++)
817 {
818 int addr_index = big_p ? bytes - i - 1 : i;
819
820 addr[addr_index] = data & 0xff;
821 data >>= 8;
822 }
823 }
824
825 bfd_uint64_t
826 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
827 {
828 const bfd_byte *addr = (const bfd_byte *) p;
829 bfd_uint64_t data;
830 int i;
831 int bytes;
832
833 if (bits % 8 != 0)
834 abort ();
835
836 data = 0;
837 bytes = bits / 8;
838 for (i = 0; i < bytes; i++)
839 {
840 int addr_index = big_p ? i : bytes - i - 1;
841
842 data = (data << 8) | addr[addr_index];
843 }
844
845 return data;
846 }
847 \f
848 /* Default implementation */
849
850 bfd_boolean
851 _bfd_generic_get_section_contents (bfd *abfd,
852 sec_ptr section,
853 void *location,
854 file_ptr offset,
855 bfd_size_type count)
856 {
857 bfd_size_type sz;
858 if (count == 0)
859 return TRUE;
860
861 if (section->compress_status != COMPRESS_SECTION_NONE)
862 {
863 _bfd_error_handler
864 /* xgettext:c-format */
865 (_("%B: unable to get decompressed section %A"),
866 abfd, section);
867 bfd_set_error (bfd_error_invalid_operation);
868 return FALSE;
869 }
870
871 /* We do allow reading of a section after bfd_final_link has
872 written the contents out to disk. In that situation, rawsize is
873 just a stale version of size, so ignore it. Otherwise we must be
874 reading an input section, where rawsize, if different to size,
875 is the on-disk size. */
876 if (abfd->direction != write_direction && section->rawsize != 0)
877 sz = section->rawsize;
878 else
879 sz = section->size;
880 if (offset + count < count
881 || offset + count > sz
882 || (abfd->my_archive != NULL
883 && !bfd_is_thin_archive (abfd->my_archive)
884 && ((ufile_ptr) section->filepos + offset + count
885 > arelt_size (abfd))))
886 {
887 bfd_set_error (bfd_error_invalid_operation);
888 return FALSE;
889 }
890
891 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
892 || bfd_bread (location, count, abfd) != count)
893 return FALSE;
894
895 return TRUE;
896 }
897
898 bfd_boolean
899 _bfd_generic_get_section_contents_in_window
900 (bfd *abfd ATTRIBUTE_UNUSED,
901 sec_ptr section ATTRIBUTE_UNUSED,
902 bfd_window *w ATTRIBUTE_UNUSED,
903 file_ptr offset ATTRIBUTE_UNUSED,
904 bfd_size_type count ATTRIBUTE_UNUSED)
905 {
906 #ifdef USE_MMAP
907 bfd_size_type sz;
908
909 if (count == 0)
910 return TRUE;
911 if (abfd->xvec->_bfd_get_section_contents
912 != _bfd_generic_get_section_contents)
913 {
914 /* We don't know what changes the bfd's get_section_contents
915 method may have to make. So punt trying to map the file
916 window, and let get_section_contents do its thing. */
917 /* @@ FIXME : If the internal window has a refcount of 1 and was
918 allocated with malloc instead of mmap, just reuse it. */
919 bfd_free_window (w);
920 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
921 if (w->i == NULL)
922 return FALSE;
923 w->i->data = bfd_malloc (count);
924 if (w->i->data == NULL)
925 {
926 free (w->i);
927 w->i = NULL;
928 return FALSE;
929 }
930 w->i->mapped = 0;
931 w->i->refcount = 1;
932 w->size = w->i->size = count;
933 w->data = w->i->data;
934 return bfd_get_section_contents (abfd, section, w->data, offset, count);
935 }
936 if (abfd->direction != write_direction && section->rawsize != 0)
937 sz = section->rawsize;
938 else
939 sz = section->size;
940 if (offset + count < count
941 || offset + count > sz
942 || (abfd->my_archive != NULL
943 && !bfd_is_thin_archive (abfd->my_archive)
944 && ((ufile_ptr) section->filepos + offset + count
945 > arelt_size (abfd)))
946 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
947 TRUE))
948 return FALSE;
949 return TRUE;
950 #else
951 abort ();
952 #endif
953 }
954
955 /* This generic function can only be used in implementations where creating
956 NEW sections is disallowed. It is useful in patching existing sections
957 in read-write files, though. See other set_section_contents functions
958 to see why it doesn't work for new sections. */
959 bfd_boolean
960 _bfd_generic_set_section_contents (bfd *abfd,
961 sec_ptr section,
962 const void *location,
963 file_ptr offset,
964 bfd_size_type count)
965 {
966 if (count == 0)
967 return TRUE;
968
969 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
970 || bfd_bwrite (location, count, abfd) != count)
971 return FALSE;
972
973 return TRUE;
974 }
975
976 /*
977 INTERNAL_FUNCTION
978 bfd_log2
979
980 SYNOPSIS
981 unsigned int bfd_log2 (bfd_vma x);
982
983 DESCRIPTION
984 Return the log base 2 of the value supplied, rounded up. E.g., an
985 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
986 */
987
988 unsigned int
989 bfd_log2 (bfd_vma x)
990 {
991 unsigned int result = 0;
992
993 if (x <= 1)
994 return result;
995 --x;
996 do
997 ++result;
998 while ((x >>= 1) != 0);
999 return result;
1000 }
1001
1002 bfd_boolean
1003 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
1004 {
1005 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1006
1007 return name[0] == locals_prefix;
1008 }
1009
1010 /* Give a warning at runtime if someone compiles code which calls
1011 old routines. */
1012
1013 void
1014 _bfd_warn_deprecated (const char *what,
1015 const char *file,
1016 int line,
1017 const char *func)
1018 {
1019 /* Poor man's tracking of functions we've already warned about. */
1020 static size_t mask = 0;
1021
1022 if (~(size_t) func & ~mask)
1023 {
1024 fflush (stdout);
1025 /* Note: separate sentences in order to allow
1026 for translation into other languages. */
1027 if (func)
1028 /* xgettext:c-format */
1029 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
1030 what, file, line, func);
1031 else
1032 fprintf (stderr, _("Deprecated %s called\n"), what);
1033 fflush (stderr);
1034 mask |= ~(size_t) func;
1035 }
1036 }
1037
1038 /* Helper function for reading uleb128 encoded data. */
1039
1040 bfd_vma
1041 _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1042 bfd_byte *buf,
1043 unsigned int *bytes_read_ptr)
1044 {
1045 bfd_vma result;
1046 unsigned int num_read;
1047 unsigned int shift;
1048 unsigned char byte;
1049
1050 result = 0;
1051 shift = 0;
1052 num_read = 0;
1053 do
1054 {
1055 byte = bfd_get_8 (abfd, buf);
1056 buf++;
1057 num_read++;
1058 result |= (((bfd_vma) byte & 0x7f) << shift);
1059 shift += 7;
1060 }
1061 while (byte & 0x80);
1062 *bytes_read_ptr = num_read;
1063 return result;
1064 }
1065
1066 /* Read in a LEB128 encoded value from ABFD starting at DATA.
1067 If SIGN is true, return a signed LEB128 value.
1068 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
1069 No bytes will be read at address END or beyond. */
1070
1071 bfd_vma
1072 _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1073 bfd_byte *data,
1074 unsigned int *length_return,
1075 bfd_boolean sign,
1076 const bfd_byte * const end)
1077 {
1078 bfd_vma result = 0;
1079 unsigned int num_read = 0;
1080 unsigned int shift = 0;
1081 unsigned char byte = 0;
1082
1083 while (data < end)
1084 {
1085 byte = bfd_get_8 (abfd, data);
1086 data++;
1087 num_read++;
1088
1089 result |= ((bfd_vma) (byte & 0x7f)) << shift;
1090
1091 shift += 7;
1092 if ((byte & 0x80) == 0)
1093 break;
1094 }
1095
1096 if (length_return != NULL)
1097 *length_return = num_read;
1098
1099 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
1100 result |= -((bfd_vma) 1 << shift);
1101
1102 return result;
1103 }
1104
1105 /* Helper function for reading sleb128 encoded data. */
1106
1107 bfd_signed_vma
1108 _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1109 bfd_byte *buf,
1110 unsigned int *bytes_read_ptr)
1111 {
1112 bfd_vma result;
1113 unsigned int shift;
1114 unsigned int num_read;
1115 unsigned char byte;
1116
1117 result = 0;
1118 shift = 0;
1119 num_read = 0;
1120 do
1121 {
1122 byte = bfd_get_8 (abfd, buf);
1123 buf ++;
1124 num_read ++;
1125 result |= (((bfd_vma) byte & 0x7f) << shift);
1126 shift += 7;
1127 }
1128 while (byte & 0x80);
1129 if (shift < 8 * sizeof (result) && (byte & 0x40))
1130 result |= (((bfd_vma) -1) << shift);
1131 *bytes_read_ptr = num_read;
1132 return result;
1133 }
1134
1135 bfd_boolean
1136 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1137 asection *isec ATTRIBUTE_UNUSED,
1138 bfd *obfd ATTRIBUTE_UNUSED,
1139 asection *osec ATTRIBUTE_UNUSED,
1140 struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1141 {
1142 return TRUE;
1143 }
This page took 0.055812 seconds and 5 git commands to generate.