1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-27 10:23:41 +03:00
This commit is contained in:
Rui Ueyama 2021-02-25 20:26:10 +09:00
parent c118086d29
commit f9ecf89b16
4 changed files with 50 additions and 1 deletions

10
main.cc
View File

@ -1287,6 +1287,16 @@ int main(int argc, char **argv) {
});
}
// If we are linking a .so file, remaining undefined symbols does
// not cause a linker error. Instead, they are treated as if they
// were imported symbols.
if (config.shared) {
Timer t("claim_unresolved_symbols");
tbb::parallel_for_each(out::objs, [](ObjectFile *file) {
file->claim_unresolved_symbols();
});
}
// Beyond this point, no new symbols will be added to the result.
// Make sure that all symbols have been resolved.

3
mold.h
View File

@ -925,6 +925,7 @@ public:
void handle_undefined_weak_symbols();
void resolve_comdat_groups();
void eliminate_duplicate_comdat_groups();
void claim_unresolved_symbols();
void scan_relocations();
void convert_common_symbols();
void compute_symtab();
@ -1230,7 +1231,7 @@ inline bool Symbol::is_absolute() const {
}
inline bool Symbol::is_imported() const {
return file->is_dso;
return file->is_dso || (esym && esym->is_undef() && esym->st_bind != STB_WEAK);
}
inline u64 Symbol::get_addr() const {

View File

@ -650,6 +650,24 @@ void ObjectFile::eliminate_duplicate_comdat_groups() {
}
}
void ObjectFile::claim_unresolved_symbols() {
if (!is_alive)
return;
for (i64 i = first_global; i < symbols.size(); i++) {
const ElfSym &esym = elf_syms[i];
Symbol &sym = *symbols[i];
std::lock_guard lock(sym.mu);
if (esym.is_undef() && (!sym.esym || sym.esym->is_undef())) {
if (sym.file && sym.file->priority < this->priority)
continue;
sym.file = this;
sym.esym = &esym;
}
}
}
void ObjectFile::scan_relocations() {
// Scan relocations against seciton contents
for (InputSection *isec : sections)

20
test/shared.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/bash
set -e
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | clang -fPIC -c -o $t/a.o -x assembler -
.globl fn1, fn2
fn1:
call fn2
EOF
clang -shared -fuse-ld=`pwd`/../mold -o $t/b.so $t/a.o
readelf --dyn-syms $t/b.so > $t/log
grep -q '0000000000000000 0 NOTYPE GLOBAL DEFAULT UND fn2' $t/log
grep -q '000000000000111c 0 NOTYPE GLOBAL DEFAULT 16 fn1' $t/log
echo OK