| 1 | /* Assorted BFD support routines, only used internally. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
| 3 | 2000, 2001, 2002, 2003 |
| 4 | Free Software Foundation, Inc. |
| 5 | Written by Cygnus Support. |
| 6 | |
| 7 | This file is part of BFD, the Binary File Descriptor library. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 22 | |
| 23 | #include "bfd.h" |
| 24 | #include "sysdep.h" |
| 25 | #include "libbfd.h" |
| 26 | |
| 27 | #ifndef HAVE_GETPAGESIZE |
| 28 | #define getpagesize() 2048 |
| 29 | #endif |
| 30 | |
| 31 | /* |
| 32 | SECTION |
| 33 | Internal functions |
| 34 | |
| 35 | DESCRIPTION |
| 36 | These routines are used within BFD. |
| 37 | They are not intended for export, but are documented here for |
| 38 | completeness. |
| 39 | */ |
| 40 | |
| 41 | /* A routine which is used in target vectors for unsupported |
| 42 | operations. */ |
| 43 | |
| 44 | bfd_boolean |
| 45 | bfd_false (bfd *ignore ATTRIBUTE_UNUSED) |
| 46 | { |
| 47 | bfd_set_error (bfd_error_invalid_operation); |
| 48 | return FALSE; |
| 49 | } |
| 50 | |
| 51 | /* A routine which is used in target vectors for supported operations |
| 52 | which do not actually do anything. */ |
| 53 | |
| 54 | bfd_boolean |
| 55 | bfd_true (bfd *ignore ATTRIBUTE_UNUSED) |
| 56 | { |
| 57 | return TRUE; |
| 58 | } |
| 59 | |
| 60 | /* A routine which is used in target vectors for unsupported |
| 61 | operations which return a pointer value. */ |
| 62 | |
| 63 | void * |
| 64 | bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED) |
| 65 | { |
| 66 | bfd_set_error (bfd_error_invalid_operation); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) |
| 72 | { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | unsigned int |
| 77 | bfd_0u (bfd *ignore ATTRIBUTE_UNUSED) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | long |
| 83 | bfd_0l (bfd *ignore ATTRIBUTE_UNUSED) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* A routine which is used in target vectors for unsupported |
| 89 | operations which return -1 on error. */ |
| 90 | |
| 91 | long |
| 92 | _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 93 | { |
| 94 | bfd_set_error (bfd_error_invalid_operation); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | bfd_void (bfd *ignore ATTRIBUTE_UNUSED) |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | bfd_boolean |
| 104 | _bfd_nocore_core_file_matches_executable_p |
| 105 | (bfd *ignore_core_bfd ATTRIBUTE_UNUSED, |
| 106 | bfd *ignore_exec_bfd ATTRIBUTE_UNUSED) |
| 107 | { |
| 108 | bfd_set_error (bfd_error_invalid_operation); |
| 109 | return FALSE; |
| 110 | } |
| 111 | |
| 112 | /* Routine to handle core_file_failing_command entry point for targets |
| 113 | without core file support. */ |
| 114 | |
| 115 | char * |
| 116 | _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 117 | { |
| 118 | bfd_set_error (bfd_error_invalid_operation); |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | /* Routine to handle core_file_failing_signal entry point for targets |
| 123 | without core file support. */ |
| 124 | |
| 125 | int |
| 126 | _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 127 | { |
| 128 | bfd_set_error (bfd_error_invalid_operation); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | const bfd_target * |
| 133 | _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
| 134 | { |
| 135 | bfd_set_error (bfd_error_wrong_format); |
| 136 | return 0; |
| 137 | } |
| 138 | \f |
| 139 | /* Allocate memory using malloc. */ |
| 140 | |
| 141 | void * |
| 142 | bfd_malloc (bfd_size_type size) |
| 143 | { |
| 144 | void *ptr; |
| 145 | |
| 146 | if (size != (size_t) size) |
| 147 | { |
| 148 | bfd_set_error (bfd_error_no_memory); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | ptr = malloc ((size_t) size); |
| 153 | if (ptr == NULL && (size_t) size != 0) |
| 154 | bfd_set_error (bfd_error_no_memory); |
| 155 | |
| 156 | return ptr; |
| 157 | } |
| 158 | |
| 159 | /* Reallocate memory using realloc. */ |
| 160 | |
| 161 | void * |
| 162 | bfd_realloc (void *ptr, bfd_size_type size) |
| 163 | { |
| 164 | void *ret; |
| 165 | |
| 166 | if (size != (size_t) size) |
| 167 | { |
| 168 | bfd_set_error (bfd_error_no_memory); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | if (ptr == NULL) |
| 173 | ret = malloc ((size_t) size); |
| 174 | else |
| 175 | ret = realloc (ptr, (size_t) size); |
| 176 | |
| 177 | if (ret == NULL && (size_t) size != 0) |
| 178 | bfd_set_error (bfd_error_no_memory); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | /* Allocate memory using malloc and clear it. */ |
| 184 | |
| 185 | void * |
| 186 | bfd_zmalloc (bfd_size_type size) |
| 187 | { |
| 188 | void *ptr; |
| 189 | |
| 190 | if (size != (size_t) size) |
| 191 | { |
| 192 | bfd_set_error (bfd_error_no_memory); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | ptr = malloc ((size_t) size); |
| 197 | |
| 198 | if ((size_t) size != 0) |
| 199 | { |
| 200 | if (ptr == NULL) |
| 201 | bfd_set_error (bfd_error_no_memory); |
| 202 | else |
| 203 | memset (ptr, 0, (size_t) size); |
| 204 | } |
| 205 | |
| 206 | return ptr; |
| 207 | } |
| 208 | /* |
| 209 | INTERNAL_FUNCTION |
| 210 | bfd_write_bigendian_4byte_int |
| 211 | |
| 212 | SYNOPSIS |
| 213 | bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int); |
| 214 | |
| 215 | DESCRIPTION |
| 216 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big |
| 217 | endian order regardless of what else is going on. This is useful in |
| 218 | archives. |
| 219 | |
| 220 | */ |
| 221 | bfd_boolean |
| 222 | bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i) |
| 223 | { |
| 224 | bfd_byte buffer[4]; |
| 225 | bfd_putb32 ((bfd_vma) i, buffer); |
| 226 | return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4; |
| 227 | } |
| 228 | |
| 229 | \f |
| 230 | /** The do-it-yourself (byte) sex-change kit */ |
| 231 | |
| 232 | /* The middle letter e.g. get<b>short indicates Big or Little endian |
| 233 | target machine. It doesn't matter what the byte order of the host |
| 234 | machine is; these routines work for either. */ |
| 235 | |
| 236 | /* FIXME: Should these take a count argument? |
| 237 | Answer (gnu@cygnus.com): No, but perhaps they should be inline |
| 238 | functions in swap.h #ifdef __GNUC__. |
| 239 | Gprof them later and find out. */ |
| 240 | |
| 241 | /* |
| 242 | FUNCTION |
| 243 | bfd_put_size |
| 244 | FUNCTION |
| 245 | bfd_get_size |
| 246 | |
| 247 | DESCRIPTION |
| 248 | These macros as used for reading and writing raw data in |
| 249 | sections; each access (except for bytes) is vectored through |
| 250 | the target format of the BFD and mangled accordingly. The |
| 251 | mangling performs any necessary endian translations and |
| 252 | removes alignment restrictions. Note that types accepted and |
| 253 | returned by these macros are identical so they can be swapped |
| 254 | around in macros---for example, @file{libaout.h} defines <<GET_WORD>> |
| 255 | to either <<bfd_get_32>> or <<bfd_get_64>>. |
| 256 | |
| 257 | In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a |
| 258 | system without prototypes, the caller is responsible for making |
| 259 | sure that is true, with a cast if necessary. We don't cast |
| 260 | them in the macro definitions because that would prevent <<lint>> |
| 261 | or <<gcc -Wall>> from detecting sins such as passing a pointer. |
| 262 | To detect calling these with less than a <<bfd_vma>>, use |
| 263 | <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. |
| 264 | |
| 265 | . |
| 266 | .{* Byte swapping macros for user section data. *} |
| 267 | . |
| 268 | .#define bfd_put_8(abfd, val, ptr) \ |
| 269 | . ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val))) |
| 270 | .#define bfd_put_signed_8 \ |
| 271 | . bfd_put_8 |
| 272 | .#define bfd_get_8(abfd, ptr) \ |
| 273 | . (*(unsigned char *) (ptr) & 0xff) |
| 274 | .#define bfd_get_signed_8(abfd, ptr) \ |
| 275 | . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80) |
| 276 | . |
| 277 | .#define bfd_put_16(abfd, val, ptr) \ |
| 278 | . BFD_SEND (abfd, bfd_putx16, ((val),(ptr))) |
| 279 | .#define bfd_put_signed_16 \ |
| 280 | . bfd_put_16 |
| 281 | .#define bfd_get_16(abfd, ptr) \ |
| 282 | . BFD_SEND (abfd, bfd_getx16, (ptr)) |
| 283 | .#define bfd_get_signed_16(abfd, ptr) \ |
| 284 | . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) |
| 285 | . |
| 286 | .#define bfd_put_32(abfd, val, ptr) \ |
| 287 | . BFD_SEND (abfd, bfd_putx32, ((val),(ptr))) |
| 288 | .#define bfd_put_signed_32 \ |
| 289 | . bfd_put_32 |
| 290 | .#define bfd_get_32(abfd, ptr) \ |
| 291 | . BFD_SEND (abfd, bfd_getx32, (ptr)) |
| 292 | .#define bfd_get_signed_32(abfd, ptr) \ |
| 293 | . BFD_SEND (abfd, bfd_getx_signed_32, (ptr)) |
| 294 | . |
| 295 | .#define bfd_put_64(abfd, val, ptr) \ |
| 296 | . BFD_SEND (abfd, bfd_putx64, ((val), (ptr))) |
| 297 | .#define bfd_put_signed_64 \ |
| 298 | . bfd_put_64 |
| 299 | .#define bfd_get_64(abfd, ptr) \ |
| 300 | . BFD_SEND (abfd, bfd_getx64, (ptr)) |
| 301 | .#define bfd_get_signed_64(abfd, ptr) \ |
| 302 | . BFD_SEND (abfd, bfd_getx_signed_64, (ptr)) |
| 303 | . |
| 304 | .#define bfd_get(bits, abfd, ptr) \ |
| 305 | . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \ |
| 306 | . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \ |
| 307 | . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \ |
| 308 | . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \ |
| 309 | . : (abort (), (bfd_vma) - 1)) |
| 310 | . |
| 311 | .#define bfd_put(bits, abfd, val, ptr) \ |
| 312 | . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \ |
| 313 | . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \ |
| 314 | . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \ |
| 315 | . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \ |
| 316 | . : (abort (), (void) 0)) |
| 317 | . |
| 318 | */ |
| 319 | |
| 320 | /* |
| 321 | FUNCTION |
| 322 | bfd_h_put_size |
| 323 | bfd_h_get_size |
| 324 | |
| 325 | DESCRIPTION |
| 326 | These macros have the same function as their <<bfd_get_x>> |
| 327 | brethren, except that they are used for removing information |
| 328 | for the header records of object files. Believe it or not, |
| 329 | some object files keep their header records in big endian |
| 330 | order and their data in little endian order. |
| 331 | . |
| 332 | .{* Byte swapping macros for file header data. *} |
| 333 | . |
| 334 | .#define bfd_h_put_8(abfd, val, ptr) \ |
| 335 | . bfd_put_8 (abfd, val, ptr) |
| 336 | .#define bfd_h_put_signed_8(abfd, val, ptr) \ |
| 337 | . bfd_put_8 (abfd, val, ptr) |
| 338 | .#define bfd_h_get_8(abfd, ptr) \ |
| 339 | . bfd_get_8 (abfd, ptr) |
| 340 | .#define bfd_h_get_signed_8(abfd, ptr) \ |
| 341 | . bfd_get_signed_8 (abfd, ptr) |
| 342 | . |
| 343 | .#define bfd_h_put_16(abfd, val, ptr) \ |
| 344 | . BFD_SEND (abfd, bfd_h_putx16, (val, ptr)) |
| 345 | .#define bfd_h_put_signed_16 \ |
| 346 | . bfd_h_put_16 |
| 347 | .#define bfd_h_get_16(abfd, ptr) \ |
| 348 | . BFD_SEND (abfd, bfd_h_getx16, (ptr)) |
| 349 | .#define bfd_h_get_signed_16(abfd, ptr) \ |
| 350 | . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr)) |
| 351 | . |
| 352 | .#define bfd_h_put_32(abfd, val, ptr) \ |
| 353 | . BFD_SEND (abfd, bfd_h_putx32, (val, ptr)) |
| 354 | .#define bfd_h_put_signed_32 \ |
| 355 | . bfd_h_put_32 |
| 356 | .#define bfd_h_get_32(abfd, ptr) \ |
| 357 | . BFD_SEND (abfd, bfd_h_getx32, (ptr)) |
| 358 | .#define bfd_h_get_signed_32(abfd, ptr) \ |
| 359 | . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr)) |
| 360 | . |
| 361 | .#define bfd_h_put_64(abfd, val, ptr) \ |
| 362 | . BFD_SEND (abfd, bfd_h_putx64, (val, ptr)) |
| 363 | .#define bfd_h_put_signed_64 \ |
| 364 | . bfd_h_put_64 |
| 365 | .#define bfd_h_get_64(abfd, ptr) \ |
| 366 | . BFD_SEND (abfd, bfd_h_getx64, (ptr)) |
| 367 | .#define bfd_h_get_signed_64(abfd, ptr) \ |
| 368 | . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr)) |
| 369 | . |
| 370 | .{* Refinements on the above, which should eventually go away. Save |
| 371 | . cluttering the source with (bfd_vma) and (bfd_byte *) casts. *} |
| 372 | . |
| 373 | .#define H_PUT_64(abfd, val, where) \ |
| 374 | . bfd_h_put_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) |
| 375 | . |
| 376 | .#define H_PUT_32(abfd, val, where) \ |
| 377 | . bfd_h_put_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) |
| 378 | . |
| 379 | .#define H_PUT_16(abfd, val, where) \ |
| 380 | . bfd_h_put_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) |
| 381 | . |
| 382 | .#define H_PUT_8 bfd_h_put_8 |
| 383 | . |
| 384 | .#define H_PUT_S64(abfd, val, where) \ |
| 385 | . bfd_h_put_signed_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) |
| 386 | . |
| 387 | .#define H_PUT_S32(abfd, val, where) \ |
| 388 | . bfd_h_put_signed_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) |
| 389 | . |
| 390 | .#define H_PUT_S16(abfd, val, where) \ |
| 391 | . bfd_h_put_signed_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where)) |
| 392 | . |
| 393 | .#define H_PUT_S8 bfd_h_put_signed_8 |
| 394 | . |
| 395 | .#define H_GET_64(abfd, where) \ |
| 396 | . bfd_h_get_64 ((abfd), (bfd_byte *) (where)) |
| 397 | . |
| 398 | .#define H_GET_32(abfd, where) \ |
| 399 | . bfd_h_get_32 ((abfd), (bfd_byte *) (where)) |
| 400 | . |
| 401 | .#define H_GET_16(abfd, where) \ |
| 402 | . bfd_h_get_16 ((abfd), (bfd_byte *) (where)) |
| 403 | . |
| 404 | .#define H_GET_8 bfd_h_get_8 |
| 405 | . |
| 406 | .#define H_GET_S64(abfd, where) \ |
| 407 | . bfd_h_get_signed_64 ((abfd), (bfd_byte *) (where)) |
| 408 | . |
| 409 | .#define H_GET_S32(abfd, where) \ |
| 410 | . bfd_h_get_signed_32 ((abfd), (bfd_byte *) (where)) |
| 411 | . |
| 412 | .#define H_GET_S16(abfd, where) \ |
| 413 | . bfd_h_get_signed_16 ((abfd), (bfd_byte *) (where)) |
| 414 | . |
| 415 | .#define H_GET_S8 bfd_h_get_signed_8 |
| 416 | . |
| 417 | .*/ |
| 418 | |
| 419 | /* Sign extension to bfd_signed_vma. */ |
| 420 | #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000) |
| 421 | #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000) |
| 422 | #define EIGHT_GAZILLION ((BFD_HOST_64_BIT) 1 << 63) |
| 423 | #define COERCE64(x) \ |
| 424 | (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION) |
| 425 | |
| 426 | bfd_vma |
| 427 | bfd_getb16 (const bfd_byte *addr) |
| 428 | { |
| 429 | return (addr[0] << 8) | addr[1]; |
| 430 | } |
| 431 | |
| 432 | bfd_vma |
| 433 | bfd_getl16 (const bfd_byte *addr) |
| 434 | { |
| 435 | return (addr[1] << 8) | addr[0]; |
| 436 | } |
| 437 | |
| 438 | bfd_signed_vma |
| 439 | bfd_getb_signed_16 (const bfd_byte *addr) |
| 440 | { |
| 441 | return COERCE16 ((addr[0] << 8) | addr[1]); |
| 442 | } |
| 443 | |
| 444 | bfd_signed_vma |
| 445 | bfd_getl_signed_16 (const bfd_byte *addr) |
| 446 | { |
| 447 | return COERCE16 ((addr[1] << 8) | addr[0]); |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | bfd_putb16 (bfd_vma data, bfd_byte *addr) |
| 452 | { |
| 453 | addr[0] = (bfd_byte) (data >> 8); |
| 454 | addr[1] = (bfd_byte) data; |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | bfd_putl16 (bfd_vma data, bfd_byte *addr) |
| 459 | { |
| 460 | addr[0] = (bfd_byte) data; |
| 461 | addr[1] = (bfd_byte) (data >> 8); |
| 462 | } |
| 463 | |
| 464 | bfd_vma |
| 465 | bfd_getb32 (const bfd_byte *addr) |
| 466 | { |
| 467 | unsigned long v; |
| 468 | |
| 469 | v = (unsigned long) addr[0] << 24; |
| 470 | v |= (unsigned long) addr[1] << 16; |
| 471 | v |= (unsigned long) addr[2] << 8; |
| 472 | v |= (unsigned long) addr[3]; |
| 473 | return v; |
| 474 | } |
| 475 | |
| 476 | bfd_vma |
| 477 | bfd_getl32 (const bfd_byte *addr) |
| 478 | { |
| 479 | unsigned long v; |
| 480 | |
| 481 | v = (unsigned long) addr[0]; |
| 482 | v |= (unsigned long) addr[1] << 8; |
| 483 | v |= (unsigned long) addr[2] << 16; |
| 484 | v |= (unsigned long) addr[3] << 24; |
| 485 | return v; |
| 486 | } |
| 487 | |
| 488 | bfd_signed_vma |
| 489 | bfd_getb_signed_32 (const bfd_byte *addr) |
| 490 | { |
| 491 | unsigned long v; |
| 492 | |
| 493 | v = (unsigned long) addr[0] << 24; |
| 494 | v |= (unsigned long) addr[1] << 16; |
| 495 | v |= (unsigned long) addr[2] << 8; |
| 496 | v |= (unsigned long) addr[3]; |
| 497 | return COERCE32 (v); |
| 498 | } |
| 499 | |
| 500 | bfd_signed_vma |
| 501 | bfd_getl_signed_32 (const bfd_byte *addr) |
| 502 | { |
| 503 | unsigned long v; |
| 504 | |
| 505 | v = (unsigned long) addr[0]; |
| 506 | v |= (unsigned long) addr[1] << 8; |
| 507 | v |= (unsigned long) addr[2] << 16; |
| 508 | v |= (unsigned long) addr[3] << 24; |
| 509 | return COERCE32 (v); |
| 510 | } |
| 511 | |
| 512 | bfd_vma |
| 513 | bfd_getb64 (const bfd_byte *addr ATTRIBUTE_UNUSED) |
| 514 | { |
| 515 | #ifdef BFD64 |
| 516 | bfd_vma v; |
| 517 | |
| 518 | v = addr[0]; v <<= 8; |
| 519 | v |= addr[1]; v <<= 8; |
| 520 | v |= addr[2]; v <<= 8; |
| 521 | v |= addr[3]; v <<= 8; |
| 522 | v |= addr[4]; v <<= 8; |
| 523 | v |= addr[5]; v <<= 8; |
| 524 | v |= addr[6]; v <<= 8; |
| 525 | v |= addr[7]; |
| 526 | |
| 527 | return v; |
| 528 | #else |
| 529 | BFD_FAIL(); |
| 530 | return 0; |
| 531 | #endif |
| 532 | } |
| 533 | |
| 534 | bfd_vma |
| 535 | bfd_getl64 (const bfd_byte *addr ATTRIBUTE_UNUSED) |
| 536 | { |
| 537 | #ifdef BFD64 |
| 538 | bfd_vma v; |
| 539 | |
| 540 | v = addr[7]; v <<= 8; |
| 541 | v |= addr[6]; v <<= 8; |
| 542 | v |= addr[5]; v <<= 8; |
| 543 | v |= addr[4]; v <<= 8; |
| 544 | v |= addr[3]; v <<= 8; |
| 545 | v |= addr[2]; v <<= 8; |
| 546 | v |= addr[1]; v <<= 8; |
| 547 | v |= addr[0]; |
| 548 | |
| 549 | return v; |
| 550 | #else |
| 551 | BFD_FAIL(); |
| 552 | return 0; |
| 553 | #endif |
| 554 | |
| 555 | } |
| 556 | |
| 557 | bfd_signed_vma |
| 558 | bfd_getb_signed_64 (addr) |
| 559 | register const bfd_byte *addr ATTRIBUTE_UNUSED; |
| 560 | { |
| 561 | #ifdef BFD64 |
| 562 | bfd_vma v; |
| 563 | |
| 564 | v = addr[0]; v <<= 8; |
| 565 | v |= addr[1]; v <<= 8; |
| 566 | v |= addr[2]; v <<= 8; |
| 567 | v |= addr[3]; v <<= 8; |
| 568 | v |= addr[4]; v <<= 8; |
| 569 | v |= addr[5]; v <<= 8; |
| 570 | v |= addr[6]; v <<= 8; |
| 571 | v |= addr[7]; |
| 572 | |
| 573 | return COERCE64 (v); |
| 574 | #else |
| 575 | BFD_FAIL(); |
| 576 | return 0; |
| 577 | #endif |
| 578 | } |
| 579 | |
| 580 | bfd_signed_vma |
| 581 | bfd_getl_signed_64 (addr) |
| 582 | register const bfd_byte *addr ATTRIBUTE_UNUSED; |
| 583 | { |
| 584 | #ifdef BFD64 |
| 585 | bfd_vma v; |
| 586 | |
| 587 | v = addr[7]; v <<= 8; |
| 588 | v |= addr[6]; v <<= 8; |
| 589 | v |= addr[5]; v <<= 8; |
| 590 | v |= addr[4]; v <<= 8; |
| 591 | v |= addr[3]; v <<= 8; |
| 592 | v |= addr[2]; v <<= 8; |
| 593 | v |= addr[1]; v <<= 8; |
| 594 | v |= addr[0]; |
| 595 | |
| 596 | return COERCE64 (v); |
| 597 | #else |
| 598 | BFD_FAIL(); |
| 599 | return 0; |
| 600 | #endif |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | bfd_putb32 (bfd_vma data, bfd_byte *addr) |
| 605 | { |
| 606 | addr[0] = (bfd_byte) (data >> 24); |
| 607 | addr[1] = (bfd_byte) (data >> 16); |
| 608 | addr[2] = (bfd_byte) (data >> 8); |
| 609 | addr[3] = (bfd_byte) data; |
| 610 | } |
| 611 | |
| 612 | void |
| 613 | bfd_putl32 (bfd_vma data, bfd_byte *addr) |
| 614 | { |
| 615 | addr[0] = (bfd_byte) data; |
| 616 | addr[1] = (bfd_byte) (data >> 8); |
| 617 | addr[2] = (bfd_byte) (data >> 16); |
| 618 | addr[3] = (bfd_byte) (data >> 24); |
| 619 | } |
| 620 | |
| 621 | void |
| 622 | bfd_putb64 (bfd_vma data ATTRIBUTE_UNUSED, bfd_byte *addr ATTRIBUTE_UNUSED) |
| 623 | { |
| 624 | #ifdef BFD64 |
| 625 | addr[0] = (bfd_byte) (data >> (7*8)); |
| 626 | addr[1] = (bfd_byte) (data >> (6*8)); |
| 627 | addr[2] = (bfd_byte) (data >> (5*8)); |
| 628 | addr[3] = (bfd_byte) (data >> (4*8)); |
| 629 | addr[4] = (bfd_byte) (data >> (3*8)); |
| 630 | addr[5] = (bfd_byte) (data >> (2*8)); |
| 631 | addr[6] = (bfd_byte) (data >> (1*8)); |
| 632 | addr[7] = (bfd_byte) (data >> (0*8)); |
| 633 | #else |
| 634 | BFD_FAIL(); |
| 635 | #endif |
| 636 | } |
| 637 | |
| 638 | void |
| 639 | bfd_putl64 (bfd_vma data ATTRIBUTE_UNUSED, bfd_byte *addr ATTRIBUTE_UNUSED) |
| 640 | { |
| 641 | #ifdef BFD64 |
| 642 | addr[7] = (bfd_byte) (data >> (7*8)); |
| 643 | addr[6] = (bfd_byte) (data >> (6*8)); |
| 644 | addr[5] = (bfd_byte) (data >> (5*8)); |
| 645 | addr[4] = (bfd_byte) (data >> (4*8)); |
| 646 | addr[3] = (bfd_byte) (data >> (3*8)); |
| 647 | addr[2] = (bfd_byte) (data >> (2*8)); |
| 648 | addr[1] = (bfd_byte) (data >> (1*8)); |
| 649 | addr[0] = (bfd_byte) (data >> (0*8)); |
| 650 | #else |
| 651 | BFD_FAIL(); |
| 652 | #endif |
| 653 | } |
| 654 | |
| 655 | void |
| 656 | bfd_put_bits (bfd_vma data, bfd_byte *addr, int bits, bfd_boolean big_p) |
| 657 | { |
| 658 | int i; |
| 659 | int bytes; |
| 660 | |
| 661 | if (bits % 8 != 0) |
| 662 | abort (); |
| 663 | |
| 664 | bytes = bits / 8; |
| 665 | for (i = 0; i < bytes; i++) |
| 666 | { |
| 667 | int index = big_p ? bytes - i - 1 : i; |
| 668 | |
| 669 | addr[index] = (bfd_byte) data; |
| 670 | data >>= 8; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | bfd_vma |
| 675 | bfd_get_bits (bfd_byte *addr, int bits, bfd_boolean big_p) |
| 676 | { |
| 677 | bfd_vma data; |
| 678 | int i; |
| 679 | int bytes; |
| 680 | |
| 681 | if (bits % 8 != 0) |
| 682 | abort (); |
| 683 | |
| 684 | data = 0; |
| 685 | bytes = bits / 8; |
| 686 | for (i = 0; i < bytes; i++) |
| 687 | { |
| 688 | int index = big_p ? i : bytes - i - 1; |
| 689 | |
| 690 | data = (data << 8) | addr[index]; |
| 691 | } |
| 692 | |
| 693 | return data; |
| 694 | } |
| 695 | \f |
| 696 | /* Default implementation */ |
| 697 | |
| 698 | bfd_boolean |
| 699 | _bfd_generic_get_section_contents (bfd *abfd, |
| 700 | sec_ptr section, |
| 701 | void *location, |
| 702 | file_ptr offset, |
| 703 | bfd_size_type count) |
| 704 | { |
| 705 | if (count == 0) |
| 706 | return TRUE; |
| 707 | |
| 708 | if (offset + count > section->_raw_size) |
| 709 | { |
| 710 | bfd_set_error (bfd_error_invalid_operation); |
| 711 | return FALSE; |
| 712 | } |
| 713 | |
| 714 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 |
| 715 | || bfd_bread (location, count, abfd) != count) |
| 716 | return FALSE; |
| 717 | |
| 718 | return TRUE; |
| 719 | } |
| 720 | |
| 721 | bfd_boolean |
| 722 | _bfd_generic_get_section_contents_in_window |
| 723 | (bfd *abfd ATTRIBUTE_UNUSED, |
| 724 | sec_ptr section ATTRIBUTE_UNUSED, |
| 725 | bfd_window *w ATTRIBUTE_UNUSED, |
| 726 | file_ptr offset ATTRIBUTE_UNUSED, |
| 727 | bfd_size_type count ATTRIBUTE_UNUSED) |
| 728 | { |
| 729 | #ifdef USE_MMAP |
| 730 | if (count == 0) |
| 731 | return TRUE; |
| 732 | if (abfd->xvec->_bfd_get_section_contents |
| 733 | != _bfd_generic_get_section_contents) |
| 734 | { |
| 735 | /* We don't know what changes the bfd's get_section_contents |
| 736 | method may have to make. So punt trying to map the file |
| 737 | window, and let get_section_contents do its thing. */ |
| 738 | /* @@ FIXME : If the internal window has a refcount of 1 and was |
| 739 | allocated with malloc instead of mmap, just reuse it. */ |
| 740 | bfd_free_window (w); |
| 741 | w->i = bfd_zmalloc (sizeof (bfd_window_internal)); |
| 742 | if (w->i == NULL) |
| 743 | return FALSE; |
| 744 | w->i->data = bfd_malloc (count); |
| 745 | if (w->i->data == NULL) |
| 746 | { |
| 747 | free (w->i); |
| 748 | w->i = NULL; |
| 749 | return FALSE; |
| 750 | } |
| 751 | w->i->mapped = 0; |
| 752 | w->i->refcount = 1; |
| 753 | w->size = w->i->size = count; |
| 754 | w->data = w->i->data; |
| 755 | return bfd_get_section_contents (abfd, section, w->data, offset, count); |
| 756 | } |
| 757 | if (offset + count > section->_raw_size |
| 758 | || ! bfd_get_file_window (abfd, section->filepos + offset, count, w, |
| 759 | TRUE)) |
| 760 | return FALSE; |
| 761 | return TRUE; |
| 762 | #else |
| 763 | abort (); |
| 764 | #endif |
| 765 | } |
| 766 | |
| 767 | /* This generic function can only be used in implementations where creating |
| 768 | NEW sections is disallowed. It is useful in patching existing sections |
| 769 | in read-write files, though. See other set_section_contents functions |
| 770 | to see why it doesn't work for new sections. */ |
| 771 | bfd_boolean |
| 772 | _bfd_generic_set_section_contents (bfd *abfd, |
| 773 | sec_ptr section, |
| 774 | const void *location, |
| 775 | file_ptr offset, |
| 776 | bfd_size_type count) |
| 777 | { |
| 778 | if (count == 0) |
| 779 | return TRUE; |
| 780 | |
| 781 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 |
| 782 | || bfd_bwrite (location, count, abfd) != count) |
| 783 | return FALSE; |
| 784 | |
| 785 | return TRUE; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | INTERNAL_FUNCTION |
| 790 | bfd_log2 |
| 791 | |
| 792 | SYNOPSIS |
| 793 | unsigned int bfd_log2 (bfd_vma x); |
| 794 | |
| 795 | DESCRIPTION |
| 796 | Return the log base 2 of the value supplied, rounded up. E.g., an |
| 797 | @var{x} of 1025 returns 11. A @var{x} of 0 returns 0. |
| 798 | */ |
| 799 | |
| 800 | unsigned int |
| 801 | bfd_log2 (bfd_vma x) |
| 802 | { |
| 803 | unsigned int result = 0; |
| 804 | |
| 805 | while ((x = (x >> 1)) != 0) |
| 806 | ++result; |
| 807 | return result; |
| 808 | } |
| 809 | |
| 810 | bfd_boolean |
| 811 | bfd_generic_is_local_label_name (bfd *abfd, const char *name) |
| 812 | { |
| 813 | char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; |
| 814 | |
| 815 | return name[0] == locals_prefix; |
| 816 | } |
| 817 | |
| 818 | /* Can be used from / for bfd_merge_private_bfd_data to check that |
| 819 | endianness matches between input and output file. Returns |
| 820 | TRUE for a match, otherwise returns FALSE and emits an error. */ |
| 821 | bfd_boolean |
| 822 | _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd) |
| 823 | { |
| 824 | if (ibfd->xvec->byteorder != obfd->xvec->byteorder |
| 825 | && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN |
| 826 | && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN) |
| 827 | { |
| 828 | const char *msg; |
| 829 | |
| 830 | if (bfd_big_endian (ibfd)) |
| 831 | msg = _("%s: compiled for a big endian system and target is little endian"); |
| 832 | else |
| 833 | msg = _("%s: compiled for a little endian system and target is big endian"); |
| 834 | |
| 835 | (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd)); |
| 836 | |
| 837 | bfd_set_error (bfd_error_wrong_format); |
| 838 | return FALSE; |
| 839 | } |
| 840 | |
| 841 | return TRUE; |
| 842 | } |
| 843 | |
| 844 | /* Give a warning at runtime if someone compiles code which calls |
| 845 | old routines. */ |
| 846 | |
| 847 | void |
| 848 | warn_deprecated (const char *what, |
| 849 | const char *file, |
| 850 | int line, |
| 851 | const char *func) |
| 852 | { |
| 853 | /* Poor man's tracking of functions we've already warned about. */ |
| 854 | static size_t mask = 0; |
| 855 | |
| 856 | if (~(size_t) func & ~mask) |
| 857 | { |
| 858 | /* Note: separate sentences in order to allow |
| 859 | for translation into other languages. */ |
| 860 | if (func) |
| 861 | fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"), |
| 862 | what, file, line, func); |
| 863 | else |
| 864 | fprintf (stderr, _("Deprecated %s called\n"), what); |
| 865 | mask |= ~(size_t) func; |
| 866 | } |
| 867 | } |