Skip to content

Failed linking to a library that uses inline assembly with asm_sym feature #104925

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

Closed
ly0va opened this issue Nov 26, 2022 · 0 comments · Fixed by #105023
Closed

Failed linking to a library that uses inline assembly with asm_sym feature #104925

ly0va opened this issue Nov 26, 2022 · 0 comments · Fixed by #105023
Labels
C-bug Category: This is a bug.

Comments

@ly0va
Copy link

ly0va commented Nov 26, 2022

What does not work

I tried this code:

// lib.rs
static EIGHTEEN: usize = 18;

#[inline(always)]
pub fn add_to_18(x: usize) -> usize {
    let mut y = x;

    unsafe {
        core::arch::asm!(
            "add {y}, [rip + {eighteen}]",
            y = inout(reg) y,
            eighteen = sym EIGHTEEN,
        );
    }

    y
}
// main.rs
fn main() {
    println!("5 + 18 = {}", my_lib::add_to_18(5));
}

I expected to see this happen: output 5 + 18 = 23

Instead, this happened:

   Compiling my_lib v0.1.0 (/home/user/my_lib)
error: linking with `clang` failed: exit status: 1
  <...omitted...>
  = note: mold: error: undefined symbol: my_lib::EIGHTEEN::he8c23468c01db196
          >>> referenced by 1551ilekjbvb99xo

What works

  1. #[no_mangle]:
// lib.rs
#[no_mangle]
static EIGHTEEN: usize = 18;

#[inline(always)]
pub fn add_to_18(x: usize) -> usize {
    let mut y = x;

    unsafe {
        core::arch::asm!(
            "add {y}, [rip + {eighteen}]",
            y = inout(reg) y,
            eighteen = sym EIGHTEEN,
        );
    }

    y
}
  1. #[inline(never)]:
// lib.rs
static EIGHTEEN: usize = 18;

#[inline(never)]
pub fn add_to_18(x: usize) -> usize {
    let mut y = x;

    unsafe {
        core::arch::asm!(
            "add {y}, [rip + {eighteen}]",
            y = inout(reg) y,
            eighteen = sym EIGHTEEN,
        );
    }

    y
}
  1. pub static:
// lib.rs
pub static EIGHTEEN: usize = 18;

#[inline(always)]
pub fn add_to_18(x: usize) -> usize {
    let mut y = x;

    unsafe {
        core::arch::asm!(
            "add {y}, [rip + {eighteen}]",
            y = inout(reg) y,
            eighteen = sym EIGHTEEN,
        );
    }

    y
}

Meta

rustc --version --verbose:

rustc 1.67.0-nightly (a28f3c88e 2022-11-20)
binary: rustc
commit-hash: a28f3c88e50a77bc2a91889241248c4543854e61
commit-date: 2022-11-20
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

mold --version:

mold 1.6.0 (323ad30e25c2c81efdb07ab76601a119335a40c8; compatible with GNU ld)

Also tried with ld linker, got the same error.

@ly0va ly0va added the C-bug Category: This is a bug. label Nov 26, 2022
@tmiasko tmiasko self-assigned this Nov 28, 2022
@tmiasko tmiasko removed their assignment Nov 28, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 29, 2022
…r=wesleywiser

Statics used in reachable function's inline asm are reachable

Fixes rust-lang#104925.
@bors bors closed this as completed in 581ca3e Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants