-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Place &str in specific linker sections #70239
Comments
This feature is also used when we need to share text section between contexts. If the function called in custom section is still in original |
At least as of recent compiler versions, I've come up with a sort of hacky way to achieve this (credit also to @cr1901 who solved the first half). TL;DR, you can define the str value as a I'm using it here with include bytes, but you could probably do something similar with strings? Here, on the playground (same as below): const EXAMPLE: &[u8] = include_bytes!("/proc/cpuinfo");
const EX_LEN: usize = EXAMPLE.len();
static BUF: [u8; EX_LEN] = {
let mut arr = [0u8; EX_LEN];
let mut idx = 0;
while idx < EX_LEN {
arr[idx] = EXAMPLE[idx];
idx += 1;
}
arr
};
fn main() {
println!("{}", BUF.len());
} |
In the usecase I am working on I want to store the string from
core::any::type_name
in a specific linker section to create an instrumentation tool for Embedded Rust micro controllers.If the string is placed in
.rodata
it does not affect the tool, but when having only a few kB of memory storing these strings in the actual micro controller is a huge price to pay.Rather only the generated ELF file, which the host can read, should have this info (i.e. in an
INFO
marked section).As it is today one can place variables in specific sections as:
However, this is not possible for
&str
, the following:Will place the string itself in
.rodata
, while placing the pointer and length in the section.my_section
.The recommended solution is to place the string in an array using something similar to the following:
However this does not work on strings given from (non-const-fn) functions such as
core::any::type_name
, and the function take no arguments so the string must be coming from a literal somewhere.Is there a way to make the strings from
core::any::type_name
(or similar functions) be placed in a specific linker section?As the string is being placed in
.rodata
something is controlling this placement, I simply wish to change this.The method stated above works in nightly, as one can use the
#![feature(const_type_name)]
feature to get the string as a const-fn. However I am unable to reproduce this in any way using stable rust.Example playground of the nightly version: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d5f5c7fbe90194640bd1918cc068c2db
The text was updated successfully, but these errors were encountered: