1 //! Write the debuginfo into an object file.
3 use rustc_data_structures::fx::FxHashMap;
5 use gimli::write::{Address, AttributeValue, EndianVec, Result, Sections, Writer};
6 use gimli::{RunTimeEndian, SectionId};
8 use crate::backend::WriteDebugInfo;
10 use super::DebugContext;
12 impl DebugContext<'_> {
13 pub(crate) fn emit<P: WriteDebugInfo>(&mut self, product: &mut P) {
14 let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
15 let root = self.dwarf.unit.root();
16 let root = self.dwarf.unit.get_mut(root);
19 AttributeValue::RangeListRef(unit_range_list_id),
22 let mut sections = Sections::new(WriterRelocate::new(self.endian));
23 self.dwarf.write(&mut sections).unwrap();
25 let mut section_map = FxHashMap::default();
26 let _: Result<()> = sections.for_each_mut(|id, section| {
27 if !section.writer.slice().is_empty() {
28 let section_id = product.add_debug_section(id, section.writer.take());
29 section_map.insert(id, section_id);
34 let _: Result<()> = sections.for_each(|id, section| {
35 if let Some(section_id) = section_map.get(&id) {
36 for reloc in §ion.relocs {
37 product.add_debug_reloc(§ion_map, section_id, reloc);
46 pub(crate) struct DebugReloc {
47 pub(crate) offset: u32,
49 pub(crate) name: DebugRelocName,
50 pub(crate) addend: i64,
51 pub(crate) kind: object::RelocationKind,
55 pub(crate) enum DebugRelocName {
60 /// A [`Writer`] that collects all necessary relocations.
62 pub(super) struct WriterRelocate {
63 pub(super) relocs: Vec<DebugReloc>,
64 pub(super) writer: EndianVec<RunTimeEndian>,
68 pub(super) fn new(endian: RunTimeEndian) -> Self {
71 writer: EndianVec::new(endian),
75 /// Perform the collected relocations to be usable for JIT usage.
76 #[cfg(feature = "jit")]
77 pub(super) fn relocate_for_jit(
79 jit_module: &cranelift_simplejit::SimpleJITModule,
81 use std::convert::TryInto;
83 for reloc in self.relocs.drain(..) {
85 super::DebugRelocName::Section(_) => unreachable!(),
86 super::DebugRelocName::Symbol(sym) => {
87 let addr = jit_module.get_finalized_function(
88 cranelift_module::FuncId::from_u32(sym.try_into().unwrap()),
90 let val = (addr as u64 as i64 + reloc.addend) as u64;
92 .write_udata_at(reloc.offset as usize, val, reloc.size)
97 self.writer.into_vec()
101 impl Writer for WriterRelocate {
102 type Endian = RunTimeEndian;
104 fn endian(&self) -> Self::Endian {
108 fn len(&self) -> usize {
112 fn write(&mut self, bytes: &[u8]) -> Result<()> {
113 self.writer.write(bytes)
116 fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()> {
117 self.writer.write_at(offset, bytes)
120 fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
122 Address::Constant(val) => self.write_udata(val, size),
123 Address::Symbol { symbol, addend } => {
124 let offset = self.len() as u64;
125 self.relocs.push(DebugReloc {
126 offset: offset as u32,
128 name: DebugRelocName::Symbol(symbol),
129 addend: addend as i64,
130 kind: object::RelocationKind::Absolute,
132 self.write_udata(0, size)
137 fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
138 let offset = self.len() as u32;
139 self.relocs.push(DebugReloc {
142 name: DebugRelocName::Section(section),
144 kind: object::RelocationKind::Absolute,
146 self.write_udata(0, size)
156 self.relocs.push(DebugReloc {
157 offset: offset as u32,
159 name: DebugRelocName::Section(section),
161 kind: object::RelocationKind::Absolute,
163 self.write_udata_at(offset, 0, size)
166 fn write_eh_pointer(&mut self, address: Address, eh_pe: gimli::DwEhPe, size: u8) -> Result<()> {
168 // Address::Constant arm copied from gimli
169 Address::Constant(val) => {
170 // Indirect doesn't matter here.
171 let val = match eh_pe.application() {
172 gimli::DW_EH_PE_absptr => val,
173 gimli::DW_EH_PE_pcrel => {
174 // TODO: better handling of sign
175 let offset = self.len() as u64;
176 offset.wrapping_sub(val)
179 return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
182 self.write_eh_pointer_data(val, eh_pe.format(), size)
184 Address::Symbol { symbol, addend } => match eh_pe.application() {
185 gimli::DW_EH_PE_pcrel => {
186 let size = match eh_pe.format() {
187 gimli::DW_EH_PE_sdata4 => 4,
188 _ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
190 self.relocs.push(DebugReloc {
191 offset: self.len() as u32,
193 name: DebugRelocName::Symbol(symbol),
195 kind: object::RelocationKind::Relative,
197 self.write_udata(0, size)
199 _ => Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),