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

pub static disappears if only used from asm #13365

Closed
kmcallister opened this Issue Apr 6, 2014 · 5 comments

Comments

Projects
None yet
7 participants
@kmcallister
Copy link
Contributor

kmcallister commented Apr 6, 2014

#[feature(asm)];

#[no_mangle]
pub static arr: [u8, ..16] = [0, ..16];

fn main() {
    unsafe {
        asm!("movups arr, %xmm0" ::: "xmm0");
    }
}
$ rustc -v
rustc 0.10-pre (68a4f7d 2014-02-24 12:42:02 -0800)
host: x86_64-unknown-linux-gnu

$ rustc -O foo.rs
error: linking with `cc` failed: exit code: 1
note: cc arguments: [...]
note: foo.o: In function `main::hcaa4d83fe49d03ddnaa::v0.0':
foo.rs:(.text+0x29): undefined reference to `arr'
collect2: error: ld returned 1 exit status

error: aborting due to previous error

Adding an unused constraint "r"(&arr) fixes it, as does passing &arr to test::black_box (which does basically that).

@luqmana luqmana added I-wrong and removed I-enhancement labels Apr 7, 2014

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Apr 20, 2015

Traige: still true

@Stebalien

This comment has been minimized.

Copy link
Contributor

Stebalien commented Sep 16, 2015

Still an issue. Updated example:

#![feature(asm)]

#[no_mangle]
pub static arr: [u8; 16] = [0; 16];

fn main() {
    unsafe {
        asm!("movups arr, %xmm0" ::: "xmm0");
    }
}
@brson

This comment has been minimized.

Copy link
Contributor

brson commented Mar 9, 2017

Error is different now:

rustc 1.17.0-nightly (b1e31766d 2017-03-03)
warning: static variable `arr` should have an upper case name such as `ARR`
 --> <anon>:4:1
  |
4 | pub static arr: [u8; 16] = [0; 16];
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(non_upper_case_globals)] on by default

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib" "./out.0.o" "-o" "./out" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-9a66b6a343d52844.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-6bc49e032a89c77d.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-a2a467c3ca3b6479.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_unicode-e54225ff8f33e08f.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-9d79f761aa668a33.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-2beb731af7a6faec.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-ce7b9706e1719f27.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-4b74d6f2808677d3.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-95af4192ed69a1c8.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-cd0ca85e71f914ca.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-0bf24067248742a8.rlib" "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util"
  = note: /usr/bin/ld: ./out.0.o: relocation R_X86_64_32S against `arr' can not be used when making a shared object; recompile with -fPIC
          ./out.0.o: error adding symbols: Bad value
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error
@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Mar 9, 2017

The correct way to do this is to pass the array as a constrained variable (per @nagisa, who can provide more details) so that the compiler knows about the fact that arr is used (I imagine the fact that it is stripped is related to building a binary, not a library, as well). Closing.

@nagisa

This comment has been minimized.

Copy link
Contributor

nagisa commented Mar 9, 2017

Namely the code should be looking something like this:

asm!("movups $0, %xmm0" : "something"(arr) :: "xmm0")

instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.