Skip to content
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

Switch addr2line to use EndianReader. #213

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ backtrace = "0.3.13"
findshlibs = "0.8"
rustc-test = "0.3"
auxiliary = { path = "tests/auxiliary" }
typed-arena = "2"

[profile.release]
debug = true
Expand Down
53 changes: 50 additions & 3 deletions examples/addr2line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ extern crate fallible_iterator;
extern crate gimli;
extern crate memmap;
extern crate object;
extern crate typed_arena;

use std::borrow::Cow;
use std::fs::File;
use std::io::{BufRead, Lines, StdinLock, Write};
use std::path::Path;
use std::result;

use clap::{App, Arg, Values};
use fallible_iterator::FallibleIterator;
use object::Object;
use object::{Object, ObjectSection};
use typed_arena::Arena;

use addr2line::{Context, Location};

Expand All @@ -35,7 +38,7 @@ impl<'a> Iterator for Addrs<'a> {
fn next(&mut self) -> Option<u64> {
let text = match *self {
Addrs::Args(ref mut vals) => vals.next().map(Cow::from),
Addrs::Stdin(ref mut lines) => lines.next().map(Result::unwrap).map(Cow::from),
Addrs::Stdin(ref mut lines) => lines.next().map(result::Result::unwrap).map(Cow::from),
};
text.as_ref()
.map(Cow::as_ref)
Expand Down Expand Up @@ -75,6 +78,23 @@ fn print_function(name: &str, language: Option<gimli::DwLang>, demangle: bool) {
}
}

fn load_file_section<'input, 'arena, Endian: gimli::Endianity>(
id: gimli::SectionId,
file: &object::File<'input>,
endian: Endian,
arena_data: &'arena Arena<Cow<'input, [u8]>>,
) -> Result<gimli::EndianSlice<'arena, Endian>, ()> {
// TODO: Unify with dwarfdump.rs in gimli.
let name = id.name();
match file.section_by_name(name) {
Some(section) => match section.uncompressed_data().unwrap() {
Cow::Borrowed(b) => Ok(gimli::EndianSlice::new(b, endian)),
Cow::Owned(b) => Ok(gimli::EndianSlice::new(arena_data.alloc(b.into()), endian)),
},
None => Ok(gimli::EndianSlice::new(&[][..], endian)),
}
}

fn main() {
let matches = App::new("hardliner")
.version("0.1")
Expand Down Expand Up @@ -148,6 +168,8 @@ fn main() {
)
.get_matches();

let arena_data = Arena::new();

let do_functions = matches.is_present("functions");
let do_inlines = matches.is_present("inlines");
let pretty = matches.is_present("pretty");
Expand All @@ -161,6 +183,16 @@ fn main() {
let map = unsafe { memmap::Mmap::map(&file).unwrap() };
let object = &object::File::parse(&*map).unwrap();

let endian = if object.is_little_endian() {
gimli::RunTimeEndian::Little
} else {
gimli::RunTimeEndian::Big
};

let mut load_section = |id: gimli::SectionId| -> Result<_, _> {
load_file_section(id, object, endian, &arena_data)
};

let sup_map;
let sup_object = if let Some(sup_path) = matches.value_of("sup") {
let sup_file = File::open(sup_path).unwrap();
Expand All @@ -169,9 +201,24 @@ fn main() {
} else {
None
};
let mut load_sup_section = |id: gimli::SectionId| -> Result<_, _> {
if let Some(ref sup_object) = sup_object {
load_file_section(id, &sup_object, endian, &arena_data)
} else {
Ok(gimli::EndianSlice::new(&[][..], endian))
}
};

let symbols = object.symbol_map();
let ctx = Context::new_with_sup(object, sup_object.as_ref()).unwrap();
let dwarf = gimli::Dwarf::load(&mut load_section, &mut load_sup_section).unwrap();
let dwarf_sup = Some(
gimli::Dwarf::load(&mut load_sup_section, |_| -> Result<_, _> {
Ok(gimli::EndianSlice::new(&[][..], endian))
})
.unwrap(),
);

let ctx = Context::from_dwarf_with_sup(dwarf, dwarf_sup).unwrap();

let stdin = std::io::stdin();
let addrs = matches
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,11 @@ fn path_push(path: &mut String, p: &str) {
if has_unix_root(p) || has_windows_root(p) {
*path = p.to_string();
} else {
let dir_separator = if has_windows_root(path.as_str()) { '\\' } else { '/' };
let dir_separator = if has_windows_root(path.as_str()) {
'\\'
} else {
'/'
};

if !path.ends_with(dir_separator) {
path.push(dir_separator);
Expand All @@ -911,8 +915,7 @@ fn has_unix_root(p: &str) -> bool {

/// Check if the path in the given string has a windows style root
fn has_windows_root(p: &str) -> bool {
p.starts_with('\\')
|| p.get(1..3) == Some(":\\")
p.starts_with('\\') || p.get(1..3) == Some(":\\")
}

fn name_attr<R>(
Expand Down