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.

59 lines
1.3 KiB
Rust

4 years ago
use std::fmt::Display;
4 years ago
use crate::{common::{Output, Relocatable}, error::Error};
4 years ago
4 years ago
pub struct Linker<'data> {
relocatables: Vec<Box<dyn Relocatable + 'data>>,
4 years ago
}
4 years ago
impl<'data> Linker<'data> {
pub fn new(relocatables: Vec<Box<dyn Relocatable + 'data>>) -> Self {
Self {
relocatables,
}
}
pub fn add_relocatable(
&mut self,
relocatable: Box<dyn Relocatable + 'data>,
4 years ago
) {
4 years ago
self.relocatables.push(relocatable);
4 years ago
}
4 years ago
4 years ago
pub fn link(&self, output: &mut dyn Output) -> Result<u64, Error> {
let allocated = output.allocate(self.total_size()?)?;
for r in self.relocatables.iter() {
for s in r.sections() {
output.append_section(&s?)?;
}
}
Ok(allocated)
4 years ago
}
4 years ago
fn total_size(&self) -> Result<u64, Error> {
let mut result = 0u64;
for o in self.relocatables.iter() {
for s in o.sections() {
result += s?.size()?;
}
}
Ok(result)
}
4 years ago
}
4 years ago
impl Display for Linker<'_> {
4 years ago
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "===Relocatables===")?;
4 years ago
for r in self.relocatables.iter() {
write!(f, "{}", r)?;
4 years ago
}
Ok(())
}
}