https://www.hex-rays.com/products/decompiler/manual/tricks.shtml

First of all, read the troubleshooting page. It explains how to deal with most decompilation problems.

Below is a mix of other useful information that did not fit into any other page:

Volatile memory

Sometimes the decompiler can be overly aggressive and optimize references to volatile memory completely away. A typical situation like the following:

                device_ready    DCD ? ; VOLATILE!

                                MOV     R0, =device_ready
LDR R1, [R0]
LOOP:
LDR R2, [R0]
SUB R2, R1
BEQ LOOP

can be decompiled into

                while ( 1 )
;

because the decompiler assumes that a variable can not change its value by itself and
it can prove that r0 continues to point to the same location during the loop.

To prevent such optimization, we need to mark the variable as volatile.
Currently the decompiler considers memory to be volatile if it belongs to a segment with one of the following names:

IO, IOPORTS, PORTS, VOLATILE.

The character case is not important.

Constant memory

Sometimes the decompiler does not optimize the code enough because it assumes that variables may change their values. For example, the following code:

                  LDR     R1, =off_45934
MOV R2, #0
ADD R3, SP, #0x14+var_C
LDR R1, [R1]
LDR R1, [R1] ; int
BL _IOServiceOpen

can be decompiled into

                IOServiceOpen(r0_1, *off_45934, 0)
        

but this code is much better:

                IOServiceOpen(r0_1, mach_task_self, 0)
        

because

                off_45934 DCD _mach_task_self
        

is a pointer that resides in constant memory and will never change its value.
 
The decompiler considers memory to be constant if one of the following conditions hold:

  1. the segment has access permissions defined but the write permission is not in the list
    (to change the segment permissions use the SetSegmentAttr built-in function)
  2. the segment type is CODE
  3. the segment name is one of the following (the list may change in the future):

    .text, .rdata, .got, .got.plt, __text, __const, __const_coal, __cstring, __literal4,
    __literal8, __pointers, __nl_symbol_ptr, __la_symbol_ptr,
    __objc_protorefs, __objc_selrefs, __objc_classrefs, __objc_superrefs, __objc_const,
    __message_refs, __cls_refs, __inst_meth, __cat_inst_meth, __cat_cls_meth.

CONTAINING_RECORD macro

The decompiler knows about the CONTAINING_RECORD macro and tries to use it in the output.
However, in most cases it is impossible to create this macro automatically,
because the information about the containing record is not available.
The decompiler uses three sources of information to determine if CONTAINING_RECORD should be used:

  1. If there is an assignment like this:

                v1 = (structype *)((char *)v2 - num);
            

    it can be converted into

                v1 = CONTAINING_RECORD(v2, structype, fieldname);
            

    by simply confirming the types of v1 and v2. 
    NOTE: the variables types must be specified explicitly.
    Even if the types are displayed as correct, the user should press Yfollowed by Enter to confirm the variable type.

  2. Struct offsets applied to numbers in the disassembly listing are used as a hint
    to create CONTAINING_RECORD. For example, applying structure offset to 0x41C in
                sub     eax, 41Ch
            

    will have the same effect as in the previous point. Please note that it makes sense to confirm the variable types as explained earlier.

  3. Struct offsets applied to numbers in the decompiler output. For example, applying _DEVICE_INFO structure offset to-131 in the following code:
                deviceInfo = (_DEVICE_INFO *)((char *)&thisEntry[-131] - 4);
            

    will convert it to:

                deviceInfo = CONTAINING_RECORD(thisEntry, _DEVICE_INFO, ListEntry);
            

    Please note that it makes sense to confirm the variable types as explained earlier.

Indirect calls

