-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile
executable file
·132 lines (101 loc) · 3.81 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
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
INTERPOSED_LD_PATH ?= /lib64/ld-linux-x86-64.so.2.orig
LD_INJECT_ENV_PATH ?= /etc/ld-inject.env
MUSL_VERSION ?= 1.1.24
MUSL_ARCHIVE ?= musl-$(MUSL_VERSION).tar.gz
# We build both a loader and a shared library (DSO)
#############################
### Common flags
CFLAGS += -std=c99 -g -O2 -fPIC -pie -fno-stack-protector
CFLAGS += -fdata-sections -ffunction-sections # allow removing unused code
# We don't want to export any symbols by default. we use our LIB_EXPORT macro if we need otherwise.
CFLAGS += -fvisibility=hidden
# These are the warnings musl uses. We keep the same as we compile their sources.
CFLAGS += -Werror=implicit-function-declaration -Werror=implicit-int -Werror=pointer-sign -Werror=pointer-arith
CFLAGS += -D_GNU_SOURCE
LDFLAGS += -fPIC -pie
LDFLAGS += -Wl,--gc-sections # remove unused code
#############################
### Loader flags
ARCH = x86_64
CFLAGS_LD += -nostdinc # we use musl for the loader, not glibc
# These are musl includes. Order matters.
CFLAGS_LD += -D_GNU_SOURCE \
-Imusl/arch/$(ARCH) \
-Imusl/arch/generic \
-Imusl/obj/src/internal \
-Imusl/src/include \
-Imusl/src/internal \
-Imusl/obj/include \
-Imusl/include
LDFLAGS_LD += -shared # needed so that our interpreter can load static and dynamic executables
LDFLAGS_LD += -nostdlib -Wl,--exclude-libs,ALL # we don't export any symbols from musl
LDFLAGS_LD += -Wl,-z,defs # complain about undefined symbols
LDFLAGS_LD += -Wl,-e_dlstart
#############################
### DSO flags
LDFLAGS_DSO += -shared -ldl
#############################
### Dependencies
CONFIG_H = src/config.h
AUTOGEN_DEPS = src/linux/capflags.c $(CONFIG_H)
DEPS = $(wildcard src/*.h) $(wildcard src/linux/*) $(wildcard src/gcc/*) $(AUTOGEN_DEPS) musl/lib/libc.a
TARGET_LD = ld-virtcpuid.so
TARGET_DSO = libvirtcpuid.so
TARGET_FEATURES_JSON = lcd_mask/features.json
TARGET_FEATURES_JSON_GEN = lcd_mask/features_gen
OBJECTS_LD = build/ld/loader.o \
build/ld/cpuid.o
OBJECTS_DSO = build/dso/dso_signal.o \
build/dso/cpuid.o
OBJECTS_LCD_MASK_FEATURES_GEN = lcd_mask/features_gen.o
all: $(TARGET_DSO) $(TARGET_LD) $(TARGET_FEATURES_JSON);
build/ld:
mkdir -p $@
build/dso:
mkdir -p $@
$(MUSL_ARCHIVE):
wget https://musl.libc.org/releases/$@
musl: $(MUSL_ARCHIVE)
rm -rf musl.tmp
mkdir musl.tmp
tar xzf $(MUSL_ARCHIVE) -C musl.tmp --strip-components=1
mv musl.tmp musl
musl/lib/libc.a: musl
cd musl && ./configure --disable-shared
+make -C musl
$(TARGET_LD): $(OBJECTS_LD) musl/lib/libc.a
$(CC) $^ $(LDFLAGS) $(LDFLAGS_LD) -o $@
$(TARGET_DSO): $(OBJECTS_DSO)
$(CC) $^ $(LDFLAGS) $(LDFLAGS_DSO) -o $@
$(TARGET_FEATURES_JSON_GEN): $(OBJECTS_LCD_MASK_FEATURES_GEN)
$(CC) $^ -o $@
$(TARGET_FEATURES_JSON): $(TARGET_FEATURES_JSON_GEN)
$< $@
src/linux/capflags.c: src/linux/cpufeatures.h src/linux/mkcapflags.sh
sh src/linux/mkcapflags.sh $< $@
build/ld/%.o: src/%.c $(DEPS) | build/ld
$(CC) -c $(CFLAGS) $(CFLAGS_LD) $< -o $@
build/dso/%.o: src/%.c $(DEPS) | build/dso
$(CC) -c $(CFLAGS) $(CFLAGS_DSO) $< -o $@
%.o: %.c $(DEPS)
$(CC) -c $(CFLAGS) $< -o $@
.PHONY: gen_config # can depend on env vars, so it must always be checked
gen_config:
@echo "// Autogenerated by Makefile" > $(CONFIG_H).tmp
@echo "#ifndef CONFIG_H" >> $(CONFIG_H).tmp
@echo "#define CONFIG_H" >> $(CONFIG_H).tmp
@echo "#define INTERPOSED_LD_PATH \"$(INTERPOSED_LD_PATH)\"" >> $(CONFIG_H).tmp
@echo "#define LD_INJECT_ENV_PATH \"$(LD_INJECT_ENV_PATH)\"" >> $(CONFIG_H).tmp
@echo "#endif" >> $(CONFIG_H).tmp
@cmp $(CONFIG_H) $(CONFIG_H).tmp > /dev/null 2>&1 \
|| (echo "Generating config.h" && mv $(CONFIG_H).tmp $(CONFIG_H))
@rm -f $(CONFIG_H).tmp
$(CONFIG_H): gen_config;
.PHONY: clean
clean:
rm -rf build $(TARGET_DSO) $(TARGET_LD) \
$(TARGET_FEATURES_JSON) $(TARGET_FEATURES_JSON_GEN) \
$(OBJECTS_LD) \
$(OBJECTS_DSO) \
$(OBJECTS_LCD_MASK_FEATURES_GEN) \
$(AUTOGEN_DEPS) musl.tmp