[lld] d2d6da3 - [lld/mac] Don't crash on 32-bit output binaries when dead-stripping

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 4 15:04:16 PDT 2021


Author: Nico Weber
Date: 2021-07-04T18:03:31-04:00
New Revision: d2d6da301100e91901efe1ba4f4c9d006c188bee

URL: https://github.com/llvm/llvm-project/commit/d2d6da301100e91901efe1ba4f4c9d006c188bee
DIFF: https://github.com/llvm/llvm-project/commit/d2d6da301100e91901efe1ba4f4c9d006c188bee.diff

LOG: [lld/mac] Don't crash on 32-bit output binaries when dead-stripping

Fixes PR50974.

Differential Revision: https://reviews.llvm.org/D105399

Added: 
    

Modified: 
    lld/MachO/UnwindInfoSection.cpp
    lld/test/MachO/compact-unwind.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp
index 5c9c5e03d1c13..f78b9cefeb19e 100644
--- a/lld/MachO/UnwindInfoSection.cpp
+++ b/lld/MachO/UnwindInfoSection.cpp
@@ -227,6 +227,9 @@ static ConcatInputSection *checkTextSegment(InputSection *isec) {
   return cast<ConcatInputSection>(isec);
 }
 
+template <class Ptr>
+constexpr Ptr TombstoneValue = std::numeric_limits<Ptr>::max();
+
 // We need to apply the relocations to the pre-link compact unwind section
 // before converting it to post-link form. There should only be absolute
 // relocations here: since we are not emitting the pre-link CU section, there
