Are `link_section` values that start with dot not allowed?

I've been experimenting and noticed that while this works and results in ELF section when applied to a static:

#[used]
#[unsafe(link_section = "TEST")]

This does not, corresponding section is simply missing in the binary:

#[used]
#[unsafe(link_section = ".TEST")]

It is possible to add with inline assembly, but not like ^ directly.

I have not been able to find any Rust issues about this or anything on the forum or elsewhere that would explain this behavior, there was also no warnings or anything like that generated by the compiler.

Does anyone know why this is happening?

FWIW, I've used a dot section here with success:

Interesting, I just added this to a test file and its compiled binary doesn't have .TEST section :thinking:

#[used]
#[unsafe(link_section = ".TEST")]
static TEST: u8 = 0;

#[used] only prevents the compiler from throwing away the static. It doesn't prevent the linker from throwing it away if it sees that it is unused. You need the unstable #[used(linker)] for preventing the linker from throwing it away or you need to pass an argument to the linker that tells it to keep it. The latter option depends on which linker is used.

2 Likes

Very interesting, #[used(linker)] worked both with default linker and with mold on Linux.

I guess this is because sections starting with . are system sections, so linker removes anything it do not recognize, but those that do not start with . are application-specific and thus preserved.

I learned something today, thank you!