Bug 28066 - Using static-pie and lld results in runtime crash
Summary: Using static-pie and lld results in runtime crash
Status: RESOLVED DUPLICATE of bug 27164
Alias: None
Product: glibc
Classification: Unclassified
Component: dynamic-link (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-07-08 18:46 UTC by Ryan Houdek
Modified: 2021-07-09 05:11 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ryan Houdek 2021-07-08 18:46:37 UTC
Due to how IRELATIVE sections are emitted in to a static-pie elf with LLD. glibc will crash early in the application boot process.

This ML post had some information on it: https://sourceware.org/pipermail/libc-alpha/2021-January/121752.html

This patch resolves the compilation crash locally: https://sourceware.org/git/?p=glibc.git;a=commit;h=c67127a88540f6de4b92370158b5da61bec23f4f

For some more context inside of the bug report. ARCH_SETUP_IREL() attempts setting up IRELATIVE sections but crashes in this instance. This is seemingly a difference between lld and ld about these sections being emitted or not and glibc handling only one case?
Comment 1 Carlos O'Donell 2021-07-08 19:09:16 UTC
H.J.'s initial position appears to be that this is a bug in lld and that it should not emit __rela_iplt_start/__rela_iplt_end for PIE:

https://sourceware.org/pipermail/libc-alpha/2021-January/121755.html

Given that upstream glibc is unable to run such binaries it's hard to argue that we need to include such compat code.

I'm curious to hear Fangrui's comments on this issue and what direction lld is considering.
Comment 2 Fangrui Song 2021-07-08 20:31:01 UTC
H.J. Lu and I did not reach a consensus in January.

I think ld.lld defining __rela_iplt_start in -pie mode is a conscious and good decision.
Taking "csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied IRELATIVE relocations" have
two benefits:

* improve readability of the glibc ld.so code
* enable removing a -pie and -no-pie difference in GNU ld

The current code relies on ARCH_SETUP_IREL doing nothing in static-pie mode (__rela_iplt_start = __rela_iplt_end = 0 (as undefined weak symbols))
but doing something in static no-pie mode.
This is less clear than making the intention explicit in the code:

-  _dl_relocate_static_pie ();
+  int irel_applied = _dl_relocate_static_pie ();
 
   /* Perform IREL{,A} relocations.  */
-  ARCH_SETUP_IREL ();
+  if (!irel_applied)
+    ARCH_SETUP_IREL ();

Second, if you run `diff =(ld.bfd --verbose) =(ld.bfd --verbose -pie)`, other than the image base difference,
whether __rela_iplt_start/__rela_iplt_end are defined is the only other difference.

Defining __rela_iplt_start/__rela_iplt_end in -pie mode can drop this difference.
In a few years, when the glibc's requirement on GNU ld raises, we can make the cleanup into GNU ld.
Comment 3 Carlos O'Donell 2021-07-08 21:40:21 UTC
(In reply to Fangrui Song from comment #2)
> Taking "csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied
> IRELATIVE relocations"

Would you please submit this as an official patch to libc-alpha? That way I can discuss and review in the next Monday morning patchwork patch queue review (2021-07-12).

Even though you and HJ did not achieve consensus, the patch as-is is functionally no different than the existing code. The existing code depends on specific expectations of the binary artifact which could vary over time, and it's more robust to be explicit in the steps and operations taken.
Comment 4 Fangrui Song 2021-07-08 22:13:19 UTC
(In reply to Carlos O'Donell from comment #3)
> (In reply to Fangrui Song from comment #2)
> > Taking "csu: Skip ARCH_SETUP_IREL if _dl_relocate_static_pie applied
> > IRELATIVE relocations"
> 
> Would you please submit this as an official patch to libc-alpha? That way I
> can discuss and review in the next Monday morning patchwork patch queue
> review (2021-07-12).
> 
> Even though you and HJ did not achieve consensus, the patch as-is is
> functionally no different than the existing code. The existing code depends
> on specific expectations of the binary artifact which could vary over time,
> and it's more robust to be explicit in the steps and operations taken.

Thanks:) Sent https://sourceware.org/pipermail/libc-alpha/2021-July/128810.html
Comment 5 Carlos O'Donell 2021-07-09 05:11:33 UTC
Closing this as a duplicate of 27164 (filed in January by Fangrui).

*** This bug has been marked as a duplicate of bug 27164 ***