aboutsummaryrefslogtreecommitdiff
path: root/04_dynld_nostd/Makefile
blob: f953e94ccd76d79be3c595f5667ff82376f078c1 (plain) (blame)
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
# Copyright (c) 2020 Johannes Stoelp

COMMON_CFLAGS := -g -O0 -Wall -Wextra \
                 -I../lib/include     \
                 -nostartfiles -nodefaultlibs

run: main
	./$<

# Build the example user program.
#
# We explicitly set the dynamic linker to `dynld.so` and use the ELF hash table
# (DT_HASH), as we didn't implement support for the GNU hash table in our
# dynamic linker.
main: dynld.so libgreet.so main.c ../lib/libcommon.a
	gcc -o $@                                   \
	    $(COMMON_CFLAGS)                        \
	    -L$(CURDIR) -lgreet                     \
	    -Wl,--dynamic-linker=$(CURDIR)/dynld.so \
	    -Wl,--hash-style=sysv                   \
	    -no-pie \
	    $(filter %.c %.a, $^)

	#readelf -W --dynamic $@
	#readelf -W --program-headers $@
	#objdump --disassemble -j .plt -M intel $@
	#objdump --disassemble=_start -M intel $@

# Build the example shared library.
#
# We explicitly use the ELF hash table (DT_HASH), as we didn't implement
# support for the GNU hash table in our dynamic linker.
libgreet.so: libgreet.c
	gcc -o $@                 \
	    $(COMMON_CFLAGS)      \
	    -fPIC -shared         \
	    -Wl,--hash-style=sysv \
	    $^

# Build the dynamic linker.
#
# We assert that the dynamic linker doesn't contain any relocations as we
# didn't implement support to resolve its own relocations.
dynld.so: dynld.S dynld.c ../lib/libcommon.a
	gcc -o $@                \
	    $(COMMON_CFLAGS)     \
	    -fPIC -static-pie    \
	    -fvisibility=hidden  \
	    -Wl,--entry=dl_start \
	    -Wl,--no-undefined   \
	    $^

	@if ! readelf -r $@ | grep 'There are no relocations in this file' >& /dev/null; then \
		echo "ERROR: $@ contains relocations while we don't support relocations in $@!"; \
		exit 1; \
	fi

../lib/libcommon.a:
	make -C ../lib

clean:
	rm -f main libgreet.so
	rm -f dynld.so