Document #[link_section = "..."] format#2236
Open
madsmtm wants to merge 3 commits intorust-lang:masterfrom
Open
Document #[link_section = "..."] format#2236madsmtm wants to merge 3 commits intorust-lang:masterfrom
#[link_section = "..."] format#2236madsmtm wants to merge 3 commits intorust-lang:masterfrom
Conversation
5ce38b4 to
2fa4226
Compare
madsmtm
commented
Apr 19, 2026
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>. |
Contributor
Author
There was a problem hiding this comment.
I'll fill these in properly if you think it makes sense to document this in the reference too?
2fa4226 to
8446a19
Compare
madsmtm
commented
Apr 19, 2026
|
|
||
| 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` |
Contributor
Author
There was a problem hiding this comment.
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).
This was referenced Apr 20, 2026
madsmtm
commented
Apr 27, 2026
| r[abi.link_section.mach-o] | ||
| ### Link section Mach-O format | ||
|
|
||
| <!-- Parsing done by LLVM in https://github.com/llvm/llvm-project/blob/llvmorg-22.1.3/llvm/lib/MC/MCSectionMachO.cpp --> |
Contributor
Author
There was a problem hiding this comment.
I checked what GCC does here, it seems to pass the section onwards to the assembler unchanged (i.e. forwards the problem to the assembler). On most GCC installations on macOS, the default assembler is LLVM's (so it supports the same syntax as documented here).
A few other assemblers:
- Apple's old, deleted assembler
cctools/assupported a subset of these in the same format: https://github.com/apple-oss-distributions/cctools/blob/cctools-1010.6/as/read.c#L232-L279 - NASM has it's own (more limited) syntax: https://github.com/netwide-assembler/nasm/blob/aa12d3381a1fd08e72f594147f2486c1b5ab7813/output/outmacho.c#L739-L748 (though they still use the same comma-separated format for segments and sections)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The values accepted by the
link_sectionattribute 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:
Results in a binary containing this data:
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_craneliftmust parse these in the same manner as LLVM to support things likector.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).
An alternative here would be to:
I'd be fine with that too, if you think that's better?