2020-12-17 14:21:51 +03:00
|
|
|
#!/bin/bash
|
2021-12-17 14:05:14 +03:00
|
|
|
export LANG=
|
2020-12-17 14:21:51 +03:00
|
|
|
set -e
|
2021-12-30 05:11:06 +03:00
|
|
|
testname=$(basename -s .sh "$0")
|
|
|
|
echo -n "Testing $testname ... "
|
|
|
|
cd "$(dirname "$0")"/../..
|
|
|
|
mold="$(pwd)/mold"
|
|
|
|
t="$(pwd)/out/test/elf/$testname"
|
2021-12-22 13:38:00 +03:00
|
|
|
mkdir -p "$t"
|
2020-12-17 14:21:51 +03:00
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
[ "$(uname -m)" = x86_64 ] || { echo skipped; exit; }
|
2021-08-23 07:44:38 +03:00
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' | cc -fPIC -c -o "$t"/a.o -x assembler -
|
2021-02-27 14:28:33 +03:00
|
|
|
.data
|
|
|
|
.globl ext_var
|
|
|
|
.type ext_var, @object
|
|
|
|
.size ext_var, 56
|
|
|
|
ext_var:
|
|
|
|
.long 56
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' | cc -fPIC -c -o "$t"/b.o -xc -
|
2021-02-27 14:28:33 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int print(int x) {
|
|
|
|
printf("%d\n", x);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-20 06:15:07 +03:00
|
|
|
|
|
|
|
int print64(long x) {
|
|
|
|
printf("%ld\n", x);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-27 14:28:33 +03:00
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
cc -shared -o "$t"/c.so "$t"/a.o "$t"/b.o
|
2021-02-27 14:28:33 +03:00
|
|
|
|
|
|
|
# Absolute symbol
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-02-27 14:28:33 +03:00
|
|
|
.globl abs_sym
|
|
|
|
.set abs_sym, 42
|
|
|
|
|
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
lea abs_sym, %edi
|
|
|
|
call print
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
ret
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -no-pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^42$'
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^42$'
|
2021-02-27 14:28:33 +03:00
|
|
|
|
|
|
|
# GOT
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-02-27 14:28:33 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
mov ext_var@GOTPCREL(%rip), %rdi
|
|
|
|
mov (%rdi), %edi
|
|
|
|
call print
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
ret
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -no-pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-02-27 14:28:33 +03:00
|
|
|
|
|
|
|
# Copyrel
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-02-27 14:28:33 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
mov ext_var(%rip), %edi
|
|
|
|
call print
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
ret
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -c -o "$t"/d.o "$t"/d.s
|
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.o -no-pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-02-27 14:28:33 +03:00
|
|
|
|
|
|
|
# Copyrel
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-02-27 14:28:33 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
mov foo(%rip), %rdi
|
|
|
|
mov (%rdi), %edi
|
|
|
|
call print
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
ret
|
|
|
|
|
|
|
|
.data
|
|
|
|
foo:
|
|
|
|
.quad ext_var
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -no-pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-02-27 14:28:33 +03:00
|
|
|
|
|
|
|
# PLT
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-02-27 14:28:33 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
mov $76, %edi
|
|
|
|
call print@PLT
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
ret
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -no-pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^76$'
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^76$'
|
2021-02-27 14:28:33 +03:00
|
|
|
|
|
|
|
# PLT
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-02-27 14:28:33 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
mov $76, %edi
|
|
|
|
lea print(%rip), %rax
|
|
|
|
call *%rax
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-02-27 14:28:33 +03:00
|
|
|
ret
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -no-pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^76$'
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s -pie
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^76$'
|
2020-12-17 14:21:51 +03:00
|
|
|
|
2021-03-20 06:15:07 +03:00
|
|
|
# SIZE32
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-03-20 06:15:07 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-03-20 06:15:07 +03:00
|
|
|
mov $foo+2@SIZE, %edi
|
|
|
|
call print@PLT
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-03-20 06:15:07 +03:00
|
|
|
ret
|
|
|
|
|
|
|
|
.data
|
|
|
|
.globl foo
|
|
|
|
.type foo, %object
|
|
|
|
.size foo, 24
|
|
|
|
foo:
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^26$'
|
2021-03-20 06:15:07 +03:00
|
|
|
|
|
|
|
# SIZE64
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/d.s
|
2021-03-20 06:15:07 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
2021-03-20 06:15:07 +03:00
|
|
|
movabs $foo+5@SIZE, %rdi
|
|
|
|
call print64@PLT
|
2021-07-15 18:11:03 +03:00
|
|
|
add $8, %rsp
|
2021-03-20 06:15:07 +03:00
|
|
|
ret
|
|
|
|
|
|
|
|
.data
|
|
|
|
.globl foo
|
|
|
|
.type foo, %object
|
|
|
|
.size foo, 56
|
|
|
|
foo:
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/d.s
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^61$'
|
2021-03-20 06:15:07 +03:00
|
|
|
|
2021-03-21 17:16:51 +03:00
|
|
|
# GOTPCREL64
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/e.c
|
2021-03-21 17:16:51 +03:00
|
|
|
extern long ext_var;
|
|
|
|
void print64(long);
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
print64(ext_var);
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -c -o "$t"/e.o "$t"/e.c -mcmodel=large -fPIC
|
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/c.so "$t"/e.o
|
2022-01-02 05:13:12 +03:00
|
|
|
"$t"/exe | grep -q '^56$'
|
2021-03-21 17:16:51 +03:00
|
|
|
|
2021-03-22 09:33:39 +03:00
|
|
|
# R_X86_64_32 against non-alloc section
|
2021-12-22 13:38:00 +03:00
|
|
|
cat <<'EOF' > "$t"/f.s
|
2021-03-22 09:33:39 +03:00
|
|
|
.globl main
|
|
|
|
main:
|
2021-07-15 18:11:03 +03:00
|
|
|
sub $8, %rsp
|
|
|
|
add $8, %rsp
|
2021-03-22 09:33:39 +03:00
|
|
|
ret
|
|
|
|
|
|
|
|
.section .foo, "", @progbits
|
|
|
|
.zero 16
|
|
|
|
foo:
|
|
|
|
.quad bar
|
|
|
|
|
|
|
|
.section .bar, "", @progbits
|
|
|
|
.zero 24
|
|
|
|
bar:
|
|
|
|
.quad foo
|
|
|
|
EOF
|
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
clang -c -o "$t"/f.o "$t"/f.s
|
|
|
|
clang -fuse-ld="$mold" -o "$t"/exe "$t"/f.o
|
|
|
|
readelf -x .foo -x .bar "$t"/exe > "$t"/log
|
2021-03-22 09:33:39 +03:00
|
|
|
|
2021-12-22 13:38:00 +03:00
|
|
|
fgrep -q '0x00000010 00000000 00000000 10000000 00000000' "$t"/log
|
|
|
|
fgrep -q '0x00000010 18000000 00000000' "$t"/log
|
2021-03-22 09:33:39 +03:00
|
|
|
|
2021-01-25 05:02:01 +03:00
|
|
|
echo OK
|