xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/config/host-linux.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* Linux host-specific hook definitions.
2*8feb0f0bSmrg    Copyright (C) 2004-2020 Free Software Foundation, Inc.
31debfc3dSmrg 
41debfc3dSmrg    This file is part of GCC.
51debfc3dSmrg 
61debfc3dSmrg    GCC is free software; you can redistribute it and/or modify it
71debfc3dSmrg    under the terms of the GNU General Public License as published
81debfc3dSmrg    by the Free Software Foundation; either version 3, or (at your
91debfc3dSmrg    option) any later version.
101debfc3dSmrg 
111debfc3dSmrg    GCC is distributed in the hope that it will be useful, but WITHOUT
121debfc3dSmrg    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
131debfc3dSmrg    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
141debfc3dSmrg    License for more details.
151debfc3dSmrg 
161debfc3dSmrg    You should have received a copy of the GNU General Public License
171debfc3dSmrg    along with GCC; see the file COPYING3.  If not see
181debfc3dSmrg    <http://www.gnu.org/licenses/>.  */
191debfc3dSmrg 
201debfc3dSmrg #include "config.h"
211debfc3dSmrg #include "system.h"
221debfc3dSmrg #include "coretypes.h"
231debfc3dSmrg #include "hosthooks.h"
241debfc3dSmrg #include "hosthooks-def.h"
251debfc3dSmrg 
261debfc3dSmrg 
271debfc3dSmrg /* Linux has a feature called exec-shield-randomize that perturbs the
281debfc3dSmrg    address of non-fixed mapped segments by a (relatively) small amount.
291debfc3dSmrg    The feature is intended to make it harder to attack the system with
301debfc3dSmrg    buffer overflow attacks, since every invocation of a program will
311debfc3dSmrg    have its libraries and data segments at slightly different addresses.
321debfc3dSmrg 
331debfc3dSmrg    This feature causes us problems with PCH because it makes it that
341debfc3dSmrg    much harder to acquire a stable location at which to map our PCH
351debfc3dSmrg    data file.
361debfc3dSmrg 
371debfc3dSmrg    [ The feature causes other points of non-determinism within the
381debfc3dSmrg      compiler as well, so we'd *really* like to be able to have the
391debfc3dSmrg      driver disable exec-shield-randomize for the process group, but
401debfc3dSmrg      that isn't possible at present.  ]
411debfc3dSmrg 
421debfc3dSmrg    We're going to try several things:
431debfc3dSmrg 
441debfc3dSmrg       * Select an architecture specific address as "likely" and see
451debfc3dSmrg 	if that's free.  For our 64-bit hosts, we can easily choose
461debfc3dSmrg 	an address in Never Never Land.
471debfc3dSmrg 
481debfc3dSmrg       * If exec-shield-randomize is disabled, then just use the
491debfc3dSmrg 	address chosen by mmap in step one.
501debfc3dSmrg 
511debfc3dSmrg       * If exec-shield-randomize is enabled, then temporarily allocate
521debfc3dSmrg 	32M of memory as a buffer, then allocate PCH memory, then
531debfc3dSmrg 	free the buffer.  The theory here is that the perturbation is
541debfc3dSmrg 	no more than 16M, and so by allocating our buffer larger than
551debfc3dSmrg 	that we make it considerably more likely that the address will
561debfc3dSmrg 	be free when we want to load the data back.
571debfc3dSmrg */
581debfc3dSmrg 
591debfc3dSmrg #undef HOST_HOOKS_GT_PCH_GET_ADDRESS
601debfc3dSmrg #define HOST_HOOKS_GT_PCH_GET_ADDRESS linux_gt_pch_get_address
611debfc3dSmrg 
621debfc3dSmrg #undef HOST_HOOKS_GT_PCH_USE_ADDRESS
631debfc3dSmrg #define HOST_HOOKS_GT_PCH_USE_ADDRESS linux_gt_pch_use_address
641debfc3dSmrg 
651debfc3dSmrg /* For various ports, try to guess a fixed spot in the vm space
661debfc3dSmrg    that's probably free.  */
671debfc3dSmrg #if defined(__alpha)
681debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x10000000000
691debfc3dSmrg #elif defined(__ia64)
701debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x2000000100000000
711debfc3dSmrg #elif defined(__x86_64) && defined(__LP64__)
721debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x1000000000
731debfc3dSmrg #elif defined(__x86_64)
741debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
751debfc3dSmrg #elif defined(__i386)
761debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
771debfc3dSmrg #elif defined(__powerpc__)
781debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
791debfc3dSmrg #elif defined(__s390x__)
801debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x8000000000
811debfc3dSmrg #elif defined(__s390__)
821debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
831debfc3dSmrg #elif defined(__sparc__) && defined(__LP64__)
841debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x8000000000
851debfc3dSmrg #elif defined(__sparc__)
861debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
871debfc3dSmrg #elif defined(__mc68000__)
881debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x40000000
891debfc3dSmrg #elif defined(__aarch64__) && defined(__ILP32__)
901debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
911debfc3dSmrg #elif defined(__aarch64__)
921debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x1000000000
931debfc3dSmrg #elif defined(__ARM_EABI__)
941debfc3dSmrg # define TRY_EMPTY_VM_SPACE     0x60000000
951debfc3dSmrg #elif defined(__mips__) && defined(__LP64__)
961debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x8000000000
971debfc3dSmrg #elif defined(__mips__)
981debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0x60000000
99*8feb0f0bSmrg #elif defined(__riscv) && defined (__LP64__)
100*8feb0f0bSmrg # define TRY_EMPTY_VM_SPACE	0x1000000000
1011debfc3dSmrg #else
1021debfc3dSmrg # define TRY_EMPTY_VM_SPACE	0
1031debfc3dSmrg #endif
1041debfc3dSmrg 
1051debfc3dSmrg /* Determine a location where we might be able to reliably allocate SIZE
1061debfc3dSmrg    bytes.  FD is the PCH file, though we should return with the file
1071debfc3dSmrg    unmapped.  */
1081debfc3dSmrg 
1091debfc3dSmrg static void *
linux_gt_pch_get_address(size_t size,int fd)1101debfc3dSmrg linux_gt_pch_get_address (size_t size, int fd)
1111debfc3dSmrg {
1121debfc3dSmrg   size_t buffer_size = 32 * 1024 * 1024;
1131debfc3dSmrg   void *addr, *buffer;
1141debfc3dSmrg   FILE *f;
1151debfc3dSmrg   bool randomize_on;
1161debfc3dSmrg 
1171debfc3dSmrg   addr = mmap ((void *)TRY_EMPTY_VM_SPACE, size, PROT_READ | PROT_WRITE,
1181debfc3dSmrg 	       MAP_PRIVATE, fd, 0);
1191debfc3dSmrg 
1201debfc3dSmrg   /* If we failed the map, that means there's *no* free space.  */
1211debfc3dSmrg   if (addr == (void *) MAP_FAILED)
1221debfc3dSmrg     return NULL;
1231debfc3dSmrg   /* Unmap the area before returning.  */
1241debfc3dSmrg   munmap (addr, size);
1251debfc3dSmrg 
1261debfc3dSmrg   /* If we got the exact area we requested, then that's great.  */
1271debfc3dSmrg   if (TRY_EMPTY_VM_SPACE && addr == (void *) TRY_EMPTY_VM_SPACE)
1281debfc3dSmrg     return addr;
1291debfc3dSmrg 
1301debfc3dSmrg   /* If we didn't, then we need to look to see if virtual address
1311debfc3dSmrg      randomization is on.  That is recorded in
1321debfc3dSmrg      kernel.randomize_va_space.  An older implementation used
1331debfc3dSmrg      kernel.exec-shield-randomize.  */
1341debfc3dSmrg   f = fopen ("/proc/sys/kernel/randomize_va_space", "r");
1351debfc3dSmrg   if (f == NULL)
1361debfc3dSmrg     f = fopen ("/proc/sys/kernel/exec-shield-randomize", "r");
1371debfc3dSmrg   randomize_on = false;
1381debfc3dSmrg   if (f != NULL)
1391debfc3dSmrg     {
1401debfc3dSmrg       char buf[100];
1411debfc3dSmrg       size_t c;
1421debfc3dSmrg 
1431debfc3dSmrg       c = fread (buf, 1, sizeof buf - 1, f);
1441debfc3dSmrg       if (c > 0)
1451debfc3dSmrg 	{
1461debfc3dSmrg 	  buf[c] = '\0';
1471debfc3dSmrg 	  randomize_on = (atoi (buf) > 0);
1481debfc3dSmrg 	}
1491debfc3dSmrg       fclose (f);
1501debfc3dSmrg     }
1511debfc3dSmrg 
1521debfc3dSmrg   /* If it isn't, then accept the address that mmap selected as fine.  */
1531debfc3dSmrg   if (!randomize_on)
1541debfc3dSmrg     return addr;
1551debfc3dSmrg 
1561debfc3dSmrg   /* Otherwise, we need to try again with buffer space.  */
1571debfc3dSmrg   buffer = mmap (0, buffer_size, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
1581debfc3dSmrg   addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
1591debfc3dSmrg   if (buffer != (void *) MAP_FAILED)
1601debfc3dSmrg     munmap (buffer, buffer_size);
1611debfc3dSmrg   if (addr == (void *) MAP_FAILED)
1621debfc3dSmrg     return NULL;
1631debfc3dSmrg   munmap (addr, size);
1641debfc3dSmrg 
1651debfc3dSmrg   return addr;
1661debfc3dSmrg }
1671debfc3dSmrg 
1681debfc3dSmrg /* Map SIZE bytes of FD+OFFSET at BASE.  Return 1 if we succeeded at
1691debfc3dSmrg    mapping the data at BASE, -1 if we couldn't.
1701debfc3dSmrg 
1711debfc3dSmrg    It's not possibly to reliably mmap a file using MAP_PRIVATE to
1721debfc3dSmrg    a specific START address on either hpux or linux.  First we see
1731debfc3dSmrg    if mmap with MAP_PRIVATE works.  If it does, we are off to the
1741debfc3dSmrg    races.  If it doesn't, we try an anonymous private mmap since the
1751debfc3dSmrg    kernel is more likely to honor the BASE address in anonymous maps.
1761debfc3dSmrg    We then copy the data to the anonymous private map.  This assumes
1771debfc3dSmrg    of course that we don't need to change the data in the PCH file
1781debfc3dSmrg    after it is created.
1791debfc3dSmrg 
1801debfc3dSmrg    This approach obviously causes a performance penalty but there is
1811debfc3dSmrg    little else we can do given the current PCH implementation.  */
1821debfc3dSmrg 
1831debfc3dSmrg static int
linux_gt_pch_use_address(void * base,size_t size,int fd,size_t offset)1841debfc3dSmrg linux_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
1851debfc3dSmrg {
1861debfc3dSmrg   void *addr;
1871debfc3dSmrg 
1881debfc3dSmrg   /* We're called with size == 0 if we're not planning to load a PCH
1891debfc3dSmrg      file at all.  This allows the hook to free any static space that
1901debfc3dSmrg      we might have allocated at link time.  */
1911debfc3dSmrg   if (size == 0)
1921debfc3dSmrg     return -1;
1931debfc3dSmrg 
1941debfc3dSmrg   /* Try to map the file with MAP_PRIVATE.  */
1951debfc3dSmrg   addr = mmap (base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, offset);
1961debfc3dSmrg 
1971debfc3dSmrg   if (addr == base)
1981debfc3dSmrg     return 1;
1991debfc3dSmrg 
2001debfc3dSmrg   if (addr != (void *) MAP_FAILED)
2011debfc3dSmrg     munmap (addr, size);
2021debfc3dSmrg 
2031debfc3dSmrg   /* Try to make an anonymous private mmap at the desired location.  */
2041debfc3dSmrg   addr = mmap (base, size, PROT_READ | PROT_WRITE,
2051debfc3dSmrg 	       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2061debfc3dSmrg 
2071debfc3dSmrg   if (addr != base)
2081debfc3dSmrg     {
2091debfc3dSmrg       if (addr != (void *) MAP_FAILED)
2101debfc3dSmrg         munmap (addr, size);
2111debfc3dSmrg       return -1;
2121debfc3dSmrg     }
2131debfc3dSmrg 
2141debfc3dSmrg   if (lseek (fd, offset, SEEK_SET) == (off_t)-1)
2151debfc3dSmrg     return -1;
2161debfc3dSmrg 
2171debfc3dSmrg   while (size)
2181debfc3dSmrg     {
2191debfc3dSmrg       ssize_t nbytes;
2201debfc3dSmrg 
2211debfc3dSmrg       nbytes = read (fd, base, MIN (size, (size_t)-1 >> 1));
2221debfc3dSmrg       if (nbytes <= 0)
2231debfc3dSmrg         return -1;
2241debfc3dSmrg       base = (char *) base + nbytes;
2251debfc3dSmrg       size -= nbytes;
2261debfc3dSmrg     }
2271debfc3dSmrg 
2281debfc3dSmrg   return 1;
2291debfc3dSmrg }
2301debfc3dSmrg 
2311debfc3dSmrg 
2321debfc3dSmrg const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;
233