]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/debuginfo/emit.rs
Auto merge of #100942 - ehuss:update-cargo, r=ehuss
[rust.git] / compiler / rustc_codegen_cranelift / src / debuginfo / emit.rs
1 //! Write the debuginfo into an object file.
2
3 use cranelift_object::ObjectProduct;
4 use rustc_data_structures::fx::FxHashMap;
5
6 use gimli::write::{Address, AttributeValue, EndianVec, Result, Sections, Writer};
7 use gimli::{RunTimeEndian, SectionId};
8
9 use super::object::WriteDebugInfo;
10 use super::DebugContext;
11
12 impl DebugContext<'_> {
13     pub(crate) fn emit(&mut self, product: &mut ObjectProduct) {
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));
18
19         let mut sections = Sections::new(WriterRelocate::new(self.endian));
20         self.dwarf.write(&mut sections).unwrap();
21
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);
27             }
28             Ok(())
29         });
30
31         let _: Result<()> = sections.for_each(|id, section| {
32             if let Some(section_id) = section_map.get(&id) {
33                 for reloc in &section.relocs {
34                     product.add_debug_reloc(&section_map, section_id, reloc);
35                 }
36             }
37             Ok(())
38         });
39     }
40 }
41
42 #[derive(Clone)]
43 pub(crate) struct DebugReloc {
44     pub(crate) offset: u32,
45     pub(crate) size: u8,
46     pub(crate) name: DebugRelocName,
47     pub(crate) addend: i64,
48     pub(crate) kind: object::RelocationKind,
49 }
50
51 #[derive(Clone)]
52 pub(crate) enum DebugRelocName {
53     Section(SectionId),
54     Symbol(usize),
55 }
56
57 /// A [`Writer`] that collects all necessary relocations.
58 #[derive(Clone)]
59 pub(super) struct WriterRelocate {
60     pub(super) relocs: Vec<DebugReloc>,
61     pub(super) writer: EndianVec<RunTimeEndian>,
62 }
63
64 impl WriterRelocate {
65     pub(super) fn new(endian: RunTimeEndian) -> Self {
66         WriterRelocate { relocs: Vec::new(), writer: EndianVec::new(endian) }
67     }
68
69     /// Perform the collected relocations to be usable for JIT usage.
70     #[cfg(all(feature = "jit", not(windows)))]
71     pub(super) fn relocate_for_jit(mut self, jit_module: &cranelift_jit::JITModule) -> Vec<u8> {
72         for reloc in self.relocs.drain(..) {
73             match reloc.name {
74                 super::DebugRelocName::Section(_) => unreachable!(),
75                 super::DebugRelocName::Symbol(sym) => {
76                     let addr = jit_module.get_finalized_function(
77                         cranelift_module::FuncId::from_u32(sym.try_into().unwrap()),
78                     );
79                     let val = (addr as u64 as i64 + reloc.addend) as u64;
80                     self.writer.write_udata_at(reloc.offset as usize, val, reloc.size).unwrap();
81                 }
82             }
83         }
84         self.writer.into_vec()
85     }
86 }
87
88 impl Writer for WriterRelocate {
89     type Endian = RunTimeEndian;
90
91     fn endian(&self) -> Self::Endian {
92         self.writer.endian()
93     }
94
95     fn len(&self) -> usize {
96         self.writer.len()
97     }
98
99     fn write(&mut self, bytes: &[u8]) -> Result<()> {
100         self.writer.write(bytes)
101     }
102
103     fn write_at(&mut self, offset: usize, bytes: &[u8]) -> Result<()> {
104         self.writer.write_at(offset, bytes)
105     }
106
107     fn write_address(&mut self, address: Address, size: u8) -> Result<()> {
108         match address {
109             Address::Constant(val) => self.write_udata(val, size),
110             Address::Symbol { symbol, addend } => {
111                 let offset = self.len() as u64;
112                 self.relocs.push(DebugReloc {
113                     offset: offset as u32,
114                     size,
115                     name: DebugRelocName::Symbol(symbol),
116                     addend: addend as i64,
117                     kind: object::RelocationKind::Absolute,
118                 });
119                 self.write_udata(0, size)
120             }
121         }
122     }
123
124     fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
125         let offset = self.len() as u32;
126         self.relocs.push(DebugReloc {
127             offset,
128             size,
129             name: DebugRelocName::Section(section),
130             addend: val as i64,
131             kind: object::RelocationKind::Absolute,
132         });
133         self.write_udata(0, size)
134     }
135
136     fn write_offset_at(
137         &mut self,
138         offset: usize,
139         val: usize,
140         section: SectionId,
141         size: u8,
142     ) -> Result<()> {
143         self.relocs.push(DebugReloc {
144             offset: offset as u32,
145             size,
146             name: DebugRelocName::Section(section),
147             addend: val as i64,
148             kind: object::RelocationKind::Absolute,
149         });
150         self.write_udata_at(offset, 0, size)
151     }
152
153     fn write_eh_pointer(&mut self, address: Address, eh_pe: gimli::DwEhPe, size: u8) -> Result<()> {
154         match address {
155             // Address::Constant arm copied from gimli
156             Address::Constant(val) => {
157                 // Indirect doesn't matter here.
158                 let val = match eh_pe.application() {
159                     gimli::DW_EH_PE_absptr => val,
160                     gimli::DW_EH_PE_pcrel => {
161                         // FIXME better handling of sign
162                         let offset = self.len() as u64;
163                         offset.wrapping_sub(val)
164                     }
165                     _ => {
166                         return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe));
167                     }
168                 };
169                 self.write_eh_pointer_data(val, eh_pe.format(), size)
170             }
171             Address::Symbol { symbol, addend } => match eh_pe.application() {
172                 gimli::DW_EH_PE_pcrel => {
173                     let size = match eh_pe.format() {
174                         gimli::DW_EH_PE_sdata4 => 4,
175                         _ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
176                     };
177                     self.relocs.push(DebugReloc {
178                         offset: self.len() as u32,
179                         size,
180                         name: DebugRelocName::Symbol(symbol),
181                         addend,
182                         kind: object::RelocationKind::Relative,
183                     });
184                     self.write_udata(0, size)
185                 }
186                 _ => Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
187             },
188         }
189     }
190 }