Skip to content

Commit

Permalink
Add PreservedSymbols from LLVM to LTO.
Browse files Browse the repository at this point in the history
When building with LTO, builtin functions that are defined but whose calls have not been inserted yet, get internalized.
We need to prevent these symbols from being internalized at LTO time.

Refer to https://reviews.llvm.org/D49434.
  • Loading branch information
DianQK committed Aug 30, 2023
1 parent c1ec76c commit f7751fa
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
25 changes: 24 additions & 1 deletion compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
}
}

fn get_llvm_preserved_symbols() -> Vec<String> {
let mut len = 0;
unsafe {
let symbols = llvm::LLVMRustPreservedSymbols(&mut len);
let symbols: &[*const _] = slice::from_raw_parts(symbols, len);
symbols
.iter()
.filter_map(|&symbol| {
if symbol.is_null() {
None
} else {
Some(String::from_utf8(CStr::from_ptr(symbol).to_bytes().to_vec()).unwrap())
}
})
.collect()
}
}

fn prepare_lto(
cgcx: &CodegenContext<LlvmCodegenBackend>,
diag_handler: &Handler,
Expand All @@ -53,8 +71,13 @@ fn prepare_lto(
Lto::No => panic!("didn't request LTO but we're doing LTO"),
};

let llvm_reserved_symbols = get_llvm_preserved_symbols();

let symbol_filter = &|&(ref name, info): &(String, SymbolExportInfo)| {
if info.level.is_below_threshold(export_threshold) || info.used {
if info.level.is_below_threshold(export_threshold)
|| info.used
|| llvm_reserved_symbols.contains(name)
{
Some(CString::new(name.as_str()).unwrap())
} else {
None
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ extern "C" {
pub fn LLVMRustSetLLVMOptions(Argc: c_int, Argv: *const *const c_char);
pub fn LLVMRustPrintPasses();
pub fn LLVMRustSetNormalizedTarget(M: &Module, triple: *const c_char);
pub fn LLVMRustPreservedSymbols(len: *mut usize) -> *const *const c_char;
pub fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);

pub fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,20 @@ extern "C" void LLVMRustPrintPasses() {
PB.printPassNames(outs());
}

// from /~https://github.com/llvm/llvm-project/blob/7021182d6b43de9488ab70de626192ce70b3a4a6/llvm/lib/Object/IRSymtab.cpp#L48-L57
static const char *PreservedSymbols[] = {
#define HANDLE_LIBCALL(code, name) name,
#include "llvm/IR/RuntimeLibcalls.def"
#undef HANDLE_LIBCALL
"__ssp_canary_word",
"__stack_chk_guard",
};

extern "C" const char **LLVMRustPreservedSymbols(size_t *len) {
*len = sizeof(PreservedSymbols) / sizeof(PreservedSymbols[0]);
return PreservedSymbols;
}

extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
size_t Len) {
auto PreserveFunctions = [=](const GlobalValue &GV) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn my_panic(_info: &core::panic::PanicInfo) -> ! {

#[no_mangle]
pub fn multer(a: i128, b: i128) -> i128 {
// Trigger usage of the __multi3 compiler intrinsic which then leads to an imported
// panic function in case of a bug. We verify that no imports exist in our verifier.
// Trigger usage of the __multi3 compiler intrinsic which then leads to an imported function
// such as panic or __multi3 in case of a bug. We verify that no imports exist in our verifier.
a * b
}

0 comments on commit f7751fa

Please sign in to comment.