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