bfdio.c 18 KB

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