1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 02:20:51 +03:00

Make linker-synthesized symbols to overwrite other symbols

This commit is contained in:
Rui Ueyama 2021-05-29 00:31:40 +09:00
parent 83c47216d1
commit 8fe15c8dc8
2 changed files with 65 additions and 2 deletions

View File

@ -303,8 +303,8 @@ void check_duplicate_symbols(Context<E> &ctx) {
const ElfSym<E> &esym = file->elf_syms[i];
Symbol<E> &sym = *file->symbols[i];
if (sym.file == file || esym.is_undef() || esym.is_common() ||
(esym.st_bind == STB_WEAK))
if (sym.file == file || sym.file == ctx.internal_obj ||
esym.is_undef() || esym.is_common() || (esym.st_bind == STB_WEAK))
continue;
if (!esym.is_abs() && !file->get_section(esym)->is_alive)

63
test/synthetic-symbols.sh Executable file
View File

@ -0,0 +1,63 @@
#!/bin/bash
set -e
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 <stdio.h>
#include <string.h>
extern char __ehdr_start[];
extern char __executable_start[];
extern char __start_foo[];
extern char __stop_foo[];
int main() {
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=`pwd`/../mold -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 <stdio.h>
#include <string.h>
char __ehdr_start[] = "foo";
char __executable_start[] = "foo";
char __start_foo[] = "foo";
char __stop_foo[] = "foo";
int main() {
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=`pwd`/../mold -Wl,--image-base=0x40000 -o $t/exe $t/a.o $t/c.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
echo OK