Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3e30f1

Browse files
authoredJan 20, 2023
Rollup merge of rust-lang#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.
2 parents 5ce39f4 + ac4956b commit c3e30f1

File tree

6 files changed

+72
-15
lines changed

6 files changed

+72
-15
lines changed
 

‎compiler/rustc_codegen_llvm/src/context.rs

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_target::abi::{
3333
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3434
use smallvec::SmallVec;
3535

36+
use libc::c_uint;
3637
use std::cell::{Cell, RefCell};
3738
use std::ffi::CStr;
3839
use std::str;
@@ -347,6 +348,23 @@ pub unsafe fn create_module<'ll>(
347348
);
348349
}
349350

351+
// Insert `llvm.ident` metadata.
352+
//
353+
// On the wasm targets it will get hooked up to the "producer" sections
354+
// `processed-by` information.
355+
let rustc_producer =
356+
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
357+
let name_metadata = llvm::LLVMMDStringInContext(
358+
llcx,
359+
rustc_producer.as_ptr().cast(),
360+
rustc_producer.as_bytes().len() as c_uint,
361+
);
362+
llvm::LLVMAddNamedMetadataOperand(
363+
llmod,
364+
cstr!("llvm.ident").as_ptr(),
365+
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
366+
);
367+
350368
llmod
351369
}
352370

‎compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

-15
Original file line numberDiff line numberDiff line change
@@ -907,21 +907,6 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
907907
);
908908
}
909909

910-
// Insert `llvm.ident` metadata on the wasm targets since that will
911-
// get hooked up to the "producer" sections `processed-by` information.
912-
if tcx.sess.target.is_like_wasm {
913-
let name_metadata = llvm::LLVMMDStringInContext(
914-
debug_context.llcontext,
915-
rustc_producer.as_ptr().cast(),
916-
rustc_producer.as_bytes().len() as c_uint,
917-
);
918-
llvm::LLVMAddNamedMetadataOperand(
919-
debug_context.llmod,
920-
cstr!("llvm.ident").as_ptr(),
921-
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
922-
);
923-
}
924-
925910
return unit_metadata;
926911
};
927912

‎compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,11 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M,
13601360
if (WasmCustomSections)
13611361
WasmCustomSections->eraseFromParent();
13621362

1363+
// `llvm.ident` named metadata also gets duplicated.
1364+
auto *llvmIdent = (*MOrErr)->getNamedMetadata("llvm.ident");
1365+
if (llvmIdent)
1366+
llvmIdent->eraseFromParent();
1367+
13631368
return MOrErr;
13641369
};
13651370
bool ClearDSOLocal = clearDSOLocalOnDeclarations(Mod, Target);

‎tests/codegen/llvm-ident.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Verifies that the `!llvm.ident` named metadata is emitted.
2+
//
3+
// revisions: NONE OPT DEBUG
4+
//
5+
// [OPT] compile-flags: -Copt-level=2
6+
// [DEBUG] compile-flags: -Cdebuginfo=2
7+
8+
// The named metadata should contain a single metadata node (see
9+
// `LLVMRustPrepareThinLTOImport` for details).
10+
// CHECK: !llvm.ident = !{![[ID:[0-9]+]]}
11+
12+
// In addition, check that the metadata node has the expected content.
13+
// CHECK: ![[ID]] = !{!"rustc version 1.{{.*}}"}
14+
15+
fn main() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include ../../run-make-fulldeps/tools.mk
2+
3+
# only-linux
4+
5+
all:
6+
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps
7+
8+
# Check linked output has a `.comment` section with the expected content.
9+
readelf -p '.comment' $(TMPDIR)/rust_out | $(CGREP) -F 'rustc version 1.'
10+
11+
# Check all object files (including temporary outputs) have a `.comment`
12+
# section with the expected content.
13+
set -e; for f in $(TMPDIR)/*.o; do \
14+
readelf -p '.comment' $$f | $(CGREP) -F 'rustc version 1.'; \
15+
done

‎tests/run-make/llvm-ident/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
include ../../run-make-fulldeps/tools.mk
2+
3+
# only-linux
4+
5+
all:
6+
# `-Ccodegen-units=16 -Copt-level=2` is used here to trigger thin LTO
7+
# across codegen units to test deduplication of the named metadata
8+
# (see `LLVMRustPrepareThinLTOImport` for details).
9+
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps -Ccodegen-units=16 -Copt-level=2
10+
11+
# `llvm-dis` is used here since `--emit=llvm-ir` does not emit LLVM IR
12+
# for temporary outputs.
13+
"$(LLVM_BIN_DIR)"/llvm-dis $(TMPDIR)/*.bc
14+
15+
# Check LLVM IR files (including temporary outputs) have `!llvm.ident`
16+
# named metadata, reusing the related codegen test.
17+
set -e; for f in $(TMPDIR)/*.ll; do \
18+
$(LLVM_FILECHECK) --input-file $$f ../../codegen/llvm-ident.rs; \
19+
done

0 commit comments

Comments
 (0)
Please sign in to comment.