Buildroot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [Buildroot] [PATCH v4] Config.in: ban textrels on musl toolchains
@ 2024-07-18 20:33 J. Neuschäfer via buildroot
  2024-07-19  6:49 ` J. Neuschäfer via buildroot
  0 siblings, 1 reply; 4+ messages in thread
From: J. Neuschäfer via buildroot @ 2024-07-18 20:33 UTC (permalink / raw)
  To: buildroot; +Cc: Yann E. MORIN, Thomas Petazzoni, J. Neuschäfer

musl-libc doesn't support TEXTRELs[1] and programs with TEXTRELs will
crash on start-up under musl.

This patch forbids the use of TEXTRELs on musl toolchains with dynamic
linking.

To verify this patch:

- Delete package/micropython/0001-py-nlrthumb-Make-non-Thumb2-long-jump-workaround-opt.patch
- Build micropython (before v1.23) with a musl toolchain and
  BR2_SHARED_LIBS. The build should abort while linking micropython.

[1]: https://www.openwall.com/lists/musl/2020/09/25/4

Signed-off-by: J. Neuschäfer <j.neuschaefer@gmx.net>
---
Changes in v4:
- Remove Config.in option, implement the whole logic in
  package/Makefile.in
- Document how to verify
- Link to v3: https://lore.kernel.org/r/20240704-ztext-v3-1-9195d06c8bd0@gmx.net

Changes in v3:
- drop micropython patch (already merged)
- rewrite to positive logic, as suggested by Yann E. MORIN
- Link to v2: https://lore.kernel.org/r/20240529-ztext-v2-0-82985032f169@gmx.net

Changes in v2:
- Slightly different wording
- Enable the option by default on musl toolchains
- Add patch to fix build of micropython

Link to v1:
- https://lore.kernel.org/r/20240419-ztext-v1-1-a8d5c2cfcf57@gmx.net
---
---
 package/Makefile.in | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/package/Makefile.in b/package/Makefile.in
index 47a89f1ae1..3587662014 100644
--- a/package/Makefile.in
+++ b/package/Makefile.in
@@ -149,6 +149,15 @@ endif

 TARGET_LDFLAGS = $(call qstrip,$(BR2_TARGET_LDFLAGS))

+# musl's dynamic loader doesn't support DT_TEXTREL, which results in a runtime
+# crash if it gets used. The "-z text" linker option issues a build-time error
+# when DT_TEXREL is used, so we capture the problem earlier.
+#
+# See also: https://www.openwall.com/lists/musl/2020/09/25/4
+ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_BINFMT_SUPPORTS_SHARED),yy)
+TARGET_LDFLAGS += -Wl,-z,text
+endif
+
 # By design, _FORTIFY_SOURCE requires gcc optimization to be enabled.
 # Therefore, we need to pass _FORTIFY_SOURCE and the optimization level
 # through the same mechanism, i.e currently through CFLAGS. Passing

---
base-commit: c624eee120b6ee0c81e636bd47abe92452610cf7
change-id: 20240417-ztext-5accbab61c0a

Best regards,
--
J. Neuschäfer <j.neuschaefer@gmx.net>

_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

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

* Re: [Buildroot] [PATCH v4] Config.in: ban textrels on musl toolchains
  2024-07-18 20:33 [Buildroot] [PATCH v4] Config.in: ban textrels on musl toolchains J. Neuschäfer via buildroot
@ 2024-07-19  6:49 ` J. Neuschäfer via buildroot
  2024-07-19  7:19   ` Arnout Vandecappelle via buildroot
  0 siblings, 1 reply; 4+ messages in thread
From: J. Neuschäfer via buildroot @ 2024-07-19  6:49 UTC (permalink / raw)
  To: J. Neuschäfer; +Cc: Yann E. MORIN, Thomas Petazzoni, buildroot

On Thu, Jul 18, 2024 at 10:33:23PM +0200, J. Neuschäfer wrote:
> +ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_BINFMT_SUPPORTS_SHARED),yy)

I just noticed that BR2_BINFMT_SUPPORTS_SHARED is incorrect, because
it's true regardless of whether shared libraries are actually used.
What I was aiming for is "BR2_SHARED_LIBS or BR2_SHARED_STATIC_LIBS".

I suppose this should work:

ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_SHARED_LIBS)$(BR2_SHARED_STATIC_LIBS),yy)

... but I find it a bit unclear because it technically doesn't limit
which two of the three options have to be true (this is done elsewhere,
in the Kconfig logic that makes BR2_SHARED_LIBS and BR2_SHARED_STATIC_LIBS
mutually exclusive). I'll rather go for two ifeqs in this case:

ifeq ($(BR2_TOOLCHAIN_USES_MUSL),y)
ifeq ($(BR2_SHARED_LIBS)$(BR2_SHARED_STATIC_LIBS),y)


-- jn
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

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

* Re: [Buildroot] [PATCH v4] Config.in: ban textrels on musl toolchains
  2024-07-19  6:49 ` J. Neuschäfer via buildroot
@ 2024-07-19  7:19   ` Arnout Vandecappelle via buildroot
  2024-07-19 13:43     ` J. Neuschäfer via buildroot
  0 siblings, 1 reply; 4+ messages in thread
From: Arnout Vandecappelle via buildroot @ 2024-07-19  7:19 UTC (permalink / raw)
  To: J. Neuschäfer; +Cc: Yann E. MORIN, Thomas Petazzoni, buildroot



On 19/07/2024 08:49, J. Neuschäfer wrote:
> On Thu, Jul 18, 2024 at 10:33:23PM +0200, J. Neuschäfer wrote:
>> +ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_BINFMT_SUPPORTS_SHARED),yy)
> 
> I just noticed that BR2_BINFMT_SUPPORTS_SHARED is incorrect, because
> it's true regardless of whether shared libraries are actually used.
> What I was aiming for is "BR2_SHARED_LIBS or BR2_SHARED_STATIC_LIBS".
> 
> I suppose this should work:
> 
> ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_SHARED_LIBS)$(BR2_SHARED_STATIC_LIBS),yy)

  What we do is:

ifeq ($(BR2_TOOLCHAIN_USES_MUSL):$(BR2_STATIC_LIBS),y:)

  This says that musl must be y and static must not be.

  Regards,
  Arnout

> 
> ... but I find it a bit unclear because it technically doesn't limit
> which two of the three options have to be true (this is done elsewhere,
> in the Kconfig logic that makes BR2_SHARED_LIBS and BR2_SHARED_STATIC_LIBS
> mutually exclusive). I'll rather go for two ifeqs in this case:
> 
> ifeq ($(BR2_TOOLCHAIN_USES_MUSL),y)
> ifeq ($(BR2_SHARED_LIBS)$(BR2_SHARED_STATIC_LIBS),y)
> 
> 
> -- jn
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

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

* Re: [Buildroot] [PATCH v4] Config.in: ban textrels on musl toolchains
  2024-07-19  7:19   ` Arnout Vandecappelle via buildroot
@ 2024-07-19 13:43     ` J. Neuschäfer via buildroot
  0 siblings, 0 replies; 4+ messages in thread
From: J. Neuschäfer via buildroot @ 2024-07-19 13:43 UTC (permalink / raw)
  To: Arnout Vandecappelle
  Cc: Yann E. MORIN, J. Neuschäfer, Thomas Petazzoni, buildroot

On Fri, Jul 19, 2024 at 09:19:22AM +0200, Arnout Vandecappelle wrote:
> On 19/07/2024 08:49, J. Neuschäfer wrote:
> > On Thu, Jul 18, 2024 at 10:33:23PM +0200, J. Neuschäfer wrote:
> > > +ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_BINFMT_SUPPORTS_SHARED),yy)
> >
> > I just noticed that BR2_BINFMT_SUPPORTS_SHARED is incorrect, because
> > it's true regardless of whether shared libraries are actually used.
> > What I was aiming for is "BR2_SHARED_LIBS or BR2_SHARED_STATIC_LIBS".
> >
> > I suppose this should work:
> >
> > ifeq ($(BR2_TOOLCHAIN_USES_MUSL)$(BR2_SHARED_LIBS)$(BR2_SHARED_STATIC_LIBS),yy)
>
>  What we do is:
>
> ifeq ($(BR2_TOOLCHAIN_USES_MUSL):$(BR2_STATIC_LIBS),y:)
>
>  This says that musl must be y and static must not be.

Nice trick, I'll use it.
_______________________________________________
buildroot mailing list
buildroot@buildroot.org
https://lists.buildroot.org/mailman/listinfo/buildroot

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

end of thread, other threads:[~2024-07-19 13:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-18 20:33 [Buildroot] [PATCH v4] Config.in: ban textrels on musl toolchains J. Neuschäfer via buildroot
2024-07-19  6:49 ` J. Neuschäfer via buildroot
2024-07-19  7:19   ` Arnout Vandecappelle via buildroot
2024-07-19 13:43     ` J. Neuschäfer via buildroot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox