Skip to content

Commit

Permalink
Add and support Symbol::size()
Browse files Browse the repository at this point in the history
  • Loading branch information
chorman0773 committed Jan 31, 2024
1 parent 2337185 commit d97de38
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
38 changes: 27 additions & 11 deletions binfmt/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,13 +1148,31 @@ impl<Class: ElfClass + 'static, Howto: HowTo + 'static> Binfmt for ElfFormat<Cla
let value = sym.value().as_u64() as u128;
let section = (sym.section().as_u64() as u32).checked_sub(1);

let sym = crate::sym::Symbol::new(
name,
section,
section.map(|_| value),
SymbolType::Object,
SymbolKind::Local,
);
let ty = match sym.info().as_usize() & 0xf {
0 => SymbolType::Null,
1 => SymbolType::Object,
2 => SymbolType::Function,
5 => SymbolType::Common,
6 => SymbolType::Tls,
val => SymbolType::FormatSpecific(val as u32),
};

let bind = match sym.info().as_usize() >> 4 {
0 => SymbolKind::Local,
1 => SymbolKind::Global,
2 => SymbolKind::Weak,
val => SymbolKind::FormatSpecific(val as u32),
};

let size = sym.size().as_usize() as u64;

let mut sym = if let Some(section) = section {
crate::sym::Symbol::new(name, section, value, ty, bind)
} else {
crate::sym::Symbol::new_undef(name, ty, bind)
};

*sym.size_mut() = Some(size);

syms.push(sym);
}
Expand Down Expand Up @@ -1299,10 +1317,8 @@ impl<Class: ElfClass + 'static, Howto: HowTo + 'static> Binfmt for ElfFormat<Cla
num_reloc_sections += 1;
for reloc in &section.relocs {
if !new_symbol_list.iter().any(|x| x.name() == reloc.symbol) {
new_symbol_list.push(crate::sym::Symbol::new(
new_symbol_list.push(crate::sym::Symbol::new_undef(
reloc.symbol.clone(),
None,
None,
SymbolType::Null,
SymbolKind::Global,
));
Expand All @@ -1328,7 +1344,7 @@ impl<Class: ElfClass + 'static, Howto: HowTo + 'static> Binfmt for ElfFormat<Cla
String::from(sym.name()).into(),
)),
Class::Addr::from_usize(sym.value().map_or(0, |x| x as usize)),
Class::Size::from_usize(0usize),
Class::Size::from_usize(sym.size().unwrap_or(0) as usize),
(match sym.kind() {
SymbolKind::Local => {
local_syms += 1;
Expand Down
8 changes: 1 addition & 7 deletions binfmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,7 @@ impl<'a> BinaryFile<'a> {
return Ok(x);
}
{
let mut sym = Symbol::new(
name.to_string(),
None,
None,
SymbolType::Null,
SymbolKind::Local,
);
let mut sym = Symbol::new_undef(name.to_string(), SymbolType::Null, SymbolKind::Local);
self.fmt.create_symbol(&mut sym)?;
symtab.insert(name.to_string(), sym);
Ok(symtab.get_mut(name).unwrap())
Expand Down
29 changes: 25 additions & 4 deletions binfmt/src/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ pub struct Symbol {
secno: Option<u32>,
symtype: SymbolType,
kind: SymbolKind,
size: Option<u64>,
}

impl Symbol {
pub fn new(
name: String,
section: Option<u32>,
value: Option<u128>,
section: u32,
value: u128,
symtype: SymbolType,
kind: SymbolKind,
) -> Self {
Self {
name,
value,
secno: section,
value: Some(value),
secno: Some(section),
symtype,
kind,
size: None,
}
}

pub fn new_undef(name: String, symtype: SymbolType, kind: SymbolKind) -> Self {
Self {
name,
value: None,
secno: None,
symtype,
kind,
size: None,
}
}

Expand Down Expand Up @@ -59,6 +72,14 @@ impl Symbol {
pub fn kind_mut(&mut self) -> &mut SymbolKind {
&mut self.kind
}

pub fn size(&self) -> Option<u64> {
self.size
}

pub fn size_mut(&mut self) -> &mut Option<u64> {
&mut self.size
}
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
Expand Down

0 comments on commit d97de38

Please sign in to comment.