[lld] [lld-macho] Add support for category names in ConcatInputSection's (PR #90850)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 5 15:06:40 PDT 2024


https://github.com/alx32 updated https://github.com/llvm/llvm-project/pull/90850

>From 778c11c82acfb6c2cd7673e0fc77c6e740a07d3c Mon Sep 17 00:00:00 2001
From: Alex Borcan <alexborcan at fb.com>
Date: Thu, 2 May 2024 05:22:23 -0700
Subject: [PATCH 1/6] [lld-macho] Add support for category names in
 ConcatInputSection's

---
 lld/MachO/ObjC.cpp                              | 17 ++++++++++++++---
 ...objc-category-merging-extern-class-minimal.s |  2 +-
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index 4760fffebe3b30..99074410871385 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -186,13 +186,24 @@ ObjcCategoryChecker::ObjcCategoryChecker()
       roClassLayout(target->wordSize), listHeaderLayout(target->wordSize),
       methodLayout(target->wordSize) {}
 
-// \p r must point to an offset within a cstring section.
+// \p r must point to an offset within a CStringInputSection or a
+// ConcatInputSection
 static StringRef getReferentString(const Reloc &r) {
   if (auto *isec = r.referent.dyn_cast<InputSection *>())
     return cast<CStringInputSection>(isec)->getStringRefAtOffset(r.addend);
   auto *sym = cast<Defined>(r.referent.get<Symbol *>());
-  return cast<CStringInputSection>(sym->isec())
-      ->getStringRefAtOffset(sym->value + r.addend);
+  if (isa<CStringInputSection>(sym->isec()))
+    return cast<CStringInputSection>(sym->isec())
+        ->getStringRefAtOffset(sym->value + r.addend);
+
+  if (isa<ConcatInputSection>(sym->isec())) {
+    auto &data = sym->isec()->data;
+    const char *pDataStart = reinterpret_cast<const char *>(data.data());
+    uint32_t len = strnlen(pDataStart + sym->value, data.size() - sym->value);
+    return StringRef(pDataStart + sym->value, len);
+  }
+
+  llvm_unreachable("unknown reference section in getReferentString");
 }
 
 void ObjcCategoryChecker::parseMethods(const ConcatInputSection *methodsIsec,
diff --git a/lld/test/MachO/objc-category-merging-extern-class-minimal.s b/lld/test/MachO/objc-category-merging-extern-class-minimal.s
index ea79f29a421c5c..f7d333ef70fce2 100644
--- a/lld/test/MachO/objc-category-merging-extern-class-minimal.s
+++ b/lld/test/MachO/objc-category-merging-extern-class-minimal.s
@@ -118,7 +118,7 @@ __OBJC_$_CATEGORY_MyBaseClass_$_Category01:
 	.quad	0
 	.long	64                              ; 0x40
 	.space	4
-	.section	__TEXT,__objc_classname,cstring_literals
+	.section	__DATA,__objc_const
 l_OBJC_CLASS_NAME_.1:                   ; @OBJC_CLASS_NAME_.1
 	.asciz	"Category02"
 	.section	__TEXT,__objc_methname,cstring_literals

>From 248eb26853cc21ac9bcb050a50f877b5953f2164 Mon Sep 17 00:00:00 2001
From: alx32 <103613512+alx32 at users.noreply.github.com>
Date: Thu, 2 May 2024 13:56:33 -0700
Subject: [PATCH 2/6] Apply suggestions from code review

Address Feedback nr.1

Co-authored-by: Ellis Hoag <ellis.sparky.hoag at gmail.com>
---
 lld/MachO/ObjC.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index 99074410871385..75581f315c99e1 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -192,9 +192,8 @@ static StringRef getReferentString(const Reloc &r) {
   if (auto *isec = r.referent.dyn_cast<InputSection *>())
     return cast<CStringInputSection>(isec)->getStringRefAtOffset(r.addend);
   auto *sym = cast<Defined>(r.referent.get<Symbol *>());
-  if (isa<CStringInputSection>(sym->isec()))
-    return cast<CStringInputSection>(sym->isec())
-        ->getStringRefAtOffset(sym->value + r.addend);
+  if (auto *s = dyn_cast_or_null<CStringInputSection>(sym->isec()))
+    return s->getStringRefAtOffset(sym->value + r.addend);
 
   if (isa<ConcatInputSection>(sym->isec())) {
     auto &data = sym->isec()->data;

>From c101bc0e64246ff2880a39def0dd4f3739821a60 Mon Sep 17 00:00:00 2001
From: Alex Borcan <alexborcan at fb.com>
Date: Thu, 2 May 2024 15:54:57 -0700
Subject: [PATCH 3/6] Address Feedback nr.2

---
 lld/MachO/ObjC.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index 75581f315c99e1..e0a6a498379188 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -196,10 +196,10 @@ static StringRef getReferentString(const Reloc &r) {
     return s->getStringRefAtOffset(sym->value + r.addend);
 
   if (isa<ConcatInputSection>(sym->isec())) {
-    auto &data = sym->isec()->data;
-    const char *pDataStart = reinterpret_cast<const char *>(data.data());
-    uint32_t len = strnlen(pDataStart + sym->value, data.size() - sym->value);
-    return StringRef(pDataStart + sym->value, len);
+    auto paddedData = sym->isec()->data.slice(sym->value);
+    uint32_t len = strnlen((const char *)paddedData.data(), paddedData.size());
+    auto strData = paddedData.slice(0, len);
+    return StringRef((const char *)strData.data(), strData.size());
   }
 
   llvm_unreachable("unknown reference section in getReferentString");

>From 927f1bbe63fb7216257ed5041dabb3400dd2250c Mon Sep 17 00:00:00 2001
From: Alex Borcan <alexborcan at fb.com>
Date: Fri, 3 May 2024 11:37:20 -0700
Subject: [PATCH 4/6] Address feedback nr.3

---
 lld/MachO/ObjC.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index e0a6a498379188..d630b85c81ff16 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -196,10 +196,9 @@ static StringRef getReferentString(const Reloc &r) {
     return s->getStringRefAtOffset(sym->value + r.addend);
 
   if (isa<ConcatInputSection>(sym->isec())) {
-    auto paddedData = sym->isec()->data.slice(sym->value);
-    uint32_t len = strnlen((const char *)paddedData.data(), paddedData.size());
-    auto strData = paddedData.slice(0, len);
-    return StringRef((const char *)strData.data(), strData.size());
+    auto strData = sym->isec()->data.slice(sym->value);
+    uint32_t len = strnlen((const char *)strData.data(), strData.size());
+    return StringRef((const char *)strData.data(), len);
   }
 
   llvm_unreachable("unknown reference section in getReferentString");

>From 4b38aa139de2575c0f422fb2d27ca00a72ae87d7 Mon Sep 17 00:00:00 2001
From: Alex Borcan <alexborcan at fb.com>
Date: Fri, 3 May 2024 15:32:27 -0700
Subject: [PATCH 5/6] Address feedback nr.4

---
 lld/MachO/ObjC.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index d630b85c81ff16..24f7607066d112 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -196,7 +196,7 @@ static StringRef getReferentString(const Reloc &r) {
     return s->getStringRefAtOffset(sym->value + r.addend);
 
   if (isa<ConcatInputSection>(sym->isec())) {
-    auto strData = sym->isec()->data.slice(sym->value);
+    auto strData = sym->isec()->data.slice(sym->value + r.addend);
     uint32_t len = strnlen((const char *)strData.data(), strData.size());
     return StringRef((const char *)strData.data(), len);
   }

>From c9e060de3cfb77fac2cac0e7cf00c50bd3751c85 Mon Sep 17 00:00:00 2001
From: Alex Borcan <alexborcan at fb.com>
Date: Sun, 5 May 2024 15:06:13 -0700
Subject: [PATCH 6/6] Address feedback nr.5

---
 lld/MachO/ObjC.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index 24f7607066d112..347e786648bb03 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -191,12 +191,16 @@ ObjcCategoryChecker::ObjcCategoryChecker()
 static StringRef getReferentString(const Reloc &r) {
   if (auto *isec = r.referent.dyn_cast<InputSection *>())
     return cast<CStringInputSection>(isec)->getStringRefAtOffset(r.addend);
+
   auto *sym = cast<Defined>(r.referent.get<Symbol *>());
-  if (auto *s = dyn_cast_or_null<CStringInputSection>(sym->isec()))
-    return s->getStringRefAtOffset(sym->value + r.addend);
+  auto *symIsec = sym->isec();
+  auto symOffset = sym->value + r.addend;
+
+  if (auto *s = dyn_cast_or_null<CStringInputSection>(symIsec))
+    return s->getStringRefAtOffset(symOffset);
 
-  if (isa<ConcatInputSection>(sym->isec())) {
-    auto strData = sym->isec()->data.slice(sym->value + r.addend);
+  if (isa<ConcatInputSection>(symIsec)) {
+    auto strData = symIsec->data.slice(symOffset);
     uint32_t len = strnlen((const char *)strData.data(), strData.size());
     return StringRef((const char *)strData.data(), len);
   }



More information about the llvm-commits mailing list