diff --git a/src/artifact.rs b/src/artifact.rs index 05ab49d..d45a675 100644 --- a/src/artifact.rs +++ b/src/artifact.rs @@ -125,8 +125,6 @@ pub struct Link<'a> { pub to: &'a str, /// The byte offset _relative_ to `from` where the relocation should be performed pub at: usize, - /// Relocation type: - pub reloc: Option, } /// The kind of import this is - either a function, or a copy relocation of data from a shared library @@ -288,12 +286,22 @@ impl Artifact { /// **NB**: If either `link.from` or `link.to` is undeclared, then this will return an error. /// If `link.from` is an import you previously declared, this will also return an error. pub fn link<'a>(&mut self, link: Link<'a>) -> Result<(), Error> { + self.link_aux(link, None) + } + /// A variant of `link` with a RelocOverride provided. Has all of the same invariants as + /// `link`. + pub fn link_with<'a>(&mut self, link: Link<'a>, reloc: RelocOverride) -> Result<(), Error> { + self.link_aux(link, Some(reloc)) + } + + /// Shared implementation of `link` and `link_with`. + fn link_aux<'a>(&mut self, link: Link<'a>, reloc: Option) -> Result<(), Error> { match (self.declarations.get(link.from), self.declarations.get(link.to)) { (Some(ref from_type), Some(_)) => { if from_type.is_import() { return Err(ArtifactError::RelocateImport(link.from.to_string()).into()); } - let link = (link.from.to_string(), link.to.to_string(), link.at, link.reloc); + let link = (link.from.to_string(), link.to.to_string(), link.at, reloc); self.links.push(link.clone()); }, (None, _) => { @@ -304,7 +312,9 @@ impl Artifact { } } Ok(()) + } + /// Emit a blob of bytes that represents this object file pub fn emit(&self) -> Result, Error> { O::to_bytes(self) diff --git a/src/bin/main.rs b/src/bin/main.rs index 7c0abc9..af1712b 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -116,10 +116,10 @@ fn run (args: Args) -> Result<(), Error> { // Next, we declare our relocations, // which are _always_ relative to the `from` symbol - obj.link(Link { from: "main", to: "str.1", at: 19, reloc: None })?; - obj.link(Link { from: "main", to: "printf", at: 29, reloc: None })?; - obj.link(Link { from: "main", to: "deadbeef", at: 10, reloc: None })?; - obj.link(Link { from: "deadbeef", to: "DEADBEEF", at: 7, reloc: None })?; + obj.link(Link { from: "main", to: "str.1", at: 19 })?; + obj.link(Link { from: "main", to: "printf", at: 29 })?; + obj.link(Link { from: "main", to: "deadbeef", at: 10 })?; + obj.link(Link { from: "deadbeef", to: "DEADBEEF", at: 7 })?; // Finally, we write which object file we desire if args.mach {