Created
January 14, 2024 20:15
-
-
Save thejpster/b162d197cf5c973a2592940dc454cafa to your computer and use it in GitHub Desktop.
Some relocatable rust code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Use this linker script: | |
```text | |
SECTIONS { | |
. = 0x10000000; | |
Flash : { | |
KEEP(*(.entry)) | |
KEEP(*(.entry.*)) | |
*(.text) | |
*(.text.*) | |
*(.rodata) | |
*(.rodata.*) | |
. = ALIGN(4); | |
} | |
Ram : { | |
*(.data) | |
*(.data.*) | |
*(.sdata) | |
*(.sdata.*) | |
*(.bss) | |
*(.bss.*) | |
*(.uninit) | |
*(.uninit.*) | |
. = ALIGN(4); | |
} | |
/DISCARD/ : { | |
/* Unused exception related info that only wastes space */ | |
*(.ARM.exidx); | |
*(.ARM.exidx.*); | |
*(.ARM.extab.*); | |
} | |
} | |
``` | |
And build with this command: | |
rustc -o reloc -C opt-level=3 reloc.rs --crate-type=bin -C relocation-model=ropi-rwpi --target=thumbv6m-none-eabi -C link-arg=-Tlink.x | |
*/ | |
#![no_std] | |
#![no_main] | |
use core::sync::atomic; | |
static GLOBAL_VALUE: atomic::AtomicI32 = atomic::AtomicI32::new(5); | |
static GLOBAL_VALUE2: atomic::AtomicI32 = atomic::AtomicI32::new(0); | |
#[inline(never)] | |
#[no_mangle] | |
#[link_section = ".entry"] | |
pub fn reloc_get() -> i32 { | |
GLOBAL_VALUE.load(atomic::Ordering::Relaxed) + GLOBAL_VALUE2.load(atomic::Ordering::Relaxed) | |
} | |
#[link_section = ".entry"] | |
#[no_mangle] | |
pub fn reloc_add(a: i32) -> i32 { | |
let x = reloc_get(); | |
a.wrapping_add(x) | |
} | |
#[link_section = ".entry"] | |
#[no_mangle] | |
pub fn reloc_update(foo: i32) { | |
GLOBAL_VALUE.store(foo, atomic::Ordering::Relaxed); | |
GLOBAL_VALUE2.store(foo, atomic::Ordering::Relaxed); | |
} | |
#[panic_handler] | |
fn panic_handler(_info: &core::panic::PanicInfo) -> ! { | |
loop {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment