Skip to content

Commit fb4ee7d

Browse files
committed
rustc_codegen_llvm: tease out functions to avoid duplication of bitcode section names
1 parent 0ae0f3a commit fb4ee7d

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::back::write::{self, save_temp_bitcode, CodegenDiagnosticsStage, DiagnosticHandlers};
1+
use crate::back::write::{
2+
self, bitcode_section_name, save_temp_bitcode, target_is_aix, target_is_apple,
3+
CodegenDiagnosticsStage, DiagnosticHandlers,
4+
};
25
use crate::errors::{
36
DynamicLinkingWithLTO, LlvmError, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib,
47
};
@@ -121,8 +124,12 @@ fn prepare_lto(
121124
.filter(|&(name, _)| looks_like_rust_object_file(name));
122125
for (name, child) in obj_files {
123126
info!("adding bitcode from {}", name);
127+
let is_apple = target_is_apple(cgcx);
128+
let is_aix = target_is_aix(cgcx);
124129
match get_bitcode_slice_from_object_data(
125130
child.data(&*archive_data).expect("corrupt rlib"),
131+
is_apple,
132+
is_aix,
126133
) {
127134
Ok(data) => {
128135
let module = SerializedModule::FromRlib(data.to_vec());
@@ -144,19 +151,19 @@ fn prepare_lto(
144151
Ok((symbols_below_threshold, upstream_modules))
145152
}
146153

147-
fn get_bitcode_slice_from_object_data(obj: &[u8]) -> Result<&[u8], LtoBitcodeFromRlib> {
154+
fn get_bitcode_slice_from_object_data(
155+
obj: &[u8],
156+
is_apple: bool,
157+
is_aix: bool,
158+
) -> Result<&[u8], LtoBitcodeFromRlib> {
148159
// The object crate doesn't understand bitcode files, but we can just sniff for the possible
149160
// magic strings here and return the whole slice directly.
150161
if obj.starts_with(b"\xDE\xC0\x17\x0B") || obj.starts_with(b"BC\xC0\xDE") {
151162
return Ok(obj);
152163
}
164+
let section = bitcode_section_name(is_apple, is_aix).trim_end_matches('\0');
153165
match object::read::File::parse(obj) {
154-
Ok(f) => match f
155-
.section_by_name(".llvmbc")
156-
.or_else(|| f.section_by_name(".llvm.lto"))
157-
.or_else(|| f.section_by_name("__LLVM,__bitcode"))
158-
.or_else(|| f.section_by_name(".ipa"))
159-
{
166+
Ok(f) => match f.section_by_name(section) {
160167
Some(d) => Ok(d.data().unwrap()),
161168
None => Err(LtoBitcodeFromRlib {
162169
llvm_err: "Bitcode section not found in object file".to_string(),

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,27 @@ fn create_section_with_flags_asm(section_name: &str, section_flags: &str, data:
853853
asm
854854
}
855855

856+
pub(crate) fn bitcode_section_name(is_apple: bool, is_aix: bool) -> &'static str {
857+
if is_apple {
858+
"__LLVM,__bitcode\0"
859+
} else if is_aix {
860+
".ipa\0"
861+
} else {
862+
".llvmbc\0"
863+
}
864+
}
865+
866+
pub(crate) fn target_is_apple(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
867+
cgcx.opts.target_triple.triple().contains("-ios")
868+
|| cgcx.opts.target_triple.triple().contains("-darwin")
869+
|| cgcx.opts.target_triple.triple().contains("-tvos")
870+
|| cgcx.opts.target_triple.triple().contains("-watchos")
871+
}
872+
873+
pub(crate) fn target_is_aix(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
874+
cgcx.opts.target_triple.triple().contains("-aix")
875+
}
876+
856877
/// Embed the bitcode of an LLVM module in the LLVM module itself.
857878
///
858879
/// This is done primarily for iOS where it appears to be standard to compile C
@@ -913,11 +934,8 @@ unsafe fn embed_bitcode(
913934
// Unfortunately, LLVM provides no way to set custom section flags. For ELF
914935
// and COFF we emit the sections using module level inline assembly for that
915936
// reason (see issue #90326 for historical background).
916-
let is_aix = cgcx.opts.target_triple.triple().contains("-aix");
917-
let is_apple = cgcx.opts.target_triple.triple().contains("-ios")
918-
|| cgcx.opts.target_triple.triple().contains("-darwin")
919-
|| cgcx.opts.target_triple.triple().contains("-tvos")
920-
|| cgcx.opts.target_triple.triple().contains("-watchos");
937+
let is_aix = target_is_aix(cgcx);
938+
let is_apple = target_is_apple(cgcx);
921939
if is_apple
922940
|| is_aix
923941
|| cgcx.opts.target_triple.triple().starts_with("wasm")
@@ -932,13 +950,7 @@ unsafe fn embed_bitcode(
932950
);
933951
llvm::LLVMSetInitializer(llglobal, llconst);
934952

935-
let section = if is_apple {
936-
"__LLVM,__bitcode\0"
937-
} else if is_aix {
938-
".ipa\0"
939-
} else {
940-
".llvmbc\0"
941-
};
953+
let section = bitcode_section_name(is_apple, is_aix);
942954
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
943955
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
944956
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);

0 commit comments

Comments
 (0)