undefined reference to `__sheap'
huntc opened this issue · comments
When linking my library with the following:
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE_BYTES) }
I receive a:
undefined reference to `__sheap'
I didn't find a documented means by which this symbol is declared. Any pointers? I'm linking my Rust library to an nRF52 C based project. Thanks.
Do you have this line on your .cargo/config
?
__sheap
is defined in the linker script here; if you're not getting it it probably means the linker script is not being included. Regardless of gcc or lld linker, you need to pass -Tlink.x
somehow or other, which is what that line in .cargo/config
does. If you have a RUSTFLAGS environment variable set, it will override .cargo/config
, or perhaps you're specifying another linker script somehow?
I have little experience with such C/Rust integrations, but I'd assume you're in a world of pain if you try to use cortex-m-rt and have some C-based runtime. cortex-m-rt will set up the vector table, do all the setup needed before entering main
(copying/zeroing memory), etc. Which is all stuff that your C runtime presumable already does.
It's required for Rust applications. For a Rust library you only need the cortex-m crate, which will provide you some APIs, but won't interfere with another program that calls into your Rust code.
But I suppose @huntc would still have to define a global allocator if the Rust library depends on it. Is there some allocator one could use that uses malloc
and free
provided by the C runtime? Otherwise you would have to define 2 separate heaps, which is probably not desirable.
Thanks for the replies, everyone. I'll keep this issue open as I'd like to ultimately raise a PR to improve the doc around calling static libs in an embedded context.
Meanwhile, I did provide my own allocator and things appear to work (although I'm having difficulty getting tests to work). Here's the gist of it - happy for feedback on the gist as I'm a Rust newbie:
https://gist.github.com/huntc/ab9a505683647aac7bccd2df0fc75f9e)
Thanks!
I'm closing this issue while doing some backlog triage; please feel free to re-open if you do get back to documenting calling static libs in an embedded context, that would definitely be useful! Perhaps it could go into the Rust with C chapter of the book?