1 /* Assorted BFD support routines, only used internally. 2 Copyright (C) 1990-2022 Free Software Foundation, Inc. 3 Written by Cygnus Support. 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 MA 02110-1301, USA. */ 21 22 #include "sysdep.h" 23 #include "bfd.h" 24 #include "libbfd.h" 25 26 #ifndef HAVE_GETPAGESIZE 27 #define getpagesize() 2048 28 #endif 29 30 /* 31 SECTION 32 Implementation details 33 34 SUBSECTION 35 Internal functions 36 37 DESCRIPTION 38 These routines are used within BFD. 39 They are not intended for export, but are documented here for 40 completeness. 41 */ 42 43 bool 44 _bfd_bool_bfd_false (bfd *abfd ATTRIBUTE_UNUSED) 45 { 46 return false; 47 } 48 49 bool 50 _bfd_bool_bfd_asymbol_false (bfd *abfd ATTRIBUTE_UNUSED, 51 asymbol *sym ATTRIBUTE_UNUSED) 52 { 53 return false; 54 } 55 56 /* A routine which is used in target vectors for unsupported 57 operations. */ 58 59 bool 60 _bfd_bool_bfd_false_error (bfd *ignore ATTRIBUTE_UNUSED) 61 { 62 bfd_set_error (bfd_error_invalid_operation); 63 return false; 64 } 65 66 bool 67 _bfd_bool_bfd_link_false_error (bfd *abfd, 68 struct bfd_link_info *info ATTRIBUTE_UNUSED) 69 { 70 return _bfd_bool_bfd_false_error (abfd); 71 } 72 73 /* A routine which is used in target vectors for supported operations 74 which do not actually do anything. */ 75 76 bool 77 _bfd_bool_bfd_true (bfd *ignore ATTRIBUTE_UNUSED) 78 { 79 return true; 80 } 81 82 bool 83 _bfd_bool_bfd_link_true (bfd *abfd ATTRIBUTE_UNUSED, 84 struct bfd_link_info *info ATTRIBUTE_UNUSED) 85 { 86 return true; 87 } 88 89 bool 90 _bfd_bool_bfd_bfd_true (bfd *ibfd ATTRIBUTE_UNUSED, 91 bfd *obfd ATTRIBUTE_UNUSED) 92 { 93 return true; 94 } 95 96 bool 97 _bfd_bool_bfd_uint_true (bfd *abfd ATTRIBUTE_UNUSED, 98 unsigned int flags ATTRIBUTE_UNUSED) 99 { 100 return true; 101 } 102 103 bool 104 _bfd_bool_bfd_asection_bfd_asection_true (bfd *ibfd ATTRIBUTE_UNUSED, 105 asection *isec ATTRIBUTE_UNUSED, 106 bfd *obfd ATTRIBUTE_UNUSED, 107 asection *osec ATTRIBUTE_UNUSED) 108 { 109 return true; 110 } 111 112 bool 113 _bfd_bool_bfd_asymbol_bfd_asymbol_true (bfd *ibfd ATTRIBUTE_UNUSED, 114 asymbol *isym ATTRIBUTE_UNUSED, 115 bfd *obfd ATTRIBUTE_UNUSED, 116 asymbol *osym ATTRIBUTE_UNUSED) 117 { 118 return true; 119 } 120 121 bool 122 _bfd_bool_bfd_ptr_true (bfd *abfd ATTRIBUTE_UNUSED, 123 void *ptr ATTRIBUTE_UNUSED) 124 { 125 return true; 126 } 127 128 /* A routine which is used in target vectors for unsupported 129 operations which return a pointer value. */ 130 131 void * 132 _bfd_ptr_bfd_null_error (bfd *ignore ATTRIBUTE_UNUSED) 133 { 134 bfd_set_error (bfd_error_invalid_operation); 135 return NULL; 136 } 137 138 int 139 _bfd_int_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) 140 { 141 return 0; 142 } 143 144 unsigned int 145 _bfd_uint_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) 146 { 147 return 0; 148 } 149 150 long 151 _bfd_long_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) 152 { 153 return 0; 154 } 155 156 /* A routine which is used in target vectors for unsupported 157 operations which return -1 on error. */ 158 159 long 160 _bfd_long_bfd_n1_error (bfd *ignore_abfd ATTRIBUTE_UNUSED) 161 { 162 bfd_set_error (bfd_error_invalid_operation); 163 return -1; 164 } 165 166 void 167 _bfd_void_bfd (bfd *ignore ATTRIBUTE_UNUSED) 168 { 169 } 170 171 void 172 _bfd_void_bfd_link (bfd *abfd ATTRIBUTE_UNUSED, 173 struct bfd_link_info *info ATTRIBUTE_UNUSED) 174 { 175 } 176 177 void 178 _bfd_void_bfd_asection (bfd *abfd ATTRIBUTE_UNUSED, 179 asection *sec ATTRIBUTE_UNUSED) 180 { 181 } 182 183 long 184 _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, 185 asection *sec ATTRIBUTE_UNUSED) 186 { 187 return sizeof (arelent *); 188 } 189 190 long 191 _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED, 192 asection *sec ATTRIBUTE_UNUSED, 193 arelent **relptr, 194 asymbol **symbols ATTRIBUTE_UNUSED) 195 { 196 *relptr = NULL; 197 return 0; 198 } 199 200 void 201 _bfd_norelocs_set_reloc (bfd *abfd ATTRIBUTE_UNUSED, 202 asection *sec ATTRIBUTE_UNUSED, 203 arelent **relptr ATTRIBUTE_UNUSED, 204 unsigned int count ATTRIBUTE_UNUSED) 205 { 206 /* Do nothing. */ 207 } 208 209 bool 210 _bfd_nocore_core_file_matches_executable_p 211 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED, 212 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED) 213 { 214 bfd_set_error (bfd_error_invalid_operation); 215 return false; 216 } 217 218 /* Routine to handle core_file_failing_command entry point for targets 219 without core file support. */ 220 221 char * 222 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED) 223 { 224 bfd_set_error (bfd_error_invalid_operation); 225 return NULL; 226 } 227 228 /* Routine to handle core_file_failing_signal entry point for targets 229 without core file support. */ 230 231 int 232 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED) 233 { 234 bfd_set_error (bfd_error_invalid_operation); 235 return 0; 236 } 237 238 /* Routine to handle the core_file_pid entry point for targets without 239 core file support. */ 240 241 int 242 _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED) 243 { 244 bfd_set_error (bfd_error_invalid_operation); 245 return 0; 246 } 247 248 bfd_cleanup 249 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED) 250 { 251 bfd_set_error (bfd_error_wrong_format); 252 return 0; 253 } 254 255 /* Allocate memory using malloc. */ 256 257 #ifndef SSIZE_MAX 258 #define SSIZE_MAX ((size_t) -1 >> 1) 259 #endif 260 261 /* 262 INTERNAL_FUNCTION 263 bfd_malloc 264 265 SYNOPSIS 266 extern void * bfd_malloc (bfd_size_type SIZE) ATTRIBUTE_HIDDEN; 267 268 DESCRIPTION 269 Returns a pointer to an allocated block of memory that is at least 270 SIZE bytes long. If SIZE is 0 then it will be treated as if it were 271 1. If SIZE is too big then NULL will be returned. 272 273 Returns NULL upon error and sets bfd_error. 274 */ 275 void * 276 bfd_malloc (bfd_size_type size) 277 { 278 void *ptr; 279 size_t sz = (size_t) size; 280 281 if (size != sz 282 /* This is to pacify memory checkers like valgrind. */ 283 || sz > SSIZE_MAX) 284 { 285 bfd_set_error (bfd_error_no_memory); 286 return NULL; 287 } 288 289 ptr = malloc (sz ? sz : 1); 290 if (ptr == NULL) 291 bfd_set_error (bfd_error_no_memory); 292 293 return ptr; 294 } 295 296 /* 297 INTERNAL_FUNCTION 298 bfd_realloc 299 300 SYNOPSIS 301 extern void * bfd_realloc (void * MEM, bfd_size_type SIZE) ATTRIBUTE_HIDDEN; 302 303 DESCRIPTION 304 Returns a pointer to an allocated block of memory that is at least 305 SIZE bytes long. If SIZE is 0 then it will be treated as if it were 306 1. If SIZE is too big then NULL will be returned. 307 308 If MEM is not NULL then it must point to an allocated block of memory. 309 If this block is large enough then MEM may be used as the return 310 value for this function, but this is not guaranteed. 311 312 If MEM is not returned then the first N bytes in the returned block 313 will be identical to the first N bytes in region pointed to by MEM, 314 where N is the lessor of SIZE and the length of the region of memory 315 currently addressed by MEM. 316 317 Returns NULL upon error and sets bfd_error. 318 */ 319 void * 320 bfd_realloc (void *ptr, bfd_size_type size) 321 { 322 void *ret; 323 size_t sz = (size_t) size; 324 325 if (ptr == NULL) 326 return bfd_malloc (size); 327 328 if (size != sz 329 /* This is to pacify memory checkers like valgrind. */ 330 || sz > SSIZE_MAX) 331 { 332 bfd_set_error (bfd_error_no_memory); 333 return NULL; 334 } 335 336 /* The behaviour of realloc(0) is implementation defined, 337 but for this function we always allocate memory. */ 338 ret = realloc (ptr, sz ? sz : 1); 339 340 if (ret == NULL) 341 bfd_set_error (bfd_error_no_memory); 342 343 return ret; 344 } 345 346 /* 347 INTERNAL_FUNCTION 348 bfd_realloc_or_free 349 350 SYNOPSIS 351 extern void * bfd_realloc_or_free (void * MEM, bfd_size_type SIZE) ATTRIBUTE_HIDDEN; 352 353 DESCRIPTION 354 Returns a pointer to an allocated block of memory that is at least 355 SIZE bytes long. If SIZE is 0 then no memory will be allocated, 356 MEM will be freed, and NULL will be returned. This will not cause 357 bfd_error to be set. 358 359 If SIZE is too big then NULL will be returned and bfd_error will be 360 set. 361 362 If MEM is not NULL then it must point to an allocated block of memory. 363 If this block is large enough then MEM may be used as the return 364 value for this function, but this is not guaranteed. 365 366 If MEM is not returned then the first N bytes in the returned block 367 will be identical to the first N bytes in region pointed to by MEM, 368 where N is the lessor of SIZE and the length of the region of memory 369 currently addressed by MEM. 370 */ 371 void * 372 bfd_realloc_or_free (void *ptr, bfd_size_type size) 373 { 374 void *ret; 375 376 /* The behaviour of realloc(0) is implementation defined, but 377 for this function we treat it is always freeing the memory. */ 378 if (size == 0) 379 { 380 free (ptr); 381 return NULL; 382 } 383 384 ret = bfd_realloc (ptr, size); 385 if (ret == NULL) 386 free (ptr); 387 388 return ret; 389 } 390 391 /* 392 INTERNAL_FUNCTION 393 bfd_zmalloc 394 395 SYNOPSIS 396 extern void * bfd_zmalloc (bfd_size_type SIZE) ATTRIBUTE_HIDDEN; 397 398 DESCRIPTION 399 Returns a pointer to an allocated block of memory that is at least 400 SIZE bytes long. If SIZE is 0 then it will be treated as if it were 401 1. If SIZE is too big then NULL will be returned. 402 403 Returns NULL upon error and sets bfd_error. 404 405 If NULL is not returned then the allocated block of memory will 406 have been cleared. 407 */ 408 void * 409 bfd_zmalloc (bfd_size_type size) 410 { 411 void *ptr = bfd_malloc (size); 412 413 if (ptr != NULL) 414 memset (ptr, 0, size ? (size_t) size : 1); 415 416 return ptr; 417 } 418 419 /* 420 INTERNAL_FUNCTION 421 bfd_write_bigendian_4byte_int 422 423 SYNOPSIS 424 bool bfd_write_bigendian_4byte_int (bfd *, unsigned int); 425 426 DESCRIPTION 427 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big 428 endian order regardless of what else is going on. This is useful in 429 archives. 430 431 */ 432 bool 433 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i) 434 { 435 bfd_byte buffer[4]; 436 bfd_putb32 ((bfd_vma) i, buffer); 437 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4; 438 } 439 440 441 /** The do-it-yourself (byte) sex-change kit */ 442 443 /* The middle letter e.g. get<b>short indicates Big or Little endian 444 target machine. It doesn't matter what the byte order of the host 445 machine is; these routines work for either. */ 446 447 /* FIXME: Should these take a count argument? 448 Answer (gnu@cygnus.com): No, but perhaps they should be inline 449 functions in swap.h #ifdef __GNUC__. 450 Gprof them later and find out. */ 451 452 /* 453 FUNCTION 454 bfd_put_size 455 FUNCTION 456 bfd_get_size 457 458 DESCRIPTION 459 These macros as used for reading and writing raw data in 460 sections; each access (except for bytes) is vectored through 461 the target format of the BFD and mangled accordingly. The 462 mangling performs any necessary endian translations and 463 removes alignment restrictions. Note that types accepted and 464 returned by these macros are identical so they can be swapped 465 around in macros---for example, @file{libaout.h} defines <<GET_WORD>> 466 to either <<bfd_get_32>> or <<bfd_get_64>>. 467 468 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a 469 system without prototypes, the caller is responsible for making 470 sure that is true, with a cast if necessary. We don't cast 471 them in the macro definitions because that would prevent <<lint>> 472 or <<gcc -Wall>> from detecting sins such as passing a pointer. 473 To detect calling these with less than a <<bfd_vma>>, use 474 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. 475 476 . 477 .{* Byte swapping macros for user section data. *} 478 . 479 .#define bfd_put_8(abfd, val, ptr) \ 480 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff)) 481 .#define bfd_put_signed_8 \ 482 . bfd_put_8 483 .#define bfd_get_8(abfd, ptr) \ 484 . ((bfd_vma) *(const unsigned char *) (ptr) & 0xff) 485 .#define bfd_get_signed_8(abfd, ptr) \ 486 . ((((bfd_signed_vma) *(const unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80) 487 . 488 .#define bfd_put_16(abfd, val, ptr) \ 489 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr))) 490 .#define bfd_put_signed_16 \ 491 . bfd_put_16 492 .#define bfd_get_16(abfd, ptr) \ 493 . BFD_SEND (abfd, bfd_getx16, (ptr)) 494 .#define bfd_get_signed_16(abfd, ptr) \ 495 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) 496 . 497 .#define bfd_put_24(abfd, val, ptr) \ 498 . do \ 499 . if (bfd_big_endian (abfd)) \ 500 . bfd_putb24 ((val), (ptr)); \ 501 . else \ 502 . bfd_putl24 ((val), (ptr)); \ 503 . while (0) 504 . 505 .bfd_vma bfd_getb24 (const void *p); 506 .bfd_vma bfd_getl24 (const void *p); 507 . 508 .#define bfd_get_24(abfd, ptr) \ 509 . (bfd_big_endian (abfd) ? bfd_getb24 (ptr) : bfd_getl24 (ptr)) 510 . 511 .#define bfd_put_32(abfd, val, ptr) \ 512 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr))) 513 .#define bfd_put_signed_32 \ 514 . bfd_put_32 515 .#define bfd_get_32(abfd, ptr) \ 516 . BFD_SEND (abfd, bfd_getx32, (ptr)) 517 .#define bfd_get_signed_32(abfd, ptr) \ 518 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr)) 519 . 520 .#define bfd_put_64(abfd, val, ptr) \ 521 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr))) 522 .#define bfd_put_signed_64 \ 523 . bfd_put_64 524 .#define bfd_get_64(abfd, ptr) \ 525 . BFD_SEND (abfd, bfd_getx64, (ptr)) 526 .#define bfd_get_signed_64(abfd, ptr) \ 527 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr)) 528 . 529 .#define bfd_get(bits, abfd, ptr) \ 530 . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \ 531 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \ 532 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \ 533 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \ 534 . : (abort (), (bfd_vma) - 1)) 535 . 536 .#define bfd_put(bits, abfd, val, ptr) \ 537 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \ 538 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \ 539 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \ 540 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \ 541 . : (abort (), (void) 0)) 542 . 543 */ 544 545 /* 546 FUNCTION 547 bfd_h_put_size 548 bfd_h_get_size 549 550 DESCRIPTION 551 These macros have the same function as their <<bfd_get_x>> 552 brethren, except that they are used for removing information 553 for the header records of object files. Believe it or not, 554 some object files keep their header records in big endian 555 order and their data in little endian order. 556 . 557 .{* Byte swapping macros for file header data. *} 558 . 559 .#define bfd_h_put_8(abfd, val, ptr) \ 560 . bfd_put_8 (abfd, val, ptr) 561 .#define bfd_h_put_signed_8(abfd, val, ptr) \ 562 . bfd_put_8 (abfd, val, ptr) 563 .#define bfd_h_get_8(abfd, ptr) \ 564 . bfd_get_8 (abfd, ptr) 565 .#define bfd_h_get_signed_8(abfd, ptr) \ 566 . bfd_get_signed_8 (abfd, ptr) 567 . 568 .#define bfd_h_put_16(abfd, val, ptr) \ 569 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr)) 570 .#define bfd_h_put_signed_16 \ 571 . bfd_h_put_16 572 .#define bfd_h_get_16(abfd, ptr) \ 573 . BFD_SEND (abfd, bfd_h_getx16, (ptr)) 574 .#define bfd_h_get_signed_16(abfd, ptr) \ 575 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr)) 576 . 577 .#define bfd_h_put_32(abfd, val, ptr) \ 578 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr)) 579 .#define bfd_h_put_signed_32 \ 580 . bfd_h_put_32 581 .#define bfd_h_get_32(abfd, ptr) \ 582 . BFD_SEND (abfd, bfd_h_getx32, (ptr)) 583 .#define bfd_h_get_signed_32(abfd, ptr) \ 584 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr)) 585 . 586 .#define bfd_h_put_64(abfd, val, ptr) \ 587 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr)) 588 .#define bfd_h_put_signed_64 \ 589 . bfd_h_put_64 590 .#define bfd_h_get_64(abfd, ptr) \ 591 . BFD_SEND (abfd, bfd_h_getx64, (ptr)) 592 .#define bfd_h_get_signed_64(abfd, ptr) \ 593 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr)) 594 . 595 .{* Aliases for the above, which should eventually go away. *} 596 . 597 .#define H_PUT_64 bfd_h_put_64 598 .#define H_PUT_32 bfd_h_put_32 599 .#define H_PUT_16 bfd_h_put_16 600 .#define H_PUT_8 bfd_h_put_8 601 .#define H_PUT_S64 bfd_h_put_signed_64 602 .#define H_PUT_S32 bfd_h_put_signed_32 603 .#define H_PUT_S16 bfd_h_put_signed_16 604 .#define H_PUT_S8 bfd_h_put_signed_8 605 .#define H_GET_64 bfd_h_get_64 606 .#define H_GET_32 bfd_h_get_32 607 .#define H_GET_16 bfd_h_get_16 608 .#define H_GET_8 bfd_h_get_8 609 .#define H_GET_S64 bfd_h_get_signed_64 610 .#define H_GET_S32 bfd_h_get_signed_32 611 .#define H_GET_S16 bfd_h_get_signed_16 612 .#define H_GET_S8 bfd_h_get_signed_8 613 . 614 .*/ 615 616 /* Sign extension to bfd_signed_vma. */ 617 #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000) 618 #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000) 619 #define COERCE64(x) \ 620 (((uint64_t) (x) ^ ((uint64_t) 1 << 63)) - ((uint64_t) 1 << 63)) 621 622 bfd_vma 623 bfd_getb16 (const void *p) 624 { 625 const bfd_byte *addr = (const bfd_byte *) p; 626 return (addr[0] << 8) | addr[1]; 627 } 628 629 bfd_vma 630 bfd_getl16 (const void *p) 631 { 632 const bfd_byte *addr = (const bfd_byte *) p; 633 return (addr[1] << 8) | addr[0]; 634 } 635 636 bfd_signed_vma 637 bfd_getb_signed_16 (const void *p) 638 { 639 const bfd_byte *addr = (const bfd_byte *) p; 640 return COERCE16 ((addr[0] << 8) | addr[1]); 641 } 642 643 bfd_signed_vma 644 bfd_getl_signed_16 (const void *p) 645 { 646 const bfd_byte *addr = (const bfd_byte *) p; 647 return COERCE16 ((addr[1] << 8) | addr[0]); 648 } 649 650 void 651 bfd_putb16 (bfd_vma data, void *p) 652 { 653 bfd_byte *addr = (bfd_byte *) p; 654 addr[0] = (data >> 8) & 0xff; 655 addr[1] = data & 0xff; 656 } 657 658 void 659 bfd_putl16 (bfd_vma data, void *p) 660 { 661 bfd_byte *addr = (bfd_byte *) p; 662 addr[0] = data & 0xff; 663 addr[1] = (data >> 8) & 0xff; 664 } 665 666 void 667 bfd_putb24 (bfd_vma data, void *p) 668 { 669 bfd_byte *addr = (bfd_byte *) p; 670 addr[0] = (data >> 16) & 0xff; 671 addr[1] = (data >> 8) & 0xff; 672 addr[2] = data & 0xff; 673 } 674 675 void 676 bfd_putl24 (bfd_vma data, void *p) 677 { 678 bfd_byte *addr = (bfd_byte *) p; 679 addr[0] = data & 0xff; 680 addr[1] = (data >> 8) & 0xff; 681 addr[2] = (data >> 16) & 0xff; 682 } 683 684 bfd_vma 685 bfd_getb24 (const void *p) 686 { 687 const bfd_byte *addr = (const bfd_byte *) p; 688 unsigned long v; 689 690 v = (unsigned long) addr[0] << 16; 691 v |= (unsigned long) addr[1] << 8; 692 v |= (unsigned long) addr[2]; 693 return v; 694 } 695 696 bfd_vma 697 bfd_getl24 (const void *p) 698 { 699 const bfd_byte *addr = (const bfd_byte *) p; 700 unsigned long v; 701 702 v = (unsigned long) addr[0]; 703 v |= (unsigned long) addr[1] << 8; 704 v |= (unsigned long) addr[2] << 16; 705 return v; 706 } 707 708 bfd_vma 709 bfd_getb32 (const void *p) 710 { 711 const bfd_byte *addr = (const bfd_byte *) p; 712 unsigned long v; 713 714 v = (unsigned long) addr[0] << 24; 715 v |= (unsigned long) addr[1] << 16; 716 v |= (unsigned long) addr[2] << 8; 717 v |= (unsigned long) addr[3]; 718 return v; 719 } 720 721 bfd_vma 722 bfd_getl32 (const void *p) 723 { 724 const bfd_byte *addr = (const bfd_byte *) p; 725 unsigned long v; 726 727 v = (unsigned long) addr[0]; 728 v |= (unsigned long) addr[1] << 8; 729 v |= (unsigned long) addr[2] << 16; 730 v |= (unsigned long) addr[3] << 24; 731 return v; 732 } 733 734 bfd_signed_vma 735 bfd_getb_signed_32 (const void *p) 736 { 737 const bfd_byte *addr = (const bfd_byte *) p; 738 unsigned long v; 739 740 v = (unsigned long) addr[0] << 24; 741 v |= (unsigned long) addr[1] << 16; 742 v |= (unsigned long) addr[2] << 8; 743 v |= (unsigned long) addr[3]; 744 return COERCE32 (v); 745 } 746 747 bfd_signed_vma 748 bfd_getl_signed_32 (const void *p) 749 { 750 const bfd_byte *addr = (const bfd_byte *) p; 751 unsigned long v; 752 753 v = (unsigned long) addr[0]; 754 v |= (unsigned long) addr[1] << 8; 755 v |= (unsigned long) addr[2] << 16; 756 v |= (unsigned long) addr[3] << 24; 757 return COERCE32 (v); 758 } 759 760 uint64_t 761 bfd_getb64 (const void *p) 762 { 763 const bfd_byte *addr = (const bfd_byte *) p; 764 uint64_t v; 765 766 v = addr[0]; v <<= 8; 767 v |= addr[1]; v <<= 8; 768 v |= addr[2]; v <<= 8; 769 v |= addr[3]; v <<= 8; 770 v |= addr[4]; v <<= 8; 771 v |= addr[5]; v <<= 8; 772 v |= addr[6]; v <<= 8; 773 v |= addr[7]; 774 775 return v; 776 } 777 778 uint64_t 779 bfd_getl64 (const void *p) 780 { 781 const bfd_byte *addr = (const bfd_byte *) p; 782 uint64_t v; 783 784 v = addr[7]; v <<= 8; 785 v |= addr[6]; v <<= 8; 786 v |= addr[5]; v <<= 8; 787 v |= addr[4]; v <<= 8; 788 v |= addr[3]; v <<= 8; 789 v |= addr[2]; v <<= 8; 790 v |= addr[1]; v <<= 8; 791 v |= addr[0]; 792 793 return v; 794 } 795 796 int64_t 797 bfd_getb_signed_64 (const void *p) 798 { 799 const bfd_byte *addr = (const bfd_byte *) p; 800 uint64_t v; 801 802 v = addr[0]; v <<= 8; 803 v |= addr[1]; v <<= 8; 804 v |= addr[2]; v <<= 8; 805 v |= addr[3]; v <<= 8; 806 v |= addr[4]; v <<= 8; 807 v |= addr[5]; v <<= 8; 808 v |= addr[6]; v <<= 8; 809 v |= addr[7]; 810 811 return COERCE64 (v); 812 } 813 814 int64_t 815 bfd_getl_signed_64 (const void *p) 816 { 817 const bfd_byte *addr = (const bfd_byte *) p; 818 uint64_t v; 819 820 v = addr[7]; v <<= 8; 821 v |= addr[6]; v <<= 8; 822 v |= addr[5]; v <<= 8; 823 v |= addr[4]; v <<= 8; 824 v |= addr[3]; v <<= 8; 825 v |= addr[2]; v <<= 8; 826 v |= addr[1]; v <<= 8; 827 v |= addr[0]; 828 829 return COERCE64 (v); 830 } 831 832 void 833 bfd_putb32 (bfd_vma data, void *p) 834 { 835 bfd_byte *addr = (bfd_byte *) p; 836 addr[0] = (data >> 24) & 0xff; 837 addr[1] = (data >> 16) & 0xff; 838 addr[2] = (data >> 8) & 0xff; 839 addr[3] = data & 0xff; 840 } 841 842 void 843 bfd_putl32 (bfd_vma data, void *p) 844 { 845 bfd_byte *addr = (bfd_byte *) p; 846 addr[0] = data & 0xff; 847 addr[1] = (data >> 8) & 0xff; 848 addr[2] = (data >> 16) & 0xff; 849 addr[3] = (data >> 24) & 0xff; 850 } 851 852 void 853 bfd_putb64 (uint64_t data, void *p) 854 { 855 bfd_byte *addr = (bfd_byte *) p; 856 addr[0] = (data >> (7*8)) & 0xff; 857 addr[1] = (data >> (6*8)) & 0xff; 858 addr[2] = (data >> (5*8)) & 0xff; 859 addr[3] = (data >> (4*8)) & 0xff; 860 addr[4] = (data >> (3*8)) & 0xff; 861 addr[5] = (data >> (2*8)) & 0xff; 862 addr[6] = (data >> (1*8)) & 0xff; 863 addr[7] = (data >> (0*8)) & 0xff; 864 } 865 866 void 867 bfd_putl64 (uint64_t data, void *p) 868 { 869 bfd_byte *addr = (bfd_byte *) p; 870 addr[7] = (data >> (7*8)) & 0xff; 871 addr[6] = (data >> (6*8)) & 0xff; 872 addr[5] = (data >> (5*8)) & 0xff; 873 addr[4] = (data >> (4*8)) & 0xff; 874 addr[3] = (data >> (3*8)) & 0xff; 875 addr[2] = (data >> (2*8)) & 0xff; 876 addr[1] = (data >> (1*8)) & 0xff; 877 addr[0] = (data >> (0*8)) & 0xff; 878 } 879 880 void 881 bfd_put_bits (uint64_t data, void *p, int bits, bool big_p) 882 { 883 bfd_byte *addr = (bfd_byte *) p; 884 int i; 885 int bytes; 886 887 if (bits % 8 != 0) 888 abort (); 889 890 bytes = bits / 8; 891 for (i = 0; i < bytes; i++) 892 { 893 int addr_index = big_p ? bytes - i - 1 : i; 894 895 addr[addr_index] = data & 0xff; 896 data >>= 8; 897 } 898 } 899 900 uint64_t 901 bfd_get_bits (const void *p, int bits, bool big_p) 902 { 903 const bfd_byte *addr = (const bfd_byte *) p; 904 uint64_t data; 905 int i; 906 int bytes; 907 908 if (bits % 8 != 0) 909 abort (); 910 911 data = 0; 912 bytes = bits / 8; 913 for (i = 0; i < bytes; i++) 914 { 915 int addr_index = big_p ? i : bytes - i - 1; 916 917 data = (data << 8) | addr[addr_index]; 918 } 919 920 return data; 921 } 922 923 /* Default implementation */ 924 925 bool 926 _bfd_generic_get_section_contents (bfd *abfd, 927 sec_ptr section, 928 void *location, 929 file_ptr offset, 930 bfd_size_type count) 931 { 932 bfd_size_type sz; 933 if (count == 0) 934 return true; 935 936 if (section->compress_status != COMPRESS_SECTION_NONE) 937 { 938 _bfd_error_handler 939 /* xgettext:c-format */ 940 (_("%pB: unable to get decompressed section %pA"), 941 abfd, section); 942 bfd_set_error (bfd_error_invalid_operation); 943 return false; 944 } 945 946 /* We do allow reading of a section after bfd_final_link has 947 written the contents out to disk. In that situation, rawsize is 948 just a stale version of size, so ignore it. Otherwise we must be 949 reading an input section, where rawsize, if different to size, 950 is the on-disk size. */ 951 if (abfd->direction != write_direction && section->rawsize != 0) 952 sz = section->rawsize; 953 else 954 sz = section->size; 955 if (offset + count < count 956 || offset + count > sz 957 || (abfd->my_archive != NULL 958 && !bfd_is_thin_archive (abfd->my_archive) 959 && ((ufile_ptr) section->filepos + offset + count 960 > arelt_size (abfd)))) 961 { 962 bfd_set_error (bfd_error_invalid_operation); 963 return false; 964 } 965 966 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 967 || bfd_bread (location, count, abfd) != count) 968 return false; 969 970 return true; 971 } 972 973 bool 974 _bfd_generic_get_section_contents_in_window 975 (bfd *abfd ATTRIBUTE_UNUSED, 976 sec_ptr section ATTRIBUTE_UNUSED, 977 bfd_window *w ATTRIBUTE_UNUSED, 978 file_ptr offset ATTRIBUTE_UNUSED, 979 bfd_size_type count ATTRIBUTE_UNUSED) 980 { 981 #ifdef USE_MMAP 982 bfd_size_type sz; 983 984 if (count == 0) 985 return true; 986 if (abfd->xvec->_bfd_get_section_contents 987 != _bfd_generic_get_section_contents) 988 { 989 /* We don't know what changes the bfd's get_section_contents 990 method may have to make. So punt trying to map the file 991 window, and let get_section_contents do its thing. */ 992 /* @@ FIXME : If the internal window has a refcount of 1 and was 993 allocated with malloc instead of mmap, just reuse it. */ 994 bfd_free_window (w); 995 w->i = bfd_zmalloc (sizeof (bfd_window_internal)); 996 if (w->i == NULL) 997 return false; 998 w->i->data = bfd_malloc (count); 999 if (w->i->data == NULL) 1000 { 1001 free (w->i); 1002 w->i = NULL; 1003 return false; 1004 } 1005 w->i->mapped = 0; 1006 w->i->refcount = 1; 1007 w->size = w->i->size = count; 1008 w->data = w->i->data; 1009 return bfd_get_section_contents (abfd, section, w->data, offset, count); 1010 } 1011 if (abfd->direction != write_direction && section->rawsize != 0) 1012 sz = section->rawsize; 1013 else 1014 sz = section->size; 1015 if (offset + count < count 1016 || offset + count > sz 1017 || (abfd->my_archive != NULL 1018 && !bfd_is_thin_archive (abfd->my_archive) 1019 && ((ufile_ptr) section->filepos + offset + count 1020 > arelt_size (abfd))) 1021 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w, 1022 true)) 1023 return false; 1024 return true; 1025 #else 1026 abort (); 1027 #endif 1028 } 1029 1030 /* This generic function can only be used in implementations where creating 1031 NEW sections is disallowed. It is useful in patching existing sections 1032 in read-write files, though. See other set_section_contents functions 1033 to see why it doesn't work for new sections. */ 1034 bool 1035 _bfd_generic_set_section_contents (bfd *abfd, 1036 sec_ptr section, 1037 const void *location, 1038 file_ptr offset, 1039 bfd_size_type count) 1040 { 1041 if (count == 0) 1042 return true; 1043 1044 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 1045 || bfd_bwrite (location, count, abfd) != count) 1046 return false; 1047 1048 return true; 1049 } 1050 1051 /* 1052 INTERNAL_FUNCTION 1053 bfd_log2 1054 1055 SYNOPSIS 1056 unsigned int bfd_log2 (bfd_vma x); 1057 1058 DESCRIPTION 1059 Return the log base 2 of the value supplied, rounded up. E.g., an 1060 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0. 1061 */ 1062 1063 unsigned int 1064 bfd_log2 (bfd_vma x) 1065 { 1066 unsigned int result = 0; 1067 1068 if (x <= 1) 1069 return result; 1070 --x; 1071 do 1072 ++result; 1073 while ((x >>= 1) != 0); 1074 return result; 1075 } 1076 1077 bool 1078 bfd_generic_is_local_label_name (bfd *abfd, const char *name) 1079 { 1080 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; 1081 1082 return name[0] == locals_prefix; 1083 } 1084 1085 /* Give a warning at runtime if someone compiles code which calls 1086 old routines. */ 1087 1088 void 1089 _bfd_warn_deprecated (const char *what, 1090 const char *file, 1091 int line, 1092 const char *func) 1093 { 1094 /* Poor man's tracking of functions we've already warned about. */ 1095 static size_t mask = 0; 1096 1097 if (~(size_t) func & ~mask) 1098 { 1099 fflush (stdout); 1100 /* Note: separate sentences in order to allow 1101 for translation into other languages. */ 1102 if (func) 1103 /* xgettext:c-format */ 1104 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"), 1105 what, file, line, func); 1106 else 1107 fprintf (stderr, _("Deprecated %s called\n"), what); 1108 fflush (stderr); 1109 mask |= ~(size_t) func; 1110 } 1111 } 1112 1113 /* Helper function for reading uleb128 encoded data. */ 1114 1115 bfd_vma 1116 _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED, 1117 bfd_byte *buf, 1118 unsigned int *bytes_read_ptr) 1119 { 1120 bfd_vma result; 1121 unsigned int num_read; 1122 unsigned int shift; 1123 unsigned char byte; 1124 1125 result = 0; 1126 shift = 0; 1127 num_read = 0; 1128 do 1129 { 1130 byte = bfd_get_8 (abfd, buf); 1131 buf++; 1132 num_read++; 1133 if (shift < 8 * sizeof (result)) 1134 { 1135 result |= (((bfd_vma) byte & 0x7f) << shift); 1136 shift += 7; 1137 } 1138 } 1139 while (byte & 0x80); 1140 *bytes_read_ptr = num_read; 1141 return result; 1142 } 1143 1144 /* Read in a LEB128 encoded value from ABFD starting at *PTR. 1145 If SIGN is true, return a signed LEB128 value. 1146 *PTR is incremented by the number of bytes read. 1147 No bytes will be read at address END or beyond. */ 1148 1149 bfd_vma 1150 _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED, 1151 bfd_byte **ptr, 1152 bool sign, 1153 const bfd_byte * const end) 1154 { 1155 bfd_vma result = 0; 1156 unsigned int shift = 0; 1157 unsigned char byte = 0; 1158 bfd_byte *data = *ptr; 1159 1160 while (data < end) 1161 { 1162 byte = bfd_get_8 (abfd, data); 1163 data++; 1164 if (shift < 8 * sizeof (result)) 1165 { 1166 result |= ((bfd_vma) (byte & 0x7f)) << shift; 1167 shift += 7; 1168 } 1169 if ((byte & 0x80) == 0) 1170 break; 1171 } 1172 1173 *ptr = data; 1174 1175 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40)) 1176 result |= -((bfd_vma) 1 << shift); 1177 1178 return result; 1179 } 1180 1181 /* Helper function for reading sleb128 encoded data. */ 1182 1183 bfd_signed_vma 1184 _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED, 1185 bfd_byte *buf, 1186 unsigned int *bytes_read_ptr) 1187 { 1188 bfd_vma result; 1189 unsigned int shift; 1190 unsigned int num_read; 1191 unsigned char byte; 1192 1193 result = 0; 1194 shift = 0; 1195 num_read = 0; 1196 do 1197 { 1198 byte = bfd_get_8 (abfd, buf); 1199 buf ++; 1200 num_read ++; 1201 if (shift < 8 * sizeof (result)) 1202 { 1203 result |= (((bfd_vma) byte & 0x7f) << shift); 1204 shift += 7; 1205 } 1206 } 1207 while (byte & 0x80); 1208 if (shift < 8 * sizeof (result) && (byte & 0x40)) 1209 result |= (((bfd_vma) -1) << shift); 1210 *bytes_read_ptr = num_read; 1211 return result; 1212 } 1213 1214 /* Write VAL in uleb128 format to P. 1215 END indicates the last byte of allocated space for the uleb128 value to fit 1216 in. 1217 Return a pointer to the byte following the last byte that was written, or 1218 NULL if the uleb128 value does not fit in the allocated space between P and 1219 END. */ 1220 bfd_byte * 1221 _bfd_write_unsigned_leb128 (bfd_byte *p, bfd_byte *end, bfd_vma val) 1222 { 1223 bfd_byte c; 1224 do 1225 { 1226 if (p > end) 1227 return NULL; 1228 c = val & 0x7f; 1229 val >>= 7; 1230 if (val) 1231 c |= 0x80; 1232 *(p++) = c; 1233 } 1234 while (val); 1235 return p; 1236 } 1237 1238 bool 1239 _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED, 1240 asection *isec ATTRIBUTE_UNUSED, 1241 bfd *obfd ATTRIBUTE_UNUSED, 1242 asection *osec ATTRIBUTE_UNUSED, 1243 struct bfd_link_info *link_info ATTRIBUTE_UNUSED) 1244 { 1245 return true; 1246 } 1247