-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathunstrip_libc.rs
More file actions
95 lines (74 loc) · 2.82 KB
/
unstrip_libc.rs
File metadata and controls
95 lines (74 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::elf;
use crate::libc_deb;
use crate::libc_version::LibcVersion;
use std::io::copy;
use std::io::stderr;
use std::io::stdout;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use std::process::ExitStatus;
use colored::Colorize;
use ex::fs::File;
use ex::io;
use snafu::ResultExt;
use snafu::Snafu;
use tempfile::TempDir;
use version_compare::Cmp;
#[derive(Debug, Snafu)]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("libc ELF parse error: {}", source))]
ElfParse { source: elf::parse::Error },
#[snafu(display("libc deb error: {}", source))]
Deb { source: libc_deb::Error },
#[snafu(display("failed creating temporary directory"))]
TmpDir { source: std::io::Error },
#[snafu(display("failed running eu-unstrip, please install elfutils: {}", source))]
CmdRun { source: std::io::Error },
#[snafu(display("eu-unstrip exited with failure: {}", status))]
CmdFail { status: ExitStatus },
#[snafu(display("failed to open symbol file: {}", source))]
SymOpen { source: io::Error },
#[snafu(display("failed to open libc file: {}", source))]
LibcOpen { source: io::Error },
#[snafu(display("failed writing symbols to libc file: {}", source))]
LibcWrite { source: std::io::Error },
}
pub type Result = std::result::Result<(), Error>;
/// Download debug symbols and apply them to a libc
fn do_unstrip_libc(libc: &Path, ver: &LibcVersion) -> Result {
println!("{}", "unstripping libc".yellow().bold());
let deb_file_name = format!("libc6-dbg_{}.deb", ver);
let tmp_dir = TempDir::new().context(TmpDirSnafu)?;
let sym_path = tmp_dir.path().join("libc-syms");
let name = if version_compare::compare_to(&ver.string_short, "2.23", Cmp::Lt).unwrap() {
format!("libc-{}.so", ver.string_short)
} else {
let build_id = elf::get_build_id(libc).context(ElfParseSnafu)?;
build_id.chars().skip(2).collect::<String>() + ".debug"
};
libc_deb::write_ubuntu_pkg_file(&deb_file_name, &name, &sym_path).context(DebSnafu)?;
let out = Command::new("eu-unstrip")
.arg(libc)
.arg(&sym_path)
.output()
.context(CmdRunSnafu)?;
let _ = stderr().write_all(&out.stderr);
let _ = stdout().write_all(&out.stdout);
if !out.status.success() {
return Err(Error::CmdFail { status: out.status });
}
let mut sym_file = File::open(sym_path).context(SymOpenSnafu)?;
let mut libc_file = File::create(libc).context(LibcOpenSnafu)?;
copy(&mut sym_file, &mut libc_file).context(LibcWriteSnafu)?;
Ok(())
}
/// Download debug symbols and apply them to a libc if it doesn't have them
/// already
pub fn unstrip_libc(libc: &Path, ver: &LibcVersion) -> Result {
if !elf::has_debug_syms(libc).context(ElfParseSnafu)? {
do_unstrip_libc(libc, ver)?;
}
Ok(())
}