Skip to content

[SRSO] ld.lld fails with "at least one side of the expression must be absolute" after SRSO mitigation #1907

Closed

Description

@nathanchance

After https://git.kernel.org/linus/fb3bd914b3ec28f5fb697ac55c4846ac2d542855, x86_64 builds fail to link with:

ld.lld: error: ./arch/x86/kernel/vmlinux.lds:210: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:211: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:210: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:211: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:210: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:211: at least one side of the expression must be absolute

With LTO enabled, I additionally see:

ld.lld: error: ./arch/x86/kernel/vmlinux.lds:35: unable to move location counter backward for: .text
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:210: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:211: at least one side of the expression must be absolute
ld.lld: error: SRSO function pair won't alias
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:35: unable to move location counter backward for: .text
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:210: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:211: at least one side of the expression must be absolute
ld.lld: error: SRSO function pair won't alias
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:35: unable to move location counter backward for: .text
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:210: at least one side of the expression must be absolute
ld.lld: error: ./arch/x86/kernel/vmlinux.lds:211: at least one side of the expression must be absolute
ld.lld: error: SRSO function pair won't alias

The linker script is https://gist.github.com/nathanchance/1c57035608e9363b3c11a1d8a781c558, as the line numbers correspond to the preprocessed source.

Activity

  1. added
    [BUG] Untriaged
    [TOOL] lld
    on Aug 8, 2023
  2. nickdesaulniers commented on Aug 8, 2023

    @nickdesaulniers
    Member
  3. nickdesaulniers commented on Aug 8, 2023

    @nickdesaulniers
    Member

    Edit: looks like we need to keep a specific distance (of 0x104104 B) between these two symbols:

    $ llvm-readelf -s vmlinux | grep -e ' srso_safe_ret_alias' -e ' srso_untrain_ret_alias'
    132516: ffffffff82001fdb     6 FUNC    GLOBAL DEFAULT     1 srso_safe_ret_alias
    132991: ffffffff82000000    10 FUNC    GLOBAL DEFAULT     1 srso_untrain_ret_alias
    
  4. nickdesaulniers commented on Aug 8, 2023

    @nickdesaulniers
    Member

    Boris on IRC says:

    113499: ffffffff81d04104     6 FUNC    GLOBAL DEFAULT    1 srso_safe_ret_alias
    148347: ffffffff81c00000    10 FUNC    GLOBAL DEFAULT    1 srso_untrain_ret_alias
    

    is what's expected. I suspect that . = srso_untrain_ret_alias | (1 << 2) | (1 << 8) | (1 << 14) | (1 << 20); is trying to place srso_safe_ret_alias 0x104104 bytes after srso_untrain_ret_alias.

  5. nickdesaulniers commented on Aug 8, 2023

    @nickdesaulniers
    Member

    I think we can do the padding in assembler.

    Or rather than deal with symbols in the linker script, I think srso_safe_ret_alias is in its own section (.text.__x86.rethunk_safe) and so is srso_untrain_ret_alias (.text.__x86.rethunk_untrain). Maybe it's easier to specify alignment between these two sections in linkerscript.

  6. nickdesaulniers commented on Aug 9, 2023

    @nickdesaulniers
    Member

    I'm thinking this might work. 0x104104 is the magic constant ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)). I don't yet understand why 0x14 is necessary (some kind of function or symbol alignment, perhaps).

    diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
    index e76813230192..4715c7a4cc31 100644
    --- a/arch/x86/kernel/vmlinux.lds.S
    +++ b/arch/x86/kernel/vmlinux.lds.S
    @@ -146,15 +146,6 @@ SECTIONS
     #endif
     
                    ENTRY_TEXT
    -
    -#ifdef CONFIG_CPU_SRSO
    -               /*
    -                * See the comment above srso_untrain_ret_alias()'s
    -                * definition.
    -                */
    -               . = srso_untrain_ret_alias | (1 << 2) | (1 << 8) | (1 << 14) | (1 << 20);
    -               *(.text.__x86.rethunk_safe)
    -#endif
                    ALIGN_ENTRY_TEXT_END
                    *(.gnu.warning)
    
     @@ -532,9 +523,14 @@ INIT_PER_CPU(irq_stack_backing_store);
      * GNU ld cannot do XOR so do: (A | B) - (A & B) in order to compute the XOR
      * of the two function addresses:
      */
    +#ifdef CONFIG_LD_IS_BFD
     . = ASSERT(((srso_untrain_ret_alias | srso_safe_ret_alias) -
                    (srso_untrain_ret_alias & srso_safe_ret_alias)) == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
                    "SRSO function pair won't alias");
    +#else
    +. = ASSERT(srso_untrain_ret_alias ^ srso_safe_ret_alias == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
    +               "SRSO function pair won't alias");
    +#endif
     #endif
     
     #endif /* CONFIG_X86_64 */
    diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
    index 2cff585f22f2..f1ae299db60b 100644
    --- a/arch/x86/lib/retpoline.S
    +++ b/arch/x86/lib/retpoline.S
    @@ -157,8 +157,7 @@ SYM_START(srso_untrain_ret_alias, SYM_L_GLOBAL, SYM_A_NONE)
            jmp __x86_return_thunk
     SYM_FUNC_END(srso_untrain_ret_alias)
     __EXPORT_THUNK(srso_untrain_ret_alias)
    -
    -       .section .text.__x86.rethunk_safe
    +.fill (. - srso_untrain_ret_alias + 0x104104 - 0x14), 1, 0xcc
     #endif
     
     /* Needs a definition for the __x86_return_thunk alternative below. */

    The idea is rather than try to have the linker place one section relative to another, have the assembler place the symbols a specific distance apart.

  7. nickdesaulniers commented on Aug 9, 2023

    @nickdesaulniers
    Member

    #1907 (comment) works for me, boots, and is 0x104104 apart in both cases.

    Besides fixing those awful hex magic constants, another concern is this comment:
    arch/x86/lib/retpoline.S:139

    139  * - srso_untrain_ret_alias() is 2M aligned                                     
    

    uh, under defconfig maybe, but under CONFIG_LTO_CLANG_THIN=y I don't think so.

    $ llvm-readelf -s vmlinux | grep -e ' srso_safe_ret_alias' -e ' srso_untrain_ret_alias'
    159108: ffffffff81109444     6 FUNC    GLOBAL DEFAULT     1 srso_safe_ret_alias
    159475: ffffffff81005340    10 FUNC    GLOBAL DEFAULT     1 srso_untrain_ret_alias
    

    Also, SYM_START(srso_safe_ret_alias, SYM_L_GLOBAL, SYM_A_NONE) -> SYM_FUNC_START_NOALIGN(srso_safe_ret_alias)

    By grace of god that works for gcc 12.2 and binutils 2.40.

    @sinkap is reporting that maybe isn't working for older releases of lld though. I checked out release/11.x and built lld, and could reproduce:

    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:195: , expected, but got ^
    >>> . = ASSERT(srso_untrain_ret_alias ^ srso_safe_ret_alias == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
    >>>                                   
    

    I'll need to bisect lld to see when this starts working.

    Another problem with my approach is that I suspect the original patch is trying to keep srso_safe_ret_alias on one side of ENTRY_TEXT; if ENTRY_TEXT is larger than .text.__x86.rethunk_untrain + 0x104104 that seems like a problem.

  8. q66 commented on Aug 9, 2023

    @q66

    i get the same error on lld 16:

    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:190: , expected, but got ^
    >>> . = ASSERT(srso_untrain_ret_alias ^ srso_safe_ret_alias == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
    
    root@cbuild: ~$ ld.lld --version
    LLD 16.0.6 (compatible with GNU linkers)
    
  9. thyrc commented on Aug 9, 2023

    @thyrc
    Member

    Not sure if helps, but: The ^ and ^= operators where added in the release/17.x branch w/ llvm/llvm-project@fae9610.

    @nickdesaulniers: With your changes to retpoline.S do we really need to check for CONFIG_LD_IS_BFD? The original ASSERT() should still work, although with ABSOLUTE():

    diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
    index 8f06ab08f51e..d4c5040410de 100644
    --- a/arch/x86/kernel/vmlinux.lds.S
    +++ b/arch/x86/kernel/vmlinux.lds.S
    @@ -509,8 +509,8 @@ INIT_PER_CPU(irq_stack_backing_store);
      * GNU ld cannot do XOR so do: (A | B) - (A & B) in order to compute the XOR
      * of the two function addresses:
      */
    -. = ASSERT(((srso_untrain_ret_alias | srso_safe_ret_alias) -
    -		(srso_untrain_ret_alias & srso_safe_ret_alias)) == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
    +. = ASSERT(((ABSOLUTE(srso_untrain_ret_alias) | srso_safe_ret_alias) -
    +		(ABSOLUTE(srso_untrain_ret_alias) & srso_safe_ret_alias)) == ((1 << 2) | (1 << 8) | (1 << 14) | (1 << 20)),
     		"SRSO function pair won't alias");
     #endif
    

    I got

    $ llvm-readelf -s vmlinux |grep -e ' srso_safe_ret_alias' -e ' srso_untrain_ret_alias'
    129233: ffffffff8108ae60    10 FUNC    GLOBAL DEFAULT     1 srso_untrain_ret_alias
    135137: ffffffff8118ef64     6 FUNC    GLOBAL DEFAULT     1 srso_safe_ret_alias
    

    Edit: Insert diff

  10. q66 commented on Aug 9, 2023

    @q66

    there is also a large patchset overhauling this code: https://lore.kernel.org/lkml/[email protected]/

    besides other things this removes CONFIG_CPU_SRSO entirely, i wonder what effect it has on the build

  11. nickdesaulniers commented on Aug 9, 2023

    @nickdesaulniers
    Member

    @sinkap is reporting that maybe isn't working for older releases of lld though. I checked out release/11.x and built lld, and could reproduce:

    Not sure if helps, but: The ^ and ^= operators where added in the release/17.x branch w/ llvm/llvm-project@fae9610.

    Yes, thank you; you've saved me a bisection.

    @nickdesaulniers: With your changes to retpoline.S do we really need to check for CONFIG_LD_IS_BFD? The original ASSERT() should still work, although with ABSOLUTE():

    Ah, nice! That BFD condition is itself a workaround since (as the commit you linked to points out) BFD only gained xor support very very recently.

    there is also a large patchset overhauling this code: https://lore.kernel.org/lkml/[email protected]/

    Good find. I'll keep an eye on how that's progressing to see if we need something to land sooner than that.

  12. nathanchance commented on Aug 9, 2023

    @nathanchance
    MemberAuthor

    there is also a large patchset overhauling this code: https://lore.kernel.org/lkml/[email protected]/

    Good find. I'll keep an eye on how that's progressing to see if we need something to land sooner than that.

    I browsed that series and I don't see how this issue would disappear, the code in arch/x86/kernel/vmlinux.lds.S is largely untouched from what I can tell.

    For the record, our matrix will go super red, as stable released the patches at the same time as mainline.

  13. nathanchance commented on Aug 9, 2023

    @nathanchance
    MemberAuthor

    I was not able to apply that series with b4 shazam for some reason, patch 11 does not apply cleanly... but I was able to grab it from Peter's repo and yeah, this issue is still present:

    $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 defconfig vmlinux
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:203: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:204: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:203: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:204: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:203: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:204: at least one side of the expression must be absolute
    
    # Turn on CONFIG_LTO_CLANG_THIN in menuconfig
    $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 defconfig menuconfig vmlinux
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:35: unable to move location counter backward for: .text
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:203: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:204: at least one side of the expression must be absolute
    ld.lld: error: SRSO function pair won't alias
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:35: unable to move location counter backward for: .text
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:203: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:204: at least one side of the expression must be absolute
    ld.lld: error: SRSO function pair won't alias
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:35: unable to move location counter backward for: .text
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:203: at least one side of the expression must be absolute
    ld.lld: error: ./arch/x86/kernel/vmlinux.lds:204: at least one side of the expression must be absolute
    ld.lld: error: SRSO function pair won't alias
    
  14. 63 remaining items

  15. added a commit that references this issue on Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[BUG] linux[FIXED][LINUX] development cycle[TOOL] lld

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions