daily update
[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,
edeb6e24 3 2000, 2001, 2002, 2003, 2004
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
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
33 Internal functions
34
35DESCRIPTION
36 These routines are used within BFD.
37 They are not intended for export, but are documented here for
38 completeness.
39*/
40
41/* A routine which is used in target vectors for unsupported
42 operations. */
43
b34976b6 44bfd_boolean
c58b9523 45bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
252b5132
RH
46{
47 bfd_set_error (bfd_error_invalid_operation);
b34976b6 48 return FALSE;
252b5132
RH
49}
50
51/* A routine which is used in target vectors for supported operations
52 which do not actually do anything. */
53
b34976b6 54bfd_boolean
c58b9523 55bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
252b5132 56{
b34976b6 57 return TRUE;
252b5132
RH
58}
59
60/* A routine which is used in target vectors for unsupported
61 operations which return a pointer value. */
62
c58b9523
AM
63void *
64bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
252b5132
RH
65{
66 bfd_set_error (bfd_error_invalid_operation);
67 return NULL;
68}
69
509945ae 70int
c58b9523 71bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
252b5132
RH
72{
73 return 0;
74}
75
509945ae 76unsigned int
c58b9523 77bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
252b5132
RH
78{
79 return 0;
80}
81
252b5132 82long
c58b9523 83bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
252b5132
RH
84{
85 return 0;
86}
87
88/* A routine which is used in target vectors for unsupported
89 operations which return -1 on error. */
90
252b5132 91long
c58b9523 92_bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
252b5132
RH
93{
94 bfd_set_error (bfd_error_invalid_operation);
95 return -1;
96}
97
509945ae 98void
c58b9523 99bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
252b5132
RH
100{
101}
102
b34976b6 103bfd_boolean
c58b9523
AM
104_bfd_nocore_core_file_matches_executable_p
105 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
106 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
252b5132
RH
107{
108 bfd_set_error (bfd_error_invalid_operation);
b34976b6 109 return FALSE;
252b5132
RH
110}
111
112/* Routine to handle core_file_failing_command entry point for targets
113 without core file support. */
114
252b5132 115char *
c58b9523 116_bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
252b5132
RH
117{
118 bfd_set_error (bfd_error_invalid_operation);
c58b9523 119 return NULL;
252b5132
RH
120}
121
122/* Routine to handle core_file_failing_signal entry point for targets
123 without core file support. */
124
252b5132 125int
c58b9523 126_bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
252b5132
RH
127{
128 bfd_set_error (bfd_error_invalid_operation);
129 return 0;
130}
131
252b5132 132const bfd_target *
c58b9523 133_bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
252b5132
RH
134{
135 bfd_set_error (bfd_error_wrong_format);
136 return 0;
137}
138\f
139/* Allocate memory using malloc. */
140
c58b9523
AM
141void *
142bfd_malloc (bfd_size_type size)
252b5132 143{
c58b9523 144 void *ptr;
252b5132 145
dc810e39
AM
146 if (size != (size_t) size)
147 {
148 bfd_set_error (bfd_error_no_memory);
149 return NULL;
150 }
151
c58b9523 152 ptr = malloc ((size_t) size);
dc810e39 153 if (ptr == NULL && (size_t) size != 0)
252b5132 154 bfd_set_error (bfd_error_no_memory);
dc810e39 155
252b5132
RH
156 return ptr;
157}
158
159/* Reallocate memory using realloc. */
160
c58b9523
AM
161void *
162bfd_realloc (void *ptr, bfd_size_type size)
252b5132 163{
c58b9523 164 void *ret;
252b5132 165
dc810e39
AM
166 if (size != (size_t) size)
167 {
168 bfd_set_error (bfd_error_no_memory);
169 return NULL;
170 }
171
252b5132 172 if (ptr == NULL)
c58b9523 173 ret = malloc ((size_t) size);
252b5132 174 else
c58b9523 175 ret = realloc (ptr, (size_t) size);
252b5132 176
dc810e39 177 if (ret == NULL && (size_t) size != 0)
252b5132
RH
178 bfd_set_error (bfd_error_no_memory);
179
180 return ret;
181}
182
183/* Allocate memory using malloc and clear it. */
184
c58b9523
AM
185void *
186bfd_zmalloc (bfd_size_type size)
252b5132 187{
c58b9523 188 void *ptr;
252b5132 189
dc810e39
AM
190 if (size != (size_t) size)
191 {
192 bfd_set_error (bfd_error_no_memory);
193 return NULL;
194 }
252b5132 195
c58b9523 196 ptr = malloc ((size_t) size);
dc810e39
AM
197
198 if ((size_t) size != 0)
252b5132
RH
199 {
200 if (ptr == NULL)
201 bfd_set_error (bfd_error_no_memory);
202 else
dc810e39 203 memset (ptr, 0, (size_t) size);
252b5132
RH
204 }
205
206 return ptr;
207}
252b5132
RH
208/*
209INTERNAL_FUNCTION
210 bfd_write_bigendian_4byte_int
211
212SYNOPSIS
b34976b6 213 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
252b5132
RH
214
215DESCRIPTION
216 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
217 endian order regardless of what else is going on. This is useful in
218 archives.
219
220*/
b34976b6 221bfd_boolean
c58b9523 222bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
252b5132
RH
223{
224 bfd_byte buffer[4];
dc810e39 225 bfd_putb32 ((bfd_vma) i, buffer);
c58b9523 226 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
252b5132
RH
227}
228
252b5132
RH
229\f
230/** The do-it-yourself (byte) sex-change kit */
231
232/* The middle letter e.g. get<b>short indicates Big or Little endian
233 target machine. It doesn't matter what the byte order of the host
234 machine is; these routines work for either. */
235
236/* FIXME: Should these take a count argument?
237 Answer (gnu@cygnus.com): No, but perhaps they should be inline
509945ae 238 functions in swap.h #ifdef __GNUC__.
252b5132
RH
239 Gprof them later and find out. */
240
241/*
242FUNCTION
243 bfd_put_size
244FUNCTION
245 bfd_get_size
246
247DESCRIPTION
248 These macros as used for reading and writing raw data in
249 sections; each access (except for bytes) is vectored through
250 the target format of the BFD and mangled accordingly. The
251 mangling performs any necessary endian translations and
252 removes alignment restrictions. Note that types accepted and
253 returned by these macros are identical so they can be swapped
254 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
255 to either <<bfd_get_32>> or <<bfd_get_64>>.
256
257 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
258 system without prototypes, the caller is responsible for making
259 sure that is true, with a cast if necessary. We don't cast
260 them in the macro definitions because that would prevent <<lint>>
261 or <<gcc -Wall>> from detecting sins such as passing a pointer.
262 To detect calling these with less than a <<bfd_vma>>, use
263 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
264
265.
266.{* Byte swapping macros for user section data. *}
267.
268.#define bfd_put_8(abfd, val, ptr) \
edeb6e24 269. ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
252b5132 270.#define bfd_put_signed_8 \
c58b9523 271. bfd_put_8
252b5132 272.#define bfd_get_8(abfd, ptr) \
c58b9523 273. (*(unsigned char *) (ptr) & 0xff)
252b5132 274.#define bfd_get_signed_8(abfd, ptr) \
c58b9523 275. (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
252b5132
RH
276.
277.#define bfd_put_16(abfd, val, ptr) \
c58b9523 278. BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
252b5132 279.#define bfd_put_signed_16 \
c58b9523 280. bfd_put_16
252b5132 281.#define bfd_get_16(abfd, ptr) \
c58b9523 282. BFD_SEND (abfd, bfd_getx16, (ptr))
252b5132 283.#define bfd_get_signed_16(abfd, ptr) \
c58b9523 284. BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
252b5132
RH
285.
286.#define bfd_put_32(abfd, val, ptr) \
c58b9523 287. BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
252b5132 288.#define bfd_put_signed_32 \
c58b9523 289. bfd_put_32
252b5132 290.#define bfd_get_32(abfd, ptr) \
c58b9523 291. BFD_SEND (abfd, bfd_getx32, (ptr))
252b5132 292.#define bfd_get_signed_32(abfd, ptr) \
c58b9523 293. BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
252b5132
RH
294.
295.#define bfd_put_64(abfd, val, ptr) \
c58b9523 296. BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
252b5132 297.#define bfd_put_signed_64 \
c58b9523 298. bfd_put_64
252b5132 299.#define bfd_get_64(abfd, ptr) \
c58b9523 300. BFD_SEND (abfd, bfd_getx64, (ptr))
252b5132 301.#define bfd_get_signed_64(abfd, ptr) \
c58b9523 302. BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
252b5132 303.
c58b9523
AM
304.#define bfd_get(bits, abfd, ptr) \
305. ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
306. : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
307. : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
308. : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
309. : (abort (), (bfd_vma) - 1))
c7ac6ff8 310.
c58b9523
AM
311.#define bfd_put(bits, abfd, val, ptr) \
312. ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
313. : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
314. : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
315. : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
316. : (abort (), (void) 0))
c7ac6ff8 317.
509945ae 318*/
252b5132
RH
319
320/*
321FUNCTION
322 bfd_h_put_size
323 bfd_h_get_size
324
325DESCRIPTION
326 These macros have the same function as their <<bfd_get_x>>
dc810e39 327 brethren, except that they are used for removing information
252b5132
RH
328 for the header records of object files. Believe it or not,
329 some object files keep their header records in big endian
330 order and their data in little endian order.
331.
332.{* Byte swapping macros for file header data. *}
333.
334.#define bfd_h_put_8(abfd, val, ptr) \
dc810e39 335. bfd_put_8 (abfd, val, ptr)
252b5132 336.#define bfd_h_put_signed_8(abfd, val, ptr) \
dc810e39 337. bfd_put_8 (abfd, val, ptr)
252b5132 338.#define bfd_h_get_8(abfd, ptr) \
dc810e39 339. bfd_get_8 (abfd, ptr)
252b5132 340.#define bfd_h_get_signed_8(abfd, ptr) \
dc810e39 341. bfd_get_signed_8 (abfd, ptr)
252b5132
RH
342.
343.#define bfd_h_put_16(abfd, val, ptr) \
dc810e39 344. BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
252b5132 345.#define bfd_h_put_signed_16 \
dc810e39 346. bfd_h_put_16
252b5132 347.#define bfd_h_get_16(abfd, ptr) \
dc810e39 348. BFD_SEND (abfd, bfd_h_getx16, (ptr))
252b5132 349.#define bfd_h_get_signed_16(abfd, ptr) \
dc810e39 350. BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
252b5132
RH
351.
352.#define bfd_h_put_32(abfd, val, ptr) \
dc810e39 353. BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
252b5132 354.#define bfd_h_put_signed_32 \
dc810e39 355. bfd_h_put_32
252b5132 356.#define bfd_h_get_32(abfd, ptr) \
dc810e39 357. BFD_SEND (abfd, bfd_h_getx32, (ptr))
252b5132 358.#define bfd_h_get_signed_32(abfd, ptr) \
dc810e39 359. BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
252b5132
RH
360.
361.#define bfd_h_put_64(abfd, val, ptr) \
dc810e39 362. BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
252b5132 363.#define bfd_h_put_signed_64 \
dc810e39 364. bfd_h_put_64
252b5132 365.#define bfd_h_get_64(abfd, ptr) \
dc810e39 366. BFD_SEND (abfd, bfd_h_getx64, (ptr))
252b5132 367.#define bfd_h_get_signed_64(abfd, ptr) \
dc810e39 368. BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
252b5132 369.
edeb6e24 370.{* Aliases for the above, which should eventually go away. *}
dc810e39 371.
edeb6e24
AM
372.#define H_PUT_64 bfd_h_put_64
373.#define H_PUT_32 bfd_h_put_32
374.#define H_PUT_16 bfd_h_put_16
375.#define H_PUT_8 bfd_h_put_8
376.#define H_PUT_S64 bfd_h_put_signed_64
377.#define H_PUT_S32 bfd_h_put_signed_32
378.#define H_PUT_S16 bfd_h_put_signed_16
379.#define H_PUT_S8 bfd_h_put_signed_8
380.#define H_GET_64 bfd_h_get_64
381.#define H_GET_32 bfd_h_get_32
382.#define H_GET_16 bfd_h_get_16
383.#define H_GET_8 bfd_h_get_8
384.#define H_GET_S64 bfd_h_get_signed_64
385.#define H_GET_S32 bfd_h_get_signed_32
386.#define H_GET_S16 bfd_h_get_signed_16
387.#define H_GET_S8 bfd_h_get_signed_8
dc810e39
AM
388.
389.*/
252b5132
RH
390
391/* Sign extension to bfd_signed_vma. */
392#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
c58b9523 393#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
8ce8c090 394#define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
252b5132 395#define COERCE64(x) \
8ce8c090 396 (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
252b5132
RH
397
398bfd_vma
edeb6e24 399bfd_getb16 (const void *p)
252b5132 400{
edeb6e24 401 const bfd_byte *addr = p;
252b5132
RH
402 return (addr[0] << 8) | addr[1];
403}
404
405bfd_vma
edeb6e24 406bfd_getl16 (const void *p)
252b5132 407{
edeb6e24 408 const bfd_byte *addr = p;
252b5132
RH
409 return (addr[1] << 8) | addr[0];
410}
411
412bfd_signed_vma
edeb6e24 413bfd_getb_signed_16 (const void *p)
252b5132 414{
edeb6e24 415 const bfd_byte *addr = p;
c58b9523 416 return COERCE16 ((addr[0] << 8) | addr[1]);
252b5132
RH
417}
418
419bfd_signed_vma
edeb6e24 420bfd_getl_signed_16 (const void *p)
252b5132 421{
edeb6e24 422 const bfd_byte *addr = p;
c58b9523 423 return COERCE16 ((addr[1] << 8) | addr[0]);
252b5132
RH
424}
425
426void
edeb6e24 427bfd_putb16 (bfd_vma data, void *p)
252b5132 428{
edeb6e24
AM
429 bfd_byte *addr = p;
430 addr[0] = (data >> 8) & 0xff;
431 addr[1] = data & 0xff;
252b5132
RH
432}
433
434void
edeb6e24 435bfd_putl16 (bfd_vma data, void *p)
252b5132 436{
edeb6e24
AM
437 bfd_byte *addr = p;
438 addr[0] = data & 0xff;
439 addr[1] = (data >> 8) & 0xff;
252b5132
RH
440}
441
442bfd_vma
edeb6e24 443bfd_getb32 (const void *p)
252b5132 444{
edeb6e24 445 const bfd_byte *addr = p;
252b5132
RH
446 unsigned long v;
447
448 v = (unsigned long) addr[0] << 24;
449 v |= (unsigned long) addr[1] << 16;
450 v |= (unsigned long) addr[2] << 8;
451 v |= (unsigned long) addr[3];
c58b9523 452 return v;
252b5132
RH
453}
454
455bfd_vma
edeb6e24 456bfd_getl32 (const void *p)
252b5132 457{
edeb6e24 458 const bfd_byte *addr = p;
252b5132
RH
459 unsigned long v;
460
461 v = (unsigned long) addr[0];
462 v |= (unsigned long) addr[1] << 8;
463 v |= (unsigned long) addr[2] << 16;
464 v |= (unsigned long) addr[3] << 24;
c58b9523 465 return v;
252b5132
RH
466}
467
468bfd_signed_vma
edeb6e24 469bfd_getb_signed_32 (const void *p)
252b5132 470{
edeb6e24 471 const bfd_byte *addr = p;
252b5132
RH
472 unsigned long v;
473
474 v = (unsigned long) addr[0] << 24;
475 v |= (unsigned long) addr[1] << 16;
476 v |= (unsigned long) addr[2] << 8;
477 v |= (unsigned long) addr[3];
478 return COERCE32 (v);
479}
480
481bfd_signed_vma
edeb6e24 482bfd_getl_signed_32 (const void *p)
252b5132 483{
edeb6e24 484 const bfd_byte *addr = p;
252b5132
RH
485 unsigned long v;
486
487 v = (unsigned long) addr[0];
488 v |= (unsigned long) addr[1] << 8;
489 v |= (unsigned long) addr[2] << 16;
490 v |= (unsigned long) addr[3] << 24;
491 return COERCE32 (v);
492}
493
8ce8c090 494bfd_uint64_t
edeb6e24 495bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
252b5132 496{
8ce8c090 497#ifdef BFD_HOST_64_BIT
edeb6e24 498 const bfd_byte *addr = p;
8ce8c090 499 bfd_uint64_t v;
c58b9523
AM
500
501 v = addr[0]; v <<= 8;
502 v |= addr[1]; v <<= 8;
503 v |= addr[2]; v <<= 8;
504 v |= addr[3]; v <<= 8;
505 v |= addr[4]; v <<= 8;
506 v |= addr[5]; v <<= 8;
507 v |= addr[6]; v <<= 8;
508 v |= addr[7];
509
510 return v;
252b5132
RH
511#else
512 BFD_FAIL();
513 return 0;
514#endif
515}
516
8ce8c090 517bfd_uint64_t
edeb6e24 518bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
252b5132 519{
8ce8c090 520#ifdef BFD_HOST_64_BIT
edeb6e24 521 const bfd_byte *addr = p;
8ce8c090 522 bfd_uint64_t v;
c58b9523
AM
523
524 v = addr[7]; v <<= 8;
525 v |= addr[6]; v <<= 8;
526 v |= addr[5]; v <<= 8;
527 v |= addr[4]; v <<= 8;
528 v |= addr[3]; v <<= 8;
529 v |= addr[2]; v <<= 8;
530 v |= addr[1]; v <<= 8;
531 v |= addr[0];
532
533 return v;
252b5132
RH
534#else
535 BFD_FAIL();
536 return 0;
537#endif
538
539}
540
8ce8c090 541bfd_int64_t
edeb6e24 542bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
252b5132 543{
8ce8c090 544#ifdef BFD_HOST_64_BIT
edeb6e24 545 const bfd_byte *addr = p;
8ce8c090 546 bfd_uint64_t v;
c58b9523
AM
547
548 v = addr[0]; v <<= 8;
549 v |= addr[1]; v <<= 8;
550 v |= addr[2]; v <<= 8;
551 v |= addr[3]; v <<= 8;
552 v |= addr[4]; v <<= 8;
553 v |= addr[5]; v <<= 8;
554 v |= addr[6]; v <<= 8;
555 v |= addr[7];
556
557 return COERCE64 (v);
252b5132
RH
558#else
559 BFD_FAIL();
560 return 0;
561#endif
562}
563
8ce8c090 564bfd_int64_t
edeb6e24 565bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
252b5132 566{
8ce8c090 567#ifdef BFD_HOST_64_BIT
edeb6e24 568 const bfd_byte *addr = p;
8ce8c090 569 bfd_uint64_t v;
c58b9523
AM
570
571 v = addr[7]; v <<= 8;
572 v |= addr[6]; v <<= 8;
573 v |= addr[5]; v <<= 8;
574 v |= addr[4]; v <<= 8;
575 v |= addr[3]; v <<= 8;
576 v |= addr[2]; v <<= 8;
577 v |= addr[1]; v <<= 8;
578 v |= addr[0];
579
580 return COERCE64 (v);
252b5132
RH
581#else
582 BFD_FAIL();
583 return 0;
584#endif
585}
586
587void
edeb6e24 588bfd_putb32 (bfd_vma data, void *p)
252b5132 589{
edeb6e24
AM
590 bfd_byte *addr = p;
591 addr[0] = (data >> 24) & 0xff;
592 addr[1] = (data >> 16) & 0xff;
593 addr[2] = (data >> 8) & 0xff;
594 addr[3] = data & 0xff;
252b5132
RH
595}
596
597void
edeb6e24 598bfd_putl32 (bfd_vma data, void *p)
252b5132 599{
edeb6e24
AM
600 bfd_byte *addr = p;
601 addr[0] = data & 0xff;
602 addr[1] = (data >> 8) & 0xff;
603 addr[2] = (data >> 16) & 0xff;
604 addr[3] = (data >> 24) & 0xff;
252b5132
RH
605}
606
607void
8ce8c090 608bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
252b5132 609{
8ce8c090 610#ifdef BFD_HOST_64_BIT
edeb6e24
AM
611 bfd_byte *addr = p;
612 addr[0] = (data >> (7*8)) & 0xff;
613 addr[1] = (data >> (6*8)) & 0xff;
614 addr[2] = (data >> (5*8)) & 0xff;
615 addr[3] = (data >> (4*8)) & 0xff;
616 addr[4] = (data >> (3*8)) & 0xff;
617 addr[5] = (data >> (2*8)) & 0xff;
618 addr[6] = (data >> (1*8)) & 0xff;
619 addr[7] = (data >> (0*8)) & 0xff;
252b5132
RH
620#else
621 BFD_FAIL();
622#endif
623}
624
625void
8ce8c090 626bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
252b5132 627{
8ce8c090 628#ifdef BFD_HOST_64_BIT
edeb6e24
AM
629 bfd_byte *addr = p;
630 addr[7] = (data >> (7*8)) & 0xff;
631 addr[6] = (data >> (6*8)) & 0xff;
632 addr[5] = (data >> (5*8)) & 0xff;
633 addr[4] = (data >> (4*8)) & 0xff;
634 addr[3] = (data >> (3*8)) & 0xff;
635 addr[2] = (data >> (2*8)) & 0xff;
636 addr[1] = (data >> (1*8)) & 0xff;
637 addr[0] = (data >> (0*8)) & 0xff;
252b5132
RH
638#else
639 BFD_FAIL();
640#endif
641}
8c603c85
NC
642
643void
8ce8c090 644bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
8c603c85 645{
edeb6e24 646 bfd_byte *addr = p;
8c603c85
NC
647 int i;
648 int bytes;
649
650 if (bits % 8 != 0)
651 abort ();
652
653 bytes = bits / 8;
654 for (i = 0; i < bytes; i++)
655 {
656 int index = big_p ? bytes - i - 1 : i;
657
edeb6e24 658 addr[index] = data & 0xff;
8c603c85
NC
659 data >>= 8;
660 }
661}
662
8ce8c090 663bfd_uint64_t
edeb6e24 664bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
8c603c85 665{
edeb6e24 666 const bfd_byte *addr = p;
8ce8c090 667 bfd_uint64_t data;
8c603c85
NC
668 int i;
669 int bytes;
670
671 if (bits % 8 != 0)
672 abort ();
673
674 data = 0;
675 bytes = bits / 8;
676 for (i = 0; i < bytes; i++)
677 {
678 int index = big_p ? i : bytes - i - 1;
509945ae 679
8c603c85
NC
680 data = (data << 8) | addr[index];
681 }
682
683 return data;
684}
252b5132
RH
685\f
686/* Default implementation */
687
b34976b6 688bfd_boolean
c58b9523
AM
689_bfd_generic_get_section_contents (bfd *abfd,
690 sec_ptr section,
691 void *location,
692 file_ptr offset,
693 bfd_size_type count)
252b5132 694{
0bff3f4b 695 if (count == 0)
b34976b6 696 return TRUE;
0bff3f4b 697
dc810e39 698 if (offset + count > section->_raw_size)
0bff3f4b
ILT
699 {
700 bfd_set_error (bfd_error_invalid_operation);
b34976b6 701 return FALSE;
0bff3f4b
ILT
702 }
703
704 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
dc810e39 705 || bfd_bread (location, count, abfd) != count)
b34976b6 706 return FALSE;
0bff3f4b 707
b34976b6 708 return TRUE;
252b5132
RH
709}
710
b34976b6 711bfd_boolean
c58b9523
AM
712_bfd_generic_get_section_contents_in_window
713 (bfd *abfd ATTRIBUTE_UNUSED,
714 sec_ptr section ATTRIBUTE_UNUSED,
715 bfd_window *w ATTRIBUTE_UNUSED,
716 file_ptr offset ATTRIBUTE_UNUSED,
717 bfd_size_type count ATTRIBUTE_UNUSED)
252b5132
RH
718{
719#ifdef USE_MMAP
720 if (count == 0)
b34976b6 721 return TRUE;
c58b9523
AM
722 if (abfd->xvec->_bfd_get_section_contents
723 != _bfd_generic_get_section_contents)
252b5132
RH
724 {
725 /* We don't know what changes the bfd's get_section_contents
726 method may have to make. So punt trying to map the file
727 window, and let get_section_contents do its thing. */
728 /* @@ FIXME : If the internal window has a refcount of 1 and was
729 allocated with malloc instead of mmap, just reuse it. */
730 bfd_free_window (w);
c58b9523 731 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
252b5132 732 if (w->i == NULL)
b34976b6 733 return FALSE;
c58b9523 734 w->i->data = bfd_malloc (count);
252b5132
RH
735 if (w->i->data == NULL)
736 {
737 free (w->i);
738 w->i = NULL;
b34976b6 739 return FALSE;
252b5132
RH
740 }
741 w->i->mapped = 0;
742 w->i->refcount = 1;
743 w->size = w->i->size = count;
744 w->data = w->i->data;
745 return bfd_get_section_contents (abfd, section, w->data, offset, count);
746 }
dc810e39 747 if (offset + count > section->_raw_size
82e51918 748 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
b34976b6
AM
749 TRUE))
750 return FALSE;
751 return TRUE;
252b5132
RH
752#else
753 abort ();
754#endif
755}
756
757/* This generic function can only be used in implementations where creating
758 NEW sections is disallowed. It is useful in patching existing sections
759 in read-write files, though. See other set_section_contents functions
760 to see why it doesn't work for new sections. */
b34976b6 761bfd_boolean
c58b9523
AM
762_bfd_generic_set_section_contents (bfd *abfd,
763 sec_ptr section,
0f867abe 764 const void *location,
c58b9523
AM
765 file_ptr offset,
766 bfd_size_type count)
252b5132
RH
767{
768 if (count == 0)
b34976b6 769 return TRUE;
252b5132 770
dc810e39
AM
771 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
772 || bfd_bwrite (location, count, abfd) != count)
b34976b6 773 return FALSE;
252b5132 774
b34976b6 775 return TRUE;
252b5132
RH
776}
777
778/*
779INTERNAL_FUNCTION
780 bfd_log2
781
782SYNOPSIS
dc810e39 783 unsigned int bfd_log2 (bfd_vma x);
252b5132
RH
784
785DESCRIPTION
786 Return the log base 2 of the value supplied, rounded up. E.g., an
dc810e39 787 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
252b5132
RH
788*/
789
790unsigned int
c58b9523 791bfd_log2 (bfd_vma x)
252b5132
RH
792{
793 unsigned int result = 0;
794
86b21447 795 while ((x = (x >> 1)) != 0)
252b5132
RH
796 ++result;
797 return result;
798}
799
b34976b6 800bfd_boolean
c58b9523 801bfd_generic_is_local_label_name (bfd *abfd, const char *name)
252b5132
RH
802{
803 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
804
b34976b6 805 return name[0] == locals_prefix;
252b5132
RH
806}
807
875f7f69
JR
808/* Can be used from / for bfd_merge_private_bfd_data to check that
809 endianness matches between input and output file. Returns
b34976b6
AM
810 TRUE for a match, otherwise returns FALSE and emits an error. */
811bfd_boolean
c58b9523 812_bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
875f7f69
JR
813{
814 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1fe494a5 815 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
875f7f69
JR
816 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
817 {
1fe494a5
NC
818 const char *msg;
819
820 if (bfd_big_endian (ibfd))
821 msg = _("%s: compiled for a big endian system and target is little endian");
822 else
823 msg = _("%s: compiled for a little endian system and target is big endian");
824
8f615d07 825 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
875f7f69
JR
826
827 bfd_set_error (bfd_error_wrong_format);
b34976b6 828 return FALSE;
875f7f69
JR
829 }
830
b34976b6 831 return TRUE;
875f7f69 832}
dc810e39
AM
833
834/* Give a warning at runtime if someone compiles code which calls
835 old routines. */
ca09e32b 836
dc810e39 837void
c58b9523
AM
838warn_deprecated (const char *what,
839 const char *file,
840 int line,
841 const char *func)
dc810e39
AM
842{
843 /* Poor man's tracking of functions we've already warned about. */
844 static size_t mask = 0;
845
846 if (~(size_t) func & ~mask)
847 {
73722af0 848 /* Note: separate sentences in order to allow
ca09e32b 849 for translation into other languages. */
dc810e39 850 if (func)
ca09e32b
NC
851 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
852 what, file, line, func);
dc810e39 853 else
ca09e32b 854 fprintf (stderr, _("Deprecated %s called\n"), what);
dc810e39
AM
855 mask |= ~(size_t) func;
856 }
857}
This page took 0.314682 seconds and 4 git commands to generate.