1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-15 14:36:25 +03:00
mold/test/synthetic-symbols.sh
Rui Ueyama fe26dad744 Fix cmake build
You can now build mold with the following commands:

  $ mkdir -p out/debug
  $ cd out/debug
  $ cmake -GNinja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug ../..
  $ ninja

To run tests, use the following commands:

  $ cd out/debug
  $ ctest -j$(nproc)
2021-07-20 00:33:28 +09:00

102 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
set -e
mold=$1
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | clang -c -o $t/a.o -x assembler -
.section foo,"a",@progbits
.ascii "section foo"
EOF
# Test synthetic symbols
cat <<EOF | clang -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
clang -fuse-ld=$mold -no-pie -Wl,--image-base=0x40000 \
-o $t/exe $t/a.o $t/b.o
$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 | clang -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
clang -fuse-ld=$mold -no-pie -Wl,--image-base=0x40000 \
-o $t/exe $t/a.o $t/c.o
$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