Rollup merge of #132340 - Zalathar:set-section, r=compiler-errors
cg_llvm: Consistently use safe wrapper function `set_section` Follow-up to #131962 and https://github.com/rust-lang/rust/pull/132260#discussion_r1821626260. To avoid too much scope creep, I've deliberately kept the changes to `LLVMRustGetSliceFromObjectDataByName` as minimal as possible.
This commit is contained in:
commit
cf2cc010a3
@ -165,13 +165,14 @@ fn get_bitcode_slice_from_object_data<'a>(
|
|||||||
// We drop the "__LLVM," prefix here because on Apple platforms there's a notion of "segment
|
// We drop the "__LLVM," prefix here because on Apple platforms there's a notion of "segment
|
||||||
// name" which in the public API for sections gets treated as part of the section name, but
|
// name" which in the public API for sections gets treated as part of the section name, but
|
||||||
// internally in MachOObjectFile.cpp gets treated separately.
|
// internally in MachOObjectFile.cpp gets treated separately.
|
||||||
let section_name = bitcode_section_name(cgcx).trim_start_matches("__LLVM,");
|
let section_name = bitcode_section_name(cgcx).to_str().unwrap().trim_start_matches("__LLVM,");
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
let data = unsafe {
|
let data = unsafe {
|
||||||
llvm::LLVMRustGetSliceFromObjectDataByName(
|
llvm::LLVMRustGetSliceFromObjectDataByName(
|
||||||
obj.as_ptr(),
|
obj.as_ptr(),
|
||||||
obj.len(),
|
obj.len(),
|
||||||
section_name.as_ptr(),
|
section_name.as_ptr(),
|
||||||
|
section_name.len(),
|
||||||
&mut len,
|
&mut len,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::ffi::CString;
|
use std::ffi::{CStr, CString};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -958,14 +958,13 @@ fn target_is_aix(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
|
|||||||
cgcx.opts.target_triple.triple().contains("-aix")
|
cgcx.opts.target_triple.triple().contains("-aix")
|
||||||
}
|
}
|
||||||
|
|
||||||
//FIXME use c string literals here too
|
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static CStr {
|
||||||
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static str {
|
|
||||||
if target_is_apple(cgcx) {
|
if target_is_apple(cgcx) {
|
||||||
"__LLVM,__bitcode\0"
|
c"__LLVM,__bitcode"
|
||||||
} else if target_is_aix(cgcx) {
|
} else if target_is_aix(cgcx) {
|
||||||
".ipa\0"
|
c".ipa"
|
||||||
} else {
|
} else {
|
||||||
".llvmbc\0"
|
c".llvmbc"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1042,8 +1041,7 @@ unsafe fn embed_bitcode(
|
|||||||
);
|
);
|
||||||
llvm::LLVMSetInitializer(llglobal, llconst);
|
llvm::LLVMSetInitializer(llglobal, llconst);
|
||||||
|
|
||||||
let section = bitcode_section_name(cgcx);
|
llvm::set_section(llglobal, bitcode_section_name(cgcx));
|
||||||
llvm::LLVMSetSection(llglobal, section.as_c_char_ptr());
|
|
||||||
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
||||||
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
|
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
|
||||||
|
|
||||||
@ -1061,7 +1059,7 @@ unsafe fn embed_bitcode(
|
|||||||
} else {
|
} else {
|
||||||
c".llvmcmd"
|
c".llvmcmd"
|
||||||
};
|
};
|
||||||
llvm::LLVMSetSection(llglobal, section.as_ptr());
|
llvm::set_section(llglobal, section);
|
||||||
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
||||||
} else {
|
} else {
|
||||||
// We need custom section flags, so emit module-level inline assembly.
|
// We need custom section flags, so emit module-level inline assembly.
|
||||||
|
@ -145,10 +145,8 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm
|
|||||||
|
|
||||||
pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
|
pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
|
||||||
let Some(sect) = attrs.link_section else { return };
|
let Some(sect) = attrs.link_section else { return };
|
||||||
unsafe {
|
let buf = SmallCStr::new(sect.as_str());
|
||||||
let buf = SmallCStr::new(sect.as_str());
|
llvm::set_section(llval, &buf);
|
||||||
llvm::LLVMSetSection(llval, buf.as_ptr());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
|
pub(crate) fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
|
||||||
|
@ -565,7 +565,7 @@ pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'
|
|||||||
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
|
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
|
||||||
llvm::LLVMSetInitializer(g, array);
|
llvm::LLVMSetInitializer(g, array);
|
||||||
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
|
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
|
||||||
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
|
llvm::set_section(g, c"llvm.metadata");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ pub(crate) fn get_or_insert_gdb_debug_scripts_section_global<'ll>(
|
|||||||
let section_var = cx
|
let section_var = cx
|
||||||
.define_global(section_var_name, llvm_type)
|
.define_global(section_var_name, llvm_type)
|
||||||
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
|
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
|
||||||
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr());
|
llvm::set_section(section_var, c".debug_gdb_scripts");
|
||||||
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
|
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
|
||||||
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
|
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
|
||||||
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
|
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);
|
||||||
|
@ -2416,6 +2416,7 @@ pub fn LLVMRustGetSliceFromObjectDataByName(
|
|||||||
data: *const u8,
|
data: *const u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
name: *const u8,
|
name: *const u8,
|
||||||
|
name_len: usize,
|
||||||
out_len: &mut usize,
|
out_len: &mut usize,
|
||||||
) -> *const u8;
|
) -> *const u8;
|
||||||
|
|
||||||
|
@ -1559,8 +1559,10 @@ extern "C" LLVMModuleRef LLVMRustParseBitcodeForLTO(LLVMContextRef Context,
|
|||||||
extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,
|
extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,
|
||||||
size_t len,
|
size_t len,
|
||||||
const char *name,
|
const char *name,
|
||||||
|
size_t name_len,
|
||||||
size_t *out_len) {
|
size_t *out_len) {
|
||||||
*out_len = 0;
|
*out_len = 0;
|
||||||
|
auto Name = StringRef(name, name_len);
|
||||||
auto Data = StringRef(data, len);
|
auto Data = StringRef(data, len);
|
||||||
auto Buffer = MemoryBufferRef(Data, ""); // The id is unused.
|
auto Buffer = MemoryBufferRef(Data, ""); // The id is unused.
|
||||||
file_magic Type = identify_magic(Buffer.getBuffer());
|
file_magic Type = identify_magic(Buffer.getBuffer());
|
||||||
@ -1571,8 +1573,8 @@ extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
for (const object::SectionRef &Sec : (*ObjFileOrError)->sections()) {
|
for (const object::SectionRef &Sec : (*ObjFileOrError)->sections()) {
|
||||||
Expected<StringRef> Name = Sec.getName();
|
Expected<StringRef> SecName = Sec.getName();
|
||||||
if (Name && *Name == name) {
|
if (SecName && *SecName == Name) {
|
||||||
Expected<StringRef> SectionOrError = Sec.getContents();
|
Expected<StringRef> SectionOrError = Sec.getContents();
|
||||||
if (!SectionOrError) {
|
if (!SectionOrError) {
|
||||||
LLVMRustSetLastError(toString(SectionOrError.takeError()).c_str());
|
LLVMRustSetLastError(toString(SectionOrError.takeError()).c_str());
|
||||||
|
Loading…
Reference in New Issue
Block a user