c - 目标文件中的函数和参数不正确?

标签 c gcc assembly

#include <stdio.h>
void DispString(const char* charList)
{
    puts(charList);
}
void main()
{
    DispString("Hello, world!");
} 

编译:gcc -c -g test.c -o test.o

链接:gcc -o test test.o

很简单,但是当我使用objdump反汇编目标文件(test.o)时,我得到了以下结果:

objdump -d test.o:

boot.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <DispString>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   48 89 7d f8             mov    %rdi,-0x8(%rbp)
   c:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  10:   48 89 c7                mov    %rax,%rdi
  13:   e8 00 00 00 00          callq  18 <DispString+0x18>
  18:   c9                      leaveq 
  19:   c3                      retq   

000000000000001a <main>:
  1a:   55                      push   %rbp
  1b:   48 89 e5                mov    %rsp,%rbp
  1e:   bf 00 00 00 00          mov    $0x0,%edi
  23:   e8 00 00 00 00          callq  28 <main+0xe>
  28:   5d                      pop    %rbp
  29:   c3                      retq   

第 23 行,它把 0 传给了 %edi 寄存器,这肯定是错误的。它应该传递“Hello, world!”的地址。串到它。它叫28 <main+0xe> ?第 28 行只是它的下一行,而不是函数 DispString(在第 0 行)。为什么会这样?我还研究了最后的 test文件,其中所有值都是正确的。那么链接器如何知道在哪里可以找到这些函数或字符串呢?

最佳答案

您只是在翻译文件,所以没有链接。一旦链接 jas 完成,然后 DispString() 的地址将被 main 知道,它将跳转到那里。因此,正如其中一条评论所建议的那样,将 objdump 与编译后的可执行文件一起使用。

关于c - 目标文件中的函数和参数不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203161/

相关文章:

c - 什么是计算 floor(log(m/n)) 的有效方法,其中 m 和 n 是整数?

assembly - 如何使用nasm为程序集8086中的数组动态分配内存

c - Ammar_1610852_Assignment.exe : 0xC0000005: Access violation reading location 0x00003232 中 0x6b20d0ac 处出现未处理的异常

c - 我该如何开始解决这个问题并修复我的代码?

c++ - gdb中的nexti和stepi有什么区别?

c - 如何修复 fprintf 循环?

Python 编译错误 : "LONG_BIT definition appears wrong for platform"

为 "-std=gnu90"编译 C 项目错误

c++ - GCC 的 undefined reference /多重定义错误

c++ - gcc 内联 asm x86 CPU 标志作为输入依赖项