Gentoo Archives: gentoo-musl

From: Lei Zhang <zhanglei.april@×××××.com>
To: "Anthony G. Basile" <blueness@g.o>
Cc: gentoo-musl@l.g.o
Subject: Re: [gentoo-musl] What is crtbeginP.o ?
Date: Fri, 10 Jun 2016 10:00:55
Message-Id: CAOYuCc0NqVvksBi2vvxUVbkkKVMM9n6OCSmF7zNad8pQfP0A-Q@mail.gmail.com
In Reply to: Re: [gentoo-musl] What is crtbeginP.o ? by "Anthony G. Basile"
1 2016-06-10 17:15 GMT+08:00 Anthony G. Basile <blueness@g.o>:
2 > On 6/10/16 4:45 AM, Lei Zhang wrote:
3 >> Hi,
4 >>
5 >> I found this file on my musl based system built by GRS:
6 >> $ ls /usr/lib/gcc/x86_64-gentoo-linux-musl/4.9.3 | grep crtbegin
7 >> crtbegin.o
8 >> crtbeginP.o
9 >> crtbeginS.o
10 >> crtbeginT.o
11 >>
12 >> while on my host:
13 >> $ ls /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3 | grep crtbegin
14 >> crtbegin.o
15 >> crtbeginS.o
16 >> crtbeginT.o
17 >>
18 >> What is crtbeginP.o on the former system? I found very little info of
19 >> it on the internet.
20 >>
21 >> Also I'm trying to replace GCC's crtbegin/end with those from NetBSD.
22 >> But NetBSD doesn't offer the *S or *T variants, which are used by
23 >> "-static" and "-shared" options. What's special about them? Are they
24 >> just compiled out of the same source with different flags?
25 >>
26 >> Any inputs are appreciated :)
27 >>
28 >> Lei
29 >>
30 >
31 > those are the end files and they are used during linking. you can see
32 > them in use if you create a little test program and build it using
33 >
34 > gcc -v -o test test.c
35 >
36 > the last line of the verbose output looks something like the following
37 > on a glibc system:
38 >
39 > /usr/libexec/gcc/x86_64-pc-linux-gnu/4.9.3/collect2 -plugin
40 > /usr/libexec/gcc/x86_64-pc-linux-gnu/4.9.3/liblto_plugin.so
41 > -plugin-opt=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.9.3/lto-wrapper
42 > -plugin-opt=-fresolution=/tmp/cchAQ9r3.res
43 > -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s
44 > -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc
45 > -plugin-opt=-pass-through=-lgcc_s --eh-frame-hdr -m elf_x86_64
46 > -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -o test
47 > /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../lib64/Scrt1.o
48 > /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../lib64/crti.o
49 > /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/crtbeginS.o
50 > -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3
51 > -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../lib64
52 > -L/lib/../lib64 -L/usr/lib/../lib64
53 > -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../x86_64-pc-linux-gnu/lib
54 > -L/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../.. /tmp/ccD0QtZd.o -lgcc
55 > --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s
56 > --no-as-needed /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/crtendS.o
57 > /usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../lib64/crtn.o
58 >
59 >
60 > sifting through the output you see Scrt1.o crti.o crtbeginS.o ...
61 > crtendS.o crtn.o. the end files deployed will depend on whether you are
62 > doing pie and/or static and possibly other factors. i don't know that
63 > there is a standard meaning to the P, S or T. i remember when zorry and
64 > I work on this for the hardened compiler, we chose T for hardened. you
65 > can see the rules for deploying these files by looking at the gcc specs
66 > using `gcc -dumpspecs`
67
68 First, thanks for your input :)
69
70 I'm aware of crtbegin{S,T}.o, but it's the first time I see
71 crtbeginP.o. Even more strangely, on my host:
72 $ ls /usr/lib/gcc/x86_64-pc-linux-musl/4.9.3 | grep crtbegin
73 crtbegin.o
74 crtbeginS.o
75 crtbeginT.o
76
77 You can see crtbeginP.o is not used by the musl cross-compiler, but
78 only on the musl based system.
79
80 >
81 > so i recommend you play with different gcc compiler profiles using
82 > gcc-config on the musl based system you built with GRS, and look at the
83 > collect2 line to see which end files are deployed with each setting.
84 > then i recommend trying to read the gcc spec file for each of the
85 > compiler profiles. then if you know some assembly, look at the code for
86 > those end files which you can find in the musl source tree. that'll
87 > give you some idea what's going on there.
88 >
89 > its difficult stuff and since you couldn't find much on the internet,
90 > now's the time to get it there! document what you find in a systematic way.
91 >
92 > finally, i don't know clang. i suspect you'll have two issues going
93 > forward with that GRS system: 1) its hardened so how to do hardening
94 > with clang. does clang use gcc style specs? 2) how to get clang to
95 > play nice with musl
96
97 1) Unfortunately clang doesn't support specs file. I know little about
98 hardened Linux. Does it require special support from the compiler? And
99 FYI, I'm not going to compile Linux kernel with clang, which requires
100 some heavy patching.
101
102 2) That's exactly what I'm working on lately, and it turns out to be
103 easy enough :)
104
105 The hard part, however, is dealing with clang's dependence on GCC,
106 like those crtbegin/end files.
107
108 >
109 > btw i doubt strongly the netbsd end files would work.
110
111 They're recommended in LLVM's mailing list, and unfortunately I found
112 no better alternatives at the moment... At least last time I gave
113 them a shot, they appeared to work pretty well.
114
115
116 Lei

Replies

Subject Author
Re: [gentoo-musl] What is crtbeginP.o ? "Anthony G. Basile" <blueness@g.o>