From 287f736e614a2931f57e9aabf42105e3cf3e8992 Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 17 Apr 2021 23:47:17 +0200 Subject: 04: able to map dependency & resolve reolcs and execture main program (initial commit) --- 04_dynld_nostd/Makefile | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 04_dynld_nostd/Makefile (limited to '04_dynld_nostd/Makefile') diff --git a/04_dynld_nostd/Makefile b/04_dynld_nostd/Makefile new file mode 100644 index 0000000..8cd9d35 --- /dev/null +++ b/04_dynld_nostd/Makefile @@ -0,0 +1,60 @@ +# Copyright (c) 2020 Johannes Stoelp + +COMMON_CFLAGS := -g -O0 -Wall -Wextra \ + -I../lib/include \ + -nostartfiles -nodefaultlibs + +run: main + ./$< + +main: dynld.so main.c ../lib/libcommon.a + @# For now ew only add support for ELF hash tables (DT_HASH). + @# Therefore we specify the `hash-style` below. + gcc -o libgreet.so \ + $(COMMON_CFLAGS) \ + -fPIC -shared \ + -Wl,--hash-style=sysv \ + libgreet.c + + @if readelf -W -S libgreet.so | grep plt >& /dev/null; then \ + echo "ERROR: libgreet.so contains PLT while we don't support relocation calls in libgreet.so!"; \ + echo " All function calls in libgreet.so must be statically resolved!"; \ + exit 1; \ + fi + + @# For now ew only add support for ELF hash tables (DT_HASH). + @# Therefore we specify the `hash-style` below. + 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 $@ + +dynld.so: dynld.S dynld.c ../lib/libcommon.a + gcc -o $@ \ + $(COMMON_CFLAGS) \ + -fPIC -static-pie \ + -fvisibility=hidden \ + -Wl,--entry=dl_start \ + -Wl,--no-allow-shlib-undefined \ + $^ + + @if readelf -W -S $@ | grep plt >& /dev/null; then \ + echo "ERROR: $@ contains PLT while we don't support relocation calls in $@!"; \ + echo " All function calls in $@ must be statically resolved!"; \ + exit 1; \ + fi + +../lib/libcommon.a: + make -C ../lib + +clean: + rm -f main libgreet.so + rm -f dynld.so -- cgit v1.2.3 From 85230524414b6d27664bf77c8584bfeced6c71cb Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 21 Apr 2021 23:41:59 +0200 Subject: add support to resolve all relocations in PLT & RELA tables; add global variable as example to libgreet.so --- 04_dynld_nostd/Makefile | 6 ------ 1 file changed, 6 deletions(-) (limited to '04_dynld_nostd/Makefile') diff --git a/04_dynld_nostd/Makefile b/04_dynld_nostd/Makefile index 8cd9d35..c1a472a 100644 --- a/04_dynld_nostd/Makefile +++ b/04_dynld_nostd/Makefile @@ -16,12 +16,6 @@ main: dynld.so main.c ../lib/libcommon.a -Wl,--hash-style=sysv \ libgreet.c - @if readelf -W -S libgreet.so | grep plt >& /dev/null; then \ - echo "ERROR: libgreet.so contains PLT while we don't support relocation calls in libgreet.so!"; \ - echo " All function calls in libgreet.so must be statically resolved!"; \ - exit 1; \ - fi - @# For now ew only add support for ELF hash tables (DT_HASH). @# Therefore we specify the `hash-style` below. gcc -o $@ \ -- cgit v1.2.3 From e6716313137bc397d7d7d42faf07b06a5fea65c1 Mon Sep 17 00:00:00 2001 From: johannst Date: Wed, 21 Apr 2021 23:44:16 +0200 Subject: own make target for libgreet.so --- 04_dynld_nostd/Makefile | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to '04_dynld_nostd/Makefile') diff --git a/04_dynld_nostd/Makefile b/04_dynld_nostd/Makefile index c1a472a..e72dde8 100644 --- a/04_dynld_nostd/Makefile +++ b/04_dynld_nostd/Makefile @@ -7,15 +7,7 @@ COMMON_CFLAGS := -g -O0 -Wall -Wextra \ run: main ./$< -main: dynld.so main.c ../lib/libcommon.a - @# For now ew only add support for ELF hash tables (DT_HASH). - @# Therefore we specify the `hash-style` below. - gcc -o libgreet.so \ - $(COMMON_CFLAGS) \ - -fPIC -shared \ - -Wl,--hash-style=sysv \ - libgreet.c - +main: dynld.so libgreet.so main.c ../lib/libcommon.a @# For now ew only add support for ELF hash tables (DT_HASH). @# Therefore we specify the `hash-style` below. gcc -o $@ \ @@ -31,6 +23,15 @@ main: dynld.so main.c ../lib/libcommon.a objdump --disassemble -j .plt -M intel $@ objdump --disassemble=_start -M intel $@ +libgreet.so: libgreet.c + @# For now ew only add support for ELF hash tables (DT_HASH). + @# Therefore we specify the `hash-style` below. + gcc -o $@ \ + $(COMMON_CFLAGS) \ + -fPIC -shared \ + -Wl,--hash-style=sysv \ + $^ + dynld.so: dynld.S dynld.c ../lib/libcommon.a gcc -o $@ \ $(COMMON_CFLAGS) \ -- cgit v1.2.3 From 96cf3e4ee49c256b214795e30eb66801bcdcef6d Mon Sep 17 00:00:00 2001 From: johannst Date: Mon, 26 Apr 2021 21:41:43 +0200 Subject: Makefile: update comments --- 04_dynld_nostd/Makefile | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to '04_dynld_nostd/Makefile') diff --git a/04_dynld_nostd/Makefile b/04_dynld_nostd/Makefile index e72dde8..e4dceb1 100644 --- a/04_dynld_nostd/Makefile +++ b/04_dynld_nostd/Makefile @@ -7,9 +7,12 @@ COMMON_CFLAGS := -g -O0 -Wall -Wextra \ 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 - @# For now ew only add support for ELF hash tables (DT_HASH). - @# Therefore we specify the `hash-style` below. gcc -o $@ \ $(COMMON_CFLAGS) \ -L$(CURDIR) -lgreet \ @@ -18,20 +21,26 @@ main: dynld.so libgreet.so main.c ../lib/libcommon.a -no-pie \ $(filter %.c %.a, $^) - readelf -W --dynamic $@ - readelf -W --program-headers $@ - objdump --disassemble -j .plt -M intel $@ - objdump --disassemble=_start -M intel $@ + #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 - @# For now ew only add support for ELF hash tables (DT_HASH). - @# Therefore we specify the `hash-style` below. 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) \ @@ -41,9 +50,8 @@ dynld.so: dynld.S dynld.c ../lib/libcommon.a -Wl,--no-allow-shlib-undefined \ $^ - @if readelf -W -S $@ | grep plt >& /dev/null; then \ - echo "ERROR: $@ contains PLT while we don't support relocation calls in $@!"; \ - echo " All function calls in $@ must be statically resolved!"; \ + @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 -- cgit v1.2.3