https://github.com/oskarwirga updated 
https://github.com/llvm/llvm-project/pull/238855

>From c6b7021c355afdcd165272975fc824d910ee1e83 Mon Sep 17 00:00:00 2001
From: Oskar Wirga <[email protected]>
Date: Wed, 4 Nov 2026 00:18:40 -0500
Subject: [PATCH] [lld-macho] Put ObjC class stubs in __objc_clsstubs

ld64 puts class stubs (_objc_msgSendClass$...) in their own section, 
__TEXT,__objc_clsstubs, between the stubs and __objc_stubs, on arm64 and 
arm64e. lld mixed them into __objc_stubs. Do as ld64 does.

The section has an explicit place in the __TEXT order, and thunk generation, 
the map file and order files treat it like __objc_stubs.

Assisted-by: Claude Code
---
 lld/MachO/ConcatOutputSection.cpp             |  20 +-
 lld/MachO/ConcatOutputSection.h               |   7 +-
 lld/MachO/Driver.cpp                          |   2 +-
 lld/MachO/InputSection.h                      |   1 +
 lld/MachO/MapFile.cpp                         |   4 +-
 lld/MachO/MarkLive.cpp                        |   2 +-
 lld/MachO/OutputSegment.cpp                   |  13 +-
 lld/MachO/SyntheticSections.cpp               |  17 +-
 lld/MachO/SyntheticSections.h                 |  10 +-
 lld/MachO/Writer.cpp                          |  22 +-
 lld/test/MachO/arm64-objc-class-stubs-dead.s  |   4 +-
 lld/test/MachO/arm64-objc-class-stubs-weak.s  |   8 +-
 lld/test/MachO/arm64-objc-class-stubs.s       | 189 +++++++++++-------
 lld/test/MachO/arm64-thunk-objc-class-stubs.s |  58 ++++++
 lld/test/MachO/arm64-thunk-stubs.s            |  12 ++
 lld/test/MachO/arm64-thunks.s                 |  12 ++
 lld/test/MachO/objc-stubs-order-file.s        |  42 ++--
 lld/test/MachO/objc-stubs-undefined-msgsend.s |  17 ++
 18 files changed, 316 insertions(+), 124 deletions(-)
 create mode 100644 lld/test/MachO/arm64-thunk-objc-class-stubs.s

