bfdwin.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Support for memory-mapped windows into a BFD.
  2. Copyright (C) 1995-2015 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. bfd_boolean
  88. bfd_get_file_window (bfd *abfd,
  89. file_ptr offset,
  90. bfd_size_type size,
  91. bfd_window *windowp,
  92. bfd_boolean 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. {
  125. offset += abfd->origin;
  126. abfd = abfd->my_archive;
  127. }
  128. /* Seek into the file, to ensure it is open if cacheable. */
  129. if (abfd->iostream == NULL
  130. && (abfd->iovec == NULL
  131. || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
  132. goto free_and_fail;
  133. fd = fileno ((FILE *) abfd->iostream);
  134. /* Compute offsets and size for mmap and for the user's data. */
  135. offset2 = offset % pagesize;
  136. if (offset2 < 0)
  137. abort ();
  138. file_offset = offset - offset2;
  139. real_size = offset + size - file_offset;
  140. real_size = real_size + pagesize - 1;
  141. real_size -= real_size % pagesize;
  142. /* If we're re-using a memory region, make sure it's big enough. */
  143. if (i->data != NULL && i->size < size)
  144. {
  145. munmap (i->data, i->size);
  146. i->data = NULL;
  147. }
  148. i->data = mmap (i->data, real_size,
  149. writable ? PROT_WRITE | PROT_READ : PROT_READ,
  150. (writable
  151. ? MAP_FILE | MAP_PRIVATE
  152. : MAP_FILE | MAP_SHARED),
  153. fd, file_offset);
  154. if (i->data == (void *) -1)
  155. {
  156. /* An error happened. Report it, or try using malloc, or
  157. something. */
  158. bfd_set_error (bfd_error_system_call);
  159. windowp->data = 0;
  160. if (debug_windows)
  161. fprintf (stderr, "\t\tmmap failed!\n");
  162. goto free_and_fail;
  163. }
  164. if (debug_windows)
  165. fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
  166. (long) real_size, i->data, (long) offset2);
  167. i->size = real_size;
  168. windowp->data = (bfd_byte *) i->data + offset2;
  169. windowp->size = size;
  170. i->mapped = 1;
  171. i->refcount = 1;
  172. windowp->i = i;
  173. return TRUE;
  174. }
  175. else if (debug_windows)
  176. {
  177. if (ok_to_map)
  178. fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
  179. (unsigned long) i->data, (int) i->mapped);
  180. else
  181. fprintf (stderr, _("not mapping: env var not set\n"));
  182. }
  183. #else
  184. ok_to_map = 0;
  185. #endif
  186. #ifdef HAVE_MPROTECT
  187. if (!writable)
  188. {
  189. size_to_alloc += pagesize - 1;
  190. size_to_alloc -= size_to_alloc % pagesize;
  191. }
  192. #endif
  193. if (debug_windows)
  194. fprintf (stderr, "\n\t%s(%6ld)",
  195. i->data ? "realloc" : " malloc", (long) size_to_alloc);
  196. i->data = bfd_realloc_or_free (i->data, size_to_alloc);
  197. if (debug_windows)
  198. fprintf (stderr, "\t-> %p\n", i->data);
  199. if (i->data == NULL)
  200. {
  201. if (size_to_alloc == 0)
  202. {
  203. windowp->i = i;
  204. return TRUE;
  205. }
  206. goto free_and_fail;
  207. }
  208. i->refcount = 1;
  209. if (bfd_seek (abfd, offset, SEEK_SET) != 0)
  210. goto free_and_fail;
  211. i->size = bfd_bread (i->data, size, abfd);
  212. if (i->size != size)
  213. goto free_and_fail;
  214. i->mapped = 0;
  215. #ifdef HAVE_MPROTECT
  216. if (!writable)
  217. {
  218. if (debug_windows)
  219. fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
  220. (long) i->size);
  221. mprotect (i->data, i->size, PROT_READ);
  222. }
  223. #endif
  224. windowp->data = i->data;
  225. windowp->size = i->size;
  226. windowp->i = i;
  227. return TRUE;
  228. free_and_fail:
  229. /* We have a bfd_window_internal, but an error occurred. Free it. */
  230. free (i);
  231. return FALSE;
  232. }
  233. #endif /* USE_MMAP */