-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.sh
executable file
·380 lines (353 loc) · 12.3 KB
/
build.sh
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# Default architecture and build type
ARCH=i386
DEFAULT_PLATFORM=pc99
BUILD_TYPE=Debug
TOOLCHAIN=llvm
if [[ $TOOLCHAIN != "llvm" ]]; then
echo "There is a GCC build profile but it is totally untested."
echo "Use the LLVM toolchain unless you want to try to make GCC work."
exit 1
fi
args=(${@,,})
if [[ ${args[@]} =~ "release" ]]; then
BUILD_TYPE=Release
elif [[ ${args[@]} =~ "reldbginfo" ]]; then
BUILD_TYPE=RelWithDebInfo
fi
if [[ ${args[@]} =~ "amd64" ]]; then
ARCH=amd64
fi
if [[ ${args[@]} =~ "arm64" ]]; then
ARCH=arm64
fi
if [[ ${ARCH} == "i386" ]]; then
SEL4_ARCH=ia32
RTLIB_ARCH=i386
MC_COMPILER_ARCH=i686
ELF_TRIPLE=i686-pc-linux-gnu
PE_TRIPLE=i686-pc-windows-msvc
elif [[ ${ARCH} == "amd64" ]]; then
SEL4_ARCH=x86_64
RTLIB_ARCH=x86_64
MC_COMPILER_ARCH=x86_64
ELF_TRIPLE=x86_64-pc-linux-gnu
PE_TRIPLE=x86_64-pc-windows-msvc
elif [[ ${ARCH} == "arm64" ]]; then
SEL4_ARCH=aarch64
RTLIB_ARCH=aarch64
MC_COMPILER_ARCH=x86_64
ELF_TRIPLE=aarch64-elf
PE_TRIPLE=aarch64-pc-windows-msvc
DEFAULT_PLATFORM=rockpro64
else
echo "Unsupported arch ${ARCH}"
exit 1
fi
PLATFORM=${PLATFORM:=$DEFAULT_PLATFORM}
BUILDDIR="build-$ARCH-${BUILD_TYPE,,}"
IMAGEDIR="images-$ARCH-${BUILD_TYPE,,}"
function build_failed
{
echo "#################################"
echo " Build failed."
echo "#################################"
exit 1
}
echo
echo "####################################################"
echo " Building ${BUILD_TYPE} version for ${ARCH}"
echo "####################################################"
cd "$(dirname "$0")"
RTLIB=$(echo ${PWD}/compiler-rt/libclang_rt.builtins-${RTLIB_ARCH}.a)
mkdir -p $BUILDDIR/{host,elf,pe_inc,ntdll,wdm,ntlnxshim,base,drivers,initcpio,ndk_lib,ddk_lib,ldk_lib,$IMAGEDIR}
cd $BUILDDIR
PE_INC=$(echo ${PWD}/pe_inc)
SPEC2DEF_PATH=$(echo ${PWD}/host/spec2def/spec2def)
UTF16LE_PATH=$(echo ${PWD}/host/utf16le/utf16le)
# Build spec2def with the native toolchain
cd host
echo
echo "---- Building native targets ----"
echo
cmake ../../tools -G Ninja
ninja
# Build ntos with the ELF toolchain
cd ../elf
echo
echo "---- Building ELF targets ----"
echo
cmake ../../private/ntos \
-DArch=${ARCH} \
-DTRIPLE=${ELF_TRIPLE} \
-DKernelPlatform=${PLATFORM} \
-DKernelSel4Arch=${SEL4_ARCH} \
-DCMAKE_TOOLCHAIN_FILE=../../sel4/${TOOLCHAIN}.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_INCLUDE_DIR="${PWD}/../../sel4/libsel4/include" \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PWD}/../../sel4/libsel4/sel4_arch_include/${SEL4_ARCH}" \
-DSEL4_GENERATED_HEADERS_DIR=${PWD} \
-DSTRUCTURES_GEN_H_ORIG=${PWD}/kernel/generated/arch/object/structures_gen.h \
-DSTRUCTURES_GEN_DIR=${PWD} \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja all-elf || build_failed
cp rtl/syssvc_gen.h ../pe_inc || build_failed
cp rtl/wdmsvc_gen.h ../pe_inc || build_failed
# For PE targets, we modify how libsel4 retrives the IPC buffer address
# so it does not rely on the thread-local variable __sel4_ipc_buffer. Also,
# for 64-bit PE targets, since the ELF toolchain assumes sizeof(long) == 8
# and the PE toolchain assumes sizeof(long) == 4, we need to modify the
# libsel4 sel4_arch headers to undefine the macro SEL4_INT64_IS_LONG and
# define SEL4_INT64_IS_LONG_LONG. So far this seems to work and produce
# valid seL4 system calls. However we need to be very careful.
cd ../pe_inc
echo
echo "---- Building private PE targets ----"
echo
mkdir -p {kernel,libsel4,sel4_include/sel4,sel4_arch_include/sel4/sel4_arch}
cp ../elf/structures_gen.h . || build_failed
cp -r ../../sel4/libsel4/include/sel4/* sel4_include/sel4 || build_failed
cp -r ../../sel4/libsel4/sel4_arch_include/$SEL4_ARCH/sel4/sel4_arch/* \
sel4_arch_include/sel4/sel4_arch || build_failed
cp -r ../elf/kernel/gen_config kernel || build_failed
for i in gen_config autoconf include arch_include sel4_arch_include; do
cp -r ../elf/libsel4/$i libsel4 || build_failed
done
cat <<EOF > sel4_get_ipc_buffer.h
LIBSEL4_INLINE_FUNC seL4_IPCBuffer *seL4_GetIPCBuffer(void)
{
PCHAR NtTib = (PVOID)NtCurrentTib();
return (PVOID)(NtTib - NT_TIB_OFFSET);
}
EOF
sed -i '/__sel4_ipc_buffer/d' sel4_include/sel4/functions.h
sed -i '/void seL4_SetIPCBuffer/,/\}/d' sel4_include/sel4/functions.h
sed -i '/LIBSEL4_INLINE_FUNC seL4_IPCBuffer \*seL4_GetIPCBuffer/,/\}/d' sel4_include/sel4/functions.h
sed -i '/CONFIG_KERNEL_INVOCATION_REPORT_ERROR_IPC/,/#endif/d' sel4_include/sel4/functions.h
sed -i '/#include <sel4\/syscalls.h>/r sel4_get_ipc_buffer.h' sel4_include/sel4/functions.h
cat <<EOF >> sel4_include/sel4/functions.h
LIBSEL4_INLINE_FUNC char seL4_CanPrintError(void)
{
return 1;
}
EOF
if [[ $ARCH == "amd64" || $ARCH == "arm64" ]]; then
cat <<EOF > sel4_arch_include/sel4/sel4_arch/simple_types.h
#pragma once
#define SEL4_WORD_IS_UINT64
#define SEL4_INT64_IS_LONG_LONG
EOF
sed -i '/assert_size_correct(long/d' libsel4/include/interfaces/sel4_client.h || build_failed
sed -i '/assert_size_correct(seL4_X86_VMAttributes,/d' libsel4/include/interfaces/sel4_client.h || build_failed
sed -i '/assert_size_correct(seL4_ARM_VMAttributes,/d' libsel4/include/interfaces/sel4_client.h || build_failed
sed -i '/assert_size_correct(seL4_VCPUReg,/d' libsel4/include/interfaces/sel4_client.h || build_failed
fi
# Build ntdll.dll with the PE toolchain
cd ../ntdll
cmake ../../private/ntdll \
-DArch=${ARCH} \
-DTRIPLE=${PE_TRIPLE} \
-DKernelPlatform=${PLATFORM} \
-DKernelSel4Arch=${SEL4_ARCH} \
-DMC_COMPILER_ARCH=${MC_COMPILER_ARCH} \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_INCLUDE_DIR="${PE_INC}/sel4_include" \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PE_INC}/sel4_arch_include" \
-DSEL4_GENERATED_HEADERS_DIR="${PE_INC}" \
-DSTRUCTURES_GEN_DIR="${PE_INC}" \
-DSPEC2DEF_PATH=${SPEC2DEF_PATH} \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGENINC_PATH=${PWD}/../host/geninc/geninc \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-DSVCGEN_TYPE="--client" \
-G Ninja
ninja || build_failed
cp ntdll.lib ../ndk_lib || build_failed
cp ntdllp.lib ../ndk_lib || build_failed
echo
# Build wdm.dll with the PE toolchain
cd ../wdm
cmake ../../private/wdm \
-DArch=${ARCH} \
-DTRIPLE=${PE_TRIPLE} \
-DKernelPlatform=${PLATFORM} \
-DKernelSel4Arch=${SEL4_ARCH} \
-DMC_COMPILER_ARCH=${MC_COMPILER_ARCH} \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_INCLUDE_DIR="${PE_INC}/sel4_include" \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PE_INC}/sel4_arch_include" \
-DSEL4_GENERATED_HEADERS_DIR="${PE_INC}" \
-DSTRUCTURES_GEN_DIR="${PE_INC}" \
-DSPEC2DEF_PATH=${SPEC2DEF_PATH} \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGENINC_PATH=${PWD}/../host/geninc/geninc \
-DNDK_LIB_PATH=${PWD}/../ndk_lib \
-DGEN_INC_DIR=${PWD}/../ntdll \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-DSVCGEN_TYPE="--client" \
-G Ninja
ninja || build_failed
cp wdm.lib ../ddk_lib || build_failed
# Build ntlnxshim.so with the ELF toolchain. Note for ntlnxshim, even though it is an
# ELF target, we use the modified sel4_include headers so the seL4 IPC buffer address
# is obtained from the NtCurrentTib() call, rather than from a thread-local variable.
cd ../ntlnxshim
cp ../elf/structures_gen.h . || build_failed
cp ../elf/rtl/syssvc_gen.h . || build_failed
cp ../elf/rtl/wdmsvc_gen.h . || build_failed
cmake ../../private/ntlnxshim \
-DArch=${ARCH} \
-DTRIPLE=${ELF_TRIPLE} \
-DKernelPlatform=${PLATFORM} \
-DKernelSel4Arch=${SEL4_ARCH} \
-DCMAKE_TOOLCHAIN_FILE=../../sel4/${TOOLCHAIN}.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_INCLUDE_DIR="${PE_INC}/sel4_include" \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PWD}/../../sel4/libsel4/sel4_arch_include/${SEL4_ARCH}" \
-DSEL4_GENERATED_HEADERS_DIR="${PWD}/../elf" \
-DSTRUCTURES_GEN_DIR="${PWD}" \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-DSVCGEN_TYPE="--utf8_client" \
-G Ninja
ninja || build_failed
cp libntlnxshimk.a ../ldk_lib || build_failed
cp libntlnxshimu.a ../ldk_lib || build_failed
# Build drivers with the PE toolchain
cd ../drivers
echo
echo "---- Building drivers ----"
echo
cmake ../../drivers \
-DArch=${ARCH} \
-DTRIPLE=${PE_TRIPLE} \
-DMC_COMPILER_ARCH=${MC_COMPILER_ARCH} \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DNDK_LIB_PATH=${PWD}/../ndk_lib \
-DDDK_LIB_PATH=${PWD}/../ddk_lib \
-DSPEC2DEF_PATH=${SPEC2DEF_PATH} \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja || build_failed
# Build base NT clients with the PE toolchain
cd ../base
echo
echo "---- Building base NT clients ----"
echo
cmake ../../base \
-DTRIPLE=${PE_TRIPLE} \
-DMC_COMPILER_ARCH=${MC_COMPILER_ARCH} \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DNDK_LIB_PATH=${PWD}/../ndk_lib \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja || build_failed
# Build initcpio
echo
echo "---- Building INITCPIO ----"
echo
cd ../initcpio
if [[ ${ARCH} == i386 ]]; then
OUTPUT_TARGET=elf32-i386
LINKER_EMULATION=elf_i386
elif [[ ${ARCH} == amd64 ]]; then
OUTPUT_TARGET=elf64-x86-64
LINKER_EMULATION=elf_x86_64
elif [[ ${ARCH} == arm64 ]]; then
OUTPUT_TARGET=elf64-aarch64
LINKER_EMULATION=elf_aarch64
else
echo "Unsupported architecture ${ARCH}"
exit 1
fi
PE_COPY_LIST='ntdll/ntdll.dll wdm/wdm.dll'
BASE_COPY_LIST='smss/smss.exe ntcmd/ntcmd.exe'
DRIVER_COPY_LIST='base/null/null.sys base/beep/beep.sys base/pnp/pnp.sys
bus/acpi/acpi.sys bus/pci/pci.sys input/kbdclass/kbdclass.sys
input/i8042prt/i8042prt.sys storage/fdc/fdc.sys filesystems/fatfs/fatfs.sys'
for i in ${PE_COPY_LIST}; do
cp ../$i . || build_failed
done
for i in ${BASE_COPY_LIST}; do
cp ../base/$i . || build_failed
done
for i in ${DRIVER_COPY_LIST}; do
cp ../drivers/$i . || build_failed
done
{ for i in ${PE_COPY_LIST}; do echo $(basename $i); done } > image-list
{ for i in ${BASE_COPY_LIST}; do echo $(basename $i); done } >> image-list
{ for i in ${DRIVER_COPY_LIST}; do echo $(basename $i); done } >> image-list
cpio -H newc -o < image-list > initcpio || build_failed
llvm-objcopy -I binary -O ${OUTPUT_TARGET} \
--rename-section .data=initcpio,CONTENTS,ALLOC,LOAD,READONLY,DATA \
initcpio initcpio.o
if [[ $? == 0 ]]; then
echo "Success."
else
build_failed
fi
# Link ntos and initcpio into final ntos image
echo
echo "---- Linking NTOS image ----"
echo
cd ../$IMAGEDIR
cp ../elf/kernel-$SEL4_ARCH-pc99 kernel || build_failed
if [[ ${BUILD_TYPE} == Release ]]; then
LLD_OPTIONS="--gc-sections -O 3"
else
LLD_OPTIONS=""
fi
ld.lld -m ${LINKER_EMULATION} ${LLD_OPTIONS} ${RTLIB} \
--allow-multiple-definition \
../elf/libntos.a \
../elf/rtl/librtl.a \
../initcpio/initcpio.o \
-T ../../private/ntos/ntos.lds \
-o ntos
if [[ $? == 0 ]]; then
echo "Success."
else
build_failed
fi
echo
echo "---- Stripping symbols for ${BUILD_TYPE} build ----"
echo
STRIP=llvm-strip
if [[ ${BUILD_TYPE} == Release ]]; then
cp kernel kernel-no-strip
cp ntos ntos-no-strip
$STRIP kernel && $STRIP ntos
if [[ $? == 0 ]]; then
echo "Success."
fi
else
$STRIP kernel -o kernel-stripped && $STRIP ntos -o ntos-stripped
if [[ $? == 0 ]]; then
echo "Success."
fi
fi
echo
echo "---- Merge compile_commands.json ----"
echo
if [[ $(which jq) ]]; then
jq -s add ../*/*.json > ../compile_commands.json
rm ../*/compile_commands.json
echo "Done."
else
echo "You'll need to install jq (https://jqlang.github.io/jq)."
fi
echo
echo "####################################"
echo " Build successful!"
echo "####################################"