Skip to content

Commit 4716332

Browse files
committed
#137 fix, elf64 improvements
1 parent 45effdb commit 4716332

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

crates/libmwemu/src/constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ pub fn get_crypto_key_len(value: u32) -> usize {
353353
pub const PT_LOAD: u32 = 1;
354354
pub const ELF_PAGE_SIZE: u64 = 4096;
355355
pub const ELF_PAGE_MASK: u64 = ELF_PAGE_SIZE - 1;
356+
pub const ELF64_DYN_BASE: u64 = 0x555555554000;
357+
pub const ELF64_STA_BASE: u64 = 0x400000;
358+
pub const LIBC_BASE: u64 = 0x7ffff7da7000;
359+
pub const LD_BASE: u64 = 0x7ffff7fd2000;
356360

357361
// linux errors
358362
pub const ENOTSOCK: u64 = -1i64 as u64; /* not open sock */

crates/libmwemu/src/elf/elf64.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,10 @@ pub const DT_STRTAB: u64 = 5;
6161
pub const PT_DYNAMIC: u32 = 2;
6262
pub const STT_FUNC: u8 = 2;
6363
pub const STT_OBJECT: u8 = 1;
64-
pub const ELF64_DYN_BASE: u64 = 0x555555554000;
65-
pub const ELF64_STA_BASE: u64 = 0x400000;
66-
pub const LIBC_BASE: u64 = 0x7ffff7da7000;
67-
pub const LD_BASE: u64 = 0x7ffff7fd2000;
6864

6965
#[derive(Debug)]
7066
pub struct Elf64 {
67+
pub base: u64,
7168
pub bin: Vec<u8>,
7269
pub elf_hdr: Elf64Ehdr,
7370
pub elf_phdr: Vec<Elf64Phdr>,
@@ -122,6 +119,7 @@ impl Elf64 {
122119
let dynstr: Vec<String> = Vec::new();
123120

124121
Ok(Elf64 {
122+
base: 0,
125123
bin,
126124
elf_hdr: ehdr,
127125
elf_phdr: ephdr,
@@ -245,15 +243,15 @@ impl Elf64 {
245243
force_base: u64,
246244
) {
247245

248-
let mut elf64_base: u64;
249-
246+
let elf64_base: u64;
250247

251248
if dynamic_linking {
252-
elf64_base = ELF64_DYN_BASE;
249+
elf64_base = constants::ELF64_DYN_BASE;
253250
self.load_programs(maps, name, is_lib, dynamic_linking);
254251
} else {
255-
elf64_base = ELF64_STA_BASE;
256-
if force_base != constants::CFG_DEFAULT_BASE {
252+
if force_base == constants::CFG_DEFAULT_BASE {
253+
elf64_base = constants::ELF64_STA_BASE;
254+
} else {
257255
elf64_base = force_base;
258256
}
259257

@@ -264,6 +262,8 @@ impl Elf64 {
264262
hdr.write_bytes(elf64_base, &self.bin[..512]);
265263
}
266264

265+
self.base = elf64_base;
266+
267267
// pre-load .dynstr
268268
for shdr in &self.elf_shdr {
269269
let sname = self.get_section_name(shdr.sh_name as usize);
@@ -381,7 +381,7 @@ impl Elf64 {
381381
pub fn craft_got_sym(&self, addr: u64, got: &mut Mem64, sym_name: &str) {
382382
if let Some(mut sym_addr) = self.sym_get_addr_from_name(sym_name) {
383383
if sym_name.contains("libc") {
384-
sym_addr += LIBC_BASE;
384+
sym_addr += constants::LIBC_BASE;
385385
}
386386
log::info!("crafting got 0x{:x} <- 0x{:x} {}", addr, sym_addr, sym_name);
387387
got.write_qword(addr, sym_addr);

crates/libmwemu/src/emu/loaders.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ impl Emu {
371371
if text_addr == 0 {
372372
panic!(".text not found on this elf64");
373373
}
374+
log::info!("text_addr: 0x{:x}", text_addr);
374375

375376
// entry point logic:
376377

@@ -386,7 +387,7 @@ impl Emu {
386387

387388
// 3. Entry point points above .text, relative entry point
388389
} else if elf64.elf_hdr.e_entry < text_addr {
389-
self.regs_mut().rip = elf64.elf_hdr.e_entry + text_addr;
390+
self.regs_mut().rip = elf64.elf_hdr.e_entry + elf64.base; //text_addr;
390391
println!("relative entry point: 0x{:x} fixed: 0x{:x}", elf64.elf_hdr.e_entry, self.regs().rip);
391392

392393
// 4. Entry point points below .text, weird case.
@@ -575,4 +576,4 @@ impl Emu {
575576
code.write_bytes(base, bytes);
576577
self.regs_mut().rip = code.get_base();
577578
}
578-
}
579+
}

0 commit comments

Comments
 (0)