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.

72 lines
1.5 KiB
Rust

use std::{fmt::Display, io::ErrorKind, path::{Path, PathBuf}};
4 years ago
use crate::{
common::{Output, Relocatable},
error::Error,
};
4 years ago
pub struct Linker<R, O>
4 years ago
where
R: Relocatable,
O: Output<R>,
4 years ago
{
relocatables: Vec<R>,
output: O,
4 years ago
}
impl<R, O> Linker<R, O>
4 years ago
where
R: Relocatable,
O: Output<R>,
4 years ago
{
pub fn new(output: O) -> Self {
Self {
relocatables: Vec::new(),
output,
}
4 years ago
}
4 years ago
pub fn add_relocatable(&mut self, path: PathBuf) -> Result<(), Error> {
self.relocatables.push(R::try_from(path)?);
Ok(())
4 years ago
}
4 years ago
pub fn link(mut self) -> Result<PathBuf, Error> {
4 years ago
for r in self.relocatables.iter() {
for s in r.sections() {
4 years ago
self.output.process_section(s?)?;
4 years ago
}
}
self.output.finalize(&self.relocatables)
4 years ago
}
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)
}
}
4 years ago
}
impl<R, O> Display for Linker<R, O>
4 years ago
where
R: Relocatable,
O: Output<R>,
4 years ago
{
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(())
}
}