Stream: general

Topic: Accessing symbols from nasm


Luca Barbato (Oct 30 2019 at 08:10, on Zulip):

in rav1e we tried to define tables in rust and then use them from nasm, when not using cdylibs on linux everything seems fine, on linux+cdylib we got this:

  = note: /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /home/lu_zero/Sources/rust/rav1e/target/debug/build/rav1e-646b21e8f2fef3cb/out/librav1easm.a(ipred.o): warning: relocation against `rav1e_filter_intra_taps' in readonly section `.text'
          /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /home/lu_zero/Sources/rust/rav1e/target/debug/build/rav1e-646b21e8f2fef3cb/out/librav1easm.a(ipred.o): relocation R_X86_64_PC32 against symbol `rav1e_dr_intra_derivative' can not be used when making a shared object; recompile with -fPIC

anybody has ideas on how to fix it?

Luca Barbato (Oct 30 2019 at 08:29, on Zulip):

setting relocation-model=pic does doesn't change anything

Amanieu (Oct 30 2019 at 09:01, on Zulip):

Try using symbol@plt instead of symbol in your asm code.

Amanieu (Oct 30 2019 at 09:05, on Zulip):

Oh wait, that's a data table, not a function.

Amanieu (Oct 30 2019 at 09:09, on Zulip):

@Luca Barbato Apparently the correct syntax for obtaining the address of a PC-relative symbol is lea rax, [rip + symbol]. Can you try that to see if it works?

Luca Barbato (Oct 30 2019 at 09:18, on Zulip):

the code using it uses lea r7, [dr_intra_derivative] and I'd rather not change it since it is shared with another project ^^

Amanieu (Oct 30 2019 at 09:27, on Zulip):

@Luca Barbato Actually, can you double-check that the symbols that are referenced are from the same shared library?

Luca Barbato (Oct 30 2019 at 09:28, on Zulip):

let me explain the whole scenario

Amanieu (Oct 30 2019 at 09:28, on Zulip):

From the error message, the linker seems to think that rav1e_filter_intra_taps is a symbol defined outside the library.

Luca Barbato (Oct 30 2019 at 09:28, on Zulip):

we have a crate that uses a .a produced by nasm

Luca Barbato (Oct 30 2019 at 09:29, on Zulip):

and it references symbols that are actually defined in the rust code

Amanieu (Oct 30 2019 at 09:31, on Zulip):

Can you try adding #[used] to rav1e_dr_intra_derivative in tables.rs? It might have been optimized out as an unreachable private symbol.

Luca Barbato (Oct 30 2019 at 09:35, on Zulip):

when building non cdylib everything works

Luca Barbato (Oct 30 2019 at 09:35, on Zulip):

so seems unlikely

Luca Barbato (Oct 30 2019 at 09:35, on Zulip):

I should check the zulip mobile app =/

Luca Barbato (Oct 30 2019 at 09:35, on Zulip):

I must run

David Michael Barr (Oct 30 2019 at 09:37, on Zulip):

I'll follow up for Luca

David Michael Barr (Oct 30 2019 at 10:06, on Zulip):

So, #[used] gave the same error.
I did manage to work around by first building the static lib and then replacing the static nasm archive link argument with that.

David Michael Barr (Oct 30 2019 at 10:38, on Zulip):

The linker on macOS does not fail in this way. Testing must be done on Linux.

David Michael Barr (Oct 30 2019 at 10:38, on Zulip):

The underlying issue is that 2 static libs are mutually dependent.

David Michael Barr (Oct 30 2019 at 12:23, on Zulip):

Some linkers might require -lA -lB -lA

gnzlbg (Oct 30 2019 at 13:10, on Zulip):

@David Michael Barr do you have a self contained example that shows the issue? (a github repo or so?)

gnzlbg (Oct 30 2019 at 13:10, on Zulip):

I've used nasm on macosx and linux and called into it from Rust code, and I think I ran into similar issues

gnzlbg (Oct 30 2019 at 13:11, on Zulip):

I don't remember exactly how I solved them, just that I had to play tetris with linker and nasm options

gnzlbg (Oct 30 2019 at 13:11, on Zulip):

but I do recall that -fPIC error and using -fPIC not solving my problem :laughter_tears:

David Michael Barr (Oct 30 2019 at 13:13, on Zulip):

I think the fPIC error is actually a secondary effect. The symbol is not being resolved as intended, causing a mismatch with respect to PIC.

David Michael Barr (Oct 30 2019 at 13:17, on Zulip):

Non-trivial reproduction, assuming nasm installed:

git clone https://github.com/xiph/rav1e
cd rav1e
cargo install cargo-c
cargo cbuild --release
David Michael Barr (Oct 30 2019 at 13:19, on Zulip):

Non-trivial reproduction, assuming nasm installed:

git clone https://github.com/xiph/rav1e
cd rav1e
cargo install cargo-c
cargo cbuild --release
David Michael Barr (Oct 30 2019 at 14:33, on Zulip):

https://github.com/barrbrain/cargo-c-nasm-repro

David Michael Barr (Oct 30 2019 at 14:37, on Zulip):

I have reduced the problem to its essence.

Last update: Dec 12 2019 at 01:50UTC