* xcofflink.c (xcoff_find_reloc): Handle the case of a single
[deliverable/binutils-gdb.git] / bfd / libbfd.c
CommitLineData
0d552306 1/* Assorted BFD support routines, only used internally.
f4bd7a8f 2 Copyright 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
f58809fd 3 Written by Cygnus Support.
4a81b561 4
f58809fd 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
f58809fd 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
f58809fd
SC
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
f58809fd 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
f58809fd 18along with this program; if not, write to the Free Software
4fe6d901 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
4a81b561 20
4a81b561 21#include "bfd.h"
f58809fd 22#include "sysdep.h"
4a81b561
DHW
23#include "libbfd.h"
24
4fe6d901
KR
25static int real_read PARAMS ((PTR, size_t, size_t, FILE *));
26
f8e01940
JG
27/*
28SECTION
17ecba1a 29 Internal functions
f8e01940
JG
30
31DESCRIPTION
c188b0be 32 These routines are used within BFD.
f8e01940
JG
33 They are not intended for export, but are documented here for
34 completeness.
35*/
4a81b561 36
6812b607
ILT
37/* A routine which is used in target vectors for unsupported
38 operations. */
4a81b561 39
728472f1 40/*ARGSUSED*/
4a81b561 41boolean
f4bd7a8f
DM
42bfd_false (ignore)
43 bfd *ignore;
4a81b561 44{
6812b607 45 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
46 return false;
47}
48
6812b607
ILT
49/* A routine which is used in target vectors for supported operations
50 which do not actually do anything. */
51
728472f1 52/*ARGSUSED*/
4a81b561 53boolean
f4bd7a8f
DM
54bfd_true (ignore)
55 bfd *ignore;
4a81b561
DHW
56{
57 return true;
58}
59
6812b607
ILT
60/* A routine which is used in target vectors for unsupported
61 operations which return a pointer value. */
62
728472f1 63/*ARGSUSED*/
d0ec7a8e 64PTR
f4bd7a8f
DM
65bfd_nullvoidptr (ignore)
66 bfd *ignore;
4a81b561 67{
6812b607
ILT
68 bfd_set_error (bfd_error_invalid_operation);
69 return NULL;
4a81b561 70}
fc723380 71
728472f1 72/*ARGSUSED*/
4a81b561 73int
f4bd7a8f
DM
74bfd_0 (ignore)
75 bfd *ignore;
4a81b561
DHW
76{
77 return 0;
78}
fc723380 79
728472f1 80/*ARGSUSED*/
4a81b561 81unsigned int
f4bd7a8f
DM
82bfd_0u (ignore)
83 bfd *ignore;
4a81b561
DHW
84{
85 return 0;
86}
87
326e32d7
ILT
88/*ARGUSED*/
89long
90bfd_0l (ignore)
91 bfd *ignore;
92{
93 return 0;
94}
95
6812b607
ILT
96/* A routine which is used in target vectors for unsupported
97 operations which return -1 on error. */
98
99/*ARGSUSED*/
100long
101_bfd_n1 (ignore_abfd)
102 bfd *ignore_abfd;
103{
104 bfd_set_error (bfd_error_invalid_operation);
105 return -1;
106}
107
728472f1 108/*ARGSUSED*/
4a81b561 109void
f4bd7a8f
DM
110bfd_void (ignore)
111 bfd *ignore;
4a81b561
DHW
112{
113}
114
728472f1 115/*ARGSUSED*/
4a81b561 116boolean
6812b607 117_bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
f4bd7a8f
DM
118 bfd *ignore_core_bfd;
119 bfd *ignore_exec_bfd;
4a81b561 120{
120f5bd9 121 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
122 return false;
123}
124
6812b607
ILT
125/* Routine to handle core_file_failing_command entry point for targets
126 without core file support. */
4a81b561 127
728472f1 128/*ARGSUSED*/
4a81b561 129char *
6812b607 130_bfd_nocore_core_file_failing_command (ignore_abfd)
f4bd7a8f 131 bfd *ignore_abfd;
4a81b561 132{
6812b607 133 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
134 return (char *)NULL;
135}
136
6812b607
ILT
137/* Routine to handle core_file_failing_signal entry point for targets
138 without core file support. */
139
728472f1 140/*ARGSUSED*/
4a81b561 141int
6812b607 142_bfd_nocore_core_file_failing_signal (ignore_abfd)
f4bd7a8f 143 bfd *ignore_abfd;
4a81b561 144{
6812b607 145 bfd_set_error (bfd_error_invalid_operation);
4a81b561
DHW
146 return 0;
147}
148
728472f1 149/*ARGSUSED*/
4fe6d901 150const bfd_target *
f4bd7a8f
DM
151_bfd_dummy_target (ignore_abfd)
152 bfd *ignore_abfd;
4a81b561 153{
4fe6d901 154 bfd_set_error (bfd_error_wrong_format);
4a81b561
DHW
155 return 0;
156}
157\f
4a81b561 158
f4bd7a8f
DM
159#ifndef bfd_zmalloc
160/* allocate and clear storage */
4a81b561 161
4a81b561 162char *
f4bd7a8f
DM
163bfd_zmalloc (size)
164 bfd_size_type size;
4a81b561 165{
120f5bd9 166 char *ptr = (char *) malloc ((size_t) size);
4a81b561 167
120f5bd9
JL
168 if (ptr && size)
169 memset(ptr, 0, (size_t) size);
4a81b561
DHW
170
171 return ptr;
172}
f4bd7a8f 173#endif /* bfd_zmalloc */
4a81b561
DHW
174\f
175/* Some IO code */
176
177
178/* Note that archive entries don't have streams; they share their parent's.
f58809fd 179 This allows someone to play with the iostream behind BFD's back.
4a81b561
DHW
180
181 Also, note that the origin pointer points to the beginning of a file's
182 contents (0 for non-archive elements). For archive entries this is the
183 first octet in the file, NOT the beginning of the archive header. */
184
4fe6d901 185static int
f4bd7a8f
DM
186real_read (where, a,b, file)
187 PTR where;
4fe6d901
KR
188 size_t a;
189 size_t b;
f4bd7a8f 190 FILE *file;
7ed4093a 191{
4fe6d901 192 return fread (where, a, b, file);
7ed4093a 193}
f4bd7a8f 194
120f5bd9
JL
195/* Return value is amount read (FIXME: how are errors and end of file dealt
196 with? We never call bfd_set_error, which is probably a mistake). */
197
23b0b558 198bfd_size_type
f4bd7a8f
DM
199bfd_read (ptr, size, nitems, abfd)
200 PTR ptr;
201 bfd_size_type size;
202 bfd_size_type nitems;
203 bfd *abfd;
4a81b561 204{
0d552306 205 int nread;
4fe6d901 206 nread = real_read (ptr, 1, (size_t)(size*nitems), bfd_cache_lookup(abfd));
0d552306
KR
207#ifdef FILE_OFFSET_IS_CHAR_INDEX
208 if (nread > 0)
209 abfd->where += nread;
210#endif
120f5bd9
JL
211
212 /* Set bfd_error if we did not read as much data as we expected.
213
214 If the read failed due to an error set the bfd_error_system_call,
215 else set bfd_error_file_truncated.
216
217 A BFD backend may wish to override bfd_error_file_truncated to
218 provide something more useful (eg. no_symbols or wrong_format). */
219 if (nread < (int)(size * nitems))
220 {
221 if (ferror (bfd_cache_lookup (abfd)))
222 bfd_set_error (bfd_error_system_call);
223 else
224 bfd_set_error (bfd_error_file_truncated);
225 }
226
0d552306 227 return nread;
4a81b561
DHW
228}
229
4fe6d901
KR
230/* The window support stuff should probably be broken out into
231 another file.... */
232/* The idea behind the next and refcount fields is that one mapped
233 region can suffice for multiple read-only windows or multiple
234 non-overlapping read-write windows. It's not implemented yet
235 though. */
236struct _bfd_window_internal {
237 struct _bfd_window_internal *next;
238 PTR data;
239 bfd_size_type size;
240 int refcount : 31; /* should be enough... */
241 unsigned mapped : 1; /* 1 = mmap, 0 = malloc */
242};
243
244void
245bfd_init_window (windowp)
246 bfd_window *windowp;
247{
248 windowp->data = 0;
249 windowp->i = 0;
250 windowp->size = 0;
251}
252
253#undef HAVE_MPROTECT /* code's not tested yet */
254
255#if HAVE_MMAP || HAVE_MPROTECT
256#include <sys/types.h>
257#include <sys/mman.h>
258#endif
259
260#ifndef MAP_FILE
261#define MAP_FILE 0
262#endif
263
264static int debug_windows;
265
266void
267bfd_free_window (windowp)
268 bfd_window *windowp;
269{
270 bfd_window_internal *i = windowp->i;
271 windowp->i = 0;
272 windowp->data = 0;
273 if (i == 0)
274 return;
275 i->refcount--;
276 if (debug_windows)
277 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
278 windowp, windowp->data, windowp->size, windowp->i);
279 if (i->refcount != 0)
280 return;
281
282 if (i->mapped)
283 {
284#ifdef HAVE_MMAP
285 munmap (i->data, i->size);
286 goto no_free;
287#else
288 abort ();
289#endif
290 }
291#ifdef HAVE_MPROTECT
292 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
293#endif
294 free (i->data);
295#ifdef HAVE_MMAP
296 no_free:
297#endif
298 i->data = 0;
299 /* There should be no more references to i at this point. */
300 free (i);
301}
302
303static int ok_to_map = 1;
304
092abcdf 305boolean
4fe6d901
KR
306bfd_get_file_window (abfd, offset, size, windowp, writable)
307 bfd *abfd;
308 file_ptr offset;
309 bfd_size_type size;
310 bfd_window *windowp;
311 int writable;
312{
313 static size_t pagesize;
314 bfd_window_internal *i = windowp->i;
315 size_t size_to_alloc = size;
316
317 if (debug_windows)
318 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
319 abfd, (long) offset, (long) size,
320 windowp, windowp->data, windowp->size, windowp->i,
321 writable);
322
323 /* Make sure we know the page size, so we can be friendly to mmap. */
324 if (pagesize == 0)
325 pagesize = getpagesize ();
326 if (pagesize == 0)
327 abort ();
328
329 if (i == 0)
330 {
331 windowp->i = i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
332 if (i == 0)
333 return false;
334 i->data = 0;
335 }
336#ifdef HAVE_MMAP
337 if (ok_to_map && (i->data == 0 || i->mapped == 1))
338 {
339 file_ptr file_offset, offset2;
340 size_t real_size;
341 int fd;
342 FILE *f;
343
344 /* Find the real file and the real offset into it. */
345 while (abfd->my_archive != NULL)
346 {
347 offset += abfd->origin;
348 abfd = abfd->my_archive;
349 }
350 f = bfd_cache_lookup (abfd);
351 fd = fileno (f);
352
353 /* Compute offsets and size for mmap and for the user's data. */
354 offset2 = offset % pagesize;
355 if (offset2 < 0)
356 abort ();
357 file_offset = offset - offset2;
358 real_size = offset + size - file_offset;
359 real_size = real_size + pagesize - 1;
360 real_size -= real_size % pagesize;
361
362 /* If we're re-using a memory region, make sure it's big enough. */
363 if (i->data && i->size < size)
364 {
365 munmap (i->data, i->size);
366 i->data = 0;
367 }
368 i->data = mmap (i->data, real_size,
369 writable ? PROT_WRITE | PROT_READ : PROT_READ,
370 writable ? MAP_FILE | MAP_PRIVATE : MAP_FILE,
371 fd, file_offset);
372 if (i->data == (PTR) -1)
373 {
374 /* An error happened. Report it, or try using malloc, or
375 something. */
376 bfd_set_error (bfd_error_system_call);
377 i->data = 0;
378 windowp->data = 0;
379 if (debug_windows)
380 fprintf (stderr, "\t\tmmap failed!\n");
381 return false;
382 }
383 if (debug_windows)
384 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
385 (long) real_size, i->data, (long) offset2);
386 i->size = real_size;
387 windowp->data = i->data + offset2;
388 windowp->size = size;
389 i->mapped = 1;
390 return true;
391 }
392 else if (debug_windows)
393 {
394 if (ok_to_map)
395 fprintf (stderr, "not mapping: data=%x mapped=%d\n",
396 i->data, i->mapped);
397 else
398 fprintf (stderr, "not mapping: env var not set\n");
399 }
400#else
401 ok_to_map = 0;
402#endif
403
404#ifdef HAVE_MPROTECT
405 if (!writable)
406 {
407 size_to_alloc += pagesize - 1;
408 size_to_alloc -= size_to_alloc % pagesize;
409 }
410#endif
411 if (debug_windows)
412 fprintf (stderr, "\n\t%s(%6ld)",
413 i->data ? "realloc" : " malloc", (long) size_to_alloc);
414 if (i->data)
092abcdf 415 i->data = (PTR) realloc (i->data, size_to_alloc);
4fe6d901 416 else
092abcdf 417 i->data = (PTR) malloc (size_to_alloc);
4fe6d901
KR
418 if (debug_windows)
419 fprintf (stderr, "\t-> %p\n", i->data);
420 i->refcount = 1;
092abcdf
ILT
421 if (i->data == NULL)
422 {
423 if (size_to_alloc == 0)
424 return true;
425 bfd_set_error (bfd_error_no_memory);
426 return false;
427 }
4fe6d901
KR
428 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
429 return false;
430 i->size = bfd_read (i->data, size, 1, abfd);
431 if (i->size != size)
432 return false;
433 i->mapped = 0;
434#ifdef HAVE_MPROTECT
435 if (!writable)
436 {
437 if (debug_windows)
438 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
439 (long) i->size);
440 mprotect (i->data, i->size, PROT_READ);
441 }
442#endif
443 windowp->data = i->data;
444 windowp->size = i->size;
445 return true;
446}
447
23b0b558 448bfd_size_type
df34342b
ILT
449bfd_write (ptr, size, nitems, abfd)
450 CONST PTR ptr;
451 bfd_size_type size;
452 bfd_size_type nitems;
453 bfd *abfd;
4a81b561 454{
4fe6d901
KR
455 long nwrote = fwrite (ptr, 1, (size_t) (size * nitems),
456 bfd_cache_lookup (abfd));
0d552306
KR
457#ifdef FILE_OFFSET_IS_CHAR_INDEX
458 if (nwrote > 0)
459 abfd->where += nwrote;
460#endif
4fe6d901 461 if ((bfd_size_type) nwrote != size * nitems)
df34342b
ILT
462 {
463#ifdef ENOSPC
464 if (nwrote >= 0)
465 errno = ENOSPC;
466#endif
120f5bd9 467 bfd_set_error (bfd_error_system_call);
df34342b 468 }
0d552306 469 return nwrote;
4a81b561
DHW
470}
471
f8e01940
JG
472/*
473INTERNAL_FUNCTION
474 bfd_write_bigendian_4byte_int
475
476SYNOPSIS
477 void bfd_write_bigendian_4byte_int(bfd *abfd, int i);
478
479DESCRIPTION
c188b0be
DM
480 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
481 endian order regardless of what else is going on. This is useful in
f8e01940 482 archives.
1e310759 483
1e310759 484*/
f58809fd 485void
f4bd7a8f
DM
486bfd_write_bigendian_4byte_int (abfd, i)
487 bfd *abfd;
488 int i;
f58809fd 489{
1e310759 490 bfd_byte buffer[4];
b5b4294e 491 bfd_putb32(i, buffer);
4002f18a
ILT
492 if (bfd_write((PTR)buffer, 4, 1, abfd) != 4)
493 abort ();
f58809fd
SC
494}
495
0d552306 496long
f4bd7a8f
DM
497bfd_tell (abfd)
498 bfd *abfd;
0d552306
KR
499{
500 file_ptr ptr;
501
502 ptr = ftell (bfd_cache_lookup(abfd));
503
504 if (abfd->my_archive)
505 ptr -= abfd->origin;
506 abfd->where = ptr;
507 return ptr;
508}
509
b5b4294e 510int
f4bd7a8f
DM
511bfd_flush (abfd)
512 bfd *abfd;
b5b4294e
JG
513{
514 return fflush (bfd_cache_lookup(abfd));
515}
516
4fe6d901
KR
517/* Returns 0 for success, negative value for failure (in which case
518 bfd_get_error can retrieve the error code). */
b5b4294e 519int
f4bd7a8f
DM
520bfd_stat (abfd, statbuf)
521 bfd *abfd;
522 struct stat *statbuf;
b5b4294e 523{
4fe6d901
KR
524 FILE *f;
525 int result;
526 f = bfd_cache_lookup (abfd);
527 if (f == NULL)
528 {
529 bfd_set_error (bfd_error_system_call);
530 return -1;
531 }
532 result = fstat (fileno (f), statbuf);
533 if (result < 0)
534 bfd_set_error (bfd_error_system_call);
535 return result;
b5b4294e
JG
536}
537
120f5bd9
JL
538/* Returns 0 for success, nonzero for failure (in which case bfd_get_error
539 can retrieve the error code). */
540
4a81b561 541int
f4bd7a8f 542bfd_seek (abfd, position, direction)
4fe6d901
KR
543 bfd *abfd;
544 file_ptr position;
545 int direction;
4a81b561 546{
0d552306
KR
547 int result;
548 FILE *f;
e5b02860 549 file_ptr file_position;
0d552306
KR
550 /* For the time being, a BFD may not seek to it's end. The problem
551 is that we don't easily have a way to recognize the end of an
552 element in an archive. */
553
554 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
555
b31d06ca 556 if (direction == SEEK_CUR && position == 0)
0d552306
KR
557 return 0;
558#ifdef FILE_OFFSET_IS_CHAR_INDEX
b6090f4d
JG
559 if (abfd->format != bfd_archive && abfd->my_archive == 0)
560 {
4c3721d5 561#if 0
b6090f4d
JG
562 /* Explanation for this code: I'm only about 95+% sure that the above
563 conditions are sufficient and that all i/o calls are properly
564 adjusting the `where' field. So this is sort of an `assert'
565 that the `where' field is correct. If we can go a while without
566 tripping the abort, we can probably safely disable this code,
567 so that the real optimizations happen. */
568 file_ptr where_am_i_now;
569 where_am_i_now = ftell (bfd_cache_lookup (abfd));
570 if (abfd->my_archive)
571 where_am_i_now -= abfd->origin;
572 if (where_am_i_now != abfd->where)
573 abort ();
574#endif
575 if (direction == SEEK_SET && position == abfd->where)
576 return 0;
577 }
578 else
579 {
580 /* We need something smarter to optimize access to archives.
581 Currently, anything inside an archive is read via the file
582 handle for the archive. Which means that a bfd_seek on one
583 component affects the `current position' in the archive, as
584 well as in any other component.
585
586 It might be sufficient to put a spike through the cache
587 abstraction, and look to the archive for the file position,
588 but I think we should try for something cleaner.
589
590 In the meantime, no optimization for archives. */
591 }
0d552306 592#endif
4a81b561 593
0d552306 594 f = bfd_cache_lookup (abfd);
e5b02860 595 file_position = position;
0d552306 596 if (direction == SEEK_SET && abfd->my_archive != NULL)
e5b02860
KR
597 file_position += abfd->origin;
598
599 result = fseek (f, file_position, direction);
600
601 if (result != 0)
df34342b
ILT
602 {
603 /* Force redetermination of `where' field. */
604 bfd_tell (abfd);
120f5bd9 605 bfd_set_error (bfd_error_system_call);
df34342b 606 }
0d552306
KR
607 else
608 {
e5b02860
KR
609#ifdef FILE_OFFSET_IS_CHAR_INDEX
610 /* Adjust `where' field. */
611 if (direction == SEEK_SET)
612 abfd->where = position;
613 else
614 abfd->where += position;
615#endif
0d552306 616 }
e5b02860 617 return result;
4a81b561
DHW
618}
619\f
4a81b561
DHW
620/** The do-it-yourself (byte) sex-change kit */
621
622/* The middle letter e.g. get<b>short indicates Big or Little endian
623 target machine. It doesn't matter what the byte order of the host
624 machine is; these routines work for either. */
625
626/* FIXME: Should these take a count argument?
627 Answer (gnu@cygnus.com): No, but perhaps they should be inline
6f715d66
SC
628 functions in swap.h #ifdef __GNUC__.
629 Gprof them later and find out. */
630
f8e01940
JG
631/*
632FUNCTION
633 bfd_put_size
634FUNCTION
635 bfd_get_size
636
637DESCRIPTION
638 These macros as used for reading and writing raw data in
639 sections; each access (except for bytes) is vectored through
640 the target format of the BFD and mangled accordingly. The
641 mangling performs any necessary endian translations and
14e3c2e4
JK
642 removes alignment restrictions. Note that types accepted and
643 returned by these macros are identical so they can be swapped
17ecba1a
DM
644 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
645 to either <<bfd_get_32>> or <<bfd_get_64>>.
f8e01940 646
17ecba1a 647 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
7e4db254
JK
648 system without prototypes, the caller is responsible for making
649 sure that is true, with a cast if necessary. We don't cast
17ecba1a
DM
650 them in the macro definitions because that would prevent <<lint>>
651 or <<gcc -Wall>> from detecting sins such as passing a pointer.
652 To detect calling these with less than a <<bfd_vma>>, use
653 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
7e4db254 654
da3cd00a
KR
655.
656.{* Byte swapping macros for user section data. *}
657.
f8e01940 658.#define bfd_put_8(abfd, val, ptr) \
4c3721d5 659. (*((unsigned char *)(ptr)) = (unsigned char)(val))
da3cd00a
KR
660.#define bfd_put_signed_8 \
661. bfd_put_8
f8e01940 662.#define bfd_get_8(abfd, ptr) \
da3cd00a
KR
663. (*(unsigned char *)(ptr))
664.#define bfd_get_signed_8(abfd, ptr) \
665. ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
666.
f8e01940 667.#define bfd_put_16(abfd, val, ptr) \
7e4db254 668. BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
da3cd00a
KR
669.#define bfd_put_signed_16 \
670. bfd_put_16
f8e01940
JG
671.#define bfd_get_16(abfd, ptr) \
672. BFD_SEND(abfd, bfd_getx16, (ptr))
14e3c2e4
JK
673.#define bfd_get_signed_16(abfd, ptr) \
674. BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
da3cd00a 675.
f8e01940 676.#define bfd_put_32(abfd, val, ptr) \
7e4db254 677. BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
da3cd00a
KR
678.#define bfd_put_signed_32 \
679. bfd_put_32
f8e01940
JG
680.#define bfd_get_32(abfd, ptr) \
681. BFD_SEND(abfd, bfd_getx32, (ptr))
14e3c2e4
JK
682.#define bfd_get_signed_32(abfd, ptr) \
683. BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
da3cd00a 684.
f8e01940 685.#define bfd_put_64(abfd, val, ptr) \
7e4db254 686. BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
da3cd00a
KR
687.#define bfd_put_signed_64 \
688. bfd_put_64
f8e01940
JG
689.#define bfd_get_64(abfd, ptr) \
690. BFD_SEND(abfd, bfd_getx64, (ptr))
14e3c2e4
JK
691.#define bfd_get_signed_64(abfd, ptr) \
692. BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
da3cd00a 693.
f8e01940
JG
694*/
695
696/*
697FUNCTION
698 bfd_h_put_size
f8e01940
JG
699 bfd_h_get_size
700
701DESCRIPTION
702 These macros have the same function as their <<bfd_get_x>>
c188b0be 703 bretheren, except that they are used for removing information
f8e01940
JG
704 for the header records of object files. Believe it or not,
705 some object files keep their header records in big endian
c188b0be 706 order and their data in little endian order.
da3cd00a
KR
707.
708.{* Byte swapping macros for file header data. *}
709.
f8e01940 710.#define bfd_h_put_8(abfd, val, ptr) \
da3cd00a
KR
711. bfd_put_8 (abfd, val, ptr)
712.#define bfd_h_put_signed_8(abfd, val, ptr) \
713. bfd_put_8 (abfd, val, ptr)
f8e01940 714.#define bfd_h_get_8(abfd, ptr) \
da3cd00a
KR
715. bfd_get_8 (abfd, ptr)
716.#define bfd_h_get_signed_8(abfd, ptr) \
717. bfd_get_signed_8 (abfd, ptr)
718.
f8e01940
JG
719.#define bfd_h_put_16(abfd, val, ptr) \
720. BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
da3cd00a
KR
721.#define bfd_h_put_signed_16 \
722. bfd_h_put_16
f8e01940
JG
723.#define bfd_h_get_16(abfd, ptr) \
724. BFD_SEND(abfd, bfd_h_getx16,(ptr))
14e3c2e4
JK
725.#define bfd_h_get_signed_16(abfd, ptr) \
726. BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
da3cd00a 727.
f8e01940
JG
728.#define bfd_h_put_32(abfd, val, ptr) \
729. BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
da3cd00a
KR
730.#define bfd_h_put_signed_32 \
731. bfd_h_put_32
f8e01940
JG
732.#define bfd_h_get_32(abfd, ptr) \
733. BFD_SEND(abfd, bfd_h_getx32,(ptr))
14e3c2e4
JK
734.#define bfd_h_get_signed_32(abfd, ptr) \
735. BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
da3cd00a 736.
f8e01940
JG
737.#define bfd_h_put_64(abfd, val, ptr) \
738. BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
da3cd00a
KR
739.#define bfd_h_put_signed_64 \
740. bfd_h_put_64
f8e01940
JG
741.#define bfd_h_get_64(abfd, ptr) \
742. BFD_SEND(abfd, bfd_h_getx64,(ptr))
14e3c2e4
JK
743.#define bfd_h_get_signed_64(abfd, ptr) \
744. BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
da3cd00a 745.
f8e01940 746*/
4a81b561 747
14e3c2e4 748/* Sign extension to bfd_signed_vma. */
df34342b
ILT
749#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
750#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
96ad107b 751#define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
df34342b
ILT
752#define COERCE64(x) \
753 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
14e3c2e4 754
f58809fd 755bfd_vma
f4bd7a8f
DM
756bfd_getb16 (addr)
757 register const bfd_byte *addr;
4a81b561 758{
f4bd7a8f 759 return (addr[0] << 8) | addr[1];
4a81b561
DHW
760}
761
f58809fd 762bfd_vma
f4bd7a8f
DM
763bfd_getl16 (addr)
764 register const bfd_byte *addr;
4a81b561 765{
f4bd7a8f 766 return (addr[1] << 8) | addr[0];
4a81b561
DHW
767}
768
14e3c2e4 769bfd_signed_vma
f4bd7a8f
DM
770bfd_getb_signed_16 (addr)
771 register const bfd_byte *addr;
14e3c2e4 772{
f4bd7a8f 773 return COERCE16((addr[0] << 8) | addr[1]);
14e3c2e4
JK
774}
775
776bfd_signed_vma
f4bd7a8f
DM
777bfd_getl_signed_16 (addr)
778 register const bfd_byte *addr;
14e3c2e4 779{
f4bd7a8f 780 return COERCE16((addr[1] << 8) | addr[0]);
14e3c2e4
JK
781}
782
4a81b561 783void
f4bd7a8f
DM
784bfd_putb16 (data, addr)
785 bfd_vma data;
786 register bfd_byte *addr;
4a81b561 787{
f4bd7a8f
DM
788 addr[0] = (bfd_byte)(data >> 8);
789 addr[1] = (bfd_byte )data;
4a81b561
DHW
790}
791
792void
f4bd7a8f
DM
793bfd_putl16 (data, addr)
794 bfd_vma data;
795 register bfd_byte *addr;
4a81b561 796{
f4bd7a8f
DM
797 addr[0] = (bfd_byte )data;
798 addr[1] = (bfd_byte)(data >> 8);
4a81b561
DHW
799}
800
f58809fd 801bfd_vma
b5b4294e 802bfd_getb32 (addr)
f4bd7a8f 803 register const bfd_byte *addr;
4a81b561 804{
f4bd7a8f
DM
805 return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
806 | addr[2]) << 8 | addr[3];
4a81b561
DHW
807}
808
f58809fd 809bfd_vma
b5b4294e 810bfd_getl32 (addr)
f4bd7a8f 811 register const bfd_byte *addr;
4a81b561 812{
f4bd7a8f
DM
813 return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
814 | addr[1]) << 8 | addr[0];
4a81b561
DHW
815}
816
14e3c2e4 817bfd_signed_vma
b5b4294e 818bfd_getb_signed_32 (addr)
f4bd7a8f 819 register const bfd_byte *addr;
14e3c2e4 820{
f4bd7a8f
DM
821 return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
822 | addr[2]) << 8 | addr[3]);
14e3c2e4
JK
823}
824
825bfd_signed_vma
b5b4294e 826bfd_getl_signed_32 (addr)
f4bd7a8f 827 register const bfd_byte *addr;
14e3c2e4 828{
f4bd7a8f
DM
829 return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
830 | addr[1]) << 8 | addr[0]);
14e3c2e4
JK
831}
832
f58809fd 833bfd_vma
f4bd7a8f
DM
834bfd_getb64 (addr)
835 register const bfd_byte *addr;
7ed4093a 836{
b5b4294e 837#ifdef BFD64
b6090f4d 838 bfd_vma low, high;
536b27a5 839
7ed4093a 840 high= ((((((((addr[0]) << 8) |
6f715d66
SC
841 addr[1]) << 8) |
842 addr[2]) << 8) |
843 addr[3]) );
7ed4093a 844
df34342b 845 low = (((((((((bfd_vma)addr[4]) << 8) |
6f715d66
SC
846 addr[5]) << 8) |
847 addr[6]) << 8) |
848 addr[7]));
7ed4093a
SC
849
850 return high << 32 | low;
851#else
852 BFD_FAIL();
f58809fd 853 return 0;
7ed4093a 854#endif
7ed4093a
SC
855}
856
f58809fd 857bfd_vma
f4bd7a8f
DM
858bfd_getl64 (addr)
859 register const bfd_byte *addr;
7ed4093a 860{
b5b4294e 861#ifdef BFD64
b6090f4d 862 bfd_vma low, high;
7ed4093a 863 high= (((((((addr[7] << 8) |
6f715d66
SC
864 addr[6]) << 8) |
865 addr[5]) << 8) |
866 addr[4]));
7ed4093a 867
df34342b 868 low = ((((((((bfd_vma)addr[3] << 8) |
6f715d66
SC
869 addr[2]) << 8) |
870 addr[1]) << 8) |
871 addr[0]) );
7ed4093a
SC
872
873 return high << 32 | low;
874#else
875 BFD_FAIL();
f58809fd 876 return 0;
7ed4093a 877#endif
6f715d66 878
7ed4093a
SC
879}
880
14e3c2e4 881bfd_signed_vma
f4bd7a8f
DM
882bfd_getb_signed_64 (addr)
883 register const bfd_byte *addr;
14e3c2e4 884{
b5b4294e 885#ifdef BFD64
14e3c2e4
JK
886 bfd_vma low, high;
887
888 high= ((((((((addr[0]) << 8) |
889 addr[1]) << 8) |
890 addr[2]) << 8) |
891 addr[3]) );
892
df34342b 893 low = (((((((((bfd_vma)addr[4]) << 8) |
14e3c2e4
JK
894 addr[5]) << 8) |
895 addr[6]) << 8) |
896 addr[7]));
897
898 return COERCE64(high << 32 | low);
899#else
900 BFD_FAIL();
901 return 0;
902#endif
14e3c2e4
JK
903}
904
905bfd_signed_vma
f4bd7a8f
DM
906bfd_getl_signed_64 (addr)
907 register const bfd_byte *addr;
14e3c2e4 908{
b5b4294e 909#ifdef BFD64
14e3c2e4
JK
910 bfd_vma low, high;
911 high= (((((((addr[7] << 8) |
912 addr[6]) << 8) |
913 addr[5]) << 8) |
914 addr[4]));
915
df34342b 916 low = ((((((((bfd_vma)addr[3] << 8) |
14e3c2e4
JK
917 addr[2]) << 8) |
918 addr[1]) << 8) |
919 addr[0]) );
920
921 return COERCE64(high << 32 | low);
922#else
923 BFD_FAIL();
924 return 0;
925#endif
14e3c2e4
JK
926}
927
4a81b561 928void
f4bd7a8f
DM
929bfd_putb32 (data, addr)
930 bfd_vma data;
931 register bfd_byte *addr;
4a81b561 932{
6f715d66
SC
933 addr[0] = (bfd_byte)(data >> 24);
934 addr[1] = (bfd_byte)(data >> 16);
935 addr[2] = (bfd_byte)(data >> 8);
936 addr[3] = (bfd_byte)data;
4a81b561
DHW
937}
938
939void
f4bd7a8f
DM
940bfd_putl32 (data, addr)
941 bfd_vma data;
942 register bfd_byte *addr;
4a81b561 943{
6f715d66
SC
944 addr[0] = (bfd_byte)data;
945 addr[1] = (bfd_byte)(data >> 8);
946 addr[2] = (bfd_byte)(data >> 16);
947 addr[3] = (bfd_byte)(data >> 24);
4a81b561 948}
f4bd7a8f 949
7ed4093a 950void
f4bd7a8f
DM
951bfd_putb64 (data, addr)
952 bfd_vma data;
953 register bfd_byte *addr;
7ed4093a 954{
b5b4294e 955#ifdef BFD64
536b27a5
SC
956 addr[0] = (bfd_byte)(data >> (7*8));
957 addr[1] = (bfd_byte)(data >> (6*8));
958 addr[2] = (bfd_byte)(data >> (5*8));
959 addr[3] = (bfd_byte)(data >> (4*8));
960 addr[4] = (bfd_byte)(data >> (3*8));
961 addr[5] = (bfd_byte)(data >> (2*8));
962 addr[6] = (bfd_byte)(data >> (1*8));
963 addr[7] = (bfd_byte)(data >> (0*8));
7ed4093a 964#else
536b27a5 965 BFD_FAIL();
7ed4093a 966#endif
7ed4093a
SC
967}
968
969void
f4bd7a8f
DM
970bfd_putl64 (data, addr)
971 bfd_vma data;
972 register bfd_byte *addr;
7ed4093a 973{
b5b4294e 974#ifdef BFD64
536b27a5
SC
975 addr[7] = (bfd_byte)(data >> (7*8));
976 addr[6] = (bfd_byte)(data >> (6*8));
977 addr[5] = (bfd_byte)(data >> (5*8));
978 addr[4] = (bfd_byte)(data >> (4*8));
979 addr[3] = (bfd_byte)(data >> (3*8));
980 addr[2] = (bfd_byte)(data >> (2*8));
981 addr[1] = (bfd_byte)(data >> (1*8));
982 addr[0] = (bfd_byte)(data >> (0*8));
7ed4093a 983#else
536b27a5 984 BFD_FAIL();
7ed4093a 985#endif
7ed4093a 986}
2203f786
JG
987\f
988/* Default implementation */
4a81b561 989
2203f786 990boolean
6812b607 991_bfd_generic_get_section_contents (abfd, section, location, offset, count)
f4bd7a8f
DM
992 bfd *abfd;
993 sec_ptr section;
994 PTR location;
995 file_ptr offset;
996 bfd_size_type count;
2203f786
JG
997{
998 if (count == 0)
6f715d66 999 return true;
f8e01940
JG
1000 if ((bfd_size_type)(offset+count) > section->_raw_size
1001 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
6f715d66
SC
1002 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
1003 return (false); /* on error */
2203f786
JG
1004 return (true);
1005}
6f715d66 1006
4fe6d901
KR
1007boolean
1008_bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count)
1009 bfd *abfd;
1010 sec_ptr section;
1011 bfd_window *w;
1012 file_ptr offset;
1013 bfd_size_type count;
1014{
1015 if (count == 0)
1016 return true;
1017 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents)
1018 {
1019 /* We don't know what changes the bfd's get_section_contents
1020 method may have to make. So punt trying to map the file
1021 window, and let get_section_contents do its thing. */
1022 /* @@ FIXME : If the internal window has a refcount of 1 and was
1023 allocated with malloc instead of mmap, just reuse it. */
1024 bfd_free_window (w);
1025 w->i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal));
1026 if (w->i == NULL)
1027 return false;
1028 w->i->data = (PTR) malloc ((size_t) count);
1029 if (w->i->data == NULL)
1030 {
1031 free (w->i);
1032 w->i = NULL;
1033 return false;
1034 }
1035 w->i->mapped = 0;
1036 w->i->refcount = 1;
1037 w->size = w->i->size = count;
1038 w->data = w->i->data;
1039 return bfd_get_section_contents (abfd, section, w->data, offset, count);
1040 }
1041 if ((bfd_size_type) (offset+count) > section->_raw_size
1042 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, 1)
1043 == false))
1044 return false;
1045 return true;
1046}
1047
f58809fd
SC
1048/* This generic function can only be used in implementations where creating
1049 NEW sections is disallowed. It is useful in patching existing sections
1050 in read-write files, though. See other set_section_contents functions
1051 to see why it doesn't work for new sections. */
1052boolean
6812b607 1053_bfd_generic_set_section_contents (abfd, section, location, offset, count)
df34342b
ILT
1054 bfd *abfd;
1055 sec_ptr section;
1056 PTR location;
1057 file_ptr offset;
1058 bfd_size_type count;
f58809fd 1059{
df34342b
ILT
1060 if (count == 0)
1061 return true;
1062
df34342b
ILT
1063 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
1064 || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
1065 return false;
1066
1067 return true;
f58809fd
SC
1068}
1069
f8e01940
JG
1070/*
1071INTERNAL_FUNCTION
1072 bfd_log2
1073
17ecba1a
DM
1074SYNOPSIS
1075 unsigned int bfd_log2(bfd_vma x);
1076
f8e01940 1077DESCRIPTION
c188b0be
DM
1078 Return the log base 2 of the value supplied, rounded up. E.g., an
1079 @var{x} of 1025 returns 11.
f8e01940 1080*/
6f715d66 1081
b5b4294e
JG
1082unsigned
1083bfd_log2(x)
1084 bfd_vma x;
6f715d66 1085{
b5b4294e 1086 unsigned result = 0;
6f715d66
SC
1087 while ( (bfd_vma)(1<< result) < x)
1088 result++;
1089 return result;
1090}
120f5bd9
JL
1091
1092boolean
1093bfd_generic_is_local_label (abfd, sym)
1094 bfd *abfd;
1095 asymbol *sym;
1096{
1097 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1098
1099 return (sym->name[0] == locals_prefix);
1100}
1101
This page took 0.21524 seconds and 4 git commands to generate.