Josh
Josh

Reputation: 11

Are PIE files given virtual memory address during linking?

I have compiled a simple hello world c code with gcc -fpie test.c, and now looking at the binary using objdump:

Disassembly of section __TEXT,__text:
__text:
100000f40:  55  pushq   %rbp
100000f41:  48 89 e5    movq    %rsp, %rbp
100000f44:  48 83 ec 10     subq    $16, %rsp
100000f48:  89 7d fc    movl    %edi, -4(%rbp)
100000f4b:  8b 75 fc    movl    -4(%rbp), %esi
100000f4e:  48 8d 3d 5d 00 00 00    leaq    93(%rip), %rdi
100000f55:  b0 00   movb    $0, %al

...

We can clearly see memory addresses are still calculated by the linker on the left. Aren't pie files supposed to have no memory addresses associated to them statically?

My second question is, how are pic files (like shared libraries) loaded in memory? do they have their own virtual address space? if so why do they need to be position independent? or are they loaded into a processes address space?

Upvotes: 1

Views: 109

Answers (1)

S.S. Anne
S.S. Anne

Reputation: 15584

Those are not addresses. What you should be looking at are the PC-relative offsets:

leaq    93(%rip), %rdi

These signify that the code is PIC/PIE.

Shared libraries are mapped into every running process that uses them.
These libraries, however, need to be relocated (to fit the address space constraints of the program), and so are compiled with PIC (position-independent code, a.k.a. usually with PC-relative offsets).

Upvotes: 1

Related Questions