1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-04 16:48:04 +03:00
mold/test/elf/synthetic-symbols.sh
Rui Ueyama e6d4154688 [ELF] Generalize tests
With this change, you can now cross compile test cases and run
them on qemu-user. Here is an example to run our test suits in
an emulated ARM32 environment.

$ CC=arm-linux-gnueabihf-gcc \
  CXX=arm-linux-gnueabihf-g++ \
  GCC=arm-linux-gnueabihf-gcc \
  GXX=arm-linux-gnueabihf-g++ \
  OBJDUMP=arm-linux-gnueabihf-objdump \
  MACHINE=arm \
  QEMU='qemu-arm -L /usr/arm-linux-gnueabihf' \
  make -j16 test
2022-04-02 16:53:41 +08:00

111 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
export LC_ALL=C
set -e
CC="${CC:-cc}"
CXX="${CXX:-c++}"
GCC="${GCC:-gcc}"
GXX="${GXX:-g++}"
OBJDUMP="${OBJDUMP:-objdump}"
MACHINE="${MACHINE:-$(uname -m)}"
testname=$(basename "$0" .sh)
echo -n "Testing $testname ... "
cd "$(dirname "$0")"/../..
mold="$(pwd)/mold"
t=out/test/elf/$testname
mkdir -p $t
[ $MACHINE = x86_64 ] || { echo skipped; exit; }
cat <<EOF | $CC -c -o $t/a.o -x assembler -
.section foo,"a",@progbits
.ascii "section foo"
EOF
# Test synthetic symbols
cat <<EOF | $CC -c -o $t/b.o -xc -
#include <assert.h>
#include <stdio.h>
#include <string.h>
extern char __ehdr_start[];
extern char __executable_start[];
extern char _end[];
extern char end[];
extern char _etext[];
extern char etext[];
extern char _edata[];
extern char edata[];
extern char __start_foo[];
extern char __stop_foo[];
int main() {
assert(_end);
assert(_end == end);
assert(_etext);
assert(_etext == etext);
assert(_edata);
assert(_edata == edata);
printf("__ehdr_start=%p\n", &__ehdr_start);
printf("__executable_start=%p\n", &__executable_start);
printf("%.*s\n", (int)(__stop_foo - __start_foo), __start_foo);
}
EOF
$CC -B. -no-pie -Wl,--image-base=0x40000 \
-o $t/exe $t/a.o $t/b.o
$QEMU $t/exe > $t/log
grep -q '^__ehdr_start=0x40000$' $t/log
grep -q '^__executable_start=0x40000$' $t/log
grep -q '^section foo$' $t/log
# Make sure that synthetic symbols overwrite existing ones
cat <<EOF | $CC -c -o $t/c.o -xc -
#include <assert.h>
#include <stdio.h>
#include <string.h>
char __ehdr_start[] = "foo";
char __executable_start[] = "foo";
char _end[] = "foo";
char end[] = "foo";
char _etext[] = "foo";
char etext[] = "foo";
char _edata[] = "foo";
char edata[] = "foo";
char __start_foo[] = "foo";
char __stop_foo[] = "foo";
int main() {
assert(_end);
assert(_end != end);
assert(_etext);
assert(_etext != etext);
assert(_edata);
assert(_edata != edata);
printf("end=%s\n", end);
printf("etext=%s\n", etext);
printf("edata=%s\n", edata);
printf("__ehdr_start=%p\n", &__ehdr_start);
printf("__executable_start=%p\n", &__executable_start);
printf("%.*s\n", (int)(__stop_foo - __start_foo), __start_foo);
}
EOF
$CC -B. -no-pie -Wl,--image-base=0x40000 -o $t/exe $t/a.o $t/c.o
$QEMU $t/exe > $t/log
grep -q '^end=foo$' $t/log
grep -q '^etext=foo$' $t/log
grep -q '^edata=foo$' $t/log
grep -q '^__ehdr_start=0x40000$' $t/log
grep -q '^__executable_start=0x40000$' $t/log
grep -q '^section foo$' $t/log
echo OK