GCC编译和链接原始输出 [英] GCC compile and link raw output

查看:228
本文介绍了GCC编译和链接原始输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得与函数调用一个简单的C程序的原始指令code输出。

I am trying to get the raw instruction code output for a simple C program with function calls.

我已经搜查这里和谷歌的答案却只能发现是正确的单一功能(无功能调用)。

I have already searched on here and Google for the answer but can only find answers that are correct for single functions (no function calls).

一个简单的例子:

int main(){
    return addition(5, 7);
}

int addition(int a, int b){
    return a + b;
}

当我使用 GCC -c test.c的-o test.o 然后 objdump的-d test.o 此, JAL (跳转和链接)指令显示跳转到地址 00000000 这显然是不正确,但是当我完全编译程序,我得到了 objdump的命令垃圾的大量

When I use gcc -c test.c -o test.o and then objdump -d test.o on this, the JAL (jump and link) instruction shows a jump to address 0x00000000 which is obviously incorrect, however when I compile the program fully, I get an enormous amount of junk in the objdump command

我对MIPS的编译器(以及相关的MIPS-objdump的,等等)进行编译。

I am compiling with the mips compiler (and associated mips-objdump, etc).

我编译的程序是自包含的(没有外部库或系统包括文件)。我想是的,其中 JAL 和等效指令指向他们调用的函数正确的地址指令的转储。

The programs I am compiling are self contained (no external libraries, or system include files). What I want is a dump of the instructions where the JAL and equivalent instructions point to the correct addresses for the functions they call.

推荐答案

当你的code未联系,地址可能会或可能不会得到解决(绝对地址肯定不会)。在后一种情况下,你应该可以看到定位项,如果您使用 objdump的-dr 。如果您链接程序,这些问题应该走了,如果程序是真正独立的(即不连C库),那么你看到的应该是你的code。您可能需要使用 -nostdlib 切换到GCC。我没有MIPS的gcc可用,但在这里是为了说明x86版本:

While your code is not linked, the addresses may or may not be resolved (absolute addresses certainly won't be). In the latter case, you should see relocation entries if you use objdump -dr. If you link your program, those issues should be gone, and if the program is really standalone (ie. not even C libraries) then all you see should be your code. You might want to use -nostdlib switch to gcc. I don't have mips gcc available, but here is the x86 version for illustration:

080480d8 <addition>:
 80480d8:       8b 44 24 08             mov    0x8(%esp),%eax
 80480dc:       03 44 24 04             add    0x4(%esp),%eax
 80480e0:       c3                      ret

080480e1 <main>:
 80480e1:       83 ec 08                sub    $0x8,%esp
 80480e4:       c7 44 24 04 07 00 00    movl   $0x7,0x4(%esp)
 80480eb:       00
 80480ec:       c7 04 24 05 00 00 00    movl   $0x5,(%esp)
 80480f3:       e8 e0 ff ff ff          call   80480d8 <addition>
 80480f8:       83 c4 08                add    $0x8,%esp
 80480fb:       c3                      ret

这是所有在二进制code,并且你可以在 80480f3 看到呼叫得到解决。我希望它的工作方式类似于MIPS的。

That is all the code in the binary, and as you can see at 80480f3 the call is resolved. I expect it works similarly for mips.

这篇关于GCC编译和链接原始输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