Skip to content

Commit cf2cc01

Browse files
authored
Rollup merge of rust-lang#132340 - Zalathar:set-section, r=compiler-errors
cg_llvm: Consistently use safe wrapper function `set_section` Follow-up to rust-lang#131962 and rust-lang#132260 (comment). To avoid too much scope creep, I've deliberately kept the changes to `LLVMRustGetSliceFromObjectDataByName` as minimal as possible.
2 parents 2055237 + 65ff2a6 commit cf2cc01

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,14 @@ fn get_bitcode_slice_from_object_data<'a>(
165165
// We drop the "__LLVM," prefix here because on Apple platforms there's a notion of "segment
166166
// name" which in the public API for sections gets treated as part of the section name, but
167167
// internally in MachOObjectFile.cpp gets treated separately.
168-
let section_name = bitcode_section_name(cgcx).trim_start_matches("__LLVM,");
168+
let section_name = bitcode_section_name(cgcx).to_str().unwrap().trim_start_matches("__LLVM,");
169169
let mut len = 0;
170170
let data = unsafe {
171171
llvm::LLVMRustGetSliceFromObjectDataByName(
172172
obj.as_ptr(),
173173
obj.len(),
174174
section_name.as_ptr(),
175+
section_name.len(),
175176
&mut len,
176177
)
177178
};

compiler/rustc_codegen_llvm/src/back/write.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::CString;
1+
use std::ffi::{CStr, CString};
22
use std::io::{self, Write};
33
use std::path::{Path, PathBuf};
44
use std::sync::Arc;
@@ -958,14 +958,13 @@ fn target_is_aix(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
958958
cgcx.opts.target_triple.triple().contains("-aix")
959959
}
960960

961-
//FIXME use c string literals here too
962-
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static str {
961+
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static CStr {
963962
if target_is_apple(cgcx) {
964-
"__LLVM,__bitcode\0"
963+
c"__LLVM,__bitcode"
965964
} else if target_is_aix(cgcx) {
966-
".ipa\0"
965+
c".ipa"
967966
} else {
968-
".llvmbc\0"
967+
c".llvmbc"
969968
}
970969
}
971970

@@ -1042,8 +1041,7 @@ unsafe fn embed_bitcode(
10421041
);
10431042
llvm::LLVMSetInitializer(llglobal, llconst);
10441043

1045-
let section = bitcode_section_name(cgcx);
1046-
llvm::LLVMSetSection(llglobal, section.as_c_char_ptr());
1044+
llvm::set_section(llglobal, bitcode_section_name(cgcx));
10471045
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
10481046
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
10491047

@@ -1061,7 +1059,7 @@ unsafe fn embed_bitcode(
10611059
} else {
10621060
c".llvmcmd"
10631061
};
1064-
llvm::LLVMSetSection(llglobal, section.as_ptr());
1062+
llvm::set_section(llglobal, section);
10651063
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
10661064
} else {
10671065
// We need custom section flags, so emit module-level inline assembly.

compiler/rustc_codegen_llvm/src/base.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,8 @@ pub(crate) fn compile_codegen_unit(
145145

146146
pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
147147
let Some(sect) = attrs.link_section else { return };
148-
unsafe {
149-
let buf = SmallCStr::new(sect.as_str());
150-
llvm::LLVMSetSection(llval, buf.as_ptr());
151-
}
148+
let buf = SmallCStr::new(sect.as_str());
149+
llvm::set_section(llval, &buf);
152150
}
153151

154152
pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {

compiler/rustc_codegen_llvm/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
565565
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
566566
llvm::LLVMSetInitializer(g, array);
567567
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
568-
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
568+
llvm::set_section(g, c"llvm.metadata");
569569
}
570570
}
571571
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
7272
let section_var = cx
7373
.define_global(section_var_name, llvm_type)
7474
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
75-
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr());
75+
llvm::set_section(section_var, c".debug_gdb_scripts");
7676
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
7777
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7878
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,7 @@ unsafe extern "C" {
24162416
data: *const u8,
24172417
len: usize,
24182418
name: *const u8,
2419+
name_len: usize,
24192420
out_len: &mut usize,
24202421
) -> *const u8;
24212422

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1559,8 +1559,10 @@ extern "C" LLVMModuleRef LLVMRustParseBitcodeForLTO(LLVMContextRef Context,
15591559
extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,
15601560
size_t len,
15611561
const char *name,
1562+
size_t name_len,
15621563
size_t *out_len) {
15631564
*out_len = 0;
1565+
auto Name = StringRef(name, name_len);
15641566
auto Data = StringRef(data, len);
15651567
auto Buffer = MemoryBufferRef(Data, ""); // The id is unused.
15661568
file_magic Type = identify_magic(Buffer.getBuffer());
@@ -1571,8 +1573,8 @@ extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,
15711573
return nullptr;
15721574
}
15731575
for (const object::SectionRef &Sec : (*ObjFileOrError)->sections()) {
1574-
Expected<StringRef> Name = Sec.getName();
1575-
if (Name && *Name == name) {
1576+
Expected<StringRef> SecName = Sec.getName();
1577+
if (SecName && *SecName == Name) {
15761578
Expected<StringRef> SectionOrError = Sec.getContents();
15771579
if (!SectionOrError) {
15781580
LLVMRustSetLastError(toString(SectionOrError.takeError()).c_str());

0 commit comments

Comments
 (0)