-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #97550 - ojeda:comment-section, r=bjorn3
[RFC] Support `.comment` section like GCC/Clang (`!llvm.ident`) Both GCC and Clang write by default a `.comment` section with compiler information: ```txt $ gcc -c -xc /dev/null && readelf -p '.comment' null.o String dump of section '.comment': [ 1] GCC: (GNU) 11.2.0 $ clang -c -xc /dev/null && readelf -p '.comment' null.o String dump of section '.comment': [ 1] clang version 14.0.1 (https://github.com/llvm/llvm-project.git c62053979489ccb002efe411c3af059addcb5d7d) ``` They also implement the `-Qn` flag to avoid doing so: ```txt $ gcc -Qn -c -xc /dev/null && readelf -p '.comment' null.o readelf: Warning: Section '.comment' was not dumped because it does not exist! $ clang -Qn -c -xc /dev/null && readelf -p '.comment' null.o readelf: Warning: Section '.comment' was not dumped because it does not exist! ``` So far, `rustc` only does it for WebAssembly targets and only when debug info is enabled: ```txt $ echo 'fn main(){}' | rustc --target=wasm32-unknown-unknown --emit=llvm-ir -Cdebuginfo=2 - && grep llvm.ident rust_out.ll !llvm.ident = !{!27} ``` The RFC part of this PR is about which behavior should `rustc` follow: - Always add it. - Add it by default, i.e. have an opt-out flag (GCC, Clang). - Have an opt-in flag. - Never add it (current). There is also the question of whether debug info being enabled matters for that decision, given the current behavior of WebAssembly targets. For instance, adding it by default gets us closer to other popular compilers, but that may surprise some users with an information leak. The most conservative option is to only do so opt-in, even if debug info is enabled (some users may be stripping debug info and not expecting something else to be leaked elsewhere). Implementation-wise, this covers both `ModuleLlvm::new()` and `ModuleLlvm::new_metadata()` cases by moving the addition to `context::create_module` and adds a few test cases. ThinLTO also sees the `llvm.ident` named metadata duplicated (in temporary outputs), so this deduplicates it like it is done for `wasm.custom_sections`. The tests also check this duplication does not take place.
- Loading branch information
Showing
6 changed files
with
72 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Verifies that the `!llvm.ident` named metadata is emitted. | ||
// | ||
// revisions: NONE OPT DEBUG | ||
// | ||
// [OPT] compile-flags: -Copt-level=2 | ||
// [DEBUG] compile-flags: -Cdebuginfo=2 | ||
|
||
// The named metadata should contain a single metadata node (see | ||
// `LLVMRustPrepareThinLTOImport` for details). | ||
// CHECK: !llvm.ident = !{![[ID:[0-9]+]]} | ||
|
||
// In addition, check that the metadata node has the expected content. | ||
// CHECK: ![[ID]] = !{!"rustc version 1.{{.*}}"} | ||
|
||
fn main() {} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
include ../tools.mk | ||
|
||
# only-linux | ||
|
||
all: | ||
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps --target=$(TARGET) | ||
|
||
# Check linked output has a `.comment` section with the expected content. | ||
readelf -p '.comment' $(TMPDIR)/rust_out | $(CGREP) -F 'rustc version 1.' | ||
|
||
# Check all object files (including temporary outputs) have a `.comment` | ||
# section with the expected content. | ||
set -e; for f in $(TMPDIR)/*.o; do \ | ||
readelf -p '.comment' $$f | $(CGREP) -F 'rustc version 1.'; \ | ||
done |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
include ../tools.mk | ||
|
||
# only-linux | ||
|
||
all: | ||
# `-Ccodegen-units=16 -Copt-level=2` is used here to trigger thin LTO | ||
# across codegen units to test deduplication of the named metadata | ||
# (see `LLVMRustPrepareThinLTOImport` for details). | ||
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps -Ccodegen-units=16 -Copt-level=2 --target=$(TARGET) | ||
|
||
# `llvm-dis` is used here since `--emit=llvm-ir` does not emit LLVM IR | ||
# for temporary outputs. | ||
"$(LLVM_BIN_DIR)"/llvm-dis $(TMPDIR)/*.bc | ||
|
||
# Check LLVM IR files (including temporary outputs) have `!llvm.ident` | ||
# named metadata, reusing the related codegen test. | ||
set -e; for f in $(TMPDIR)/*.ll; do \ | ||
$(LLVM_FILECHECK) --input-file $$f ../../codegen/llvm-ident.rs; \ | ||
done |