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