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