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