bfdwin.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Support for memory-mapped windows into a BFD.
  2. Copyright (C) 1995-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 "bfd.h"
  19. #include "libbfd.h"
  20. /* Currently, if USE_MMAP is undefined, none of the window stuff is
  21. used. Enabled by --with-mmap. */
  22. #ifdef USE_MMAP
  23. #undef HAVE_MPROTECT /* code's not tested yet */
  24. #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
  25. #include <sys/mman.h>
  26. #endif
  27. #ifndef MAP_FILE
  28. #define MAP_FILE 0
  29. #endif
  30. static int debug_windows;
  31. /* The idea behind the next and refcount fields is that one mapped
  32. region can suffice for multiple read-only windows or multiple
  33. non-overlapping read-write windows. It's not implemented yet
  34. though. */
  35. /*
  36. INTERNAL_DEFINITION
  37. .struct _bfd_window_internal {
  38. . struct _bfd_window_internal *next;
  39. . void *data;
  40. . bfd_size_type size;
  41. . int refcount : 31; {* should be enough... *}
  42. . unsigned mapped : 1; {* 1 = mmap, 0 = malloc *}
  43. .};
  44. */
  45. void
  46. bfd_init_window (bfd_window *windowp)
  47. {
  48. windowp->data = 0;
  49. windowp->i = 0;
  50. windowp->size = 0;
  51. }
  52. void
  53. bfd_free_window (bfd_window *windowp)
  54. {
  55. bfd_window_internal *i = windowp->i;
  56. windowp->i = 0;
  57. windowp->data = 0;
  58. if (i == 0)
  59. return;
  60. i->refcount--;
  61. if (debug_windows)
  62. fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
  63. windowp, windowp->data, (unsigned long) windowp->size, windowp->i);
  64. if (i->refcount != 0)
  65. return;
  66. if (i->mapped)
  67. {
  68. #ifdef HAVE_MMAP
  69. munmap (i->data, i->size);
  70. goto no_free;
  71. #else
  72. abort ();
  73. #endif
  74. }
  75. #ifdef HAVE_MPROTECT
  76. mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
  77. #endif
  78. free (i->data);
  79. #ifdef HAVE_MMAP
  80. no_free:
  81. #endif
  82. i->data = 0;
  83. /* There should be no more references to i at this point. */
  84. free (i);
  85. }
  86. static int ok_to_map = 1;
  87. bool
  88. bfd_get_file_window (bfd *abfd,
  89. file_ptr offset,
  90. bfd_size_type size,
  91. bfd_window *windowp,
  92. bool writable)
  93. {
  94. static size_t pagesize;
  95. bfd_window_internal *i = windowp->i;
  96. bfd_size_type size_to_alloc = size;
  97. if (debug_windows)
  98. fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
  99. abfd, (long) offset, (long) size,
  100. windowp, windowp->data, (unsigned long) windowp->size,
  101. windowp->i, writable);
  102. /* Make sure we know the page size, so we can be friendly to mmap. */
  103. if (pagesize == 0)
  104. pagesize = getpagesize ();
  105. if (pagesize == 0)
  106. abort ();
  107. if (i == NULL)
  108. {
  109. i = bfd_zmalloc (sizeof (bfd_window_internal));
  110. if (i == NULL)
  111. return false;
  112. i->data = NULL;
  113. }
  114. #ifdef HAVE_MMAP
  115. if (ok_to_map
  116. && (i->data == NULL || i->mapped == 1)
  117. && (abfd->flags & BFD_IN_MEMORY) == 0)
  118. {
  119. file_ptr file_offset, offset2;
  120. size_t real_size;
  121. int fd;
  122. /* Find the real file and the real offset into it. */
  123. while (abfd->my_archive != NULL
  124. && !bfd_is_thin_archive (abfd->my_archive))
  125. {
  126. offset += abfd->origin;
  127. abfd = abfd->my_archive;
  128. }
  129. offset += abfd->origin;
  130. /* Seek into the file, to ensure it is open if cacheable. */
  131. if (abfd->iostream == NULL
  132. && (abfd->iovec == NULL
  133. || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
  134. goto free_and_fail;
  135. fd = fileno ((FILE *) abfd->iostream);
  136. /* Compute offsets and size for mmap and for the user's data. */
  137. offset2 = offset % pagesize;
  138. if (offset2 < 0)
  139. abort ();
  140. file_offset = offset - offset2;
  141. real_size = offset + size - file_offset;
  142. real_size = real_size + pagesize - 1;
  143. real_size -= real_size % pagesize;
  144. /* If we're re-using a memory region, make sure it's big enough. */
  145. if (i->data != NULL && i->size < size)
  146. {
  147. munmap (i->data, i->size);
  148. i->data = NULL;
  149. }
  150. i->data = mmap (i->data, real_size,
  151. writable ? PROT_WRITE | PROT_READ : PROT_READ,
  152. (writable
  153. ? MAP_FILE | MAP_PRIVATE
  154. : MAP_FILE | MAP_SHARED),
  155. fd, file_offset);
  156. if (i->data == (void *) -1)
  157. {
  158. /* An error happened. Report it, or try using malloc, or
  159. something. */
  160. bfd_set_error (bfd_error_system_call);
  161. windowp->data = 0;
  162. if (debug_windows)
  163. fprintf (stderr, "\t\tmmap failed!\n");
  164. goto free_and_fail;
  165. }
  166. if (debug_windows)
  167. fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
  168. (long) real_size, i->data, (long) offset2);
  169. i->size = real_size;
  170. windowp->data = (bfd_byte *) i->data + offset2;
  171. windowp->size = size;
  172. i->mapped = 1;
  173. i->refcount = 1;
  174. windowp->i = i;
  175. return true;
  176. }
  177. else if (debug_windows)
  178. {
  179. if (ok_to_map)
  180. fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
  181. (unsigned long) i->data, (int) i->mapped);
  182. else
  183. fprintf (stderr, _("not mapping: env var not set\n"));
  184. }
  185. #else
  186. ok_to_map = 0;
  187. #endif
  188. #ifdef HAVE_MPROTECT
  189. if (!writable)
  190. {
  191. size_to_alloc += pagesize - 1;
  192. size_to_alloc -= size_to_alloc % pagesize;
  193. }
  194. #endif
  195. if (debug_windows)
  196. fprintf (stderr, "\n\t%s(%6ld)",
  197. i->data ? "realloc" : " malloc", (long) size_to_alloc);
  198. i->data = bfd_realloc_or_free (i->data, size_to_alloc);
  199. if (debug_windows)
  200. fprintf (stderr, "\t-> %p\n", i->data);
  201. if (i->data == NULL)
  202. {
  203. if (size_to_alloc == 0)
  204. {
  205. windowp->i = i;
  206. return true;
  207. }
  208. goto free_and_fail;
  209. }
  210. i->refcount = 1;
  211. if (bfd_seek (abfd, offset, SEEK_SET) != 0)
  212. goto free_and_fail;
  213. i->size = bfd_bread (i->data, size, abfd);
  214. if (i->size != size)
  215. goto free_and_fail;
  216. i->mapped = 0;
  217. #ifdef HAVE_MPROTECT
  218. if (!writable)
  219. {
  220. if (debug_windows)
  221. fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
  222. (long) i->size);
  223. mprotect (i->data, i->size, PROT_READ);
  224. }
  225. #endif
  226. windowp->data = i->data;
  227. windowp->size = i->size;
  228. windowp->i = i;
  229. return true;
  230. free_and_fail:
  231. /* We have a bfd_window_internal, but an error occurred. Free it. */
  232. free (i);
  233. return false;
  234. }
  235. #endif /* USE_MMAP */