[lld] [lld-macho] Fix Issue with makeSyntheticInputSection + dead_strip (PR #86878)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 27 14:57:30 PDT 2024


https://github.com/alx32 created https://github.com/llvm/llvm-project/pull/86878

Previously, `makeSyntheticInputSection` would create a new `ConcatInputSection` without setting `live` explicitly for it. Without `-dead_strip` this would be OK since `live` would default to `true`.
However, with `-dead_strip`, `live` would default to false, and it would remain set to false.
This hasn't resulted in any issues so far since no code paths that exposed this issue were present.
However a recent change - ObjC relative method lists (https://github.com/llvm/llvm-project/pull/86231) exposes this issue by creating relocations to the `SyntheticInputSection`. 
When these relocations are attempted to be written, this ends up with a crash, since the `SyntheticInputSection` is marked as dead (`live` = `false`).

With this change, we set the correct behavior - `live` will always be true. We add a test case that before this change would crash the linker. 

>From fe5c9d0532e92ee5948407622a2f218be72a9cf3 Mon Sep 17 00:00:00 2001
From: Alex B <alexborcan at meta.com>
Date: Wed, 27 Mar 2024 14:51:19 -0700
Subject: [PATCH] [lld-macho] Fix Issue with makeSyntheticInputSection +
 dead_strip

---
 lld/MachO/InputSection.cpp                         | 1 +
 lld/test/MachO/objc-relative-method-lists-simple.s | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index e3d7a400e4ce92..ec0440ce7a9632 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -281,6 +281,7 @@ ConcatInputSection *macho::makeSyntheticInputSection(StringRef segName,
   Section &section =
       *make<Section>(/*file=*/nullptr, segName, sectName, flags, /*addr=*/0);
   auto isec = make<ConcatInputSection>(section, data, align);
+  isec->live = true;
   section.subsections.push_back({0, isec});
   return isec;
 }
diff --git a/lld/test/MachO/objc-relative-method-lists-simple.s b/lld/test/MachO/objc-relative-method-lists-simple.s
index 1ffec3c6241cf4..5a77085c7d93d8 100644
--- a/lld/test/MachO/objc-relative-method-lists-simple.s
+++ b/lld/test/MachO/objc-relative-method-lists-simple.s
@@ -8,6 +8,10 @@
 # RUN: %no-lsystem-lld a64_rel_dylib.o -o a64_rel_dylib.dylib -map a64_rel_dylib.map -dylib -arch arm64 -objc_relative_method_lists
 # RUN: llvm-objdump --macho --objc-meta-data a64_rel_dylib.dylib  | FileCheck %s --check-prefix=CHK_REL
 
+## Test arm64 + relative method lists + dead-strip
+# RUN: %no-lsystem-lld a64_rel_dylib.o -o a64_rel_dylib.dylib -map a64_rel_dylib.map -dylib -arch arm64 -objc_relative_method_lists -dead_strip
+# RUN: llvm-objdump --macho --objc-meta-data a64_rel_dylib.dylib  | FileCheck %s --check-prefix=CHK_REL
+
 ## Test arm64 + traditional method lists (no relative offsets)
 # RUN: %no-lsystem-lld a64_rel_dylib.o -o a64_rel_dylib.dylib -map a64_rel_dylib.map -dylib -arch arm64 -no_objc_relative_method_lists
 # RUN: llvm-objdump --macho --objc-meta-data a64_rel_dylib.dylib  | FileCheck %s --check-prefix=CHK_NO_REL



More information about the llvm-commits mailing list