2000-12-29 05:23:09 +03:00
|
|
|
/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
2002-03-19 07:30:17 +03:00
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
2000-12-29 05:23:09 +03:00
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is foldelf.cpp, released November 28, 2000.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2000 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Edward Kandrot <kandrot@netscape.com>
|
|
|
|
* Chris Waterson <waterson@netscape.com>
|
|
|
|
*
|
|
|
|
* This program reads an ELF file and computes information about
|
|
|
|
* redundancies.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2001-01-02 10:44:26 +03:00
|
|
|
#include <list>
|
2000-12-29 05:23:09 +03:00
|
|
|
#include <elf.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2001-01-02 10:44:26 +03:00
|
|
|
//#include <byteswap.h>
|
2000-12-29 05:23:09 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool gDebug=false;
|
2001-01-02 10:44:26 +03:00
|
|
|
bool gCompact=false;
|
|
|
|
bool gOptimize=false;
|
2001-01-02 12:19:35 +03:00
|
|
|
bool gAssembly=false;
|
2001-01-04 08:35:23 +03:00
|
|
|
bool gfirst=false;
|
2001-01-02 10:44:26 +03:00
|
|
|
|
|
|
|
#define bswap_32(x) \
|
|
|
|
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
|
|
|
|
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
|
2000-12-29 05:23:09 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
hexdump(ostream& out, const char* bytes, size_t count)
|
|
|
|
{
|
|
|
|
hex(out);
|
|
|
|
|
|
|
|
size_t off = 0;
|
|
|
|
while (off < count) {
|
|
|
|
out.form("%08lx: ", off);
|
|
|
|
|
|
|
|
const char* p = bytes + off;
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
while (j < 16) {
|
|
|
|
out.form("%02x", p[j++] & 0xff);
|
|
|
|
if (j + off >= count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
out.form("%02x ", p[j++] & 0xff);
|
|
|
|
if (j + off >= count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pad
|
|
|
|
for (; j < 16; ++j)
|
|
|
|
out << ((j%2) ? " " : " ");
|
|
|
|
|
|
|
|
for (j = 0; j < 16; ++j) {
|
|
|
|
if (j + off < count)
|
|
|
|
out.put(isprint(p[j]) ? p[j] : '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
out << endl;
|
|
|
|
off += 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
int
|
|
|
|
verify_elf_header(const Elf32_Ehdr* hdr)
|
|
|
|
{
|
|
|
|
if (hdr->e_ident[EI_MAG0] != ELFMAG0
|
|
|
|
|| hdr->e_ident[EI_MAG1] != ELFMAG1
|
|
|
|
|| hdr->e_ident[EI_MAG2] != ELFMAG2
|
|
|
|
|| hdr->e_ident[EI_MAG3] != ELFMAG3) {
|
|
|
|
cerr << "not an elf file" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr->e_ident[EI_CLASS] != ELFCLASS32) {
|
|
|
|
cerr << "not a 32-bit elf file" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr->e_ident[EI_DATA] != ELFDATA2LSB) {
|
|
|
|
cerr << "not a little endian elf file" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr->e_ident[EI_VERSION] != EV_CURRENT) {
|
|
|
|
cerr << "incompatible version" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class elf_symbol : public Elf32_Sym
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
elf_symbol(const Elf32_Sym& sym)
|
|
|
|
{ ::memcpy(static_cast<Elf32_Sym*>(this), &sym, sizeof(Elf32_Sym)); }
|
|
|
|
|
|
|
|
friend bool operator==(const elf_symbol& lhs, const elf_symbol& rhs) {
|
|
|
|
return 0 == ::memcmp(static_cast<const Elf32_Sym*>(&lhs),
|
|
|
|
static_cast<const Elf32_Sym*>(&rhs),
|
|
|
|
sizeof(Elf32_Sym)); }
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
st_bind(unsigned char info)
|
|
|
|
{
|
|
|
|
switch (ELF32_ST_BIND(info)) {
|
|
|
|
case STB_LOCAL: return "local";
|
|
|
|
case STB_GLOBAL: return "global";
|
|
|
|
case STB_WEAK: return "weak";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
st_type(unsigned char info)
|
|
|
|
{
|
|
|
|
switch (ELF32_ST_TYPE(info)) {
|
|
|
|
case STT_NOTYPE: return "none";
|
|
|
|
case STT_OBJECT: return "object";
|
|
|
|
case STT_FUNC: return "func";
|
|
|
|
case STT_SECTION: return "section";
|
|
|
|
case STT_FILE: return "file";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char
|
|
|
|
st_type(const char* type)
|
|
|
|
{
|
|
|
|
if (strcmp(type, "none") == 0) {
|
|
|
|
return STT_NOTYPE;
|
|
|
|
}
|
|
|
|
else if (strcmp(type, "object") == 0) {
|
|
|
|
return STT_OBJECT;
|
|
|
|
}
|
|
|
|
else if (strcmp(type, "func") == 0) {
|
|
|
|
return STT_FUNC;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
int DIGIT_MAP[256] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x07
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, // 0x38 - 0x3f
|
|
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, // 0x40 - 0x47
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, // 0x78 - 0x7f
|
|
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, // 0x80 - 0x87
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7, // 0xb8 - 0xbf
|
|
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, // 0xc0 - 0xc7
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
5, 5, 5, 5, 5, 5, 5, 5,
|
|
|
|
6, 6, 6, 6, 6, 6, 6, 6,
|
|
|
|
7, 7, 7, 7, 7, 7, 7, 7}; // 0xf8 - 0xff
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dceclation of the Instruction types
|
|
|
|
*/
|
|
|
|
|
2000-12-30 15:45:08 +03:00
|
|
|
enum eRegister {
|
|
|
|
kNoReg = -1,
|
2000-12-31 09:59:21 +03:00
|
|
|
keax = 0, kecx, kedx, kebx, kesp, kebp, kesi, kedi,
|
|
|
|
kcs, kss, kds, kes, kfs, kgs
|
2000-12-30 12:30:34 +03:00
|
|
|
};
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
char reg_name[14][4] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
|
|
|
|
"cs", "ss", "ds", "es", "fs", "gs" };
|
|
|
|
|
|
|
|
|
2000-12-30 15:45:08 +03:00
|
|
|
enum eInstruction {
|
2000-12-31 01:12:54 +03:00
|
|
|
kunknown, kpush, kadd, ksub, kcmp, kmov, kjmp, klea,
|
2001-01-04 08:35:23 +03:00
|
|
|
kinc, kpop, kxor, knop, kret, kcall, kand,
|
2000-12-30 12:30:34 +03:00
|
|
|
};
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
char instr_name[][8] = { "unknown", "push", "add", "sub", "cmp", "mov", "j", "lea",
|
|
|
|
"inc", "pop", "xor", "nop", "ret", "call", "and", };
|
|
|
|
|
|
|
|
|
|
|
|
char cond_name[][4] = { "o", "no", "b", "nb", "z", "nz", "be", "nbe",
|
|
|
|
"s", "ns", "pe", "po", "l", "ge", "le", "g", "mp", "cxz" };
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
enum eCond {
|
|
|
|
kjo, kjno, kjb, kjnb, kjz, kjnz, kjbe, kjnbe,
|
|
|
|
kjs, kjns, kjpe, kjpo, kjl, kjge, kjle, kjg, kjt, kjcxz
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
const int kBaseFormatMask = (1<<5) -1;
|
|
|
|
enum eFormat {
|
|
|
|
kfNone, kfreg, kfrm32, kfrm32_r32, kfr32_rm32,
|
2001-01-01 02:20:00 +03:00
|
|
|
kfmImm8 = 1 << 5, // set on if imm8
|
|
|
|
kfmImm32 = 1 << 6, // set on if imm32
|
|
|
|
kfmSize8 = 1 << 7, // set on if 8 bit
|
|
|
|
kfmSize16 = 1 << 8, // set on if 16 bit
|
2001-01-02 10:44:26 +03:00
|
|
|
kfmDeref = 1 << 9,
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef unsigned char uchar;
|
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
//******************************** Base OpCode Classes **********************
|
|
|
|
|
|
|
|
|
2000-12-30 15:45:08 +03:00
|
|
|
struct CInstruction {
|
2000-12-31 09:59:21 +03:00
|
|
|
CInstruction();
|
|
|
|
|
|
|
|
int fSize; // size of the instruction in bytes
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
eInstruction fInstr;
|
|
|
|
eFormat fFormat;
|
|
|
|
eRegister fReg1;
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
eRegister fReg2;
|
|
|
|
long fDisp32;
|
2000-12-30 15:45:08 +03:00
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
eRegister fReg3;
|
|
|
|
int fScale;
|
|
|
|
|
|
|
|
long fImm32;
|
|
|
|
|
|
|
|
virtual void output_text( void );
|
2001-01-01 02:20:00 +03:00
|
|
|
virtual void optimize( void ) {}
|
2000-12-31 09:59:21 +03:00
|
|
|
virtual int generate_opcode( uchar *buffer );
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
int am_rm32( uchar *pCode );
|
|
|
|
int am_rm32_reg( uchar *pCode );
|
2001-01-02 10:44:26 +03:00
|
|
|
int am_encode( uchar *buffer, eRegister reg1 );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CFunction {
|
|
|
|
CFunction( string funcName ){fName = funcName;}
|
|
|
|
string fName;
|
|
|
|
list<CInstruction*> fInstructions;
|
|
|
|
|
|
|
|
void get_instructions( uchar *pCode, long codeSize );
|
|
|
|
void remove_nops();
|
2000-12-30 12:30:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
CInstruction::CInstruction()
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
fSize = 1;
|
|
|
|
fInstr = kunknown;
|
|
|
|
fFormat = kfNone;
|
|
|
|
fReg1 = kNoReg;
|
|
|
|
fReg2 = kNoReg;
|
|
|
|
fDisp32 = 0;
|
|
|
|
fReg3 = kNoReg;
|
|
|
|
fScale = 1;
|
|
|
|
fImm32 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct CPop:CInstruction {
|
|
|
|
CPop( eRegister reg )
|
|
|
|
{
|
|
|
|
fInstr = kpop;
|
|
|
|
fFormat = kfreg;
|
|
|
|
fReg1 = reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPop( char imm8 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CPop( long imm32 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CPop( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = kpop;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CPush:CInstruction {
|
|
|
|
CPush( eRegister reg )
|
|
|
|
{
|
|
|
|
fInstr = kpush;
|
|
|
|
fFormat = kfreg;
|
|
|
|
fReg1 = reg;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPush( char imm8 )
|
|
|
|
{
|
2001-01-04 08:35:23 +03:00
|
|
|
fInstr = kpush;
|
|
|
|
fFormat = kfreg;
|
|
|
|
fImm32 = imm8;
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CPush( long imm32 )
|
|
|
|
{
|
2001-01-04 08:35:23 +03:00
|
|
|
fInstr = kpush;
|
|
|
|
fFormat = kfreg;
|
|
|
|
fImm32 = imm32;
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CPush( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = kpush;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CInc:CInstruction {
|
2001-01-02 12:19:35 +03:00
|
|
|
CInc( eRegister reg )
|
|
|
|
{
|
2001-01-04 08:35:23 +03:00
|
|
|
fInstr = kinc;
|
2001-01-02 12:19:35 +03:00
|
|
|
fFormat = kfreg;
|
|
|
|
fReg1 = reg;
|
|
|
|
}
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
CInc( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = kinc;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CMov:CInstruction {
|
2001-01-04 08:35:23 +03:00
|
|
|
CMov( eRegister reg, uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fReg1 = reg;
|
|
|
|
fInstr = kmov;
|
|
|
|
fFormat = format;
|
|
|
|
fSize += am_rm32( pCode );
|
|
|
|
}
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
CMov( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = kmov;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32_reg( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
virtual void optimize( void );
|
2001-01-01 02:20:00 +03:00
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CCmp:CInstruction {
|
|
|
|
CCmp( uchar *pCode, eFormat format )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
fInstr = kcmp;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32_reg( pCode );
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CAdd:CInstruction {
|
|
|
|
CAdd( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = kadd;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32_reg( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
virtual void optimize( void );
|
2001-01-01 02:20:00 +03:00
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CSub:CInstruction {
|
|
|
|
CSub( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = ksub;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32_reg( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual void optimize( void );
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
struct CAnd:CInstruction {
|
|
|
|
CAnd( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = kand;
|
|
|
|
fFormat = format;
|
|
|
|
fSize += am_rm32_reg( pCode );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
struct CXor:CInstruction {
|
|
|
|
CXor( uchar *pCode, eFormat format )
|
2000-12-30 15:45:08 +03:00
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
fInstr = kxor;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32_reg( pCode );
|
2000-12-30 15:45:08 +03:00
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
struct CCall:CInstruction {
|
|
|
|
CCall( long imm32 )
|
|
|
|
{
|
|
|
|
fInstr = kcall;
|
|
|
|
fImm32 = imm32;
|
|
|
|
fSize = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// virtual void output_text( void );
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
struct CJmp:CInstruction {
|
2001-01-02 10:44:26 +03:00
|
|
|
eCond fCond;
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
CJmp( eCond cond, char imm8 )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
fInstr = kjmp;
|
|
|
|
fImm32 = imm8;
|
2001-01-02 10:44:26 +03:00
|
|
|
fCond = cond;
|
2000-12-31 09:59:21 +03:00
|
|
|
fSize = 2;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
2000-12-31 09:59:21 +03:00
|
|
|
|
|
|
|
virtual void output_text( void );
|
2001-01-02 10:44:26 +03:00
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CNop:CInstruction {
|
|
|
|
CNop()
|
|
|
|
{
|
|
|
|
fInstr = knop;
|
|
|
|
fSize = 1;
|
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer ) { buffer[0] = 0x90; return 1; }
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CRet:CInstruction {
|
|
|
|
CRet()
|
|
|
|
{
|
|
|
|
fInstr = kret;
|
|
|
|
fSize = 1;
|
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct CLea:CInstruction {
|
|
|
|
CLea( uchar *pCode, eFormat format )
|
|
|
|
{
|
|
|
|
fInstr = klea;
|
|
|
|
fFormat = format;
|
2001-01-02 12:19:35 +03:00
|
|
|
fSize += am_rm32_reg( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
virtual void optimize( void );
|
2001-01-02 10:44:26 +03:00
|
|
|
virtual int generate_opcode( uchar *buffer );
|
2000-12-31 09:59:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
//************************* Address Mode En/Decoding ************************
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
/*
|
|
|
|
* am_rm32 decodes the destination reg, and assumes that the src reg is a
|
|
|
|
* selector or was set outside of this function
|
|
|
|
*/
|
2001-01-02 12:19:35 +03:00
|
|
|
int CInstruction::am_rm32( uchar *pCode )
|
2000-12-31 09:59:21 +03:00
|
|
|
{
|
|
|
|
unsigned char reg = *pCode++;
|
|
|
|
int isize = 1;
|
|
|
|
|
|
|
|
fReg2 = (eRegister)(reg & 0x07);
|
|
|
|
|
|
|
|
if ( ((reg & 0x07) == 0x04) && // check for SIB
|
|
|
|
((reg & 0xC0) != 0xC0) )
|
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
fFormat = (eFormat)(fFormat | kfmDeref);
|
|
|
|
fReg2 = (eRegister)(*pCode & 0x07);
|
|
|
|
fReg3 = (eRegister)((*pCode & 0x38) >> 3);
|
|
|
|
if ((int)fReg3 = 0x04) fReg3 = kNoReg;
|
|
|
|
fScale = 1 << ((*pCode & 0xC0) >> 6);
|
2000-12-31 09:59:21 +03:00
|
|
|
pCode++;
|
|
|
|
isize++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((reg & 0xC0) == 0x80) // disp32
|
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
fDisp32 = bswap_32( *(long*)pCode );
|
2001-01-02 10:44:26 +03:00
|
|
|
fFormat = (eFormat)(fFormat | kfmDeref);
|
2000-12-31 09:59:21 +03:00
|
|
|
pCode += 4;
|
|
|
|
isize += 4;
|
|
|
|
}
|
|
|
|
else if ((reg & 0xC0) == 0x40) // disp8
|
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
fDisp32 = *(char*)pCode; // need it as a signed value
|
2001-01-02 10:44:26 +03:00
|
|
|
fFormat = (eFormat)(fFormat | kfmDeref);
|
2000-12-31 09:59:21 +03:00
|
|
|
pCode++;
|
|
|
|
isize++;
|
|
|
|
}
|
|
|
|
else if ((reg & 0xC0) == 0x00) // no disp
|
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
fFormat = (eFormat)(fFormat | kfmDeref);
|
2001-01-04 08:35:23 +03:00
|
|
|
if (fReg2 == 0x05) // absolute address
|
|
|
|
{
|
|
|
|
fReg2 = kNoReg;
|
|
|
|
fDisp32 = bswap_32( *(long*)pCode );
|
|
|
|
pCode += 4;
|
|
|
|
isize += 4;
|
|
|
|
}
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fFormat & kfmImm8)
|
2000-12-31 09:59:21 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
fImm32 = *(char*)pCode; // need it as a signed value
|
2000-12-31 09:59:21 +03:00
|
|
|
pCode++;
|
|
|
|
isize++;
|
|
|
|
}
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fFormat & kfmImm32)
|
2000-12-31 09:59:21 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
fImm32 = bswap_32( *(long*)pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
pCode+=4;
|
|
|
|
isize+=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isize;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
int CInstruction::am_rm32_reg( uchar *pCode )
|
2000-12-31 09:59:21 +03:00
|
|
|
{
|
|
|
|
fReg1 = (eRegister)((*pCode & 0x38) >> 3);
|
2001-01-02 12:19:35 +03:00
|
|
|
return am_rm32( pCode );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
int CInstruction::am_encode( uchar *buffer, eRegister reg1 )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
int isize = 1;
|
|
|
|
uchar sib=0;
|
|
|
|
bool use_sib=false;
|
|
|
|
eFormat format=fFormat;
|
|
|
|
|
|
|
|
if ((fScale != 1) ||
|
|
|
|
(fReg3 != kNoReg) ||
|
|
|
|
( (fReg2 == kesp) && (format & kfmDeref) ))
|
|
|
|
{
|
|
|
|
uchar scale=1;
|
|
|
|
switch (fScale)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
scale = 0x00;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
scale = 0x40;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
scale = 0x80;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
scale = 0xC0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fReg3 == kNoReg)
|
|
|
|
sib = scale | (0x04 << 3) | fReg2;
|
|
|
|
else
|
|
|
|
sib = scale | (fReg3 << 3) | fReg2;
|
|
|
|
use_sib = true;
|
2001-01-02 12:19:35 +03:00
|
|
|
*buffer = 0xff; // ek must fix the order of the SIB output -> opcode, sib, disp, imm
|
2001-01-02 10:44:26 +03:00
|
|
|
buffer++;
|
|
|
|
isize++;
|
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
if ( (fReg2 == kNoReg) && (fFormat & kfmDeref) ) // absolute address
|
|
|
|
{
|
|
|
|
*buffer = 0x00 | (reg1 << 3) | 0x05;
|
|
|
|
long bsDisp32 = bswap_32( fDisp32 );
|
|
|
|
memcpy( buffer+1, &bsDisp32, 4 );
|
|
|
|
isize += 4;
|
|
|
|
}
|
|
|
|
else if (fDisp32 == 0)
|
2001-01-01 02:20:00 +03:00
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
if (format & kfmDeref)
|
|
|
|
{
|
|
|
|
*buffer = 0x00 | (reg1 << 3) | fReg2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*buffer = 0xC0 | (reg1 << 3) | fReg2;
|
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
}
|
|
|
|
else if ( (fDisp32 >= -128) && (fDisp32 <= 127) )
|
|
|
|
{
|
|
|
|
*buffer = 0x40 | (reg1 << 3) | fReg2;
|
|
|
|
*(buffer+1) = (uchar)fDisp32;
|
2001-01-02 10:44:26 +03:00
|
|
|
isize++;
|
2001-01-01 02:20:00 +03:00
|
|
|
}
|
2001-01-04 08:35:23 +03:00
|
|
|
else
|
2001-01-01 02:20:00 +03:00
|
|
|
{
|
|
|
|
*buffer = 0x40 | (reg1 << 3) | fReg2;
|
|
|
|
long bsDisp32 = bswap_32( fDisp32 );
|
|
|
|
memcpy( buffer+1, &bsDisp32, 4 );
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += 4;
|
2001-01-01 02:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format & kfmImm8)
|
|
|
|
{
|
|
|
|
buffer[isize] = (uchar)fImm32;
|
|
|
|
isize++;
|
|
|
|
}
|
|
|
|
else if (format & kfmImm32)
|
|
|
|
{
|
|
|
|
long bsImm32 = bswap_32( fImm32 );
|
|
|
|
memcpy( buffer+isize, &bsImm32, 4 );
|
|
|
|
isize += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isize;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
//********************************** OpCode generators ************************
|
|
|
|
|
|
|
|
|
2000-12-30 15:45:08 +03:00
|
|
|
/*
|
2001-01-01 02:20:00 +03:00
|
|
|
* returns the size of the generated opcode, which is in buffer
|
2000-12-30 15:45:08 +03:00
|
|
|
*/
|
2001-01-01 02:20:00 +03:00
|
|
|
int CInstruction::generate_opcode( uchar *buffer )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
buffer[0] = 0x90; // make the default NOP
|
|
|
|
return 0;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CPop::generate_opcode( uchar *buffer )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
if (fFormat == kfreg)
|
|
|
|
{
|
|
|
|
buffer[0] = 0x58 | fReg1;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
|
|
|
|
return 0;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CPush::generate_opcode( uchar *buffer )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
if (fFormat == kfreg)
|
|
|
|
{
|
|
|
|
buffer[0] = 0x50 | fReg1;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
|
|
|
|
return 0;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
int CCall::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
if (fFormat == kfNone)
|
|
|
|
{
|
|
|
|
buffer[0] = 0xE8;
|
|
|
|
long bsImm32 = bswap_32( fImm32 );
|
|
|
|
memcpy( buffer+1, &bsImm32, 4 );
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CRet::generate_opcode( uchar *buffer )
|
2000-12-31 01:12:54 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
if (fFormat == kfNone)
|
|
|
|
{
|
|
|
|
buffer[0] = 0xC3;
|
|
|
|
return 1;
|
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
void CLea::optimize( void )
|
|
|
|
{
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if ( (fDisp32 == 0) && (fReg1 == fReg2) && (fReg3 == kNoReg) )
|
|
|
|
fInstr = knop;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
int CLea::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
buffer[0] = 0x8D;
|
|
|
|
return 1+am_encode( &buffer[1], fReg1 );
|
2000-12-31 01:12:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
void CMov::optimize( void )
|
|
|
|
{
|
|
|
|
if ( (fReg1 == fReg2) && (fReg3 == kNoReg) && !(fFormat & kfmDeref) )
|
|
|
|
fInstr = knop;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CMov::generate_opcode( uchar *buffer )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0xC6;
|
|
|
|
else
|
|
|
|
buffer[0] = 0xC7;
|
|
|
|
}
|
|
|
|
else if (format == kfrm32_r32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x88;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x89;
|
|
|
|
}
|
|
|
|
else if (format == kfr32_rm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x8A;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x8B;
|
|
|
|
}
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], fReg1 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CInc::generate_opcode( uchar *buffer )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
if (fFormat == kfreg)
|
|
|
|
{
|
|
|
|
buffer[0] = 0x40 | fReg1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (kfmSize8 & fFormat)
|
|
|
|
{
|
|
|
|
buffer[0] = 0xFE;
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)0 );
|
2001-01-01 02:20:00 +03:00
|
|
|
}
|
|
|
|
else if (kfmSize16 & fFormat)
|
|
|
|
{
|
|
|
|
buffer[0] = 0xFF;
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)0 );
|
2001-01-01 02:20:00 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[0] = 0xFF;
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)6 );
|
2001-01-01 02:20:00 +03:00
|
|
|
}
|
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isize;
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CCmp::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
2000-12-31 09:59:21 +03:00
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x80;
|
|
|
|
else if (fFormat & kfmImm8)
|
|
|
|
buffer[0] = 0x83;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x81;
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)7 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
else if (format == kfrm32_r32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x38;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x39;
|
|
|
|
}
|
|
|
|
else if (format == kfr32_rm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x3A;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x3B;
|
|
|
|
}
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], fReg1 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
int CAnd::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x80;
|
|
|
|
else if (fFormat & kfmImm8)
|
|
|
|
buffer[0] = 0x83;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x81;
|
|
|
|
|
|
|
|
isize += am_encode( &buffer[1], (eRegister)4 );
|
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
else if (format == kfrm32_r32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x20;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x21;
|
|
|
|
}
|
|
|
|
else if (format == kfr32_rm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x22;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x23;
|
|
|
|
}
|
|
|
|
|
|
|
|
isize += am_encode( &buffer[1], fReg1 );
|
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CXor::generate_opcode( uchar *buffer )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x80;
|
|
|
|
else if (fFormat & kfmImm8)
|
|
|
|
buffer[0] = 0x83;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x81;
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)6 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
else if (format == kfrm32_r32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x30;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x31;
|
|
|
|
}
|
|
|
|
else if (format == kfr32_rm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x32;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x33;
|
|
|
|
}
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], fReg1 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
2000-12-30 15:45:08 +03:00
|
|
|
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
void CAdd::optimize()
|
|
|
|
{
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
eFormat isize = (eFormat)((int)fFormat & (kfmSize8 | kfmSize16 ));
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (kfmSize8 & isize)
|
|
|
|
{
|
|
|
|
fFormat = (eFormat)(format | kfmImm8 | kfmSize8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (fImm32 >= -128) && (fImm32 <= 127) )
|
|
|
|
{
|
|
|
|
fFormat =(eFormat)(format | kfmImm8 | isize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fFormat =(eFormat)(format | kfmImm32 | isize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
int CAdd::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x80;
|
|
|
|
else if (fFormat & kfmImm8)
|
|
|
|
buffer[0] = 0x83;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x81;
|
|
|
|
|
|
|
|
isize += am_encode( &buffer[1], (eRegister)0 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
else if (format == kfrm32_r32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x00;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x01;
|
|
|
|
}
|
|
|
|
else if (format == kfr32_rm32)
|
|
|
|
{
|
|
|
|
if (fFormat & kfmSize8)
|
|
|
|
buffer[0] = 0x02;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x03;
|
|
|
|
}
|
2001-01-01 02:20:00 +03:00
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], fReg1 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CSub::optimize()
|
|
|
|
{
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
eFormat isize = (eFormat)((int)fFormat & (kfmSize8 | kfmSize16 ));
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (kfmSize8 & isize)
|
|
|
|
{
|
|
|
|
fFormat = (eFormat)(format | kfmImm8 | kfmSize8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (fImm32 >= -128) && (fImm32 <= 127) )
|
|
|
|
{
|
|
|
|
fFormat =(eFormat)(format | kfmImm8 | isize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fFormat =(eFormat)(format | kfmImm32 | isize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CSub::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
int isize = 1;
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
|
|
|
|
if (format == kfrm32)
|
|
|
|
{
|
|
|
|
if (kfmSize8 & fFormat)
|
|
|
|
{
|
|
|
|
buffer[0] = 0x80;
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)5 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fFormat | kfmImm8)
|
|
|
|
buffer[0] = 0x83;
|
|
|
|
else
|
|
|
|
buffer[0] = 0x81;
|
2001-01-02 10:44:26 +03:00
|
|
|
isize += am_encode( &buffer[1], (eRegister)5 );
|
2001-01-01 02:20:00 +03:00
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
int CJmp::generate_opcode( uchar *buffer )
|
|
|
|
{
|
|
|
|
int isize = 1;
|
|
|
|
|
|
|
|
if (fCond == kjt)
|
|
|
|
{
|
|
|
|
buffer[0] = 0xEB;
|
|
|
|
}
|
|
|
|
else if (fCond == kjcxz)
|
|
|
|
{
|
|
|
|
buffer[0] = 0xE3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[0] = fCond | 0x70;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[1] = fImm32;
|
|
|
|
isize++;
|
|
|
|
|
|
|
|
return isize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-01 02:20:00 +03:00
|
|
|
//*********************************** Mneumonic Outputers *********************
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
void CInstruction::output_text( void )
|
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
if (fInstr == kunknown) return;
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
cout << instr_name[fInstr] << "\t";
|
|
|
|
|
|
|
|
eFormat format = (eFormat)(kBaseFormatMask & (int)fFormat);
|
|
|
|
switch (format)
|
2000-12-30 15:45:08 +03:00
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
case kfreg:
|
|
|
|
cout << reg_name[fReg1];
|
|
|
|
break;
|
|
|
|
case kfrm32:
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fDisp32) cout << fDisp32;
|
|
|
|
if (fFormat & kfmDeref) cout << "(";
|
2000-12-31 09:59:21 +03:00
|
|
|
cout << reg_name[fReg2];
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fFormat & kfmDeref) cout << ")";
|
2000-12-31 09:59:21 +03:00
|
|
|
break;
|
|
|
|
case kfrm32_r32:
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fDisp32) cout << fDisp32;
|
|
|
|
if (fFormat & kfmDeref) cout << "(";
|
|
|
|
cout << reg_name[fReg2];
|
|
|
|
if (fFormat & kfmDeref) cout << ")";
|
|
|
|
cout << ", ";
|
2000-12-31 09:59:21 +03:00
|
|
|
cout << reg_name[fReg1];
|
|
|
|
break;
|
|
|
|
case kfr32_rm32:
|
|
|
|
cout << reg_name[fReg1] << ", ";
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fDisp32) cout << fDisp32;
|
|
|
|
if (fFormat & kfmDeref) cout << "(";
|
2001-01-04 08:35:23 +03:00
|
|
|
if (fReg2 != kNoReg)
|
|
|
|
cout << reg_name[fReg2];
|
|
|
|
else
|
|
|
|
cout.form( "0x%08X", (long)(bswap_32(fDisp32)) );
|
2001-01-02 12:19:35 +03:00
|
|
|
if (fFormat & kfmDeref) cout << ")";
|
2000-12-31 09:59:21 +03:00
|
|
|
break;
|
2000-12-30 15:45:08 +03:00
|
|
|
}
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
if (fFormat & kfmImm8) cout.form( ", #0x%02X", fImm32 );
|
|
|
|
if (fFormat & kfmImm32) cout.form( ", #0x%08X", fImm32 );
|
|
|
|
|
2000-12-30 15:45:08 +03:00
|
|
|
cout << "\n";
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
void CJmp::output_text( void )
|
2000-12-30 15:45:08 +03:00
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
cout << "j" << cond_name[fCond];
|
2001-01-04 08:35:23 +03:00
|
|
|
// cout.form( "\t.+0x%02X\n", fImm32 );
|
|
|
|
cout.form( "\t.+%ld\n", fImm32 );
|
2000-12-31 09:59:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
//********************* the rest of the code ********************************
|
|
|
|
|
|
|
|
|
|
|
|
int hexdump( CInstruction *instr )
|
2000-12-31 09:59:21 +03:00
|
|
|
{
|
|
|
|
uchar buffer[16];
|
|
|
|
int instrSize = instr->generate_opcode( buffer );
|
2001-01-02 10:44:26 +03:00
|
|
|
if (instrSize)
|
2000-12-30 15:45:08 +03:00
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
for (int i=0; i<instrSize; i++)
|
|
|
|
{
|
|
|
|
cout.form("%02x ", buffer[i]);
|
|
|
|
}
|
|
|
|
cout << "\t";
|
2000-12-30 15:45:08 +03:00
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
|
|
|
|
return instrSize;
|
2000-12-30 15:45:08 +03:00
|
|
|
}
|
2000-12-30 12:30:34 +03:00
|
|
|
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
CInstruction* get_2byte_instruction( uchar *pCode )
|
|
|
|
{
|
|
|
|
CInstruction *retInstr=NULL;
|
|
|
|
uchar *reg = pCode+1;
|
|
|
|
|
|
|
|
switch (*pCode)
|
|
|
|
{
|
|
|
|
case 0xBE:
|
|
|
|
retInstr = new CMov( reg, (eFormat)(kfr32_rm32 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
case 0xBF:
|
|
|
|
retInstr = new CMov( reg, (eFormat)(kfr32_rm32 | kfmSize16) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ek only until the table above is completed
|
|
|
|
if (retInstr == NULL) retInstr = new CInstruction();
|
|
|
|
|
|
|
|
retInstr->fSize++;
|
|
|
|
return retInstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
CInstruction* get_next_instruction( uchar *pCode )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
CInstruction *retInstr=NULL;
|
|
|
|
uchar *reg = pCode+1;
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
switch (*pCode)
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
case 0x00:
|
|
|
|
retInstr = new CAdd( reg, (eFormat)(kfrm32_r32 | kfmSize8) );
|
|
|
|
break;
|
2000-12-30 15:45:08 +03:00
|
|
|
case 0x01:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CAdd( reg, kfrm32_r32 );
|
2000-12-30 15:45:08 +03:00
|
|
|
break;
|
2001-01-02 10:44:26 +03:00
|
|
|
case 0x02:
|
|
|
|
retInstr = new CAdd( reg, (eFormat)(kfr32_rm32 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
case 0x03:
|
|
|
|
retInstr = new CAdd( reg, kfr32_rm32 );
|
|
|
|
break;
|
2001-01-04 08:35:23 +03:00
|
|
|
case 0x06:
|
|
|
|
retInstr = new CPush( kes );
|
|
|
|
break;
|
|
|
|
case 0x0F:
|
|
|
|
retInstr = get_2byte_instruction( reg );
|
|
|
|
break;
|
|
|
|
case 0x25:
|
|
|
|
retInstr = new CAnd( reg, kfr32_rm32 );
|
|
|
|
break;
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0x28:
|
|
|
|
retInstr = new CSub( reg, (eFormat)(kfrm32_r32 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
case 0x29:
|
|
|
|
retInstr = new CSub( reg, kfrm32_r32 );
|
|
|
|
break;
|
|
|
|
case 0x2A:
|
|
|
|
retInstr = new CSub( reg, (eFormat)(kfr32_rm32 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
case 0x2B:
|
|
|
|
retInstr = new CSub( reg, kfr32_rm32 );
|
|
|
|
break;
|
2000-12-31 01:12:54 +03:00
|
|
|
case 0x31:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CXor( reg, kfrm32_r32 );
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0x40:
|
|
|
|
case 0x41:
|
|
|
|
case 0x42:
|
|
|
|
case 0x43:
|
|
|
|
case 0x44:
|
|
|
|
case 0x45:
|
|
|
|
case 0x46:
|
|
|
|
case 0x47:
|
|
|
|
retInstr = new CInc( (eRegister)(*pCode & 0x07) );
|
|
|
|
break;
|
2000-12-31 01:12:54 +03:00
|
|
|
case 0x50:
|
|
|
|
case 0x51:
|
|
|
|
case 0x52:
|
|
|
|
case 0x53:
|
|
|
|
case 0x54:
|
2000-12-30 12:30:34 +03:00
|
|
|
case 0x55:
|
2000-12-31 01:12:54 +03:00
|
|
|
case 0x56:
|
|
|
|
case 0x57:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CPush( (eRegister)(*pCode & 0x07) );
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
|
|
|
case 0x58:
|
|
|
|
case 0x59:
|
|
|
|
case 0x5a:
|
|
|
|
case 0x5b:
|
|
|
|
case 0x5c:
|
|
|
|
case 0x5d:
|
|
|
|
case 0x5e:
|
|
|
|
case 0x5f:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CPop( (eRegister)(*pCode & 0x07) );
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
2001-01-04 08:35:23 +03:00
|
|
|
case 0x6A:
|
|
|
|
retInstr = new CPush( (char)(*reg) );
|
|
|
|
break;
|
|
|
|
case 0x70:
|
|
|
|
case 0x71:
|
|
|
|
case 0x72:
|
|
|
|
case 0x73:
|
|
|
|
case 0x74:
|
|
|
|
case 0x75:
|
|
|
|
case 0x76:
|
|
|
|
case 0x77:
|
|
|
|
case 0x78:
|
|
|
|
case 0x79:
|
|
|
|
case 0x7A:
|
|
|
|
case 0x7B:
|
|
|
|
case 0x7C:
|
|
|
|
case 0x7D:
|
|
|
|
case 0x7E:
|
|
|
|
case 0x7F:
|
|
|
|
retInstr = new CJmp( (eCond)(*pCode - 0x70), (char)*reg );
|
|
|
|
break;
|
2001-01-02 10:44:26 +03:00
|
|
|
case 0x80:
|
|
|
|
switch (DIGIT_MAP[*reg])
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
retInstr = new CAdd( reg, (eFormat)(kfrm32 | kfmImm8 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
retInstr = new CSub( reg, (eFormat)(kfrm32 | kfmImm8 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
retInstr = new CCmp( reg, (eFormat)(kfrm32 | kfmImm8 | kfmSize8) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// retInstr = am_rm32_imm8( kunknown, reg );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x81:
|
|
|
|
switch (DIGIT_MAP[*reg])
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
retInstr = new CAdd( reg, (eFormat)(kfrm32 | kfmImm32) );
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
retInstr = new CSub( reg, (eFormat)(kfrm32 | kfmImm32) );
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
retInstr = new CCmp( reg, (eFormat)(kfrm32 | kfmImm32) );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// retInstr = am_rm32_imm8( kunknown, reg );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2000-12-30 12:30:34 +03:00
|
|
|
case 0x83:
|
2000-12-31 01:12:54 +03:00
|
|
|
switch (DIGIT_MAP[*reg])
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
2001-01-01 02:20:00 +03:00
|
|
|
case 0:
|
|
|
|
retInstr = new CAdd( reg, (eFormat)(kfrm32 | kfmImm8) );
|
|
|
|
break;
|
2000-12-30 12:30:34 +03:00
|
|
|
case 5:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CSub( reg, (eFormat)(kfrm32 | kfmImm8) );
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
|
|
|
case 7:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CCmp( reg, (eFormat)(kfrm32 | kfmImm8) );
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
|
|
|
default:
|
2000-12-31 09:59:21 +03:00
|
|
|
// retInstr = am_rm32_imm8( kunknown, reg );
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x89:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CMov( reg, kfrm32_r32 );
|
2000-12-30 15:45:08 +03:00
|
|
|
break;
|
|
|
|
case 0x88:
|
2000-12-30 12:30:34 +03:00
|
|
|
case 0x8a:
|
2000-12-30 15:45:08 +03:00
|
|
|
break;
|
2000-12-30 12:30:34 +03:00
|
|
|
case 0x8b:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CMov( reg, kfr32_rm32 );
|
2000-12-30 15:45:08 +03:00
|
|
|
break;
|
2000-12-30 12:30:34 +03:00
|
|
|
case 0x8c:
|
|
|
|
case 0x8e:
|
2000-12-30 15:45:08 +03:00
|
|
|
break;
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0x8D:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CLea( reg, kfr32_rm32 );
|
2000-12-30 15:45:08 +03:00
|
|
|
break;
|
2000-12-31 01:12:54 +03:00
|
|
|
case 0x90:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CNop();
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
2001-01-04 08:35:23 +03:00
|
|
|
case 0xB8:
|
|
|
|
case 0xB9:
|
|
|
|
case 0xBA:
|
|
|
|
case 0xBB:
|
|
|
|
case 0xBC:
|
|
|
|
case 0xBD:
|
|
|
|
case 0xBE:
|
|
|
|
case 0xBF:
|
|
|
|
retInstr = new CMov( (eRegister)(*pCode & 0x07), reg, (eFormat)(kfrm32 | kfmImm32) );
|
|
|
|
break;
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0xC3:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CRet();
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0xC7:
|
2000-12-31 09:59:21 +03:00
|
|
|
retInstr = new CMov( reg, (eFormat)(kfrm32 | kfmImm32) );
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
2000-12-31 01:12:54 +03:00
|
|
|
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0xE8:
|
|
|
|
retInstr = new CCall( (long)bswap_32( *(long*)reg ));
|
|
|
|
break;
|
|
|
|
case 0xEB:
|
2001-01-02 10:44:26 +03:00
|
|
|
retInstr = new CJmp( kjt, *reg );
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
2001-01-02 12:19:35 +03:00
|
|
|
case 0xFF:
|
2000-12-31 01:12:54 +03:00
|
|
|
switch (DIGIT_MAP[*reg])
|
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
case 0:
|
2001-01-02 12:19:35 +03:00
|
|
|
retInstr = new CInc( reg, (eFormat)(kfrm32 | kfmSize16) );
|
2000-12-31 09:59:21 +03:00
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
retInstr = new CPush( reg, kfrm32 );
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
|
|
|
default:
|
2000-12-31 09:59:21 +03:00
|
|
|
// retInstr = am_rm32_imm8( kunknown, reg );
|
2000-12-31 01:12:54 +03:00
|
|
|
break;
|
|
|
|
}
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-12-31 09:59:21 +03:00
|
|
|
// retInstr = am_rm32_imm8( kunknown, reg);
|
2000-12-30 12:30:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
// ek only until the table above is completed
|
2001-01-04 08:35:23 +03:00
|
|
|
if (retInstr == NULL)
|
|
|
|
{
|
|
|
|
if (!gfirst)
|
|
|
|
{
|
|
|
|
cout << (unsigned long)*pCode << " was the first\n";
|
|
|
|
cout << (unsigned long)*(pCode+1) << "\n";
|
|
|
|
}
|
|
|
|
gfirst = true;
|
|
|
|
retInstr = new CInstruction();
|
|
|
|
}
|
2000-12-31 09:59:21 +03:00
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
return retInstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
//************************* Function Level Code ******************************
|
|
|
|
|
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
/*
|
|
|
|
* Takes the compile intel code and tries to optimize it.
|
|
|
|
*
|
|
|
|
* returns the number of bytes smaller the new code is.
|
|
|
|
*/
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
void CFunction::get_instructions( uchar *pCode, long codeSize )
|
2000-12-30 12:30:34 +03:00
|
|
|
{
|
|
|
|
long saved=0;
|
|
|
|
|
|
|
|
while (codeSize > 0)
|
|
|
|
{
|
2000-12-31 09:59:21 +03:00
|
|
|
CInstruction *instr = get_next_instruction( pCode );
|
|
|
|
int instrSize = instr->fSize;
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2000-12-31 09:59:21 +03:00
|
|
|
pCode += instrSize;
|
2000-12-30 12:30:34 +03:00
|
|
|
codeSize -= instrSize;
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
fInstructions.push_back( instr );
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
}
|
2000-12-30 12:30:34 +03:00
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
void CFunction::remove_nops( void )
|
|
|
|
{
|
|
|
|
for( list<CInstruction*>::iterator p = fInstructions.begin();
|
|
|
|
p != fInstructions.end(); ++p )
|
|
|
|
{
|
|
|
|
if ( (*p)->fInstr == knop )
|
|
|
|
{
|
|
|
|
list<CInstruction*>::iterator s = p;
|
|
|
|
--p;
|
2001-01-02 12:19:35 +03:00
|
|
|
delete *s;
|
2001-01-02 10:44:26 +03:00
|
|
|
fInstructions.erase(s);
|
|
|
|
}
|
|
|
|
}
|
2000-12-30 12:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-29 05:23:09 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
typedef vector<elf_symbol> elf_symbol_table;
|
|
|
|
typedef map< basic_string<char>, elf_symbol_table > elf_text_map;
|
|
|
|
|
|
|
|
void
|
|
|
|
process_mapping(char* mapping, size_t size)
|
|
|
|
{
|
|
|
|
const Elf32_Ehdr* ehdr = reinterpret_cast<Elf32_Ehdr*>(mapping);
|
|
|
|
if (verify_elf_header(ehdr) < 0)
|
|
|
|
return;
|
|
|
|
// find the section headers
|
|
|
|
const Elf32_Shdr* shdrs = reinterpret_cast<Elf32_Shdr*>(mapping + ehdr->e_shoff);
|
|
|
|
|
|
|
|
// find the section header string table, .shstrtab
|
|
|
|
const Elf32_Shdr* shstrtabsh = shdrs + ehdr->e_shstrndx;
|
|
|
|
const char* shstrtab = mapping + shstrtabsh->sh_offset;
|
|
|
|
|
|
|
|
// find the sections we care about
|
|
|
|
const Elf32_Shdr *symtabsh, *strtabsh, *textsh;
|
|
|
|
int textndx;
|
|
|
|
for (int i = 0; i < ehdr->e_shnum; ++i)
|
|
|
|
{
|
|
|
|
basic_string<char> name(shstrtab + shdrs[i].sh_name);
|
|
|
|
if (gDebug) cout << "name = " << name << "\n";
|
|
|
|
if (name == ".symtab") {
|
|
|
|
symtabsh = shdrs + i;
|
|
|
|
}
|
|
|
|
else if (name == ".text") {
|
|
|
|
textsh = shdrs + i;
|
2000-12-30 12:30:34 +03:00
|
|
|
textndx = i;
|
2000-12-29 05:23:09 +03:00
|
|
|
}
|
|
|
|
else if (name == ".strtab") {
|
|
|
|
strtabsh = shdrs + i;
|
|
|
|
}
|
2001-01-04 08:35:23 +03:00
|
|
|
cout << name << " size = " << (shdrs+i)->sh_size << "\n";
|
2000-12-29 05:23:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the .strtab
|
|
|
|
char* strtab = mapping + strtabsh->sh_offset;
|
|
|
|
|
|
|
|
// find the .text
|
|
|
|
char* text = mapping + textsh->sh_offset;
|
|
|
|
int textaddr = textsh->sh_addr;
|
|
|
|
|
2001-01-04 08:35:23 +03:00
|
|
|
|
2000-12-29 05:23:09 +03:00
|
|
|
// find the symbol table
|
|
|
|
int nentries = symtabsh->sh_size / sizeof(Elf32_Sym);
|
|
|
|
Elf32_Sym* symtab = reinterpret_cast<Elf32_Sym*>(mapping + symtabsh->sh_offset);
|
2000-12-30 12:30:34 +03:00
|
|
|
|
|
|
|
// look for code in the .text section
|
2000-12-29 05:23:09 +03:00
|
|
|
elf_text_map textmap;
|
2001-01-02 10:44:26 +03:00
|
|
|
long newSize = 0, oldSize = 0;
|
|
|
|
long numUnknowns=0;
|
2000-12-29 05:23:09 +03:00
|
|
|
for (int i = 0; i < nentries; ++i) {
|
|
|
|
const Elf32_Sym* sym = symtab + i;
|
2000-12-30 12:30:34 +03:00
|
|
|
if ( sym->st_shndx == textndx && sym->st_size)
|
|
|
|
{
|
2001-01-02 10:44:26 +03:00
|
|
|
// basic_string<char> funcname(sym->st_name + strtab, sym->st_size);
|
|
|
|
string funcname(sym->st_name + strtab, sym->st_size);
|
2000-12-29 05:23:09 +03:00
|
|
|
basic_string<char> functext(text + sym->st_value - textaddr, sym->st_size);
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
CFunction *func = new CFunction( funcname );
|
|
|
|
|
2000-12-30 12:30:34 +03:00
|
|
|
if (gDebug) cout << funcname << "\n\n";
|
2001-01-02 10:44:26 +03:00
|
|
|
if (gDebug) (void)hexdump(cout,functext.data(),sym->st_size);
|
|
|
|
oldSize += sym->st_size;
|
|
|
|
func->get_instructions( (unsigned char *)functext.data(), sym->st_size );
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
if (gOptimize)
|
|
|
|
{
|
|
|
|
for( list<CInstruction*>::iterator p = func->fInstructions.begin();
|
|
|
|
p != func->fInstructions.end(); ++p )
|
|
|
|
{
|
|
|
|
(*p)->optimize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
if (gCompact) func->remove_nops();
|
|
|
|
|
|
|
|
for( list<CInstruction*>::iterator p = func->fInstructions.begin();
|
|
|
|
p != func->fInstructions.end(); ++p )
|
|
|
|
{
|
|
|
|
//if ( (*p)->fInstr == kunknown) numUnknowns++;
|
|
|
|
uchar buffer[16];
|
|
|
|
int instrSize = (*p)->generate_opcode( buffer );
|
|
|
|
if (instrSize == 0) numUnknowns++;
|
|
|
|
newSize += instrSize;
|
|
|
|
|
2001-01-02 12:19:35 +03:00
|
|
|
if (gAssembly)
|
|
|
|
{
|
|
|
|
(void)hexdump( *p );
|
|
|
|
(*p)->output_text();
|
|
|
|
}
|
2001-01-02 10:44:26 +03:00
|
|
|
|
|
|
|
delete *p;
|
|
|
|
}
|
|
|
|
delete func;
|
2000-12-29 05:23:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-02 10:44:26 +03:00
|
|
|
cout << "Code size reduction of " << oldSize-newSize -numUnknowns << " bytes out of "
|
|
|
|
<< oldSize << " bytes.\n";
|
|
|
|
if (numUnknowns) cout << "*** Unknowns found: " << numUnknowns << "\n";
|
2000-12-29 05:23:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
process_file(const char* name)
|
|
|
|
{
|
|
|
|
if (gDebug) cout << name << "\n";
|
|
|
|
|
|
|
|
int fd = open(name, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
cerr << "***Failed to Open!***"<< "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
if (fstat(fd, &statbuf) < 0)
|
|
|
|
{
|
|
|
|
cerr << "***Failed to Access File!***"<< "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t size = statbuf.st_size;
|
|
|
|
|
|
|
|
void* mapping = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (mapping != MAP_FAILED)
|
|
|
|
{
|
|
|
|
process_mapping(static_cast<char*>(mapping), size);
|
|
|
|
munmap(mapping, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2001-01-02 12:19:35 +03:00
|
|
|
char c = getopt( argc, argv, "dcoa" );
|
2000-12-29 05:23:09 +03:00
|
|
|
if (c == -1) break;
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
2001-01-02 12:19:35 +03:00
|
|
|
case 'a':
|
|
|
|
gAssembly = true;
|
|
|
|
break;
|
2001-01-02 10:44:26 +03:00
|
|
|
case 'c':
|
|
|
|
gCompact = true;
|
|
|
|
cout << "Compacting Dead Code ON\n";
|
|
|
|
break;
|
2000-12-29 05:23:09 +03:00
|
|
|
case 'd':
|
|
|
|
gDebug = true;
|
|
|
|
cout << "Debugging Info ON\n";
|
|
|
|
break;
|
2001-01-02 10:44:26 +03:00
|
|
|
case 'o':
|
|
|
|
gOptimize = true;
|
|
|
|
cout << "Instrustion Optimization ON\n";
|
|
|
|
break;
|
2000-12-29 05:23:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = optind; i < argc; ++i)
|
|
|
|
process_file(argv[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|