RRON
RRON

Reputation: 1115

How to declare and access extern symbols in rust?

I am trying to understand all the pieces in declaring and accessing extern symbols in RUST. Here is an example.

extern {
      static foreign_symbol: ui32;
}

Keyword extern is to say linker that the symbol is outside of this binary.
Keyword static is for lifetime to last forever.
The type ui32, should match the definition from foreign source.

Here are the questions:
a. What about the mutability of this external variable?
b. How to initialize this extern variable?
c. Most of the extern usage sample code have extern -C. What is the difference between using extern & extern -C ?
d. I need to access my risc-v assembly function from rust code. What is the calling convention for this?

Upvotes: 1

Views: 1485

Answers (1)

RRON
RRON

Reputation: 1115

Here I'm answering my own question after reading & trying out.

a. extern variables aren't mutable by default unless marked as mutable in declaration.

b. extern variables can't be initialized and can't even have a body.

  extern "C" {
        static  _stack_start: u32 = 20;
    }

It throws the following error -

extern` blocks define existing foreign statics and statics inside of them cannot have a body

c. extern C adopts C calling convention vs extern can be one of "systems" or "C" etc (https://doc.rust-lang.org/reference/items/external-blocks.html )

d. Couldn't find the answer yet. But as of Aug, 2020 it was unspecified (https://users.rust-lang.org/t/where-can-i-find-the-rust-calling-convention-for-risc-v/47573)

Upvotes: 2

Related Questions