Key difference in the out of ld -pie and ld without -pie?

Tadeus Prastowo tadeus.prastowo@unitn.it
Fri Apr 23 06:25:49 GMT 2021


On Fri, Apr 23, 2021 at 1:30 AM Peng Yu via Binutils
<binutils@sourceware.org> wrote:
>
> Hi,
>
> I am trying to see the key difference between ld -pie and ld without
> it in terms of the a.out output. I tried the following example.
>
> $ cat hello.s
>         .text
>         .global main
> main:
>         lea     s(%rip), %rdi
>         call    puts@PLT
>         xor     %rax, %rax
>         ret
>         .section        .rodata
> s:
>         .string "Hello World!"
>
> $ as -c -o hello.o hello.s
> $ cmd=(
>         ld
>         -pie
>         -dynamic-linker /lib64/ld-linux-x86-64.so.2
>         /usr/lib/x86_64-linux-gnu/crt1.o
>         /usr/lib/x86_64-linux-gnu/crti.o
>         /usr/lib/x86_64-linux-gnu/crtn.o
>         -lc)
> $ "${cmd[@]}"
>
> diff of `readelf -lW`, `readelf -SW` and `objdump -d` running on a.out
> generated by ld with and without -pie does show some differences. But
> I mainly notice the differences in the addresses.
>
> I am afraid that I may miss some more important differences on
> positional independence.  Could anybody show me where is the key part
> relevant to positional independence?

That I will leave for others to help as of now.

> Also, this example used libc. Is there an even simpler program that
> doesn't depend on libc yet is more suitable for demonstrating the
> effect of -pie of ld? I am afarid to show the effect of pie, libc is
> not necessary to be dependent on.
>
> I tried the following program. But -pie can not be used with ld. So
> this simplification is not successful.
>
> $ as -o hello.o ../hello.s
> $ ld -pie hello.o
> ld: hello.o: relocation R_X86_64_32S against `.text' can not be used
> when making a PIE object; recompile with -fPIE
>
>
>         .global _start
>
>         .text
> _start:
>         # write(1, message, 13)
>         mov     $1, %rax                # system call 1 is write
>         mov     $1, %rdi                # file handle 1 is stdout
>         mov     $message, %rsi          # address of string to output
>         mov     $13, %rdx               # number of bytes
>         syscall                         # invoke operating system to
> do the write
>
>         # exit(0)
>         mov     $60, %rax               # system call 60 is exit
>         xor     %rdi, %rdi              # we want return code 0
>         syscall                         # invoke operating system to exit
> message:
>         .ascii  "Hello, world\n"

I simplified your hello.s and did:
$ cat hello.s
        .text
        .global main
main:
        lea     s(%rip), %rdi
        xor     %rax, %rax
        ret
        .section        .rodata
s:
        .string "Hello World!"
$ as -c -o hello.o hello.s
$ ld -pie hello.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000000221

It's just a warning.  I obtained the a.out executable.  Now you can
try this on your own and perform further experiments.

> --
> Regards,
> Peng

-- 
Best regards,
Tadeus


More information about the Binutils mailing list