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