1 use rustc_data_structures::fx::FxHashMap;
3 use cranelift_module::FuncId;
4 use cranelift_object::ObjectProduct;
6 use object::write::{Relocation, StandardSegment};
7 use object::{RelocationEncoding, SectionKind};
11 use crate::debuginfo::{DebugReloc, DebugRelocName};
13 pub(super) trait WriteDebugInfo {
16 fn add_debug_section(&mut self, name: SectionId, data: Vec<u8>) -> Self::SectionId;
19 section_map: &FxHashMap<SectionId, Self::SectionId>,
20 from: &Self::SectionId,
25 impl WriteDebugInfo for ObjectProduct {
26 type SectionId = (object::write::SectionId, object::write::SymbolId);
32 ) -> (object::write::SectionId, object::write::SymbolId) {
33 let name = if self.object.format() == object::BinaryFormat::MachO {
34 id.name().replace('.', "__") // machO expects __debug_info instead of .debug_info
40 let segment = self.object.segment_name(StandardSegment::Debug).to_vec();
41 // FIXME use SHT_X86_64_UNWIND for .eh_frame
42 let section_id = self.object.add_section(
45 if id == SectionId::EhFrame { SectionKind::ReadOnlyData } else { SectionKind::Debug },
48 .section_mut(section_id)
49 .set_data(data, if id == SectionId::EhFrame { 8 } else { 1 });
50 let symbol_id = self.object.section_symbol(section_id);
51 (section_id, symbol_id)
56 section_map: &FxHashMap<SectionId, Self::SectionId>,
57 from: &Self::SectionId,
60 let (symbol, symbol_offset) = match reloc.name {
61 DebugRelocName::Section(id) => (section_map.get(&id).unwrap().1, 0),
62 DebugRelocName::Symbol(id) => {
63 let symbol_id = self.function_symbol(FuncId::from_u32(id.try_into().unwrap()));
65 .symbol_section_and_offset(symbol_id)
66 .expect("Debug reloc for undef sym???")
73 offset: u64::from(reloc.offset),
76 encoding: RelocationEncoding::Generic,
78 addend: i64::try_from(symbol_offset).unwrap() + reloc.addend,