Results 1 to 7 of 7

Thread: Convert IDA HexRays Decompiler source to working C source

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts

    Convert IDA HexRays Decompiler source to working C source

    Since I just had the question on Steam, I thought we can collect all the quirks of converting decompiled source to working code.

    For the beginning, I made this screenshot, which shows the working version and the decompiled one:

    Left = working version = https://github.com/kungfooman/libcod...bcod.cpp#L1920
    Right = decompiled version = http://pastebin.com/a2ydu2NH

    Click image for larger version. 

Name:	decompiler_vs_real.png 
Views:	165 
Size:	84.4 KB 
ID:	872

    Since something like
    PHP Code:
    dword_8571428 
    is internally for HexRays fully correct, it's not helpful in a shared library, since we cannot just tell LD (the linker) to place this variable to that memory location. Hence we need to dereference it like
    PHP Code:
    *(int *)0x08571428 
    I made a macro for this in config.hpp: https://github.com/kungfooman/libcod...ter/config.hpp

    PHP Code:
    #define INT(address) (*(int*)(address)) 
    So we can just write:


    PHP Code:
    INT(0x08571428)

    // so this is all the same:
    dword_8571428
    *(int *)0x08571428
    INT
    (0x08571428
    There are many other types, which can be treated equally, but nobody ever added macros for them, but the principle will be the same. A nice list:

    PHP Code:
    #include <cstdint>

    typedef uint8_t  CHAR;
    typedef uint16_t WORD;
    typedef uint32_t DWORD;

    typedef int8_t  BYTE;
    typedef int16_t SHORT;
    typedef int32_t LONG;

    typedef LONG INT;
    typedef INT BOOL
    So you don't need much conversion, when HexRays gives you something like this:

    PHP Code:
    *(_WORD *)(a2 8) + 50
    More conversion rules, questions etc. all welcome
    timescale 0.01

  2. The Following 2 Users Say Thank You to kung foo man For This Useful Post:

    kubislav23 (29th September 2017),Mitch (1st July 2015)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •