libbfd.c revision 94536
1/* Assorted BFD support routines, only used internally. 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 3 2000, 2001, 2002 4 Free Software Foundation, Inc. 5 Written by Cygnus Support. 6 7 This file is part of BFD, the Binary File Descriptor library. 8 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. 13 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. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 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 31static size_t real_read PARAMS ((PTR, size_t, size_t, FILE *)); 32 33/* 34SECTION 35 Internal functions 36 37DESCRIPTION 38 These routines are used within BFD. 39 They are not intended for export, but are documented here for 40 completeness. 41*/ 42 43/* A routine which is used in target vectors for unsupported 44 operations. */ 45 46boolean 47bfd_false (ignore) 48 bfd *ignore ATTRIBUTE_UNUSED; 49{ 50 bfd_set_error (bfd_error_invalid_operation); 51 return false; 52} 53 54/* A routine which is used in target vectors for supported operations 55 which do not actually do anything. */ 56 57boolean 58bfd_true (ignore) 59 bfd *ignore ATTRIBUTE_UNUSED; 60{ 61 return true; 62} 63 64/* A routine which is used in target vectors for unsupported 65 operations which return a pointer value. */ 66 67PTR 68bfd_nullvoidptr (ignore) 69 bfd *ignore ATTRIBUTE_UNUSED; 70{ 71 bfd_set_error (bfd_error_invalid_operation); 72 return NULL; 73} 74 75int 76bfd_0 (ignore) 77 bfd *ignore ATTRIBUTE_UNUSED; 78{ 79 return 0; 80} 81 82unsigned int 83bfd_0u (ignore) 84 bfd *ignore ATTRIBUTE_UNUSED; 85{ 86 return 0; 87} 88 89long 90bfd_0l (ignore) 91 bfd *ignore ATTRIBUTE_UNUSED; 92{ 93 return 0; 94} 95 96/* A routine which is used in target vectors for unsupported 97 operations which return -1 on error. */ 98 99long 100_bfd_n1 (ignore_abfd) 101 bfd *ignore_abfd ATTRIBUTE_UNUSED; 102{ 103 bfd_set_error (bfd_error_invalid_operation); 104 return -1; 105} 106 107void 108bfd_void (ignore) 109 bfd *ignore ATTRIBUTE_UNUSED; 110{ 111} 112 113boolean 114_bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd) 115 bfd *ignore_core_bfd ATTRIBUTE_UNUSED; 116 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED; 117{ 118 bfd_set_error (bfd_error_invalid_operation); 119 return false; 120} 121 122/* Routine to handle core_file_failing_command entry point for targets 123 without core file support. */ 124 125char * 126_bfd_nocore_core_file_failing_command (ignore_abfd) 127 bfd *ignore_abfd ATTRIBUTE_UNUSED; 128{ 129 bfd_set_error (bfd_error_invalid_operation); 130 return (char *)NULL; 131} 132 133/* Routine to handle core_file_failing_signal entry point for targets 134 without core file support. */ 135 136int 137_bfd_nocore_core_file_failing_signal (ignore_abfd) 138 bfd *ignore_abfd ATTRIBUTE_UNUSED; 139{ 140 bfd_set_error (bfd_error_invalid_operation); 141 return 0; 142} 143 144const bfd_target * 145_bfd_dummy_target (ignore_abfd) 146 bfd *ignore_abfd ATTRIBUTE_UNUSED; 147{ 148 bfd_set_error (bfd_error_wrong_format); 149 return 0; 150} 151 152/* Allocate memory using malloc. */ 153 154PTR 155bfd_malloc (size) 156 bfd_size_type size; 157{ 158 PTR ptr; 159 160 if (size != (size_t) size) 161 { 162 bfd_set_error (bfd_error_no_memory); 163 return NULL; 164 } 165 166 ptr = (PTR) malloc ((size_t) size); 167 if (ptr == NULL && (size_t) size != 0) 168 bfd_set_error (bfd_error_no_memory); 169 170 return ptr; 171} 172 173/* Reallocate memory using realloc. */ 174 175PTR 176bfd_realloc (ptr, size) 177 PTR ptr; 178 bfd_size_type size; 179{ 180 PTR ret; 181 182 if (size != (size_t) size) 183 { 184 bfd_set_error (bfd_error_no_memory); 185 return NULL; 186 } 187 188 if (ptr == NULL) 189 ret = malloc ((size_t) size); 190 else 191 ret = realloc (ptr, (size_t) size); 192 193 if (ret == NULL && (size_t) size != 0) 194 bfd_set_error (bfd_error_no_memory); 195 196 return ret; 197} 198 199/* Allocate memory using malloc and clear it. */ 200 201PTR 202bfd_zmalloc (size) 203 bfd_size_type size; 204{ 205 PTR ptr; 206 207 if (size != (size_t) size) 208 { 209 bfd_set_error (bfd_error_no_memory); 210 return NULL; 211 } 212 213 ptr = (PTR) malloc ((size_t) size); 214 215 if ((size_t) size != 0) 216 { 217 if (ptr == NULL) 218 bfd_set_error (bfd_error_no_memory); 219 else 220 memset (ptr, 0, (size_t) size); 221 } 222 223 return ptr; 224} 225 226/* Some IO code */ 227 228/* Note that archive entries don't have streams; they share their parent's. 229 This allows someone to play with the iostream behind BFD's back. 230 231 Also, note that the origin pointer points to the beginning of a file's 232 contents (0 for non-archive elements). For archive entries this is the 233 first octet in the file, NOT the beginning of the archive header. */ 234 235static size_t 236real_read (where, a, b, file) 237 PTR where; 238 size_t a; 239 size_t b; 240 FILE *file; 241{ 242 /* FIXME - this looks like an optimization, but it's really to cover 243 up for a feature of some OSs (not solaris - sigh) that 244 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs 245 internally and tries to link against them. BFD seems to be smart 246 enough to realize there are no symbol records in the "file" that 247 doesn't exist but attempts to read them anyway. On Solaris, 248 attempting to read zero bytes from a NULL file results in a core 249 dump, but on other platforms it just returns zero bytes read. 250 This makes it to something reasonable. - DJ */ 251 if (a == 0 || b == 0) 252 return 0; 253 254 255#if defined (__VAX) && defined (VMS) 256 /* Apparently fread on Vax VMS does not keep the record length 257 information. */ 258 return read (fileno (file), where, a * b); 259#else 260 return fread (where, a, b, file); 261#endif 262} 263 264/* Return value is amount read. */ 265 266bfd_size_type 267bfd_bread (ptr, size, abfd) 268 PTR ptr; 269 bfd_size_type size; 270 bfd *abfd; 271{ 272 size_t nread; 273 274 if ((abfd->flags & BFD_IN_MEMORY) != 0) 275 { 276 struct bfd_in_memory *bim; 277 bfd_size_type get; 278 279 bim = (struct bfd_in_memory *) abfd->iostream; 280 get = size; 281 if (abfd->where + get > bim->size) 282 { 283 if (bim->size < (bfd_size_type) abfd->where) 284 get = 0; 285 else 286 get = bim->size - abfd->where; 287 bfd_set_error (bfd_error_file_truncated); 288 } 289 memcpy (ptr, bim->buffer + abfd->where, (size_t) get); 290 abfd->where += get; 291 return get; 292 } 293 294 nread = real_read (ptr, 1, (size_t) size, bfd_cache_lookup (abfd)); 295 if (nread != (size_t) -1) 296 abfd->where += nread; 297 298 /* Set bfd_error if we did not read as much data as we expected. 299 300 If the read failed due to an error set the bfd_error_system_call, 301 else set bfd_error_file_truncated. 302 303 A BFD backend may wish to override bfd_error_file_truncated to 304 provide something more useful (eg. no_symbols or wrong_format). */ 305 if (nread != size) 306 { 307 if (ferror (bfd_cache_lookup (abfd))) 308 bfd_set_error (bfd_error_system_call); 309 else 310 bfd_set_error (bfd_error_file_truncated); 311 } 312 313 return nread; 314} 315 316/* The window support stuff should probably be broken out into 317 another file.... */ 318/* The idea behind the next and refcount fields is that one mapped 319 region can suffice for multiple read-only windows or multiple 320 non-overlapping read-write windows. It's not implemented yet 321 though. */ 322struct _bfd_window_internal { 323 struct _bfd_window_internal *next; 324 PTR data; 325 bfd_size_type size; 326 int refcount : 31; /* should be enough... */ 327 unsigned mapped : 1; /* 1 = mmap, 0 = malloc */ 328}; 329 330void 331bfd_init_window (windowp) 332 bfd_window *windowp; 333{ 334 windowp->data = 0; 335 windowp->i = 0; 336 windowp->size = 0; 337} 338 339/* Currently, if USE_MMAP is undefined, none if the window stuff is 340 used. Okay, so it's mis-named. At least the command-line option 341 "--without-mmap" is more obvious than "--without-windows" or some 342 such. */ 343#ifdef USE_MMAP 344 345#undef HAVE_MPROTECT /* code's not tested yet */ 346 347#if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE 348#include <sys/mman.h> 349#endif 350 351#ifndef MAP_FILE 352#define MAP_FILE 0 353#endif 354 355static int debug_windows; 356 357void 358bfd_free_window (windowp) 359 bfd_window *windowp; 360{ 361 bfd_window_internal *i = windowp->i; 362 windowp->i = 0; 363 windowp->data = 0; 364 if (i == 0) 365 return; 366 i->refcount--; 367 if (debug_windows) 368 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n", 369 windowp, windowp->data, windowp->size, windowp->i); 370 if (i->refcount != 0) 371 return; 372 373 if (i->mapped) 374 { 375#ifdef HAVE_MMAP 376 munmap (i->data, i->size); 377 goto no_free; 378#else 379 abort (); 380#endif 381 } 382#ifdef HAVE_MPROTECT 383 mprotect (i->data, i->size, PROT_READ | PROT_WRITE); 384#endif 385 free (i->data); 386#ifdef HAVE_MMAP 387 no_free: 388#endif 389 i->data = 0; 390 /* There should be no more references to i at this point. */ 391 free (i); 392} 393 394static int ok_to_map = 1; 395 396boolean 397bfd_get_file_window (abfd, offset, size, windowp, writable) 398 bfd *abfd; 399 file_ptr offset; 400 bfd_size_type size; 401 bfd_window *windowp; 402 boolean writable; 403{ 404 static size_t pagesize; 405 bfd_window_internal *i = windowp->i; 406 bfd_size_type size_to_alloc = size; 407 408 if (debug_windows) 409 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)", 410 abfd, (long) offset, (long) size, 411 windowp, windowp->data, (unsigned long) windowp->size, 412 windowp->i, writable); 413 414 /* Make sure we know the page size, so we can be friendly to mmap. */ 415 if (pagesize == 0) 416 pagesize = getpagesize (); 417 if (pagesize == 0) 418 abort (); 419 420 if (i == 0) 421 { 422 i = ((bfd_window_internal *) 423 bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal))); 424 windowp->i = i; 425 if (i == 0) 426 return false; 427 i->data = 0; 428 } 429#ifdef HAVE_MMAP 430 if (ok_to_map 431 && (i->data == 0 || i->mapped == 1) 432 && (abfd->flags & BFD_IN_MEMORY) == 0) 433 { 434 file_ptr file_offset, offset2; 435 size_t real_size; 436 int fd; 437 FILE *f; 438 439 /* Find the real file and the real offset into it. */ 440 while (abfd->my_archive != NULL) 441 { 442 offset += abfd->origin; 443 abfd = abfd->my_archive; 444 } 445 f = bfd_cache_lookup (abfd); 446 fd = fileno (f); 447 448 /* Compute offsets and size for mmap and for the user's data. */ 449 offset2 = offset % pagesize; 450 if (offset2 < 0) 451 abort (); 452 file_offset = offset - offset2; 453 real_size = offset + size - file_offset; 454 real_size = real_size + pagesize - 1; 455 real_size -= real_size % pagesize; 456 457 /* If we're re-using a memory region, make sure it's big enough. */ 458 if (i->data && i->size < size) 459 { 460 munmap (i->data, i->size); 461 i->data = 0; 462 } 463 i->data = mmap (i->data, real_size, 464 writable ? PROT_WRITE | PROT_READ : PROT_READ, 465 (writable 466 ? MAP_FILE | MAP_PRIVATE 467 : MAP_FILE | MAP_SHARED), 468 fd, file_offset); 469 if (i->data == (PTR) -1) 470 { 471 /* An error happened. Report it, or try using malloc, or 472 something. */ 473 bfd_set_error (bfd_error_system_call); 474 i->data = 0; 475 windowp->data = 0; 476 if (debug_windows) 477 fprintf (stderr, "\t\tmmap failed!\n"); 478 return false; 479 } 480 if (debug_windows) 481 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n", 482 (long) real_size, i->data, (long) offset2); 483 i->size = real_size; 484 windowp->data = (PTR) ((bfd_byte *) i->data + offset2); 485 windowp->size = size; 486 i->mapped = 1; 487 return true; 488 } 489 else if (debug_windows) 490 { 491 if (ok_to_map) 492 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"), 493 (unsigned long) i->data, (int) i->mapped); 494 else 495 fprintf (stderr, _("not mapping: env var not set\n")); 496 } 497#else 498 ok_to_map = 0; 499#endif 500 501#ifdef HAVE_MPROTECT 502 if (!writable) 503 { 504 size_to_alloc += pagesize - 1; 505 size_to_alloc -= size_to_alloc % pagesize; 506 } 507#endif 508 if (debug_windows) 509 fprintf (stderr, "\n\t%s(%6ld)", 510 i->data ? "realloc" : " malloc", (long) size_to_alloc); 511 i->data = (PTR) bfd_realloc (i->data, size_to_alloc); 512 if (debug_windows) 513 fprintf (stderr, "\t-> %p\n", i->data); 514 i->refcount = 1; 515 if (i->data == NULL) 516 { 517 if (size_to_alloc == 0) 518 return true; 519 return false; 520 } 521 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 522 return false; 523 i->size = bfd_bread (i->data, size, abfd); 524 if (i->size != size) 525 return false; 526 i->mapped = 0; 527#ifdef HAVE_MPROTECT 528 if (!writable) 529 { 530 if (debug_windows) 531 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data, 532 (long) i->size); 533 mprotect (i->data, i->size, PROT_READ); 534 } 535#endif 536 windowp->data = i->data; 537 windowp->size = i->size; 538 return true; 539} 540 541#endif /* USE_MMAP */ 542 543bfd_size_type 544bfd_bwrite (ptr, size, abfd) 545 const PTR ptr; 546 bfd_size_type size; 547 bfd *abfd; 548{ 549 size_t nwrote; 550 551 if ((abfd->flags & BFD_IN_MEMORY) != 0) 552 { 553 struct bfd_in_memory *bim = (struct bfd_in_memory *) (abfd->iostream); 554 size = (size_t) size; 555 if (abfd->where + size > bim->size) 556 { 557 bfd_size_type newsize, oldsize; 558 559 oldsize = (bim->size + 127) & ~(bfd_size_type) 127; 560 bim->size = abfd->where + size; 561 /* Round up to cut down on memory fragmentation */ 562 newsize = (bim->size + 127) & ~(bfd_size_type) 127; 563 if (newsize > oldsize) 564 { 565 bim->buffer = bfd_realloc (bim->buffer, newsize); 566 if (bim->buffer == 0) 567 { 568 bim->size = 0; 569 return 0; 570 } 571 } 572 } 573 memcpy (bim->buffer + abfd->where, ptr, (size_t) size); 574 abfd->where += size; 575 return size; 576 } 577 578 nwrote = fwrite (ptr, 1, (size_t) size, bfd_cache_lookup (abfd)); 579 if (nwrote != (size_t) -1) 580 abfd->where += nwrote; 581 if (nwrote != size) 582 { 583#ifdef ENOSPC 584 errno = ENOSPC; 585#endif 586 bfd_set_error (bfd_error_system_call); 587 } 588 return nwrote; 589} 590 591/* 592INTERNAL_FUNCTION 593 bfd_write_bigendian_4byte_int 594 595SYNOPSIS 596 boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int); 597 598DESCRIPTION 599 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big 600 endian order regardless of what else is going on. This is useful in 601 archives. 602 603*/ 604boolean 605bfd_write_bigendian_4byte_int (abfd, i) 606 bfd *abfd; 607 unsigned int i; 608{ 609 bfd_byte buffer[4]; 610 bfd_putb32 ((bfd_vma) i, buffer); 611 return bfd_bwrite ((PTR) buffer, (bfd_size_type) 4, abfd) == 4; 612} 613 614bfd_vma 615bfd_tell (abfd) 616 bfd *abfd; 617{ 618 file_ptr ptr; 619 620 if ((abfd->flags & BFD_IN_MEMORY) != 0) 621 return abfd->where; 622 623 ptr = ftell (bfd_cache_lookup (abfd)); 624 625 if (abfd->my_archive) 626 ptr -= abfd->origin; 627 abfd->where = ptr; 628 return ptr; 629} 630 631int 632bfd_flush (abfd) 633 bfd *abfd; 634{ 635 if ((abfd->flags & BFD_IN_MEMORY) != 0) 636 return 0; 637 return fflush (bfd_cache_lookup(abfd)); 638} 639 640/* Returns 0 for success, negative value for failure (in which case 641 bfd_get_error can retrieve the error code). */ 642int 643bfd_stat (abfd, statbuf) 644 bfd *abfd; 645 struct stat *statbuf; 646{ 647 FILE *f; 648 int result; 649 650 if ((abfd->flags & BFD_IN_MEMORY) != 0) 651 abort (); 652 653 f = bfd_cache_lookup (abfd); 654 if (f == NULL) 655 { 656 bfd_set_error (bfd_error_system_call); 657 return -1; 658 } 659 result = fstat (fileno (f), statbuf); 660 if (result < 0) 661 bfd_set_error (bfd_error_system_call); 662 return result; 663} 664 665/* Returns 0 for success, nonzero for failure (in which case bfd_get_error 666 can retrieve the error code). */ 667 668int 669bfd_seek (abfd, position, direction) 670 bfd *abfd; 671 file_ptr position; 672 int direction; 673{ 674 int result; 675 FILE *f; 676 long file_position; 677 /* For the time being, a BFD may not seek to it's end. The problem 678 is that we don't easily have a way to recognize the end of an 679 element in an archive. */ 680 681 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); 682 683 if (direction == SEEK_CUR && position == 0) 684 return 0; 685 686 if ((abfd->flags & BFD_IN_MEMORY) != 0) 687 { 688 struct bfd_in_memory *bim; 689 690 bim = (struct bfd_in_memory *) abfd->iostream; 691 692 if (direction == SEEK_SET) 693 abfd->where = position; 694 else 695 abfd->where += position; 696 697 if (abfd->where > bim->size) 698 { 699 if ((abfd->direction == write_direction) || 700 (abfd->direction == both_direction)) 701 { 702 bfd_size_type newsize, oldsize; 703 oldsize = (bim->size + 127) & ~(bfd_size_type) 127; 704 bim->size = abfd->where; 705 /* Round up to cut down on memory fragmentation */ 706 newsize = (bim->size + 127) & ~(bfd_size_type) 127; 707 if (newsize > oldsize) 708 { 709 bim->buffer = bfd_realloc (bim->buffer, newsize); 710 if (bim->buffer == 0) 711 { 712 bim->size = 0; 713 return -1; 714 } 715 } 716 } 717 else 718 { 719 abfd->where = bim->size; 720 bfd_set_error (bfd_error_file_truncated); 721 return -1; 722 } 723 } 724 return 0; 725 } 726 727 if (abfd->format != bfd_archive && abfd->my_archive == 0) 728 { 729#if 0 730 /* Explanation for this code: I'm only about 95+% sure that the above 731 conditions are sufficient and that all i/o calls are properly 732 adjusting the `where' field. So this is sort of an `assert' 733 that the `where' field is correct. If we can go a while without 734 tripping the abort, we can probably safely disable this code, 735 so that the real optimizations happen. */ 736 file_ptr where_am_i_now; 737 where_am_i_now = ftell (bfd_cache_lookup (abfd)); 738 if (abfd->my_archive) 739 where_am_i_now -= abfd->origin; 740 if (where_am_i_now != abfd->where) 741 abort (); 742#endif 743 if (direction == SEEK_SET && (bfd_vma) position == abfd->where) 744 return 0; 745 } 746 else 747 { 748 /* We need something smarter to optimize access to archives. 749 Currently, anything inside an archive is read via the file 750 handle for the archive. Which means that a bfd_seek on one 751 component affects the `current position' in the archive, as 752 well as in any other component. 753 754 It might be sufficient to put a spike through the cache 755 abstraction, and look to the archive for the file position, 756 but I think we should try for something cleaner. 757 758 In the meantime, no optimization for archives. */ 759 } 760 761 f = bfd_cache_lookup (abfd); 762 file_position = position; 763 if (direction == SEEK_SET && abfd->my_archive != NULL) 764 file_position += abfd->origin; 765 766 result = fseek (f, file_position, direction); 767 if (result != 0) 768 { 769 int hold_errno = errno; 770 771 /* Force redetermination of `where' field. */ 772 bfd_tell (abfd); 773 774 /* An EINVAL error probably means that the file offset was 775 absurd. */ 776 if (hold_errno == EINVAL) 777 bfd_set_error (bfd_error_file_truncated); 778 else 779 { 780 bfd_set_error (bfd_error_system_call); 781 errno = hold_errno; 782 } 783 } 784 else 785 { 786 /* Adjust `where' field. */ 787 if (direction == SEEK_SET) 788 abfd->where = position; 789 else 790 abfd->where += position; 791 } 792 return result; 793} 794 795/** The do-it-yourself (byte) sex-change kit */ 796 797/* The middle letter e.g. get<b>short indicates Big or Little endian 798 target machine. It doesn't matter what the byte order of the host 799 machine is; these routines work for either. */ 800 801/* FIXME: Should these take a count argument? 802 Answer (gnu@cygnus.com): No, but perhaps they should be inline 803 functions in swap.h #ifdef __GNUC__. 804 Gprof them later and find out. */ 805 806/* 807FUNCTION 808 bfd_put_size 809FUNCTION 810 bfd_get_size 811 812DESCRIPTION 813 These macros as used for reading and writing raw data in 814 sections; each access (except for bytes) is vectored through 815 the target format of the BFD and mangled accordingly. The 816 mangling performs any necessary endian translations and 817 removes alignment restrictions. Note that types accepted and 818 returned by these macros are identical so they can be swapped 819 around in macros---for example, @file{libaout.h} defines <<GET_WORD>> 820 to either <<bfd_get_32>> or <<bfd_get_64>>. 821 822 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a 823 system without prototypes, the caller is responsible for making 824 sure that is true, with a cast if necessary. We don't cast 825 them in the macro definitions because that would prevent <<lint>> 826 or <<gcc -Wall>> from detecting sins such as passing a pointer. 827 To detect calling these with less than a <<bfd_vma>>, use 828 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. 829 830. 831.{* Byte swapping macros for user section data. *} 832. 833.#define bfd_put_8(abfd, val, ptr) \ 834. ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val))) 835.#define bfd_put_signed_8 \ 836. bfd_put_8 837.#define bfd_get_8(abfd, ptr) \ 838. (*(unsigned char *) (ptr) & 0xff) 839.#define bfd_get_signed_8(abfd, ptr) \ 840. (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80) 841. 842.#define bfd_put_16(abfd, val, ptr) \ 843. BFD_SEND(abfd, bfd_putx16, ((val),(ptr))) 844.#define bfd_put_signed_16 \ 845. bfd_put_16 846.#define bfd_get_16(abfd, ptr) \ 847. BFD_SEND(abfd, bfd_getx16, (ptr)) 848.#define bfd_get_signed_16(abfd, ptr) \ 849. BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) 850. 851.#define bfd_put_32(abfd, val, ptr) \ 852. BFD_SEND(abfd, bfd_putx32, ((val),(ptr))) 853.#define bfd_put_signed_32 \ 854. bfd_put_32 855.#define bfd_get_32(abfd, ptr) \ 856. BFD_SEND(abfd, bfd_getx32, (ptr)) 857.#define bfd_get_signed_32(abfd, ptr) \ 858. BFD_SEND(abfd, bfd_getx_signed_32, (ptr)) 859. 860.#define bfd_put_64(abfd, val, ptr) \ 861. BFD_SEND(abfd, bfd_putx64, ((val), (ptr))) 862.#define bfd_put_signed_64 \ 863. bfd_put_64 864.#define bfd_get_64(abfd, ptr) \ 865. BFD_SEND(abfd, bfd_getx64, (ptr)) 866.#define bfd_get_signed_64(abfd, ptr) \ 867. BFD_SEND(abfd, bfd_getx_signed_64, (ptr)) 868. 869.#define bfd_get(bits, abfd, ptr) \ 870. ( (bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \ 871. : (bits) == 16 ? bfd_get_16 (abfd, ptr) \ 872. : (bits) == 32 ? bfd_get_32 (abfd, ptr) \ 873. : (bits) == 64 ? bfd_get_64 (abfd, ptr) \ 874. : (abort (), (bfd_vma) - 1)) 875. 876.#define bfd_put(bits, abfd, val, ptr) \ 877. ( (bits) == 8 ? bfd_put_8 (abfd, val, ptr) \ 878. : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \ 879. : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \ 880. : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \ 881. : (abort (), (void) 0)) 882. 883*/ 884 885/* 886FUNCTION 887 bfd_h_put_size 888 bfd_h_get_size 889 890DESCRIPTION 891 These macros have the same function as their <<bfd_get_x>> 892 brethren, except that they are used for removing information 893 for the header records of object files. Believe it or not, 894 some object files keep their header records in big endian 895 order and their data in little endian order. 896. 897.{* Byte swapping macros for file header data. *} 898. 899.#define bfd_h_put_8(abfd, val, ptr) \ 900. bfd_put_8 (abfd, val, ptr) 901.#define bfd_h_put_signed_8(abfd, val, ptr) \ 902. bfd_put_8 (abfd, val, ptr) 903.#define bfd_h_get_8(abfd, ptr) \ 904. bfd_get_8 (abfd, ptr) 905.#define bfd_h_get_signed_8(abfd, ptr) \ 906. bfd_get_signed_8 (abfd, ptr) 907. 908.#define bfd_h_put_16(abfd, val, ptr) \ 909. BFD_SEND (abfd, bfd_h_putx16, (val, ptr)) 910.#define bfd_h_put_signed_16 \ 911. bfd_h_put_16 912.#define bfd_h_get_16(abfd, ptr) \ 913. BFD_SEND (abfd, bfd_h_getx16, (ptr)) 914.#define bfd_h_get_signed_16(abfd, ptr) \ 915. BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr)) 916. 917.#define bfd_h_put_32(abfd, val, ptr) \ 918. BFD_SEND (abfd, bfd_h_putx32, (val, ptr)) 919.#define bfd_h_put_signed_32 \ 920. bfd_h_put_32 921.#define bfd_h_get_32(abfd, ptr) \ 922. BFD_SEND (abfd, bfd_h_getx32, (ptr)) 923.#define bfd_h_get_signed_32(abfd, ptr) \ 924. BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr)) 925. 926.#define bfd_h_put_64(abfd, val, ptr) \ 927. BFD_SEND (abfd, bfd_h_putx64, (val, ptr)) 928.#define bfd_h_put_signed_64 \ 929. bfd_h_put_64 930.#define bfd_h_get_64(abfd, ptr) \ 931. BFD_SEND (abfd, bfd_h_getx64, (ptr)) 932.#define bfd_h_get_signed_64(abfd, ptr) \ 933. BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr)) 934. 935.{* Refinements on the above, which should eventually go away. Save 936. cluttering the source with (bfd_vma) and (bfd_byte *) casts. *} 937. 938.#define H_PUT_64(abfd, val, where) \ 939. bfd_h_put_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) 940. 941.#define H_PUT_32(abfd, val, where) \ 942. bfd_h_put_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) 943. 944.#define H_PUT_16(abfd, val, where) \ 945. bfd_h_put_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) 946. 947.#define H_PUT_8 bfd_h_put_8 948. 949.#define H_PUT_S64(abfd, val, where) \ 950. bfd_h_put_signed_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) 951. 952.#define H_PUT_S32(abfd, val, where) \ 953. bfd_h_put_signed_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) 954. 955.#define H_PUT_S16(abfd, val, where) \ 956. bfd_h_put_signed_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) 957. 958.#define H_PUT_S8 bfd_h_put_signed_8 959. 960.#define H_GET_64(abfd, where) \ 961. bfd_h_get_64 ((abfd), (bfd_byte *) (where)) 962. 963.#define H_GET_32(abfd, where) \ 964. bfd_h_get_32 ((abfd), (bfd_byte *) (where)) 965. 966.#define H_GET_16(abfd, where) \ 967. bfd_h_get_16 ((abfd), (bfd_byte *) (where)) 968. 969.#define H_GET_8 bfd_h_get_8 970. 971.#define H_GET_S64(abfd, where) \ 972. bfd_h_get_signed_64 ((abfd), (bfd_byte *) (where)) 973. 974.#define H_GET_S32(abfd, where) \ 975. bfd_h_get_signed_32 ((abfd), (bfd_byte *) (where)) 976. 977.#define H_GET_S16(abfd, where) \ 978. bfd_h_get_signed_16 ((abfd), (bfd_byte *) (where)) 979. 980.#define H_GET_S8 bfd_h_get_signed_8 981. 982.*/ 983 984/* Sign extension to bfd_signed_vma. */ 985#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000) 986#define COERCE32(x) \ 987 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000)) 988#define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32) 989#define COERCE64(x) \ 990 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION) 991 992bfd_vma 993bfd_getb16 (addr) 994 register const bfd_byte *addr; 995{ 996 return (addr[0] << 8) | addr[1]; 997} 998 999bfd_vma 1000bfd_getl16 (addr) 1001 register const bfd_byte *addr; 1002{ 1003 return (addr[1] << 8) | addr[0]; 1004} 1005 1006bfd_signed_vma 1007bfd_getb_signed_16 (addr) 1008 register const bfd_byte *addr; 1009{ 1010 return COERCE16((addr[0] << 8) | addr[1]); 1011} 1012 1013bfd_signed_vma 1014bfd_getl_signed_16 (addr) 1015 register const bfd_byte *addr; 1016{ 1017 return COERCE16((addr[1] << 8) | addr[0]); 1018} 1019 1020void 1021bfd_putb16 (data, addr) 1022 bfd_vma data; 1023 register bfd_byte *addr; 1024{ 1025 addr[0] = (bfd_byte) (data >> 8); 1026 addr[1] = (bfd_byte) data; 1027} 1028 1029void 1030bfd_putl16 (data, addr) 1031 bfd_vma data; 1032 register bfd_byte *addr; 1033{ 1034 addr[0] = (bfd_byte) data; 1035 addr[1] = (bfd_byte) (data >> 8); 1036} 1037 1038bfd_vma 1039bfd_getb32 (addr) 1040 register const bfd_byte *addr; 1041{ 1042 unsigned long v; 1043 1044 v = (unsigned long) addr[0] << 24; 1045 v |= (unsigned long) addr[1] << 16; 1046 v |= (unsigned long) addr[2] << 8; 1047 v |= (unsigned long) addr[3]; 1048 return (bfd_vma) v; 1049} 1050 1051bfd_vma 1052bfd_getl32 (addr) 1053 register const bfd_byte *addr; 1054{ 1055 unsigned long v; 1056 1057 v = (unsigned long) addr[0]; 1058 v |= (unsigned long) addr[1] << 8; 1059 v |= (unsigned long) addr[2] << 16; 1060 v |= (unsigned long) addr[3] << 24; 1061 return (bfd_vma) v; 1062} 1063 1064bfd_signed_vma 1065bfd_getb_signed_32 (addr) 1066 register const bfd_byte *addr; 1067{ 1068 unsigned long v; 1069 1070 v = (unsigned long) addr[0] << 24; 1071 v |= (unsigned long) addr[1] << 16; 1072 v |= (unsigned long) addr[2] << 8; 1073 v |= (unsigned long) addr[3]; 1074 return COERCE32 (v); 1075} 1076 1077bfd_signed_vma 1078bfd_getl_signed_32 (addr) 1079 register const bfd_byte *addr; 1080{ 1081 unsigned long v; 1082 1083 v = (unsigned long) addr[0]; 1084 v |= (unsigned long) addr[1] << 8; 1085 v |= (unsigned long) addr[2] << 16; 1086 v |= (unsigned long) addr[3] << 24; 1087 return COERCE32 (v); 1088} 1089 1090bfd_vma 1091bfd_getb64 (addr) 1092 register const bfd_byte *addr ATTRIBUTE_UNUSED; 1093{ 1094#ifdef BFD64 1095 bfd_vma low, high; 1096 1097 high= ((((((((addr[0]) << 8) | 1098 addr[1]) << 8) | 1099 addr[2]) << 8) | 1100 addr[3]) ); 1101 1102 low = (((((((((bfd_vma)addr[4]) << 8) | 1103 addr[5]) << 8) | 1104 addr[6]) << 8) | 1105 addr[7])); 1106 1107 return high << 32 | low; 1108#else 1109 BFD_FAIL(); 1110 return 0; 1111#endif 1112} 1113 1114bfd_vma 1115bfd_getl64 (addr) 1116 register const bfd_byte *addr ATTRIBUTE_UNUSED; 1117{ 1118#ifdef BFD64 1119 bfd_vma low, high; 1120 high= (((((((addr[7] << 8) | 1121 addr[6]) << 8) | 1122 addr[5]) << 8) | 1123 addr[4])); 1124 1125 low = ((((((((bfd_vma)addr[3] << 8) | 1126 addr[2]) << 8) | 1127 addr[1]) << 8) | 1128 addr[0]) ); 1129 1130 return high << 32 | low; 1131#else 1132 BFD_FAIL(); 1133 return 0; 1134#endif 1135 1136} 1137 1138bfd_signed_vma 1139bfd_getb_signed_64 (addr) 1140 register const bfd_byte *addr ATTRIBUTE_UNUSED; 1141{ 1142#ifdef BFD64 1143 bfd_vma low, high; 1144 1145 high= ((((((((addr[0]) << 8) | 1146 addr[1]) << 8) | 1147 addr[2]) << 8) | 1148 addr[3]) ); 1149 1150 low = (((((((((bfd_vma)addr[4]) << 8) | 1151 addr[5]) << 8) | 1152 addr[6]) << 8) | 1153 addr[7])); 1154 1155 return COERCE64(high << 32 | low); 1156#else 1157 BFD_FAIL(); 1158 return 0; 1159#endif 1160} 1161 1162bfd_signed_vma 1163bfd_getl_signed_64 (addr) 1164 register const bfd_byte *addr ATTRIBUTE_UNUSED; 1165{ 1166#ifdef BFD64 1167 bfd_vma low, high; 1168 high= (((((((addr[7] << 8) | 1169 addr[6]) << 8) | 1170 addr[5]) << 8) | 1171 addr[4])); 1172 1173 low = ((((((((bfd_vma)addr[3] << 8) | 1174 addr[2]) << 8) | 1175 addr[1]) << 8) | 1176 addr[0]) ); 1177 1178 return COERCE64(high << 32 | low); 1179#else 1180 BFD_FAIL(); 1181 return 0; 1182#endif 1183} 1184 1185void 1186bfd_putb32 (data, addr) 1187 bfd_vma data; 1188 register bfd_byte *addr; 1189{ 1190 addr[0] = (bfd_byte) (data >> 24); 1191 addr[1] = (bfd_byte) (data >> 16); 1192 addr[2] = (bfd_byte) (data >> 8); 1193 addr[3] = (bfd_byte) data; 1194} 1195 1196void 1197bfd_putl32 (data, addr) 1198 bfd_vma data; 1199 register bfd_byte *addr; 1200{ 1201 addr[0] = (bfd_byte) data; 1202 addr[1] = (bfd_byte) (data >> 8); 1203 addr[2] = (bfd_byte) (data >> 16); 1204 addr[3] = (bfd_byte) (data >> 24); 1205} 1206 1207void 1208bfd_putb64 (data, addr) 1209 bfd_vma data ATTRIBUTE_UNUSED; 1210 register bfd_byte *addr ATTRIBUTE_UNUSED; 1211{ 1212#ifdef BFD64 1213 addr[0] = (bfd_byte) (data >> (7*8)); 1214 addr[1] = (bfd_byte) (data >> (6*8)); 1215 addr[2] = (bfd_byte) (data >> (5*8)); 1216 addr[3] = (bfd_byte) (data >> (4*8)); 1217 addr[4] = (bfd_byte) (data >> (3*8)); 1218 addr[5] = (bfd_byte) (data >> (2*8)); 1219 addr[6] = (bfd_byte) (data >> (1*8)); 1220 addr[7] = (bfd_byte) (data >> (0*8)); 1221#else 1222 BFD_FAIL(); 1223#endif 1224} 1225 1226void 1227bfd_putl64 (data, addr) 1228 bfd_vma data ATTRIBUTE_UNUSED; 1229 register bfd_byte *addr ATTRIBUTE_UNUSED; 1230{ 1231#ifdef BFD64 1232 addr[7] = (bfd_byte) (data >> (7*8)); 1233 addr[6] = (bfd_byte) (data >> (6*8)); 1234 addr[5] = (bfd_byte) (data >> (5*8)); 1235 addr[4] = (bfd_byte) (data >> (4*8)); 1236 addr[3] = (bfd_byte) (data >> (3*8)); 1237 addr[2] = (bfd_byte) (data >> (2*8)); 1238 addr[1] = (bfd_byte) (data >> (1*8)); 1239 addr[0] = (bfd_byte) (data >> (0*8)); 1240#else 1241 BFD_FAIL(); 1242#endif 1243} 1244 1245void 1246bfd_put_bits (data, addr, bits, big_p) 1247 bfd_vma data; 1248 bfd_byte *addr; 1249 int bits; 1250 boolean big_p; 1251{ 1252 int i; 1253 int bytes; 1254 1255 if (bits % 8 != 0) 1256 abort (); 1257 1258 bytes = bits / 8; 1259 for (i = 0; i < bytes; i++) 1260 { 1261 int index = big_p ? bytes - i - 1 : i; 1262 1263 addr[index] = (bfd_byte) data; 1264 data >>= 8; 1265 } 1266} 1267 1268bfd_vma 1269bfd_get_bits (addr, bits, big_p) 1270 bfd_byte *addr; 1271 int bits; 1272 boolean big_p; 1273{ 1274 bfd_vma data; 1275 int i; 1276 int bytes; 1277 1278 if (bits % 8 != 0) 1279 abort (); 1280 1281 data = 0; 1282 bytes = bits / 8; 1283 for (i = 0; i < bytes; i++) 1284 { 1285 int index = big_p ? i : bytes - i - 1; 1286 1287 data = (data << 8) | addr[index]; 1288 } 1289 1290 return data; 1291} 1292 1293/* Default implementation */ 1294 1295boolean 1296_bfd_generic_get_section_contents (abfd, section, location, offset, count) 1297 bfd *abfd; 1298 sec_ptr section; 1299 PTR location; 1300 file_ptr offset; 1301 bfd_size_type count; 1302{ 1303 if (count == 0) 1304 return true; 1305 1306 if (offset + count > section->_raw_size) 1307 { 1308 bfd_set_error (bfd_error_invalid_operation); 1309 return false; 1310 } 1311 1312 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 1313 || bfd_bread (location, count, abfd) != count) 1314 return false; 1315 1316 return true; 1317} 1318 1319boolean 1320_bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count) 1321 bfd *abfd ATTRIBUTE_UNUSED; 1322 sec_ptr section ATTRIBUTE_UNUSED; 1323 bfd_window *w ATTRIBUTE_UNUSED; 1324 file_ptr offset ATTRIBUTE_UNUSED; 1325 bfd_size_type count ATTRIBUTE_UNUSED; 1326{ 1327#ifdef USE_MMAP 1328 if (count == 0) 1329 return true; 1330 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents) 1331 { 1332 /* We don't know what changes the bfd's get_section_contents 1333 method may have to make. So punt trying to map the file 1334 window, and let get_section_contents do its thing. */ 1335 /* @@ FIXME : If the internal window has a refcount of 1 and was 1336 allocated with malloc instead of mmap, just reuse it. */ 1337 bfd_free_window (w); 1338 w->i = ((bfd_window_internal *) 1339 bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal))); 1340 if (w->i == NULL) 1341 return false; 1342 w->i->data = (PTR) bfd_malloc (count); 1343 if (w->i->data == NULL) 1344 { 1345 free (w->i); 1346 w->i = NULL; 1347 return false; 1348 } 1349 w->i->mapped = 0; 1350 w->i->refcount = 1; 1351 w->size = w->i->size = count; 1352 w->data = w->i->data; 1353 return bfd_get_section_contents (abfd, section, w->data, offset, count); 1354 } 1355 if (offset + count > section->_raw_size 1356 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true) 1357 == false)) 1358 return false; 1359 return true; 1360#else 1361 abort (); 1362#endif 1363} 1364 1365/* This generic function can only be used in implementations where creating 1366 NEW sections is disallowed. It is useful in patching existing sections 1367 in read-write files, though. See other set_section_contents functions 1368 to see why it doesn't work for new sections. */ 1369boolean 1370_bfd_generic_set_section_contents (abfd, section, location, offset, count) 1371 bfd *abfd; 1372 sec_ptr section; 1373 PTR location; 1374 file_ptr offset; 1375 bfd_size_type count; 1376{ 1377 if (count == 0) 1378 return true; 1379 1380 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 1381 || bfd_bwrite (location, count, abfd) != count) 1382 return false; 1383 1384 return true; 1385} 1386 1387/* 1388INTERNAL_FUNCTION 1389 bfd_log2 1390 1391SYNOPSIS 1392 unsigned int bfd_log2 (bfd_vma x); 1393 1394DESCRIPTION 1395 Return the log base 2 of the value supplied, rounded up. E.g., an 1396 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0. 1397*/ 1398 1399unsigned int 1400bfd_log2 (x) 1401 bfd_vma x; 1402{ 1403 unsigned int result = 0; 1404 1405 while ((x = (x >> 1)) != 0) 1406 ++result; 1407 return result; 1408} 1409 1410boolean 1411bfd_generic_is_local_label_name (abfd, name) 1412 bfd *abfd; 1413 const char *name; 1414{ 1415 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; 1416 1417 return (name[0] == locals_prefix); 1418} 1419 1420/* Can be used from / for bfd_merge_private_bfd_data to check that 1421 endianness matches between input and output file. Returns 1422 true for a match, otherwise returns false and emits an error. */ 1423boolean 1424_bfd_generic_verify_endian_match (ibfd, obfd) 1425 bfd *ibfd; 1426 bfd *obfd; 1427{ 1428 if (ibfd->xvec->byteorder != obfd->xvec->byteorder 1429 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN 1430 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN) 1431 { 1432 const char *msg; 1433 1434 if (bfd_big_endian (ibfd)) 1435 msg = _("%s: compiled for a big endian system and target is little endian"); 1436 else 1437 msg = _("%s: compiled for a little endian system and target is big endian"); 1438 1439 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd)); 1440 1441 bfd_set_error (bfd_error_wrong_format); 1442 return false; 1443 } 1444 1445 return true; 1446} 1447 1448/* Give a warning at runtime if someone compiles code which calls 1449 old routines. */ 1450 1451void 1452warn_deprecated (what, file, line, func) 1453 const char *what; 1454 const char *file; 1455 int line; 1456 const char *func; 1457{ 1458 /* Poor man's tracking of functions we've already warned about. */ 1459 static size_t mask = 0; 1460 1461 if (~(size_t) func & ~mask) 1462 { 1463 /* Note: seperate sentances in order to allow 1464 for translation into other languages. */ 1465 if (func) 1466 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"), 1467 what, file, line, func); 1468 else 1469 fprintf (stderr, _("Deprecated %s called\n"), what); 1470 mask |= ~(size_t) func; 1471 } 1472} 1473