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

read: add Object::relative_address_base #315

Merged
merged 1 commit into from
Jun 1, 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
2 changes: 2 additions & 0 deletions examples/objdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ fn dump_parsed_object(file: &object::File) {
);
println!("Architecture: {:?}", file.architecture());
println!("Flags: {:x?}", file.flags());
println!("Relative Address Base: {:x?}", file.relative_address_base());
println!("Entry Address: {:x?}", file.entry());

match file.mach_uuid() {
Ok(Some(uuid)) => println!("Mach UUID: {:x?}", uuid),
Expand Down
4 changes: 4 additions & 0 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ where
with_inner!(self.inner, FileInternal, |x| x.pdb_info())
}

fn relative_address_base(&self) -> u64 {
with_inner!(self.inner, FileInternal, |x| x.relative_address_base())
}

fn entry(&self) -> u64 {
with_inner!(self.inner, FileInternal, |x| x.entry())
}
Expand Down
4 changes: 4 additions & 0 deletions src/read/coff/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ where
self.section_by_name(".debug_info").is_some()
}

fn relative_address_base(&self) -> u64 {
0
}

#[inline]
fn entry(&self) -> u64 {
0
Expand Down
4 changes: 4 additions & 0 deletions src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ where
Ok(Some((filename, build_id)))
}

fn relative_address_base(&self) -> u64 {
0
}

fn entry(&self) -> u64 {
self.header.e_entry(self.endian).into()
}
Expand Down
4 changes: 4 additions & 0 deletions src/read/macho/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ where
self.header.uuid(self.endian, self.data, self.header_offset)
}

fn relative_address_base(&self) -> u64 {
0
}

fn entry(&self) -> u64 {
if let Ok(mut commands) =
self.header
Expand Down
2 changes: 1 addition & 1 deletion src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl<'data> Export<'data> {
self.name.0
}

/// The symbol address.
/// The virtual address of the symbol.
#[inline]
pub fn address(&self) -> u64 {
self.address
Expand Down
5 changes: 5 additions & 0 deletions src/read/pe/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,13 @@ where
self.section_by_name(".debug_info").is_some()
}

fn relative_address_base(&self) -> u64 {
self.common.image_base
}

fn entry(&self) -> u64 {
u64::from(self.nt_headers.optional_header().address_of_entry_point())
.wrapping_add(self.common.image_base)
}

fn flags(&self) -> FileFlags {
Expand Down
4 changes: 2 additions & 2 deletions src/read/pe/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
{
#[inline]
fn address(&self) -> u64 {
u64::from(self.section.virtual_address.get(LE))
u64::from(self.section.virtual_address.get(LE)).wrapping_add(self.file.common.image_base)
}

#[inline]
Expand Down Expand Up @@ -218,7 +218,7 @@ where

#[inline]
fn address(&self) -> u64 {
u64::from(self.section.virtual_address.get(LE))
u64::from(self.section.virtual_address.get(LE)).wrapping_add(self.file.common.image_base)
}

#[inline]
Expand Down
11 changes: 8 additions & 3 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ pub trait Object<'data: 'file, 'file>: read::private::Sealed {
/// Get an iterator over the segments in the file.
fn segments(&'file self) -> Self::SegmentIterator;

/// Get the entry point address of the binary
fn entry(&'file self) -> u64;

/// Get the section named `section_name`, if such a section exists.
///
/// If `section_name` starts with a '.' then it is treated as a system section name,
Expand Down Expand Up @@ -205,6 +202,14 @@ pub trait Object<'data: 'file, 'file>: read::private::Sealed {
Ok(None)
}

/// Get the base address used for relative virtual addresses.
///
/// Currently this is only non-zero for PE.
fn relative_address_base(&'file self) -> u64;

/// Get the virtual address of the entry point of the binary
fn entry(&'file self) -> u64;

/// File flags that are specific to each file format.
fn flags(&self) -> FileFlags;
}
Expand Down
14 changes: 9 additions & 5 deletions src/read/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,6 @@ where
WasmSegmentIterator { file: self }
}

#[inline]
fn entry(&'file self) -> u64 {
self.entry
}

fn section_by_name(&'file self, section_name: &str) -> Option<WasmSection<'data, 'file, R>> {
self.sections()
.find(|section| section.name() == Ok(section_name))
Expand Down Expand Up @@ -416,6 +411,15 @@ where
self.has_debug_symbols
}

fn relative_address_base(&self) -> u64 {
0
}

#[inline]
fn entry(&'file self) -> u64 {
self.entry
}

#[inline]
fn flags(&self) -> FileFlags {
FileFlags::None
Expand Down