Bug 28789 - RISC-V: ld resolves absolute symbols via PC-relative relocations for position-independent targets
Summary: RISC-V: ld resolves absolute symbols via PC-relative relocations for position...
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Palmer Dabbelt
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-18 02:58 UTC by Palmer Dabbelt
Modified: 2023-03-31 10:32 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
First shot at a patch (2.06 KB, patch)
2022-01-18 02:58 UTC, Palmer Dabbelt
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Palmer Dabbelt 2022-01-18 02:58:52 UTC
Created attachment 13911 [details]
First shot at a patch

During a recent discussion on the psABI spec, it came up that we're allowing absolute symbols to be resolved via PC-relative addressing sequences when linked into position-independent targets.  This may not produce the expected answer at runtime, which is not particularly friendly for users.

I've attached a patch that resolves some of these issues, but see the big FIXME related to absolute symbols defined by linker scripts.  I'm not sure what the expected behavior is there, but x86 is explicitly treating these as resolvable via PC-relative relocations and that's not what I'd expect so I'm assuming I'm missing something.  Our GP initialization sequences are also using absolute addresses in this way, which is at best worrisome. 

Given how late we are in the 2.38 cycle and how tricky absolute symbols are, I think it's best if we hold off on doing anything for now.
Comment 1 Sourceware Commits 2023-03-29 23:58:44 UTC
The master branch has been updated by Nelson Chu <nelsonc1225@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=890744e8585ad75e4adf7b4c447301540479a885

commit 890744e8585ad75e4adf7b4c447301540479a885
Author: Palmer Dabbelt <palmer@rivosinc.com>
Date:   Sat Mar 25 08:41:13 2023 +0800

    RISC-V: PR28789, Reject R_RISCV_PCREL relocations with ABS symbol in PIC/PIE.
    
    The non-preemptible SHN_ABS symbol with a pc-relative relocation should be
    disallowed when generating shared object (pic and pie).  Generally, the
    following cases, which refer to pr25749, will cause a symbol be
    non-preemptible,
    
    * -pie, or -shared with -symbolic
    * STV_HIDDEN, STV_INTERNAL, STV_PROTECTED
    * Have dynamic symbol table, but without the symbol
    * VER_NDX_LOCAL
    
    However, PCREL_HI20/LO12 relocs are always bind locally when generating
    shared object, so not only the non-preemptible absolute symbol need to
    be disallowed, all absolute symbol references need but except that they
    are defined in linker script.  If we also disallow the absolute symbol
    in linker script, then the glibc-linux toolchain build failed, so regard
    them as pc-relative symbols, just like what x86 did.
    
    Maybe we should add this check for all pc-relative relocations, rather
    than just handle in R_RISCV_PCREL relocs.  Ideally, since the value of
    SHN_ABS symbol is a constant, only S - A relocations should be allowed
    in the shared object, so only BFD_RELOC_8/16/32/64 are allowed, which
    means R_RISCV_32/R_RISCV_64.
    
    bfd/
        PR 28789
        * elfnn-riscv.c (riscv_elf_check_relocs): The absolute symbol cannot be
        referneced with pc-relative relocation when generating shared object.
    ld/
        PR 28789
        * ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp: Updated.
        * ld/testsuite/ld-riscv-elf/pcrel-reloc*: New testcases.
Comment 2 Nelson Chu 2023-03-31 10:32:00 UTC
Marked as resolved and fixed.