Re: What is the difference between Scrt1.o and crt1.o?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/22/21, Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx> wrote:
> 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.

Thanks for your other replies

But I am sorry for this as I don't think this is off-topic. The
decision to use Scrt1.o or crt1.o is made by gcc. Therefore, it is a
legitimate question to ask this question on a gcc mailing list. I will
have to ignore your comment on this aspect.

> 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.

These options are quite confusing. I have a hard time understanding
the difference they are. Can anybody have a clear explanation (not
just how they are used, but also the underlying reasons why it is so)
for them?

-fpic  -fPIC  -fpie  -fPIE

> 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
>
>


-- 
Regards,
Peng



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux