| 1 | /* Low-level I/O routines for BFDs. |
| 2 | |
| 3 | Copyright (C) 1990-2020 Free Software Foundation, Inc. |
| 4 | |
| 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 3 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., 51 Franklin Street - Fifth Floor, Boston, |
| 22 | MA 02110-1301, USA. */ |
| 23 | |
| 24 | #include "sysdep.h" |
| 25 | #include <limits.h> |
| 26 | #include "bfd.h" |
| 27 | #include "libbfd.h" |
| 28 | #include "aout/ar.h" |
| 29 | #if defined (_WIN32) |
| 30 | #include <windows.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifndef S_IXUSR |
| 34 | #define S_IXUSR 0100 /* Execute by owner. */ |
| 35 | #endif |
| 36 | #ifndef S_IXGRP |
| 37 | #define S_IXGRP 0010 /* Execute by group. */ |
| 38 | #endif |
| 39 | #ifndef S_IXOTH |
| 40 | #define S_IXOTH 0001 /* Execute by others. */ |
| 41 | #endif |
| 42 | |
| 43 | #ifndef FD_CLOEXEC |
| 44 | #define FD_CLOEXEC 1 |
| 45 | #endif |
| 46 | |
| 47 | file_ptr |
| 48 | _bfd_real_ftell (FILE *file) |
| 49 | { |
| 50 | #if defined (HAVE_FTELLO64) |
| 51 | return ftello64 (file); |
| 52 | #elif defined (HAVE_FTELLO) |
| 53 | return ftello (file); |
| 54 | #else |
| 55 | return ftell (file); |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | int |
| 60 | _bfd_real_fseek (FILE *file, file_ptr offset, int whence) |
| 61 | { |
| 62 | #if defined (HAVE_FSEEKO64) |
| 63 | return fseeko64 (file, offset, whence); |
| 64 | #elif defined (HAVE_FSEEKO) |
| 65 | return fseeko (file, offset, whence); |
| 66 | #else |
| 67 | return fseek (file, offset, whence); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in |
| 72 | which case nothing is done. */ |
| 73 | static FILE * |
| 74 | close_on_exec (FILE *file) |
| 75 | { |
| 76 | #if defined (HAVE_FILENO) && defined (F_GETFD) |
| 77 | if (file) |
| 78 | { |
| 79 | int fd = fileno (file); |
| 80 | int old = fcntl (fd, F_GETFD, 0); |
| 81 | if (old >= 0) |
| 82 | fcntl (fd, F_SETFD, old | FD_CLOEXEC); |
| 83 | } |
| 84 | #endif |
| 85 | return file; |
| 86 | } |
| 87 | |
| 88 | FILE * |
| 89 | _bfd_real_fopen (const char *filename, const char *modes) |
| 90 | { |
| 91 | #ifdef VMS |
| 92 | char *vms_attr; |
| 93 | |
| 94 | /* On VMS, fopen allows file attributes as optional arguments. |
| 95 | We need to use them but we'd better to use the common prototype. |
| 96 | In fopen-vms.h, they are separated from the mode with a comma. |
| 97 | Split here. */ |
| 98 | vms_attr = strchr (modes, ','); |
| 99 | if (vms_attr != NULL) |
| 100 | { |
| 101 | /* Attributes found. Split. */ |
| 102 | size_t modes_len = strlen (modes) + 1; |
| 103 | char attrs[modes_len + 1]; |
| 104 | char *at[3]; |
| 105 | int i; |
| 106 | |
| 107 | memcpy (attrs, modes, modes_len); |
| 108 | at[0] = attrs; |
| 109 | for (i = 0; i < 2; i++) |
| 110 | { |
| 111 | at[i + 1] = strchr (at[i], ','); |
| 112 | BFD_ASSERT (at[i + 1] != NULL); |
| 113 | *(at[i + 1]++) = 0; /* Replace ',' with a nul, and skip it. */ |
| 114 | } |
| 115 | return close_on_exec (fopen (filename, at[0], at[1], at[2])); |
| 116 | } |
| 117 | |
| 118 | #elif defined (_WIN32) |
| 119 | size_t filelen = strlen (filename) + 1; |
| 120 | |
| 121 | if (filelen > MAX_PATH - 1) |
| 122 | { |
| 123 | FILE *file; |
| 124 | char* fullpath = (char *) malloc (filelen + 8); |
| 125 | |
| 126 | /* Add a Microsoft recommended prefix that |
| 127 | will allow the extra-long path to work. */ |
| 128 | strcpy (fullpath, "\\\\?\\"); |
| 129 | strcat (fullpath, filename); |
| 130 | file = close_on_exec (fopen (fullpath, modes)); |
| 131 | free (fullpath); |
| 132 | return file; |
| 133 | } |
| 134 | |
| 135 | #elif defined (HAVE_FOPEN64) |
| 136 | return close_on_exec (fopen64 (filename, modes)); |
| 137 | #endif |
| 138 | |
| 139 | return close_on_exec (fopen (filename, modes)); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | INTERNAL_DEFINITION |
| 144 | struct bfd_iovec |
| 145 | |
| 146 | DESCRIPTION |
| 147 | |
| 148 | The <<struct bfd_iovec>> contains the internal file I/O class. |
| 149 | Each <<BFD>> has an instance of this class and all file I/O is |
| 150 | routed through it (it is assumed that the instance implements |
| 151 | all methods listed below). |
| 152 | |
| 153 | .struct bfd_iovec |
| 154 | .{ |
| 155 | . {* To avoid problems with macros, a "b" rather than "f" |
| 156 | . prefix is prepended to each method name. *} |
| 157 | . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching |
| 158 | . bytes starting at PTR. Return the number of bytes actually |
| 159 | . transfered (a read past end-of-file returns less than NBYTES), |
| 160 | . or -1 (setting <<bfd_error>>) if an error occurs. *} |
| 161 | . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes); |
| 162 | . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr, |
| 163 | . file_ptr nbytes); |
| 164 | . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>> |
| 165 | . if an error occurs. *} |
| 166 | . file_ptr (*btell) (struct bfd *abfd); |
| 167 | . {* For the following, on successful completion a value of 0 is returned. |
| 168 | . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *} |
| 169 | . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence); |
| 170 | . int (*bclose) (struct bfd *abfd); |
| 171 | . int (*bflush) (struct bfd *abfd); |
| 172 | . int (*bstat) (struct bfd *abfd, struct stat *sb); |
| 173 | . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual |
| 174 | . mmap parameter, except that LEN and OFFSET do not need to be page |
| 175 | . aligned. Returns (void *)-1 on failure, mmapped address on success. |
| 176 | . Also write in MAP_ADDR the address of the page aligned buffer and in |
| 177 | . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and |
| 178 | . MAP_LEN to unmap. *} |
| 179 | . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len, |
| 180 | . int prot, int flags, file_ptr offset, |
| 181 | . void **map_addr, bfd_size_type *map_len); |
| 182 | .}; |
| 183 | |
| 184 | .extern const struct bfd_iovec _bfd_memory_iovec; |
| 185 | |
| 186 | */ |
| 187 | |
| 188 | |
| 189 | /* Return value is amount read. */ |
| 190 | |
| 191 | bfd_size_type |
| 192 | bfd_bread (void *ptr, bfd_size_type size, bfd *abfd) |
| 193 | { |
| 194 | file_ptr nread; |
| 195 | bfd *element_bfd = abfd; |
| 196 | ufile_ptr offset = 0; |
| 197 | |
| 198 | while (abfd->my_archive != NULL |
| 199 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 200 | { |
| 201 | offset += abfd->origin; |
| 202 | abfd = abfd->my_archive; |
| 203 | } |
| 204 | |
| 205 | /* If this is an archive element, don't read past the end of |
| 206 | this element. */ |
| 207 | if (element_bfd->arelt_data != NULL) |
| 208 | { |
| 209 | bfd_size_type maxbytes = arelt_size (element_bfd); |
| 210 | |
| 211 | if (abfd->where < offset || abfd->where - offset >= maxbytes) |
| 212 | { |
| 213 | bfd_set_error (bfd_error_invalid_operation); |
| 214 | return -1; |
| 215 | } |
| 216 | if (abfd->where - offset + size > maxbytes) |
| 217 | size = maxbytes - (abfd->where - offset); |
| 218 | } |
| 219 | |
| 220 | if (abfd->iovec == NULL) |
| 221 | { |
| 222 | bfd_set_error (bfd_error_invalid_operation); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | nread = abfd->iovec->bread (abfd, ptr, size); |
| 227 | if (nread != -1) |
| 228 | abfd->where += nread; |
| 229 | |
| 230 | return nread; |
| 231 | } |
| 232 | |
| 233 | bfd_size_type |
| 234 | bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd) |
| 235 | { |
| 236 | file_ptr nwrote; |
| 237 | |
| 238 | while (abfd->my_archive != NULL |
| 239 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 240 | abfd = abfd->my_archive; |
| 241 | |
| 242 | if (abfd->iovec == NULL) |
| 243 | { |
| 244 | bfd_set_error (bfd_error_invalid_operation); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | nwrote = abfd->iovec->bwrite (abfd, ptr, size); |
| 249 | if (nwrote != -1) |
| 250 | abfd->where += nwrote; |
| 251 | if ((bfd_size_type) nwrote != size) |
| 252 | { |
| 253 | #ifdef ENOSPC |
| 254 | errno = ENOSPC; |
| 255 | #endif |
| 256 | bfd_set_error (bfd_error_system_call); |
| 257 | } |
| 258 | return nwrote; |
| 259 | } |
| 260 | |
| 261 | file_ptr |
| 262 | bfd_tell (bfd *abfd) |
| 263 | { |
| 264 | ufile_ptr offset = 0; |
| 265 | file_ptr ptr; |
| 266 | |
| 267 | while (abfd->my_archive != NULL |
| 268 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 269 | { |
| 270 | offset += abfd->origin; |
| 271 | abfd = abfd->my_archive; |
| 272 | } |
| 273 | |
| 274 | if (abfd->iovec == NULL) |
| 275 | return 0; |
| 276 | |
| 277 | ptr = abfd->iovec->btell (abfd); |
| 278 | abfd->where = ptr; |
| 279 | return ptr - offset; |
| 280 | } |
| 281 | |
| 282 | int |
| 283 | bfd_flush (bfd *abfd) |
| 284 | { |
| 285 | while (abfd->my_archive != NULL |
| 286 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 287 | abfd = abfd->my_archive; |
| 288 | |
| 289 | if (abfd->iovec == NULL) |
| 290 | return 0; |
| 291 | |
| 292 | return abfd->iovec->bflush (abfd); |
| 293 | } |
| 294 | |
| 295 | /* Returns 0 for success, negative value for failure (in which case |
| 296 | bfd_get_error can retrieve the error code). */ |
| 297 | int |
| 298 | bfd_stat (bfd *abfd, struct stat *statbuf) |
| 299 | { |
| 300 | int result; |
| 301 | |
| 302 | while (abfd->my_archive != NULL |
| 303 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 304 | abfd = abfd->my_archive; |
| 305 | |
| 306 | if (abfd->iovec == NULL) |
| 307 | { |
| 308 | bfd_set_error (bfd_error_invalid_operation); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | result = abfd->iovec->bstat (abfd, statbuf); |
| 313 | if (result < 0) |
| 314 | bfd_set_error (bfd_error_system_call); |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | /* Returns 0 for success, nonzero for failure (in which case bfd_get_error |
| 319 | can retrieve the error code). */ |
| 320 | |
| 321 | int |
| 322 | bfd_seek (bfd *abfd, file_ptr position, int direction) |
| 323 | { |
| 324 | int result; |
| 325 | ufile_ptr offset = 0; |
| 326 | |
| 327 | while (abfd->my_archive != NULL |
| 328 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 329 | { |
| 330 | offset += abfd->origin; |
| 331 | abfd = abfd->my_archive; |
| 332 | } |
| 333 | |
| 334 | if (abfd->iovec == NULL) |
| 335 | { |
| 336 | bfd_set_error (bfd_error_invalid_operation); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | /* For the time being, a BFD may not seek to it's end. The problem |
| 341 | is that we don't easily have a way to recognize the end of an |
| 342 | element in an archive. */ |
| 343 | BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); |
| 344 | |
| 345 | if (direction != SEEK_CUR) |
| 346 | position += offset; |
| 347 | |
| 348 | if ((direction == SEEK_CUR && position == 0) |
| 349 | || (direction == SEEK_SET && (ufile_ptr) position == abfd->where)) |
| 350 | return 0; |
| 351 | |
| 352 | result = abfd->iovec->bseek (abfd, position, direction); |
| 353 | if (result != 0) |
| 354 | { |
| 355 | /* An EINVAL error probably means that the file offset was |
| 356 | absurd. */ |
| 357 | if (errno == EINVAL) |
| 358 | bfd_set_error (bfd_error_file_truncated); |
| 359 | else |
| 360 | bfd_set_error (bfd_error_system_call); |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | /* Adjust `where' field. */ |
| 365 | if (direction == SEEK_CUR) |
| 366 | abfd->where += position; |
| 367 | else |
| 368 | abfd->where = position; |
| 369 | } |
| 370 | |
| 371 | return result; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | FUNCTION |
| 376 | bfd_get_mtime |
| 377 | |
| 378 | SYNOPSIS |
| 379 | long bfd_get_mtime (bfd *abfd); |
| 380 | |
| 381 | DESCRIPTION |
| 382 | Return the file modification time (as read from the file system, or |
| 383 | from the archive header for archive members). |
| 384 | |
| 385 | */ |
| 386 | |
| 387 | long |
| 388 | bfd_get_mtime (bfd *abfd) |
| 389 | { |
| 390 | struct stat buf; |
| 391 | |
| 392 | if (abfd->mtime_set) |
| 393 | return abfd->mtime; |
| 394 | |
| 395 | if (bfd_stat (abfd, &buf) != 0) |
| 396 | return 0; |
| 397 | |
| 398 | abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */ |
| 399 | return buf.st_mtime; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | FUNCTION |
| 404 | bfd_get_size |
| 405 | |
| 406 | SYNOPSIS |
| 407 | ufile_ptr bfd_get_size (bfd *abfd); |
| 408 | |
| 409 | DESCRIPTION |
| 410 | Return the file size (as read from file system) for the file |
| 411 | associated with BFD @var{abfd}. |
| 412 | |
| 413 | The initial motivation for, and use of, this routine is not |
| 414 | so we can get the exact size of the object the BFD applies to, since |
| 415 | that might not be generally possible (archive members for example). |
| 416 | It would be ideal if someone could eventually modify |
| 417 | it so that such results were guaranteed. |
| 418 | |
| 419 | Instead, we want to ask questions like "is this NNN byte sized |
| 420 | object I'm about to try read from file offset YYY reasonable?" |
| 421 | As as example of where we might do this, some object formats |
| 422 | use string tables for which the first <<sizeof (long)>> bytes of the |
| 423 | table contain the size of the table itself, including the size bytes. |
| 424 | If an application tries to read what it thinks is one of these |
| 425 | string tables, without some way to validate the size, and for |
| 426 | some reason the size is wrong (byte swapping error, wrong location |
| 427 | for the string table, etc.), the only clue is likely to be a read |
| 428 | error when it tries to read the table, or a "virtual memory |
| 429 | exhausted" error when it tries to allocate 15 bazillon bytes |
| 430 | of space for the 15 bazillon byte table it is about to read. |
| 431 | This function at least allows us to answer the question, "is the |
| 432 | size reasonable?". |
| 433 | |
| 434 | A return value of zero indicates the file size is unknown. |
| 435 | */ |
| 436 | |
| 437 | ufile_ptr |
| 438 | bfd_get_size (bfd *abfd) |
| 439 | { |
| 440 | /* A size of 0 means we haven't yet called bfd_stat. A size of 1 |
| 441 | means we have a cached value of 0, ie. unknown. */ |
| 442 | if (abfd->size <= 1 || bfd_write_p (abfd)) |
| 443 | { |
| 444 | struct stat buf; |
| 445 | |
| 446 | if (abfd->size == 1 && !bfd_write_p (abfd)) |
| 447 | return 0; |
| 448 | |
| 449 | if (bfd_stat (abfd, &buf) != 0 |
| 450 | || buf.st_size == 0 |
| 451 | || buf.st_size - (ufile_ptr) buf.st_size != 0) |
| 452 | { |
| 453 | abfd->size = 1; |
| 454 | return 0; |
| 455 | } |
| 456 | abfd->size = buf.st_size; |
| 457 | } |
| 458 | return abfd->size; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | FUNCTION |
| 463 | bfd_get_file_size |
| 464 | |
| 465 | SYNOPSIS |
| 466 | ufile_ptr bfd_get_file_size (bfd *abfd); |
| 467 | |
| 468 | DESCRIPTION |
| 469 | Return the file size (as read from file system) for the file |
| 470 | associated with BFD @var{abfd}. It supports both normal files |
| 471 | and archive elements. |
| 472 | |
| 473 | */ |
| 474 | |
| 475 | ufile_ptr |
| 476 | bfd_get_file_size (bfd *abfd) |
| 477 | { |
| 478 | ufile_ptr file_size, archive_size = (ufile_ptr) -1; |
| 479 | |
| 480 | if (abfd->my_archive != NULL |
| 481 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 482 | { |
| 483 | struct areltdata *adata = (struct areltdata *) abfd->arelt_data; |
| 484 | archive_size = adata->parsed_size; |
| 485 | /* If the archive is compressed we can't compare against file size. */ |
| 486 | if (memcmp (((struct ar_hdr *) adata->arch_header)->ar_fmag, |
| 487 | "Z\012", 2) == 0) |
| 488 | return archive_size; |
| 489 | abfd = abfd->my_archive; |
| 490 | } |
| 491 | |
| 492 | file_size = bfd_get_size (abfd); |
| 493 | if (archive_size < file_size) |
| 494 | return archive_size; |
| 495 | return file_size; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | FUNCTION |
| 500 | bfd_mmap |
| 501 | |
| 502 | SYNOPSIS |
| 503 | void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, |
| 504 | int prot, int flags, file_ptr offset, |
| 505 | void **map_addr, bfd_size_type *map_len); |
| 506 | |
| 507 | DESCRIPTION |
| 508 | Return mmap()ed region of the file, if possible and implemented. |
| 509 | LEN and OFFSET do not need to be page aligned. The page aligned |
| 510 | address and length are written to MAP_ADDR and MAP_LEN. |
| 511 | |
| 512 | */ |
| 513 | |
| 514 | void * |
| 515 | bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, |
| 516 | int prot, int flags, file_ptr offset, |
| 517 | void **map_addr, bfd_size_type *map_len) |
| 518 | { |
| 519 | while (abfd->my_archive != NULL |
| 520 | && !bfd_is_thin_archive (abfd->my_archive)) |
| 521 | { |
| 522 | offset += abfd->origin; |
| 523 | abfd = abfd->my_archive; |
| 524 | } |
| 525 | |
| 526 | if (abfd->iovec == NULL) |
| 527 | { |
| 528 | bfd_set_error (bfd_error_invalid_operation); |
| 529 | return (void *) -1; |
| 530 | } |
| 531 | |
| 532 | return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset, |
| 533 | map_addr, map_len); |
| 534 | } |
| 535 | |
| 536 | /* Memory file I/O operations. */ |
| 537 | |
| 538 | static file_ptr |
| 539 | memory_bread (bfd *abfd, void *ptr, file_ptr size) |
| 540 | { |
| 541 | struct bfd_in_memory *bim; |
| 542 | bfd_size_type get; |
| 543 | |
| 544 | bim = (struct bfd_in_memory *) abfd->iostream; |
| 545 | get = size; |
| 546 | if (abfd->where + get > bim->size) |
| 547 | { |
| 548 | if (bim->size < (bfd_size_type) abfd->where) |
| 549 | get = 0; |
| 550 | else |
| 551 | get = bim->size - abfd->where; |
| 552 | bfd_set_error (bfd_error_file_truncated); |
| 553 | } |
| 554 | memcpy (ptr, bim->buffer + abfd->where, (size_t) get); |
| 555 | return get; |
| 556 | } |
| 557 | |
| 558 | static file_ptr |
| 559 | memory_bwrite (bfd *abfd, const void *ptr, file_ptr size) |
| 560 | { |
| 561 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; |
| 562 | |
| 563 | if (abfd->where + size > bim->size) |
| 564 | { |
| 565 | bfd_size_type newsize, oldsize; |
| 566 | |
| 567 | oldsize = (bim->size + 127) & ~(bfd_size_type) 127; |
| 568 | bim->size = abfd->where + size; |
| 569 | /* Round up to cut down on memory fragmentation */ |
| 570 | newsize = (bim->size + 127) & ~(bfd_size_type) 127; |
| 571 | if (newsize > oldsize) |
| 572 | { |
| 573 | bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); |
| 574 | if (bim->buffer == NULL) |
| 575 | { |
| 576 | bim->size = 0; |
| 577 | return 0; |
| 578 | } |
| 579 | if (newsize > bim->size) |
| 580 | memset (bim->buffer + bim->size, 0, newsize - bim->size); |
| 581 | } |
| 582 | } |
| 583 | memcpy (bim->buffer + abfd->where, ptr, (size_t) size); |
| 584 | return size; |
| 585 | } |
| 586 | |
| 587 | static file_ptr |
| 588 | memory_btell (bfd *abfd) |
| 589 | { |
| 590 | return abfd->where; |
| 591 | } |
| 592 | |
| 593 | static int |
| 594 | memory_bseek (bfd *abfd, file_ptr position, int direction) |
| 595 | { |
| 596 | file_ptr nwhere; |
| 597 | struct bfd_in_memory *bim; |
| 598 | |
| 599 | bim = (struct bfd_in_memory *) abfd->iostream; |
| 600 | |
| 601 | if (direction == SEEK_SET) |
| 602 | nwhere = position; |
| 603 | else |
| 604 | nwhere = abfd->where + position; |
| 605 | |
| 606 | if (nwhere < 0) |
| 607 | { |
| 608 | abfd->where = 0; |
| 609 | errno = EINVAL; |
| 610 | return -1; |
| 611 | } |
| 612 | |
| 613 | if ((bfd_size_type)nwhere > bim->size) |
| 614 | { |
| 615 | if (abfd->direction == write_direction |
| 616 | || abfd->direction == both_direction) |
| 617 | { |
| 618 | bfd_size_type newsize, oldsize; |
| 619 | |
| 620 | oldsize = (bim->size + 127) & ~(bfd_size_type) 127; |
| 621 | bim->size = nwhere; |
| 622 | /* Round up to cut down on memory fragmentation */ |
| 623 | newsize = (bim->size + 127) & ~(bfd_size_type) 127; |
| 624 | if (newsize > oldsize) |
| 625 | { |
| 626 | bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); |
| 627 | if (bim->buffer == NULL) |
| 628 | { |
| 629 | errno = EINVAL; |
| 630 | bim->size = 0; |
| 631 | return -1; |
| 632 | } |
| 633 | memset (bim->buffer + oldsize, 0, newsize - oldsize); |
| 634 | } |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | abfd->where = bim->size; |
| 639 | errno = EINVAL; |
| 640 | bfd_set_error (bfd_error_file_truncated); |
| 641 | return -1; |
| 642 | } |
| 643 | } |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int |
| 648 | memory_bclose (struct bfd *abfd) |
| 649 | { |
| 650 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; |
| 651 | |
| 652 | if (bim->buffer != NULL) |
| 653 | free (bim->buffer); |
| 654 | free (bim); |
| 655 | abfd->iostream = NULL; |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int |
| 661 | memory_bflush (bfd *abfd ATTRIBUTE_UNUSED) |
| 662 | { |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static int |
| 667 | memory_bstat (bfd *abfd, struct stat *statbuf) |
| 668 | { |
| 669 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; |
| 670 | |
| 671 | memset (statbuf, 0, sizeof (*statbuf)); |
| 672 | statbuf->st_size = bim->size; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static void * |
| 678 | memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED, |
| 679 | bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED, |
| 680 | int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED, |
| 681 | void **map_addr ATTRIBUTE_UNUSED, |
| 682 | bfd_size_type *map_len ATTRIBUTE_UNUSED) |
| 683 | { |
| 684 | return (void *)-1; |
| 685 | } |
| 686 | |
| 687 | const struct bfd_iovec _bfd_memory_iovec = |
| 688 | { |
| 689 | &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek, |
| 690 | &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap |
| 691 | }; |