#[link_section] in libraries discarded

It seems variables marked with #[link_section] are not placed in the final binary, per this example:

lib.rs:

#![no_std]

#[used]
#[link_section = ".vector.reset"]
static RESET: Option<extern "C" fn() -> !> = None;

#[used]
#[link_section = ".vector.exceptions"]
static EXCEPTIONS: [Option<extern "C" fn()>; 14] = [
    None, // NMI
    None, // HardFault
    None,
    None,
    None,
    None,
    None,
    None,
    None,
    None, // PendSV
    None, // SysTick
    None,
    None,
    None,
];

bin.rs:

#![no_std]
#![no_main]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

linker.ld:

MEMORY {
    rom : ORIGIN = 0x00000000, LENGTH = 128K
}

SECTIONS {
    .vector : {
        WORD(0x00000000)

        KEEP(*(.vector.reset))
        KEEP(*(.vector.exceptions))
    } > rom
}

Provides the following image dump, where .vector is missing:

architecture: arm, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x00000000

Program Header:
    PHDR off    0x00000034 vaddr 0xfffff034 paddr 0xfffff034 align 2**2
        filesz 0x00000060 memsz 0x00000060 flags r--
    LOAD off    0x00000000 vaddr 0xfffff000 paddr 0xfffff000 align 2**12
        filesz 0x00000094 memsz 0x00000094 flags r-x
STACK off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**0
        filesz 0x00000000 memsz 0x00000000 flags rw-
private flags = 5000200: [Version5 EABI] [soft-float ABI]

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
0 .debug_str    000004d1  00000000  00000000  00000094  2**0
                CONTENTS, READONLY, DEBUGGING
1 .debug_abbrev 000000fd  00000000  00000000  00000565  2**0
                CONTENTS, READONLY, DEBUGGING
2 .debug_info   00000611  00000000  00000000  00000662  2**0
                CONTENTS, READONLY, DEBUGGING
3 .debug_macinfo 00000001  00000000  00000000  00000c73  2**0
                CONTENTS, READONLY, DEBUGGING
4 .debug_pubnames 0000009a  00000000  00000000  00000c74  2**0
                CONTENTS, READONLY, DEBUGGING
5 .debug_pubtypes 00000385  00000000  00000000  00000d0e  2**0
                CONTENTS, READONLY, DEBUGGING
6 .ARM.attributes 00000032  00000000  00000000  00001093  2**0
                CONTENTS, READONLY
7 .debug_frame  00000028  00000000  00000000  000010c8  2**2
                CONTENTS, READONLY, DEBUGGING
8 .debug_line   0000004b  00000000  00000000  000010f0  2**0
                CONTENTS, READONLY, DEBUGGING
9 .comment      00000012  00000000  00000000  0000113b  2**0
                CONTENTS, READONLY
SYMBOL TABLE:
00000000 l    df *ABS*  00000000 o0jzeh1sn11gu9v

The sections are there in the archive.

Note that this also doesn't work:

bin.rs:

#![no_std]
#![no_main]

#[link_section = ".vector.reset"]
pub use thumbv6m::RESET;

#[link_section = ".vector.exceptions"]
pub use thumbv6m::EXCEPTIONS;

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

Am I missing something here? How else can I place data in a linker section from within a library?

I have had problems with this as well, have not found a workaround. This is tracked in rust-lang/rust#47384.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.