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