Bug 2014699

Summary: [ld][glibc] Adopt SHT_RELR/DT_RELR to decrease PIE and shared object size
Product: [Fedora] Fedora Reporter: Fangrui Song <i>
Component: glibcAssignee: Carlos O'Donell <codonell>
Status: CLOSED UPSTREAM QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 34CC: aoliva, arjun.is, codonell, dj, fweimer, law, mcermak, mfabian, pfrankli, rth, sipoyare
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2021-10-19 13:12:00 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Fangrui Song 2021-10-15 21:46:35 UTC
The SHT_RELR/DT_RELR format encodes relative relocations in a very efficient way (quite usually takes just 3% or smaller space).
The size optimization can greatly decrease the virtual memory size of PIE and shared objects with many R_*_RELATIVE relocations.

E.g. The (mostly statically linked) clang executable's virtual memory size is 8.2% smaller with SHT_RELR/DT_RELR. The size varies across projects, non-symbolic linking may dilute the saving, but I anticipate at least 5% decrease for most PIE.

% ~/projects/bloaty/Release/bloaty clang.pie.relr -- clang.pie
    FILE SIZE        VM SIZE    
 --------------  -------------- 
  [NEW]  +163Ki  [NEW]  +163Ki    .relr.dyn
  +4.9%     +32  +5.4%     +32    .dynamic
  +2.5%      +8  [ = ]       0    .shstrtab
 -99.5% -13.8Mi -99.5% -13.8Mi    .rela.dyn
  -8.3% -13.6Mi  -8.2% -13.6Mi    TOTAL

The SHT_RELR/DT_RELR relocation format requires linker and loader support.

* On the linkder side, ld.lld has supported .relr.dyn/SHT_RELR/DT_RELR (`--pack-dyn-relocs=relr`) for a long time and the support has been stable since 2019-09 (after I fixed an address oscillating bug).
* On the loader (glibc ld.so) side, a patch exists https://sourceware.org/pipermail/libc-alpha/2021-October/131768.html
  but lack of GNU ld support may impede its adoption among Linux distributions.
  User support is also important to push the patch forward. (ia64 according to folks isn't an issue. glibc doesn't implement ELFCLASS32 for ia64 AFAICT.)

(
Worth noting that the Linux kernel's arm64 port supports SHT_RELR/DT_RELR since 2019.
ChromeOS has maintained a local glibc patch since around 2018.

RELR can render Firefox's ELF hack (https://glandium.org/blog/?p=1177) unneeded.
)

So I file this ticket seeking for support (comment on https://sourceware.org/bugzilla/show_bug.cgi?id=27924). I hope that with sufficient attention from users,
someone (e.g. a GNU ld maintainer) will eventually stand up and implement `--pack-dyn-relocs=relr` for GNU ld (https://sourceware.org/bugzilla/show_bug.cgi?id=27923).
Even in the absence of GNU ld support, I hope glibc can accept the DT_RELR patch, so that ld.lld users can use `--pack-dyn-relocs=relr`.

See also Gentoo: https://bugs.gentoo.org/818376 Arch Linux: https://bugs.archlinux.org/task/72433 Debian https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=996598

glibc patch: https://sourceware.org/pipermail/libc-alpha/2021-October/132029.html

Comment 1 Carlos O'Donell 2021-10-19 13:12:00 UTC
This issue was discussed in the Monday morning community patch review:
https://sourceware.org/pipermail/libc-alpha/2021-October/132046.html
~~~
 * [v2] elf: Support DT_RELR relative relocation format [BZ #27924]
  * Need official gABI assignment (Carry Coutant status?)
   * Needs to be reserved verbally by Carry (Action item)
   * Required for patch acceptance.
  * Need BFD port or a released llvm/lld that supports DT_RELR to review patches.
   * Minimally a 32-bit and 64-bit port that can build glibc and passes regression testing.
   * Required for patch review.
  * Official glibc relese with feature?
   * Need BFD port to support build-many-glibcs to provide testing confidence.
  * HJ: Would like to see a BFD port before a glibc release with the features.
   * Sufficient to have a released binutils with DT_RELR also.
  * Release date of released static linkers with support should be close to glibc release with feature.
   * When glibc is released users can pick a static linker to test with (binutils or lld).
~~~
This needs to be fixed in upstream glibc before we will consider this for downstream Fedora.

I'm marking this bug CLOSED/UPSTREAM to indicate that we are tracking this upstream in sourceware bugzilla 27924.

When the upstream bug is fixed we can come back to this for Fedora.