indirect via object

master
Ales Katona 4 years ago
parent bbcd914380
commit ef7f942349
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -3,12 +3,10 @@ use std::iter::once;
use crate::error::Error; use crate::error::Error;
use crate::{ use crate::{
common::{Relocatable, Section, SectionInfo, Symbol}, common::{Relocatable, Section, SectionInfo, SymbolIndex},
error::{LinkError, Trace, LE_GLOBAL_SYMBOL_DUPLICATE}, error::LinkError,
}; };
use super::Binding;
pub enum SegmentType { pub enum SegmentType {
Text, Text,
Data, Data,
@ -44,7 +42,7 @@ pub struct Loadable {
rodata: SegmentSections, rodata: SegmentSections,
data: SegmentSections, data: SegmentSections,
bss: SegmentSections, bss: SegmentSections,
symbol_map: HashMap<String, Vec<Symbol>>, symbol_map: HashMap<String, Vec<SymbolIndex>>,
} }
impl Loadable { impl Loadable {
@ -59,15 +57,26 @@ impl Loadable {
Ok(()) Ok(())
} }
pub fn process_symbol(&mut self, name: &str, symbol: Symbol) -> Result<(), Error> { pub fn process_symbol<R>(&mut self, index: usize, object: &R) -> Result<(), Error>
if symbol.binding == Binding::Local { where
R: Relocatable,
{
if !object.symbol_needs_resolving(index) {
return Ok(()); // local symbols are not stored 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) { if let Some(existing) = self.symbol_map.get_mut(name) {
existing.push(symbol); existing.push(si);
} else { } else {
self.symbol_map.insert(name.to_owned(), Vec::new()); self.symbol_map.insert(name.to_owned(), vec![si]);
} }
Ok(()) Ok(())
@ -151,8 +160,6 @@ impl Loadable {
} }
pub fn start_offset(&self) -> Result<u64, Error> { pub fn start_offset(&self) -> Result<u64, Error> {
use crate::error::LinkError;
self.start_offset.ok_or_else(|| { self.start_offset.ok_or_else(|| {
let link_error = LinkError { let link_error = LinkError {
message: "Program entrypoint not found".into(), message: "Program entrypoint not found".into(),

@ -3,23 +3,27 @@ use std::{fmt::Display, path::PathBuf};
use crate::error::Error; use crate::error::Error;
use super::{Section, Symbol}; use super::Section;
pub type SectionIterBox<'iter> = Box<dyn Iterator<Item = Result<Section, Error>> + 'iter>; pub type SectionIterBox<'iter> = Box<dyn Iterator<Item = Result<Section, Error>> + 'iter>;
pub type SymbolIterBox<'iter> = Box<dyn Iterator<Item = (&'iter str, Symbol)> + 'iter>;
/// Contains all the needed getters to construct a final /// Contains all the needed getters to construct a final
/// mushed and relocated executable from an object file /// mushed and relocated executable from an object file
pub trait Relocatable: Display + Sized { pub trait Relocatable: Display + Sized {
// + TryFrom<PathBuf, Error = Error> { // + TryFrom<PathBuf, Error = Error> {
fn new(origin: PathBuf, object_index: usize) -> Result<Self, Error>; fn new(origin: PathBuf, object_index: usize) -> Result<Self, Error>;
fn object_index(&self) -> usize;
fn origin(&self) -> &Path; // not same as section's path since this one's supposed to be cannonical fn origin(&self) -> &Path; // not same as section's path since this one's supposed to be cannonical
fn sections(&self) -> SectionIterBox; 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>; fn bytes(&self, offset: u64, size: u64) -> Result<&[u8], Error>;
} }

@ -1,19 +1,7 @@
use elf_utilities::relocation; use std::fmt::Debug;
use crate::error::Error; #[derive(Default, Debug)]
pub struct SymbolIndex {
use super::Relocatable;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Binding {
Local,
Global,
Weak,
}
pub struct Symbol {
pub object_index: usize, pub object_index: usize,
pub binding: Binding, pub symbol_index: usize,
pub address: u64,
pub size: u64,
} }

@ -11,6 +11,8 @@ pub enum Error {
DataError(PathBuf), // generic "something wrong with data" error, not specific to parsing DataError(PathBuf), // generic "something wrong with data" error, not specific to parsing
InvalidObjectType(u32), InvalidObjectType(u32),
InvalidSectionName, InvalidSectionName,
InvalidSymbolIndex,
InvalidSymbolName,
InvalidSectionData, InvalidSectionData,
InvalidInput, // input is completely wrong (e.g. no magic bytes etc.) InvalidInput, // input is completely wrong (e.g. no magic bytes etc.)
ParseError(Box<dyn std::error::Error>), // error on parsing input ParseError(Box<dyn std::error::Error>), // error on parsing input
@ -41,6 +43,8 @@ impl Error {
Error::DataError(_) => 800, Error::DataError(_) => 800,
Error::InvalidObjectType(_) => 200, Error::InvalidObjectType(_) => 200,
Error::InvalidSectionName => 300, Error::InvalidSectionName => 300,
Error::InvalidSymbolIndex => 325,
Error::InvalidSymbolName => 350,
Error::InvalidSectionData => 400, Error::InvalidSectionData => 400,
Error::ParseError(_) => 500, Error::ParseError(_) => 500,
Error::TryFromIntError => 600, Error::TryFromIntError => 600,
@ -77,6 +81,8 @@ impl Display for Error {
Error::DataError(origin) => write!(f, "Data error in {:?}", origin), Error::DataError(origin) => write!(f, "Data error in {:?}", origin),
Error::InvalidObjectType(ot) => write!(f, "Invalid object type: {}", ot), Error::InvalidObjectType(ot) => write!(f, "Invalid object type: {}", ot),
Error::InvalidSectionName => write!(f, "Invalid section name"), 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::InvalidSectionData => write!(f, "Invalid section data"),
Error::ParseError(err) => write!(f, "Parse error: {}", err), Error::ParseError(err) => write!(f, "Parse error: {}", err),
Error::TryFromIntError => write!(f, "Integer conversion error"), Error::TryFromIntError => write!(f, "Integer conversion error"),

@ -4,12 +4,12 @@ use std::{
path::{Path, PathBuf}, 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 crate::{common::SectionIterBox, error::Error};
use elf_utilities::{ use elf_utilities::{
header::Ehdr64, header::Ehdr64,
section::{Contents64, Shdr64, Type}, section::{Shdr64, Type},
symbol::{Bind, Symbol64}, symbol::{Bind, Symbol64, Type as SymType},
}; };
use memmap::Mmap; use memmap::Mmap;
@ -19,7 +19,7 @@ pub struct ElfObject {
data: Mmap, data: Mmap,
ehdr: Ehdr64, ehdr: Ehdr64,
shdrs: Vec<Shdr64>, shdrs: Vec<Shdr64>,
symbols: Vec<Symbol64>, symbols: Vec<(usize, Symbol64)>,
} }
impl ElfObject { impl ElfObject {
@ -49,6 +49,16 @@ impl ElfObject {
} }
fn resolve_name(&self, name_idx: usize, sh_index: usize) -> Result<&str, Error> { 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 = self.parse_strtab(sh_index)?;
let strtab_offset: usize = strtab.sh_offset as usize; let strtab_offset: usize = strtab.sh_offset as usize;
let start = strtab_offset + name_idx; let start = strtab_offset + name_idx;
@ -91,14 +101,21 @@ impl ElfObject {
let offset = shdr.sh_offset as usize; let offset = shdr.sh_offset as usize;
let count = shdr.sh_size as usize / ent_size; 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 start = offset + s_i * ent_size;
let s64 = Symbol64::deserialize(&self.data, start) let s64 = Symbol64::deserialize(&self.data, start)
.map_err(|err| Error::ParseError(err))?; .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); self.shdrs.push(shdr);
} }
@ -132,22 +149,6 @@ impl ElfObject {
None 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 { impl Relocatable for ElfObject {
@ -155,6 +156,10 @@ impl Relocatable for ElfObject {
ElfObject::new(origin, object_index) ElfObject::new(origin, object_index)
} }
fn object_index(&self) -> usize {
self.object_index
}
fn origin(&self) -> &Path { fn origin(&self) -> &Path {
&self.origin &self.origin
} }
@ -177,14 +182,23 @@ impl Relocatable for ElfObject {
Ok(&self.data[o..o + s]) Ok(&self.data[o..o + s])
} }
fn symbols(&self) -> SymbolIterBox { fn symbol_count(&self) -> usize {
Box::new(self.symbols.iter().map(move |s| { self.symbols.len()
// TODO: change SymbolIterBox to allow errors! }
let name = self
.resolve_name(s.st_name as usize, s.st_other as usize) fn symbol_name(&self, index: usize) -> Result<&str, Error> {
.expect("Symbol name resolution"); if let Some(symbol) = self.symbols.get(index) {
(name, self.make_symbol(s)) 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,
}
} }
} }

@ -1,7 +1,7 @@
use std::{fmt::Display, path::PathBuf}; use std::{fmt::Display, path::PathBuf};
use crate::{ use crate::{
common::{Loadable, Output, Relocatable}, common::{Loadable, Output, Relocatable, SymbolIndex},
error::Error, error::Error,
}; };
@ -50,24 +50,15 @@ where
fn process_symbols(&mut self) -> Result<(), Error> { fn process_symbols(&mut self) -> Result<(), Error> {
for r in &self.relocatables { for r in &self.relocatables {
for (name, symbol) in r.symbols() { let symbol_count = r.symbol_count();
self.loadable.process_symbol(name, symbol)?;
for i in 0..symbol_count {
self.loadable.process_symbol(i, r)?;
} }
} }
Ok(()) Ok(())
} }
// pub fn object_path(origin: &Path) -> Result<PathBuf, Error> {
// 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<R, O> Display for Linker<R, O> impl<R, O> Display for Linker<R, O>

Loading…
Cancel
Save