rust-lang / rust-lang/rust

The `.end` assembly directive in `global_asm!` with fat LTO causes any subsequent `global_asm!` not to assemble

Open
#148,503 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-global-asm C-gub
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

TLDR:
Adding .end directive inside a global_asm!() code with fat LTO enabled prevents the assembly of any other global_asm!() blocks even in other crates.

Specific example for risc-v no_std (note, the code is artificial example, it is not supposed to run or do anything)

Crate startup:

Cargo.toml
[package]
name = "startup"
version = "0.1.0"
edition = "2024"

[dependencies]
src/lib.rs
#![no_std]

unsafe extern "C" {
    unsafe fn main() -> !;
}

core::arch::global_asm!(r#"

.section .text.init, "ax", @progbits
.balign  4
.globl _start
.type _start, @function
_start:
    /* do stuff here */
    tail {main}
"#
, main = sym main);

Main crate:

Cargo.toml
[package]
name = "main_bin"
version = "0.1.0"
edition = "2024"

[dependencies]
startup = {path="../startup"} 

[profile.release]
lto="fat"
src/main.rs
#![no_std]
#![no_main]

extern crate startup;
//extern crate header;

core::arch::global_asm!(r#"
   .text
    asm_fun0: 
        j .
    .end
"#);

unsafe extern "C" {
    unsafe fn asm_fun0();
}

#[used]
static _ASM_FUN0: unsafe extern "C" fn() = asm_fun0;

#[unsafe(no_mangle)]
extern "C" fn main() {
    loop {}
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}
.cargo/config.toml
[build]
target = "riscv32imc-unknown-none-elf"

[target.riscv32imc-unknown-none-elf]
rustflags = ["-C", "link-args=-Tlink.ld -Map=fmc.map"]
Linker script (link.ld)
OUTPUT_ARCH("riscv")
ENTRY(_start)

MEMORY
{
  ram (rwx) : ORIGIN = 0x1000, LENGTH = 0x10000
}

SECTIONS {
    .text : {
        _sinit = .;
        KEEP(*(.text.init))
        KEEP(*(.text.init.*))
        _einit = .;
        *(.text .text.*)
    } > ram

    ASSERT(_sinit != _einit,
     "init section is empty!")

   .rodata :
    {
        * (.rodata .rodata.*)
        * (.srodata .srodata.*)
    } > ram

    .data :
    {
        * (.sdata .sdata.*)
         * (.data .data.*)
    } > ram

    .bss (NOLOAD) :
    {
        *(.bss .bss.* .sbss .sbss.*)
    } > ram

    .stack ALIGN(16) (NOLOAD) :
    {
        . = . + 0x1000;
    } > ram
}

Building as is with

cargo build --release

gives the error from the linker script assertion:

  = note: rust-lld: error: init section is empty!

Indicating that the whole assembly code from startup is not there.
Removing the .end from main.rs above builds successfully. Disabling LTO or making it "thin" also eliminates the issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided startup and main_bin crates with cargo build --release, using the shown RISC-V target and fat LTO, then compare builds with and without .end in src/main.rs. Trace how the global_asm! blocks are handled across crates; done means the startup assembly remains present and the linker assertion no longer fails with .end and fat LTO enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.