From 94f11cc3a886ef1a6b057463f300e96f1586ce78 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 23 Sep 2020 10:00:09 +0200 Subject: [PATCH] Use pcrel pointers in .eh_frame Fixes #1055 --- src/backend.rs | 15 +++++++++--- src/debuginfo/emit.rs | 53 +++++++++++++++++++++++++++++++++++++++-- src/debuginfo/unwind.rs | 5 +++- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index d9cb1d939f7..072fdc1f246 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -68,8 +68,17 @@ impl WriteDebugInfo for ObjectProduct { .into_bytes(); let segment = self.object.segment_name(StandardSegment::Debug).to_vec(); - let section_id = self.object.add_section(segment, name, SectionKind::Debug); - self.object.section_mut(section_id).set_data(data, 1); + // FIXME use SHT_X86_64_UNWIND for .eh_frame + let section_id = self.object.add_section(segment, name.clone(), if id == SectionId::EhFrame { + SectionKind::ReadOnlyData + } else { + SectionKind::Debug + }); + self.object.section_mut(section_id).set_data(data, if id == SectionId::EhFrame { + 8 + } else { + 1 + }); let symbol_id = self.object.section_symbol(section_id); (section_id, symbol_id) } @@ -95,7 +104,7 @@ impl WriteDebugInfo for ObjectProduct { Relocation { offset: u64::from(reloc.offset), symbol, - kind: RelocationKind::Absolute, + kind: reloc.kind, encoding: RelocationEncoding::Generic, size: reloc.size * 8, addend: i64::try_from(symbol_offset).unwrap() + reloc.addend, diff --git a/src/debuginfo/emit.rs b/src/debuginfo/emit.rs index 01cb6d3484d..22e284b9df4 100644 --- a/src/debuginfo/emit.rs +++ b/src/debuginfo/emit.rs @@ -46,6 +46,7 @@ pub(crate) struct DebugReloc { pub(crate) size: u8, pub(crate) name: DebugRelocName, pub(crate) addend: i64, + pub(crate) kind: object::RelocationKind, } #[derive(Clone)] @@ -122,14 +123,13 @@ impl Writer for WriterRelocate { size, name: DebugRelocName::Symbol(symbol), addend: addend as i64, + kind: object::RelocationKind::Absolute, }); self.write_udata(0, size) } } } - // TODO: implement write_eh_pointer - fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> { let offset = self.len() as u32; self.relocs.push(DebugReloc { @@ -137,6 +137,7 @@ impl Writer for WriterRelocate { size, name: DebugRelocName::Section(section), addend: val as i64, + kind: object::RelocationKind::Absolute, }); self.write_udata(0, size) } @@ -153,7 +154,55 @@ impl Writer for WriterRelocate { size, name: DebugRelocName::Section(section), addend: val as i64, + kind: object::RelocationKind::Absolute, }); self.write_udata_at(offset, 0, size) } + + fn write_eh_pointer( + &mut self, + address: Address, + eh_pe: gimli::DwEhPe, + size: u8, + ) -> Result<()> { + match address { + // Address::Constant arm copied from gimli + Address::Constant(val) => { + // Indirect doesn't matter here. + let val = match eh_pe.application() { + gimli::DW_EH_PE_absptr => val, + gimli::DW_EH_PE_pcrel => { + // TODO: better handling of sign + let offset = self.len() as u64; + offset.wrapping_sub(val) + } + _ => { + return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)); + } + }; + self.write_eh_pointer_data(val, eh_pe.format(), size) + } + Address::Symbol { symbol, addend } => { + match eh_pe.application() { + gimli::DW_EH_PE_pcrel => { + let size = match eh_pe.format() { + gimli::DW_EH_PE_sdata4 => 4, + _ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)), + }; + self.relocs.push(DebugReloc { + offset: self.len() as u32, + size, + name: DebugRelocName::Symbol(symbol), + addend, + kind: object::RelocationKind::Relative, + }); + self.write_udata(0, size) + } + _ => { + return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)); + } + } + } + } + } } diff --git a/src/debuginfo/unwind.rs b/src/debuginfo/unwind.rs index 4c23f75be69..39a951e5eca 100644 --- a/src/debuginfo/unwind.rs +++ b/src/debuginfo/unwind.rs @@ -16,7 +16,10 @@ impl<'tcx> UnwindContext<'tcx> { pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self { let mut frame_table = FrameTable::default(); - let cie_id = if let Some(cie) = isa.create_systemv_cie() { + let cie_id = if let Some(mut cie) = isa.create_systemv_cie() { + if isa.flags().is_pic() { + cie.fde_address_encoding = gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0); + } Some(frame_table.add_cie(cie)) } else { None