1

I have a set of static objects in my library that I need access to from my library_main() in the example below. In C, I can put it in an ELF section, get the start and end address of the section and use pointer math to walk through the objects. Is there an idiomatic way of doing that in Rust?

Is there either a C-like ELF approach or any other recommended approach to this where I can just get a list / array of static objects in my lib?

fn foo() {
    #[link_section = ".early_init"]
    static FOO: usize = 10;
}

fn bar() {
    #[link_section = ".early_init"]
    static BAR: usize = 10;
}

fn library_main() {
    // I need to access variables FOO and BAR here and do
    // some initializations with them before I do anything else
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
gopa
  • 73
  • 8
  • You confuse C with a language I don't know. Don't mix implemented extra feature and C17 language please when you use compiler feature, precise it. – Stargateur Dec 18 '19 at 00:01
  • When I was talking about C, I wasnt talking about any "compiler feature" of C - its simply a mechanism to put objects in a certain elf section, and some tricks to get the start and end address of the section. – gopa Dec 18 '19 at 00:16
  • Your question might be answered by the answers of [How can I statically register structures at compile time?](https://stackoverflow.com/a/57419729/155423) / [How to create a vector of all decorated functions from a specific module?](https://stackoverflow.com/a/57420135/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 18 '19 at 00:51
  • Thanks for the response, yes the inventory crate does look like what I wanted, although there is currently a limitation that inventory::submit!() cannot be called from inside a function (which is what I want) - the documentation clearly states it wont compile and it doesnt because the impl::submit() is a procedural macro and rust complains "procedural macros cannot be expanded to statement" .. But at any rate, I think looking through the source for the inventory crate, I understand what is the mechanism, so thats good enough. Thanks again for the response, this question can be marked answered – gopa Dec 18 '19 at 13:16

0 Answers0