[PATCH] D107747: [ELF] Don't emit SHF_GNU_RETAIN on Solaris

Rainer Orth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 9 01:10:37 PDT 2021


ro created this revision.
ro added a reviewer: MaskRay.
Herald added subscribers: pengfei, fedor.sergeev, hiraditya, jyknight.
ro requested review of this revision.
Herald added a project: LLVM.

The introduction of `SHF_GNU_RETAIN` has caused massive problems on Solaris.

Initially, as reported in Bug 49437, it caused dozens of testsuite failures on both sparc and
x86.  The objects were marked as `ELFOSABI_NONE`, but `SHF_GNU_RETAIN` is a GNU
extension. In the native Solaris ABI, that flag (in the range for OS specific values) is
`SHF_SUNW_ABSENT` with a completely different semantics, which confuses Solaris `ld`
very much.

Later, the objects became (correctly) marked `ELFOSABI_GNU`, which Solaris `ld`
doesn't support, causing it to SEGV and break the build.  The linker is currently being
hardened to not accept non-native OS ABIs to avoid this.

The need for linker support is already documented in `clang/include/clang/Basic/AttrDocs.td`,
but not currently checked.

This patch avoids all this by not emitting `SHF_GNU_RETAIN` on Solaris at all.

Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and `x86_64-pc-linux-gnu`.

The current patch is a minimal solution.  For full generality, any code emitting extensions
beyond the ELF gABI should check that all of the assembler, linker, and runtime linker
used support it.  While in some cases assembler support is enough, in others all three
components will be needed.

If support for `SHF_GNU_RETAIN` is crucial, Solaris has an equivalent flag `SHF_SUNW_NODISCARD`
that could be used instead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107747

Files:
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp


Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -677,8 +677,9 @@
   }
 
   if (Retain) {
-    if (Ctx.getAsmInfo()->useIntegratedAssembler() ||
-        Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36))
+    if ((Ctx.getAsmInfo()->useIntegratedAssembler() ||
+         Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) &&
+        !TM.getTargetTriple().isOSSolaris())
       Flags |= ELF::SHF_GNU_RETAIN;
     return NextUniqueID++;
   }
@@ -855,8 +856,10 @@
     EmitUniqueSection = true;
     Flags |= ELF::SHF_LINK_ORDER;
   }
-  if (Retain && (Ctx.getAsmInfo()->useIntegratedAssembler() ||
-                 Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36))) {
+  if (Retain &&
+      (Ctx.getAsmInfo()->useIntegratedAssembler() ||
+       Ctx.getAsmInfo()->binutilsIsAtLeast(2, 36)) &&
+      !TM.getTargetTriple().isOSSolaris()) {
     EmitUniqueSection = true;
     Flags |= ELF::SHF_GNU_RETAIN;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107747.365116.patch
Type: text/x-patch
Size: 1078 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210809/2fca913e/attachment.bin>


More information about the llvm-commits mailing list