@@ -243,7 +246,7 @@ relocateCompactUnwind(ConcatOutputSection *compactUnwindSection,
     memcpy(buf, isec->data.data(), isec->data.size());
 
     for (const Reloc &r : isec->relocs) {
-      uint64_t referentVA = UINT64_MAX; // Tombstone value
+      uint64_t referentVA = TombstoneValue<Ptr>;
       if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
         if (!isa<Undefined>(referentSym)) {
           assert(referentSym->isInGot());
@@ -303,7 +306,7 @@ static void addEntriesForFunctionsWithoutUnwindInfo(
     std::vector<CompactUnwindEntry<Ptr>> &cuVector) {
   DenseSet<Ptr> hasUnwindInfo;
   for (CompactUnwindEntry<Ptr> &cuEntry : cuVector)
-    if (cuEntry.functionAddress != UINT64_MAX)
+    if (cuEntry.functionAddress != TombstoneValue<Ptr>)
       hasUnwindInfo.insert(cuEntry.functionAddress);
 
   // Add explicit "has no unwind info" entries for all global and local symbols
@@ -384,13 +387,13 @@ template <class Ptr> void UnwindInfoSectionImpl<Ptr>::finalize() {
     return a->functionAddress < b->functionAddress;
   });
 
-  // Dead-stripped functions get a functionAddress of UINT64_MAX in
+  // Dead-stripped functions get a functionAddress of TombstoneValue in
   // relocateCompactUnwind(). Filter them out here.
   // FIXME: This doesn't yet collect associated data like LSDAs kept
   // alive only by a now-removed CompactUnwindEntry or other comdat-like
   // data (`kindNoneGroupSubordinate*` in ld64).
   CompactUnwindEntry<Ptr> tombstone;
-  tombstone.functionAddress = static_cast<Ptr>(UINT64_MAX);
+  tombstone.functionAddress = TombstoneValue<Ptr>;
   cuPtrVector.erase(
       std::lower_bound(cuPtrVector.begin(), cuPtrVector.end(), &tombstone,
                        [](const CompactUnwindEntry<Ptr> *a,

diff  --git a/lld/test/MachO/compact-unwind.s b/lld/test/MachO/compact-unwind.s
index 075bb326fd55e..a1aa250f6c08e 100644
--- a/lld/test/MachO/compact-unwind.s
+++ b/lld/test/MachO/compact-unwind.s
@@ -4,21 +4,21 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/main.s -o %t/x86_64-main.o
 # RUN: %lld -arch x86_64 -pie -lSystem -lc++ %t/x86_64-my-personality.o %t/x86_64-main.o -o %t/x86_64-personality-first
 # RUN: llvm-objdump --macho --unwind-info --syms --indirect-symbols --rebase %t/x86_64-personality-first | FileCheck %s --check-prefixes=FIRST,CHECK -D#%x,BASE=0x100000000
-# RUN: %lld -arch x86_64 -pie -lSystem -lc++ %t/x86_64-main.o %t/x86_64-my-personality.o -o %t/x86_64-personality-second
+# RUN: %lld -dead_strip -arch x86_64 -pie -lSystem -lc++ %t/x86_64-main.o %t/x86_64-my-personality.o -o %t/x86_64-personality-second
 # RUN: llvm-objdump --macho --unwind-info --syms --indirect-symbols --rebase %t/x86_64-personality-second | FileCheck %s --check-prefixes=SECOND,CHECK -D#%x,BASE=0x100000000
 
 # RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin19.0.0 %t/my-personality.s -o %t/arm64-my-personality.o
 # RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin19.0.0 %t/main.s -o %t/arm64-main.o
 # RUN: %lld -arch arm64 -pie -lSystem -lc++ %t/arm64-my-personality.o %t/arm64-main.o -o %t/arm64-personality-first
 # RUN: llvm-objdump --macho --unwind-info --syms --indirect-symbols --rebase %t/arm64-personality-first | FileCheck %s --check-prefixes=FIRST,CHECK -D#%x,BASE=0x100000000
-# RUN: %lld -arch arm64 -pie -lSystem -lc++ %t/arm64-main.o %t/arm64-my-personality.o -o %t/arm64-personality-second
+# RUN: %lld -dead_strip -arch arm64 -pie -lSystem -lc++ %t/arm64-main.o %t/arm64-my-personality.o -o %t/arm64-personality-second
 # RUN: llvm-objdump --macho --unwind-info --syms --indirect-symbols --rebase %t/arm64-personality-second | FileCheck %s --check-prefixes=SECOND,CHECK -D#%x,BASE=0x100000000
 
 # RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-watchos %t/my-personality.s -o %t/arm64-32-my-personality.o
 # RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-watchos %t/main.s -o %t/arm64-32-main.o
 # RUN: %lld-watchos -pie -lSystem -lc++ %t/arm64-32-my-personality.o %t/arm64-32-main.o -o %t/arm64-32-personality-first
 # RUN: llvm-objdump --macho --unwind-info --syms --indirect-symbols --rebase %t/arm64-32-personality-first | FileCheck %s --check-prefixes=FIRST,CHECK -D#%x,BASE=0x4000
-# RUN: %lld-watchos -pie -lSystem -lc++ %t/arm64-32-main.o %t/arm64-32-my-personality.o -o %t/arm64-32-personality-second
+# RUN: %lld-watchos -dead_strip -pie -lSystem -lc++ %t/arm64-32-main.o %t/arm64-32-my-personality.o -o %t/arm64-32-personality-second
 # RUN: llvm-objdump --macho --unwind-info --syms --indirect-symbols --rebase %t/arm64-32-personality-second | FileCheck %s --check-prefixes=SECOND,CHECK -D#%x,BASE=0x4000
 
 # FIRST:      Indirect symbols for (__DATA_CONST,__got)
@@ -107,13 +107,16 @@ _main:
 ## -fno-exceptions, while the previous and next TU might be Objective-C++
 ## which has unwind info for Objective-C).
 .p2align 2
+.no_dead_strip _quux
 _quux:
   ret
 
 .globl _abs
+.no_dead_strip _abs
 _abs = 4
 
 .p2align 2
+.no_dead_strip _baz
 _baz:
   .cfi_startproc
 ## This will generate a symbol relocation. Check that we reuse the personality
@@ -124,7 +127,18 @@ _baz:
   ret
   .cfi_endproc
 
+.globl _stripped
+_stripped:
+  .cfi_startproc
+  .cfi_personality 155, ___gxx_personality_v0
+  .cfi_lsda 16, _exception1
+  .cfi_def_cfa_offset 16
+  ret
+  .cfi_endproc
+
 
 .section __TEXT,__gcc_except_tab
 _exception1:
   .space 1
+
+.subsections_via_symbols


        


More information about the llvm-commits mailing list