Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add codegen option strip #71825

Merged
merged 1 commit into from
May 11, 2020
Merged

add codegen option strip #71825

merged 1 commit into from
May 11, 2020

Conversation

contrun
Copy link
Contributor

@contrun contrun commented May 3, 2020

closes #71757

I don't know if the flags added here works for all linkers. I only tested on my Linux pc. I also don't know what is the best for emlinker, PtxLinker, MsvcLinker. The option for WasmLd is copied from https://aransentin.github.io/cwasm/.

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 3, 2020
@GabrielMajeri
Copy link
Contributor

Looks good to me, but I think according to the comment in the options.rs file, the following should also be updated:

// If you add a new option, please update:
// - src/librustc_interface/tests.rs
// - src/doc/rustc/src/codegen-options/index.md

@contrun
Copy link
Contributor Author

contrun commented May 3, 2020

@GabrielMajeri Thanks. I added the relevant sections in aforementioned files. I don't know if there are still things overlooked.

@rust-highfive

This comment has been minimized.

@contrun contrun force-pushed the cg-option-strip branch 2 times, most recently from d188309 to 942a16e Compare May 3, 2020 15:28
Copy link
Contributor

@josephlr josephlr left a comment

Choose a reason for hiding this comment

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

This looks reasonable, but I think we can do better than noop for emscripten and MSVC

@@ -898,6 +909,10 @@ impl<'a> Linker for EmLinker<'a> {
});
}

fn strip(&mut self) {
// noop
Copy link
Contributor

Choose a reason for hiding this comment

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

For emscripten, you should try to use --llvm-opts to pass -strip-all to the LLVM toolchain. See the emcc documentation

You may also need something like -Wl to make this work, I'm not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know if --llvm-opts can be specified multiple times. Can we just add two --llvm-opts and ['--strip-all'] here? Or we should add an field llvmOpts in Emlinker?

src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
@petrochenkov
Copy link
Contributor

This PR introduces the option as immediately stable (-C strip).
New options are usually introduced as unstable (-Z strip) instead.

@cramertj
Copy link
Member

cramertj commented May 8, 2020

r? @petrochenkov

@petrochenkov
Copy link
Contributor

Upon some closer investigation, I think this should be added in a slightly different form.

  • -Z strip-debuginfo-if-disabled should be removed.
  • A new option -Z strip=[none|debuginfo|symbols] should be added.
  • From implementation point of view Linker::strip should be merged with Linker::debuginfo.

cc #46034 #49212

@petrochenkov
Copy link
Contributor

Effect of the option on different platforms:

  • ELF

    • -Z strip=debuginfo -> --strip-debug, strips debuginfo sections and debuginfo symbols from the symbol table section at link time.
    • -Z strip=symbols -> --strip-all, same as -Z strip=debuginfo but also strips the rest of the symbol table section. Dynamic symbol table is not stripped, so if we want to load shared libraries we are still good.
  • PE/COFF + DWARF

    • -Z strip=debuginfo -> --strip-debug strips debuginfo sections and debuginfo symbols from the symbol table section at link time.
    • -Z strip=symbols -> --strip-all same as -Z strip=debuginfo, but also strips the rest of the symbol table section.
  • PE/COFF + CodeView

    • -Z strip=debuginfo -> /DEBUG:NONE strips debuginfo sections (not copies them to PDB) at link time.
    • -Z strip=symbols -> /DEBUG:NONE, same as -Z strip=debuginfo
    • We may want so add one more option for /PDBSTRIPPED in the future, but I haven't seen any demand for it so far.

I wouldn't care too much about other target flavors right now, it's safe to do nothing if these options are passed.
If people care about PTX linker etc, they will contribute the proper implementation eventually.

@petrochenkov
Copy link
Contributor

petrochenkov commented May 8, 2020

GCC doc says that -s (which translates to --strip-all) "removes all ... relocation information from the executable", but ld doc doesn't say that, so I'm not sure what they mean.

The relocations required by dynamic linker are still there, and I assumed that all other relocations are removed during linking anyway as unnecessary.

cc @mati865

@petrochenkov petrochenkov 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 May 8, 2020
@tesuji
Copy link
Contributor

tesuji commented May 9, 2020

I wouldn't care too much about other target flavors right now, it's safe to do nothing if these options are passed.

Would adding a waring about doing nothing for those targets/linkers is better?

@mati865
Copy link
Contributor

mati865 commented May 9, 2020

Clang will just pass -s to the linker for both Linux and MinGW.
I've tried to find code for that in GCC but had to give up because I've already wasted too much time looking at this mess.

src/librustc_session/config.rs Show resolved Hide resolved
src/librustc_session/options.rs Outdated Show resolved Hide resolved
src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
@rust-highfive

This comment has been minimized.

src/librustc_session/options.rs Outdated Show resolved Hide resolved
src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
src/librustc_codegen_ssa/back/linker.rs Outdated Show resolved Hide resolved
@rust-highfive

This comment has been minimized.

@petrochenkov
Copy link
Contributor

Great!
r=me after squashing the commits

move strip option to "Z"

add more strip options, remove strip-debuginfo-if-disabled

merge strip and debuginfo
@petrochenkov
Copy link
Contributor

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented May 10, 2020

📌 Commit a6c2f73 has been approved by petrochenkov

@bors bors 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 May 10, 2020
@bors
Copy link
Contributor

bors commented May 10, 2020

⌛ Testing commit a6c2f73 with merge 0f73d7656803c0a3f9b88595e0dc9590eac4c573...

@Dylan-DPC-zz
Copy link

@bors retry yield

@bors
Copy link
Contributor

bors commented May 10, 2020

⌛ Testing commit a6c2f73 with merge cb49bb804985ac5e7d2df2250f9980bfd306ac42...

@Dylan-DPC-zz
Copy link

@bors retry yield

@bors
Copy link
Contributor

bors commented May 10, 2020

⌛ Testing commit a6c2f73 with merge 69da478ebbb84dd58aabace4bd3c803392cff8cb...

@Dylan-DPC-zz
Copy link

@bors retry yield

@bors
Copy link
Contributor

bors commented May 10, 2020

⌛ Testing commit a6c2f73 with merge aeb4738...

@bors
Copy link
Contributor

bors commented May 11, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: petrochenkov
Pushing aeb4738 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 11, 2020
@bors bors merged commit aeb4738 into rust-lang:master May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add compiler option to strip symbols from generated binaries
10 participants