mold icon indicating copy to clipboard operation
mold copied to clipboard

Hidden weak undefined symbol generates dynamic relocation in shared library

Open 64 opened this issue 2 years ago • 1 comments

Here is a simple reproducer:

// foo.cpp
[[gnu::weak]] [[gnu::visibility("hidden")]] void foo();

int bar() {
	if (foo) {
		foo();
		return 0;
	} else {
		return 1;
	}
}

Then compile with

$ clang++ foo.cpp -mno-relax --target=riscv64-unknown-linux-none -Wall -c -o foo.o

and link with

$ mold foo.o -shared -o foo.so

Then we have:

$ llvm-readelf -r foo.so

Relocation section '.rela.dyn' at offset 0x290 contains 1 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000003000  0000000100000002 R_RISCV_64             0000000000000000 _Z3foov + 0

This behaviour differs to lld and GNU ld, e.g:

$ ld.lld foo.o -shared -o foo.so
$ llvm-readelf -r foo.so

There are no relocations in this file.

Mold version used:

$ ./mold -v
mold 1.2.1 (826bb56d59312bd8a4ad3fc1d60aa7b430cee94b; compatible with GNU ld)

64 avatar Jun 01 '22 00:06 64

Thank you for your report! It looks like this is a bug in mold. We want to fix this before the next release.

rui314 avatar Jun 04 '22 02:06 rui314

Hi, @rui314! I am trying to tackle this atm. It looks like this might be fixed if we can correctly convert hidden globals to local symbols. It might take me some time to figure this one out since I am trying to understand all implications of this change.

rbartlensky avatar Sep 14 '22 23:09 rbartlensky

This one has fallen off my radar for some reason, though I wanted to get this fixed.

@rbartlensky I'll assign it to you to avoid duplicated work. Please reassign to me if you don't want to take this one.

rui314 avatar Sep 15 '22 00:09 rui314

Fixed in https://github.com/rui314/mold/commit/adde0130f5e24b6bc6551270395c79d679d80c06

rui314 avatar Sep 18 '22 13:09 rui314