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

compile: gcc -c -g test.c -o test.o

link: gcc -o test test.o

Very simple, but when I use objdump to disassemble the object file(test.o), I got the following result:

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   

For the line 23, it passed 0 to %edi register, which is definitely wrong. It should pass the address of the "Hello, world!" string to it. And it called 28 <main+0xe>? The line 28 is just its next line, rather than function DispString(which is in line 0). Why could this happen? I've also looked into the final test file, in which all the values are just correct. So how could the linker know where to find those functions or strings?

1
  • 4
    Your file hasn't been linked yet. 0 means "will fill in later".
    – Kerrek SB
    Oct 5, 2014 at 13:57

1 Answer 1

0

You are only translating file so no linking has been done. Once linking jas been done, then and then DispString()'s address will be known to main and it will jump to there. So as suggested in one of the comments, use objdump with the comliled executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.