IMPORTS WORKING
This commit is contained in:
@@ -26,6 +26,26 @@ impl ByteEncoder {
|
||||
self.data.resize(self.data.len().next_multiple_of(align), 0);
|
||||
}
|
||||
|
||||
fn ptr_at<T>(&mut self, index: usize) -> *mut T {
|
||||
let slice = &mut self.data[index..index + size_of::<T>()];
|
||||
(slice as *mut [u8]) as *mut T
|
||||
}
|
||||
|
||||
pub fn set_at<T>(&mut self, index: usize, val: T) {
|
||||
let ptr = self.ptr_at::<T>(index);
|
||||
unsafe {
|
||||
ptr.write_unaligned(val);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn edit_at<T>(&mut self, index: usize, edit: impl FnOnce(T) -> T) {
|
||||
let ptr = self.ptr_at::<T>(index);
|
||||
unsafe {
|
||||
let val = ptr.read_unaligned();
|
||||
ptr.write_unaligned(edit(val));
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn reserve<T>(&mut self) -> Reserved<T> {
|
||||
let pos = self.pos();
|
||||
|
||||
@@ -1,18 +1,9 @@
|
||||
use crate::backend::pe::data_dir::DataDir;
|
||||
use crate::backend::{LibImport, pe::data_dir::DataDir};
|
||||
|
||||
use super::ByteEncoder;
|
||||
|
||||
pub struct LibImport {
|
||||
pub name: String,
|
||||
pub syms: Vec<SymImport>,
|
||||
}
|
||||
|
||||
pub struct SymImport {
|
||||
name: String,
|
||||
usages: Vec<usize>,
|
||||
}
|
||||
|
||||
pub fn encode(data: &mut ByteEncoder, imports: &[LibImport]) -> DataDir {
|
||||
pub fn encode(data: &mut ByteEncoder, imports: &[LibImport], code_start: usize) -> DataDir {
|
||||
data.align(4);
|
||||
let start = data.pos() as u32;
|
||||
let idt = data.reserve_arr::<ImportDirTable>(imports.len());
|
||||
// null entry to mark end
|
||||
@@ -30,7 +21,6 @@ pub fn encode(data: &mut ByteEncoder, imports: &[LibImport]) -> DataDir {
|
||||
let lookup_start = data.pos();
|
||||
let lookup = data.reserve_arr::<ImportLookupEntry>(import.syms.len());
|
||||
data.pad(size_of::<ImportLookupEntry>());
|
||||
let lookup_end = data.pos();
|
||||
|
||||
for (i, sym) in import.syms.iter().enumerate() {
|
||||
let rva = hint_name_entry(data, 0, &sym.name);
|
||||
@@ -40,9 +30,17 @@ pub fn encode(data: &mut ByteEncoder, imports: &[LibImport]) -> DataDir {
|
||||
// address table
|
||||
data.align(size_of::<ImportLookupEntry>());
|
||||
let addr_start = data.pos();
|
||||
let len = lookup_end - lookup_start;
|
||||
data.pad(len);
|
||||
data.data.copy_within(lookup_start..lookup_end, addr_start);
|
||||
for (i, sym) in import.syms.iter().enumerate() {
|
||||
let here = data.pos() as i32;
|
||||
for &usage in &sym.usages {
|
||||
// NOTE: sets relative offet rn
|
||||
let code_pos = code_start + usage;
|
||||
data.set_at::<i32>(code_pos, here - code_pos as i32 - 4);
|
||||
}
|
||||
let entry = data[lookup][i];
|
||||
data.val(&entry);
|
||||
}
|
||||
data.pad(size_of::<ImportLookupEntry>());
|
||||
|
||||
// entry
|
||||
data[idt][i] = ImportDirTable {
|
||||
@@ -69,6 +67,7 @@ pub struct ImportDirTable {
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ImportLookupEntry(u64);
|
||||
|
||||
impl ImportLookupEntry {
|
||||
|
||||
@@ -3,13 +3,12 @@ mod header;
|
||||
mod import;
|
||||
|
||||
use super::*;
|
||||
use crate::backend::LinkedProgram;
|
||||
use crate::backend::{LibImport, LinkedProgram};
|
||||
|
||||
use data_dir::*;
|
||||
use header::*;
|
||||
pub use import::LibImport;
|
||||
|
||||
pub fn create(program: &[u8], start_offset: u64, imports: &[LibImport]) -> Vec<u8> {
|
||||
pub fn create(program: &LinkedProgram<u64>) -> Vec<u8> {
|
||||
let mut data = ByteEncoder::default();
|
||||
let file_align = 1;
|
||||
let section_align = 1;
|
||||
@@ -44,22 +43,26 @@ pub fn create(program: &[u8], start_offset: u64, imports: &[LibImport]) -> Vec<u
|
||||
let code_sect = data.reserve::<Section>();
|
||||
let hdr_size = data.pos() as u32;
|
||||
|
||||
let code_start = data.pos() as u32;
|
||||
if !imports.is_empty() {
|
||||
let import_rva = import::encode(&mut data, &imports);
|
||||
// .text start
|
||||
let text_start = data.pos() as u32;
|
||||
|
||||
let code_start = data.pos();
|
||||
data.extend(&program.code);
|
||||
|
||||
if !program.imports.is_empty() {
|
||||
let import_rva = import::encode(&mut data, &program.imports, code_start);
|
||||
data[data_dirs].import = import_rva;
|
||||
}
|
||||
|
||||
let program_start = data.pos() as u32;
|
||||
data.extend(program);
|
||||
let code_size = data.pos() as u32 - code_start;
|
||||
let text_size = data.pos() as u32 - text_start;
|
||||
// .text end
|
||||
|
||||
data[code_sect] = Section {
|
||||
name: *b".text\0\0\0",
|
||||
virtual_size: code_size,
|
||||
virtual_size: text_size,
|
||||
virtual_addr: hdr_size.next_multiple_of(section_align),
|
||||
raw_data_size: code_size.next_multiple_of(file_align),
|
||||
raw_data_ptr: code_start,
|
||||
raw_data_size: text_size.next_multiple_of(file_align),
|
||||
raw_data_ptr: text_start,
|
||||
reloc_ptr: 0,
|
||||
line_num_ptr: 0,
|
||||
num_relocs: 0,
|
||||
@@ -73,11 +76,11 @@ pub fn create(program: &[u8], start_offset: u64, imports: &[LibImport]) -> Vec<u
|
||||
magic: 0x20b,
|
||||
major_linker_ver: 8,
|
||||
minor_linker_ver: 0,
|
||||
code_size: code_size.next_multiple_of(file_align),
|
||||
code_size: text_size.next_multiple_of(file_align),
|
||||
init_data_size: 0,
|
||||
uninit_data_size: 0,
|
||||
entry_addr: program_start + start_offset as u32,
|
||||
code_base: code_start,
|
||||
entry_addr: (code_start as u64 + program.entry.unwrap()) as u32,
|
||||
code_base: text_start,
|
||||
image_base: 0x400000,
|
||||
section_align,
|
||||
file_align,
|
||||
@@ -105,7 +108,7 @@ pub fn create(program: &[u8], start_offset: u64, imports: &[LibImport]) -> Vec<u
|
||||
}
|
||||
|
||||
impl LinkedProgram<u64> {
|
||||
pub fn to_pe(&self, imports: &[LibImport]) -> Vec<u8> {
|
||||
create(&self.code, self.entry.expect("no start"), imports)
|
||||
pub fn to_pe(&self) -> Vec<u8> {
|
||||
create(&self)
|
||||
}
|
||||
}
|
||||
|
||||
+27
-7
@@ -11,6 +11,7 @@ pub struct Program<A: Arch> {
|
||||
pub entry: Option<Symbol>,
|
||||
pub external: Vec<External>,
|
||||
|
||||
sym_info: Vec<SymInfo>,
|
||||
sym_count: usize,
|
||||
}
|
||||
|
||||
@@ -29,6 +30,11 @@ pub struct External {
|
||||
pub syms: Vec<Symbol>,
|
||||
}
|
||||
|
||||
pub struct SymInfo {
|
||||
pub name: String,
|
||||
pub external: bool,
|
||||
}
|
||||
|
||||
pub enum Instr<A: Arch> {
|
||||
Set { dst: VarId, src: Vec<u8> },
|
||||
Call { dst: FnId, args: Vec<VarId> },
|
||||
@@ -48,16 +54,22 @@ impl<A: Arch> Program<A> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ro_data(&mut self, bytes: impl Into<Vec<u8>>) -> Symbol {
|
||||
pub fn ro_data(&mut self, name: impl Into<String>, bytes: impl Into<Vec<u8>>) -> Symbol {
|
||||
let bytes = bytes.into();
|
||||
let sym = self.reserve();
|
||||
let sym = self.reserve(SymInfo {
|
||||
name: name.into(),
|
||||
external: false,
|
||||
});
|
||||
self.ro_data.push(Data { bytes, sym });
|
||||
sym
|
||||
}
|
||||
|
||||
pub fn func(&mut self, instrs: impl Into<Vec<Instr<A>>>) -> Symbol {
|
||||
pub fn func(&mut self, name: impl Into<String>, instrs: impl Into<Vec<Instr<A>>>) -> Symbol {
|
||||
let instrs = instrs.into();
|
||||
let sym = self.reserve();
|
||||
let sym = self.reserve(SymInfo {
|
||||
name: name.into(),
|
||||
external: false,
|
||||
});
|
||||
self.funcs.push(Func { instrs, sym });
|
||||
sym
|
||||
}
|
||||
@@ -68,8 +80,10 @@ impl<A: Arch> Program<A> {
|
||||
names: [impl Into<String>; LEN],
|
||||
) -> [Symbol; LEN] {
|
||||
let syms = names.map(|s| {
|
||||
let sym = self.reserve();
|
||||
sym
|
||||
self.reserve(SymInfo {
|
||||
name: s.into(),
|
||||
external: true,
|
||||
})
|
||||
});
|
||||
self.external.push(External {
|
||||
file: file.into(),
|
||||
@@ -78,8 +92,9 @@ impl<A: Arch> Program<A> {
|
||||
syms
|
||||
}
|
||||
|
||||
fn reserve(&mut self) -> Symbol {
|
||||
fn reserve(&mut self, info: SymInfo) -> Symbol {
|
||||
let res = Symbol(self.sym_count);
|
||||
self.sym_info.push(info);
|
||||
self.sym_count += 1;
|
||||
res
|
||||
}
|
||||
@@ -91,6 +106,10 @@ impl<A: Arch> Program<A> {
|
||||
pub fn sym_count(&self) -> usize {
|
||||
self.sym_count
|
||||
}
|
||||
|
||||
pub fn sym_info(&self, sym: Symbol) -> &SymInfo {
|
||||
&self.sym_info[sym.0]
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Arch> Default for Program<A> {
|
||||
@@ -101,6 +120,7 @@ impl<A: Arch> Default for Program<A> {
|
||||
entry: Default::default(),
|
||||
sym_count: Default::default(),
|
||||
external: Default::default(),
|
||||
sym_info: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
pub struct LinkedProgram<Addr> {
|
||||
pub code: Vec<u8>,
|
||||
pub entry: Option<Addr>,
|
||||
pub imports: Vec<LibImport>,
|
||||
}
|
||||
|
||||
pub struct LibImport {
|
||||
pub name: String,
|
||||
pub syms: Vec<SymImport>,
|
||||
}
|
||||
|
||||
pub struct SymImport {
|
||||
pub name: String,
|
||||
pub usages: Vec<usize>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user