On Thu, 2021-04-22 at 10:05 -0500, Peng Yu via Gcc-help wrote: > Hi, > > When I use gcc -no-pie to compile a .c file, > /usr/lib/x86_64-linux-gnu/crt1.o is used with ld. > > When I use gcc without -no-pie to compile a .c file, > /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/Scrt1.o is > used with ld along with the additional option -pie. What is the > difference between Scrt1.o and crt1.o? When the option -pie of ld is > deleted, it still works, why is it so? > > If I use -pie with ld, and use crt1.o instead of Scrt1.o, it still > works. So what is the point of switch from crt1.o to Scrt1.o when > -no-pie is removed from gcc options. Again you are being off-topic. Scrt1.o and crt1.o are from glibc, not gcc. If you keep this ignorance you'll eventually annoy everyone on the list. The advantage of PIE is when it is loaded the kernel will randomize its address space layout. That would help to prevent buffer overflow attacks. That's why your distro (I guess it's Ubuntu) enable -fpie (by modification of GCC code) by default. Vanilla GCC defaults to -fno-pie instead. But, Ubuntu doesn't modify binutils code to add -pie by default. So ld will still generate non-pies by default. And you *can* link pie codes into a non-pie (it *does not care* if the code is position-independent), but not vice versa. And anyway that non-pie won't get the advantage of ASLR. > # equivalent commands of gcc without -no-pie > > /usr/lib/gcc/x86_64-linux-gnu/10/cc1 -quiet a.c -o a.s > as -o a.o a.s > cmd=(ld > -dynamic-linker > /lib64/ld-linux-x86-64.so.2 > -pie > /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/Scrt1.o > /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crti.o > a.o > -lc > /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crtn.o > ) > "${cmd[@]}" > > # equivalent commands of gcc with -no-pie > > /usr/lib/gcc/x86_64-linux-gnu/10/cc1 -quiet a.c -o a.s > as -o a.o a.s > cmd=(ld > -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 > a.o > -lc > /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/crtn.o > ) > "${cmd[@]}" > > $ cat a.c > #include <stdio.h> > const char s[] = "Hello World2!"; > int main() { > puts("Hello World!"); > puts(s); > } > -- Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University