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);
17 root.set(gimli::DW_AT_ranges, AttributeValue::RangeListRef(unit_range_list_id));
19 let mut sections = Sections::new(WriterRelocate::new(self.endian));
20 self.dwarf.write(&mut sections).unwrap();
22 let mut section_map = FxHashMap::default();
23 let _: Result<()> = sections.for_each_mut(|id, section| {
24 if !section.writer.slice().is_empty() {
25 let section_id = product.add_debug_section(id, section.writer.take());
26 section_map.insert(id, section_id);
31 let _: Result<()> = sections.for_each(|id, section| {
32 if let Some(section_id) = section_map.get(&id) {
33 for reloc in §ion.relocs {
34 product.add_debug_reloc(§ion_map, section_id, reloc);
43 pub(crate) struct DebugReloc {
44 pub(crate) offset: u32,
46 pub(crate) name: DebugRelocName,
47 pub(crate) addend: i64,
48 pub(crate) kind: object::RelocationKind,
52 pub(crate) enum DebugRelocName {
57 /// A [`Writer`] that collects all necessary relocations.
59 pub(super) struct WriterRelocate {
60 pub(super) relocs: Vec<DebugReloc>,
61 pub(super) writer: EndianVec<RunTimeEndian>,
65 pub(super) fn new(endian: RunTimeEndian) -> Self {
66 WriterRelocate { relocs: Vec::new(), writer: EndianVec::new(endian) }
69 /// Perform the collected relocations to be usable for JIT usage.
70 #[cfg(feature = "jit")]
71 pub(super) fn relocate_for_jit(mut self, jit_module: &cranelift_jit::JITModule) -> Vec<u8> {
72 use std::convert::TryInto;
74 for reloc in self.relocs.drain(..) {
76 super::DebugRelocName::Section(_) => unreachable!(),
77 super::DebugRelocName::Symbol(sym) => {
78 let addr = jit_module.get_finalized_function(
79 cranelift_module::FuncId::from_u32(sym.try_into().unwrap()),
81 let val = (addr as u64 as i64 + reloc.addend) as u64;
82 self.writer.write_udata_at(reloc.offset as usize, val, reloc.size).unwrap();
86 self.writer.into_vec()
90 impl Writer for WriterRelocate {
91 type Endian = RunTimeEndian;
93 fn endian(&self) -> Self::Endian {
97 fn len(&self) -> usize {
101 fn write(&mut self, bytes: &[u8]) -> Result<()> {
102 self.writer.write(bytes)
105 fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()> {
106 self.writer.write_at(offset, bytes)
109 fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
111 Address::Constant(val) => self.write_udata(val, size),
112 Address::Symbol { symbol, addend } => {
113 let offset = self.len() as u64;
114 self.relocs.push(DebugReloc {
115 offset: offset as u32,
117 name: DebugRelocName::Symbol(symbol),
118 addend: addend as i64,
119 kind: object::RelocationKind::Absolute,
121 self.write_udata(0, size)
126 fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
127 let offset = self.len() as u32;
128 self.relocs.push(DebugReloc {
131 name: DebugRelocName::Section(section),
133 kind: object::RelocationKind::Absolute,
135 self.write_udata(0, size)
145 self.relocs.push(DebugReloc {
146 offset: offset as u32,
148 name: DebugRelocName::Section(section),
150 kind: object::RelocationKind::Absolute,
152 self.write_udata_at(offset, 0, size)
155 fn write_eh_pointer(&mut self, address: Address, eh_pe: gimli::DwEhPe, size: u8) -> Result<()> {
157 // Address::Constant arm copied from gimli
158 Address::Constant(val) => {
159 // Indirect doesn't matter here.
160 let val = match eh_pe.application() {
161 gimli::DW_EH_PE_absptr => val,
162 gimli::DW_EH_PE_pcrel => {
163 // FIXME better handling of sign
164 let offset = self.len() as u64;
165 offset.wrapping_sub(val)
168 return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
171 self.write_eh_pointer_data(val, eh_pe.format(), size)
173 Address::Symbol { symbol, addend } => match eh_pe.application() {
174 gimli::DW_EH_PE_pcrel => {
175 let size = match eh_pe.format() {
176 gimli::DW_EH_PE_sdata4 => 4,
177 _ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
179 self.relocs.push(DebugReloc {
180 offset: self.len() as u32,
182 name: DebugRelocName::Symbol(symbol),
184 kind: object::RelocationKind::Relative,
186 self.write_udata(0, size)
188 _ => Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),