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