Skip to content

Commit aa2f5ad

Browse files
authored
read/elf: small fixes to low level API (#685)
Implement Default for SectionTable and RelocationSections. Return a SectionIndex from SectionTable::section_by_name. Add some helpers to Sym trait.
1 parent 017624a commit aa2f5ad

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/read/elf/file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ where
147147
.section_by_name(self.endian, section_name)
148148
.map(|(index, section)| ElfSection {
149149
file: self,
150-
index: SectionIndex(index),
150+
index,
151151
section,
152152
})
153153
}

src/read/elf/relocation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::read::{
1414
use super::{ElfFile, FileHeader, SectionHeader, SectionTable};
1515

1616
/// A mapping from section index to associated relocation sections.
17-
#[derive(Debug)]
17+
#[derive(Debug, Default)]
1818
pub struct RelocationSections {
1919
relocations: Vec<usize>,
2020
}

src/read/elf/section.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::{
2020
/// Also includes the string table used for the section names.
2121
///
2222
/// Returned by [`FileHeader::sections`].
23-
#[derive(Debug, Default, Clone, Copy)]
23+
#[derive(Debug, Clone, Copy)]
2424
pub struct SectionTable<'data, Elf: FileHeader, R = &'data [u8]>
2525
where
2626
R: ReadRef<'data>,
@@ -29,6 +29,15 @@ where
2929
strings: StringTable<'data, R>,
3030
}
3131

32+
impl<'data, Elf: FileHeader, R: ReadRef<'data>> Default for SectionTable<'data, Elf, R> {
33+
fn default() -> Self {
34+
SectionTable {
35+
sections: &[],
36+
strings: StringTable::default(),
37+
}
38+
}
39+
}
40+
3241
impl<'data, Elf: FileHeader, R: ReadRef<'data>> SectionTable<'data, Elf, R> {
3342
/// Create a new section table.
3443
#[inline]
@@ -86,10 +95,8 @@ impl<'data, Elf: FileHeader, R: ReadRef<'data>> SectionTable<'data, Elf, R> {
8695
&self,
8796
endian: Elf::Endian,
8897
name: &[u8],
89-
) -> Option<(usize, &'data Elf::SectionHeader)> {
90-
self.sections
91-
.iter()
92-
.enumerate()
98+
) -> Option<(SectionIndex, &'data Elf::SectionHeader)> {
99+
self.enumerate()
93100
.find(|(_, section)| self.section_name(endian, section) == Ok(name))
94101
}
95102

@@ -133,16 +140,12 @@ impl<'data, Elf: FileHeader, R: ReadRef<'data>> SectionTable<'data, Elf, R> {
133140
) -> read::Result<SymbolTable<'data, Elf, R>> {
134141
debug_assert!(sh_type == elf::SHT_DYNSYM || sh_type == elf::SHT_SYMTAB);
135142

136-
let (index, section) = match self
137-
.iter()
138-
.enumerate()
139-
.find(|s| s.1.sh_type(endian) == sh_type)
140-
{
143+
let (index, section) = match self.enumerate().find(|s| s.1.sh_type(endian) == sh_type) {
141144
Some(s) => s,
142145
None => return Ok(SymbolTable::default()),
143146
};
144147

145-
SymbolTable::parse(endian, data, self, SectionIndex(index), section)
148+
SymbolTable::parse(endian, data, self, index, section)
146149
}
147150

148151
/// Return the symbol table at the given section index.

src/read/elf/symbol.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ impl<'data, Elf: FileHeader, R: ReadRef<'data>> SymbolTable<'data, Elf, R> {
6969

7070
let mut shndx_section = SectionIndex(0);
7171
let mut shndx = &[][..];
72-
for (i, s) in sections.iter().enumerate() {
72+
for (i, s) in sections.enumerate() {
7373
if s.sh_type(endian) == elf::SHT_SYMTAB_SHNDX && s.link(endian) == section_index {
74-
shndx_section = SectionIndex(i);
74+
shndx_section = i;
7575
shndx = s
7676
.data_as_array(endian, data)
7777
.read_error("Invalid ELF symtab_shndx data")?;
@@ -431,7 +431,7 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data>
431431

432432
#[inline]
433433
fn is_undefined(&self) -> bool {
434-
self.symbol.st_shndx(self.endian) == elf::SHN_UNDEF
434+
self.symbol.is_undefined(self.endian)
435435
}
436436

437437
#[inline]
@@ -441,12 +441,12 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data>
441441

442442
#[inline]
443443
fn is_common(&self) -> bool {
444-
self.symbol.st_shndx(self.endian) == elf::SHN_COMMON
444+
self.symbol.is_common(self.endian)
445445
}
446446

447447
#[inline]
448448
fn is_weak(&self) -> bool {
449-
self.symbol.st_bind() == elf::STB_WEAK
449+
self.symbol.is_weak()
450450
}
451451

452452
fn scope(&self) -> SymbolScope {
@@ -469,12 +469,12 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data>
469469

470470
#[inline]
471471
fn is_global(&self) -> bool {
472-
self.symbol.st_bind() != elf::STB_LOCAL
472+
!self.symbol.is_local()
473473
}
474474

475475
#[inline]
476476
fn is_local(&self) -> bool {
477-
self.symbol.st_bind() == elf::STB_LOCAL
477+
self.symbol.is_local()
478478
}
479479

480480
#[inline]
@@ -513,7 +513,7 @@ pub trait Sym: Debug + Pod {
513513
.read_error("Invalid ELF symbol name offset")
514514
}
515515

516-
/// Return true if the symbol is undefined.
516+
/// Return true if the symbol section is `SHN_UNDEF`.
517517
#[inline]
518518
fn is_undefined(&self, endian: Self::Endian) -> bool {
519519
self.st_shndx(endian) == elf::SHN_UNDEF
@@ -531,6 +531,26 @@ pub trait Sym: Debug + Pod {
531531
_ => false,
532532
}
533533
}
534+
535+
/// Return true if the symbol section is `SHN_COMMON`.
536+
fn is_common(&self, endian: Self::Endian) -> bool {
537+
self.st_shndx(endian) == elf::SHN_COMMON
538+
}
539+
540+
/// Return true if the symbol section is `SHN_ABS`.
541+
fn is_absolute(&self, endian: Self::Endian) -> bool {
542+
self.st_shndx(endian) == elf::SHN_ABS
543+
}
544+
545+
/// Return true if the symbol binding is `STB_LOCAL`.
546+
fn is_local(&self) -> bool {
547+
self.st_bind() == elf::STB_LOCAL
548+
}
549+
550+
/// Return true if the symbol binding is `STB_WEAK`.
551+
fn is_weak(&self) -> bool {
552+
self.st_bind() == elf::STB_WEAK
553+
}
534554
}
535555

536556
impl<Endian: endian::Endian> Sym for elf::Sym32<Endian> {

0 commit comments

Comments
 (0)