Skip to content

Commit

Permalink
restore Artifact::link() to old signature, create Artifact::link_with
Browse files Browse the repository at this point in the history
link_with allows you to provide a RelocOverride
  • Loading branch information
pchickey committed Dec 6, 2017
1 parent 66eb3c8 commit b5f03d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RelocOverride>,
}

/// The kind of import this is - either a function, or a copy relocation of data from a shared library
Expand Down Expand Up @@ -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<RelocOverride>) -> 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, _) => {
Expand All @@ -304,7 +312,9 @@ impl Artifact {
}
}
Ok(())

}

/// Emit a blob of bytes that represents this object file
pub fn emit<O: Object>(&self) -> Result<Vec<u8>, Error> {
O::to_bytes(self)
Expand Down
8 changes: 4 additions & 4 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b5f03d5

Please sign in to comment.