Skip to content

Commit 94f11cc

Browse files
committed
Use pcrel pointers in .eh_frame
Fixes rust-lang#1055
1 parent a0f8765 commit 94f11cc

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/backend.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ impl WriteDebugInfo for ObjectProduct {
6868
.into_bytes();
6969

7070
let segment = self.object.segment_name(StandardSegment::Debug).to_vec();
71-
let section_id = self.object.add_section(segment, name, SectionKind::Debug);
72-
self.object.section_mut(section_id).set_data(data, 1);
71+
// FIXME use SHT_X86_64_UNWIND for .eh_frame
72+
let section_id = self.object.add_section(segment, name.clone(), if id == SectionId::EhFrame {
73+
SectionKind::ReadOnlyData
74+
} else {
75+
SectionKind::Debug
76+
});
77+
self.object.section_mut(section_id).set_data(data, if id == SectionId::EhFrame {
78+
8
79+
} else {
80+
1
81+
});
7382
let symbol_id = self.object.section_symbol(section_id);
7483
(section_id, symbol_id)
7584
}
@@ -95,7 +104,7 @@ impl WriteDebugInfo for ObjectProduct {
95104
Relocation {
96105
offset: u64::from(reloc.offset),
97106
symbol,
98-
kind: RelocationKind::Absolute,
107+
kind: reloc.kind,
99108
encoding: RelocationEncoding::Generic,
100109
size: reloc.size * 8,
101110
addend: i64::try_from(symbol_offset).unwrap() + reloc.addend,

src/debuginfo/emit.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(crate) struct DebugReloc {
4646
pub(crate) size: u8,
4747
pub(crate) name: DebugRelocName,
4848
pub(crate) addend: i64,
49+
pub(crate) kind: object::RelocationKind,
4950
}
5051

5152
#[derive(Clone)]
@@ -122,21 +123,21 @@ impl Writer for WriterRelocate {
122123
size,
123124
name: DebugRelocName::Symbol(symbol),
124125
addend: addend as i64,
126+
kind: object::RelocationKind::Absolute,
125127
});
126128
self.write_udata(0, size)
127129
}
128130
}
129131
}
130132

131-
// TODO: implement write_eh_pointer
132-
133133
fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
134134
let offset = self.len() as u32;
135135
self.relocs.push(DebugReloc {
136136
offset,
137137
size,
138138
name: DebugRelocName::Section(section),
139139
addend: val as i64,
140+
kind: object::RelocationKind::Absolute,
140141
});
141142
self.write_udata(0, size)
142143
}
@@ -153,7 +154,55 @@ impl Writer for WriterRelocate {
153154
size,
154155
name: DebugRelocName::Section(section),
155156
addend: val as i64,
157+
kind: object::RelocationKind::Absolute,
156158
});
157159
self.write_udata_at(offset, 0, size)
158160
}
161+
162+
fn write_eh_pointer(
163+
&mut self,
164+
address: Address,
165+
eh_pe: gimli::DwEhPe,
166+
size: u8,
167+
) -> Result<()> {
168+
match address {
169+
// Address::Constant arm copied from gimli
170+
Address::Constant(val) => {
171+
// Indirect doesn't matter here.
172+
let val = match eh_pe.application() {
173+
gimli::DW_EH_PE_absptr => val,
174+
gimli::DW_EH_PE_pcrel => {
175+
// TODO: better handling of sign
176+
let offset = self.len() as u64;
177+
offset.wrapping_sub(val)
178+
}
179+
_ => {
180+
return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
181+
}
182+
};
183+
self.write_eh_pointer_data(val, eh_pe.format(), size)
184+
}
185+
Address::Symbol { symbol, addend } => {
186+
match eh_pe.application() {
187+
gimli::DW_EH_PE_pcrel => {
188+
let size = match eh_pe.format() {
189+
gimli::DW_EH_PE_sdata4 => 4,
190+
_ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
191+
};
192+
self.relocs.push(DebugReloc {
193+
offset: self.len() as u32,
194+
size,
195+
name: DebugRelocName::Symbol(symbol),
196+
addend,
197+
kind: object::RelocationKind::Relative,
198+
});
199+
self.write_udata(0, size)
200+
}
201+
_ => {
202+
return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
203+
}
204+
}
205+
}
206+
}
207+
}
159208
}

src/debuginfo/unwind.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ impl<'tcx> UnwindContext<'tcx> {
1616
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
1717
let mut frame_table = FrameTable::default();
1818

19-
let cie_id = if let Some(cie) = isa.create_systemv_cie() {
19+
let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
20+
if isa.flags().is_pic() {
21+
cie.fde_address_encoding = gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
22+
}
2023
Some(frame_table.add_cie(cie))
2124
} else {
2225
None

0 commit comments

Comments
 (0)