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
pub mod ast;
mod compiler;
pub mod parser;
mod debug;
mod x64data;

use crate::State;
use crate::arch::{Arch, Error, BasicExprBuilder};
use crate::common::{Size, Stmt, Jump};

#[cfg(feature = "dynasm_opmap")]
pub use debug::create_opmap;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum X86Mode {
    Long,
    Protected
}

struct Context<'a> {
    pub state: &'a mut dyn BasicExprBuilder,
    pub mode: X86Mode,
    pub features: x64data::Features
}

#[derive(Clone, Debug)]
pub struct Archx64 {
    features: x64data::Features,
}

#[derive(Clone, Debug)]
pub struct Archx86 {
    features: x64data::Features,
}

#[derive(Debug)]
pub struct InstructionX64 {
    pub inst: ast::Instruction,
    pub args: Vec<ast::CleanArg>,
}

#[derive(Debug)]
pub struct InstructionX86 {
    pub inst: ast::Instruction,
    pub args: Vec<ast::CleanArg>,
}

impl Default for Archx64 {
    fn default() -> Archx64 {
        Archx64 { features: x64data::Features::all() }
    }
}

impl Default for Archx86 {
    fn default() -> Archx86 {
        Archx86 { features: x64data::Features::all() }
    }
}

pub trait AssembleX64 {
    /// Turn an expression into binary format.
    /// May error when dynamic data is present at bad locations such as memory address scaling.
    fn compile_instruction(&mut self, arch: &Archx64, _: InstructionX64) -> Result<(), Error>;

    /// Create an instruction composed from dynamic data.
    /// Only available when the type is also capable of building new composite expressions.
    fn build_instruction(&mut self, arch: &Archx64, _: InstructionX64) -> Result<(), Error>
        where Self: BasicExprBuilder;
}

pub trait AssembleX86 {
    /// Turn an expression into binary format.
    /// May error when dynamic data is present at bad locations such as memory address scaling.
    fn compile_instruction(&mut self, arch: &Archx86, _: InstructionX86) -> Result<(), Error>;

    /// Create an instruction composed from dynamic data.
    /// Only available when the type is also capable of building new composite expressions.
    fn build_instruction(&mut self, arch: &Archx86, _: InstructionX86) -> Result<(), Error>
        where Self: BasicExprBuilder;
}

impl Arch for Archx64 {
    fn name(&self) -> &str {
        "x64"
    }

    fn set_features(&mut self, features: &[String]) {
        let mut new_features = x64data::Features::empty();
        for ident in features {
            new_features |= match x64data::Features::from_str(&ident.to_string()) {
                Some(feature) => feature,
                None => {
                    eprintln!("Architecture x64 does not support feature '{}'", ident.to_string());
                    continue;
                }
            }
        }
        self.features = new_features;
    }

    fn handle_static_reloc(&self, stmts: &mut Vec<Stmt>, reloc: Jump, size: Size) {
        let data = [0, size.in_bytes()]; // no offset, specified size, relative implicit

        stmts.push(Stmt::zeroed(size));
        stmts.push(reloc.encode(&data));
    }

    fn default_align(&self) -> u8 {
        0x90
    }
}

impl AssembleX64 for State<'_> {
    fn compile_instruction(&mut self, arch: &Archx64, instruction: InstructionX64) -> Result<(), Error> {
        let InstructionX64 { inst, args } = instruction;

        let ctx = Context {
            state: self,
            mode: X86Mode::Long,
            features: arch.features,
        };

        compiler::compile_instruction(ctx, inst, args)
    }

    fn build_instruction(&mut self, _: &Archx64, _: InstructionX64) -> Result<(), Error>
        where Self: BasicExprBuilder
    {
        unreachable!("Statically uncallable, Self is not BasicExprBuilder")
    }
}

impl Arch for Archx86 {
    fn name(&self) -> &str {
        "x86"
    }

    fn set_features(&mut self, features: &[String]) {
        let mut new_features = x64data::Features::empty();
        for ident in features {
            new_features |= match x64data::Features::from_str(&ident.to_string()) {
                Some(feature) => feature,
                None => {
                    eprintln!("Architecture x86 does not support feature '{}'", ident.to_string());
                    continue;
                }
            }
        }
        self.features = new_features;
    }

    fn handle_static_reloc(&self, stmts: &mut Vec<Stmt>, reloc: Jump, size: Size) {
        let data = [0, size.in_bytes(), 0]; // no offset, specified size, relative

        stmts.push(Stmt::zeroed(size));
        stmts.push(reloc.encode(&data));
    }

    fn default_align(&self) -> u8 {
        0x90
    }
}

impl AssembleX86 for State<'_> {
    fn compile_instruction(&mut self, arch: &Archx86, instruction: InstructionX86) -> Result<(), Error> {
        let InstructionX86 { inst, args } = instruction;

        let ctx = Context {
            state: self,
            mode: X86Mode::Protected,
            features: arch.features,
        };

        compiler::compile_instruction(ctx, inst, args)
    }

    fn build_instruction(&mut self, _: &Archx86, _: InstructionX86) -> Result<(), Error>
        where Self: BasicExprBuilder
    {
        unreachable!("Statically uncallable, Self is not BasicExprBuilder")
    }
}