From 2616477aa6b1c07cbbf5a9613e85b169c6f87bd2 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 16 Nov 2019 12:42:37 +0100 Subject: [PATCH 1/3] Fix dwarfdump machO relocations --- examples/dwarfdump.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/dwarfdump.rs b/examples/dwarfdump.rs index 74f5bd4a6..c0451ef79 100644 --- a/examples/dwarfdump.rs +++ b/examples/dwarfdump.rs @@ -160,8 +160,17 @@ fn add_relocations( match relocation.kind() { object::RelocationKind::Absolute => { if let Some(symbol) = file.symbol_by_index(relocation.symbol()) { - let addend = symbol.address().wrapping_add(relocation.addend() as u64); - relocation.set_addend(addend as i64); + match file.format() { + object::Format::Elf32 | object::Format::Elf64 => { + let addend = symbol.address().wrapping_add(relocation.addend() as u64); + relocation.set_addend(addend as i64); + } + object::Format::MachO32 | object::Format::MachO64 => {} + object::Format::Pe32 | object::Format::Pe64 | object::Format::Wasm => { + println!("File format {:?} is not yet supported.", file.format()); + std::process::exit(1); + } + } if relocations.insert(offset, relocation).is_some() { println!( "Multiple relocations for section {} at offset 0x{:08x}", @@ -935,7 +944,7 @@ fn dump_types( let unit = match dwarf.type_unit(header) { Ok(unit) => unit, Err(err) => { - writeln_error(w, dwarf, err.into(), "Failed to parse unit root entry")?; + writeln_error(w, dwarf, err.into(), "Failed to parse type unit root entry")?; continue; } }; @@ -1754,7 +1763,7 @@ fn dump_line(w: &mut W, dwarf: &gimli::Dwarf) -> Result< let unit = match dwarf.unit(header) { Ok(unit) => unit, Err(err) => { - writeln_error(w, dwarf, err.into(), "Failed to parse unit root entry")?; + writeln_error(w, dwarf, err.into(), "Failed to parse unit root entry for dump_line")?; continue; } }; From ada4d03882a9be5a4055f20add98c1b303755508 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 16 Nov 2019 14:58:31 +0100 Subject: [PATCH 2/3] Bump object to 0.15 --- Cargo.toml | 3 ++- examples/dwarfdump.rs | 48 ++++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef2a56509..80f3b9fce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,11 +28,12 @@ crossbeam = "0.7.1" getopts = "0.2" memmap = "0.7" num_cpus = "1" -object = "0.12" +object = "0.15" rayon = "1.0" regex = "1" test-assembler = "0.1.3" typed-arena = "1" +target-lexicon = "0.8" [features] read = [] diff --git a/examples/dwarfdump.rs b/examples/dwarfdump.rs index c0451ef79..0d865bd51 100644 --- a/examples/dwarfdump.rs +++ b/examples/dwarfdump.rs @@ -159,28 +159,24 @@ fn add_relocations( let offset = offset as usize; match relocation.kind() { object::RelocationKind::Absolute => { - if let Some(symbol) = file.symbol_by_index(relocation.symbol()) { - match file.format() { - object::Format::Elf32 | object::Format::Elf64 => { + match relocation.target() { + object::RelocationTarget::Symbol(symbol_idx) => { + if let Some(symbol) = file.symbol_by_index(symbol_idx) { let addend = symbol.address().wrapping_add(relocation.addend() as u64); relocation.set_addend(addend as i64); + } else { + println!( + "Relocation with invalid symbol for section {} at offset 0x{:08x}", + section.name().unwrap(), + offset + ); } - object::Format::MachO32 | object::Format::MachO64 => {} - object::Format::Pe32 | object::Format::Pe64 | object::Format::Wasm => { - println!("File format {:?} is not yet supported.", file.format()); - std::process::exit(1); - } - } - if relocations.insert(offset, relocation).is_some() { - println!( - "Multiple relocations for section {} at offset 0x{:08x}", - section.name().unwrap(), - offset - ); } - } else { + object::RelocationTarget::Section(_section_idx) => {} + } + if relocations.insert(offset, relocation).is_some() { println!( - "Relocation with invalid symbol for section {} at offset 0x{:08x}", + "Multiple relocations for section {} at offset 0x{:08x}", section.name().unwrap(), offset ); @@ -523,19 +519,19 @@ where let out = io::stdout(); if flags.eh_frame { // TODO: this might be better based on the file format. - let address_size = match file.machine() { - object::Machine::Arm | object::Machine::Mips | object::Machine::X86 => 4, - object::Machine::Arm64 | object::Machine::X86_64 => 8, - object::Machine::Other => mem::size_of::() as u8, - }; + let address_size = file + .architecture() + .pointer_width() + .map(|w| w.bytes()) + .unwrap_or(mem::size_of::() as u8); fn register_name_none(_: gimli::Register) -> Option<&'static str> { None } - let arch_register_name = match file.machine() { - object::Machine::Arm | object::Machine::Arm64 => gimli::Arm::register_name, - object::Machine::X86 => gimli::X86::register_name, - object::Machine::X86_64 => gimli::X86_64::register_name, + let arch_register_name = match file.architecture() { + target_lexicon::Architecture::Arm(_) | target_lexicon::Architecture::Aarch64(_) => gimli::Arm::register_name, + target_lexicon::Architecture::I386 | target_lexicon::Architecture::I586 | target_lexicon::Architecture::I686 => gimli::X86::register_name, + target_lexicon::Architecture::X86_64 => gimli::X86_64::register_name, _ => register_name_none, }; let register_name = |register| match arch_register_name(register) { From e5bcb30e98b31442da23277efc4379886260297a Mon Sep 17 00:00:00 2001 From: Philip Craig Date: Tue, 19 Nov 2019 16:44:21 +1000 Subject: [PATCH 3/3] Review fixes --- .travis.yml | 2 +- examples/dwarfdump.rs | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0d88ccf13..77159c347 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ os: osx_image: xcode7.1 rust: -- 1.34.0 +- 1.36.0 - nightly - beta - stable diff --git a/examples/dwarfdump.rs b/examples/dwarfdump.rs index 0d865bd51..e05594140 100644 --- a/examples/dwarfdump.rs +++ b/examples/dwarfdump.rs @@ -529,8 +529,12 @@ where None } let arch_register_name = match file.architecture() { - target_lexicon::Architecture::Arm(_) | target_lexicon::Architecture::Aarch64(_) => gimli::Arm::register_name, - target_lexicon::Architecture::I386 | target_lexicon::Architecture::I586 | target_lexicon::Architecture::I686 => gimli::X86::register_name, + target_lexicon::Architecture::Arm(_) | target_lexicon::Architecture::Aarch64(_) => { + gimli::Arm::register_name + } + target_lexicon::Architecture::I386 + | target_lexicon::Architecture::I586 + | target_lexicon::Architecture::I686 => gimli::X86::register_name, target_lexicon::Architecture::X86_64 => gimli::X86_64::register_name, _ => register_name_none, }; @@ -1759,7 +1763,12 @@ fn dump_line(w: &mut W, dwarf: &gimli::Dwarf) -> Result< let unit = match dwarf.unit(header) { Ok(unit) => unit, Err(err) => { - writeln_error(w, dwarf, err.into(), "Failed to parse unit root entry for dump_line")?; + writeln_error( + w, + dwarf, + err.into(), + "Failed to parse unit root entry for dump_line", + )?; continue; } };