summarylogtreecommitdiffstats
path: root/sgx-compile
blob: 76c3f3d3850e4610d322160f1a43577958e3736b (plain)
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
#!/bin/bash
loader=$(mktemp -p /tmp sgx-XXXXXXXX.lds)
sed \
-e "/ENCT_START/,/ENCT_END/{ /ENCT_START/{p; i \\
	O_TEXT_SEGMENTS \\
	/usr/lib/sgx/polarssl_sgx/*.o(.text) \\
	/usr/lib/sgx/lib/*.o(.text) \\
	/usr/lib/sgx/*(.enc_text) \\
	/usr/lib/sgx/*Lib.o(.text)
}; /ENCT_END/p; d }" \
-e "/ENCD_START/,/ENCD_END/{ /ENCD_START/{p; i \\
	O_DATA_SEGMENTS \\
	/usr/lib/sgx/polarssl_sgx/*.o(.data .data.rel.local .bss .rodata COMMON) \\
	/usr/lib/sgx/lib/*.o(.data .data.rel.local .bss .rodata COMMON) \\
	/usr/lib/sgx/*(.enc_data) \\
	/usr/lib/sgx/*Lib.o(.data .data.rel.local .bss .rodata COMMON)
}; /ENCD_END/p; d }"  /usr/lib/sgx/sgx.lds > "$loader"

args=()
for a in "$@"; do
	if [ ! -e "$a" ]; then
		args=("${args[@]}" "$a")
		continue
	fi
	if [ "${a%.c}" = "$a" ]; then
		args=("${args[@]}" "$a")
		continue
	fi

	# Compile all .cs into .os so they can be included in .enc_data/.enc_text
	cc -c -g -Wall -pedantic -Wno-unused-function -std=gnu1x -fno-stack-protector -fvisibility=hidden -o "${a%.c}.o" "$a"
	end=$?
	if [ $end -ne 0 ]; then
		exit $end
	fi

	sed -i \
	-e "/O_TEXT_SEGMENTS/i \\
	${a%.c}.o(.text)" \
	-e "/O_DATA_SEGMENTS/i \\
	${a%.c}.o(.data .data.rel.local .bss .rodata COMMON)" "$loader"
	args=("${args[@]}" "${a%.c}.o")
done

sed -i -e '/O_TEXT_SEGMENTS/d' -e '/O_DATA_SEGMENTS/d' "$loader"

find /usr/lib/sgx/ -type f -iname '*.o' -print0 | xargs -0 cc -g -Wall -pedantic -Wno-unused-function -std=gnu1x -fno-stack-protector -fvisibility=hidden "-Wl,-T,$loader" "${args[@]}"
rm "$loader"