Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mold refused to link GP-setup code for RISC-V #1182

Closed
piggynl opened this issue Jan 31, 2024 · 1 comment
Closed

mold refused to link GP-setup code for RISC-V #1182

piggynl opened this issue Jan 31, 2024 · 1 comment

Comments

@piggynl
Copy link

piggynl commented Jan 31, 2024

Description

While building musl for riscv64 with mold as linker, mold refused to link the GP-setup code.

A minimal reproduce

This assembly snippet is created from arch/riscv64/crt_arch.h, which is included by ldso/dlstart.c. Save this snippet as dlstart.s.

.section .sdata,"aw"

.text
.global _dlstart
.type _dlstart,%function
_dlstart:
.weak __global_pointer$
.hidden __global_pointer$
.option push
.option norelax
        lla     gp, __global_pointer$
.option pop
        mv      a0, sp
.weak _DYNAMIC
.hidden _DYNAMIC
        lla     a1, _DYNAMIC
        andi    sp, sp, -16
        tail    _dlstart_c

.type _dlstart_c,%function
_dlstart_c:
        # The actual code here does not matter.
        ret

Then compile and link.

$ clang --target=riscv64 -march=rv64g dlstart.s -c -o dlstart.o
$ ld.lld -e _dlstart -shared dlstart.o -o fake_libc.so # no error
$ mold -shared -e _dlstart dlstart.o -o fake_libc.so
mold: error: dlstart.o:(.text): R_RISCV_PCREL_HI20 relocation at offset 0x0 against symbol `__global_pointer$' can not be used; recompile with -fno-PIC

Note that the GP-setup code in _dlstart do follows the recommended way to initialize the gp register in RISC-V ELF specification, and the PIC option for compiler doesn't affect the content of dlstart.o.

More information

With git bisect I found mold refuses to link such input after 5fb6091.

@rui314
Copy link
Owner

rui314 commented Jan 31, 2024

Thank you for your report. Maybe we should just revert the commit that broke your test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants