mailing list of musl libc
 help / color / Atom feed
* [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs.
@ 2019-12-24 14:38 Youren Shen
  2019-12-24 15:03 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Youren Shen @ 2019-12-24 14:38 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 900 bytes --]

Hey, there,
Recently I'm trying to build a non-gnu toolchain with musl, clang, llvm,
libc++, compiler-rt. While static-pie feature is very useful in our
project, musl-clang force to link a dynamic linker into the binary. This
behavior will cause a crash in c++ programs with compiler-rt and libc++.
For more details and reproduction of this bug, you can read my previous
email to llvm-dev mail lists.[1]
I spend a few days to find the reason -- in function _dlstart_c, the
program will get a "base" of relocation in /lib/ld-musl-x86_64.so.1, which
is not right when it tries to relocate .rel.dyn section in the binary.
Overall, the static-pie program does not need a dynamic linker at all. So
maybe we should remove this argument in wrapper when static-pie is enabled.

Thank you very much.

[1]. https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ
-- 
Best Regards.
Youren Shen.

[-- Attachment #2: Type: text/html, Size: 1213 bytes --]

<div dir="ltr">Hey, there,<div>Recently I&#39;m trying to build a non-gnu toolchain with musl, clang, llvm, libc++, compiler-rt. While static-pie feature is very useful in our project, musl-clang force to link a dynamic linker into the binary. This behavior will cause a crash in c++ programs with compiler-rt and libc++. For more details and reproduction of this bug, you can read my previous email to llvm-dev mail lists.[1]<br>I spend a few days to find the reason -- in function _dlstart_c, the program will get a &quot;base&quot; of relocation in /lib/ld-musl-x86_64.so.1, which is not right when it tries to relocate .rel.dyn section in the binary.<br>Overall, the static-pie program does not need a dynamic linker at all. So maybe we should remove this argument in wrapper when static-pie is enabled.<br></div><div><br></div><div>Thank you very much.</div><div><div><br></div><div>[1]. <a href="https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ">https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ</a></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Best Regards.<div>Youren Shen.</div></div></div></div></div>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs.
  2019-12-24 14:38 [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs Youren Shen
@ 2019-12-24 15:03 ` Rich Felker
  2019-12-25 10:37   ` Youren Shen
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Felker @ 2019-12-24 15:03 UTC (permalink / raw)
  To: Youren Shen; +Cc: musl

On Tue, Dec 24, 2019 at 10:38:49PM +0800, Youren Shen wrote:
> Hey, there,
> Recently I'm trying to build a non-gnu toolchain with musl, clang, llvm,
> libc++, compiler-rt. While static-pie feature is very useful in our
> project, musl-clang force to link a dynamic linker into the binary. This
> behavior will cause a crash in c++ programs with compiler-rt and libc++.
> For more details and reproduction of this bug, you can read my previous
> email to llvm-dev mail lists.[1]
> I spend a few days to find the reason -- in function _dlstart_c, the
> program will get a "base" of relocation in /lib/ld-musl-x86_64.so.1, which
> is not right when it tries to relocate .rel.dyn section in the binary.
> Overall, the static-pie program does not need a dynamic linker at all. So
> maybe we should remove this argument in wrapper when static-pie is enabled.
> 
> Thank you very much.
> 
> [1]. https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ

I don't think the wrappers (gcc or clang one) have been updated with
logic for static pie since it was added, and I'm not sure how easy it
is to add. I'd welcome patches for review, though.

Rich


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs.
  2019-12-24 15:03 ` Rich Felker
@ 2019-12-25 10:37   ` Youren Shen
  2019-12-26  4:46     ` Fangrui Song
  0 siblings, 1 reply; 4+ messages in thread
From: Youren Shen @ 2019-12-25 10:37 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

[-- Attachment #1.1: Type: text/plain, Size: 2453 bytes --]

Thanks for your reply.

Dived deeper, I think this is because the options clang/gcc passed to ld
are not well handled by ld. Unlike "-static", which is passed directly to
the linker, the clang pass -no-dynamic-linker when static-pie is enabled.
However, -dynamic-linker=<file> in ld.musl-clang wrapper conflict with this
-no-dynamic-linker. As a result, ld accepts the last one it received, which
is "-dynamic-linker=<file>" in this case. Here is the problem: even we
passed static-pie to clang, and then the clang pass -no-dynamic-linker to
ld, This option is still omitted by ld.musl-clang. I suggest a fix on it:
move -dynamic-linker "$ldso" before the previous user inputs.
Before:
```
exec $($cc -print-prog-name=ld) -nostdlib "$@" -lc -dynamic-linker "$ldso"
```
After:
```
 exec $($cc -print-prog-name=ld) -nostdlib -dynamic-linker "$ldso" "$@" -lc
```
As you can see, this is a quite simple patch. Further testes may required.
And for musl-gcc, as I test, it seems that it has the same problem. But I'm
not familiar with gcc specs file. So maybe if anyone encounter the same
problem, they can refer this email and give a solution.

On Tue, Dec 24, 2019 at 11:03 PM Rich Felker <dalias@libc.org> wrote:

> On Tue, Dec 24, 2019 at 10:38:49PM +0800, Youren Shen wrote:
> > Hey, there,
> > Recently I'm trying to build a non-gnu toolchain with musl, clang, llvm,
> > libc++, compiler-rt. While static-pie feature is very useful in our
> > project, musl-clang force to link a dynamic linker into the binary. This
> > behavior will cause a crash in c++ programs with compiler-rt and libc++.
> > For more details and reproduction of this bug, you can read my previous
> > email to llvm-dev mail lists.[1]
> > I spend a few days to find the reason -- in function _dlstart_c, the
> > program will get a "base" of relocation in /lib/ld-musl-x86_64.so.1,
> which
> > is not right when it tries to relocate .rel.dyn section in the binary.
> > Overall, the static-pie program does not need a dynamic linker at all. So
> > maybe we should remove this argument in wrapper when static-pie is
> enabled.
> >
> > Thank you very much.
> >
> > [1].
> https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ
>
> I don't think the wrappers (gcc or clang one) have been updated with
> logic for static pie since it was added, and I'm not sure how easy it
> is to add. I'd welcome patches for review, though.
>
> Rich
>


-- 
Best Regards.
Youren Shen.

[-- Attachment #1.2: Type: text/html, Size: 3303 bytes --]

<div dir="ltr">Thanks for your reply.<div><br></div><div>Dived deeper, I think this is because the options clang/gcc passed to ld are not well handled by ld. Unlike &quot;-static&quot;, which is passed directly to the linker, the clang pass -no-dynamic-linker when static-pie is enabled. However, -dynamic-linker=&lt;file&gt; in ld.musl-clang wrapper conflict with this -no-dynamic-linker. As a result, ld accepts the last one it received, which is &quot;-dynamic-linker=&lt;file&gt;&quot; in this case. Here is the problem: even we passed static-pie to clang, and then the clang pass -no-dynamic-linker to ld, This option is still omitted by ld.musl-clang. I suggest a fix on it: move -dynamic-linker &quot;$ldso&quot; before the previous user inputs.<br></div><div>Before:</div><div>```</div><div>exec $($cc -print-prog-name=ld) -nostdlib &quot;$@&quot; -lc -dynamic-linker &quot;$ldso&quot;</div><div>```</div><div>After:</div><div>```</div><div> exec $($cc -print-prog-name=ld) -nostdlib -dynamic-linker &quot;$ldso&quot; &quot;$@&quot; -lc </div><div>```</div><div>As you can see, this is a quite simple patch. Further testes may required. And for musl-gcc, as I test, it seems that it has the same problem. But I&#39;m not familiar with gcc specs file. So maybe if anyone encounter the same problem, they can refer this email and give a solution.</div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 24, 2019 at 11:03 PM Rich Felker &lt;<a href="mailto:dalias@libc.org" target="_blank">dalias@libc.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, Dec 24, 2019 at 10:38:49PM +0800, Youren Shen wrote:<br>
&gt; Hey, there,<br>
&gt; Recently I&#39;m trying to build a non-gnu toolchain with musl, clang, llvm,<br>
&gt; libc++, compiler-rt. While static-pie feature is very useful in our<br>
&gt; project, musl-clang force to link a dynamic linker into the binary. This<br>
&gt; behavior will cause a crash in c++ programs with compiler-rt and libc++.<br>
&gt; For more details and reproduction of this bug, you can read my previous<br>
&gt; email to llvm-dev mail lists.[1]<br>
&gt; I spend a few days to find the reason -- in function _dlstart_c, the<br>
&gt; program will get a &quot;base&quot; of relocation in /lib/ld-musl-x86_64.so.1, which<br>
&gt; is not right when it tries to relocate .rel.dyn section in the binary.<br>
&gt; Overall, the static-pie program does not need a dynamic linker at all. So<br>
&gt; maybe we should remove this argument in wrapper when static-pie is enabled.<br>
&gt; <br>
&gt; Thank you very much.<br>
&gt; <br>
&gt; [1]. <a href="https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ" rel="noreferrer" target="_blank">https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/YtH6Fi2YAgAJ</a><br>
<br>
I don&#39;t think the wrappers (gcc or clang one) have been updated with<br>
logic for static pie since it was added, and I&#39;m not sure how easy it<br>
is to add. I&#39;d welcome patches for review, though.<br>
<br>
Rich<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr">Best Regards.<div>Youren Shen.</div></div></div></div></div>

[-- Attachment #2: static-pie-for-ld.musl-clang.patch --]
[-- Type: application/octet-stream, Size: 346 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs.
  2019-12-25 10:37   ` Youren Shen
@ 2019-12-26  4:46     ` Fangrui Song
  0 siblings, 0 replies; 4+ messages in thread
From: Fangrui Song @ 2019-12-26  4:46 UTC (permalink / raw)
  To: Youren Shen; +Cc: Rich Felker, musl

>On Tue, Dec 24, 2019 at 11:03 PM Rich Felker <dalias@libc.org> wrote:
>
>    On Tue, Dec 24, 2019 at 10:38:49PM +0800, Youren Shen wrote:
>    > Hey, there,
>    > Recently I'm trying to build a non-gnu toolchain with musl, clang, llvm,
>    > libc++, compiler-rt. While static-pie feature is very useful in our
>    > project, musl-clang force to link a dynamic linker into the binary. This
>    > behavior will cause a crash in c++ programs with compiler-rt and libc++.
>    > For more details and reproduction of this bug, you can read my previous
>    > email to llvm-dev mail lists.[1]
>    > I spend a few days to find the reason -- in function _dlstart_c, the
>    > program will get a "base" of relocation in /lib/ld-musl-x86_64.so.1,
>    which
>    > is not right when it tries to relocate .rel.dyn section in the binary.
>    > Overall, the static-pie program does not need a dynamic linker at all. So
>    > maybe we should remove this argument in wrapper when static-pie is
>    enabled.
>    >
>    > Thank you very much.
>    >
>    > [1]. https://groups.google.com/forum/#!msg/llvm-dev/XPrSPqD2zjM/
>    YtH6Fi2YAgAJ
>
>    I don't think the wrappers (gcc or clang one) have been updated with
>    logic for static pie since it was added, and I'm not sure how easy it
>    is to add. I'd welcome patches for review, though.
>
>    Rich
>
> Thanks for your reply.
>
> Dived deeper, I think this is because the options clang/gcc passed to ld are
> not well handled by ld. Unlike "-static", which is passed directly to the
> linker, the clang pass -no-dynamic-linker when static-pie is enabled. However,
> -dynamic-linker=<file> in ld.musl-clang wrapper conflict with this
> -no-dynamic-linker. As a result, ld accepts the last one it received, which is
> "-dynamic-linker=<file>" in this case. Here is the problem: even we passed
> static-pie to clang, and then the clang pass -no-dynamic-linker to ld, This
> option is still omitted by ld.musl-clang. I suggest a fix on it: move
> -dynamic-linker "$ldso" before the previous user inputs.
> Before:
> ```
> exec $($cc -print-prog-name=ld) -nostdlib "$@" -lc -dynamic-linker "$ldso"
> ```
> After:
> ```
>  exec $($cc -print-prog-name=ld) -nostdlib -dynamic-linker "$ldso" "$@" -lc 
> ```
> As you can see, this is a quite simple patch. Further testes may required. And
> for musl-gcc, as I test, it seems that it has the same problem. But I'm not
> familiar with gcc specs file. So maybe if anyone encounter the same problem,
> they can refer this email and give a solution.

It may be better moving -dynamic-linker from ld.musl-clang to musl-clang.
For -shared, -static, and -static-pie, gcc specs does not pass -dynamic-linker.
musl-clang should not pass -dynamic-linker if one of -shared, -static, -static-pie is used.

(I think the ideal solution is not to have any platform-dependent
default ld.so path (if GNU ld had done this, there would have been no
point to have --no-dynamic-linker in the first place). Let
-dynamic-linker create .interp . See https://reviews.llvm.org/D62765)

[-Wunused-command-line-argument] is another issue:

   % ~/musl/Debug/obj/musl-clang a.c -c
   clang: warning: argument unused during compilation: '-fuse-ld=musl-clang' [-Wunused-command-line-argument]
   clang: warning: argument unused during compilation: '-static-libgcc' [-Wunused-command-line-argument]
   clang: warning: argument unused during compilation: '-L-user-start' [-Wunused-command-line-argument]
   clang: warning: argument unused during compilation: '-L/home/ray/.local/stow/musl/lib' [-Wunused-command-line-argument]
   clang: warning: argument unused during compilation: '-L-user-end' [-Wunused-command-line-argument]

Adding a default -Wno-unused-command-line-argument may be a solution.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-24 14:38 [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs Youren Shen
2019-12-24 15:03 ` Rich Felker
2019-12-25 10:37   ` Youren Shen
2019-12-26  4:46     ` Fangrui Song

mailing list of musl libc

Archives are clonable: git clone --mirror http://inbox.vuxu.org/musl

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git