Rust-lld, how to troubleshoot 'section' ... 'will not fit in region' errors?

⚓ Rust    📅 2026-06-27    👤 surdeus    👁️ 1      

surdeus

Hello

I have been struggling to find information on how to troubleshoot rust-ldd errors with a message like this:

  = note: rust-lld: error: section '.text' will not fit in region 'FLASH': overflowed by 123696 bytes
          rust-lld: error: section '.rodata' will not fit in region 'FLASH': overflowed by 125724 bytes
          rust-lld: error: section '.data' will not fit in region 'FLASH': overflowed by 125780 bytes
          rust-lld: error: section '.gnu.sgstubs' will not fit in region 'FLASH': overflowed by 125792 bytes

I tried using:

cargo objdump -- -D > /tmp/disassembly 

but this fails with the same error as above, and I am not sure if this would lead me to understand what the issue might be.

Below is the description of the specific issue.

(On AHB SMRAM2, STM32H7) Using a raw pointer to an array of raw pointers I define a structure like this:

[*mut &str, ... , *mut &str], &str, .... &str
 |                            |
 0x30004000 (AHBSRAM2)        0x30004800 (First addr. after pointers to messages)

Concrete:

    // Pointer to array containg poiners to messages.
    let pointers_msg = AHB_SRAM2_FIRST as *mut [*mut &str; N_MSG_MAX];
    // Pointer to default message after the space intended for pointers_msg.
    let msg_default = unsafe { pointers_msg.offset(1) }
        as *mut [u8; MSG_DEFAULT.len()]
        as *mut &str;
    // Writing default message into its place. 
    unsafe { core::ptr::write_volatile(msg_default, MSG_DEFAULT) };
    // Setting all pointers to the location of the default message.
    unsafe { core::ptr::write_volatile(pointers_msg, [msg_default; N_MSG_MAX]) };

All positions in the array now point to the default message. Now I copy a new message to memory, placed after the default message, and point to it from the first position (0) of the pointer array (pointers_msg).

    // ERROR
    let msg_error = unsafe { msg_default.offset(1) }
        as *mut [u8; ERROR.len()]
        as *mut &str;
    unsafe { core::ptr::write_volatile(msg_error, ERROR) };
    unsafe { core::ptr::write_volatile( &mut (*pointers_msg)[0], msg_error); }

In the same init() function, i test if I can access the messages:

defmt::println!("{}\n{}\n{}\n{}\n{}",
        unsafe { pointers_msg.offset(1) },
        unsafe { *(*pointers_msg)[0] },
        unsafe { *(*pointers_msg)[1] },
        unsafe { *(*pointers_msg)[2] },
        unsafe { *(*pointers_msg)[3] },
        unsafe { *(*pointers_msg)[4] },
    );

This prints as expect:

0x30004800
Error
Denied
BankConfig can only be created once.
Maximum number of banks reached.
Bank overlaps with previous one.

A function to access a string using a matching pointers_msg. This way, I can access any message as long as i know what position in the array points to it (would improove this with an enum). If the initalisation was not run, I do that now:

pub fn get_msg(n: usize) -> &'static str {
    if unsafe { (&raw mut INIT).read() } {
        // Pointer to array containg poiners to messages.
        let pointers_msg = AHB_SRAM2_FIRST as *mut [*mut &str; N_MSG_MAX];
        unsafe { *(*pointers_msg)[n] }
    } else {
        init();
        get_msg(n)
    }
}

And it works:

defmt::println!("get_msg(4): {}", msg::get_msg(4));
get_msg(4): Bank overlaps with previous one.

Or does it? Calling the above function from a struct method, i get the rust-lld error shown on top of this page. Here is a bit of context:

pub fn add_bank(&mut self, addr: u32, size: u32)
            -> Result<(), crate::err::E<()>>
    {
        defmt::println!("{}", crate::msg::get_msg(4))
...

I am stuck here.
Things i tried:

  • cargo clean.
  • Commenting the "-C", "linker=flip-link", line in .cargo/config.toml.
  • Trying opt-level = 's' instead of opt-level = 3 in the dev profile in Cargo.toml.
  • Having get_msg() returning the pointer rather than &str (Getting a bit desperate).
  • Removing the INIT flag handling in the init() and/or get_msg() functions, which had some success, but I can't life without the INIT check and seemed too random to be a reliable "solution".
  • Searching online. I thought this could be an issue with how I use raw pointers, but other than better ways to access mutable statics, nothing meaningful to me came up. Same when searching for the error message directly. I have the impression that what I am seeing is not a common occurrence.

3 posts - 3 participants

Read full topic

🏷️ Rust_feed