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 ObjectSection::dynamic_relocations #729

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions crates/examples/src/objdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ fn dump_parsed_object<W: Write, E: Write>(w: &mut W, e: &mut E, file: &object::F
writeln!(w, "{:x?}", relocation)?;
}
}
if section.dynamic_relocations().next().is_some() {
writeln!(
w,
"\n{} dynamic relocations",
section.name().unwrap_or("<invalid name>")
)?;
for relocation in section.dynamic_relocations() {
writeln!(w, "{:x?}", relocation)?;
}
}
}

writeln!(w)?;
Expand Down
7 changes: 7 additions & 0 deletions crates/examples/testfiles/elf/base-aarch64.objdump
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ Symbols
86: Symbol { name: "printf@@GLIBC_2.17", address: 0, size: 0, kind: Text, section: Undefined, scope: Unknown, weak: false, flags: Elf { st_info: 12, st_other: 0 } }
87: Symbol { name: "_init", address: 598, size: 0, kind: Text, section: Section(SectionIndex(b)), scope: Dynamic, weak: false, flags: Elf { st_info: 12, st_other: 0 } }

.got dynamic relocations
(10f98, Relocation { kind: Unknown, encoding: Generic, size: 0, target: Symbol(SymbolIndex(4)), addend: 0, implicit_addend: false, flags: Elf { r_type: 402 } })
(10fa0, Relocation { kind: Unknown, encoding: Generic, size: 0, target: Symbol(SymbolIndex(5)), addend: 0, implicit_addend: false, flags: Elf { r_type: 402 } })
(10fa8, Relocation { kind: Unknown, encoding: Generic, size: 0, target: Symbol(SymbolIndex(6)), addend: 0, implicit_addend: false, flags: Elf { r_type: 402 } })
(10fb0, Relocation { kind: Unknown, encoding: Generic, size: 0, target: Symbol(SymbolIndex(7)), addend: 0, implicit_addend: false, flags: Elf { r_type: 402 } })
(10fb8, Relocation { kind: Unknown, encoding: Generic, size: 0, target: Symbol(SymbolIndex(9)), addend: 0, implicit_addend: false, flags: Elf { r_type: 402 } })

Dynamic symbols
1: Symbol { name: "", address: 598, size: 0, kind: Section, section: Section(SectionIndex(b)), scope: Compilation, weak: false, flags: Elf { st_info: 3, st_other: 0 } }
2: Symbol { name: "", address: 11000, size: 0, kind: Section, section: Section(SectionIndex(15)), scope: Compilation, weak: false, flags: Elf { st_info: 3, st_other: 0 } }
Expand Down
3 changes: 3 additions & 0 deletions crates/examples/testfiles/elf/base.objdump
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ Symbols
62: Symbol { name: "__cxa_finalize@@GLIBC_2.2.5", address: 0, size: 0, kind: Text, section: Undefined, scope: Unknown, weak: true, flags: Elf { st_info: 22, st_other: 0 } }
63: Symbol { name: "_init", address: 520, size: 0, kind: Text, section: Section(SectionIndex(c)), scope: Dynamic, weak: false, flags: Elf { st_info: 12, st_other: 0 } }

.got dynamic relocations
(200fd0, Relocation { kind: Unknown, encoding: Generic, size: 0, target: Symbol(SymbolIndex(2)), addend: 0, implicit_addend: false, flags: Elf { r_type: 7 } })

Dynamic symbols
1: Symbol { name: "_ITM_deregisterTMCloneTable", address: 0, size: 0, kind: Unknown, section: Undefined, scope: Unknown, weak: true, flags: Elf { st_info: 20, st_other: 0 } }
2: Symbol { name: "printf", address: 0, size: 0, kind: Text, section: Undefined, scope: Unknown, weak: false, flags: Elf { st_info: 12, st_other: 0 } }
Expand Down
11 changes: 11 additions & 0 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,17 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for Section<'data, 'f
}
}

fn dynamic_relocations(&self) -> SectionRelocationIterator<'data, 'file, R> {
SectionRelocationIterator {
inner: map_inner!(
self.inner,
SectionInternal,
SectionRelocationIteratorInternal,
|x| x.dynamic_relocations()
),
}
}

fn relocation_map(&self) -> Result<RelocationMap> {
with_inner!(self.inner, SectionInternal, |x| x.relocation_map())
}
Expand Down
7 changes: 7 additions & 0 deletions src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSection<'data>
}
}

fn dynamic_relocations(&self) -> CoffRelocationIterator<'data, 'file, R, Coff> {
CoffRelocationIterator {
file: self.file,
iter: [].iter(),
}
}

