1 /* Darwin host-specific hook definitions.
2 Copyright (C) 2003-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "diagnostic-core.h"
24 #include "config/host-darwin.h"
25 #include "hosthooks.h"
26 #include "hosthooks-def.h"
27
28 /* Yes, this is really supposed to work. */
29 /* This allows for a pagesize of 16384, which we have on Darwin20, but should
30 continue to work OK for pagesize 4096 which we have on earlier versions.
31 The size is 1 (binary) Gb. */
32 static char pch_address_space[65536*16384] __attribute__((aligned (16384)));
33
34 /* Return the address of the PCH address space, if the PCH will fit in it. */
35
36 void *
darwin_gt_pch_get_address(size_t sz,int fd ATTRIBUTE_UNUSED)37 darwin_gt_pch_get_address (size_t sz, int fd ATTRIBUTE_UNUSED)
38 {
39 if (sz <= sizeof (pch_address_space))
40 return pch_address_space;
41 else
42 return NULL;
43 }
44
45 /* Check ADDR and SZ for validity, and deallocate (using munmap) that part of
46 pch_address_space beyond SZ. */
47
48 int
darwin_gt_pch_use_address(void * addr,size_t sz,int fd,size_t off)49 darwin_gt_pch_use_address (void *addr, size_t sz, int fd, size_t off)
50 {
51 const size_t pagesize = getpagesize();
52 void *mmap_result;
53 int ret;
54
55 gcc_assert ((size_t)pch_address_space % pagesize == 0
56 && sizeof (pch_address_space) % pagesize == 0);
57
58 ret = (addr == pch_address_space && sz <= sizeof (pch_address_space));
59 if (! ret)
60 sz = 0;
61
62 /* Round the size to a whole page size. Normally this is a no-op. */
63 sz = (sz + pagesize - 1) / pagesize * pagesize;
64
65 if (munmap (pch_address_space + sz, sizeof (pch_address_space) - sz) != 0)
66 fatal_error (input_location, "couldn%'t unmap pch_address_space: %m");
67
68 if (ret)
69 {
70 mmap_result = mmap (addr, sz,
71 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
72 fd, off);
73
74 /* The file might not be mmap-able. */
75 ret = mmap_result != (void *) MAP_FAILED;
76
77 /* Sanity check for broken MAP_FIXED. */
78 gcc_assert (!ret || mmap_result == addr);
79 }
80
81 return ret;
82 }
83
84 const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;
85