Skip to content

Commit f086e7a

Browse files
committed
Allow debuginfo to reference global variables
1 parent 1f75f0f commit f086e7a

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

src/debuginfo/emit.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Write the debuginfo into an object file.
22
3+
use cranelift_module::{DataId, FuncId};
34
use cranelift_object::ObjectProduct;
45
use gimli::write::{Address, AttributeValue, EndianVec, Result, Sections, Writer};
56
use gimli::{RunTimeEndian, SectionId};
@@ -8,6 +9,19 @@ use rustc_data_structures::fx::FxHashMap;
89
use super::object::WriteDebugInfo;
910
use super::DebugContext;
1011

12+
pub(super) fn address_for_func(func_id: FuncId) -> Address {
13+
let symbol = func_id.as_u32();
14+
assert!(symbol & 1 << 31 == 0);
15+
Address::Symbol { symbol: symbol as usize, addend: 0 }
16+
}
17+
18+
#[allow(dead_code)]
19+
pub(super) fn address_for_data(data_id: DataId) -> Address {
20+
let symbol = data_id.as_u32();
21+
assert!(symbol & 1 << 31 == 0);
22+
Address::Symbol { symbol: (symbol | 1 << 31) as usize, addend: 0 }
23+
}
24+
1125
impl DebugContext {
1226
pub(crate) fn emit(&mut self, product: &mut ObjectProduct) {
1327
let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone());
@@ -171,6 +185,7 @@ impl Writer for WriterRelocate {
171185
gimli::DW_EH_PE_pcrel => {
172186
let size = match eh_pe.format() {
173187
gimli::DW_EH_PE_sdata4 => 4,
188+
gimli::DW_EH_PE_sdata8 => 8,
174189
_ => return Err(gimli::write::Error::UnsupportedPointerEncoding(eh_pe)),
175190
};
176191
self.relocs.push(DebugReloc {

src/debuginfo/line_info.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use std::path::{Component, Path};
55

66
use cranelift_codegen::binemit::CodeOffset;
77
use cranelift_codegen::MachSrcLoc;
8-
use gimli::write::{
9-
Address, AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable,
10-
};
8+
use gimli::write::{AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable};
119
use rustc_span::{
1210
FileName, Pos, SourceFile, SourceFileAndLine, SourceFileHash, SourceFileHashAlgorithm,
1311
};
1412

13+
use crate::debuginfo::emit::address_for_func;
1514
use crate::debuginfo::FunctionDebugContext;
1615
use crate::prelude::*;
1716

@@ -143,7 +142,7 @@ impl FunctionDebugContext {
143142
pub(super) fn create_debug_lines(
144143
&mut self,
145144
debug_context: &mut DebugContext,
146-
symbol: usize,
145+
func_id: FuncId,
147146
context: &Context,
148147
) -> CodeOffset {
149148
let create_row_for_span =
@@ -156,11 +155,7 @@ impl FunctionDebugContext {
156155
debug_context.dwarf.unit.line_program.generate_row();
157156
};
158157

159-
debug_context
160-
.dwarf
161-
.unit
162-
.line_program
163-
.begin_sequence(Some(Address::Symbol { symbol, addend: 0 }));
158+
debug_context.dwarf.unit.line_program.begin_sequence(Some(address_for_func(func_id)));
164159

165160
let mut func_end = 0;
166161

@@ -183,10 +178,7 @@ impl FunctionDebugContext {
183178
assert_ne!(func_end, 0);
184179

185180
let entry = debug_context.dwarf.unit.get_mut(self.entry_id);
186-
entry.set(
187-
gimli::DW_AT_low_pc,
188-
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
189-
);
181+
entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
190182
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end)));
191183

192184
func_end

src/debuginfo/mod.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_span::{SourceFileHash, StableSourceFileId};
2020

2121
pub(crate) use self::emit::{DebugReloc, DebugRelocName};
2222
pub(crate) use self::unwind::UnwindContext;
23+
use crate::debuginfo::emit::address_for_func;
2324
use crate::prelude::*;
2425

2526
pub(crate) fn producer(sess: &Session) -> String {
@@ -174,7 +175,6 @@ impl DebugContext {
174175
) -> FunctionDebugContext {
175176
let (file_id, line, column) = self.get_span_loc(tcx, function_span, function_span);
176177

177-
// FIXME: add to appropriate scope instead of root
178178
let scope = self.item_namespace(tcx, tcx.parent(instance.def_id()));
179179

180180
let mut name = String::new();
@@ -240,21 +240,16 @@ impl FunctionDebugContext {
240240
func_id: FuncId,
241241
context: &Context,
242242
) {
243-
let symbol = func_id.as_u32() as usize;
243+
let end = self.create_debug_lines(debug_context, func_id, context);
244244

245-
let end = self.create_debug_lines(debug_context, symbol, context);
246-
247-
debug_context.unit_range_list.0.push(Range::StartLength {
248-
begin: Address::Symbol { symbol, addend: 0 },
249-
length: u64::from(end),
250-
});
245+
debug_context
246+
.unit_range_list
247+
.0
248+
.push(Range::StartLength { begin: address_for_func(func_id), length: u64::from(end) });
251249

252250
let func_entry = debug_context.dwarf.unit.get_mut(self.entry_id);
253251
// Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped.
254-
func_entry.set(
255-
gimli::DW_AT_low_pc,
256-
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
257-
);
252+
func_entry.set(gimli::DW_AT_low_pc, AttributeValue::Address(address_for_func(func_id)));
258253
// Using Udata for DW_AT_high_pc requires at least DWARF4
259254
func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end)));
260255
}

src/debuginfo/object.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cranelift_module::FuncId;
1+
use cranelift_module::{DataId, FuncId};
22
use cranelift_object::ObjectProduct;
33
use gimli::SectionId;
44
use object::write::{Relocation, StandardSegment};
@@ -57,10 +57,13 @@ impl WriteDebugInfo for ObjectProduct {
5757
let (symbol, symbol_offset) = match reloc.name {
5858
DebugRelocName::Section(id) => (section_map.get(&id).unwrap().1, 0),
5959
DebugRelocName::Symbol(id) => {
60-
let symbol_id = self.function_symbol(FuncId::from_u32(id.try_into().unwrap()));
61-
self.object
62-
.symbol_section_and_offset(symbol_id)
63-
.expect("Debug reloc for undef sym???")
60+
let id = id.try_into().unwrap();
61+
let symbol_id = if id & 1 << 31 == 0 {
62+
self.function_symbol(FuncId::from_u32(id))
63+
} else {
64+
self.data_symbol(DataId::from_u32(id & !(1 << 31)))
65+
};
66+
self.object.symbol_section_and_offset(symbol_id).unwrap_or((symbol_id, 0))
6467
}
6568
};
6669
self.object

src/debuginfo/unwind.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
use cranelift_codegen::ir::Endianness;
44
use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
55
use cranelift_object::ObjectProduct;
6-
use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
6+
use gimli::write::{CieId, EhFrame, FrameTable, Section};
77
use gimli::RunTimeEndian;
88

9+
use super::emit::address_for_func;
910
use super::object::WriteDebugInfo;
1011
use crate::prelude::*;
1112

@@ -47,11 +48,8 @@ impl UnwindContext {
4748

4849
match unwind_info {
4950
UnwindInfo::SystemV(unwind_info) => {
50-
self.frame_table.add_fde(
51-
self.cie_id.unwrap(),
52-
unwind_info
53-
.to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
54-
);
51+
self.frame_table
52+
.add_fde(self.cie_id.unwrap(), unwind_info.to_fde(address_for_func(func_id)));
5553
}
5654
UnwindInfo::WindowsX64(_) => {
5755
// FIXME implement this

0 commit comments

Comments
 (0)