Skip to content

Document #[link_section = "..."] format#2236

Open
madsmtm wants to merge 3 commits intorust-lang:masterfrom
madsmtm:document-link-section-format
Open

Document #[link_section = "..."] format#2236
madsmtm wants to merge 3 commits intorust-lang:masterfrom
madsmtm:document-link-section-format

Conversation

@madsmtm
Copy link
Copy Markdown
Contributor

@madsmtm madsmtm commented Apr 19, 2026

The values accepted by the link_section attribute is platform-specific, it depends on the binary format.

On most platforms, it just forwards to the linker to do any special stuff with the name IIUC, but for Mach-O, it's a bit different, there LLVM parses the name and sets various flags depending on what the user specified.

For example, the following:

#[unsafe(link_section = "__DATA,my_cstring,cstring_literals,no_toc+no_dead_strip")]
static MY_CSTRING: [u8; 1] = *b"\0";

Results in a binary containing this data:

object::macho::Section64 {
    sectname: "__DATA",
    segname: "my_cstring",
    align: 1,
    reloff: 0,
    nreloc: 0,
    flags: object::macho::S_CSTRING_LITERALS | object::macho::S_ATTR_NO_TOC | object::macho::S_ATTR_NO_DEAD_STRIP,
    // ...
}

That is, the name used in #[link_section] on Mach-O is not inherent to the file format. Therefore, I think we should document what the expected format actually is!

(We should perhaps have had something like #[link_section(segment = "__DATA", segment = "my_cstring", type = "cstring_literals", attributes = ["no_dead_strip", "no_toc"])] on Mach-O instead, but that ship has probably sailed a while ago, I don't think it makes sense to change this).

I discovered this while working on rust-lang/rustc_codegen_cranelift#1648, it turns out that rustc_codegen_cranelift must parse these in the same manner as LLVM to support things like ctor.

Also closely related to rust-lang/rust#155065, it might make sense to expand that parsing to the entire string (to have better control over diagnostics / not be reliant on LLVM changing it).

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Apr 19, 2026
@madsmtm madsmtm force-pushed the document-link-section-format branch from 5ce38b4 to 2fa4226 Compare April 19, 2026 15:42
Comment thread src/abi.md
Comment on lines +124 to +142
r[abi.link_section.elf]
### Link section ELF format

TODO, maybe document `.hash` -> `SHT_HASH` etc.?

Or perhaps just link to <https://www.man7.org/linux/man-pages/man5/elf.5.html#:~:text=Various%20sections%20hold%20program%20and%20control%20information>?

r[abi.link_section.coff]
### Link section COFF format

TODO. Something like:

r[abi.link_section.coff.syntax]
```grammar,attributes
COFFLinkSection -> IDENTIFIER (`$` COFFOrder)?
COFFOrder -> IDENTIFIER
```

See <https://learn.microsoft.com/en-us/windows/win32/debug/pe-format>.
Copy link
Copy Markdown
Contributor Author

@madsmtm madsmtm Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fill these in properly if you think it makes sense to document this in the reference too?

View changes since the review

@madsmtm madsmtm force-pushed the document-link-section-format branch from 2fa4226 to 8446a19 Compare April 19, 2026 15:45
Comment thread src/abi.md

MachOSection -> <1 to 16 bytes>

MachOSectionType -> `regular` | `zerofill` | `cstring_literals` | `4byte_literals` | `8byte_literals` | `literal_pointers` | `non_lazy_symbol_pointers` | `lazy_symbol_pointers` | `mod_init_funcs` | `mod_term_funcs` | `coalesced` | `interposing` | `16byte_literals` | `thread_local_regular` | `thread_local_zerofill` | `thread_local_variables` | `thread_local_variable_pointers` | `thread_local_init_function_pointers`
Copy link
Copy Markdown
Contributor Author

@madsmtm madsmtm Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave out the exact names here if you prefer? Though I tend to think it makes sense to have, for example mod_init_funcs is slightly different from what the attribute is documented as in the header (S_MOD_INIT_FUNC_POINTERS).

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants