Skip to content

Commit a0871ed

Browse files
committed
Fix __eh_frame for AArch64 on macOS
The relocations must use symbols (not sections), and the relative relocation needs ARM64_RELOC_SUBTRACTOR. The section must be in __TEXT rather than __DWARF, which is handled by using StandardSection::EhFrame. This also changes the section type for ELF x86-64.
1 parent 04564d1 commit a0871ed

3 files changed

Lines changed: 38 additions & 30 deletions

File tree

src/debuginfo/emit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl DebugContext {
4343
let _: Result<()> = sections.for_each(|id, section| {
4444
if let Some(section_id) = section_map.get(&id) {
4545
for reloc in &section.relocs {
46-
product.add_debug_reloc(&section_map, section_id, reloc);
46+
product.add_debug_reloc(&section_map, section_id, reloc, true);
4747
}
4848
}
4949
Ok(())

src/debuginfo/object.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use cranelift_module::{DataId, FuncId};
22
use cranelift_object::ObjectProduct;
33
use gimli::SectionId;
4-
use object::write::{Relocation, StandardSegment};
4+
use object::write::{Relocation, StandardSection, StandardSegment};
55
use object::{RelocationEncoding, RelocationFlags, SectionKind};
66
use rustc_data_structures::fx::FxHashMap;
77

@@ -16,6 +16,7 @@ pub(super) trait WriteDebugInfo {
1616
section_map: &FxHashMap<SectionId, Self::SectionId>,
1717
from: &Self::SectionId,
1818
reloc: &DebugReloc,
19+
use_section_symbol: bool,
1920
);
2021
}
2122

@@ -27,29 +28,31 @@ impl WriteDebugInfo for ObjectProduct {
2728
id: SectionId,
2829
data: Vec<u8>,
2930
) -> (object::write::SectionId, object::write::SymbolId) {
30-
let name = if self.object.format() == object::BinaryFormat::MachO {
31-
id.name().replace('.', "__") // machO expects __debug_info instead of .debug_info
31+
let (section_id, align);
32+
if id == SectionId::EhFrame {
33+
section_id = self.object.section_id(StandardSection::EhFrame);
34+
align = 8;
3235
} else {
33-
id.name().to_string()
34-
}
35-
.into_bytes();
36-
37-
let segment = self.object.segment_name(StandardSegment::Debug).to_vec();
38-
// FIXME use SHT_X86_64_UNWIND for .eh_frame
39-
let section_id = self.object.add_section(
40-
segment,
41-
name,
42-
if id == SectionId::DebugStr || id == SectionId::DebugLineStr {
43-
SectionKind::DebugString
44-
} else if id == SectionId::EhFrame {
45-
SectionKind::ReadOnlyData
36+
let name = if self.object.format() == object::BinaryFormat::MachO {
37+
id.name().replace('.', "__") // machO expects __debug_info instead of .debug_info
4638
} else {
47-
SectionKind::Debug
48-
},
49-
);
50-
self.object
51-
.section_mut(section_id)
52-
.set_data(data, if id == SectionId::EhFrame { 8 } else { 1 });
39+
id.name().to_string()
40+
}
41+
.into_bytes();
42+
43+
let segment = self.object.segment_name(StandardSegment::Debug).to_vec();
44+
section_id = self.object.add_section(
45+
segment,
46+
name,
47+
if id == SectionId::DebugStr || id == SectionId::DebugLineStr {
48+
SectionKind::DebugString
49+
} else {
50+
SectionKind::Debug
51+
},
52+
);
53+
align = 1;
54+
}
55+
self.object.section_mut(section_id).set_data(data, align);
5356
let symbol_id = self.object.section_symbol(section_id);
5457
(section_id, symbol_id)
5558
}
@@ -59,6 +62,7 @@ impl WriteDebugInfo for ObjectProduct {
5962
section_map: &FxHashMap<SectionId, Self::SectionId>,
6063
from: &Self::SectionId,
6164
reloc: &DebugReloc,
65+
use_section_symbol: bool,
6266
) {
6367
let (symbol, symbol_offset) = match reloc.name {
6468
DebugRelocName::Section(id) => (section_map.get(&id).unwrap().1, 0),
@@ -69,7 +73,11 @@ impl WriteDebugInfo for ObjectProduct {
6973
} else {
7074
self.data_symbol(DataId::from_u32(id & !(1 << 31)))
7175
};
72-
self.object.symbol_section_and_offset(symbol_id).unwrap_or((symbol_id, 0))
76+
if use_section_symbol {
77+
self.object.symbol_section_and_offset(symbol_id).unwrap_or((symbol_id, 0))
78+
} else {
79+
(symbol_id, 0)
80+
}
7381
}
7482
};
7583
self.object

src/debuginfo/unwind.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,12 @@ impl UnwindContext {
120120
func_id: FuncId,
121121
context: &Context,
122122
) {
123-
if let target_lexicon::OperatingSystem::MacOSX { .. } =
124-
module.isa().triple().operating_system
123+
let triple = module.isa().triple();
124+
if matches!(triple.operating_system, target_lexicon::OperatingSystem::MacOSX { .. })
125+
&& triple.architecture == target_lexicon::Architecture::X86_64
125126
{
126127
// The object crate doesn't currently support DW_GNU_EH_PE_absptr, which macOS
127-
// requires for unwinding tables. In addition on arm64 it currently doesn't
128-
// support 32bit relocations as we currently use for the unwinding table.
129-
// See gimli-rs/object#415 and rust-lang/rustc_codegen_cranelift#1371
128+
// requires for unwinding tables. See gimli-rs/object#415.
130129
return;
131130
}
132131

@@ -250,8 +249,9 @@ impl UnwindContext {
250249
let mut section_map = FxHashMap::default();
251250
section_map.insert(id, section_id);
252251

252+
let use_section_symbol = product.object.format() != object::BinaryFormat::MachO;
253253
for reloc in &eh_frame.0.relocs {
254-
product.add_debug_reloc(&section_map, &section_id, reloc);
254+
product.add_debug_reloc(&section_map, &section_id, reloc, use_section_symbol);
255255
}
256256
}
257257
}

0 commit comments

Comments
 (0)