Hi, newbie here. I'm trying to copy included bytes (from include_bytes! on a binary) to a specific memory address. Right now, I am using ptr::copy_nonoverlapping, but the data I have is significant (20 Mb).
Is ptr::copy_nonoverlapping the fastest way to do this? Is there some way to directly include the bytes to the desired memory address? Thanks!
Write a linker script that instructs the linker to assign DATA this address. (I haven't used linker scripts myself, but I know they are used for this purpose.)
It's necessary for the linker script to be able to name the symbol.
That would make the static be a reference, not the data itself, which would defeat the point of getting the linker to set its address. Please note that I was answering the second part of @ScarfEater’s question: “Is there some way to directly include the bytes to the desired memory address?” That is, to have the data being at that address be part of the binary, rather than the data needing to be copied by the program after having been loaded at a different address.
Hey, thanks for the response. I had a few more questions (sorry for the basic questions, I started with Rust literally today haha).
Under the hood, what exactly is this approach doing? I should clarify my use case - I am trying to load a binary to DRAM and the binary contains a linux kernel (unsure if this affects the level of protection or anything). What exactly does declaring a static and the linker script do? Does it actually copy any bytes to the destination address? If not, what does it move instead?
All code and data of your program has to be copied (or mapped) into memory. You're just adding the extra request that this specific data needs to be at a specific address.
You can give instructions to the linker that a specific symbol (such as a static item) should be given a specific address rather than “whatever the next free address is”. The loader (the thing that runs before your program does, to load it into memory) then follows the instructions left by the linker — or perhaps the linker creates a raw memory image where addresses equal offsets in the file, and the loader consists of just copying everything. Either way may be used, depending on what exactly is loading your program.
(Or when non-volatile (flash) program memory is used, such as with microcontrollers, instead of a loader that runs on boot, you have a step where the binary produced by the linker is copied to the microcontroller's memory and it stays there until overwritten. That's the same function as a loader, just running only when the program is changed rather than every time it starts.)