Bug 27164

Summary: csu/libc-start.c should not call ARCH_APPLY_IREL in static pie mode
Product: glibc Reporter: Fangrui Song <i>
Component: dynamic-linkAssignee: Not yet assigned to anyone <unassigned>
Status: UNCONFIRMED ---    
Severity: normal CC: hjl.tools, Houdek.Ryan
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Fangrui Song 2021-01-08 17:57:31 UTC
* In GNU ld, __rela_iplt_start is defined for -no-pie, but not for -pie.
* LLD defines __rela_iplt_start, regardless of -no-pie, -pie or -shared.

glibc csu/libc-start.c has undefined weak __rela_iplt_start/__rela_iplt_end and currently requires the two to be equal in static pie mode.
Since there are unavoidable R_*_IRELATIVE relocations, this essentially requires __rela_iplt_start and __rela_iplt_end to be 0, i.e.
the linker does not define the two symbols in -pie mode. However, this address can be easily fixed on glibc side by differenciating static pie from static.

(Note GNU ld does not currently have -no-pie (its -no-pie is currently -n -o "-pie"), but gold/LLD do have.)

binutils commit 3aa14d16c669ca75f9fa4e995a2e2d13069dff3f (2009-06-01) added __rela_iplt_start to ld.
That was before glibc gained static pie support (2017; http://sourceware.org/PR19574).
With static pie, I think the design of __rela_iplt_start should be revised.

diff -u =(ld.bfd --verbose) =(ld.bfd -pie --verbose) reveals two differences.
The image base (__executable_start) is an essential one but the other one (whether __rela_iplt_start is defined) should not be their.


In https://sourceware.org/git/?p=glibc.git;a=shortlog;h=refs/heads/google/grte/v5-2.27/master ,
I find "Make _dl_relocate_static_pie return an int indicating whether it applied relocs." can address the exact glibc csu/libc-start.c issue.

I do see that GNU ld may not want a change (probably in a couple of years) because it does not want to gratuitously break unpatched glibc `configure --enable-static-pie`. But once glibc is fixed and the minimum required version is bumped, the code can be removed from GNU ld. On glibc side, the explicit guard before ARCH_APPLY_IREL also makes things more clear, not relying on subtle zero address __rela_iplt_start.
Comment 1 Carlos O'Donell 2021-07-09 05:11:33 UTC
*** Bug 28066 has been marked as a duplicate of this bug. ***
Comment 2 H.J. Lu 2021-07-11 15:23:21 UTC
Glibc uses DT_XXX to handle relocations, including IFUNC relocations.  Since
there is no need to relocate static executable and there is no DT_XXX in static
executable, I added __rela_iplt_start/__rela_iplt_end to handle IFUNC in static
executable which doesn't need relocation.  Since DT_XXX is used to handle relocations, including IFUNC relocations, in static PIE, linker shouldn't
define __rela_iplt_start/__rela_iplt_end when DT_XXX is created.

Please fix the lld bug.
Comment 3 Fangrui Song 2021-07-15 18:33:55 UTC
I worked around the glibc csu/libc-start.c ARCH_APPLY_IREL issue (unclear semantics) and unneeded diff -u =(ld.bfd --verbose) =(ld.bfd -pie --verbose) difference in ld.lld f8cb78e99aae9aa3f89f7bfe667db2c5b767f21f
([ELF] Don't define __rela_iplt_start for -pie/-shared)
(target: LLD 13.0.0)

but my complaint remains for glibc: https://sourceware.org/pipermail/libc-alpha/2021-July/128981.html