From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/15058 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Youren Shen Newsgroups: gmane.linux.lib.musl.general Subject: Re: [BUG] Force to use a dynamic linker in musl-clang wapper cause a crash for static-pie c++ programs. Date: Wed, 25 Dec 2019 18:37:01 +0800 Message-ID: References: <20191224150331.GX1666@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="000000000000f260ec059a84d754" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="99445"; mail-complaints-to="usenet@blaine.gmane.org" Cc: musl@lists.openwall.com To: Rich Felker Original-X-From: musl-return-15074-gllmg-musl=m.gmane.org@lists.openwall.com Wed Dec 25 15:49:03 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1ik7yB-000Pm4-7T for gllmg-musl@m.gmane.org; Wed, 25 Dec 2019 15:49:03 +0100 Original-Received: (qmail 30474 invoked by uid 550); 25 Dec 2019 14:48:58 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26137 invoked from network); 25 Dec 2019 10:37:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nDn6OfOtjIyI+eauC234Z3NyMhmDpiIbGjOFyTaTMFM=; b=mkYXk3FrhNBdm66Qtd/a+7pi0wzXF28W5OoJFyoawWGfWxPIFLlc2tiLaWr7OAuRJh syOKaPFDfBIT0XXXv27+8MkGVwjkLUt/ZSjLGz/Sd1Tbz7lUSTi19WyBsOPMGUNsXYD9 LPCSCYG2U5afS3KWiMjF2oj1YrKUay+oD9c5rDisMH/6UKE57sUhI2IjXBkL63WUEXXN GfdzYblwTu8bDeRY6FeneWcpxAWsH20n3IhqR2D6o9GPtiFTewgsqHjlFJMP8OqV4JTC mXq84WcxFKnyRP1oJ3BFE1AjEfNvszwJ1nvZeL24rC5FjiFEJeuz26TQqx4MvEcCwOaH 9RvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nDn6OfOtjIyI+eauC234Z3NyMhmDpiIbGjOFyTaTMFM=; b=QKVmESEGO2AMVqKHQxn4VvGHXaMb2cmp3yB5Jx+YgUE0mnZkfYU/vYWRWkBp21BbwV V+hxvdtMVdonfYL/Fa4CfZuDg0oBe+imgmWS3SJB26d7cVs2Y5O4DZ7balUKnr71UWWN vrR1NZYh6ElOBlaEtBmUh+xAvAezFozB1GVcembIEVmj04H2/JBDhR2fjKQI2W2euMpe Hg/pjyJHVA2KCtd5DLRXNsqKK1L3LWlzTWaQ/WwPPWqPy399GC6ykddaca5qubNOd6w1 3AN/97tySz3lKa6+Dl5hIlfaMOL5Elxx2en5ncrg/8If6+VAFAXRNNap0p67qJp+74Zy ogHw== X-Gm-Message-State: APjAAAUDnKsFLYPfXBleATGpkYZz6qNI+FMHTnELVuBx7v9lxJP39dzM PjjytyaKdEbdjF8W5D8ATcS9LbarYAg0TpO9Ei4= X-Google-Smtp-Source: APXvYqyM7bAR8Zc2EmFyrwcCM20/74Z0SoeEbjJVjcHJXQ7Tn9yN9ws9gzwUGN1fPKhQlPk/56RjmS6PoYOx3BwKll4= X-Received: by 2002:a5d:68c5:: with SMTP id p5mr40669669wrw.193.1577270232893; Wed, 25 Dec 2019 02:37:12 -0800 (PST) In-Reply-To: <20191224150331.GX1666@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:15058 Archived-At: --000000000000f260ec059a84d754 Content-Type: multipart/alternative; boundary="000000000000f260ea059a84d752" --000000000000f260ea059a84d752 Content-Type: text/plain; charset="UTF-8" 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= 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=" 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 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. --000000000000f260ea059a84d752 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks for your reply.

Dived deeper, I = think this is because the options clang/gcc passed to ld are not well handl= ed by ld. Unlike "-static", which is passed directly to the linke= r, the clang pass -no-dynamic-linker when static-pie is enabled. However, -= dynamic-linker=3D<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=3D<file>" in this case. Here is the pro= blem: even we passed static-pie to clang, and then the clang pass -no-dynam= ic-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-pr= og-name=3Dld) -nostdlib "$@" -lc -dynamic-linker "$ldso"= ;
```
After:
```
=C2=A0exec $($cc -= print-prog-name=3Dld) -nostdlib -dynamic-linker "$ldso" "$@&= quot; -lc=C2=A0
```
As you can see, this is a quite sim= ple patch. Further testes may required. And for musl-gcc, as I test, it see= ms that it has the same problem. But I'm not familiar with gcc specs fi= le. So maybe if anyone encounter the same problem, they can refer this emai= l and give a solution.

On Tue, Dec 24, 2019 at 11:03 PM Rich Felker <= ;dalias@libc.org&g= t; 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. Th= is
> 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 previou= s
> 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 ena= bled.
>
> Thank you very much.
>
> [1]. https://groups.goog= le.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.
--000000000000f260ea059a84d752-- --000000000000f260ec059a84d754 Content-Type: application/octet-stream; name="static-pie-for-ld.musl-clang.patch" Content-Disposition: attachment; filename="static-pie-for-ld.musl-clang.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_k4l5p2i20 ZGlmZiAtLWdpdCBhL3Rvb2xzL2xkLm11c2wtY2xhbmcuaW4gYi90b29scy9sZC5tdXNsLWNsYW5n LmluCmluZGV4IDkzNzYzZDYuLjdjYjQ5ZDkgMTAwNjQ0Ci0tLSBhL3Rvb2xzL2xkLm11c2wtY2xh bmcuaW4KKysrIGIvdG9vbHMvbGQubXVzbC1jbGFuZy5pbgpAQCAtNDgsNCArNDgsNCBAQCBmb3Ig eCA7IGRvCiAgICAgZXNhYwogZG9uZQoKLWV4ZWMgJCgkY2MgLXByaW50LXByb2ctbmFtZT1sZCkg LW5vc3RkbGliICIkQCIgLWxjIC1keW5hbWljLWxpbmtlciAiJGxkc28iCitleGVjICQoJGNjIC1w cmludC1wcm9nLW5hbWU9bGQpIC1ub3N0ZGxpYiAtZHluYW1pYy1saW5rZXIgIiRsZHNvIiAiJEAi IC1sYw== --000000000000f260ec059a84d754--