-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
executable file
·108 lines (103 loc) · 3.11 KB
/
Makefile
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
PROGNAME = main
obj:
clang \
-c \
-S \
-emit-llvm \
-fPIC \
-ffunction-sections \
$(PROGNAME).c \
-o $(PROGNAME).bc
base-bin: obj
opt \
--function-sections \
-O3 \
-loop-simplify \
< $(PROGNAME).bc \
> $(PROGNAME)_ls_opt.bc
llc \
$(PROGNAME)_ls_opt.bc \
-relocation-model=pic \
-o $(PROGNAME)_ls_opt.s
g++ \
$(PROGNAME)_ls_opt.s \
-o $(PROGNAME)_base \
-lm -lrt
bc-ics: obj
llvm-link $(PROGNAME).bc ics.ll -o $(PROGNAME)_ics.bc
bc-opt: bc-ics
opt \
--function-sections \
-O3 \
-loop-simplify \
< $(PROGNAME)_ics.bc \
> $(PROGNAME)_ls_ics_opt.bc
wpd-ics: bc-opt
opt \
--function-sections \
-load libWholeProgramDebloat.so \
-WholeProgramDebloat \
--enable-indirect-call-sinking=true \
< $(PROGNAME)_ls_ics_opt.bc \
> $(PROGNAME)_wpd_ics_opt.bc
# disassemble so we can do a hacky fix to the ics symbols
llvm-dis $(PROGNAME)_wpd_ics_opt.bc
sed -i \
's/declare extern_weak i32 @ics_map_indirect_call.2(i64)//w sed_changes_0.txt' \
$(PROGNAME)_wpd_ics_opt.ll
sed -i \
's/declare extern_weak i32 @ics_wrapper_debrt_protect_loop_end.3(i32)//w sed_changes_1.txt' \
$(PROGNAME)_wpd_ics_opt.ll
sed -i \
's/ics_map_indirect_call.2/ics_map_indirect_call/w sed_changes_2.txt' \
$(PROGNAME)_wpd_ics_opt.ll
sed -i \
's/ics_wrapper_debrt_protect_loop_end.3/ics_wrapper_debrt_protect_loop_end/w sed_changes_3.txt' \
$(PROGNAME)_wpd_ics_opt.ll
sed -i \
's/debrt_protect_indirect.1/debrt_protect_indirect/w sed_changes_5.txt' \
$(PROGNAME)_wpd_ics_opt.ll
sed -i \
's/declare i32 @debrt_protect_indirect(i64)$$//w sed_changes_4.txt' \
$(PROGNAME)_wpd_ics_opt.ll
# now ingest the modified .ll file and run always-inline on it.
opt \
--function-sections \
-always-inline \
< $(PROGNAME)_wpd_ics_opt.ll \
> $(PROGNAME)_wpd_ics_opt.bc
llc \
--function-sections \
$(PROGNAME)_wpd_ics_opt.bc \
-relocation-model=pic \
-o $(PROGNAME)_wpd_ics_opt.s
# Produce a default linker script.
# FIXME Is there an easier way to do this than building the entire binary here?
g++ \
-ffunction-sections \
-Wl,--verbose \
$(PROGNAME)_wpd_ics_opt.s \
-o $(PROGNAME)_wpd_tmp \
-ldebloat_rt -lm -lrt > linker.lds
rm $(PROGNAME)_wpd_tmp
# Create a custom linker script
python3 linker.py .
g++ \
-g \
-ffunction-sections \
-c $(PROGNAME)_wpd_ics_opt.s \
-o $(PROGNAME)_wpd_ics_opt.o
wpd-bin: wpd-ics
g++ \
-g \
-ffunction-sections \
-T wpd-linker-script.lds \
$(PROGNAME)_wpd_ics_opt.o \
-o $(PROGNAME)_wpd \
-ldebloat_rt -lm -lrt
readelf -s --wide $(PROGNAME)_wpd > readelf.out
readelf --sections $(PROGNAME)_wpd > readelf-sections.out
all: base-bin wpd-bin
clean:
rm -f *.bc *.s a.out *.lds readelf*.out wpd*.txt wpd*.out debrt.out debrt*.out sed_changes*.txt $(PROGNAME)_wpd $(PROGNAME)_wpd_ics_opt.o $(PROGNAME)_wpd_ics_opt.ll $(PROGNAME)_base ics.ll linker.py
.PHONY: clean