1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
#!/bin/sh
#-
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2022 SRI International
#
# This software was developed by SRI International and the University of
# Cambridge Computer Laboratory (Department of Computer Science and
# Technology) under Defense Advanced Research Projects Agency (DARPA)
# Contract No. HR001122C0110 ("ETC").
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
LOCALBASE="%%LOCALBASE%%"
LLVM_PREFIX="%%LLVM_PREFIX%%"
LLVM_SUFFIX="%%LLVM_SUFFIX%%"
_TARGET=${_TARGET:-%%CONFIGURE_TARGET%%}
_SYSROOT=${_SYSROOT:-""}
VERBOSE=${VERBOSE:-0}
err()
{
ret=$1
shift
echo >&2 "$@"
exit "$ret"
}
debug()
{
if [ "$VERBOSE" -ne 0 ]; then
echo >&2 "$@"
fi
}
run()
{
debug "Running:" "$@"
"$@"
}
run_tool()
{
local tool=$1
shift
run env LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${LLVM_PREFIX}/lib" \
"${LLVM_PREFIX}/bin/$tool" "$@"
}
set_sysroot()
{
local sysroot="$1"
# Clang doesn't seem to care if the sysroot exists, but we'll
# blow up trying to get __CheriBSD_version if it doesn't so require
# it to exist.
if [ ! -d "$sysroot" ]; then
err 1 "nonexistant sysroot: '$sysroot'"
fi
_SYSROOT="$sysroot"
}
set_target()
{
_TARGET="$1"
}
tool=$0
[ -L "$tool" ] && tool=$(/bin/realpath $tool)
tool=${tool##*/}
basetool=${tool%${LLVM_SUFFIX}}
next_arg_is_target=0
next_arg_is_sysroot=0
for arg in "$@"; do
if [ $next_arg_is_target -ne 0 ]; then
set_target "$arg"
next_arg_is_target=0
continue
fi
if [ $next_arg_is_sysroot -ne 0 ]; then
set_sysroot "$arg"
next_arg_is_sysroot=0
continue
fi
case "$arg" in
--sysroot)
next_arg_is_sysroot=1
;;
--sysroot=*)
set_sysroot "${arg#--sysroot=}"
;;
-target)
next_arg_is_target=1
;;
--target=*)
set_target "${arg#--target=}"
;;
esac
done
if [ $next_arg_is_target -ne 0 ]; then
err 1 "-target without target argument"
fi
if [ -z "$CHERIBSD_VERSION" -a -e "${_SYSROOT}/usr/include/sys/param.h" ]; then
CHERIBSD_VERSION=$(awk '/^#define[[:space:]]+__CheriBSD_version/{print $3}' ${_SYSROOT}/usr/include/sys/param.h)
fi
if [ -z "$CHERIBSD_VERSION" ]; then
CHERIBSD_VERSION=0
fi
arch_cflags=
arch_ldflags=
arch_objdump_flags=
# If we're targeting CheriBSD, assume we want to produce CHERI binaries
# (either hybrid or purecap) and choose which one based on the default
# libc in the sysroot. This isn't quite right, but isn't much worse
# than a misguided -mcpu flag.
if [ $CHERIBSD_VERSION -gt 0 ]; then
case "$_TARGET" in
aarch64-*-freebsd*|aarch64-freebsd*)
if run_tool llvm-readelf -h ${_SYSROOT}/lib/libc.so.7 | grep "Flags:.*purecap" >/dev/null; then
tls_flags=
vararg_flags=
codeptr_flags=
capreloc_flags=
if [ "$CHERIBSD_VERSION" -le 20220314 ]; then
tls_flags="-femulated-tls"
elif [ "$CHERIBSD_VERSION" -le 20220828 ]; then
vararg_flags="-Xclang -morello-vararg=new"
elif [ "$CHERIBSD_VERSION" -le 20230804 ]; then
vararg_flags="-Xclang -morello-vararg=new -Xclang -morello-bounded-memargs=caller-only"
else
vararg_flags="-Xclang -morello-vararg=new -Xclang -morello-bounded-memargs"
fi
if [ "$CHERIBSD_VERSION" -ge 20250127 ]; then
capreloc_flags="-Wl,--local-caprelocs=elf"
fi
if [ "$CHERIBSD_VERSION" -gt 20250127 ]; then
codeptr_flags=-cheri-codeptr-relocs
fi
# Some compiler invocations (e.g., "clang -v") don't
# consume -Xclang arguments which can lead to unused
# argument warnings so we supress them with
# --start/end-no-unused-arguments.
arch_cflags="-march=morello -mabi=purecap --start-no-unused-arguments $tls_flags $vararg_flags $capreloc_flags $codeptr_flags --end-no-unused-arguments"
arch_ldflags="$capreloc_flags $codeptr_flags"
arch_objdump_flags="--mattr=+morello"
else
vararg_flags=
if [ "$CHERIBSD_VERSION" -gt 20220314 ]; then
vararg_flags="-Xclang -morello-vararg=new"
fi
arch_cflags="-march=morello $vararg_flags"
arch_objdump_flags="--mattr=+morello"
fi
;;
riscv64-*-freebsd*|riscv64-freebsd*)
if run_tool llvm-readelf -h ${_SYSROOT}/lib/libc.so.7 | grep "Flags:.*cheriabi" >/dev/null; then
mabi=l64pc128d
else
mabi=lp64d
fi
arch_cflags="-march=rv64gcxcheri -mabi=$mabi -mno-relax"
;;
esac
arch_cflags="${arch_cflags} -Wno-implicit-int -Wno-implicit-function-declaration"
fi
case $basetool in
clang|clang++|clang-cpp)
toolflags=$arch_cflags
;;
ld.lld|ld64.lld)
toolflags=$arch_ldflags
;;
objdump)
toolflags=$arch_objdump_flags
;;
*)
debug no special args for $tool
;;
esac
run_tool "${basetool}" $toolflags "$@"
|