diff --git a/examples/obj.rs b/examples/obj.rs index 2161054f..6a9bb651 100644 --- a/examples/obj.rs +++ b/examples/obj.rs @@ -8,16 +8,33 @@ fn main() -> Result<(), Box> { ); 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")? diff --git a/output.o b/output.o deleted file mode 100644 index 9a257862..00000000 Binary files a/output.o and /dev/null differ diff --git a/src/Obj/wrapper.rs b/src/Obj/wrapper.rs index 731d908e..1282f237 100644 --- a/src/Obj/wrapper.rs +++ b/src/Obj/wrapper.rs @@ -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; @@ -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 @@ -213,11 +215,11 @@ 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), @@ -225,7 +227,7 @@ impl ObjectBuilder { 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 @@ -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: { diff --git a/test.c b/test.c deleted file mode 100644 index ed928f7d..00000000 --- a/test.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -int test_data = 5; - -extern int test(); - -int main() { - printf("func() => %d", test()); - return 0; -} \ No newline at end of file