Since the arguments of indirect calls are collected before defining variables, specifying the type of the variable
that holds the function pointer may not be enough. The user have to specify the function type using other methods in this case.
The following methods exist (in the order of preference):

  1. For indirect calls of this form:

                call ds:funcptr
            

    If funcptr is initialized statically and points to a valid function, just ensure a correct function prototype. The decompiler will use it.

  2. For indirect calls of this form:
                call [reg+offset]
            

    If reg points to a structure with a member that is a function pointer, just convert the operand into a structure offset (hotkey T):

                call [reg+mystruct.funcptr]
            

    and ensure that the type of mystruct::funcptr is a pointer to a function of the desired type.

  3. Specify the type of the called function using Edit, Operand type, Set operand type.
    If the first two methods can not be applied, this is the recommended method.
    The operand type has the highest priority, it is always used if present.
  4. If the address of the called function is known, use Edit, Plugins, Change the callee address (hotkey Alt-F11).
    The decompiler will use the type of the specified callee. This method is available only for x86.
    For other processors adding a code cross reference from the call instruction to the callee will help.

Hex-Rays Decompiler Tips and tricks Volatile memory的更多相关文章

  1. Matlab tips and tricks

    matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...

  2. Android Studio tips and tricks 翻译学习

    Android Studio tips and tricks 翻译 这里是原文的链接. 正文: 如果你对Android Studio和IntelliJ不熟悉,本页提供了一些建议,让你可以从最常见的任务 ...

  3. Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

    原文链接:http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips- ...

  4. (转) How to Train a GAN? Tips and tricks to make GANs work

    How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...

  5. LoadRunner AJAX TruClient协议Tips and Tricks

    LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...

  6. Tips and Tricks for Debugging in chrome

    Tips and Tricks for Debugging in chrome Pretty print On sources panel ,clicking on the {} on the bot ...

  7. [转]Tips——Chrome DevTools - 25 Tips and Tricks

    Chrome DevTools - 25 Tips and Tricks 原文地址:https://www.keycdn.com/blog/chrome-devtools 如何打开? 1.从浏览器菜单 ...

  8. 10 Essential TypeScript Tips And Tricks For Angular Devs

    原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...

  9. WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks

    What’s New Automatic Dimensions - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSect ...

随机推荐

  1. Bootstrap--全局CSS样式之栅格系统

    Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列.它包含了易于使用的预定义类,还有强大的mixin 用于生成更具 ...

  2. linearlayout 水平垂直居中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  3. oracle 组函数

    一.组函数嵌套 ORACLE中规定,组函数嵌套只能嵌两层.其实多层嵌套并没有实际的用途,因此ORACLE没有提供组函数的多层嵌套.但是,单行函数是可以多层嵌套的. 二. 1.Oracle包含以下组函数 ...

  4. git/github初级运用自如(zz)

    ----//git/github环境配置 一 .  github上创建立一个项目 用户登录后系统,在github首页,点击页面右下角“New Repository” 填写项目信息: project n ...

  5. Android实现button一边圆角一边直角

    http://www.it165.net/pro/html/201503/36211.html

  6. Eclipse插件安装的三种方法

    转自:http://www.blogjava.net/tangzurui/archive/2008/06/30/211669.html  整理了一下格式. (前两种安装方式以多国语言包的安装为例) 1 ...

  7. Enter回车切换输入焦点方法兼容各大浏览器

    做项目时,客户要求能够用enter回车直接切换输入(焦点),当最后一个时候,直接提交信息. 第一想法就是,网上去copy一段代码直接用.但了百度.谷歌找了个遍,找到的代码80%以上都是一样的.有的代码 ...

  8. manacher算法_求最长回文子串长度

    很好的总结,转自: http://blog.csdn.net/dyx404514/article/details/42061017 总结为:两大情况,三小情况. 两大情况:I. i <= p 1 ...

  9. Spring Batch的事务– Part 3: 略过和重试

    原文:https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ This i ...

  10. sphinx下的max_matches取值对SetLimits的影响

    使用PHP在客户端执行 $s -> SetLimits(0, 15, 1200); 传递的第三个参数,是服务器端设定当前查询的结果集大小为1200,但是运行结果,确实$s最终查询得到的结果为空值 ...