| 1 | /* Assorted BFD support routines, only used internally. |
| 2 | Copyright (C) 1990-2016 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 | /* A routine which is used in target vectors for unsupported |
| 44 | operations. */ |
| 45 | |
| 46 | bfd_boolean |
| 47 | bfd_false (bfd *ignore ATTRIBUTE_UNUSED) |
| 48 | { |
| 49 | bfd_set_error (bfd_error_invalid_operation); |
| 50 | return FALSE; |
| 51 | } |
| 52 | |
| 53 | /* A routine which is used in target vectors for supported operations |
| 54 | which do not actually do anything. */ |
| 55 | |
| 56 | bfd_boolean |
| 57 | bfd_true (bfd *ignore ATTRIBUTE_UNUSED) |
| 58 | { |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | /* A routine which is used in target vectors for unsupported |
| 63 | operations which return a pointer value. */ |
| 64 | |
| 65 | void * |
| 66 | bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED) |
| 67 | { |
| 68 | bfd_set_error (bfd_error_invalid_operation); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | int |
| 73 | bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | unsigned int |
| 79 | bfd_0u (bfd *ignore ATTRIBUTE_UNUSED) |
| 80 | { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | long |
| 85 | bfd_0l (bfd *ignore ATTRIBUTE_UNUSED) |
| 86 | { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* A routine which is used in target vectors for unsupported |
| 91 | operations which return -1 on error. */ |
| 92 | |
| 93 | long |
| 94 | _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 95 | { |
| 96 | bfd_set_error (bfd_error_invalid_operation); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | bfd_void (bfd *ignore ATTRIBUTE_UNUSED) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | long |
| 106 | _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
| 107 | asection *sec ATTRIBUTE_UNUSED) |
| 108 | { |
| 109 | return sizeof (arelent *); |
| 110 | } |
| 111 | |
| 112 | long |
| 113 | _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
| 114 | asection *sec ATTRIBUTE_UNUSED, |
| 115 | arelent **relptr, |
| 116 | asymbol **symbols ATTRIBUTE_UNUSED) |
| 117 | { |
| 118 | *relptr = NULL; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | bfd_boolean |
| 123 | _bfd_nocore_core_file_matches_executable_p |
| 124 | (bfd *ignore_core_bfd ATTRIBUTE_UNUSED, |
| 125 | bfd *ignore_exec_bfd ATTRIBUTE_UNUSED) |
| 126 | { |
| 127 | bfd_set_error (bfd_error_invalid_operation); |
| 128 | return FALSE; |
| 129 | } |
| 130 | |
| 131 | /* Routine to handle core_file_failing_command entry point for targets |
| 132 | without core file support. */ |
| 133 | |
| 134 | char * |
| 135 | _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 136 | { |
| 137 | bfd_set_error (bfd_error_invalid_operation); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | /* Routine to handle core_file_failing_signal entry point for targets |
| 142 | without core file support. */ |
| 143 | |
| 144 | int |
| 145 | _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 146 | { |
| 147 | bfd_set_error (bfd_error_invalid_operation); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* Routine to handle the core_file_pid entry point for targets without |
| 152 | core file support. */ |
| 153 | |
| 154 | int |
| 155 | _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 156 | { |
| 157 | bfd_set_error (bfd_error_invalid_operation); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | const bfd_target * |
| 162 | _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 163 | { |
| 164 | bfd_set_error (bfd_error_wrong_format); |
| 165 | return 0; |
| 166 | } |
| 167 | \f |
| 168 | /* Allocate memory using malloc. */ |
| 169 | |
| 170 | void * |
| 171 | bfd_malloc (bfd_size_type size) |
| 172 | { |
| 173 | void *ptr; |
| 174 | size_t sz = (size_t) size; |
| 175 | |
| 176 | if (size != sz |
| 177 | /* This is to pacify memory checkers like valgrind. */ |
| 178 | || ((signed long) sz) < 0) |
| 179 | { |
| 180 | bfd_set_error (bfd_error_no_memory); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | ptr = malloc (sz); |
| 185 | if (ptr == NULL && sz != 0) |
| 186 | bfd_set_error (bfd_error_no_memory); |
| 187 | |
| 188 | return ptr; |
| 189 | } |
| 190 | |
| 191 | /* Allocate memory using malloc, nmemb * size with overflow checking. */ |
| 192 | |
| 193 | void * |
| 194 | bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size) |
| 195 | { |
| 196 | if ((nmemb | size) >= HALF_BFD_SIZE_TYPE |
| 197 | && size != 0 |
| 198 | && nmemb > ~(bfd_size_type) 0 / size) |
| 199 | { |
| 200 | bfd_set_error (bfd_error_no_memory); |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | return bfd_malloc (size * nmemb); |
| 205 | } |
| 206 | |
| 207 | /* Reallocate memory using realloc. */ |
| 208 | |
| 209 | void * |
| 210 | bfd_realloc (void *ptr, bfd_size_type size) |
| 211 | { |
| 212 | void *ret; |
| 213 | size_t sz = (size_t) size; |
| 214 | |
| 215 | if (ptr == NULL) |
| 216 | return bfd_malloc (size); |
| 217 | |
| 218 | if (size != sz |
| 219 | /* This is to pacify memory checkers like valgrind. */ |
| 220 | || ((signed long) sz) < 0) |
| 221 | { |
| 222 | bfd_set_error (bfd_error_no_memory); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | ret = realloc (ptr, sz); |
| 227 | |
| 228 | if (ret == NULL && sz != 0) |
| 229 | bfd_set_error (bfd_error_no_memory); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* Reallocate memory using realloc, nmemb * size with overflow checking. */ |
| 235 | |
| 236 | void * |
| 237 | bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size) |
| 238 | { |
| 239 | if ((nmemb | size) >= HALF_BFD_SIZE_TYPE |
| 240 | && size != 0 |
| 241 | && nmemb > ~(bfd_size_type) 0 / size) |
| 242 | { |
| 243 | bfd_set_error (bfd_error_no_memory); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | return bfd_realloc (ptr, size * nmemb); |
| 248 | } |
| 249 | |
| 250 | /* Reallocate memory using realloc. |
| 251 | If this fails the pointer is freed before returning. */ |
| 252 | |
| 253 | void * |
| 254 | bfd_realloc_or_free (void *ptr, bfd_size_type size) |
| 255 | { |
| 256 | void *ret = bfd_realloc (ptr, size); |
| 257 | |
| 258 | if (ret == NULL && ptr != NULL) |
| 259 | free (ptr); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | /* Allocate memory using malloc and clear it. */ |
| 265 | |
| 266 | void * |
| 267 | bfd_zmalloc (bfd_size_type size) |
| 268 | { |
| 269 | void *ptr = bfd_malloc (size); |
| 270 | |
| 271 | if (ptr != NULL && size > 0) |
| 272 | memset (ptr, 0, (size_t) size); |
| 273 | |
| 274 | return ptr; |
| 275 | } |
| 276 | |
| 277 | /* Allocate memory using malloc (nmemb * size) with overflow checking |
| 278 | and clear it. */ |
| 279 | |
| 280 | void * |
| 281 | bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size) |
| 282 | { |
| 283 | void *ptr = bfd_malloc2 (nmemb, size); |
| 284 | |
| 285 | if (ptr != NULL) |
| 286 | { |
| 287 | size_t sz = nmemb * size; |
| 288 | |
| 289 | if (sz > 0) |
| 290 | memset (ptr, 0, sz); |
| 291 | } |
| 292 | |
| 293 | return ptr; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | INTERNAL_FUNCTION |
| 298 | bfd_write_bigendian_4byte_int |
| 299 | |
| 300 | SYNOPSIS |
| 301 | bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int); |
| 302 | |
| 303 | DESCRIPTION |
| 304 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big |
| 305 | endian order regardless of what else is going on. This is useful in |
| 306 | archives. |
| 307 | |
| 308 | */ |
| 309 | bfd_boolean |
| 310 | bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i) |
| 311 | { |
| 312 | bfd_byte buffer[4]; |
| 313 | bfd_putb32 ((bfd_vma) i, buffer); |
| 314 | return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4; |
| 315 | } |
| 316 | |
| 317 | \f |
| 318 | /** The do-it-yourself (byte) sex-change kit */ |
| 319 | |
| 320 | /* The middle letter e.g. get<b>short indicates Big or Little endian |
| 321 | target machine. It doesn't matter what the byte order of the host |
| 322 | machine is; these routines work for either. */ |
| 323 | |
| 324 | /* FIXME: Should these take a count argument? |
| 325 | Answer (gnu@cygnus.com): No, but perhaps they should be inline |
| 326 | functions in swap.h #ifdef __GNUC__. |
| 327 | Gprof them later and find out. */ |
| 328 | |
| 329 | /* |
| 330 | FUNCTION |
| 331 | bfd_put_size |
| 332 | FUNCTION |
| 333 | bfd_get_size |
| 334 | |
| 335 | DESCRIPTION |
| 336 | These macros as used for reading and writing raw data in |
| 337 | sections; each access (except for bytes) is vectored through |
| 338 | the target format of the BFD and mangled accordingly. The |
| 339 | mangling performs any necessary endian translations and |
| 340 | removes alignment restrictions. Note that types accepted and |
| 341 | returned by these macros are identical so they can be swapped |
| 342 | around in macros---for example, @file{libaout.h} defines <<GET_WORD>> |
| 343 | to either <<bfd_get_32>> or <<bfd_get_64>>. |
| 344 | |
| 345 | In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a |
| 346 | system without prototypes, the caller is responsible for making |
| 347 | sure that is true, with a cast if necessary. We don't cast |
| 348 | them in the macro definitions because that would prevent <<lint>> |
| 349 | or <<gcc -Wall>> from detecting sins such as passing a pointer. |
| 350 | To detect calling these with less than a <<bfd_vma>>, use |
| 351 | <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. |
| 352 | |
| 353 | . |
| 354 | .{* Byte swapping macros for user section data. *} |
| 355 | . |
| 356 | .#define bfd_put_8(abfd, val, ptr) \ |
| 357 | . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff)) |
| 358 | .#define bfd_put_signed_8 \ |
| 359 | . bfd_put_8 |
| 360 | .#define bfd_get_8(abfd, ptr) \ |
| 361 | . (*(const unsigned char *) (ptr) & 0xff) |
| 362 | .#define bfd_get_signed_8(abfd, ptr) \ |
| 363 | . (((*(const unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80) |
| 364 | . |
| 365 | .#define bfd_put_16(abfd, val, ptr) \ |
| 366 | . BFD_SEND (abfd, bfd_putx16, ((val),(ptr))) |
| 367 | .#define bfd_put_signed_16 \ |
| 368 | . bfd_put_16 |
| 369 | .#define bfd_get_16(abfd, ptr) \ |
| 370 | . BFD_SEND (abfd, bfd_getx16, (ptr)) |
| 371 | .#define bfd_get_signed_16(abfd, ptr) \ |
| 372 | . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) |
| 373 | . |
| 374 | .#define bfd_put_32(abfd, val, ptr) \ |
| 375 | . BFD_SEND (abfd, bfd_putx32, ((val),(ptr))) |
| 376 | .#define bfd_put_signed_32 \ |
| 377 | . bfd_put_32 |
| 378 | .#define bfd_get_32(abfd, ptr) \ |
| 379 | . BFD_SEND (abfd, bfd_getx32, (ptr)) |
| 380 | .#define bfd_get_signed_32(abfd, ptr) \ |
| 381 | . BFD_SEND (abfd, bfd_getx_signed_32, (ptr)) |
| 382 | . |
| 383 | .#define bfd_put_64(abfd, val, ptr) \ |
| 384 | . BFD_SEND (abfd, bfd_putx64, ((val), (ptr))) |
| 385 | .#define bfd_put_signed_64 \ |
| 386 | . bfd_put_64 |
| 387 | .#define bfd_get_64(abfd, ptr) \ |
| 388 | . BFD_SEND (abfd, bfd_getx64, (ptr)) |
| 389 | .#define bfd_get_signed_64(abfd, ptr) \ |
| 390 | . BFD_SEND (abfd, bfd_getx_signed_64, (ptr)) |
| 391 | . |
| 392 | .#define bfd_get(bits, abfd, ptr) \ |
| 393 | . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \ |
| 394 | . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \ |
| 395 | . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \ |
| 396 | . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \ |
| 397 | . : (abort (), (bfd_vma) - 1)) |
| 398 | . |
| 399 | .#define bfd_put(bits, abfd, val, ptr) \ |
| 400 | . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \ |
| 401 | . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \ |
| 402 | . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \ |
| 403 | . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \ |
| 404 | . : (abort (), (void) 0)) |
| 405 | . |
| 406 | */ |
| 407 | |
| 408 | /* |
| 409 | FUNCTION |
| 410 | bfd_h_put_size |
| 411 | bfd_h_get_size |
| 412 | |
| 413 | DESCRIPTION |
| 414 | These macros have the same function as their <<bfd_get_x>> |
| 415 | brethren, except that they are used for removing information |
| 416 | for the header records of object files. Believe it or not, |
| 417 | some object files keep their header records in big endian |
| 418 | order and their data in little endian order. |
| 419 | . |
| 420 | .{* Byte swapping macros for file header data. *} |
| 421 | . |
| 422 | .#define bfd_h_put_8(abfd, val, ptr) \ |
| 423 | . bfd_put_8 (abfd, val, ptr) |
| 424 | .#define bfd_h_put_signed_8(abfd, val, ptr) \ |
| 425 | . bfd_put_8 (abfd, val, ptr) |
| 426 | .#define bfd_h_get_8(abfd, ptr) \ |
| 427 | . bfd_get_8 (abfd, ptr) |
| 428 | .#define bfd_h_get_signed_8(abfd, ptr) \ |
| 429 | . bfd_get_signed_8 (abfd, ptr) |
| 430 | . |
| 431 | .#define bfd_h_put_16(abfd, val, ptr) \ |
| 432 | . BFD_SEND (abfd, bfd_h_putx16, (val, ptr)) |
| 433 | .#define bfd_h_put_signed_16 \ |
| 434 | . bfd_h_put_16 |
| 435 | .#define bfd_h_get_16(abfd, ptr) \ |
| 436 | . BFD_SEND (abfd, bfd_h_getx16, (ptr)) |
| 437 | .#define bfd_h_get_signed_16(abfd, ptr) \ |
| 438 | . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr)) |
| 439 | . |
| 440 | .#define bfd_h_put_32(abfd, val, ptr) \ |
| 441 | . BFD_SEND (abfd, bfd_h_putx32, (val, ptr)) |
| 442 | .#define bfd_h_put_signed_32 \ |
| 443 | . bfd_h_put_32 |
| 444 | .#define bfd_h_get_32(abfd, ptr) \ |
| 445 | . BFD_SEND (abfd, bfd_h_getx32, (ptr)) |
| 446 | .#define bfd_h_get_signed_32(abfd, ptr) \ |
| 447 | . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr)) |
| 448 | . |
| 449 | .#define bfd_h_put_64(abfd, val, ptr) \ |
| 450 | . BFD_SEND (abfd, bfd_h_putx64, (val, ptr)) |
| 451 | .#define bfd_h_put_signed_64 \ |
| 452 | . bfd_h_put_64 |
| 453 | .#define bfd_h_get_64(abfd, ptr) \ |
| 454 | . BFD_SEND (abfd, bfd_h_getx64, (ptr)) |
| 455 | .#define bfd_h_get_signed_64(abfd, ptr) \ |
| 456 | . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr)) |
| 457 | . |
| 458 | .{* Aliases for the above, which should eventually go away. *} |
| 459 | . |
| 460 | .#define H_PUT_64 bfd_h_put_64 |
| 461 | .#define H_PUT_32 bfd_h_put_32 |
| 462 | .#define H_PUT_16 bfd_h_put_16 |
| 463 | .#define H_PUT_8 bfd_h_put_8 |
| 464 | .#define H_PUT_S64 bfd_h_put_signed_64 |
| 465 | .#define H_PUT_S32 bfd_h_put_signed_32 |
| 466 | .#define H_PUT_S16 bfd_h_put_signed_16 |
| 467 | .#define H_PUT_S8 bfd_h_put_signed_8 |
| 468 | .#define H_GET_64 bfd_h_get_64 |
| 469 | .#define H_GET_32 bfd_h_get_32 |
| 470 | .#define H_GET_16 bfd_h_get_16 |
| 471 | .#define H_GET_8 bfd_h_get_8 |
| 472 | .#define H_GET_S64 bfd_h_get_signed_64 |
| 473 | .#define H_GET_S32 bfd_h_get_signed_32 |
| 474 | .#define H_GET_S16 bfd_h_get_signed_16 |
| 475 | .#define H_GET_S8 bfd_h_get_signed_8 |
| 476 | . |
| 477 | .*/ |
| 478 | |
| 479 | /* Sign extension to bfd_signed_vma. */ |
| 480 | #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000) |
| 481 | #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000) |
| 482 | #define COERCE64(x) \ |
| 483 | (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63)) |
| 484 | |
| 485 | bfd_vma |
| 486 | bfd_getb16 (const void *p) |
| 487 | { |
| 488 | const bfd_byte *addr = (const bfd_byte *) p; |
| 489 | return (addr[0] << 8) | addr[1]; |
| 490 | } |
| 491 | |
| 492 | bfd_vma |
| 493 | bfd_getl16 (const void *p) |
| 494 | { |
| 495 | const bfd_byte *addr = (const bfd_byte *) p; |
| 496 | return (addr[1] << 8) | addr[0]; |
| 497 | } |
| 498 | |
| 499 | bfd_signed_vma |
| 500 | bfd_getb_signed_16 (const void *p) |
| 501 | { |
| 502 | const bfd_byte *addr = (const bfd_byte *) p; |
| 503 | return COERCE16 ((addr[0] << 8) | addr[1]); |
| 504 | } |
| 505 | |
| 506 | bfd_signed_vma |
| 507 | bfd_getl_signed_16 (const void *p) |
| 508 | { |
| 509 | const bfd_byte *addr = (const bfd_byte *) p; |
| 510 | return COERCE16 ((addr[1] << 8) | addr[0]); |
| 511 | } |
| 512 | |
| 513 | void |
| 514 | bfd_putb16 (bfd_vma data, void *p) |
| 515 | { |
| 516 | bfd_byte *addr = (bfd_byte *) p; |
| 517 | addr[0] = (data >> 8) & 0xff; |
| 518 | addr[1] = data & 0xff; |
| 519 | } |
| 520 | |
| 521 | void |
| 522 | bfd_putl16 (bfd_vma data, void *p) |
| 523 | { |
| 524 | bfd_byte *addr = (bfd_byte *) p; |
| 525 | addr[0] = data & 0xff; |
| 526 | addr[1] = (data >> 8) & 0xff; |
| 527 | } |
| 528 | |
| 529 | bfd_vma |
| 530 | bfd_getb32 (const void *p) |
| 531 | { |
| 532 | const bfd_byte *addr = (const bfd_byte *) p; |
| 533 | unsigned long v; |
| 534 | |
| 535 | v = (unsigned long) addr[0] << 24; |
| 536 | v |= (unsigned long) addr[1] << 16; |
| 537 | v |= (unsigned long) addr[2] << 8; |
| 538 | v |= (unsigned long) addr[3]; |
| 539 | return v; |
| 540 | } |
| 541 | |
| 542 | bfd_vma |
| 543 | bfd_getl32 (const void *p) |
| 544 | { |
| 545 | const bfd_byte *addr = (const bfd_byte *) p; |
| 546 | unsigned long v; |
| 547 | |
| 548 | v = (unsigned long) addr[0]; |
| 549 | v |= (unsigned long) addr[1] << 8; |
| 550 | v |= (unsigned long) addr[2] << 16; |
| 551 | v |= (unsigned long) addr[3] << 24; |
| 552 | return v; |
| 553 | } |
| 554 | |
| 555 | bfd_signed_vma |
| 556 | bfd_getb_signed_32 (const void *p) |
| 557 | { |
| 558 | const bfd_byte *addr = (const bfd_byte *) p; |
| 559 | unsigned long v; |
| 560 | |
| 561 | v = (unsigned long) addr[0] << 24; |
| 562 | v |= (unsigned long) addr[1] << 16; |
| 563 | v |= (unsigned long) addr[2] << 8; |
| 564 | v |= (unsigned long) addr[3]; |
| 565 | return COERCE32 (v); |
| 566 | } |
| 567 | |
| 568 | bfd_signed_vma |
| 569 | bfd_getl_signed_32 (const void *p) |
| 570 | { |
| 571 | const bfd_byte *addr = (const bfd_byte *) p; |
| 572 | unsigned long v; |
| 573 | |
| 574 | v = (unsigned long) addr[0]; |
| 575 | v |= (unsigned long) addr[1] << 8; |
| 576 | v |= (unsigned long) addr[2] << 16; |
| 577 | v |= (unsigned long) addr[3] << 24; |
| 578 | return COERCE32 (v); |
| 579 | } |
| 580 | |
| 581 | bfd_uint64_t |
| 582 | bfd_getb64 (const void *p ATTRIBUTE_UNUSED) |
| 583 | { |
| 584 | #ifdef BFD_HOST_64_BIT |
| 585 | const bfd_byte *addr = (const bfd_byte *) p; |
| 586 | bfd_uint64_t v; |
| 587 | |
| 588 | v = addr[0]; v <<= 8; |
| 589 | v |= addr[1]; v <<= 8; |
| 590 | v |= addr[2]; v <<= 8; |
| 591 | v |= addr[3]; v <<= 8; |
| 592 | v |= addr[4]; v <<= 8; |
| 593 | v |= addr[5]; v <<= 8; |
| 594 | v |= addr[6]; v <<= 8; |
| 595 | v |= addr[7]; |
| 596 | |
| 597 | return v; |
| 598 | #else |
| 599 | BFD_FAIL(); |
| 600 | return 0; |
| 601 | #endif |
| 602 | } |
| 603 | |
| 604 | bfd_uint64_t |
| 605 | bfd_getl64 (const void *p ATTRIBUTE_UNUSED) |
| 606 | { |
| 607 | #ifdef BFD_HOST_64_BIT |
| 608 | const bfd_byte *addr = (const bfd_byte *) p; |
| 609 | bfd_uint64_t v; |
| 610 | |
| 611 | v = addr[7]; v <<= 8; |
| 612 | v |= addr[6]; v <<= 8; |
| 613 | v |= addr[5]; v <<= 8; |
| 614 | v |= addr[4]; v <<= 8; |
| 615 | v |= addr[3]; v <<= 8; |
| 616 | v |= addr[2]; v <<= 8; |
| 617 | v |= addr[1]; v <<= 8; |
| 618 | v |= addr[0]; |
| 619 | |
| 620 | return v; |
| 621 | #else |
| 622 | BFD_FAIL(); |
| 623 | return 0; |
| 624 | #endif |
| 625 | |
| 626 | } |
| 627 | |
| 628 | bfd_int64_t |
| 629 | bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED) |
| 630 | { |
| 631 | #ifdef BFD_HOST_64_BIT |
| 632 | const bfd_byte *addr = (const bfd_byte *) p; |
| 633 | bfd_uint64_t v; |
| 634 | |
| 635 | v = addr[0]; v <<= 8; |
| 636 | v |= addr[1]; v <<= 8; |
| 637 | v |= addr[2]; v <<= 8; |
| 638 | v |= addr[3]; v <<= 8; |
| 639 | v |= addr[4]; v <<= 8; |
| 640 | v |= addr[5]; v <<= 8; |
| 641 | v |= addr[6]; v <<= 8; |
| 642 | v |= addr[7]; |
| 643 | |
| 644 | return COERCE64 (v); |
| 645 | #else |
| 646 | BFD_FAIL(); |
| 647 | return 0; |
| 648 | #endif |
| 649 | } |
| 650 | |
| 651 | bfd_int64_t |
| 652 | bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED) |
| 653 | { |
| 654 | #ifdef BFD_HOST_64_BIT |
| 655 | const bfd_byte *addr = (const bfd_byte *) p; |
| 656 | bfd_uint64_t v; |
| 657 | |
| 658 | v = addr[7]; v <<= 8; |
| 659 | v |= addr[6]; v <<= 8; |
| 660 | v |= addr[5]; v <<= 8; |
| 661 | v |= addr[4]; v <<= 8; |
| 662 | v |= addr[3]; v <<= 8; |
| 663 | v |= addr[2]; v <<= 8; |
| 664 | v |= addr[1]; v <<= 8; |
| 665 | v |= addr[0]; |
| 666 | |
| 667 | return COERCE64 (v); |
| 668 | #else |
| 669 | BFD_FAIL(); |
| 670 | return 0; |
| 671 | #endif |
| 672 | } |
| 673 | |
| 674 | void |
| 675 | bfd_putb32 (bfd_vma data, void *p) |
| 676 | { |
| 677 | bfd_byte *addr = (bfd_byte *) p; |
| 678 | addr[0] = (data >> 24) & 0xff; |
| 679 | addr[1] = (data >> 16) & 0xff; |
| 680 | addr[2] = (data >> 8) & 0xff; |
| 681 | addr[3] = data & 0xff; |
| 682 | } |
| 683 | |
| 684 | void |
| 685 | bfd_putl32 (bfd_vma data, void *p) |
| 686 | { |
| 687 | bfd_byte *addr = (bfd_byte *) p; |
| 688 | addr[0] = data & 0xff; |
| 689 | addr[1] = (data >> 8) & 0xff; |
| 690 | addr[2] = (data >> 16) & 0xff; |
| 691 | addr[3] = (data >> 24) & 0xff; |
| 692 | } |
| 693 | |
| 694 | void |
| 695 | bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED) |
| 696 | { |
| 697 | #ifdef BFD_HOST_64_BIT |
| 698 | bfd_byte *addr = (bfd_byte *) p; |
| 699 | addr[0] = (data >> (7*8)) & 0xff; |
| 700 | addr[1] = (data >> (6*8)) & 0xff; |
| 701 | addr[2] = (data >> (5*8)) & 0xff; |
| 702 | addr[3] = (data >> (4*8)) & 0xff; |
| 703 | addr[4] = (data >> (3*8)) & 0xff; |
| 704 | addr[5] = (data >> (2*8)) & 0xff; |
| 705 | addr[6] = (data >> (1*8)) & 0xff; |
| 706 | addr[7] = (data >> (0*8)) & 0xff; |
| 707 | #else |
| 708 | BFD_FAIL(); |
| 709 | #endif |
| 710 | } |
| 711 | |
| 712 | void |
| 713 | bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED) |
| 714 | { |
| 715 | #ifdef BFD_HOST_64_BIT |
| 716 | bfd_byte *addr = (bfd_byte *) p; |
| 717 | addr[7] = (data >> (7*8)) & 0xff; |
| 718 | addr[6] = (data >> (6*8)) & 0xff; |
| 719 | addr[5] = (data >> (5*8)) & 0xff; |
| 720 | addr[4] = (data >> (4*8)) & 0xff; |
| 721 | addr[3] = (data >> (3*8)) & 0xff; |
| 722 | addr[2] = (data >> (2*8)) & 0xff; |
| 723 | addr[1] = (data >> (1*8)) & 0xff; |
| 724 | addr[0] = (data >> (0*8)) & 0xff; |
| 725 | #else |
| 726 | BFD_FAIL(); |
| 727 | #endif |
| 728 | } |
| 729 | |
| 730 | void |
| 731 | bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p) |
| 732 | { |
| 733 | bfd_byte *addr = (bfd_byte *) p; |
| 734 | int i; |
| 735 | int bytes; |
| 736 | |
| 737 | if (bits % 8 != 0) |
| 738 | abort (); |
| 739 | |
| 740 | bytes = bits / 8; |
| 741 | for (i = 0; i < bytes; i++) |
| 742 | { |
| 743 | int addr_index = big_p ? bytes - i - 1 : i; |
| 744 | |
| 745 | addr[addr_index] = data & 0xff; |
| 746 | data >>= 8; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | bfd_uint64_t |
| 751 | bfd_get_bits (const void *p, int bits, bfd_boolean big_p) |
| 752 | { |
| 753 | const bfd_byte *addr = (const bfd_byte *) p; |
| 754 | bfd_uint64_t data; |
| 755 | int i; |
| 756 | int bytes; |
| 757 | |
| 758 | if (bits % 8 != 0) |
| 759 | abort (); |
| 760 | |
| 761 | data = 0; |
| 762 | bytes = bits / 8; |
| 763 | for (i = 0; i < bytes; i++) |
| 764 | { |
| 765 | int addr_index = big_p ? i : bytes - i - 1; |
| 766 | |
| 767 | data = (data << 8) | addr[addr_index]; |
| 768 | } |
| 769 | |
| 770 | return data; |
| 771 | } |
| 772 | \f |
| 773 | /* Default implementation */ |
| 774 | |
| 775 | bfd_boolean |
| 776 | _bfd_generic_get_section_contents (bfd *abfd, |
| 777 | sec_ptr section, |
| 778 | void *location, |
| 779 | file_ptr offset, |
| 780 | bfd_size_type count) |
| 781 | { |
| 782 | bfd_size_type sz; |
| 783 | if (count == 0) |
| 784 | return TRUE; |
| 785 | |
| 786 | if (section->compress_status != COMPRESS_SECTION_NONE) |
| 787 | { |
| 788 | _bfd_error_handler |
| 789 | /* xgettext:c-format */ |
| 790 | (_("%B: unable to get decompressed section %A"), |
| 791 | abfd, section); |
| 792 | bfd_set_error (bfd_error_invalid_operation); |
| 793 | return FALSE; |
| 794 | } |
| 795 | |
| 796 | /* We do allow reading of a section after bfd_final_link has |
| 797 | written the contents out to disk. In that situation, rawsize is |
| 798 | just a stale version of size, so ignore it. Otherwise we must be |
| 799 | reading an input section, where rawsize, if different to size, |
| 800 | is the on-disk size. */ |
| 801 | if (abfd->direction != write_direction && section->rawsize != 0) |
| 802 | sz = section->rawsize; |
| 803 | else |
| 804 | sz = section->size; |
| 805 | if (offset + count < count |
| 806 | || offset + count > sz) |
| 807 | { |
| 808 | bfd_set_error (bfd_error_invalid_operation); |
| 809 | return FALSE; |
| 810 | } |
| 811 | |
| 812 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 |
| 813 | || bfd_bread (location, count, abfd) != count) |
| 814 | return FALSE; |
| 815 | |
| 816 | return TRUE; |
| 817 | } |
| 818 | |
| 819 | bfd_boolean |
| 820 | _bfd_generic_get_section_contents_in_window |
| 821 | (bfd *abfd ATTRIBUTE_UNUSED, |
| 822 | sec_ptr section ATTRIBUTE_UNUSED, |
| 823 | bfd_window *w ATTRIBUTE_UNUSED, |
| 824 | file_ptr offset ATTRIBUTE_UNUSED, |
| 825 | bfd_size_type count ATTRIBUTE_UNUSED) |
| 826 | { |
| 827 | #ifdef USE_MMAP |
| 828 | bfd_size_type sz; |
| 829 | |
| 830 | if (count == 0) |
| 831 | return TRUE; |
| 832 | if (abfd->xvec->_bfd_get_section_contents |
| 833 | != _bfd_generic_get_section_contents) |
| 834 | { |
| 835 | /* We don't know what changes the bfd's get_section_contents |
| 836 | method may have to make. So punt trying to map the file |
| 837 | window, and let get_section_contents do its thing. */ |
| 838 | /* @@ FIXME : If the internal window has a refcount of 1 and was |
| 839 | allocated with malloc instead of mmap, just reuse it. */ |
| 840 | bfd_free_window (w); |
| 841 | w->i = bfd_zmalloc (sizeof (bfd_window_internal)); |
| 842 | if (w->i == NULL) |
| 843 | return FALSE; |
| 844 | w->i->data = bfd_malloc (count); |
| 845 | if (w->i->data == NULL) |
| 846 | { |
| 847 | free (w->i); |
| 848 | w->i = NULL; |
| 849 | return FALSE; |
| 850 | } |
| 851 | w->i->mapped = 0; |
| 852 | w->i->refcount = 1; |
| 853 | w->size = w->i->size = count; |
| 854 | w->data = w->i->data; |
| 855 | return bfd_get_section_contents (abfd, section, w->data, offset, count); |
| 856 | } |
| 857 | if (abfd->direction != write_direction && section->rawsize != 0) |
| 858 | sz = section->rawsize; |
| 859 | else |
| 860 | sz = section->size; |
| 861 | if (offset + count > sz |
| 862 | || ! bfd_get_file_window (abfd, section->filepos + offset, count, w, |
| 863 | TRUE)) |
| 864 | return FALSE; |
| 865 | return TRUE; |
| 866 | #else |
| 867 | abort (); |
| 868 | #endif |
| 869 | } |
| 870 | |
| 871 | /* This generic function can only be used in implementations where creating |
| 872 | NEW sections is disallowed. It is useful in patching existing sections |
| 873 | in read-write files, though. See other set_section_contents functions |
| 874 | to see why it doesn't work for new sections. */ |
| 875 | bfd_boolean |
| 876 | _bfd_generic_set_section_contents (bfd *abfd, |
| 877 | sec_ptr section, |
| 878 | const void *location, |
| 879 | file_ptr offset, |
| 880 | bfd_size_type count) |
| 881 | { |
| 882 | if (count == 0) |
| 883 | return TRUE; |
| 884 | |
| 885 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 |
| 886 | || bfd_bwrite (location, count, abfd) != count) |
| 887 | return FALSE; |
| 888 | |
| 889 | return TRUE; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | INTERNAL_FUNCTION |
| 894 | bfd_log2 |
| 895 | |
| 896 | SYNOPSIS |
| 897 | unsigned int bfd_log2 (bfd_vma x); |
| 898 | |
| 899 | DESCRIPTION |
| 900 | Return the log base 2 of the value supplied, rounded up. E.g., an |
| 901 | @var{x} of 1025 returns 11. A @var{x} of 0 returns 0. |
| 902 | */ |
| 903 | |
| 904 | unsigned int |
| 905 | bfd_log2 (bfd_vma x) |
| 906 | { |
| 907 | unsigned int result = 0; |
| 908 | |
| 909 | if (x <= 1) |
| 910 | return result; |
| 911 | --x; |
| 912 | do |
| 913 | ++result; |
| 914 | while ((x >>= 1) != 0); |
| 915 | return result; |
| 916 | } |
| 917 | |
| 918 | bfd_boolean |
| 919 | bfd_generic_is_local_label_name (bfd *abfd, const char *name) |
| 920 | { |
| 921 | char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; |
| 922 | |
| 923 | return name[0] == locals_prefix; |
| 924 | } |
| 925 | |
| 926 | /* Give a warning at runtime if someone compiles code which calls |
| 927 | old routines. */ |
| 928 | |
| 929 | void |
| 930 | warn_deprecated (const char *what, |
| 931 | const char *file, |
| 932 | int line, |
| 933 | const char *func) |
| 934 | { |
| 935 | /* Poor man's tracking of functions we've already warned about. */ |
| 936 | static size_t mask = 0; |
| 937 | |
| 938 | if (~(size_t) func & ~mask) |
| 939 | { |
| 940 | fflush (stdout); |
| 941 | /* Note: separate sentences in order to allow |
| 942 | for translation into other languages. */ |
| 943 | if (func) |
| 944 | /* xgettext:c-format */ |
| 945 | fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"), |
| 946 | what, file, line, func); |
| 947 | else |
| 948 | fprintf (stderr, _("Deprecated %s called\n"), what); |
| 949 | fflush (stderr); |
| 950 | mask |= ~(size_t) func; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* Helper function for reading uleb128 encoded data. */ |
| 955 | |
| 956 | bfd_vma |
| 957 | read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED, |
| 958 | bfd_byte *buf, |
| 959 | unsigned int *bytes_read_ptr) |
| 960 | { |
| 961 | bfd_vma result; |
| 962 | unsigned int num_read; |
| 963 | unsigned int shift; |
| 964 | unsigned char byte; |
| 965 | |
| 966 | result = 0; |
| 967 | shift = 0; |
| 968 | num_read = 0; |
| 969 | do |
| 970 | { |
| 971 | byte = bfd_get_8 (abfd, buf); |
| 972 | buf++; |
| 973 | num_read++; |
| 974 | result |= (((bfd_vma) byte & 0x7f) << shift); |
| 975 | shift += 7; |
| 976 | } |
| 977 | while (byte & 0x80); |
| 978 | *bytes_read_ptr = num_read; |
| 979 | return result; |
| 980 | } |
| 981 | |
| 982 | /* Read in a LEB128 encoded value from ABFD starting at DATA. |
| 983 | If SIGN is true, return a signed LEB128 value. |
| 984 | If LENGTH_RETURN is not NULL, return in it the number of bytes read. |
| 985 | No bytes will be read at address END or beyond. */ |
| 986 | |
| 987 | bfd_vma |
| 988 | safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED, |
| 989 | bfd_byte *data, |
| 990 | unsigned int *length_return, |
| 991 | bfd_boolean sign, |
| 992 | const bfd_byte * const end) |
| 993 | { |
| 994 | bfd_vma result = 0; |
| 995 | unsigned int num_read = 0; |
| 996 | unsigned int shift = 0; |
| 997 | unsigned char byte = 0; |
| 998 | |
| 999 | while (data < end) |
| 1000 | { |
| 1001 | byte = bfd_get_8 (abfd, data); |
| 1002 | data++; |
| 1003 | num_read++; |
| 1004 | |
| 1005 | result |= ((bfd_vma) (byte & 0x7f)) << shift; |
| 1006 | |
| 1007 | shift += 7; |
| 1008 | if ((byte & 0x80) == 0) |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | if (length_return != NULL) |
| 1013 | *length_return = num_read; |
| 1014 | |
| 1015 | if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40)) |
| 1016 | result |= -((bfd_vma) 1 << shift); |
| 1017 | |
| 1018 | return result; |
| 1019 | } |
| 1020 | |
| 1021 | /* Helper function for reading sleb128 encoded data. */ |
| 1022 | |
| 1023 | bfd_signed_vma |
| 1024 | read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED, |
| 1025 | bfd_byte *buf, |
| 1026 | unsigned int *bytes_read_ptr) |
| 1027 | { |
| 1028 | bfd_vma result; |
| 1029 | unsigned int shift; |
| 1030 | unsigned int num_read; |
| 1031 | unsigned char byte; |
| 1032 | |
| 1033 | result = 0; |
| 1034 | shift = 0; |
| 1035 | num_read = 0; |
| 1036 | do |
| 1037 | { |
| 1038 | byte = bfd_get_8 (abfd, buf); |
| 1039 | buf ++; |
| 1040 | num_read ++; |
| 1041 | result |= (((bfd_vma) byte & 0x7f) << shift); |
| 1042 | shift += 7; |
| 1043 | } |
| 1044 | while (byte & 0x80); |
| 1045 | if (shift < 8 * sizeof (result) && (byte & 0x40)) |
| 1046 | result |= (((bfd_vma) -1) << shift); |
| 1047 | *bytes_read_ptr = num_read; |
| 1048 | return result; |
| 1049 | } |
| 1050 | |
| 1051 | bfd_boolean |
| 1052 | _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED, |
| 1053 | asection *isec ATTRIBUTE_UNUSED, |
| 1054 | bfd *obfd ATTRIBUTE_UNUSED, |
| 1055 | asection *osec ATTRIBUTE_UNUSED, |
| 1056 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED) |
| 1057 | { |
| 1058 | return TRUE; |
| 1059 | } |