Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning address for instruction in object file

When we compile any c code with gcc -c and do objdump -d <filename>.o we see

Disassembly of section .text:
0000000000000000 <main>:
 0:   55                      push   %rbp
 1:   48 89 e5                mov    %rsp,%rbp
 4:   48 83 ec 10             sub    $0x10,%rsp
 8:   48 8d 45 fc             lea    -0x4(%rbp),%rax
 c:   48 89 c7                mov    %rax,%rdi
 f:   b8 00 00 00 00          mov    $0x0,%eax
 . . .

But after linking, the offset changes to gcc -o prog -L/library/path -llibrary *.o

0000000000400644 <main>:
400644:       55                      push   %rbp
400645:       48 89 e5                mov    %rsp,%rbp
400648:       48 83 ec 10             sub    $0x10,%rsp
40064c:       48 8d 45 fc             lea    -0x4(%rbp),%rax
400650:       48 89 c7                mov    %rax,%rdi
400653:       b8 00 00 00 00          mov    $0x0,%eax

How the offset is calculated after linking is done?

We basically get 3 set of addresses, 1. After compiling 2. After linking 3. After Loading

How the above address are related?

like image 230
Shrinock Avatar asked Jan 26 '26 14:01

Shrinock


1 Answers

You have to remember that the object file contains only your code, so it will always be at offset zero.

When you link you add modules from other sources, like the runtime-initialization and library functions. You don't know the size of these objects, or where they will be placed in the resulting executable files, and therefore can't calculate the offset to the different parts of your code yourself. Also, if you have multiple object files, the linker may rearrange them as it sees fit.

What the exact virtual address the code will end up in when running, depends partly on the linker, but mostly on the operating system and things like address-space randomization and such.

like image 113
Some programmer dude Avatar answered Jan 28 '26 06:01

Some programmer dude