| 1 | /* Assorted BFD support routines, only used internally. |
| 2 | Copyright 1990, 91, 92, 93, 94 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | #include "bfd.h" |
| 22 | #include "sysdep.h" |
| 23 | #include "libbfd.h" |
| 24 | |
| 25 | /* |
| 26 | SECTION |
| 27 | Internal functions |
| 28 | |
| 29 | DESCRIPTION |
| 30 | These routines are used within BFD. |
| 31 | They are not intended for export, but are documented here for |
| 32 | completeness. |
| 33 | */ |
| 34 | |
| 35 | /* A routine which is used in target vectors for unsupported |
| 36 | operations. */ |
| 37 | |
| 38 | /*ARGSUSED*/ |
| 39 | boolean |
| 40 | bfd_false (ignore) |
| 41 | bfd *ignore; |
| 42 | { |
| 43 | bfd_set_error (bfd_error_invalid_operation); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | /* A routine which is used in target vectors for supported operations |
| 48 | which do not actually do anything. */ |
| 49 | |
| 50 | /*ARGSUSED*/ |
| 51 | boolean |
| 52 | bfd_true (ignore) |
| 53 | bfd *ignore; |
| 54 | { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | /* A routine which is used in target vectors for unsupported |
| 59 | operations which return a pointer value. */ |
| 60 | |
| 61 | /*ARGSUSED*/ |
| 62 | PTR |
| 63 | bfd_nullvoidptr (ignore) |
| 64 | bfd *ignore; |
| 65 | { |
| 66 | bfd_set_error (bfd_error_invalid_operation); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | /*ARGSUSED*/ |
| 71 | int |
| 72 | bfd_0 (ignore) |
| 73 | bfd *ignore; |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /*ARGSUSED*/ |
| 79 | unsigned int |
| 80 | bfd_0u (ignore) |
| 81 | bfd *ignore; |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /*ARGUSED*/ |
| 87 | long |
| 88 | bfd_0l (ignore) |
| 89 | bfd *ignore; |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /* A routine which is used in target vectors for unsupported |
| 95 | operations which return -1 on error. */ |
| 96 | |
| 97 | /*ARGSUSED*/ |
| 98 | long |
| 99 | _bfd_n1 (ignore_abfd) |
| 100 | bfd *ignore_abfd; |
| 101 | { |
| 102 | bfd_set_error (bfd_error_invalid_operation); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | /*ARGSUSED*/ |
| 107 | void |
| 108 | bfd_void (ignore) |
| 109 | bfd *ignore; |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | /*ARGSUSED*/ |
| 114 | boolean |
| 115 | _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd) |
| 116 | bfd *ignore_core_bfd; |
| 117 | bfd *ignore_exec_bfd; |
| 118 | { |
| 119 | bfd_set_error (bfd_error_invalid_operation); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | /* Routine to handle core_file_failing_command entry point for targets |
| 124 | without core file support. */ |
| 125 | |
| 126 | /*ARGSUSED*/ |
| 127 | char * |
| 128 | _bfd_nocore_core_file_failing_command (ignore_abfd) |
| 129 | bfd *ignore_abfd; |
| 130 | { |
| 131 | bfd_set_error (bfd_error_invalid_operation); |
| 132 | return (char *)NULL; |
| 133 | } |
| 134 | |
| 135 | /* Routine to handle core_file_failing_signal entry point for targets |
| 136 | without core file support. */ |
| 137 | |
| 138 | /*ARGSUSED*/ |
| 139 | int |
| 140 | _bfd_nocore_core_file_failing_signal (ignore_abfd) |
| 141 | bfd *ignore_abfd; |
| 142 | { |
| 143 | bfd_set_error (bfd_error_invalid_operation); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /*ARGSUSED*/ |
| 148 | bfd_target * |
| 149 | _bfd_dummy_target (ignore_abfd) |
| 150 | bfd *ignore_abfd; |
| 151 | { |
| 152 | return 0; |
| 153 | } |
| 154 | \f |
| 155 | |
| 156 | #ifndef bfd_zmalloc |
| 157 | /* allocate and clear storage */ |
| 158 | |
| 159 | char * |
| 160 | bfd_zmalloc (size) |
| 161 | bfd_size_type size; |
| 162 | { |
| 163 | char *ptr = (char *) malloc ((size_t) size); |
| 164 | |
| 165 | if (ptr && size) |
| 166 | memset(ptr, 0, (size_t) size); |
| 167 | |
| 168 | return ptr; |
| 169 | } |
| 170 | #endif /* bfd_zmalloc */ |
| 171 | \f |
| 172 | /* Some IO code */ |
| 173 | |
| 174 | |
| 175 | /* Note that archive entries don't have streams; they share their parent's. |
| 176 | This allows someone to play with the iostream behind BFD's back. |
| 177 | |
| 178 | Also, note that the origin pointer points to the beginning of a file's |
| 179 | contents (0 for non-archive elements). For archive entries this is the |
| 180 | first octet in the file, NOT the beginning of the archive header. */ |
| 181 | |
| 182 | static |
| 183 | int |
| 184 | real_read (where, a,b, file) |
| 185 | PTR where; |
| 186 | int a; |
| 187 | int b; |
| 188 | FILE *file; |
| 189 | { |
| 190 | return fread(where, a,b,file); |
| 191 | } |
| 192 | |
| 193 | /* Return value is amount read (FIXME: how are errors and end of file dealt |
| 194 | with? We never call bfd_set_error, which is probably a mistake). */ |
| 195 | |
| 196 | bfd_size_type |
| 197 | bfd_read (ptr, size, nitems, abfd) |
| 198 | PTR ptr; |
| 199 | bfd_size_type size; |
| 200 | bfd_size_type nitems; |
| 201 | bfd *abfd; |
| 202 | { |
| 203 | int nread; |
| 204 | nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd)); |
| 205 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
| 206 | if (nread > 0) |
| 207 | abfd->where += nread; |
| 208 | #endif |
| 209 | |
| 210 | /* Set bfd_error if we did not read as much data as we expected. |
| 211 | |
| 212 | If the read failed due to an error set the bfd_error_system_call, |
| 213 | else set bfd_error_file_truncated. |
| 214 | |
| 215 | A BFD backend may wish to override bfd_error_file_truncated to |
| 216 | provide something more useful (eg. no_symbols or wrong_format). */ |
| 217 | if (nread < (int)(size * nitems)) |
| 218 | { |
| 219 | if (ferror (bfd_cache_lookup (abfd))) |
| 220 | bfd_set_error (bfd_error_system_call); |
| 221 | else |
| 222 | bfd_set_error (bfd_error_file_truncated); |
| 223 | } |
| 224 | |
| 225 | return nread; |
| 226 | } |
| 227 | |
| 228 | bfd_size_type |
| 229 | bfd_write (ptr, size, nitems, abfd) |
| 230 | CONST PTR ptr; |
| 231 | bfd_size_type size; |
| 232 | bfd_size_type nitems; |
| 233 | bfd *abfd; |
| 234 | { |
| 235 | int nwrote = fwrite (ptr, 1, (int) (size * nitems), bfd_cache_lookup (abfd)); |
| 236 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
| 237 | if (nwrote > 0) |
| 238 | abfd->where += nwrote; |
| 239 | #endif |
| 240 | if (nwrote != size * nitems) |
| 241 | { |
| 242 | #ifdef ENOSPC |
| 243 | if (nwrote >= 0) |
| 244 | errno = ENOSPC; |
| 245 | #endif |
| 246 | bfd_set_error (bfd_error_system_call); |
| 247 | } |
| 248 | return nwrote; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | INTERNAL_FUNCTION |
| 253 | bfd_write_bigendian_4byte_int |
| 254 | |
| 255 | SYNOPSIS |
| 256 | void bfd_write_bigendian_4byte_int(bfd *abfd, int i); |
| 257 | |
| 258 | DESCRIPTION |
| 259 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big |
| 260 | endian order regardless of what else is going on. This is useful in |
| 261 | archives. |
| 262 | |
| 263 | */ |
| 264 | void |
| 265 | bfd_write_bigendian_4byte_int (abfd, i) |
| 266 | bfd *abfd; |
| 267 | int i; |
| 268 | { |
| 269 | bfd_byte buffer[4]; |
| 270 | bfd_putb32(i, buffer); |
| 271 | if (bfd_write((PTR)buffer, 4, 1, abfd) != 4) |
| 272 | abort (); |
| 273 | } |
| 274 | |
| 275 | long |
| 276 | bfd_tell (abfd) |
| 277 | bfd *abfd; |
| 278 | { |
| 279 | file_ptr ptr; |
| 280 | |
| 281 | ptr = ftell (bfd_cache_lookup(abfd)); |
| 282 | |
| 283 | if (abfd->my_archive) |
| 284 | ptr -= abfd->origin; |
| 285 | abfd->where = ptr; |
| 286 | return ptr; |
| 287 | } |
| 288 | |
| 289 | int |
| 290 | bfd_flush (abfd) |
| 291 | bfd *abfd; |
| 292 | { |
| 293 | return fflush (bfd_cache_lookup(abfd)); |
| 294 | } |
| 295 | |
| 296 | int |
| 297 | bfd_stat (abfd, statbuf) |
| 298 | bfd *abfd; |
| 299 | struct stat *statbuf; |
| 300 | { |
| 301 | return fstat (fileno(bfd_cache_lookup(abfd)), statbuf); |
| 302 | } |
| 303 | |
| 304 | /* Returns 0 for success, nonzero for failure (in which case bfd_get_error |
| 305 | can retrieve the error code). */ |
| 306 | |
| 307 | int |
| 308 | bfd_seek (abfd, position, direction) |
| 309 | bfd * CONST abfd; |
| 310 | CONST file_ptr position; |
| 311 | CONST int direction; |
| 312 | { |
| 313 | int result; |
| 314 | FILE *f; |
| 315 | file_ptr file_position; |
| 316 | /* For the time being, a BFD may not seek to it's end. The problem |
| 317 | is that we don't easily have a way to recognize the end of an |
| 318 | element in an archive. */ |
| 319 | |
| 320 | BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); |
| 321 | |
| 322 | if (direction == SEEK_CUR && position == 0) |
| 323 | return 0; |
| 324 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
| 325 | if (abfd->format != bfd_archive && abfd->my_archive == 0) |
| 326 | { |
| 327 | #if 0 |
| 328 | /* Explanation for this code: I'm only about 95+% sure that the above |
| 329 | conditions are sufficient and that all i/o calls are properly |
| 330 | adjusting the `where' field. So this is sort of an `assert' |
| 331 | that the `where' field is correct. If we can go a while without |
| 332 | tripping the abort, we can probably safely disable this code, |
| 333 | so that the real optimizations happen. */ |
| 334 | file_ptr where_am_i_now; |
| 335 | where_am_i_now = ftell (bfd_cache_lookup (abfd)); |
| 336 | if (abfd->my_archive) |
| 337 | where_am_i_now -= abfd->origin; |
| 338 | if (where_am_i_now != abfd->where) |
| 339 | abort (); |
| 340 | #endif |
| 341 | if (direction == SEEK_SET && position == abfd->where) |
| 342 | return 0; |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | /* We need something smarter to optimize access to archives. |
| 347 | Currently, anything inside an archive is read via the file |
| 348 | handle for the archive. Which means that a bfd_seek on one |
| 349 | component affects the `current position' in the archive, as |
| 350 | well as in any other component. |
| 351 | |
| 352 | It might be sufficient to put a spike through the cache |
| 353 | abstraction, and look to the archive for the file position, |
| 354 | but I think we should try for something cleaner. |
| 355 | |
| 356 | In the meantime, no optimization for archives. */ |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | f = bfd_cache_lookup (abfd); |
| 361 | file_position = position; |
| 362 | if (direction == SEEK_SET && abfd->my_archive != NULL) |
| 363 | file_position += abfd->origin; |
| 364 | |
| 365 | result = fseek (f, file_position, direction); |
| 366 | |
| 367 | if (result != 0) |
| 368 | { |
| 369 | /* Force redetermination of `where' field. */ |
| 370 | bfd_tell (abfd); |
| 371 | bfd_set_error (bfd_error_system_call); |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
| 376 | /* Adjust `where' field. */ |
| 377 | if (direction == SEEK_SET) |
| 378 | abfd->where = position; |
| 379 | else |
| 380 | abfd->where += position; |
| 381 | #endif |
| 382 | } |
| 383 | return result; |
| 384 | } |
| 385 | \f |
| 386 | /** Make a string table */ |
| 387 | |
| 388 | /*>bfd.h< |
| 389 | Add string to table pointed to by table, at location starting with free_ptr. |
| 390 | resizes the table if necessary (if it's NULL, creates it, ignoring |
| 391 | table_length). Updates free_ptr, table, table_length */ |
| 392 | |
| 393 | boolean |
| 394 | bfd_add_to_string_table (table, new_string, table_length, free_ptr) |
| 395 | char **table; |
| 396 | char *new_string; |
| 397 | unsigned int *table_length; |
| 398 | char **free_ptr; |
| 399 | { |
| 400 | size_t string_length = strlen (new_string) + 1; /* include null here */ |
| 401 | char *base = *table; |
| 402 | size_t space_length = *table_length; |
| 403 | unsigned int offset = (base ? *free_ptr - base : 0); |
| 404 | |
| 405 | if (base == NULL) { |
| 406 | /* Avoid a useless regrow if we can (but of course we still |
| 407 | take it next time). */ |
| 408 | space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ? |
| 409 | DEFAULT_STRING_SPACE_SIZE : string_length+1); |
| 410 | base = bfd_zmalloc ((bfd_size_type) space_length); |
| 411 | |
| 412 | if (base == NULL) { |
| 413 | bfd_set_error (bfd_error_no_memory); |
| 414 | return false; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if ((size_t)(offset + string_length) >= space_length) { |
| 419 | /* Make sure we will have enough space */ |
| 420 | while ((size_t)(offset + string_length) >= space_length) |
| 421 | space_length += space_length/2; /* grow by 50% */ |
| 422 | |
| 423 | base = (char *) realloc (base, space_length); |
| 424 | if (base == NULL) { |
| 425 | bfd_set_error (bfd_error_no_memory); |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | } |
| 430 | |
| 431 | memcpy (base + offset, new_string, string_length); |
| 432 | *table = base; |
| 433 | *table_length = space_length; |
| 434 | *free_ptr = base + offset + string_length; |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | \f |
| 439 | /** The do-it-yourself (byte) sex-change kit */ |
| 440 | |
| 441 | /* The middle letter e.g. get<b>short indicates Big or Little endian |
| 442 | target machine. It doesn't matter what the byte order of the host |
| 443 | machine is; these routines work for either. */ |
| 444 | |
| 445 | /* FIXME: Should these take a count argument? |
| 446 | Answer (gnu@cygnus.com): No, but perhaps they should be inline |
| 447 | functions in swap.h #ifdef __GNUC__. |
| 448 | Gprof them later and find out. */ |
| 449 | |
| 450 | /* |
| 451 | FUNCTION |
| 452 | bfd_put_size |
| 453 | FUNCTION |
| 454 | bfd_get_size |
| 455 | |
| 456 | DESCRIPTION |
| 457 | These macros as used for reading and writing raw data in |
| 458 | sections; each access (except for bytes) is vectored through |
| 459 | the target format of the BFD and mangled accordingly. The |
| 460 | mangling performs any necessary endian translations and |
| 461 | removes alignment restrictions. Note that types accepted and |
| 462 | returned by these macros are identical so they can be swapped |
| 463 | around in macros---for example, @file{libaout.h} defines <<GET_WORD>> |
| 464 | to either <<bfd_get_32>> or <<bfd_get_64>>. |
| 465 | |
| 466 | In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a |
| 467 | system without prototypes, the caller is responsible for making |
| 468 | sure that is true, with a cast if necessary. We don't cast |
| 469 | them in the macro definitions because that would prevent <<lint>> |
| 470 | or <<gcc -Wall>> from detecting sins such as passing a pointer. |
| 471 | To detect calling these with less than a <<bfd_vma>>, use |
| 472 | <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. |
| 473 | |
| 474 | . |
| 475 | .{* Byte swapping macros for user section data. *} |
| 476 | . |
| 477 | .#define bfd_put_8(abfd, val, ptr) \ |
| 478 | . (*((unsigned char *)(ptr)) = (unsigned char)(val)) |
| 479 | .#define bfd_put_signed_8 \ |
| 480 | . bfd_put_8 |
| 481 | .#define bfd_get_8(abfd, ptr) \ |
| 482 | . (*(unsigned char *)(ptr)) |
| 483 | .#define bfd_get_signed_8(abfd, ptr) \ |
| 484 | . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80) |
| 485 | . |
| 486 | .#define bfd_put_16(abfd, val, ptr) \ |
| 487 | . BFD_SEND(abfd, bfd_putx16, ((val),(ptr))) |
| 488 | .#define bfd_put_signed_16 \ |
| 489 | . bfd_put_16 |
| 490 | .#define bfd_get_16(abfd, ptr) \ |
| 491 | . BFD_SEND(abfd, bfd_getx16, (ptr)) |
| 492 | .#define bfd_get_signed_16(abfd, ptr) \ |
| 493 | . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) |
| 494 | . |
| 495 | .#define bfd_put_32(abfd, val, ptr) \ |
| 496 | . BFD_SEND(abfd, bfd_putx32, ((val),(ptr))) |
| 497 | .#define bfd_put_signed_32 \ |
| 498 | . bfd_put_32 |
| 499 | .#define bfd_get_32(abfd, ptr) \ |
| 500 | . BFD_SEND(abfd, bfd_getx32, (ptr)) |
| 501 | .#define bfd_get_signed_32(abfd, ptr) \ |
| 502 | . BFD_SEND(abfd, bfd_getx_signed_32, (ptr)) |
| 503 | . |
| 504 | .#define bfd_put_64(abfd, val, ptr) \ |
| 505 | . BFD_SEND(abfd, bfd_putx64, ((val), (ptr))) |
| 506 | .#define bfd_put_signed_64 \ |
| 507 | . bfd_put_64 |
| 508 | .#define bfd_get_64(abfd, ptr) \ |
| 509 | . BFD_SEND(abfd, bfd_getx64, (ptr)) |
| 510 | .#define bfd_get_signed_64(abfd, ptr) \ |
| 511 | . BFD_SEND(abfd, bfd_getx_signed_64, (ptr)) |
| 512 | . |
| 513 | */ |
| 514 | |
| 515 | /* |
| 516 | FUNCTION |
| 517 | bfd_h_put_size |
| 518 | bfd_h_get_size |
| 519 | |
| 520 | DESCRIPTION |
| 521 | These macros have the same function as their <<bfd_get_x>> |
| 522 | bretheren, except that they are used for removing information |
| 523 | for the header records of object files. Believe it or not, |
| 524 | some object files keep their header records in big endian |
| 525 | order and their data in little endian order. |
| 526 | . |
| 527 | .{* Byte swapping macros for file header data. *} |
| 528 | . |
| 529 | .#define bfd_h_put_8(abfd, val, ptr) \ |
| 530 | . bfd_put_8 (abfd, val, ptr) |
| 531 | .#define bfd_h_put_signed_8(abfd, val, ptr) \ |
| 532 | . bfd_put_8 (abfd, val, ptr) |
| 533 | .#define bfd_h_get_8(abfd, ptr) \ |
| 534 | . bfd_get_8 (abfd, ptr) |
| 535 | .#define bfd_h_get_signed_8(abfd, ptr) \ |
| 536 | . bfd_get_signed_8 (abfd, ptr) |
| 537 | . |
| 538 | .#define bfd_h_put_16(abfd, val, ptr) \ |
| 539 | . BFD_SEND(abfd, bfd_h_putx16,(val,ptr)) |
| 540 | .#define bfd_h_put_signed_16 \ |
| 541 | . bfd_h_put_16 |
| 542 | .#define bfd_h_get_16(abfd, ptr) \ |
| 543 | . BFD_SEND(abfd, bfd_h_getx16,(ptr)) |
| 544 | .#define bfd_h_get_signed_16(abfd, ptr) \ |
| 545 | . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr)) |
| 546 | . |
| 547 | .#define bfd_h_put_32(abfd, val, ptr) \ |
| 548 | . BFD_SEND(abfd, bfd_h_putx32,(val,ptr)) |
| 549 | .#define bfd_h_put_signed_32 \ |
| 550 | . bfd_h_put_32 |
| 551 | .#define bfd_h_get_32(abfd, ptr) \ |
| 552 | . BFD_SEND(abfd, bfd_h_getx32,(ptr)) |
| 553 | .#define bfd_h_get_signed_32(abfd, ptr) \ |
| 554 | . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr)) |
| 555 | . |
| 556 | .#define bfd_h_put_64(abfd, val, ptr) \ |
| 557 | . BFD_SEND(abfd, bfd_h_putx64,(val, ptr)) |
| 558 | .#define bfd_h_put_signed_64 \ |
| 559 | . bfd_h_put_64 |
| 560 | .#define bfd_h_get_64(abfd, ptr) \ |
| 561 | . BFD_SEND(abfd, bfd_h_getx64,(ptr)) |
| 562 | .#define bfd_h_get_signed_64(abfd, ptr) \ |
| 563 | . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr)) |
| 564 | . |
| 565 | */ |
| 566 | |
| 567 | /* Sign extension to bfd_signed_vma. */ |
| 568 | #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000) |
| 569 | #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000) |
| 570 | #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32) |
| 571 | #define COERCE64(x) \ |
| 572 | (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION) |
| 573 | |
| 574 | bfd_vma |
| 575 | bfd_getb16 (addr) |
| 576 | register const bfd_byte *addr; |
| 577 | { |
| 578 | return (addr[0] << 8) | addr[1]; |
| 579 | } |
| 580 | |
| 581 | bfd_vma |
| 582 | bfd_getl16 (addr) |
| 583 | register const bfd_byte *addr; |
| 584 | { |
| 585 | return (addr[1] << 8) | addr[0]; |
| 586 | } |
| 587 | |
| 588 | bfd_signed_vma |
| 589 | bfd_getb_signed_16 (addr) |
| 590 | register const bfd_byte *addr; |
| 591 | { |
| 592 | return COERCE16((addr[0] << 8) | addr[1]); |
| 593 | } |
| 594 | |
| 595 | bfd_signed_vma |
| 596 | bfd_getl_signed_16 (addr) |
| 597 | register const bfd_byte *addr; |
| 598 | { |
| 599 | return COERCE16((addr[1] << 8) | addr[0]); |
| 600 | } |
| 601 | |
| 602 | void |
| 603 | bfd_putb16 (data, addr) |
| 604 | bfd_vma data; |
| 605 | register bfd_byte *addr; |
| 606 | { |
| 607 | addr[0] = (bfd_byte)(data >> 8); |
| 608 | addr[1] = (bfd_byte )data; |
| 609 | } |
| 610 | |
| 611 | void |
| 612 | bfd_putl16 (data, addr) |
| 613 | bfd_vma data; |
| 614 | register bfd_byte *addr; |
| 615 | { |
| 616 | addr[0] = (bfd_byte )data; |
| 617 | addr[1] = (bfd_byte)(data >> 8); |
| 618 | } |
| 619 | |
| 620 | bfd_vma |
| 621 | bfd_getb32 (addr) |
| 622 | register const bfd_byte *addr; |
| 623 | { |
| 624 | return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8) |
| 625 | | addr[2]) << 8 | addr[3]; |
| 626 | } |
| 627 | |
| 628 | bfd_vma |
| 629 | bfd_getl32 (addr) |
| 630 | register const bfd_byte *addr; |
| 631 | { |
| 632 | return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8) |
| 633 | | addr[1]) << 8 | addr[0]; |
| 634 | } |
| 635 | |
| 636 | bfd_signed_vma |
| 637 | bfd_getb_signed_32 (addr) |
| 638 | register const bfd_byte *addr; |
| 639 | { |
| 640 | return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8) |
| 641 | | addr[2]) << 8 | addr[3]); |
| 642 | } |
| 643 | |
| 644 | bfd_signed_vma |
| 645 | bfd_getl_signed_32 (addr) |
| 646 | register const bfd_byte *addr; |
| 647 | { |
| 648 | return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8) |
| 649 | | addr[1]) << 8 | addr[0]); |
| 650 | } |
| 651 | |
| 652 | bfd_vma |
| 653 | bfd_getb64 (addr) |
| 654 | register const bfd_byte *addr; |
| 655 | { |
| 656 | #ifdef BFD64 |
| 657 | bfd_vma low, high; |
| 658 | |
| 659 | high= ((((((((addr[0]) << 8) | |
| 660 | addr[1]) << 8) | |
| 661 | addr[2]) << 8) | |
| 662 | addr[3]) ); |
| 663 | |
| 664 | low = (((((((((bfd_vma)addr[4]) << 8) | |
| 665 | addr[5]) << 8) | |
| 666 | addr[6]) << 8) | |
| 667 | addr[7])); |
| 668 | |
| 669 | return high << 32 | low; |
| 670 | #else |
| 671 | BFD_FAIL(); |
| 672 | return 0; |
| 673 | #endif |
| 674 | } |
| 675 | |
| 676 | bfd_vma |
| 677 | bfd_getl64 (addr) |
| 678 | register const bfd_byte *addr; |
| 679 | { |
| 680 | #ifdef BFD64 |
| 681 | bfd_vma low, high; |
| 682 | high= (((((((addr[7] << 8) | |
| 683 | addr[6]) << 8) | |
| 684 | addr[5]) << 8) | |
| 685 | addr[4])); |
| 686 | |
| 687 | low = ((((((((bfd_vma)addr[3] << 8) | |
| 688 | addr[2]) << 8) | |
| 689 | addr[1]) << 8) | |
| 690 | addr[0]) ); |
| 691 | |
| 692 | return high << 32 | low; |
| 693 | #else |
| 694 | BFD_FAIL(); |
| 695 | return 0; |
| 696 | #endif |
| 697 | |
| 698 | } |
| 699 | |
| 700 | bfd_signed_vma |
| 701 | bfd_getb_signed_64 (addr) |
| 702 | register const bfd_byte *addr; |
| 703 | { |
| 704 | #ifdef BFD64 |
| 705 | bfd_vma low, high; |
| 706 | |
| 707 | high= ((((((((addr[0]) << 8) | |
| 708 | addr[1]) << 8) | |
| 709 | addr[2]) << 8) | |
| 710 | addr[3]) ); |
| 711 | |
| 712 | low = (((((((((bfd_vma)addr[4]) << 8) | |
| 713 | addr[5]) << 8) | |
| 714 | addr[6]) << 8) | |
| 715 | addr[7])); |
| 716 | |
| 717 | return COERCE64(high << 32 | low); |
| 718 | #else |
| 719 | BFD_FAIL(); |
| 720 | return 0; |
| 721 | #endif |
| 722 | } |
| 723 | |
| 724 | bfd_signed_vma |
| 725 | bfd_getl_signed_64 (addr) |
| 726 | register const bfd_byte *addr; |
| 727 | { |
| 728 | #ifdef BFD64 |
| 729 | bfd_vma low, high; |
| 730 | high= (((((((addr[7] << 8) | |
| 731 | addr[6]) << 8) | |
| 732 | addr[5]) << 8) | |
| 733 | addr[4])); |
| 734 | |
| 735 | low = ((((((((bfd_vma)addr[3] << 8) | |
| 736 | addr[2]) << 8) | |
| 737 | addr[1]) << 8) | |
| 738 | addr[0]) ); |
| 739 | |
| 740 | return COERCE64(high << 32 | low); |
| 741 | #else |
| 742 | BFD_FAIL(); |
| 743 | return 0; |
| 744 | #endif |
| 745 | } |
| 746 | |
| 747 | void |
| 748 | bfd_putb32 (data, addr) |
| 749 | bfd_vma data; |
| 750 | register bfd_byte *addr; |
| 751 | { |
| 752 | addr[0] = (bfd_byte)(data >> 24); |
| 753 | addr[1] = (bfd_byte)(data >> 16); |
| 754 | addr[2] = (bfd_byte)(data >> 8); |
| 755 | addr[3] = (bfd_byte)data; |
| 756 | } |
| 757 | |
| 758 | void |
| 759 | bfd_putl32 (data, addr) |
| 760 | bfd_vma data; |
| 761 | register bfd_byte *addr; |
| 762 | { |
| 763 | addr[0] = (bfd_byte)data; |
| 764 | addr[1] = (bfd_byte)(data >> 8); |
| 765 | addr[2] = (bfd_byte)(data >> 16); |
| 766 | addr[3] = (bfd_byte)(data >> 24); |
| 767 | } |
| 768 | |
| 769 | void |
| 770 | bfd_putb64 (data, addr) |
| 771 | bfd_vma data; |
| 772 | register bfd_byte *addr; |
| 773 | { |
| 774 | #ifdef BFD64 |
| 775 | addr[0] = (bfd_byte)(data >> (7*8)); |
| 776 | addr[1] = (bfd_byte)(data >> (6*8)); |
| 777 | addr[2] = (bfd_byte)(data >> (5*8)); |
| 778 | addr[3] = (bfd_byte)(data >> (4*8)); |
| 779 | addr[4] = (bfd_byte)(data >> (3*8)); |
| 780 | addr[5] = (bfd_byte)(data >> (2*8)); |
| 781 | addr[6] = (bfd_byte)(data >> (1*8)); |
| 782 | addr[7] = (bfd_byte)(data >> (0*8)); |
| 783 | #else |
| 784 | BFD_FAIL(); |
| 785 | #endif |
| 786 | } |
| 787 | |
| 788 | void |
| 789 | bfd_putl64 (data, addr) |
| 790 | bfd_vma data; |
| 791 | register bfd_byte *addr; |
| 792 | { |
| 793 | #ifdef BFD64 |
| 794 | addr[7] = (bfd_byte)(data >> (7*8)); |
| 795 | addr[6] = (bfd_byte)(data >> (6*8)); |
| 796 | addr[5] = (bfd_byte)(data >> (5*8)); |
| 797 | addr[4] = (bfd_byte)(data >> (4*8)); |
| 798 | addr[3] = (bfd_byte)(data >> (3*8)); |
| 799 | addr[2] = (bfd_byte)(data >> (2*8)); |
| 800 | addr[1] = (bfd_byte)(data >> (1*8)); |
| 801 | addr[0] = (bfd_byte)(data >> (0*8)); |
| 802 | #else |
| 803 | BFD_FAIL(); |
| 804 | #endif |
| 805 | } |
| 806 | \f |
| 807 | /* Default implementation */ |
| 808 | |
| 809 | boolean |
| 810 | _bfd_generic_get_section_contents (abfd, section, location, offset, count) |
| 811 | bfd *abfd; |
| 812 | sec_ptr section; |
| 813 | PTR location; |
| 814 | file_ptr offset; |
| 815 | bfd_size_type count; |
| 816 | { |
| 817 | if (count == 0) |
| 818 | return true; |
| 819 | if ((bfd_size_type)(offset+count) > section->_raw_size |
| 820 | || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1 |
| 821 | || bfd_read(location, (bfd_size_type)1, count, abfd) != count) |
| 822 | return (false); /* on error */ |
| 823 | return (true); |
| 824 | } |
| 825 | |
| 826 | /* This generic function can only be used in implementations where creating |
| 827 | NEW sections is disallowed. It is useful in patching existing sections |
| 828 | in read-write files, though. See other set_section_contents functions |
| 829 | to see why it doesn't work for new sections. */ |
| 830 | boolean |
| 831 | _bfd_generic_set_section_contents (abfd, section, location, offset, count) |
| 832 | bfd *abfd; |
| 833 | sec_ptr section; |
| 834 | PTR location; |
| 835 | file_ptr offset; |
| 836 | bfd_size_type count; |
| 837 | { |
| 838 | if (count == 0) |
| 839 | return true; |
| 840 | |
| 841 | if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1 |
| 842 | || bfd_write (location, (bfd_size_type) 1, count, abfd) != count) |
| 843 | return false; |
| 844 | |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | INTERNAL_FUNCTION |
| 850 | bfd_log2 |
| 851 | |
| 852 | SYNOPSIS |
| 853 | unsigned int bfd_log2(bfd_vma x); |
| 854 | |
| 855 | DESCRIPTION |
| 856 | Return the log base 2 of the value supplied, rounded up. E.g., an |
| 857 | @var{x} of 1025 returns 11. |
| 858 | */ |
| 859 | |
| 860 | unsigned |
| 861 | bfd_log2(x) |
| 862 | bfd_vma x; |
| 863 | { |
| 864 | unsigned result = 0; |
| 865 | while ( (bfd_vma)(1<< result) < x) |
| 866 | result++; |
| 867 | return result; |
| 868 | } |
| 869 | |
| 870 | boolean |
| 871 | bfd_generic_is_local_label (abfd, sym) |
| 872 | bfd *abfd; |
| 873 | asymbol *sym; |
| 874 | { |
| 875 | char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; |
| 876 | |
| 877 | return (sym->name[0] == locals_prefix); |
| 878 | } |
| 879 | |