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

Peng Yu pengyu.ut@gmail.com
Thu Apr 22 23:30:21 GMT 2021


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?

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"

-- 
Regards,
Peng


More information about the Binutils mailing list