1 /* Darwin host-specific hook definitions.
2 Copyright (C) 2003-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
22 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "config/host-darwin.h"
28 /* For Darwin (macOS only) platforms, without ASLR (PIE) enabled on the
29 binaries, the following VM addresses are expected to be available.
30 NOTE, that for aarch64, ASLR is always enabled - but the VM address
31 mentioned below is available (at least on Darwin20).
33 The spaces should all have 512Mb available c.f. PCH files for large
34 C++ or Objective-C in the range of 150Mb for 64b hosts.
36 We also try to steer clear of places already used for sanitizers.
38 If the allocation fails at the 'ideal' address, we go with what the
39 kernel provides (there is more likelihood that we will need to relocate
43 #if defined(__x86_64) && defined(__LP64__)
44 # define TRY_EMPTY_VM_SPACE 0x180000000000ULL
45 #elif defined(__x86_64)
46 # define TRY_EMPTY_VM_SPACE 0x00006fe00000ULL
48 # define TRY_EMPTY_VM_SPACE 0x00006fe00000ULL
49 #elif defined(__POWERPC__) && defined(__LP64__)
50 # define TRY_EMPTY_VM_SPACE 0x180000000000ULL
51 #elif defined(__POWERPC__)
52 # define TRY_EMPTY_VM_SPACE 0x00006fe00000ULL
53 #elif defined(__aarch64__)
55 # define PAGE_SZ 16384
56 # define TRY_EMPTY_VM_SPACE 0x180000000000ULL
58 # error "unknown Darwin target"
61 /* Try to map a known position in the VM. The current PCH implementation
62 can adjust values at write-time, but not at read-time thus we need to
63 pick up the same position when reading as we got at write-time. */
66 darwin_gt_pch_get_address (size_t sz
, int fd
)
68 /* First try with the constraint that we really want this address... */
69 void *addr
= mmap ((void *)TRY_EMPTY_VM_SPACE
, sz
, PROT_READ
| PROT_WRITE
,
70 MAP_PRIVATE
| MAP_FIXED
, fd
, 0);
72 if (addr
!= (void *) MAP_FAILED
)
75 /* This ought to be the only alternative to failure, but there are comments
76 that suggest some versions of mmap can be buggy and return a different
78 if (addr
== (void *) TRY_EMPTY_VM_SPACE
)
81 /* OK try to find a space without the constraint. */
82 addr
= mmap ((void *) TRY_EMPTY_VM_SPACE
, sz
, PROT_READ
| PROT_WRITE
,
85 /* We return whatever the kernel gave us. */
86 if (addr
!= (void *) MAP_FAILED
)
88 /* Unmap the area before returning. */
93 /* Otherwise, try again but put some arbitrary buffer space first. */
94 size_t buffer_size
= 64 * 1024 * 1024;
95 void *buffer
= mmap (0, buffer_size
, PROT_NONE
,
96 MAP_PRIVATE
| MAP_ANON
, -1, 0);
97 addr
= mmap ((void *)TRY_EMPTY_VM_SPACE
, sz
, PROT_READ
| PROT_WRITE
,
100 if (buffer
!= (void *) MAP_FAILED
)
101 munmap (buffer
, buffer_size
);
103 /* If we failed this time, that means there is *no* large enough free
105 if (addr
== (void *) MAP_FAILED
)
107 error ("PCH memory is not available: %m");
115 /* Try to mmap the PCH file at ADDR for SZ bytes at OFF offset in the file.
116 If we succeed return 1, if we cannot mmap the desired address, then we
120 darwin_gt_pch_use_address (void *&addr
, size_t sz
, int fd
, size_t off
)
124 /* We're called with size == 0 if we're not planning to load a PCH
125 file at all. This allows the hook to free any static space that
126 we might have allocated at link time. */
130 gcc_checking_assert (!(off
% PAGE_SZ
));
132 /* Try to map the file with MAP_PRIVATE and FIXED. */
133 mapped_addr
= mmap (addr
, sz
, PROT_READ
| PROT_WRITE
,
134 MAP_PRIVATE
| MAP_FIXED
, fd
, (off_t
) off
);
136 /* Hopefully, we succeed. */
137 if (mapped_addr
== addr
)
140 /* In theory, the only alternative to success for MAP_FIXED should be FAILED
141 however, there are some buggy earlier implementations that could return
143 if (mapped_addr
!= (void *) MAP_FAILED
)
144 munmap (mapped_addr
, sz
);
146 /* Try to map the file with MAP_PRIVATE but let the kernel move it. */
147 mapped_addr
= mmap (addr
, sz
, PROT_READ
| PROT_WRITE
,
148 MAP_PRIVATE
, fd
, (off_t
) off
);
150 /* Hopefully, we succeed. */
151 if (mapped_addr
!= (void *) MAP_FAILED
)
157 /* Try to make an anonymous private mmap at the desired location in case
158 the problem is in mapping the file. */
159 mapped_addr
= mmap (addr
, sz
, PROT_READ
| PROT_WRITE
,
160 MAP_PRIVATE
| MAP_ANON
, -1, (off_t
)0);
162 /* If this fails, we are out of ideas (and maybe memory). */
163 if (mapped_addr
== (void *) MAP_FAILED
)
168 if (lseek (fd
, off
, SEEK_SET
) == (off_t
) -1)
175 nbytes
= read (fd
, mapped_addr
, MIN (sz
, (size_t) -1 >> 1));
178 mapped_addr
= (char *) mapped_addr
+ nbytes
;