Access a Variable Length C-Byte Array in a Different Section

To prevent some performance overhead I would like to append some binary data created by openssl to a Rust executable as an ELF section. While the data itself might have a variable length, its actual length may be parse from the header though.

I add the data with the following command:

objcopy --update-section .custom_section=output.bin target/debug/section-test

While I had success with static data, I failed to work with data of variable length. Is there some way to address this?

If you're just trying to include the file so your program can read them, you can use include_bytes rather than manually modifying the executable. The compiler will keep track of the number of bytes included.

Unfortunately this does not address my problem. The section I try to append is generated after the executable has been generated.

How do you access the data in the source code?

Apparently I missed to write down the initial idea in form of an example. I would like to work with the [link-section] attribute to access the data that is attached in the custom ELF section similar to this example

#[link_section = ".additional_data"]
static ADDITIONAL_DATA: <special type>;

fn main(){
  ...
  // do some computing with ADDITIONAL_DATA
  ...
}

Apparently llvm's objcopy does not support increasing/changing the size of a section: ⚙ D112116 [llvm-objcopy] Add --update-section

How does your special type look like? You might need to use UnsafeCell or static mut to avoid constant propagation.

Maybe something like this:

const MAX_SIZE: usize = 2048;

#[no_mangle]
#[used]
#[link_section=".additional_data"]
static mut STORAGE: [u8; MAX_SIZE] = [0; MAX_SIZE];

pub fn get_storage() -> &'static [u8] {
    unsafe {
        // SAFETY: never written to at runtime
        let actual_len = u16::from_ne_bytes(STORAGE[..2].try_into().unwrap()) as usize;
        &STORAGE[2..][..actual_len]
    }
}
1 Like

Good catch. So far I only used the GNU objcopy missing this little detail. I guess I need to work with a constant size for now.

Though I don't know how to make it happen, it should be possible to use partial linking to produce an object file of your program with an unresolved symbol referring to the OpenSSL data.

You can then use objcopy to pack up the data into a second object file that exports this symbol, and the linker can put the two together into a final executable without recompiling the program.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.