host-linux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Linux host-specific hook definitions.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "hosthooks.h"
  19. #include "hosthooks-def.h"
  20. /* Linux has a feature called exec-shield-randomize that perturbs the
  21. address of non-fixed mapped segments by a (relatively) small amount.
  22. The feature is intended to make it harder to attack the system with
  23. buffer overflow attacks, since every invocation of a program will
  24. have its libraries and data segments at slightly different addresses.
  25. This feature causes us problems with PCH because it makes it that
  26. much harder to acquire a stable location at which to map our PCH
  27. data file.
  28. [ The feature causes other points of non-determinism within the
  29. compiler as well, so we'd *really* like to be able to have the
  30. driver disable exec-shield-randomize for the process group, but
  31. that isn't possible at present. ]
  32. We're going to try several things:
  33. * Select an architecture specific address as "likely" and see
  34. if that's free. For our 64-bit hosts, we can easily choose
  35. an address in Never Never Land.
  36. * If exec-shield-randomize is disabled, then just use the
  37. address chosen by mmap in step one.
  38. * If exec-shield-randomize is enabled, then temporarily allocate
  39. 32M of memory as a buffer, then allocate PCH memory, then
  40. free the buffer. The theory here is that the perturbation is
  41. no more than 16M, and so by allocating our buffer larger than
  42. that we make it considerably more likely that the address will
  43. be free when we want to load the data back.
  44. */
  45. #undef HOST_HOOKS_GT_PCH_GET_ADDRESS
  46. #define HOST_HOOKS_GT_PCH_GET_ADDRESS linux_gt_pch_get_address
  47. #undef HOST_HOOKS_GT_PCH_USE_ADDRESS
  48. #define HOST_HOOKS_GT_PCH_USE_ADDRESS linux_gt_pch_use_address
  49. /* For various ports, try to guess a fixed spot in the vm space
  50. that's probably free. */
  51. #if defined(__alpha)
  52. # define TRY_EMPTY_VM_SPACE 0x10000000000
  53. #elif defined(__ia64)
  54. # define TRY_EMPTY_VM_SPACE 0x2000000100000000
  55. #elif defined(__x86_64) && defined(__LP64__)
  56. # define TRY_EMPTY_VM_SPACE 0x1000000000
  57. #elif defined(__x86_64)
  58. # define TRY_EMPTY_VM_SPACE 0x60000000
  59. #elif defined(__i386)
  60. # define TRY_EMPTY_VM_SPACE 0x60000000
  61. #elif defined(__powerpc__)
  62. # define TRY_EMPTY_VM_SPACE 0x60000000
  63. #elif defined(__s390x__)
  64. # define TRY_EMPTY_VM_SPACE 0x8000000000
  65. #elif defined(__s390__)
  66. # define TRY_EMPTY_VM_SPACE 0x60000000
  67. #elif defined(__sparc__) && defined(__LP64__)
  68. # define TRY_EMPTY_VM_SPACE 0x8000000000
  69. #elif defined(__sparc__)
  70. # define TRY_EMPTY_VM_SPACE 0x60000000
  71. #elif defined(__mc68000__)
  72. # define TRY_EMPTY_VM_SPACE 0x40000000
  73. #elif defined(__aarch64__) && defined(__ILP32__)
  74. # define TRY_EMPTY_VM_SPACE 0x60000000
  75. #elif defined(__aarch64__)
  76. # define TRY_EMPTY_VM_SPACE 0x1000000000
  77. #elif defined(__ARM_EABI__)
  78. # define TRY_EMPTY_VM_SPACE 0x60000000
  79. #elif defined(__mips__) && defined(__LP64__)
  80. # define TRY_EMPTY_VM_SPACE 0x8000000000
  81. #elif defined(__mips__)
  82. # define TRY_EMPTY_VM_SPACE 0x60000000
  83. #else
  84. # define TRY_EMPTY_VM_SPACE 0
  85. #endif
  86. /* Determine a location where we might be able to reliably allocate SIZE
  87. bytes. FD is the PCH file, though we should return with the file
  88. unmapped. */
  89. static void *
  90. linux_gt_pch_get_address (size_t size, int fd)
  91. {
  92. size_t buffer_size = 32 * 1024 * 1024;
  93. void *addr, *buffer;
  94. FILE *f;
  95. bool randomize_on;
  96. addr = mmap ((void *)TRY_EMPTY_VM_SPACE, size, PROT_READ | PROT_WRITE,
  97. MAP_PRIVATE, fd, 0);
  98. /* If we failed the map, that means there's *no* free space. */
  99. if (addr == (void *) MAP_FAILED)
  100. return NULL;
  101. /* Unmap the area before returning. */
  102. munmap (addr, size);
  103. /* If we got the exact area we requested, then that's great. */
  104. if (TRY_EMPTY_VM_SPACE && addr == (void *) TRY_EMPTY_VM_SPACE)
  105. return addr;
  106. /* If we didn't, then we need to look to see if virtual address
  107. randomization is on. That is recorded in
  108. kernel.randomize_va_space. An older implementation used
  109. kernel.exec-shield-randomize. */
  110. f = fopen ("/proc/sys/kernel/randomize_va_space", "r");
  111. if (f == NULL)
  112. f = fopen ("/proc/sys/kernel/exec-shield-randomize", "r");
  113. randomize_on = false;
  114. if (f != NULL)
  115. {
  116. char buf[100];
  117. size_t c;
  118. c = fread (buf, 1, sizeof buf - 1, f);
  119. if (c > 0)
  120. {
  121. buf[c] = '\0';
  122. randomize_on = (atoi (buf) > 0);
  123. }
  124. fclose (f);
  125. }
  126. /* If it isn't, then accept the address that mmap selected as fine. */
  127. if (!randomize_on)
  128. return addr;
  129. /* Otherwise, we need to try again with buffer space. */
  130. buffer = mmap (0, buffer_size, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
  131. addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  132. if (buffer != (void *) MAP_FAILED)
  133. munmap (buffer, buffer_size);
  134. if (addr == (void *) MAP_FAILED)
  135. return NULL;
  136. munmap (addr, size);
  137. return addr;
  138. }
  139. /* Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
  140. mapping the data at BASE, -1 if we couldn't.
  141. It's not possibly to reliably mmap a file using MAP_PRIVATE to
  142. a specific START address on either hpux or linux. First we see
  143. if mmap with MAP_PRIVATE works. If it does, we are off to the
  144. races. If it doesn't, we try an anonymous private mmap since the
  145. kernel is more likely to honor the BASE address in anonymous maps.
  146. We then copy the data to the anonymous private map. This assumes
  147. of course that we don't need to change the data in the PCH file
  148. after it is created.
  149. This approach obviously causes a performance penalty but there is
  150. little else we can do given the current PCH implementation. */
  151. static int
  152. linux_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
  153. {
  154. void *addr;
  155. /* We're called with size == 0 if we're not planning to load a PCH
  156. file at all. This allows the hook to free any static space that
  157. we might have allocated at link time. */
  158. if (size == 0)
  159. return -1;
  160. /* Try to map the file with MAP_PRIVATE. */
  161. addr = mmap (base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, offset);
  162. if (addr == base)
  163. return 1;
  164. if (addr != (void *) MAP_FAILED)
  165. munmap (addr, size);
  166. /* Try to make an anonymous private mmap at the desired location. */
  167. addr = mmap (base, size, PROT_READ | PROT_WRITE,
  168. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  169. if (addr != base)
  170. {
  171. if (addr != (void *) MAP_FAILED)
  172. munmap (addr, size);
  173. return -1;
  174. }
  175. if (lseek (fd, offset, SEEK_SET) == (off_t)-1)
  176. return -1;
  177. while (size)
  178. {
  179. ssize_t nbytes;
  180. nbytes = read (fd, base, MIN (size, (size_t)-1 >> 1));
  181. if (nbytes <= 0)
  182. return -1;
  183. base = (char *) base + nbytes;
  184. size -= nbytes;
  185. }
  186. return 1;
  187. }
  188. const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;