Skip to content

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

@ly0va

Description

@ly0va

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.

Activity

self-assigned this
on Nov 28, 2022
removed their assignment
on Nov 28, 2022
added 2 commits that reference this issue on Nov 29, 2022
1505fc0
581ca3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ly0va@tmiasko

      Issue actions

        Failed linking to a library that uses inline assembly with `asm_sym` feature · Issue #104925 · rust-lang/rust