Skip to content

Commit 1106761

Browse files
wkozaczuknyh
authored andcommitted
elf: when relocating do not lookup STB_LOCAL symbols by name
As the issue #1065 describes, some linkers (like gold) in certain scenarios generate ELF objects that end up with symbols with local binding (STB_LOCAL) that need to be relocated. The known concrete scenarios involve symbols of type STT_SECTION and STT_TLS which fail to get relocated by OSv linker as it attempts to look them up by name and they are not present in the relevant string lookup tables or worse point to the empty name in the string table at index 0. To address it we follow what Musl does which is simply looking such symbols by index in the current ELF object instead of by name. This patch also adds extra unit test that fails without this patch to prove that the fix is correct. Fixes #1065 Signed-off-by: Waldemar Kozaczuk <jwkozaczuk@gmail.com> Message-Id: <20191223170751.7166-1-jwkozaczuk@gmail.com>
1 parent c9640a3 commit 1106761

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

core/elf.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,13 @@ symbol_module object::symbol(unsigned idx, bool ignore_missing)
642642
auto symtab = dynamic_ptr<Elf64_Sym>(DT_SYMTAB);
643643
assert(dynamic_val(DT_SYMENT) == sizeof(Elf64_Sym));
644644
auto sym = &symtab[idx];
645+
auto binding = symbol_binding(*sym);
646+
if (binding == STB_LOCAL) {
647+
return symbol_module(sym, this);
648+
}
645649
auto nameidx = sym->st_name;
646650
auto name = dynamic_ptr<const char>(DT_STRTAB) + nameidx;
647651
auto ret = _prog.lookup(name);
648-
auto binding = symbol_binding(*sym);
649652
if (!ret.symbol && binding == STB_WEAK) {
650653
return symbol_module(sym, this);
651654
}

modules/tests/Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ tests := tst-pthread.so misc-ramdisk.so tst-vblk.so tst-bsd-evh.so \
119119
tst-sendfile.so misc-lock-perf.so tst-uio.so tst-printf.so \
120120
tst-pthread-affinity.so tst-pthread-tsd.so tst-thread-local.so \
121121
tst-zfs-mount.so tst-regex.so tst-tcp-siocoutq.so \
122-
libtls.so tst-tls.so tst-tls-pie.so tst-select-timeout.so tst-faccessat.so \
122+
libtls.so libtls_gold.so tst-tls.so tst-tls-gold.so tst-tls-pie.so tst-select-timeout.so tst-faccessat.so \
123123
tst-fstatat.so misc-reboot.so tst-fcntl.so payload-namespace.so \
124124
tst-namespace.so tst-without-namespace.so payload-env.so \
125125
payload-merge-env.so misc-execve.so misc-execve-payload.so misc-mutex2.so \
@@ -162,6 +162,18 @@ $(out)/tests/tst-tls-pie.so: \
162162
$(makedir)
163163
$(call quiet, cd $(out); $(CXX) $(CXXFLAGS) -fuse-ld=bfd -pthread -pie -o $@ $< tests/libtls.so, LD tests/tst-tls-pie.so)
164164

165+
$(out)/tests/libtls_gold.so: COMMON += -fuse-ld=gold
166+
$(out)/tests/libtls_gold.so: $(out)/tests/libtls.o
167+
$(makedir)
168+
$(call quiet, $(CXX) $(CXXFLAGS) -shared -o $@ $< $(LIBS), LD tests/libtls_gold.so)
169+
170+
$(out)/tests/tst-tls-gold.so: COMMON += -fuse-ld=gold
171+
$(out)/tests/tst-tls-gold.so: \
172+
$(src)/tests/tst-tls.cc \
173+
$(out)/tests/libtls_gold.so
174+
$(makedir)
175+
$(call quiet, cd $(out); $(CXX) $(CXXFLAGS) -D__SHARED_OBJECT__=1 -shared -o $@ $< tests/libtls_gold.so, CXX tests/tst-tls.cc)
176+
165177
boost-lib-dir := $(dir $(shell $(CC) --print-file-name libboost_system.so))
166178

167179
boost-tests := tst-vfs.so tst-libc-locking.so misc-fs-stress.so \

0 commit comments

Comments
 (0)