Skip to content

Commit

Permalink
Merge pull request #219 from philipc/endian
Browse files Browse the repository at this point in the history
Add RunTimeEndian
  • Loading branch information
philipc committed Jul 13, 2017
2 parents 414f8a1 + c23c747 commit a2ef012
Show file tree
Hide file tree
Showing 19 changed files with 829 additions and 746 deletions.
53 changes: 26 additions & 27 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ extern crate gimli;
extern crate test;

use gimli::{DebugAbbrev, DebugAranges, DebugInfo, DebugLine, DebugLineOffset, DebugLoc,
DebugPubNames, DebugPubTypes, DebugRanges, Reader, EndianBuf, LittleEndian,
EntriesTreeIter};
DebugPubNames, DebugPubTypes, DebugRanges, Reader, LittleEndian, EntriesTreeIter};
use std::env;
use std::fs::File;
use std::io::Read;
Expand All @@ -27,7 +26,7 @@ pub fn read_section(section: &str) -> Vec<u8> {
#[bench]
fn bench_parsing_debug_abbrev(b: &mut test::Bencher) {
let debug_info = read_section("debug_info");
let debug_info = DebugInfo::<EndianBuf<LittleEndian>>::new(&debug_info);
let debug_info = DebugInfo::new(&debug_info, LittleEndian);
let unit = debug_info
.units()
.next()
Expand All @@ -37,7 +36,7 @@ fn bench_parsing_debug_abbrev(b: &mut test::Bencher) {
let debug_abbrev = read_section("debug_abbrev");

b.iter(|| {
let debug_abbrev = DebugAbbrev::<EndianBuf<LittleEndian>>::new(&debug_abbrev);
let debug_abbrev = DebugAbbrev::new(&debug_abbrev, LittleEndian);
test::black_box(unit.abbreviations(&debug_abbrev)
.expect("Should parse abbreviations"));
});
Expand All @@ -46,12 +45,12 @@ fn bench_parsing_debug_abbrev(b: &mut test::Bencher) {
#[bench]
fn bench_parsing_debug_info(b: &mut test::Bencher) {
let debug_abbrev = read_section("debug_abbrev");
let debug_abbrev = DebugAbbrev::<EndianBuf<LittleEndian>>::new(&debug_abbrev);
let debug_abbrev = DebugAbbrev::new(&debug_abbrev, LittleEndian);

let debug_info = read_section("debug_info");

b.iter(|| {
let debug_info = DebugInfo::<EndianBuf<LittleEndian>>::new(&debug_info);
let debug_info = DebugInfo::new(&debug_info, LittleEndian);

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
Expand All @@ -72,12 +71,12 @@ fn bench_parsing_debug_info(b: &mut test::Bencher) {
#[bench]
fn bench_parsing_debug_info_tree(b: &mut test::Bencher) {
let debug_abbrev = read_section("debug_abbrev");
let debug_abbrev = DebugAbbrev::<EndianBuf<LittleEndian>>::new(&debug_abbrev);
let debug_abbrev = DebugAbbrev::new(&debug_abbrev, LittleEndian);

let debug_info = read_section("debug_info");

b.iter(|| {
let debug_info = DebugInfo::<EndianBuf<LittleEndian>>::new(&debug_info);
let debug_info = DebugInfo::new(&debug_info, LittleEndian);

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
Expand All @@ -91,7 +90,7 @@ fn bench_parsing_debug_info_tree(b: &mut test::Bencher) {
});
}

fn parse_debug_info_tree(mut iter: EntriesTreeIter<EndianBuf<LittleEndian>>) {
fn parse_debug_info_tree<R: Reader>(mut iter: EntriesTreeIter<R>) {
{
let entry = iter.entry().expect("Should have current entry");
let mut attrs = entry.attrs();
Expand All @@ -107,7 +106,7 @@ fn parse_debug_info_tree(mut iter: EntriesTreeIter<EndianBuf<LittleEndian>>) {
#[bench]
fn bench_parsing_debug_aranges(b: &mut test::Bencher) {
let debug_aranges = read_section("debug_aranges");
let debug_aranges = DebugAranges::<EndianBuf<LittleEndian>>::new(&debug_aranges);
let debug_aranges = DebugAranges::new(&debug_aranges, LittleEndian);

b.iter(|| {
let mut aranges = debug_aranges.items();
Expand All @@ -120,7 +119,7 @@ fn bench_parsing_debug_aranges(b: &mut test::Bencher) {
#[bench]
fn bench_parsing_debug_pubnames(b: &mut test::Bencher) {
let debug_pubnames = read_section("debug_pubnames");
let debug_pubnames = DebugPubNames::<EndianBuf<LittleEndian>>::new(&debug_pubnames);
let debug_pubnames = DebugPubNames::new(&debug_pubnames, LittleEndian);

b.iter(|| {
let mut pubnames = debug_pubnames.items();
Expand All @@ -133,7 +132,7 @@ fn bench_parsing_debug_pubnames(b: &mut test::Bencher) {
#[bench]
fn bench_parsing_debug_pubtypes(b: &mut test::Bencher) {
let debug_pubtypes = read_section("debug_pubtypes");
let debug_pubtypes = DebugPubTypes::<EndianBuf<LittleEndian>>::new(&debug_pubtypes);
let debug_pubtypes = DebugPubTypes::new(&debug_pubtypes, LittleEndian);

b.iter(|| {
let mut pubtypes = debug_pubtypes.items();
Expand All @@ -152,7 +151,7 @@ const ADDRESS_SIZE: u8 = 8;
#[bench]
fn bench_parsing_line_number_program_opcodes(b: &mut test::Bencher) {
let debug_line = read_section("debug_line");
let debug_line = DebugLine::<EndianBuf<LittleEndian>>::new(&debug_line);
let debug_line = DebugLine::new(&debug_line, LittleEndian);

b.iter(|| {
let program = debug_line
Expand All @@ -170,7 +169,7 @@ fn bench_parsing_line_number_program_opcodes(b: &mut test::Bencher) {
#[bench]
fn bench_executing_line_number_programs(b: &mut test::Bencher) {
let debug_line = read_section("debug_line");
let debug_line = DebugLine::<EndianBuf<LittleEndian>>::new(&debug_line);
let debug_line = DebugLine::new(&debug_line, LittleEndian);

b.iter(|| {
let program = debug_line
Expand All @@ -189,13 +188,13 @@ fn bench_executing_line_number_programs(b: &mut test::Bencher) {
#[bench]
fn bench_parsing_debug_loc(b: &mut test::Bencher) {
let debug_info = read_section("debug_info");
let debug_info = DebugInfo::<EndianBuf<LittleEndian>>::new(&debug_info);
let debug_info = DebugInfo::new(&debug_info, LittleEndian);

let debug_abbrev = read_section("debug_abbrev");
let debug_abbrev = DebugAbbrev::<EndianBuf<LittleEndian>>::new(&debug_abbrev);
let debug_abbrev = DebugAbbrev::new(&debug_abbrev, LittleEndian);

let debug_loc = read_section("debug_loc");
let debug_loc = DebugLoc::<EndianBuf<LittleEndian>>::new(&debug_loc);
let debug_loc = DebugLoc::new(&debug_loc, LittleEndian);

let mut offsets = Vec::new();

Expand Down Expand Up @@ -243,13 +242,13 @@ fn bench_parsing_debug_loc(b: &mut test::Bencher) {
#[bench]
fn bench_parsing_debug_ranges(b: &mut test::Bencher) {
let debug_info = read_section("debug_info");
let debug_info = DebugInfo::<EndianBuf<LittleEndian>>::new(&debug_info);
let debug_info = DebugInfo::new(&debug_info, LittleEndian);

let debug_abbrev = read_section("debug_abbrev");
let debug_abbrev = DebugAbbrev::<EndianBuf<LittleEndian>>::new(&debug_abbrev);
let debug_abbrev = DebugAbbrev::new(&debug_abbrev, LittleEndian);

let debug_ranges = read_section("debug_ranges");
let debug_ranges = DebugRanges::<EndianBuf<LittleEndian>>::new(&debug_ranges);
let debug_ranges = DebugRanges::new(&debug_ranges, LittleEndian);

let mut offsets = Vec::new();

Expand Down Expand Up @@ -310,7 +309,7 @@ mod cfi {
#[bench]
fn iterate_entries_and_do_not_parse_any_fde(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);

let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0);

Expand All @@ -325,7 +324,7 @@ mod cfi {
#[bench]
fn iterate_entries_and_parse_every_fde(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);

let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0);

Expand All @@ -350,7 +349,7 @@ mod cfi {
#[bench]
fn iterate_entries_and_parse_every_fde_and_instructions(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);

let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0);

Expand Down Expand Up @@ -385,7 +384,7 @@ mod cfi {
#[bench]
fn iterate_entries_evaluate_every_fde(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);

let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0);

Expand Down Expand Up @@ -463,7 +462,7 @@ mod cfi {
#[bench]
fn parse_longest_fde_instructions(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);
let fde = get_fde_with_longest_cfi_instructions(&eh_frame);

b.iter(|| {
Expand All @@ -477,7 +476,7 @@ mod cfi {
#[bench]
fn eval_longest_fde_instructions_new_ctx_everytime(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);
let fde = get_fde_with_longest_cfi_instructions(&eh_frame);

b.iter(|| {
Expand All @@ -495,7 +494,7 @@ mod cfi {
#[bench]
fn eval_longest_fde_instructions_same_ctx(b: &mut test::Bencher) {
let eh_frame = read_section("eh_frame");
let eh_frame = EhFrame::<EndianBuf<LittleEndian>>::new(&eh_frame);
let eh_frame = EhFrame::new(&eh_frame, LittleEndian);
let fde = get_fde_with_longest_cfi_instructions(&eh_frame);

let mut ctx = Some(UninitializedUnwindContext::new());
Expand Down
56 changes: 35 additions & 21 deletions examples/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,39 @@ fn main() {
let file = object::File::parse(unsafe { file.as_slice() })
.expect("Should parse object file");

if file.is_little_endian() {
dump_file::<gimli::LittleEndian>(&file, &flags);
let endian = if file.is_little_endian() {
gimli::RunTimeEndian::Little
} else {
dump_file::<gimli::BigEndian>(&file, &flags);
}
gimli::RunTimeEndian::Big
};
dump_file(&file, endian, &flags);
}
}

fn dump_file<Endian>(file: &object::File, flags: &Flags)
fn dump_file<Endian>(file: &object::File, endian: Endian, flags: &Flags)
where Endian: gimli::Endianity
{
fn load_section<'input, 'file, S, Endian>(file: &'file object::File<'input>) -> S
fn load_section<'input, 'file, S, Endian>(file: &'file object::File<'input>,
endian: Endian)
-> S
where S: gimli::Section<gimli::EndianBuf<'input, Endian>>,
Endian: gimli::Endianity,
'file: 'input
{
let data = file.get_section(S::section_name()).unwrap_or(&[]);
S::from(gimli::EndianBuf::new(data))
S::from(gimli::EndianBuf::new(data, endian))
}

let debug_abbrev: &gimli::DebugAbbrev<_> = &load_section::<_, Endian>(file);
let debug_aranges: &gimli::DebugAranges<_> = &load_section(file);
let debug_info: &gimli::DebugInfo<_> = &load_section(file);
let debug_line: &gimli::DebugLine<_> = &load_section(file);
let debug_loc: &gimli::DebugLoc<_> = &load_section(file);
let debug_pubnames: &gimli::DebugPubNames<_> = &load_section(file);
let debug_pubtypes: &gimli::DebugPubTypes<_> = &load_section(file);
let debug_ranges: &gimli::DebugRanges<_> = &load_section(file);
let debug_str: &gimli::DebugStr<_> = &load_section(file);
let debug_types: &gimli::DebugTypes<_> = &load_section(file);
let debug_abbrev = &load_section(file, endian);
let debug_aranges = &load_section(file, endian);
let debug_info = &load_section(file, endian);
let debug_line = &load_section(file, endian);
let debug_loc = &load_section(file, endian);
let debug_pubnames = &load_section(file, endian);
let debug_pubtypes = &load_section(file, endian);
let debug_ranges = &load_section(file, endian);
let debug_str = &load_section(file, endian);
let debug_types = &load_section(file, endian);

if flags.info {
dump_info(debug_info,
Expand All @@ -134,13 +137,15 @@ fn dump_file<Endian>(file: &object::File, flags: &Flags)
debug_loc,
debug_ranges,
debug_str,
endian,
flags);
dump_types(debug_types,
debug_abbrev,
debug_line,
debug_loc,
debug_ranges,
debug_str,
endian,
flags);
println!("");
}
Expand All @@ -158,12 +163,14 @@ fn dump_file<Endian>(file: &object::File, flags: &Flags)
}
}

#[allow(too_many_arguments)]
fn dump_info<R: gimli::Reader>(debug_info: &gimli::DebugInfo<R>,
debug_abbrev: &gimli::DebugAbbrev<R>,
debug_line: &gimli::DebugLine<R>,
debug_loc: &gimli::DebugLoc<R>,
debug_ranges: &gimli::DebugRanges<R>,
debug_str: &gimli::DebugStr<R>,
endian: R::Endian,
flags: &Flags) {
println!("\n.debug_info");

Expand All @@ -180,16 +187,19 @@ fn dump_info<R: gimli::Reader>(debug_info: &gimli::DebugInfo<R>,
debug_loc,
debug_ranges,
debug_str,
endian,
flags);
}
}

#[allow(too_many_arguments)]
fn dump_types<R: gimli::Reader>(debug_types: &gimli::DebugTypes<R>,
debug_abbrev: &gimli::DebugAbbrev<R>,
debug_line: &gimli::DebugLine<R>,
debug_loc: &gimli::DebugLoc<R>,
debug_ranges: &gimli::DebugRanges<R>,
debug_str: &gimli::DebugStr<R>,
endian: R::Endian,
flags: &Flags) {
println!("\n.debug_types");

Expand All @@ -200,7 +210,7 @@ fn dump_types<R: gimli::Reader>(debug_types: &gimli::DebugTypes<R>,

println!("\nCU_HEADER:");
print!(" signature = ");
dump_type_signature::<R::Endian>(unit.type_signature());
dump_type_signature(unit.type_signature(), endian);
println!("");
println!(" typeoffset = 0x{:08x} {}",
unit.type_offset().0,
Expand All @@ -214,12 +224,14 @@ fn dump_types<R: gimli::Reader>(debug_types: &gimli::DebugTypes<R>,
debug_loc,
debug_ranges,
debug_str,
endian,
flags);
}
}

// TODO: most of this should be moved to the main library.
struct Unit<R: gimli::Reader> {
endian: R::Endian,
format: gimli::Format,
address_size: u8,
base_address: u64,
Expand All @@ -237,8 +249,10 @@ fn dump_entries<R: gimli::Reader>(offset: usize,
debug_loc: &gimli::DebugLoc<R>,
debug_ranges: &gimli::DebugRanges<R>,
debug_str: &gimli::DebugStr<R>,
endian: R::Endian,
flags: &Flags) {
let mut unit = Unit {
endian: endian,
format: format,
address_size: address_size,
base_address: 0,
Expand Down Expand Up @@ -421,7 +435,7 @@ fn dump_attr_value<R: gimli::Reader>(attr: &gimli::Attribute<R>,
dump_range_list(debug_ranges, offset, unit);
}
gimli::AttributeValue::DebugTypesRef(signature) => {
dump_type_signature::<R::Endian>(signature);
dump_type_signature(signature, unit.endian);
println!(" <type signature>");
}
gimli::AttributeValue::DebugStrRef(offset) => {
Expand Down Expand Up @@ -478,12 +492,12 @@ fn dump_attr_value<R: gimli::Reader>(attr: &gimli::Attribute<R>,
}
}

fn dump_type_signature<Endian>(signature: gimli::DebugTypeSignature)
fn dump_type_signature<Endian>(signature: gimli::DebugTypeSignature, endian: Endian)
where Endian: gimli::Endianity
{
// Convert back to bytes so we can match libdwarf-dwarfdump output.
let mut buf = [0; 8];
Endian::write_u64(&mut buf, signature.0);
endian.write_u64(&mut buf, signature.0);
print!("0x");
for byte in &buf {
print!("{:02x}", byte);
Expand Down
Loading

0 comments on commit a2ef012

Please sign in to comment.