diff --git a/src/common/loadable.rs b/src/common/loadable.rs index bde3643..9bbf4b4 100644 --- a/src/common/loadable.rs +++ b/src/common/loadable.rs @@ -3,12 +3,10 @@ use std::iter::once; use crate::error::Error; use crate::{ - common::{Relocatable, Section, SectionInfo, Symbol}, - error::{LinkError, Trace, LE_GLOBAL_SYMBOL_DUPLICATE}, + common::{Relocatable, Section, SectionInfo, SymbolIndex}, + error::LinkError, }; -use super::Binding; - pub enum SegmentType { Text, Data, @@ -44,7 +42,7 @@ pub struct Loadable { rodata: SegmentSections, data: SegmentSections, bss: SegmentSections, - symbol_map: HashMap>, + symbol_map: HashMap>, } impl Loadable { @@ -59,15 +57,26 @@ impl Loadable { Ok(()) } - pub fn process_symbol(&mut self, name: &str, symbol: Symbol) -> Result<(), Error> { - if symbol.binding == Binding::Local { + pub fn process_symbol(&mut self, index: usize, object: &R) -> Result<(), Error> + where + R: Relocatable, + { + if !object.symbol_needs_resolving(index) { return Ok(()); // local symbols are not stored } + let name = object.symbol_name(index)?; + eprintln!("NAME: {}", name); + + let si = SymbolIndex { + object_index: object.object_index(), + symbol_index: index, + }; + if let Some(existing) = self.symbol_map.get_mut(name) { - existing.push(symbol); + existing.push(si); } else { - self.symbol_map.insert(name.to_owned(), Vec::new()); + self.symbol_map.insert(name.to_owned(), vec![si]); } Ok(()) @@ -151,8 +160,6 @@ impl Loadable { } pub fn start_offset(&self) -> Result { - use crate::error::LinkError; - self.start_offset.ok_or_else(|| { let link_error = LinkError { message: "Program entrypoint not found".into(), diff --git a/src/common/relocatable.rs b/src/common/relocatable.rs index 9a9870c..d84425b 100644 --- a/src/common/relocatable.rs +++ b/src/common/relocatable.rs @@ -3,23 +3,27 @@ use std::{fmt::Display, path::PathBuf}; use crate::error::Error; -use super::{Section, Symbol}; +use super::Section; pub type SectionIterBox<'iter> = Box> + 'iter>; -pub type SymbolIterBox<'iter> = Box + 'iter>; - /// Contains all the needed getters to construct a final /// mushed and relocated executable from an object file pub trait Relocatable: Display + Sized { // + TryFrom { fn new(origin: PathBuf, object_index: usize) -> Result; + fn object_index(&self) -> usize; + fn origin(&self) -> &Path; // not same as section's path since this one's supposed to be cannonical fn sections(&self) -> SectionIterBox; - fn symbols(&self) -> SymbolIterBox; + fn symbol_count(&self) -> usize; + + fn symbol_name(&self, index: usize) -> Result<&str, Error>; + + fn symbol_needs_resolving(&self, index: usize) -> bool; fn bytes(&self, offset: u64, size: u64) -> Result<&[u8], Error>; } diff --git a/src/common/symbol.rs b/src/common/symbol.rs index 8e0ab35..7ec0a9e 100644 --- a/src/common/symbol.rs +++ b/src/common/symbol.rs @@ -1,19 +1,7 @@ -use elf_utilities::relocation; +use std::fmt::Debug; -use crate::error::Error; - -use super::Relocatable; - -#[derive(Clone, Copy, PartialEq, Eq)] -pub enum Binding { - Local, - Global, - Weak, -} - -pub struct Symbol { +#[derive(Default, Debug)] +pub struct SymbolIndex { pub object_index: usize, - pub binding: Binding, - pub address: u64, - pub size: u64, + pub symbol_index: usize, } diff --git a/src/error.rs b/src/error.rs index 057280a..63e269b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,6 +11,8 @@ pub enum Error { DataError(PathBuf), // generic "something wrong with data" error, not specific to parsing InvalidObjectType(u32), InvalidSectionName, + InvalidSymbolIndex, + InvalidSymbolName, InvalidSectionData, InvalidInput, // input is completely wrong (e.g. no magic bytes etc.) ParseError(Box), // error on parsing input @@ -41,6 +43,8 @@ impl Error { Error::DataError(_) => 800, Error::InvalidObjectType(_) => 200, Error::InvalidSectionName => 300, + Error::InvalidSymbolIndex => 325, + Error::InvalidSymbolName => 350, Error::InvalidSectionData => 400, Error::ParseError(_) => 500, Error::TryFromIntError => 600, @@ -77,6 +81,8 @@ impl Display for Error { Error::DataError(origin) => write!(f, "Data error in {:?}", origin), Error::InvalidObjectType(ot) => write!(f, "Invalid object type: {}", ot), Error::InvalidSectionName => write!(f, "Invalid section name"), + Error::InvalidSymbolIndex => write!(f, "Invalid symbol index"), + Error::InvalidSymbolName => write!(f, "Invalid symbol name"), Error::InvalidSectionData => write!(f, "Invalid section data"), Error::ParseError(err) => write!(f, "Parse error: {}", err), Error::TryFromIntError => write!(f, "Integer conversion error"), diff --git a/src/formats/elf/object.rs b/src/formats/elf/object.rs index 4fdd3ea..fa72a67 100644 --- a/src/formats/elf/object.rs +++ b/src/formats/elf/object.rs @@ -4,12 +4,12 @@ use std::{ path::{Path, PathBuf}, }; -use crate::common::{Binding, Relocatable, Section, SectionInfo, Symbol, SymbolIterBox}; +use crate::common::{Relocatable, Section, SectionInfo}; use crate::{common::SectionIterBox, error::Error}; use elf_utilities::{ header::Ehdr64, - section::{Contents64, Shdr64, Type}, - symbol::{Bind, Symbol64}, + section::{Shdr64, Type}, + symbol::{Bind, Symbol64, Type as SymType}, }; use memmap::Mmap; @@ -19,7 +19,7 @@ pub struct ElfObject { data: Mmap, ehdr: Ehdr64, shdrs: Vec, - symbols: Vec, + symbols: Vec<(usize, Symbol64)>, } impl ElfObject { @@ -49,6 +49,16 @@ impl ElfObject { } fn resolve_name(&self, name_idx: usize, sh_index: usize) -> Result<&str, Error> { + if name_idx == 0 { + let error = if sh_index == self.ehdr.e_shstrndx.into() { + Error::InvalidSectionName + } else { + Error::InvalidSymbolName + }; + + return Err(error); + } + let strtab = self.parse_strtab(sh_index)?; let strtab_offset: usize = strtab.sh_offset as usize; let start = strtab_offset + name_idx; @@ -91,14 +101,21 @@ impl ElfObject { let offset = shdr.sh_offset as usize; let count = shdr.sh_size as usize / ent_size; - for s_i in 0..count { + for s_i in 1..count { + // skip null-symbol let start = offset + s_i * ent_size; let s64 = Symbol64::deserialize(&self.data, start) .map_err(|err| Error::ParseError(err))?; - self.symbols.push(s64); + + match s64.get_type() { + SymType::Object | SymType::Func | SymType::NoType => { + self.symbols.push((shdr.sh_link as usize, s64)) + } + _ => {} + } } } - // self.symbols.push(); + self.shdrs.push(shdr); } @@ -132,22 +149,6 @@ impl ElfObject { None } } - - fn make_symbol(&self, s64: &Symbol64) -> Symbol { - let binding = match s64.get_bind() { - Bind::Global => Binding::Global, - Bind::Local => Binding::Local, - Bind::Weak => Binding::Weak, - _ => panic!("Unexpected binding type encountered on symbol"), // this is screened! - }; - - Symbol { - object_index: self.object_index, - binding, - address: s64.st_value, - size: s64.st_size, - } - } } impl Relocatable for ElfObject { @@ -155,6 +156,10 @@ impl Relocatable for ElfObject { ElfObject::new(origin, object_index) } + fn object_index(&self) -> usize { + self.object_index + } + fn origin(&self) -> &Path { &self.origin } @@ -177,14 +182,23 @@ impl Relocatable for ElfObject { Ok(&self.data[o..o + s]) } - fn symbols(&self) -> SymbolIterBox { - Box::new(self.symbols.iter().map(move |s| { - // TODO: change SymbolIterBox to allow errors! - let name = self - .resolve_name(s.st_name as usize, s.st_other as usize) - .expect("Symbol name resolution"); - (name, self.make_symbol(s)) - })) + fn symbol_count(&self) -> usize { + self.symbols.len() + } + + fn symbol_name(&self, index: usize) -> Result<&str, Error> { + if let Some(symbol) = self.symbols.get(index) { + self.resolve_name(symbol.1.st_name as usize, symbol.0) + } else { + Err(Error::InvalidSymbolIndex) + } + } + + fn symbol_needs_resolving(&self, index: usize) -> bool { + match self.symbols[index].1.get_bind() { + Bind::Global | Bind::Weak => true, + _ => false, + } } } diff --git a/src/linker.rs b/src/linker.rs index 0e7b84d..625602b 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -1,7 +1,7 @@ use std::{fmt::Display, path::PathBuf}; use crate::{ - common::{Loadable, Output, Relocatable}, + common::{Loadable, Output, Relocatable, SymbolIndex}, error::Error, }; @@ -50,24 +50,15 @@ where fn process_symbols(&mut self) -> Result<(), Error> { for r in &self.relocatables { - for (name, symbol) in r.symbols() { - self.loadable.process_symbol(name, symbol)?; + let symbol_count = r.symbol_count(); + + for i in 0..symbol_count { + self.loadable.process_symbol(i, r)?; } } Ok(()) } - - // pub fn object_path(origin: &Path) -> Result { - // let mut dest = std::fs::canonicalize(origin)?; - // if !dest.pop() { - // let err = std::io::Error::new(ErrorKind::Other, "Destination path invalid"); - // Err(Error::IOError(Box::new(err))) - // } else { - // dest.push("rld.out"); - // Ok(dest) - // } - // } } impl Display for Linker