Skip to content

Support file hashes in .debug_line #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions src/debuginfo/line_info.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::ffi::OsStr;
use std::convert::TryFrom;
use std::path::{Component, Path};

use crate::prelude::*;

use rustc_span::{FileName, SourceFileAndLine, Pos};
use rustc_span::{FileName, SourceFile, SourceFileAndLine, Pos, SourceFileHash, SourceFileHashAlgorithm};

use cranelift_codegen::binemit::CodeOffset;

use gimli::write::{
Address, AttributeValue, FileId, LineProgram, LineString, LineStringTable, UnitEntryId,
Address, AttributeValue, FileId, LineProgram, LineString, FileInfo, LineStringTable, UnitEntryId,
};

// OPTIMIZATION: It is cheaper to do this in one pass than using `.parent()` and `.file_name()`.
Expand All @@ -35,12 +36,39 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
}
}

pub(crate) const MD5_LEN: usize = 16;

#[derive(Default, Clone, Copy)]
pub struct FileHash([u8; MD5_LEN]);

impl FileHash {
pub fn inner(self) -> [u8; MD5_LEN] {
self.0
}
}

pub struct UnsupportedHashType;

impl TryFrom<SourceFileHash> for FileHash {
type Error = UnsupportedHashType;

fn try_from(hash: SourceFileHash) -> Result<Self, Self::Error> {
if hash.kind == SourceFileHashAlgorithm::Md5 {
let mut buf = [0u8; MD5_LEN];
buf.copy_from_slice(hash.hash_bytes());
Ok(Self(buf))
} else {
Err(UnsupportedHashType)
}
}
}

fn line_program_add_file(
line_program: &mut LineProgram,
line_strings: &mut LineStringTable,
file: &FileName,
file: &SourceFile,
) -> FileId {
match file {
match &file.name {
FileName::Real(path) => {
let (dir_path, file_name) = split_path_dir_and_file(path);
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
Expand All @@ -57,13 +85,21 @@ fn line_program_add_file(
line_program.encoding(),
line_strings,
);
line_program.add_file(file_name, dir_id, None)

let file_hash = FileHash::try_from(file.src_hash);

line_program.file_has_md5 = file_hash.is_ok();
line_program.add_file(file_name, dir_id, Some(FileInfo {
timestamp: 0,
size: 0,
md5: file_hash.unwrap_or_default().inner(),
}))
}
// FIXME give more appropriate file names
_ => {
filename => {
let dir_id = line_program.default_directory();
let dummy_file_name = LineString::new(
file.to_string().into_bytes(),
filename.to_string().into_bytes(),
line_program.encoding(),
line_strings,
);
Expand All @@ -79,7 +115,7 @@ impl<'tcx> DebugContext<'tcx> {
let file_id = line_program_add_file(
&mut self.dwarf.unit.line_program,
&mut self.dwarf.line_strings,
&loc.file.name,
&loc.file,
);

let entry = self.dwarf.unit.get_mut(entry_id);
Expand Down Expand Up @@ -167,7 +203,7 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
true
};
if current_file_changed {
let file_id = line_program_add_file(line_program, line_strings, &file.name);
let file_id = line_program_add_file(line_program, line_strings, &file);
line_program.row().file = file_id;
last_file = Some(file.clone());
}
Expand Down
36 changes: 29 additions & 7 deletions src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
mod emit;
mod line_info;

use std::convert::TryFrom;

use crate::prelude::*;

use rustc_span::FileName;

use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::ValueLocRange;

use gimli::write::{
self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
LocationList, Range, RangeList, UnitEntryId, Writer,
LocationList, Range, RangeList, UnitEntryId, Writer, FileInfo,
};
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, X86_64};

Expand Down Expand Up @@ -42,7 +46,12 @@ impl<'tcx> DebugContext<'tcx> {
format: Format::Dwarf32,
// TODO: this should be configurable
// macOS doesn't seem to support DWARF > 3
version: 3,
// 5 version is required for md5 file hash
version: if tcx.sess.target.target.options.is_like_osx {
3
} else {
5
},
address_size,
};

Expand All @@ -52,18 +61,31 @@ impl<'tcx> DebugContext<'tcx> {
// Normally this would use option_env!("CFG_VERSION").
let producer = format!("cg_clif (rustc {})", "unknown version");
let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned();
let name = match tcx.sess.local_crate_source_file {
Some(ref path) => path.to_string_lossy().into_owned(),
None => tcx.crate_name(LOCAL_CRATE).to_string(),
let (name, file_hash) = match tcx.sess.local_crate_source_file.clone() {
Some(path) => {
let name = path.to_string_lossy().into_owned();
let hash = tcx.sess
.source_map()
.get_source_file(&FileName::Real(path))
.and_then(|f| line_info::FileHash::try_from(f.src_hash).ok());
(name, hash)
},
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
};

let line_program = LineProgram::new(
let mut line_program = LineProgram::new(
encoding,
LineEncoding::default(),
LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
None,
Some(FileInfo {
timestamp: 0,
size: 0,
md5: file_hash.unwrap_or_default().inner(),
}),
);
line_program.file_has_md5 = file_hash.is_some();

dwarf.unit.line_program = line_program;

{
Expand Down