1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
use std::{collections::HashMap, fs, fmt::Display, io::Write}; use std::process::Command;
use gimli::UnwindSection; use object::{Object, ObjectSection};
fn main() -> Result<(), Box<dyn std::error::Error>> { let mut arg = std::env::args(); if arg.len() != 2 { panic!("Argument Error!") } let bin_data = fs::read(arg.nth(1).unwrap())?; let obj_file = object::File::parse(&*bin_data)?; let data = obj_file.section_by_name(".eh_frame").unwrap(); let eh_frame = gimli::read::EhFrame::new(data.data()?, gimli::LittleEndian); let bases = gimli::BaseAddresses::default().set_eh_frame(data.address()); let mut entries = eh_frame.entries(&bases);
let mut file = fs::OpenOptions::new().append(false).truncate(true).write(true).create(true).open("./output.c")?; writeln!(file, "#include <stdint.h>")?;
let mut cies = HashMap::new(); while let Some(entry) = entries.next()? { if let gimli::CieOrFde::Fde(partial) = entry { let fde = partial.parse(|_, bases, o| { cies.entry(o) .or_insert_with(|| eh_frame.cie_from_offset(bases, o)) .clone() })?; if fde.entry_len() < 100 { continue; } let mut instructions = fde.instructions(&eh_frame, &bases); use gimli::CallFrameInstruction::*; loop { match instructions.next() { Err(e) => { println!("Failed to decode CFI instruction: {}", e); break; } Ok(Some(ValExpression { register, expression, })) => { println!( "DW_CFA_val_expression ({}, ...)", gimli::X86_64::register_name(register).unwrap_or("{unknown}") ); display_val_expression(register, expression, &mut file)?; } Ok(None) => { break; }
_ => {} } } } } file.flush()?;
Command::new("gcc") .arg("-O3") .arg("./output.c") .arg("-c") .spawn()?;
Ok(()) }
#[derive(Clone, Copy)] struct Val { id: u64, }
impl Val { fn new(id: u64) -> Self { Val { id } } }
struct ValGenerator { id: u64, }
impl ValGenerator { fn new() -> Self { Self { id: 0 } } fn next(&mut self) -> Val { self.id += 1; Val::new(self.id - 1) } }
impl Display for Val { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "v{}", self.id) } }
fn display_val_expression<R>(target_reg: gimli::Register, exp: gimli::Expression<R>, w: &mut dyn Write) -> Result<(), Box<dyn std::error::Error>> where R: gimli::Reader, { let mut val_generator = ValGenerator::new(); let mut ops = exp.operations(gimli::Encoding { address_size: 8, format: gimli::Format::Dwarf64, version: 5 }); let mut stack: Vec<Val> = Vec::new(); writeln!(w, "uint64_t cal_{}(uint64_t r12, uint64_t r13, uint64_t r14, uint64_t r15){{", gimli::X86_64::register_name(target_reg).unwrap())?; writeln!(w, " uint64_t rax=0,rbx=0;")?; loop { if let Ok(Some(op)) = ops.next() { match op { gimli::Operation::Drop => { stack.pop(); } gimli::Operation::Pick { index } => { let val1 = stack.get(stack.len() - 1 - index as usize).unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={};", new_val, val1)?; stack.push(new_val); } gimli::Operation::Swap => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); stack.push(val1); stack.push(val2); } gimli::Operation::Rot => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let val3 = stack.pop().unwrap(); stack.push(val1); stack.push(val3); stack.push(val2); } gimli::Operation::And => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}&{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Minus => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}-{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Neg => { let val = stack.get(stack.len() - 1).unwrap(); writeln!(w, " {}=-{};", val, val)?; } gimli::Operation::Not => { let val = stack.get(stack.len() - 1).unwrap(); writeln!(w, " {}=~{};", val, val)?; } gimli::Operation::Or => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}|{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Plus => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}+{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::PlusConstant { value } => { let val = stack.get(stack.len() - 1).unwrap(); writeln!(w, " {}+={}ull;", val, value)?; } gimli::Operation::Shl => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}<<{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Shr => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}>>{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Shra => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}=(uint64_t)((int64_t){}>>(int64_t){});", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Xor => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}^{};", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Eq => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}= {}=={}?1:0;", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Ge => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}>={}?1:0;", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Gt => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}>{}?1:0;", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Le => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}<={}?1:0;", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Lt => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}<{}?1:0;", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::Ne => { let val1 = stack.pop().unwrap(); let val2 = stack.pop().unwrap(); let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}!={}?1:0;", new_val, val2, val1)?; stack.push(new_val); } gimli::Operation::UnsignedConstant { value } => { let new_val = val_generator.next(); writeln!(w, " uint64_t {}={}ull;", new_val, value)?; stack.push(new_val); } gimli::Operation::SignedConstant { value } => { let new_val = val_generator.next(); writeln!(w, " uint64_t {}=(uint64_t){}ll;", new_val, value)?; stack.push(new_val); } gimli::Operation::RegisterOffset { register, offset, base_type} => { let new_val = val_generator.next(); writeln!(w, " uint64_t {}={};", new_val, gimli::X86_64::register_name(register).unwrap_or("{error}"))?; stack.push(new_val); } _ => todo!("{:?}", op) } } else { break; } } assert_eq!(stack.len(), 1); writeln!(w, " return {};", stack.pop().unwrap())?; writeln!(w, "}}\n")?; Ok(()) }
|