Skip to content

Commit 0673cde

Browse files
committed
Use LLVM for getting symbols from COFF bigobj files
1 parent be67084 commit 0673cde

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,20 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
261261
}
262262
}
263263

264+
// The object crate doesn't know how to get symbols for LLVM bitcode and COFF bigobj files.
265+
// As such we need to use LLVM for them.
264266
#[deny(unsafe_op_in_unsafe_fn)]
265267
fn get_llvm_object_symbols(
266268
buf: &[u8],
267269
f: &mut dyn FnMut(&[u8]) -> io::Result<()>,
268270
) -> io::Result<bool> {
269-
if unsafe { llvm::LLVMRustIsBitcode(buf.as_ptr(), buf.len()) } {
271+
let is_bitcode = unsafe { llvm::LLVMRustIsBitcode(buf.as_ptr(), buf.len()) };
272+
273+
// COFF bigobj file, msvc LTO file or import library. See
274+
// https://github.com/llvm/llvm-project/blob/453f27bc9/llvm/lib/BinaryFormat/Magic.cpp#L38-L51
275+
let is_unsupported_windows_obj_file = buf.get(0..4) == Some(b"\0\0\xFF\xFF");
276+
277+
if is_bitcode || is_unsupported_windows_obj_file {
270278
let mut state = Box::new(f);
271279

272280
let err = unsafe {

compiler/rustc_llvm/llvm-wrapper/SymbolWrapper.cpp

+24-10
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,33 @@ extern "C" void *LLVMRustGetSymbols(
4949
std::unique_ptr<object::SymbolicFile> Obj;
5050

5151
const file_magic Type = identify_magic(Buf->getBuffer());
52-
if (Type != file_magic::bitcode) {
53-
return ErrorCallback("not bitcode");
52+
if (!object::SymbolicFile::isSymbolicFile(Type, &Context)) {
53+
return 0;
5454
}
55-
auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
55+
56+
if (Type == file_magic::bitcode) {
57+
auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
5658
Buf->getMemBufferRef(), file_magic::bitcode, &Context);
57-
if (!ObjOrErr) {
58-
Error E = ObjOrErr.takeError();
59-
SmallString<0> ErrorBuf;
60-
raw_svector_ostream Error(ErrorBuf);
61-
Error << E << '\0';
62-
return ErrorCallback(Error.str().data());
59+
if (!ObjOrErr) {
60+
Error E = ObjOrErr.takeError();
61+
SmallString<0> ErrorBuf;
62+
raw_svector_ostream Error(ErrorBuf);
63+
Error << E << '\0';
64+
return ErrorCallback(Error.str().data());
65+
}
66+
Obj = std::move(*ObjOrErr);
67+
} else {
68+
auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf->getMemBufferRef());
69+
if (!ObjOrErr) {
70+
Error E = ObjOrErr.takeError();
71+
SmallString<0> ErrorBuf;
72+
raw_svector_ostream Error(ErrorBuf);
73+
Error << E << '\0';
74+
return ErrorCallback(Error.str().data());
75+
}
76+
Obj = std::move(*ObjOrErr);
6377
}
64-
Obj = std::move(*ObjOrErr);
78+
6579

6680
for (const object::BasicSymbolRef &S : Obj->symbols()) {
6781
if (!isArchiveSymbol(S))

0 commit comments

Comments
 (0)