You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.8 KiB
Rust
83 lines
1.8 KiB
Rust
use std::{fmt::Display, path::PathBuf};
|
|
|
|
use crate::{
|
|
common::{Loadable, Output, Relocatable, SymbolIndex},
|
|
error::Error,
|
|
};
|
|
|
|
pub struct Linker<R, O>
|
|
where
|
|
R: Relocatable,
|
|
O: Output<R>,
|
|
{
|
|
objects: Vec<R>,
|
|
loadable: Loadable,
|
|
output: O,
|
|
}
|
|
|
|
impl<R, O> Linker<R, O>
|
|
where
|
|
R: Relocatable,
|
|
O: Output<R>,
|
|
{
|
|
pub fn new(output: O) -> Self {
|
|
Self {
|
|
objects: Vec::new(),
|
|
loadable: Loadable::default(),
|
|
output,
|
|
}
|
|
}
|
|
|
|
pub fn add_relocatable(&mut self, origin: PathBuf) -> Result<(), Error> {
|
|
let r = R::new(origin, self.objects.len())?;
|
|
|
|
// TODO: parallelize?
|
|
for section in r.sections() {
|
|
self.loadable.process_section(section?)?;
|
|
}
|
|
|
|
self.objects.push(r);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn link(mut self) -> Result<PathBuf, Error> {
|
|
self.process_symbols()?;
|
|
|
|
self.loadable.set_start_offset(4096); // TODO: get from .start symbol location
|
|
self.output.finalize(&self.objects, &self.loadable)
|
|
}
|
|
|
|
fn process_symbols(&mut self) -> Result<(), Error> {
|
|
for r in &self.objects {
|
|
let symbol_count = r.symbol_count();
|
|
|
|
for i in 0..symbol_count {
|
|
let si = SymbolIndex {
|
|
object_index: r.object_index(),
|
|
symbol_index: i,
|
|
};
|
|
self.loadable.process_symbol(si, &self.objects)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<R, O> Display for Linker<R, O>
|
|
where
|
|
R: Relocatable,
|
|
O: Output<R>,
|
|
{
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
writeln!(f, "===Relocatables===")?;
|
|
|
|
for r in self.objects.iter() {
|
|
write!(f, "{}", r)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|