diff --git a/lld/MachO/ConcatOutputSection.cpp 
b/lld/MachO/ConcatOutputSection.cpp
index 39cb2a84710488..641e8a4bd38098 100644
--- a/lld/MachO/ConcatOutputSection.cpp
+++ b/lld/MachO/ConcatOutputSection.cpp
@@ -82,9 +82,10 @@ bool TextOutputSection::needsThunks() const {
   // Calculate the total size of all branch target sections
   uint64_t branchTargetsSize = in.stubs->getSize();
 
-  // Add the size of __objc_stubs section if it exists
-  if (in.objcStubs && in.objcStubs->isNeeded())
-    branchTargetsSize += in.objcStubs->getSize();
+  // Add the sizes of the ObjC stubs sections if they exist
+  for (const ObjCStubsSection *osec : {in.objcClassStubs, in.objcStubs})
+    if (osec && osec->isNeeded())
+      branchTargetsSize += osec->getSize();
 
   if (!needsThunks &&
       isecAddr - addr + branchTargetsSize <=
@@ -216,7 +217,8 @@ TextOutputSection::estimateStubsEndVA(unsigned 
numPotentialThunks) const {
   // Walk backwards to find the last stubs section
   while (!sections.empty()) {
     auto *osec = sections.back();
-    if (osec->isNeeded() && (osec == in.stubs || osec == in.objcStubs))
+    if (osec->isNeeded() &&
+        (osec == in.stubs || osec == in.objcClassStubs || osec == 
in.objcStubs))
       break;
     sections.consume_back();
   }
@@ -232,7 +234,8 @@ TextOutputSection::estimateStubsEndVA(unsigned 
numPotentialThunks) const {
     if (!osec->isNeeded())
       continue;
     // Check if we will emit any more sections before the last stubs section
-    if (osec != in.stubs && osec != in.stubHelper && osec != in.objcStubs)
+    if (osec != in.stubs && osec != in.stubHelper &&
+        osec != in.objcClassStubs && osec != in.objcStubs)
       return std::nullopt;
     estimatedStubsEnd =
         alignToPowerOf2(estimatedStubsEnd, osec->align) + osec->getSize();
@@ -246,7 +249,10 @@ bool TextOutputSection::isTargetStubsAndInRange(
   if (!estimatedStubsEnd.has_value())
     return false;
   auto *funcSym = cast<Symbol *>(r.referent);
-  if (!funcSym->isInStubs() && !(in.objcStubs && in.objcStubs->isNeeded() &&
+  ObjCStubsSection *objcStubs = 
ObjCStubsSection::isObjCClassStubSymbol(funcSym)
+                                    ? in.objcClassStubs
+                                    : in.objcStubs;
+  if (!funcSym->isInStubs() && !(objcStubs && objcStubs->isNeeded() &&
                                  ObjCStubsSection::isObjCStubSymbol(funcSym)))
     return false;
   if (r.getAddend())
@@ -334,7 +340,7 @@ void TextOutputSection::finalize() {
   });
   // Count distinct unresolved branch targets that still lack an in-range 
thunk.
   // We use this as an upper bound on the number of thunks we may still create
-  // when estimating where __stubs / __objc_stubs could end up.
+  // when estimating where the stubs sections could end up.
   DenseSet<ThunkKey, ThunkMapKeyInfo> branchTargets;
   for (auto [callerIsec, r] : branchesToProcess) {
     if (!findThunkInRange(*callerIsec, *r))
diff --git a/lld/MachO/ConcatOutputSection.h b/lld/MachO/ConcatOutputSection.h
index 3c690dc443df98..da32da38997996 100644
--- a/lld/MachO/ConcatOutputSection.h
+++ b/lld/MachO/ConcatOutputSection.h
@@ -117,9 +117,10 @@ class TextOutputSection : public ConcatOutputSection {
   /// can't estimate this yet. Used to determine if stub symbol targets are in
   /// range.
   std::optional<uint64_t> estimateStubsEndVA(unsigned numPotentialThunks) 
const;
-  /// \return true if the target in \p r is in __stubs or __objc_stubs and in
-  /// range from the location in \p isec. \p estimatedStubsEnd is the estimated
-  /// VA of the end of the last stubs section.
+  /// \return true if the target in \p r is in __stubs, __objc_clsstubs or
+  /// __objc_stubs and in range from the location in \p isec.
+  /// \p estimatedStubsEnd is the estimated VA of the end of the last stubs
+  /// section.
   bool isTargetStubsAndInRange(const ConcatInputSection &isec,
                                const Relocation &r,
                                std::optional<uint64_t> estimatedStubsEnd) 
const;
diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 525c86d4fc141a..e39a1f9d1c28d6 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -1656,7 +1656,7 @@ static void prepareObjCStubs() {
     if (needsEarlyUndefinedClassRef)
       classSym = symtab->addUndefined(names->classSymbolName, /*file=*/nullptr,
                                       /*isWeakRef=*/false);
-    in.objcStubs->recordClassSymbol(sym, classSym);
+    in.objcClassStubs->recordClassSymbol(sym, classSym);
   }
 
   if (data.empty())
diff --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index 5a58bdc099ede4..837cba361a4dc0 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -355,6 +355,7 @@ constexpr const char moduleTermFunc[] = "__mod_term_func";
 constexpr const char nonLazySymbolPtr[] = "__nl_symbol_ptr";
 constexpr const char objcCatList[] = "__objc_catlist";
 constexpr const char objcClassList[] = "__objc_classlist";
+constexpr const char objcClassStubs[] = "__objc_clsstubs";
 constexpr const char objcMethList[] = "__objc_methlist";
 constexpr const char objcClassRefs[] = "__objc_classrefs";
 constexpr const char objcConst[] = "__objc_const";
diff --git a/lld/MachO/MapFile.cpp b/lld/MachO/MapFile.cpp
index 93d0e87a402a18..87e99045108c54 100644
--- a/lld/MachO/MapFile.cpp
+++ b/lld/MachO/MapFile.cpp
@@ -141,7 +141,7 @@ static void printStubsEntries(
                  sym->getName().str().data());
 }
 
-// For printing the contents of the __objc_stubs section.
+// For printing the contents of the __objc_stubs and __objc_clsstubs sections.
 static void printObjCStubsEntries(raw_fd_ostream &os,
                                   const ObjCStubsSection *osec) {
   for (const Defined *sym : osec->getSymbols())
@@ -264,6 +264,8 @@ void macho::writeMapFile() {
                      osec->addr, osec->getSize());
       } else if (osec == in.stubs) {
         printStubsEntries(os, readerToFileOrdinal, osec, target->stubSize);
+      } else if (osec == in.objcClassStubs) {
+        printObjCStubsEntries(os, in.objcClassStubs);
       } else if (osec == in.objcStubs) {
         printObjCStubsEntries(os, in.objcStubs);
       } else if (osec == in.lazyPointers) {
diff --git a/lld/MachO/MarkLive.cpp b/lld/MachO/MarkLive.cpp
index 4f781b54ccd80d..5364b101f2e6f2 100644
--- a/lld/MachO/MarkLive.cpp
+++ b/lld/MachO/MarkLive.cpp
@@ -109,7 +109,7 @@ void MarkLiveImpl<RecordWhyLive>::addSym(
     if (!config->whyLive.empty() && config->whyLive.match(s->getName()))
       printWhyLive(s, prev);
   if (ObjCStubsSection::isObjCClassStubSymbol(s))
-    if (auto *classSym = in.objcStubs->lookupClassSymbol(s))
+    if (auto *classSym = in.objcClassStubs->lookupClassSymbol(s))
       addSym(classSym, prev);
   if (auto *d = dyn_cast<Defined>(s)) {
     if (d->isec())
diff --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp
index 540571722e586e..ac52c516cf6b69 100644
--- a/lld/MachO/OutputSegment.cpp
+++ b/lld/MachO/OutputSegment.cpp
@@ -96,12 +96,12 @@ static int sectionOrder(OutputSection *osec) {
   // Sections are uniquely identified by their segment + section name.
   if (segname == segment_names::text) {
     if (osec->name == section_names::header)
-      return -8;
+      return -9;
     // `__text` needs to precede the other code sections since its
     // expected to be the largest. This means in effect that it will
     // be the section that determines whether we need thunks or not.
     if (osec->name == section_names::text)
-      return -7;
+      return -8;
 
     // Prioritize specific section ordering based on our knowledge. This 
ensures
     // that certain sections are placed in a particular order, even if they
@@ -109,10 +109,11 @@ static int sectionOrder(OutputSection *osec) {
     // precedence over the general code section ordering.
     int knownPriority =
         StringSwitch<int>(osec->name)
-            .Case(section_names::stubs, -5)
+            .Case(section_names::stubs, -6)
             // needsThunks() assumes that the stubs follow __text.
-            .Case(section_names::authStubs, -4)
-            .Case(section_names::stubHelper, -3)
+            .Case(section_names::authStubs, -5)
+            .Case(section_names::stubHelper, -4)
+            .Case(section_names::objcClassStubs, -3)
             .Case(section_names::objcStubs, -2)
             .Case(section_names::initOffsets, -1)
             .Case(section_names::unwindInfo,
@@ -126,7 +127,7 @@ static int sectionOrder(OutputSection *osec) {
     // Ensure all code sections are contiguous with `__text` for thunk
     // calculations.
     if (sections::isCodeSection(osec->name, segment_names::text, osec->flags)) 
{
-      return -6;
+      return -7;
     }
 
     return osec->inputOrder;
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index ac16725752603c..31c0b4a43a9987 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -1025,8 +1025,8 @@ ConcatInputSection 
*ObjCSelRefsHelper::getSelRef(StringRef methname) {
   return it->second;
 }
 
-ObjCStubsSection::ObjCStubsSection()
-    : SyntheticSection(segment_names::text, section_names::objcStubs) {
+ObjCStubsSection::ObjCStubsSection(const char *name)
+    : SyntheticSection(segment_names::text, name) {
   flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
   align = config->objcStubsMode == ObjCStubsMode::fast
               ? target->objcStubsFastAlignment
@@ -1161,8 +1161,10 @@ void ObjCStubsSection::addEntry(Symbol *sym) {
 }
 
 void ObjCStubsSection::setUp() {
-  objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr,
-                                     /*isWeakRef=*/false);
+  Symbol *objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr,
+                                             /*isWeakRef=*/false);
+  in.objcClassStubs->objcMsgSend = objcMsgSend;
+  in.objcStubs->objcMsgSend = objcMsgSend;
   if (auto *undefined = dyn_cast<Undefined>(objcMsgSend)) {
     treatUndefinedSymbol(*undefined,
                          "lazy binding (normally in libobjc.dylib)");
@@ -1219,12 +1221,11 @@ void ObjCStubsSection::writeTo(uint8_t *buf) const {
     if (isObjCClassStubSymbol(sym)) {
       Symbol *classSym = classSymbols.lookup(sym);
       assert(classSym != nullptr && "no class symbol for objc class stub");
-      target->writeObjCMsgSendClassStub(buf + stubOffset, sym,
-                                        in.objcStubs->addr, stubOffset,
+      target->writeObjCMsgSendClassStub(buf + stubOffset, sym, addr, 
stubOffset,
                                         selrefAddr, classSym, objcMsgSend);
     } else {
-      target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,
-                                   stubOffset, selrefAddr, objcMsgSend);
+      target->writeObjCMsgSendStub(buf + stubOffset, sym, addr, stubOffset,
+                                   selrefAddr, objcMsgSend);
     }
   }
 }
diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index 3521e9fd9bf2f0..a5d2d22df46e14 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -344,16 +344,19 @@ class ObjCSelRefsHelper {
 // '_objc_msgSend$foo', which are then synthesized by the linker. The stubs
 // load the particular selector 'foo' from __objc_selrefs, setting it to the
 // first argument of the objc_msgSend call, and then jumps to objc_msgSend. The
-// actual stub contents are mirrored from ld64.
+// actual stub contents are mirrored from ld64, and so is the layout: class
+// stubs, which also load the receiver class, go in __objc_clsstubs and the
+// others in __objc_stubs.
 class ObjCStubsSection final : public SyntheticSection {
 public:
-  ObjCStubsSection();
+  explicit ObjCStubsSection(const char *name);
   void addEntry(Symbol *sym);
   uint64_t getSize() const override;
   bool isNeeded() const override { return !symbols.empty(); }
   void finalize() override { isec->isFinal = true; }
   void writeTo(uint8_t *buf) const override;
-  void setUp();
+  // Sets up _objc_msgSend, which both sections branch to.
+  static void setUp();
 
   static constexpr llvm::StringLiteral objcMsgSendStubPrefix = 
"_objc_msgSend$";
   static constexpr llvm::StringLiteral objcMsgSendClassStubPrefix =
@@ -894,6 +897,7 @@ struct InStruct {
   LazyPointerSection *lazyPointers = nullptr;
   StubsSection *stubs = nullptr;
   StubHelperSection *stubHelper = nullptr;
+  ObjCStubsSection *objcClassStubs = nullptr;
   ObjCStubsSection *objcStubs = nullptr;
   UnwindInfoSection *unwindInfo = nullptr;
   ObjCImageInfoSection *objCImageInfo = nullptr;
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 91f41f7b096079..eb95694b7dc595 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -791,7 +791,10 @@ void Writer::scanSymbols() {
           if (!sym->isLive())
             continue;
         }
-        in.objcStubs->addEntry(sym);
+        if (ObjCStubsSection::isObjCClassStubSymbol(sym))
+          in.objcClassStubs->addEntry(sym);
+        else
+          in.objcStubs->addEntry(sym);
       }
     }
   }
@@ -984,13 +987,14 @@ template <class LP> void Writer::createLoadCommands() {
                               : 0));
 }
 
-// __objc_stubs is synthetic, so the input section sorting in
-// sortSegmentsAndSections() does not reach its entries. Order each stub by the
-// priority of the earliest-laid-out section that calls it, which keeps the
+// __objc_clsstubs and __objc_stubs are synthetic, so the input section sorting
+// in sortSegmentsAndSections() does not reach their entries. Order each stub 
by
+// the priority of the earliest-laid-out section that calls it, which keeps the
 // stubs reached during startup together.
 static void orderObjCStubsByCallerPriority(
     const DenseMap<const InputSection *, int> &priorities) {
-  if (priorities.empty() || !in.objcStubs->isNeeded())
+  if (priorities.empty() ||
+      (!in.objcClassStubs->isNeeded() && !in.objcStubs->isNeeded()))
     return;
 
   // ICF may make the prioritized section differ from the section whose
@@ -1020,6 +1024,7 @@ static void orderObjCStubsByCallerPriority(
   if (stubPriority.empty())
     return;
 
+  in.objcClassStubs->sortSymbols(stubPriority);
   in.objcStubs->sortSymbols(stubPriority);
 }
 
@@ -1385,8 +1390,8 @@ template <class LP> void Writer::run() {
   // these two scan* methods. I.e. from this point onward, for all live
   // InputSections, we should have `isec->canonical() == isec`.
   scanSymbols();
-  if (in.objcStubs->isNeeded())
-    in.objcStubs->setUp();
+  if (in.objcClassStubs->isNeeded() || in.objcStubs->isNeeded())
+    ObjCStubsSection::setUp();
   if (in.objcMethList->isNeeded())
     in.objcMethList->setUp();
   scanRelocations();
@@ -1458,7 +1463,8 @@ void macho::createSyntheticSections() {
   if (config->arch() == AK_arm64e)
     in.authGot = make<AuthGotSection>();
   in.stubs = make<StubsSection>();
-  in.objcStubs = make<ObjCStubsSection>();
+  in.objcClassStubs = make<ObjCStubsSection>(section_names::objcClassStubs);
+  in.objcStubs = make<ObjCStubsSection>(section_names::objcStubs);
   in.unwindInfo = makeUnwindInfoSection();
   in.objCImageInfo = make<ObjCImageInfoSection>();
   in.initOffsets = make<InitOffsetsSection>();
diff --git a/lld/test/MachO/arm64-objc-class-stubs-dead.s 
b/lld/test/MachO/arm64-objc-class-stubs-dead.s
index f0f4d6322ea09d..46d26fe2889c9b 100644
--- a/lld/test/MachO/arm64-objc-class-stubs-dead.s
+++ b/lld/test/MachO/arm64-objc-class-stubs-dead.s
@@ -14,12 +14,12 @@
 # RUN: %lld -arch arm64 -lSystem -dead_strip -dead_strip_dylibs \
 # RUN:   -o %t.out %t/main.o %t/libdead.dylib -U _objc_msgSend \
 # RUN:   -objc_stubs_small
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t.out | FileCheck %s --check-prefix=STUBS
 # RUN: llvm-nm %t.out | FileCheck %s --check-prefix=SYMS
 # RUN: llvm-otool -L %t.out | FileCheck %s --check-prefix=LOAD
 
-# STUBS:      Contents of (__TEXT,__objc_stubs) section
+# STUBS:      Contents of (__TEXT,__objc_clsstubs) section
 # STUBS-NEXT: _objc_msgSendClass$live$_OBJC_CLASS_$_LiveClass:
 # STUBS-NOT:  _objc_msgSendClass$dead$_OBJC_CLASS_$_DeadClass:
 # STUBS-NOT:  _objc_msgSendClass$missing$_OBJC_CLASS_$_NeverDefined:
diff --git a/lld/test/MachO/arm64-objc-class-stubs-weak.s 
b/lld/test/MachO/arm64-objc-class-stubs-weak.s
index 3c74a6174c2506..36cbe7cbcd0ea9 100644
--- a/lld/test/MachO/arm64-objc-class-stubs-weak.s
+++ b/lld/test/MachO/arm64-objc-class-stubs-weak.s
@@ -21,24 +21,24 @@
 # RUN:   -o %t/weak-thin-fast.out %t/weak.thin.bc %t/libFuture.dylib
 # RUN: llvm-nm -m %t/weak-thin-fast.out | FileCheck %s --check-prefix=WEAK-NM
 # RUN: llvm-objdump --macho --chained-fixups %t/weak-thin-fast.out | FileCheck 
%s --check-prefix=WEAK-CHAIN
-# RUN: llvm-objdump --macho --no-show-raw-insn --section=__TEXT,__objc_stubs 
%t/weak-thin-fast.out | FileCheck %s --check-prefix=STUB
+# RUN: llvm-objdump --macho --no-show-raw-insn 
--section=__TEXT,__objc_clsstubs %t/weak-thin-fast.out | FileCheck %s 
--check-prefix=STUB
 # RUN: %lld -arch arm64 -dead_strip -fixup_chains -objc_stubs_small \
 # RUN:   -o %t/weak-thin-small.out %t/weak.thin.bc %t/libFuture.dylib
 # RUN: llvm-nm -m %t/weak-thin-small.out | FileCheck %s --check-prefix=WEAK-NM
 # RUN: llvm-objdump --macho --chained-fixups %t/weak-thin-small.out | 
FileCheck %s --check-prefix=WEAK-CHAIN
-# RUN: llvm-objdump --macho --no-show-raw-insn --section=__TEXT,__objc_stubs 
%t/weak-thin-small.out | FileCheck %s --check-prefix=STUB
+# RUN: llvm-objdump --macho --no-show-raw-insn 
--section=__TEXT,__objc_clsstubs %t/weak-thin-small.out | FileCheck %s 
--check-prefix=STUB
 
 # Check both stub modes and serialized chained import flags with dead 
stripping.
 # RUN: %lld -arch arm64 -dead_strip -dead_strip_dylibs -fixup_chains \
 # RUN:   -objc_stubs_fast -o %t/weak-fast.out %t/weak.o %t/libFuture.dylib
 # RUN: llvm-nm -m %t/weak-fast.out | FileCheck %s --check-prefix=WEAK-NM
 # RUN: llvm-objdump --macho --chained-fixups %t/weak-fast.out | FileCheck %s 
--check-prefix=WEAK-CHAIN
-# RUN: llvm-objdump --macho --no-show-raw-insn --section=__TEXT,__objc_stubs 
%t/weak-fast.out | FileCheck %s --check-prefix=STUB
+# RUN: llvm-objdump --macho --no-show-raw-insn 
--section=__TEXT,__objc_clsstubs %t/weak-fast.out | FileCheck %s 
--check-prefix=STUB
 # RUN: %lld -arch arm64 -dead_strip -dead_strip_dylibs -fixup_chains \
 # RUN:   -objc_stubs_small -o %t/weak-small.out %t/weak.o %t/libFuture.dylib
 # RUN: llvm-nm -m %t/weak-small.out | FileCheck %s --check-prefix=WEAK-NM
 # RUN: llvm-objdump --macho --chained-fixups %t/weak-small.out | FileCheck %s 
--check-prefix=WEAK-CHAIN
-# RUN: llvm-objdump --macho --no-show-raw-insn --section=__TEXT,__objc_stubs 
%t/weak-small.out | FileCheck %s --check-prefix=STUB
+# RUN: llvm-objdump --macho --no-show-raw-insn 
--section=__TEXT,__objc_clsstubs %t/weak-small.out | FileCheck %s 
--check-prefix=STUB
 
 # The GOT and ordinary classref must both keep weak_import in legacy bind info.
 # RUN: %lld -arch arm64 -no_fixup_chains -objc_stubs_fast \
diff --git a/lld/test/MachO/arm64-objc-class-stubs.s 
b/lld/test/MachO/arm64-objc-class-stubs.s
index a6ac35d50e33d4..52f2a94ae0beb1 100644
--- a/lld/test/MachO/arm64-objc-class-stubs.s
+++ b/lld/test/MachO/arm64-objc-class-stubs.s
@@ -13,13 +13,30 @@
 # RUN: %lld -arch arm64 -dylib -install_name 
@executable_path/libexternal.dylib \
 # RUN:   -o %t/libexternal.dylib %t/external.o
 # RUN: %lld -arch arm64 -lSystem -o %t/fast.out %t/main.o \
-# RUN:   %t/libexternal.dylib -objc_stubs_fast
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --syms \
-# RUN:   --macho %t/fast.out | FileCheck %s --check-prefix=FAST
+# RUN:   %t/libexternal.dylib -objc_stubs_fast -map %t/fast.map
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --syms --macho %t/fast.out \
+# RUN:   | FileCheck %s --check-prefix=FAST
+# RUN: FileCheck %s --check-prefix=MAP < %t/fast.map
 # RUN: %lld -arch arm64 -lSystem -o %t/small.out %t/main.o \
 # RUN:   %t/libexternal.dylib -objc_stubs_small
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --syms \
-# RUN:   --macho %t/small.out | FileCheck %s --check-prefix=SMALL
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --syms --macho %t/small.out \
+# RUN:   | FileCheck %s --check-prefix=SMALL
+
+# Class stubs have their own section, after the stubs and before the selector
+# stubs, as in ld64.
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/order.s -o 
%t/order.o
+# RUN: %lld -arch arm64 -lSystem -fixup_chains -U _extern_func -U 
_objc_msgSend \
+# RUN:   -o %t/order.out %t/order.o
+# RUN: llvm-objdump --section-headers %t/order.out \
+# RUN:   | FileCheck %s --check-prefix=ORDER -DSTUBS=__stubs
+# RUN: llvm-mc -filetype=obj -triple=arm64e-apple-darwin %t/order.s \
+# RUN:   -o %t/order-arm64e.o
+# RUN: %lld -arch arm64e -lSystem -U _extern_func -U _objc_msgSend \
+# RUN:   -o %t/order-arm64e.out %t/order-arm64e.o
+# RUN: llvm-objdump --section-headers %t/order-arm64e.out \
+# RUN:   | FileCheck %s --check-prefix=ORDER -DSTUBS=__auth_stubs
 
 # Check arm64e. Fast stubs reach _objc_msgSend through __auth_got and braa;
 # small stubs branch to it or to its stub, as on arm64.
@@ -29,18 +46,20 @@
 # RUN:   -o %t/libexternal-arm64e.dylib %t/external-arm64e.o
 # RUN: %lld -arch arm64e -lSystem -o %t/fast-arm64e.out %t/main-arm64e.o \
 # RUN:   %t/libexternal-arm64e.dylib -objc_stubs_fast
-# RUN: llvm-objdump --no-show-raw-insn --section=__objc_stubs --syms \
-# RUN:   --section=__auth_got --section-headers --macho %t/fast-arm64e.out \
+# RUN: llvm-objdump --no-show-raw-insn --section=__objc_clsstubs \
+# RUN:   --section=__objc_stubs --syms --section=__auth_got --section-headers \
+# RUN:   --macho %t/fast-arm64e.out \
 # RUN:   | FileCheck %s --check-prefix=FAST-ARM64E
 # RUN: llvm-objdump --macho --dyld-info %t/fast-arm64e.out \
 # RUN:   | FileCheck %s --check-prefix=FAST-ARM64E-GOT
 # RUN: %lld -arch arm64e -lSystem -o %t/small-arm64e.out %t/main-arm64e.o \
 # RUN:   %t/libexternal-arm64e.dylib -objc_stubs_small
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --syms \
-# RUN:   --macho %t/small-arm64e.out | FileCheck %s --check-prefix=SMALL-ARM64E
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --syms --macho %t/small-arm64e.out \
+# RUN:   | FileCheck %s --check-prefix=SMALL-ARM64E
 # RUN: %lld -arch arm64e -lSystem -interposable -o %t/interposable-arm64e.out \
 # RUN:   %t/main-arm64e.o %t/libexternal-arm64e.dylib -objc_stubs_fast
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/interposable-arm64e.out \
 # RUN:   | FileCheck %s --check-prefixes=INTERPOSABLE,INTERPOSABLE-ARM64E
 
@@ -49,8 +68,9 @@
 # RUN:   -o %t/got-load-arm64e.o
 # RUN: %lld -arch arm64e -lSystem -o %t/got-load-arm64e.out 
%t/got-load-arm64e.o \
 # RUN:   %t/libexternal-arm64e.dylib -objc_stubs_fast -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__objc_stubs \
-# RUN:   --section=__auth_got --section-headers --macho %t/got-load-arm64e.out 
\
+# RUN: llvm-objdump --no-show-raw-insn --section=__objc_clsstubs \
+# RUN:   --section=__objc_stubs --section=__auth_got --section-headers \
+# RUN:   --macho %t/got-load-arm64e.out \
 # RUN:   | FileCheck %s --check-prefix=GOT-LOAD-ARM64E
 # RUN: llvm-objdump --macho --dyld-info %t/got-load-arm64e.out \
 # RUN:   | FileCheck %s --check-prefix=GOT-LOAD-ARM64E-GOT
@@ -61,7 +81,7 @@
 # RUN: %lld -arch arm64e -lSystem -o %t/dynamic-small-arm64e.out \
 # RUN:   %t/dynamic-arm64e.o -objc_stubs_small \
 # RUN:   -U '_OBJC_CLASS_$_DynamicClass' -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__objc_clsstubs \
 # RUN:   --section=__auth_stubs --section-headers --macho \
 # RUN:   %t/dynamic-small-arm64e.out | FileCheck %s 
--check-prefix=DYNAMIC-SMALL-ARM64E
 
@@ -75,13 +95,13 @@
 # RUN: llvm-ar rcs %t/libarchive.a %t/archive-class.o %t/archive-nested.o
 # RUN: %lld -arch arm64 -lSystem -o %t/archive.out %t/archive-main.o \
 # RUN:   %t/libarchive.a -objc_stubs_fast -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/archive.out | FileCheck %s --check-prefix=ARCHIVE
 # RUN: llvm-nm %t/archive.out | FileCheck %s --check-prefix=ARCHIVE-SYMS
 # RUN: %lld -arch arm64 -lSystem -o %t/start-lib.out %t/archive-main.o \
 # RUN:   --start-lib %t/archive-class.o %t/archive-nested.o --end-lib \
 # RUN:   -objc_stubs_fast -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/start-lib.out | FileCheck %s --check-prefix=ARCHIVE
 # RUN: llvm-nm %t/start-lib.out | FileCheck %s --check-prefix=ARCHIVE-SYMS
 
@@ -93,7 +113,7 @@
 # RUN: llvm-ar rcs %t/libautolinka.a %t/autolink-dep-a.o
 # RUN: %lld -arch arm64 -lSystem -o %t/autolink-a.out \
 # RUN:   %t/autolink-main-a.o -L%t -objc_stubs_fast -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/autolink-a.out | FileCheck %s --check-prefix=AUTOLINK-A
 
 # Check autolink extraction when the class is already known.
@@ -104,7 +124,7 @@
 # RUN: llvm-ar rcs %t/libautolinkb.a %t/autolink-dep-b.o
 # RUN: %lld -arch arm64 -lSystem -dead_strip -o %t/autolink-b.out \
 # RUN:   %t/autolink-main-b.o -L%t -objc_stubs_fast -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/autolink-b.out | FileCheck %s --check-prefix=AUTOLINK-B
 # RUN: llvm-nm %t/autolink-b.out | FileCheck %s --check-prefix=AUTOLINK-B-SYMS
 
@@ -113,17 +133,17 @@
 # RUN:   -o %t/dynamic.o
 # RUN: %lld -arch arm64 -lSystem -o %t/dynamic.out %t/dynamic.o \
 # RUN:   -objc_stubs_fast -U '_OBJC_CLASS_$_DynamicClass' -U _objc_msgSend
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/dynamic.out | FileCheck %s --check-prefix=DYNAMIC
 # RUN: %lld -arch arm64 -lSystem -o %t/dynamic-lookup.out %t/dynamic.o \
 # RUN:   -objc_stubs_fast -undefined dynamic_lookup
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/dynamic-lookup.out | FileCheck %s --check-prefix=DYNAMIC
 
 # Check that a locally defined class that needs binding is loaded through the 
GOT.
 # RUN: %lld -arch arm64 -lSystem -interposable -o %t/interposable.out \
 # RUN:   %t/main.o %t/libexternal.dylib -objc_stubs_fast
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs \
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
 # RUN:   --macho %t/interposable.out | FileCheck %s --check-prefix=INTERPOSABLE
 
 # Check diagnostics for missing class symbols.
@@ -138,17 +158,8 @@
 # RUN: not %lld -arch arm64 -lSystem -o /dev/null %t/malformed.o \
 # RUN:   -objc_stubs_fast 2>&1 | FileCheck %s --check-prefix=MALFORMED
 
-# FAST:      Contents of (__TEXT,__objc_stubs) section
-# FAST:      _objc_msgSend$instance:
-# FAST-NEXT: adrp    x1,
-# FAST-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
-# FAST-NEXT: adrp    x16,
-# FAST-NEXT: ldr     x16,
-# FAST-NEXT: br      x16
-# FAST-NEXT: brk     #0x1
-# FAST-NEXT: brk     #0x1
-# FAST-NEXT: brk     #0x1
-# FAST-NEXT: _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
+# FAST:      Contents of (__TEXT,__objc_clsstubs) section
+# FAST:      _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
 # FAST-NEXT: adrp    x0,
 # FAST-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_ExternalClass
 # FAST-NEXT: adrp    x1,
@@ -166,15 +177,34 @@
 # FAST-NEXT: ldr     x16,
 # FAST-NEXT: br      x16
 # FAST-NEXT: brk     #0x1
+# FAST:      Contents of (__TEXT,__objc_stubs) section
+# FAST:      _objc_msgSend$instance:
+# FAST-NEXT: adrp    x1,
+# FAST-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
+# FAST-NEXT: adrp    x16,
+# FAST-NEXT: ldr     x16,
+# FAST-NEXT: br      x16
+# FAST-NEXT: brk     #0x1
+# FAST-NEXT: brk     #0x1
+# FAST-NEXT: brk     #0x1
 # FAST-LABEL: SYMBOL TABLE:
 # FAST-DAG: {{0*}}[[#%x,FAST_LOCAL_CLASS_PAGE+FAST_LOCAL_CLASS_OFF]] g     O 
__DATA,__data _OBJC_CLASS_$_LocalClass
 
-# SMALL:      Contents of (__TEXT,__objc_stubs) section
-# SMALL:      _objc_msgSend$instance:
-# SMALL-NEXT: adrp    x1,
-# SMALL-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
-# SMALL-NEXT: b       _objc_msgSend
-# SMALL-NEXT: _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
+# MAP-LABEL: # Sections:
+# MAP:       __TEXT __objc_clsstubs
+# MAP-NEXT:  __TEXT __objc_stubs
+# MAP-LABEL: # Symbols:
+# MAP:       0x{{[0-9A-F]+}} 0x00000020 [  0] 
_objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass
+# MAP-NEXT:  0x{{[0-9A-F]+}} 0x00000020 [  0] 
_objc_msgSendClass$local$_OBJC_CLASS_$_LocalClass
+# MAP-NEXT:  0x{{[0-9A-F]+}} 0x00000020 [  0] _objc_msgSend$instance
+
+# ORDER:      __text
+# ORDER-NEXT: [[STUBS]]
+# ORDER-NEXT: __objc_clsstubs
+# ORDER-NEXT: __objc_stubs
+
+# SMALL:      Contents of (__TEXT,__objc_clsstubs) section
+# SMALL:      _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
 # SMALL-NEXT: adrp    x0,
 # SMALL-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_ExternalClass
 # SMALL-NEXT: adrp    x1,
@@ -186,23 +216,19 @@
 # SMALL-NEXT: adrp    x1,
 # SMALL-NEXT: ldr     x1, {{.*}} ; Objc selector ref: local
 # SMALL-NEXT: b       _objc_msgSend
+# SMALL:      Contents of (__TEXT,__objc_stubs) section
+# SMALL:      _objc_msgSend$instance:
+# SMALL-NEXT: adrp    x1,
+# SMALL-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
+# SMALL-NEXT: b       _objc_msgSend
 # SMALL-LABEL: SYMBOL TABLE:
 # SMALL-DAG: {{0*}}[[#%x,SMALL_LOCAL_CLASS_PAGE+SMALL_LOCAL_CLASS_OFF]] g     
O __DATA,__data _OBJC_CLASS_$_LocalClass
 
 # Each arm64e tail is pinned to the address of __auth_got: __auth_got and __got
 # can share a page, so a stub that read the raw __got would otherwise match.
 # FAST-ARM64E:      __auth_got {{[0-9a-f]+}} {{0*}}[[#%x,FAST_E_AUTH_GOT:]] 
DATA
-# FAST-ARM64E:      Contents of (__TEXT,__objc_stubs) section
-# FAST-ARM64E:      _objc_msgSend$instance:
-# FAST-ARM64E-NEXT: adrp    x1,
-# FAST-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
-# FAST-ARM64E-NEXT: adrp    x17, [[#]] ; 0x[[#%x,FAST_E_INSTANCE_PAGE:]]
-# FAST-ARM64E-NEXT: add     x17, x17, 
#0x[[#%x,FAST_E_AUTH_GOT-FAST_E_INSTANCE_PAGE]]
-# FAST-ARM64E-NEXT: ldr     x16, [x17]
-# FAST-ARM64E-NEXT: braa    x16, x17
-# FAST-ARM64E-NEXT: brk     #0x1
-# FAST-ARM64E-NEXT: brk     #0x1
-# FAST-ARM64E-NEXT: _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
+# FAST-ARM64E:      Contents of (__TEXT,__objc_clsstubs) section
+# FAST-ARM64E:      _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
 # FAST-ARM64E-NEXT: adrp    x0,
 # FAST-ARM64E-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_ExternalClass
 # FAST-ARM64E-NEXT: adrp    x1,
@@ -220,6 +246,16 @@
 # FAST-ARM64E-NEXT: add     x17, x17, 
#0x[[#%x,FAST_E_AUTH_GOT-FAST_E_LOCAL_PAGE]]
 # FAST-ARM64E-NEXT: ldr     x16, [x17]
 # FAST-ARM64E-NEXT: braa    x16, x17
+# FAST-ARM64E:      Contents of (__TEXT,__objc_stubs) section
+# FAST-ARM64E:      _objc_msgSend$instance:
+# FAST-ARM64E-NEXT: adrp    x1,
+# FAST-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
+# FAST-ARM64E-NEXT: adrp    x17, [[#]] ; 0x[[#%x,FAST_E_INSTANCE_PAGE:]]
+# FAST-ARM64E-NEXT: add     x17, x17, 
#0x[[#%x,FAST_E_AUTH_GOT-FAST_E_INSTANCE_PAGE]]
+# FAST-ARM64E-NEXT: ldr     x16, [x17]
+# FAST-ARM64E-NEXT: braa    x16, x17
+# FAST-ARM64E-NEXT: brk     #0x1
+# FAST-ARM64E-NEXT: brk     #0x1
 # FAST-ARM64E-LABEL: SYMBOL TABLE:
 # FAST-ARM64E-DAG: 
{{0*}}[[#%x,FAST_E_LOCAL_CLASS_PAGE+FAST_E_LOCAL_CLASS_OFF]] g     O 
__DATA,__data _OBJC_CLASS_$_LocalClass
 
@@ -227,12 +263,8 @@
 # FAST-ARM64E-GOT: __DATA_CONST __auth_got 0x{{[0-9A-Fa-f]+}} 
0x{{[0-9A-Fa-f]+}} rebase 0x0000 true IA
 # FAST-ARM64E-GOT: __DATA_CONST __got 0x{{[0-9A-Fa-f]+}} 0x{{[0-9A-Fa-f]+}} 
bind 0x0 libexternal _OBJC_CLASS_$_ExternalClass
 
-# SMALL-ARM64E:      Contents of (__TEXT,__objc_stubs) section
-# SMALL-ARM64E:      _objc_msgSend$instance:
-# SMALL-ARM64E-NEXT: adrp    x1,
-# SMALL-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
-# SMALL-ARM64E-NEXT: b       _objc_msgSend
-# SMALL-ARM64E-NEXT: _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
+# SMALL-ARM64E:      Contents of (__TEXT,__objc_clsstubs) section
+# SMALL-ARM64E:      _objc_msgSendClass$external$_OBJC_CLASS_$_ExternalClass:
 # SMALL-ARM64E-NEXT: adrp    x0,
 # SMALL-ARM64E-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_ExternalClass
 # SMALL-ARM64E-NEXT: adrp    x1,
@@ -244,12 +276,27 @@
 # SMALL-ARM64E-NEXT: adrp    x1,
 # SMALL-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: local
 # SMALL-ARM64E-NEXT: b       _objc_msgSend
+# SMALL-ARM64E:      Contents of (__TEXT,__objc_stubs) section
+# SMALL-ARM64E:      _objc_msgSend$instance:
+# SMALL-ARM64E-NEXT: adrp    x1,
+# SMALL-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: instance
+# SMALL-ARM64E-NEXT: b       _objc_msgSend
 # SMALL-ARM64E-LABEL: SYMBOL TABLE:
 # SMALL-ARM64E-DAG: 
{{0*}}[[#%x,SMALL_E_LOCAL_CLASS_PAGE+SMALL_E_LOCAL_CLASS_OFF]] g     O 
__DATA,__data _OBJC_CLASS_$_LocalClass
 
 # GOT-LOAD-ARM64E:      __auth_got {{[0-9a-f]+}} 
{{0*}}[[#%x,GOT_LOAD_AUTH_GOT:]] DATA
+# GOT-LOAD-ARM64E:      Contents of (__TEXT,__objc_clsstubs) section
+# GOT-LOAD-ARM64E:      _objc_msgSendClass$got$_OBJC_CLASS_$_ExternalClass:
+# GOT-LOAD-ARM64E-NEXT: adrp    x0,
+# GOT-LOAD-ARM64E-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_ExternalClass
+# GOT-LOAD-ARM64E-NEXT: adrp    x1,
+# GOT-LOAD-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: got
+# GOT-LOAD-ARM64E-NEXT: adrp    x17, [[#]] ; 0x[[#%x,GOT_LOAD_CLASS_PAGE:]]
+# GOT-LOAD-ARM64E-NEXT: add     x17, x17, 
#0x[[#%x,GOT_LOAD_AUTH_GOT-GOT_LOAD_CLASS_PAGE]]
+# GOT-LOAD-ARM64E-NEXT: ldr     x16, [x17]
+# GOT-LOAD-ARM64E-NEXT: braa    x16, x17
 # GOT-LOAD-ARM64E:      Contents of (__TEXT,__objc_stubs) section
-# GOT-LOAD-ARM64E-NEXT: _objc_msgSend$got:
+# GOT-LOAD-ARM64E:      _objc_msgSend$got:
 # GOT-LOAD-ARM64E-NEXT: adrp    x1,
 # GOT-LOAD-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: got
 # GOT-LOAD-ARM64E-NEXT: adrp    x17, [[#]] ; 0x[[#%x,GOT_LOAD_SEL_PAGE:]]
@@ -258,15 +305,6 @@
 # GOT-LOAD-ARM64E-NEXT: braa    x16, x17
 # GOT-LOAD-ARM64E-NEXT: brk     #0x1
 # GOT-LOAD-ARM64E-NEXT: brk     #0x1
-# GOT-LOAD-ARM64E-NEXT: _objc_msgSendClass$got$_OBJC_CLASS_$_ExternalClass:
-# GOT-LOAD-ARM64E-NEXT: adrp    x0,
-# GOT-LOAD-ARM64E-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_ExternalClass
-# GOT-LOAD-ARM64E-NEXT: adrp    x1,
-# GOT-LOAD-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: got
-# GOT-LOAD-ARM64E-NEXT: adrp    x17, [[#]] ; 0x[[#%x,GOT_LOAD_CLASS_PAGE:]]
-# GOT-LOAD-ARM64E-NEXT: add     x17, x17, 
#0x[[#%x,GOT_LOAD_AUTH_GOT-GOT_LOAD_CLASS_PAGE]]
-# GOT-LOAD-ARM64E-NEXT: ldr     x16, [x17]
-# GOT-LOAD-ARM64E-NEXT: braa    x16, x17
 
 # _objc_msgSend has an authenticated bind in __auth_got for the stubs and a
 # plain one in __got for the GOT load.
@@ -281,7 +319,7 @@
 # DYNAMIC-SMALL-ARM64E-NEXT: ldr     x1, {{.*}} ; Objc selector ref: dynamic
 # DYNAMIC-SMALL-ARM64E-NEXT: b       0x[[#%x,DYN_E_AUTH_STUBS]]
 
-# DYNAMIC:      Contents of (__TEXT,__objc_stubs) section
+# DYNAMIC:      Contents of (__TEXT,__objc_clsstubs) section
 # DYNAMIC-NEXT: _objc_msgSendClass$dynamic$_OBJC_CLASS_$_DynamicClass:
 # DYNAMIC-NEXT: adrp    x0,
 # DYNAMIC-NEXT: ldr     x0, {{.*}} ; literal pool symbol address: 
_OBJC_CLASS_$_DynamicClass
@@ -304,7 +342,7 @@
 # INTERPOSABLE-ARM64E-NEXT: ldr     x16, [x17]
 # INTERPOSABLE-ARM64E-NEXT: braa    x16, x17
 
-# ARCHIVE:      Contents of (__TEXT,__objc_stubs) section
+# ARCHIVE:      Contents of (__TEXT,__objc_clsstubs) section
 # ARCHIVE-NEXT: _objc_msgSendClass$archive$_OBJC_CLASS_$_ArchiveClass:
 # ARCHIVE-NEXT: adrp    x0,
 # ARCHIVE-NEXT: add     x0, x0,
@@ -321,7 +359,7 @@
 # ARCHIVE-SYMS: _OBJC_CLASS_$_ArchiveClass
 # ARCHIVE-SYMS: _OBJC_CLASS_$_NestedClass
 
-# AUTOLINK-A:      Contents of (__TEXT,__objc_stubs) section
+# AUTOLINK-A:      Contents of (__TEXT,__objc_clsstubs) section
 # AUTOLINK-A-NEXT: _objc_msgSendClass$auto$_OBJC_CLASS_$_AutoClass:
 # AUTOLINK-A-NEXT: adrp    x0,
 # AUTOLINK-A-NEXT: add     x0, x0,
@@ -332,7 +370,7 @@
 # AUTOLINK-A-NEXT: br      x16
 # AUTOLINK-A-NEXT: brk     #0x1
 #
-# AUTOLINK-B:      Contents of (__TEXT,__objc_stubs) section
+# AUTOLINK-B:      Contents of (__TEXT,__objc_clsstubs) section
 # AUTOLINK-B-NEXT: _objc_msgSendClass$late$_OBJC_CLASS_$_LateClass:
 # AUTOLINK-B-NEXT: adrp    x0,
 # AUTOLINK-B-NEXT: add     x0, x0,
@@ -352,6 +390,21 @@
 # MALFORMED-NOT: Objc selector ref:
 # MALFORMED-NOT: undefined symbol: _objc_msgSendClass$malformed
 
+#--- order.s
+# One call through each kind of stub.
+.globl _main
+.p2align 2
+_main:
+  bl _extern_func
+  bl _objc_msgSend$order
+  bl _objc_msgSendClass$order$_OBJC_CLASS_$_OrderClass
+  ret
+
+.data
+.globl _OBJC_CLASS_$_OrderClass
+_OBJC_CLASS_$_OrderClass:
+  .quad 0
+
 #--- main.s
 # Main fast/small case: one regular selector stub plus local and dylib class
 # stubs.
diff --git a/lld/test/MachO/arm64-thunk-objc-class-stubs.s 
b/lld/test/MachO/arm64-thunk-objc-class-stubs.s
new file mode 100644
index 00000000000000..427a5d77ae1314
--- /dev/null
+++ b/lld/test/MachO/arm64-thunk-objc-class-stubs.s
@@ -0,0 +1,58 @@
+# REQUIRES: aarch64
+
+## Thunk generation treats __objc_clsstubs like the other stubs sections when
+## it is the only one. __text is 0x7fffff0 bytes, so only the class stubs that
+## follow it put _main's calls out of range and make thunks necessary, while
+## _goo's calls stay in range and do not use them.
+
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
+# RUN: %lld -arch arm64 -o %t %t.o
+# RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn %t \
+# RUN:   | FileCheck %s --implicit-check-not=.thunk.
+
+# CHECK-LABEL: <_main>:
+# CHECK-NEXT:    bl 0x[[#%x,THUNK_A:]] 
<_objc_msgSendClass$a$_OBJC_CLASS_$_Foo.thunk.0>
+# CHECK-NEXT:    bl 0x[[#%x,THUNK_B:]] 
<_objc_msgSendClass$b$_OBJC_CLASS_$_Foo.thunk.0>
+# CHECK-NEXT:    ret
+# CHECK:       [[#THUNK_A]] <_objc_msgSendClass$a$_OBJC_CLASS_$_Foo.thunk.0>:
+# CHECK:       [[#THUNK_B]] <_objc_msgSendClass$b$_OBJC_CLASS_$_Foo.thunk.0>:
+
+# CHECK-LABEL: <_goo>:
+# CHECK-NEXT:    bl 0x[[#%x,STUB_A:]] <_objc_msgSendClass$a$_OBJC_CLASS_$_Foo>
+# CHECK-NEXT:    bl 0x[[#%x,STUB_B:]] <_objc_msgSendClass$b$_OBJC_CLASS_$_Foo>
+# CHECK-NEXT:    ret
+
+# CHECK-LABEL: Disassembly of section __TEXT,__objc_clsstubs:
+# CHECK:       [[#STUB_A]] <_objc_msgSendClass$a$_OBJC_CLASS_$_Foo>:
+# CHECK:       [[#STUB_B]] <_objc_msgSendClass$b$_OBJC_CLASS_$_Foo>:
+
+.text
+.globl _main
+.p2align 2
+_main:
+  bl _objc_msgSendClass$a$_OBJC_CLASS_$_Foo
+  bl _objc_msgSendClass$b$_OBJC_CLASS_$_Foo
+  ret
+
+## 0x7fffff0 bytes of __text in total.
+_spacer:
+.space 0x7fffff0-28
+
+.globl _goo
+.p2align 2
+_goo:
+  bl _objc_msgSendClass$a$_OBJC_CLASS_$_Foo
+  bl _objc_msgSendClass$b$_OBJC_CLASS_$_Foo
+  ret
+
+.globl _objc_msgSend
+.p2align 2
+_objc_msgSend:
+  ret
+
+.data
+.globl _OBJC_CLASS_$_Foo
+_OBJC_CLASS_$_Foo:
+  .quad 0
+
+.subsections_via_symbols
diff --git a/lld/test/MachO/arm64-thunk-stubs.s 
b/lld/test/MachO/arm64-thunk-stubs.s
index 046d3965525d06..3584596597a458 100644
--- a/lld/test/MachO/arm64-thunk-stubs.s
+++ b/lld/test/MachO/arm64-thunk-stubs.s
@@ -11,19 +11,23 @@
 # CHECK-NEXT:    bl 0x[[#%x,THUNK:]] <_extern_sym.thunk.0>
 # CHECK-NEXT:    bl 0x[[#%x,THUNK_FOO:]] <_objc_msgSend$foo.thunk.0>
 # CHECK-NEXT:    bl 0x[[#%x,THUNK_BAR:]] <_objc_msgSend$bar.thunk.0>
+# CHECK-NEXT:    bl 0x[[#%x,THUNK_BAZ:]] 
<_objc_msgSendClass$baz$_OBJC_CLASS_$_Baz.thunk.0>
 # CHECK-NEXT:    ret
 
 # CHECK-LABEL: <_foo>:
 # CHECK-NEXT:    bl 0x[[#%x,THUNK:]] <_extern_sym.thunk.0>
 # CHECK-NEXT:    bl 0x[[#%x,THUNK_FOO:]] <_objc_msgSend$foo.thunk.0>
 # CHECK-NEXT:    bl 0x[[#%x,THUNK_BAR:]] <_objc_msgSend$bar.thunk.0>
+# CHECK-NEXT:    bl 0x[[#%x,THUNK_BAZ:]] 
<_objc_msgSendClass$baz$_OBJC_CLASS_$_Baz.thunk.0>
 # CHECK-NEXT:    ret
 
 # CHECK: [[#THUNK]] <_extern_sym.thunk.0>:
 # CHECK: [[#THUNK_FOO]] <_objc_msgSend$foo.thunk.0>:
 # CHECK: [[#THUNK_BAR]] <_objc_msgSend$bar.thunk.0>:
+# CHECK: [[#THUNK_BAZ]] <_objc_msgSendClass$baz$_OBJC_CLASS_$_Baz.thunk.0>:
 
 # CHECK-LABEL: Disassembly of section __TEXT,__stubs:
+# CHECK-LABEL: Disassembly of section __TEXT,__objc_clsstubs:
 # CHECK-LABEL: Disassembly of section __TEXT,__objc_stubs:
 
 .section  __TEXT,__objc_methname,cstring_literals
@@ -37,6 +41,11 @@ lselref2:
 .quad lselref1
 .quad lselref2
 
+.data
+.globl _OBJC_CLASS_$_Baz
+_OBJC_CLASS_$_Baz:
+  .quad 0
+
 .text
 .globl _objc_msgSend
 _objc_msgSend:
@@ -50,6 +59,7 @@ _main:
   bl _extern_sym
   bl _objc_msgSend$foo
   bl _objc_msgSend$bar
+  bl _objc_msgSendClass$baz$_OBJC_CLASS_$_Baz
   ret
 
 _spacer0:
@@ -60,6 +70,7 @@ _foo:
   bl _extern_sym
   bl _objc_msgSend$foo
   bl _objc_msgSend$bar
+  bl _objc_msgSendClass$baz$_OBJC_CLASS_$_Baz
   ret
 
 _spacer1:
@@ -70,6 +81,7 @@ _goo:
   bl _extern_sym
   bl _objc_msgSend$foo
   bl _objc_msgSend$bar
+  bl _objc_msgSendClass$baz$_OBJC_CLASS_$_Baz
   ret
 
 .subsections_via_symbols
diff --git a/lld/test/MachO/arm64-thunks.s b/lld/test/MachO/arm64-thunks.s
index 9c1c9ab8d5418e..b222f853b57fe0 100644
--- a/lld/test/MachO/arm64-thunks.s
+++ b/lld/test/MachO/arm64-thunks.s
@@ -29,6 +29,7 @@
 # OBJC: __text
 # OBJC-NEXT: __stubs
 # OBJC-NEXT: __stub_helper
+# OBJC-NEXT: __objc_clsstubs
 # OBJC-NEXT: __objc_stubs
 
 # OBJC: Contents of (__DATA,__objc_selrefs) section
@@ -203,6 +204,7 @@
 # CHECK:  bl 0x[[#%x, NAN]]
 # CHECK:  bl 0x[[#%x, FOO:]] <_objc_msgSend$foo>
 # CHECK:  bl 0x[[#%x, BAR:]] <_objc_msgSend$bar>
+# CHECK:  bl 0x[[#%x, BAZ:]] <_objc_msgSendClass$baz$_OBJC_CLASS_$_Baz>
 
 # CHECK: [[#%x, FOLD_HIGH]] <_fold_func_high_addr>:
 # CHECK:  b 0x[[#%x, FOLD_LOW_THUNK_0]] <_fold_func_low_addr.thunk.0>
@@ -231,6 +233,10 @@
 
 # CHECK: [[#%x, NAN_PAGE + NAN_OFFSET]] <__stubs>:
 
+# CHECK: Disassembly of section __TEXT,__objc_clsstubs:
+
+# CHECK: [[#%x, BAZ]] <_objc_msgSendClass$baz$_OBJC_CLASS_$_Baz>:
+
 # CHECK: Disassembly of section __TEXT,__objc_stubs:
 
 # CHECK: <_objc_msgSend$bar>:
@@ -247,6 +253,11 @@ lselref2:
 .quad lselref1
 .quad lselref2
 
+.data
+.globl _OBJC_CLASS_$_Baz
+_OBJC_CLASS_$_Baz:
+  .quad 0
+
 .text
 .globl _objc_msgSend
 _objc_msgSend:
@@ -400,6 +411,7 @@ _main:
   bl ___nan
   bl _objc_msgSend$foo
   bl _objc_msgSend$bar
+  bl _objc_msgSendClass$baz$_OBJC_CLASS_$_Baz
   ret
 
 .globl _fold_func_high_addr
diff --git a/lld/test/MachO/objc-stubs-order-file.s 
b/lld/test/MachO/objc-stubs-order-file.s
index 6881bb44b282ed..4900f6644a32bc 100644
--- a/lld/test/MachO/objc-stubs-order-file.s
+++ b/lld/test/MachO/objc-stubs-order-file.s
@@ -1,8 +1,8 @@
 # REQUIRES: aarch64
 
-## __objc_stubs is synthetic, so the input section sorting does not reach its
-## entries. Check that they are instead ordered by the priority of the sections
-## that call them.
+## __objc_stubs and __objc_clsstubs are synthetic, so the input section sorting
+## does not reach their entries. Check that they are instead ordered by the
+## priority of the sections that call them.
 
 # RUN: rm -rf %t && split-file %s %t
 # RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/a.s -o %t/a.o
@@ -10,39 +10,49 @@
 ## Without an order file the stubs keep the order they were interned in.
 # RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/base.out %t/a.o \
 # RUN:   -objc_stubs_small
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
-# RUN:   %t/base.out | FileCheck %s --check-prefix=BASE
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --macho %t/base.out \
+# RUN:   | FileCheck %s --check-prefix=BASE
 
-## _hot is ordered first, so the stub it calls moves ahead of the others, which
+## _hot is ordered first, so the stubs it calls move ahead of the others, which
 ## keep their relative order.
 # RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/ordered.out %t/a.o \
 # RUN:   -objc_stubs_small -order_file %t/order -map %t/ordered.map
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
-# RUN:   %t/ordered.out | FileCheck %s --check-prefix=ORDERED
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --macho %t/ordered.out \
+# RUN:   | FileCheck %s --check-prefix=ORDERED
 # RUN: FileCheck %s --check-prefix=MAP < %t/ordered.map
 
 # RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/fast.out %t/a.o \
 # RUN:   -objc_stubs_fast -order_file %t/order
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
-# RUN:   %t/fast.out | FileCheck %s --check-prefix=ORDERED
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --macho %t/fast.out \
+# RUN:   | FileCheck %s --check-prefix=ORDERED
 
 ## Callers are recorded after ICF. _dup is identical to _hot and folds with it,
 ## so the priority has to reach the stub through the surviving section.
 # RUN: %lld -arch arm64 -e _main -U _objc_msgSend -o %t/icf.out %t/a.o \
 # RUN:   -objc_stubs_small -order_file %t/order --icf=all
 # RUN: llvm-nm --numeric-sort %t/icf.out | FileCheck %s --check-prefix=FOLDED
-# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_stubs --macho \
-# RUN:   %t/icf.out | FileCheck %s --check-prefix=ORDERED
+# RUN: llvm-objdump --no-show-raw-insn --section=__TEXT,__objc_clsstubs \
+# RUN:   --section=__TEXT,__objc_stubs --macho %t/icf.out \
+# RUN:   | FileCheck %s --check-prefix=ORDERED
 
 ## Check that _dup folded into _hot.
 # FOLDED:      [[ADDR:[0-9a-f]+]] T _dup
 # FOLDED-NEXT: [[ADDR]] T _hot
 
+# BASE:      Contents of (__TEXT,__objc_clsstubs) section
+# BASE-NEXT: _objc_msgSendClass$cold$_OBJC_CLASS_$_Foo:
+# BASE:      _objc_msgSendClass$hot$_OBJC_CLASS_$_Foo:
 # BASE:      Contents of (__TEXT,__objc_stubs) section
 # BASE-NEXT: _objc_msgSend$cold:
 # BASE:      _objc_msgSend$hot:
 # BASE:      _objc_msgSend$mild:
 
+# ORDERED:      Contents of (__TEXT,__objc_clsstubs) section
+# ORDERED-NEXT: _objc_msgSendClass$hot$_OBJC_CLASS_$_Foo:
+# ORDERED:      _objc_msgSendClass$cold$_OBJC_CLASS_$_Foo:
 # ORDERED:      Contents of (__TEXT,__objc_stubs) section
 # ORDERED-NEXT: _objc_msgSend$hot:
 # ORDERED:      _objc_msgSend$cold:
@@ -60,6 +70,7 @@
 .p2align 2
 _cold:
   bl _objc_msgSend$cold
+  bl _objc_msgSendClass$cold$_OBJC_CLASS_$_Foo
   ret
 
 .globl _mild
@@ -72,12 +83,14 @@ _mild:
 .p2align 2
 _hot:
   bl _objc_msgSend$hot
+  bl _objc_msgSendClass$hot$_OBJC_CLASS_$_Foo
   ret
 
 .globl _dup
 .p2align 2
 _dup:
   bl _objc_msgSend$hot
+  bl _objc_msgSendClass$hot$_OBJC_CLASS_$_Foo
   ret
 
 .globl _main
@@ -88,6 +101,11 @@ _main:
   bl _hot
   ret
 
+.data
+.globl _OBJC_CLASS_$_Foo
+_OBJC_CLASS_$_Foo:
+  .quad 0
+
 .subsections_via_symbols
 
 #--- order
diff --git a/lld/test/MachO/objc-stubs-undefined-msgsend.s 
b/lld/test/MachO/objc-stubs-undefined-msgsend.s
index 25096b66172fff..7ad200c6d2e2c2 100644
--- a/lld/test/MachO/objc-stubs-undefined-msgsend.s
+++ b/lld/test/MachO/objc-stubs-undefined-msgsend.s
@@ -8,13 +8,30 @@
 # RUN: not %lld -arch arm64 -o %t.out %t.o -objc_stubs_small 2>&1 | FileCheck 
%s
 # RUN: not %lld -arch arm64 -o %t.out %t.o -no_fixup_chains 2>&1 | FileCheck %s
 
+## Class stubs need it too. With both kinds of stubs it is still set up once, 
so
+## the reference is listed once.
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin --defsym CLASS=1 %s -o 
%t-class.o
+# RUN: not %lld -arch arm64 -o %t.out %t-class.o 2>&1 | FileCheck %s
+# RUN: not %lld -arch arm64 -o %t.out %t-class.o -objc_stubs_small 2>&1 | 
FileCheck %s
+
 # CHECK:      error: undefined symbol: _objc_msgSend
 # CHECK-NEXT: >>> referenced by lazy binding (normally in libobjc.dylib)
+# CHECK-NOT:  >>> referenced by
 
 .section __TEXT,__text
 .globl _main
 _main:
   bl _objc_msgSend$foo
+.ifdef CLASS
+  bl _objc_msgSendClass$foo$_OBJC_CLASS_$_Foo
+.endif
   ret
 
+.ifdef CLASS
+.data
+.globl _OBJC_CLASS_$_Foo
+_OBJC_CLASS_$_Foo:
+  .quad 0
+.endif
+
 .subsections_via_symbols

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to