fn relocation_map(&self) -> read::Result<RelocationMap> {
RelocationMap::new(self.file, self)
}
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 @@ -46,6 +46,7 @@ where
pub(super) segments: &'data [Elf::ProgramHeader],
pub(super) sections: SectionTable<'data, Elf, R>,
pub(super) relocations: RelocationSections,
pub(super) dynamic_relocations: RelocationSections,
pub(super) symbols: SymbolTable<'data, Elf, R>,
pub(super) dynamic_symbols: SymbolTable<'data, Elf, R>,
}
Expand All @@ -66,6 +67,8 @@ where
let dynamic_symbols = sections.symbols(endian, data, elf::SHT_DYNSYM)?;
// The API we provide requires a mapping from section to relocations, so build it now.
let relocations = sections.relocation_sections(endian, symbols.section())?;
let dynamic_relocations =
sections.relocation_sections(endian, dynamic_symbols.section())?;

Ok(ElfFile {
endian,
Expand All @@ -74,6 +77,7 @@ where
segments,
sections,
relocations,
dynamic_relocations,
symbols,
dynamic_symbols,
})
Expand Down
3 changes: 2 additions & 1 deletion src/read/elf/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ where
/// The current pointer in the chain of relocation sections.
pub(super) section_index: SectionIndex,
pub(super) file: &'file ElfFile<'data, Elf, R>,
pub(super) relocation_sections: &'file RelocationSections,
pub(super) relocations: Option<ElfRelaIterator<'data, Elf>>,
}

Expand All @@ -211,7 +212,7 @@ where
}
self.relocations = None;
}
self.section_index = self.file.relocations.get(self.section_index)?;
self.section_index = self.relocation_sections.get(self.section_index)?;
// The construction of RelocationSections ensures section_index is valid.
let section = self.file.sections.section(self.section_index).unwrap();
match section.sh_type(endian) {
Expand Down
10 changes: 10 additions & 0 deletions src/read/elf/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ where
ElfSectionRelocationIterator {
section_index: self.index,
file: self.file,
relocation_sections: &self.file.relocations,
relocations: None,
}
}

fn dynamic_relocations(&self) -> ElfSectionRelocationIterator<'data, 'file, Elf, R> {
ElfSectionRelocationIterator {
section_index: self.index,
file: self.file,
relocation_sections: &self.file.dynamic_relocations,
relocations: None,
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/read/macho/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ where
}
}

fn dynamic_relocations(&self) -> MachORelocationIterator<'data, 'file, Mach, R> {
MachORelocationIterator {
file: self.file,
relocations: [].iter(),
}
}

fn relocation_map(&self) -> read::Result<RelocationMap> {
RelocationMap::new(self.file, self)
}
Expand Down
4 changes: 4 additions & 0 deletions src/read/pe/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ where
PeRelocationIterator(PhantomData)
}

fn dynamic_relocations(&self) -> PeRelocationIterator<'data, 'file, R> {
PeRelocationIterator(PhantomData)
}

fn relocation_map(&self) -> read::Result<RelocationMap> {
RelocationMap::new(self.file, self)
}
Expand Down
9 changes: 9 additions & 0 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ pub trait ObjectSection<'data>: read::private::Sealed {
/// Get the relocations for this section.
fn relocations(&self) -> Self::RelocationIterator;

/// Get the dynamic relocations for this section.
///
/// Symbol indices in these relocations refer to the dynamic symbol table.
///
/// Only ELF has dynamic relocations. This may not return all of the dynamic
/// relocations for the section; it can only return relocations from sections
/// that have their `sh_info` set to the index of this section.
fn dynamic_relocations(&self) -> Self::RelocationIterator;

/// Construct a relocation map for this section.
fn relocation_map(&self) -> Result<RelocationMap>;

Expand Down
4 changes: 4 additions & 0 deletions src/read/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for WasmSection<'data
WasmRelocationIterator(PhantomData)
}

fn dynamic_relocations(&self) -> WasmRelocationIterator<'data, 'file, R> {
WasmRelocationIterator(PhantomData)
}

fn relocation_map(&self) -> read::Result<RelocationMap> {
RelocationMap::new(self.file, self)
}
Expand Down
7 changes: 7 additions & 0 deletions src/read/xcoff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ where
}
}

fn dynamic_relocations(&self) -> Self::RelocationIterator {
XcoffRelocationIterator {
file: self.file,
relocations: [].iter(),
}
}

fn relocation_map(&self) -> read::Result<RelocationMap> {
RelocationMap::new(self.file, self)
}
Expand Down
Loading