Skip to content

Commit c92e941

Browse files
refactor: Make fetching of source info platform-specific (#1593)
Issue #1538
1 parent aa3b9e0 commit c92e941

7 files changed

Lines changed: 80 additions & 16 deletions

File tree

libwild/src/dwarf_address_info.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::platform::ObjectFile as _;
88
use crate::platform::Platform;
99
use crate::platform::Relocation;
1010
use crate::platform::RelocationSequence as _;
11+
use crate::platform::SourceInfo;
12+
use crate::platform::SourceInfoDetails;
1113
use anyhow::Context;
1214
use object::LittleEndian;
1315
use object::read::elf::Crel;
@@ -19,14 +21,6 @@ use std::os::unix::ffi::OsStrExt;
1921
use std::path::Path;
2022
use std::path::PathBuf;
2123

22-
pub(crate) struct SourceInfo(Option<SourceInfoDetails>);
23-
24-
#[derive(Debug)]
25-
pub(crate) struct SourceInfoDetails {
26-
path: PathBuf,
27-
line: u64,
28-
}
29-
3024
/// The address at which we'll pretend that we loaded the section we're interested in. This value is
3125
/// arbitrary, but should be larger than the largest input section we expect to encounter and small
3226
/// enough to fit comfortably in a u32.

libwild/src/elf_aarch64.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ impl<'data> crate::platform::Platform<'data> for ElfAArch64 {
290290
.symbol(symbol_index)
291291
.is_ok_and(|sym| (sym.st_other & object::elf::STO_AARCH64_VARIANT_PCS) != 0)
292292
}
293+
294+
fn get_source_info(
295+
object: &Self::File,
296+
relocations: &<Self::File as crate::platform::ObjectFile<'data>>::RelocationSections,
297+
section: &<Self::File as crate::platform::ObjectFile<'data>>::SectionHeader,
298+
offset_in_section: u64,
299+
) -> Result<crate::platform::SourceInfo> {
300+
crate::dwarf_address_info::get_source_info::<Self>(
301+
object,
302+
relocations,
303+
section,
304+
offset_in_section,
305+
)
306+
}
293307
}
294308

295309
#[derive(Debug, Clone)]

libwild/src/elf_loongarch64.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ impl<'data> crate::platform::Platform<'data> for ElfLoongArch64 {
154154

155155
None
156156
}
157+
158+
fn get_source_info(
159+
object: &Self::File,
160+
relocations: &<Self::File as crate::platform::ObjectFile<'data>>::RelocationSections,
161+
section: &<Self::File as crate::platform::ObjectFile<'data>>::SectionHeader,
162+
offset_in_section: u64,
163+
) -> Result<crate::platform::SourceInfo> {
164+
crate::dwarf_address_info::get_source_info::<Self>(
165+
object,
166+
relocations,
167+
section,
168+
offset_in_section,
169+
)
170+
}
157171
}
158172

159173
#[derive(Debug, Clone)]

libwild/src/elf_riscv64.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ impl<'data> crate::platform::Platform<'data> for ElfRiscV64 {
258258
),
259259
}
260260
}
261+
262+
fn get_source_info(
263+
object: &Self::File,
264+
relocations: &<Self::File as crate::platform::ObjectFile<'data>>::RelocationSections,
265+
section: &<Self::File as crate::platform::ObjectFile<'data>>::SectionHeader,
266+
offset_in_section: u64,
267+
) -> Result<crate::platform::SourceInfo> {
268+
crate::dwarf_address_info::get_source_info::<Self>(
269+
object,
270+
relocations,
271+
section,
272+
offset_in_section,
273+
)
274+
}
261275
}
262276

263277
#[derive(Debug, Clone)]

libwild/src/elf_x86_64.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,20 @@ impl<'data> crate::platform::Platform<'data> for ElfX86_64 {
398398
};
399399
None
400400
}
401+
402+
fn get_source_info(
403+
object: &Self::File,
404+
relocations: &<Self::File as crate::platform::ObjectFile<'data>>::RelocationSections,
405+
section: &<Self::File as crate::platform::ObjectFile<'data>>::SectionHeader,
406+
offset_in_section: u64,
407+
) -> Result<crate::platform::SourceInfo> {
408+
crate::dwarf_address_info::get_source_info::<Self>(
409+
object,
410+
relocations,
411+
section,
412+
offset_in_section,
413+
)
414+
}
401415
}
402416

403417
#[derive(Debug, Clone)]

libwild/src/layout.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,7 +2838,7 @@ fn process_relocation<
28382838
P: Platform<'data, File = crate::elf::File<'data>>,
28392839
R: Relocation,
28402840
>(
2841-
object: &ObjectLayoutState,
2841+
object: &ObjectLayoutState<'data>,
28422842
common: &mut CommonGroupState,
28432843
rel: &R,
28442844
section: &object::elf::SectionHeader64<LittleEndian>,
@@ -2970,13 +2970,9 @@ fn process_relocation<
29702970
symbol_db.output_kind,
29712971
) {
29722972
let symbol_name = symbol_db.symbol_name_for_display(symbol_id);
2973-
let source_info = crate::dwarf_address_info::get_source_info::<P>(
2974-
object.object,
2975-
&object.relocations,
2976-
section,
2977-
rel_offset,
2978-
)
2979-
.context("Failed to get source info")?;
2973+
let source_info =
2974+
P::get_source_info(object.object, &object.relocations, section, rel_offset)
2975+
.context("Failed to get source info")?;
29802976

29812977
if args.error_unresolved_symbols {
29822978
resources.report_error(error!(

libwild/src/platform.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use linker_utils::relaxation::RelocationModifier;
2020
use linker_utils::relaxation::SectionRelaxDeltas;
2121
use std::borrow::Cow;
2222
use std::ops::Range;
23+
use std::path::PathBuf;
2324

2425
/// Represents a supported object file format + architecture combination.
2526
pub(crate) trait Platform<'data>: 'data {
@@ -69,6 +70,15 @@ pub(crate) trait Platform<'data>: 'data {
6970
false
7071
}
7172

73+
/// Uses debug info, if available, to get information about where in the source code a
74+
/// particular offset in a particular section came from.
75+
fn get_source_info(
76+
object: &Self::File,
77+
relocations: &<Self::File as ObjectFile<'data>>::RelocationSections,
78+
section: &<Self::File as ObjectFile<'data>>::SectionHeader,
79+
offset_in_section: u64,
80+
) -> Result<SourceInfo>;
81+
7282
fn collect_relaxation_deltas(
7383
_section_output_address: u64,
7484
_section_bytes: &[u8],
@@ -480,3 +490,11 @@ pub(crate) trait SectionAttributes: std::fmt::Debug + Send + Sync + 'static {
480490

481491
fn apply(&self, output_sections: &mut OutputSections, section_id: OutputSectionId);
482492
}
493+
494+
pub(crate) struct SourceInfo(pub(crate) Option<SourceInfoDetails>);
495+
496+
#[derive(Debug)]
497+
pub(crate) struct SourceInfoDetails {
498+
pub(crate) path: PathBuf,
499+
pub(crate) line: u64,
500+
}

0 commit comments

Comments
 (0)