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