#!/bin/bash
export LANG=
set -e
CC="${CC:-cc}"
CXX="${CXX:-c++}"
testname=$(basename -s .sh "$0")
echo -n "Testing $testname ... "
cd "$(dirname "$0")"/../..
mold="$(pwd)/mold"
t=out/test/elf/$testname
mkdir -p $t

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
$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
$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
