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