www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - porting an IAx86 disassembler

reply llee <llee goucher.edu> writes:
        I'm trying to port the distorm disassembly library over to D. The
library was originally written in C, but I converted the header file into a D
module. The library exports one main function distorm_decode (). This function
is an alias for distorm_decode64(). This function takes an array of structures
(DecodedInst). Each structure represents a single disassembled instruction.
When the function is called it disassembles a code sequence and stores the
results in this array. When I called this function it successfully disassembled
the first instruction in the code sequence that I passed to it. Then it began
acting erratically. In some of the remaining structures, the mnemonic string
would be contained in the operand string, or the hex string would be in the
operand string, etc. The data structure seemed to be jumbled.
        Source code illustrating a call to this function is given below, along
with parameter information. The declaration is contained within an extern (C)
block, and is located within an external module. 

The function declaration:
       ...
       _DecodeResult  distorm_decode64 
       (
	       _OffsetType codeOffset, 
	       ubyte *code, 
	       int codeLen, 
	       _DecodeType dt, 
	        _DecodedInst *result,
	        uint maxInstructions, 
	        uint *usedInstructionsCount
        );
        ...

The function call:
        ...
        dout.writeLine ("reading source file... ");
        ubyte[] source = cast (ubyte[]) std.file.read (commandline [1]);
        dout.writeLine ("source file read. ");

        _OffsetType			offset = 0;
        _DecodeType			decode_type = Decode32Bits;
        _DecodeResult 			decode_result;
        _DecodedInst[30]		decoded_instructions;
	uint decode_count;

	dout.writeLine ("disassembling source file... ");
	decode_result = distorm_decode64
	(
		offset				,
		&source[0]			,				
		source.length			,
		decode_type			,
		&decoded_instructions[0]	,
		decoded_instructions.length	,
		&decode_count
	);
        ...

The code used to display the results:
        ...
	foreach (_DecodedInst decoded_instruction; decoded_instructions)
	{
		dout.writefln ("instruction size: %s ", decoded_instruction.size);
		printf ("instruction Hex: %s \n", cast (char*)
decoded_instruction.instructionHex.p);
		printf ("instruction mnemonic: %s \n", cast (char*)
decoded_instruction.mnemonic.p);
		printf ("instruction operands: %s \n", cast (char*)
decoded_instruction.operands.p);
	}
        ...

Any help would be appreciated. 
Oct 08 2007
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Sounds like you should be checking the alignment/size/offsets of struct 
members to be sure the D & C versions match.
Oct 08 2007
parent reply llee <llee goucher.edu> writes:
That's what I'm thinking, but I do not know how to do this. Any ideas?


Walter Bright Wrote:

 Sounds like you should be checking the alignment/size/offsets of struct 
 members to be sure the D & C versions match.
Oct 08 2007
parent BCS <ao pathlink.com> writes:
Reply to llee,

 That's what I'm thinking, but I do not know how to do this. Any ideas?
 
 Walter Bright Wrote:
 
 Sounds like you should be checking the alignment/size/offsets of
 struct members to be sure the D & C versions match.
 
offsetof comes to mind or make a struct and print out it's address and the address of each of it's parts. Do this is D and C and do the math. if the offset's are differnt then you have a problem.
Oct 08 2007