WIP
parent
ef7f942349
commit
a6eb022b91
@ -1,7 +1,92 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
use crate::error::{Error, LinkError, Trace, LE_GLOBAL_SYMBOL_DUPLICATE};
|
||||
|
||||
use super::Relocatable;
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct SymbolIndex {
|
||||
pub object_index: usize,
|
||||
pub symbol_index: usize,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Symbols {
|
||||
indexes: Vec<SymbolIndex>,
|
||||
value_index: Option<usize>,
|
||||
}
|
||||
|
||||
impl Symbols {
|
||||
pub fn push<R>(&mut self, si: SymbolIndex, objects: &[R]) -> Result<(), Error>
|
||||
where
|
||||
R: Relocatable,
|
||||
{
|
||||
let object = objects
|
||||
.get(si.object_index)
|
||||
.ok_or(Error::InvalidSymbolIndex)?;
|
||||
let value = object.symbol_value(si.symbol_index)?;
|
||||
|
||||
self.indexes.push(si);
|
||||
|
||||
if value.len() > 0 {
|
||||
if let Some(existing) = self.value_index {
|
||||
let osi = self
|
||||
.indexes
|
||||
.get(existing)
|
||||
.ok_or(Error::InvalidSymbolIndex)?;
|
||||
Self::duplicate_symbol_error(si, *osi, objects)
|
||||
} else {
|
||||
self.value_index = Some(self.indexes.len() - 1);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value<'o, R>(&self, object: &'o R) -> Result<&'o [u8], Error>
|
||||
where
|
||||
R: Relocatable,
|
||||
{
|
||||
self.value_index.map_or(Ok(&[]), |v| {
|
||||
object.symbol_value(self.indexes[v].symbol_index)
|
||||
})
|
||||
}
|
||||
|
||||
fn duplicate_symbol_error<R>(
|
||||
si: SymbolIndex,
|
||||
osi: SymbolIndex,
|
||||
objects: &[R],
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
R: Relocatable,
|
||||
{
|
||||
let object = objects
|
||||
.get(si.object_index)
|
||||
.ok_or(Error::InvalidSymbolIndex)?;
|
||||
let other = objects
|
||||
.get(osi.object_index)
|
||||
.ok_or(Error::InvalidObjectIndex)?;
|
||||
let mut traces = Vec::new();
|
||||
|
||||
traces.push(Trace {
|
||||
origin: object.origin().into(),
|
||||
offset: object.symbol_file_offset(si.symbol_index)?,
|
||||
source_info: None,
|
||||
});
|
||||
traces.push(Trace {
|
||||
origin: other.origin().into(),
|
||||
offset: other.symbol_file_offset(osi.symbol_index)?,
|
||||
source_info: None,
|
||||
});
|
||||
|
||||
let link_error = LinkError {
|
||||
code: LE_GLOBAL_SYMBOL_DUPLICATE,
|
||||
message: "duplicate global symbol found".into(),
|
||||
traces,
|
||||
};
|
||||
|
||||
Err(Error::LinkingError(link_error))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue