Skip to content

Commit

Permalink
Issue gimli-rs#40 -- Use Gimli support for DWARF-in-PE.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Solberg committed Oct 17, 2018
1 parent d9435d5 commit 8c40f47
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use alloc::vec::Vec;
use std::slice;

use goblin::pe;
use scroll::Pread;

use {
Machine, Object, ObjectSection, ObjectSegment, SectionKind, Symbol, SymbolKind, SymbolMap,
Expand All @@ -13,7 +12,6 @@ use {
#[derive(Debug)]
pub struct PeFile<'data> {
pe: pe::PE<'data>,
symbol_table_offset: usize,
data: &'data [u8],
}

Expand Down Expand Up @@ -78,27 +76,7 @@ impl<'data> PeFile<'data> {
/// Parse the raw PE file data.
pub fn parse(data: &'data [u8]) -> Result<Self, &'static str> {
let pe = pe::PE::parse(data).map_err(|_| "Could not parse PE header")?;
// get offset to secret symbol table that follows the COFF symbol table
let symbol_table_offset = (
pe.header.coff_header.pointer_to_symbol_table +
pe.header.coff_header.number_of_symbol_table * 18
) as usize;
Ok(PeFile { pe, symbol_table_offset, data })
}

/// Get section name from special PE symbol table
fn get_section_name(
&self,
section: &'data pe::section_table::SectionTable,
) -> Option<&'data str> {
let mut name = section.name().ok()?;
// if name start with "/" then it's an index into the secret symbol table
let mut name_chars = name.chars();
if name_chars.next() == Some('/') {
let index = name_chars.as_str().parse::<usize>().ok()?;
name = self.data.pread(self.symbol_table_offset + index).ok()?;
};
Some(name)
Ok(PeFile { pe, data })
}
}

Expand Down Expand Up @@ -130,7 +108,7 @@ where

fn section_data_by_name(&self, section_name: &str) -> Option<Cow<'data, [u8]>> {
for section in &self.pe.sections {
if let Some(name) = self.get_section_name(&section) {
if let Ok(name) = section.name() {
if name == section_name {
return Some(Cow::from(
&self.data[section.pointer_to_raw_data as usize..]
Expand Down Expand Up @@ -181,7 +159,7 @@ where
fn has_debug_symbols(&self) -> bool {
// TODO: check if CodeView-in-PE still works
for section in &self.pe.sections {
if let Some(name) = self.get_section_name(&section) {
if let Ok(name) = section.name() {
if name == ".debug_info" {
return true;
}
Expand Down

0 comments on commit 8c40f47

Please sign in to comment.