Skip to content

naked functions: respect function-sections#147811

Open
folkertdev wants to merge 8 commits intorust-lang:mainfrom
folkertdev:naked-function-sections
Open

naked functions: respect function-sections#147811
folkertdev wants to merge 8 commits intorust-lang:mainfrom
folkertdev:naked-function-sections

Conversation

@folkertdev
Copy link
Copy Markdown
Contributor

@folkertdev folkertdev commented Oct 17, 2025

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Oct 17, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 17, 2025
Comment thread tests/codegen-llvm/naked-fn/dead-code-elimination.rs Outdated
@folkertdev folkertdev changed the title Naked function sections naked functions: respect function-sections Oct 17, 2025
Comment thread compiler/rustc_codegen_ssa/src/mir/naked_asm.rs Outdated
@folkertdev folkertdev force-pushed the naked-function-sections branch from 2ebd197 to ac4b179 Compare October 17, 2025 14:39
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Oct 17, 2025
Comment thread compiler/rustc_codegen_ssa/src/mir/naked_asm.rs Outdated
@folkertdev folkertdev force-pushed the naked-function-sections branch from ac4b179 to 6580a5f Compare October 17, 2025 14:51
if let Some(section) = &link_section {
writeln!(begin, ".pushsection {section},\"xr\"").unwrap()
} else if function_sections {
writeln!(begin, ".pushsection .text${asm_name},\"xr\"").unwrap()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On -msvc targets, function_sections is ignored. .text$sym is only used on -gnu targets.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also LLVM currently generates the following line on COFF targets:

.section        {section},"xr",one_only,{sym},unique,0`

Should we be doing the same here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On -msvc targets, function_sections is ignored.

Meaning that it's just always assumed to be on? https://godbolt.org/z/a6ofW49YK

.text$sym is only used on -gnu targets.

It should still work for msvc, no?

Should we be doing the same here?

I don't think we can reliably emit the unique,id bit of that line because how do we make that ID?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually using -Zfunction-sections=no on msvc does have an effect. So I'm not really sure what it being ignored means then. Maybe this is a more recent LLVM change?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, it's always assumed to be off on msvc. You can see it always uses .text.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It does always use .text but normally it emits the line you quoted:

  .section .text,"xr",one_only,foo,unique,0

i.e. it uses a subsection which, as far as I understand, does allow DCE, with -Zfunction-sections=no it emits just

  .text

So then DCE is impossible

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The unique,0 bit is apparently an LLVM extension https://llvm.org/docs/Extensions.html#id2. It is documented as elf-specific, but clearly it's used for COFF as well...

I just don't see how we can generate the unique ID in a reliable way. Also because we would generate a function per section, the function name (which should be unique?) should be sufficient to disambiguate the sections.

In short, I think the implementation in this PR is the best we can reliably do.

@bors
Copy link
Copy Markdown
Collaborator

bors commented Nov 3, 2025

☔ The latest upstream changes (presumably #148305) made this pull request unmergeable. Please resolve the merge conflicts.

@folkertdev folkertdev force-pushed the naked-function-sections branch from 6580a5f to b12830c Compare November 7, 2025 17:00
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@folkertdev
Copy link
Copy Markdown
Contributor Author

folkertdev commented Nov 8, 2025

Looks like windows will also be using function sections by default soon #148669

@folkertdev folkertdev force-pushed the naked-function-sections branch from b12830c to 77de006 Compare November 9, 2025 10:42
@rustbot

This comment has been minimized.

@mati865
Copy link
Copy Markdown
Member

mati865 commented Nov 9, 2025

Looks like windows will also be using function sections by default soon #148669

This is unlikely to happen, as you can see crashes in failed tests in that PR. That is what I was talking about in #147789 (comment)

@wesleywiser
Copy link
Copy Markdown
Member

Hi @folkertdev, are you waiting for another review or is the discussion thread here still active?

@folkertdev
Copy link
Copy Markdown
Contributor Author

In my mind this was blocked on review, but now that I read it again I should give this a once-over and probably test both with and without -Zfunction-sections=no

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jan 15, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Comment thread compiler/rustc_codegen_ssa/src/mir/naked_asm.rs Outdated
@folkertdev
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 17, 2026
@wesleywiser
Copy link
Copy Markdown
Member

Hi @Amanieu, it looks like this is ready for another round of review when you have a chance 🙂

@rust-bors rust-bors bot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 16, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 16, 2026
…, r=Amanieu

naked functions: respect `function-sections`

fixes rust-lang#147789

r? @Amanieu
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors r-

Probably this pr?
#155398 (comment)

@rust-bors rust-bors bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 16, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 16, 2026

This pull request was unapproved.

This PR was contained in a rollup (#155398), which was unapproved.

@folkertdev
Copy link
Copy Markdown
Contributor Author

@bors try jobs=dist-various-1,dist-various-2,aarch64-apple,x86_64-mingw-1

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 16, 2026
naked functions: respect `function-sections`


try-job: dist-various-1
try-job: dist-various-2
try-job: aarch64-apple
try-job: x86_64-mingw-1
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 16, 2026

💔 Test for 1a49225 failed: CI. Failed job:

@folkertdev folkertdev force-pushed the naked-function-sections branch from d6949a1 to c186ad4 Compare April 16, 2026 22:51
@folkertdev
Copy link
Copy Markdown
Contributor Author

hmm

@bors try jobs=dist-various-1,dist-various-2,aarch64-apple,x86_64-mingw-1

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 16, 2026
naked functions: respect `function-sections`


try-job: dist-various-1
try-job: dist-various-2
try-job: aarch64-apple
try-job: x86_64-mingw-1
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 17, 2026

💔 Test for 74be459 failed: CI. Failed job:

@folkertdev folkertdev force-pushed the naked-function-sections branch from c186ad4 to 2db9de3 Compare April 17, 2026 09:26
@folkertdev
Copy link
Copy Markdown
Contributor Author

ok whatever aarch64

@bors try jobs=dist-various-1,dist-various-2,aarch64-apple,x86_64-mingw-1

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 17, 2026
naked functions: respect `function-sections`


try-job: dist-various-1
try-job: dist-various-2
try-job: aarch64-apple
try-job: x86_64-mingw-1
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 17, 2026

☀️ Try build successful (CI)
Build commit: 30ba749 (30ba749b739d451baf017c104599a074a054f48f, parent: 1b8f2e46e14b08208a53585570edd9206374aae8)

@folkertdev
Copy link
Copy Markdown
Contributor Author

@bors iffy
@bors r=Amanieu

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 17, 2026

Unknown command "iffy". Run @bors help to see available commands.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 17, 2026

📌 Commit 2db9de3 has been approved by Amanieu

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 17, 2026
@folkertdev
Copy link
Copy Markdown
Contributor Author

@bors rollup=iffy

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

Labels

A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Each naked function creates separate PE code section

10 participants