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