Skip to content

Commit

Permalink
[OBJ] fixing relocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Jul 12, 2024
1 parent aefc9ea commit e92dc63
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
31 changes: 24 additions & 7 deletions examples/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,33 @@ fn main() -> Result<(), Box<dyn Error>> {
);

obj.decls(vec![
("test", Decl::Function, Linkage::External),
("test_data", Decl::Constant, Linkage::Extern),
("main", Decl::Function, Linkage::External),
("string", Decl::Constant, Linkage::Internal),
("puts", Decl::Function, Linkage::Extern),
]);

obj.define("test", vec![
0xB8, 0x00, 0x00, 0x00, 0x00, // mov eax, 5
0xC3,
]);
let mut data = vec![0x55];

data.extend_from_slice(&[0x48, 0x83, 0xc4, 0x20]);

if cfg!(target_os = "windows") {
data.extend_from_slice(&[0x48, 0x8d, 0x0d, 0x00, 0x00, 0x00, 0x00])
} else {
data.extend_from_slice(&[0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00])
}

obj.link( Link { from: "main".into(), to: "string".into(), at: data.len() -4, addend: -4 });

data.extend_from_slice(&[0xE8, 0x00, 0x00, 0x00, 0x00]);

obj.link( Link { from: "main".into(), to: "puts".into(), at: data.len() -4, addend: -4});

data.extend_from_slice(&[0x31, 0xC0]);
data.extend_from_slice(&[0x48, 0x83, 0xEC, 0x20]);
data.extend_from_slice(&[0xC3]);

obj.link(Link { from: "test".into(), to: "test_data".into(), at: 1 });
obj.define("main", data);
obj.define("string", b"Hello World!\00".into());

obj.emit(
OpenOptions::new().create(true).write(true).open("output.o")?
Expand Down
Binary file removed output.o
Binary file not shown.
20 changes: 11 additions & 9 deletions src/Obj/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use object::write::{Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection};
use object::write::{Relocation, SectionId, Symbol, SymbolId, SymbolSection};
use object::{Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationFlags, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope};

use crate::prelude::Triple;
Expand Down Expand Up @@ -44,6 +44,8 @@ pub struct Link {
pub to: String,
/// The binary offset of the start of the function
pub at: usize,
/// The addend to use
pub addend: i64,
}

/// The linkage of the target symbol
Expand Down Expand Up @@ -213,19 +215,19 @@ impl ObjectBuilder {
flags: SymbolFlags::None,
});

let def_section = match decl {
/*let def_section = match decl {
Decl::Function => obj.add_subsection(StandardSection::Text, name.as_bytes()),
Decl::Data => obj.add_subsection(StandardSection::Data, name.as_bytes()),
Decl::Constant => obj.add_subsection(StandardSection::ReadOnlyData, name.as_bytes()),
};
};*/

let def_offset = match decl {
Decl::Function => obj.add_symbol_data(sym, secText, &data, 16),
Decl::Data => obj.add_symbol_data(sym, secData, &data, 16),
Decl::Constant => obj.add_symbol_data(sym, secConsts, &data, 16),
};

syms.insert(name.clone(), (Some(def_section), Some(def_offset), sym));
syms.insert(name.clone(), (None, Some(def_offset), sym));
}

for (name, decl, linkage) in &self.decls { // for extern symbols
Expand All @@ -251,13 +253,13 @@ impl ObjectBuilder {
}

for link in &self.links {
let (from_sec, from_off, _) = syms.get(&link.from).unwrap();
let (_, to_off, to_sym) = syms.get(&link.to).unwrap();
let (_, off, _) = syms.get(&link.from).unwrap();
let (_, _, to_sym) = syms.get(&link.to).unwrap();

obj.add_relocation(from_sec.unwrap().to_owned(), Relocation {
offset: from_off.unwrap() + link.at as u64,
obj.add_relocation(secText, Relocation {
offset: link.at as u64,
symbol: to_sym.to_owned(),
addend: {if let Some(off) = *to_off {off as i64} else {0}} + -4,
addend: link.addend + {if let Some(off) = off { *off as i64} else { 0 }},
flags: RelocationFlags::Generic {
kind: RelocationKind::PltRelative,
encoding: {
Expand Down
10 changes: 0 additions & 10 deletions test.c

This file was deleted.

0 comments on commit e92dc63

Please sign in to comment.