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