简体   繁体   English

在编译的C程序中查找asm指令

[英]finding asm instruction in compiled C program

I have very simple C program: 我有非常简单的C程序:

int foobar(int a)
{
    int b = a;
}

int main(int argc, char *argv[])
{
    foobar(0xDEAD);
    return 0;
}

Using objdump -d main.out I got disassembled binary with a lot of assembler instructions: 使用objdump -d main.out我得到了带有大量汇编指令的反汇编二进制文件:

  4004a3:   55                      push   %ebp
  4004a4:   48 89 e5                mov    %esp,%ebp
  4004a7:   48 83 ec 10             sub    $0x10,%esp

How can I find for example address of every push instruction from another C program? 如何找到来自另一个C程序的每个push指令的地址? Can it be done this way?: 可以这样做吗?:

position = 0;
while (...)
{
   ...
   int act_value;
   read(binary_file, &act_value, 4);

   if (act_value == /*what value?*/)
   {
      printf("Instruction: push\n");
      printf("Address: %X\n", position * 4); /* is this correct?*/
   }
   position++;
   ...
}

As Oli Charlesworth already pointed out, instructions are of variable length on the x86 architecture. 正如Oli Charlesworth已经指出的那样,x86架构上的指令长度可变。 You can still write a program to do this for you, but you'll need to parse all the instructions to properly know how long they are and where the next one starts. 您仍然可以编写一个程序来为您执行此操作,但是您需要解析所有指令以正确地知道它们的长度以及下一个启动的位置。

I don't understand why you want to write your own program to solve the problem, or is there something you're not telling us? 我不明白你为什么要编写自己的程序来解决问题,或者有什么你不告诉我们的? Are you only looking for a way to find the addresses of the push instructions? 您是否只想找到push指令的地址? If so, just do this: 如果是这样,请执行以下操作:

objdump -d another_c_program | grep push

Of course, this will also find pushl and so on. 当然,这也会找到pushl等。 I guess you want them too, otherwise the command can be modified. 我猜你也想要它们,否则可以修改命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM