diff --git a/lld/MachO/Arch/ARM.cpp b/lld/MachO/Arch/ARM.cpp --- a/lld/MachO/Arch/ARM.cpp +++ b/lld/MachO/Arch/ARM.cpp @@ -37,6 +37,11 @@ void writeStubHelperEntry(uint8_t *buf, const Symbol &, uint64_t entryAddr) const override; + void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const override; + void relaxGotLoad(uint8_t *loc, uint8_t type) const override; uint64_t getPageSize() const override { return 4 * 1024; } @@ -148,6 +153,13 @@ fatal("TODO: implement this"); } +void ARM::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const { + fatal("TODO: implement this"); +} + void ARM::relaxGotLoad(uint8_t *loc, uint8_t type) const { fatal("TODO: implement this"); } @@ -159,6 +171,8 @@ stubSize = 0 /* FIXME */; stubHelperHeaderSize = 0 /* FIXME */; stubHelperEntrySize = 0 /* FIXME */; + objcStubsFastSize = 0 /* FIXME */; + objcStubsSmallSize = 0 /* FIXME */; relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()}; } diff --git a/lld/MachO/Arch/ARM64.cpp b/lld/MachO/Arch/ARM64.cpp --- a/lld/MachO/Arch/ARM64.cpp +++ b/lld/MachO/Arch/ARM64.cpp @@ -34,6 +34,11 @@ void writeStubHelperHeader(uint8_t *buf) const override; void writeStubHelperEntry(uint8_t *buf, const Symbol &, uint64_t entryAddr) const override; + + void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const override; void populateThunk(InputSection *thunk, Symbol *funcSym) override; void applyOptimizationHints(uint8_t *, const ConcatInputSection *, ArrayRef) const override; @@ -100,6 +105,42 @@ ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA); } +static constexpr uint32_t objcStubsFastCode[] = { + 0x90000001, // adrp x1, __objc_selrefs@page + 0xf9400021, // ldr x1, [x1, @selector("foo")@pageoff] + 0x90000010, // adrp x16, _got@page + 0xf9400210, // ldr x16, [x16, _objc_msgSend@pageoff] + 0xd61f0200, // br x16 + 0xd4200020, // brk #0 + 0xd4200020, // brk #0 + 0xd4200020, // brk #0 +}; + +void ARM64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const { + SymbolDiagnostic d = {sym, sym->getName()}; + auto *buf32 = reinterpret_cast(buf); + + auto pcPageBits = [stubsAddr, stubOffset](int i) { + return pageBits(stubsAddr + stubOffset + i * sizeof(uint32_t)); + }; + + uint64_t selectorOffset = selectorIndex * LP64::wordSize; + encodePage21(&buf32[0], d, objcStubsFastCode[0], + pageBits(selrefsVA + selectorOffset) - pcPageBits(0)); + encodePageOff12(&buf32[1], objcStubsFastCode[1], selrefsVA + selectorOffset); + encodePage21(&buf32[2], d, objcStubsFastCode[2], + pageBits(gotAddr) - pcPageBits(2)); + encodePage21(&buf32[3], d, objcStubsFastCode[3], + msgSendIndex * LP64::wordSize); + buf32[4] = objcStubsFastCode[4]; + buf32[5] = objcStubsFastCode[5]; + buf32[6] = objcStubsFastCode[6]; + buf32[7] = objcStubsFastCode[7]; +} + // A thunk is the relaxed variation of stubCode. We don't need the // extra indirection through a lazy pointer because the target address // is known at link time. @@ -130,6 +171,9 @@ stubSize = sizeof(stubCode); thunkSize = sizeof(thunkCode); + objcStubsFastSize = sizeof(objcStubsFastCode); + objcStubsSmallSize = 0 /* FIXME */; + // Branch immediate is two's complement 26 bits, which is implicitly // multiplied by 4 (since all functions are 4-aligned: The branch range // is -4*(2**(26-1))..4*(2**(26-1) - 1). diff --git a/lld/MachO/Arch/ARM64_32.cpp b/lld/MachO/Arch/ARM64_32.cpp --- a/lld/MachO/Arch/ARM64_32.cpp +++ b/lld/MachO/Arch/ARM64_32.cpp @@ -33,6 +33,10 @@ void writeStubHelperHeader(uint8_t *buf) const override; void writeStubHelperEntry(uint8_t *buf, const Symbol &, uint64_t entryAddr) const override; + void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const override; }; } // namespace @@ -94,6 +98,14 @@ ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA); } +void ARM64_32::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, + uint64_t stubsAddr, uint64_t stubOffset, + uint64_t selrefsVA, uint64_t selectorIndex, + uint64_t gotAddr, + uint64_t msgSendIndex) const { + fatal("TODO: implement this"); +} + ARM64_32::ARM64_32() : ARM64Common(ILP32()) { cpuType = CPU_TYPE_ARM64_32; cpuSubtype = CPU_SUBTYPE_ARM64_V8; diff --git a/lld/MachO/Arch/X86_64.cpp b/lld/MachO/Arch/X86_64.cpp --- a/lld/MachO/Arch/X86_64.cpp +++ b/lld/MachO/Arch/X86_64.cpp @@ -36,6 +36,11 @@ void writeStubHelperEntry(uint8_t *buf, const Symbol &, uint64_t entryAddr) const override; + void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const override; + void relaxGotLoad(uint8_t *loc, uint8_t type) const override; uint64_t getPageSize() const override { return 4 * 1024; } @@ -170,6 +175,13 @@ sizeof(stubHelperEntry), in.stubHelper->addr); } +void X86_64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t stubOffset, uint64_t selrefsVA, + uint64_t selectorIndex, uint64_t gotAddr, + uint64_t msgSendIndex) const { + fatal("TODO: implement this"); +} + void X86_64::relaxGotLoad(uint8_t *loc, uint8_t type) const { // Convert MOVQ to LEAQ if (loc[-2] != 0x8b) diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1099,9 +1099,15 @@ inputSections.push_back(isec); } else if (auto *isec = dyn_cast(subsection.isec)) { - if (in.cStringSection->inputOrder == UnspecifiedInputOrder) - in.cStringSection->inputOrder = inputOrder++; - in.cStringSection->addInput(isec); + if (isec->getName() == section_names::objcMethname) { + if (in.objcMethnameSection->inputOrder == UnspecifiedInputOrder) + in.objcMethnameSection->inputOrder = inputOrder++; + in.objcMethnameSection->addInput(isec); + } else { + if (in.cStringSection->inputOrder == UnspecifiedInputOrder) + in.cStringSection->inputOrder = inputOrder++; + in.cStringSection->addInput(isec); + } } else if (auto *isec = dyn_cast(subsection.isec)) { if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder) @@ -1124,10 +1130,35 @@ // true. If it isn't, we simply create a non-deduplicating CStringSection. // Either way, we must unconditionally finalize it here. in.cStringSection->finalizeContents(); + in.objcMethnameSection->finalizeContents(); if (in.wordLiteralSection) in.wordLiteralSection->finalizeContents(); } +static void addSynthenticMethnames() { + std::string &data = *make(); + llvm::raw_string_ostream os(data); + const int prefixLength = strlen(ObjCStubsSection::symbolPrefix); + for (Symbol *sym : symtab->getSymbols()) + if (const auto *undefined = dyn_cast(sym)) + if (sym->getName().startswith(ObjCStubsSection::symbolPrefix)) + os << sym->getName().drop_front(prefixLength) << '\0'; + + if (data.empty()) + return; + + const auto *buf = reinterpret_cast(data.c_str()); + Section §ion = *make
(/*file=*/nullptr, segment_names::text, + section_names::objcMethname, + S_CSTRING_LITERALS, /*addr=*/0); + + auto *isec = make( + section, ArrayRef{buf, data.size()}, 1); + isec->splitIntoPieces(); + section.subsections.push_back({0, isec}); + in.objcMethnameSection->addInput(isec); +} + static void referenceStubBinder() { bool needsStubHelper = config->outputType == MH_DYLIB || config->outputType == MH_EXECUTE || @@ -1607,6 +1638,7 @@ createSyntheticSections(); createSyntheticSymbols(); + addSynthenticMethnames(); for (const auto &pair : config->aliasedSymbols) { if (const auto &sym = symtab->find(pair.first)) { diff --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h --- a/lld/MachO/InputSection.h +++ b/lld/MachO/InputSection.h @@ -322,6 +322,9 @@ constexpr const char objcClassRefs[] = "__objc_classrefs"; constexpr const char objcConst[] = "__objc_const"; constexpr const char objCImageInfo[] = "__objc_imageinfo"; +constexpr const char objcStubs[] = "__objc_stubs"; +constexpr const char objcSelrefs[] = "__objc_selrefs"; +constexpr const char objcMethname[] = "__objc_methname"; constexpr const char objcNonLazyCatList[] = "__objc_nlcatlist"; constexpr const char objcNonLazyClassList[] = "__objc_nlclslist"; constexpr const char objcProtoList[] = "__objc_protolist"; diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h --- a/lld/MachO/SyntheticSections.h +++ b/lld/MachO/SyntheticSections.h @@ -311,6 +311,25 @@ Defined *dyldPrivate = nullptr; }; +class ObjCStubsSection final : public SyntheticSection { +public: + ObjCStubsSection(); + 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(); + + static constexpr auto symbolPrefix = "_objc_msgSend$"; + +private: + std::vector symbols; + std::vector offsets; + int symbolPrefixLength = 0; + int objcMsgSendGotIndex = 0; +}; + // Note that this section may also be targeted by non-lazy bindings. In // particular, this happens when branch relocations target weak symbols. class LazyPointerSection final : public SyntheticSection { @@ -515,21 +534,32 @@ class CStringSection : public SyntheticSection { public: - CStringSection(); + CStringSection(const char *name, bool trackStringOffsets = false); void addInput(CStringInputSection *); uint64_t getSize() const override { return size; } virtual void finalizeContents(); bool isNeeded() const override { return !inputs.empty(); } void writeTo(uint8_t *buf) const override; + llvm::DenseMap + getDuplicateStringOffsetMap() const { + assert(trackOffsets); + return duplicateStringOffsetMap; + } std::vector inputs; private: uint64_t size; + +protected: + llvm::DenseMap duplicateStringOffsetMap; + bool trackOffsets = false; }; class DeduplicatedCStringSection final : public CStringSection { public: + DeduplicatedCStringSection(const char *name, bool trackStringOffsets = false) + : CStringSection(name, trackStringOffsets){}; uint64_t getSize() const override { return size; } void finalizeContents() override; void writeTo(uint8_t *buf) const override; @@ -624,6 +654,7 @@ const uint8_t *bufferStart = nullptr; MachHeaderSection *header = nullptr; CStringSection *cStringSection = nullptr; + CStringSection *objcMethnameSection = nullptr; WordLiteralSection *wordLiteralSection = nullptr; RebaseSection *rebase = nullptr; BindingSection *binding = nullptr; @@ -635,6 +666,8 @@ LazyPointerSection *lazyPointers = nullptr; StubsSection *stubs = nullptr; StubHelperSection *stubHelper = nullptr; + ObjCStubsSection *objcStubs = nullptr; + ConcatInputSection *objcSelrefs = nullptr; UnwindInfoSection *unwindInfo = nullptr; ObjCImageInfoSection *objCImageInfo = nullptr; ConcatInputSection *imageLoaderCache = nullptr; diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -716,6 +716,95 @@ dyldPrivate->used = true; } +ObjCStubsSection::ObjCStubsSection() + : SyntheticSection(segment_names::text, section_names::objcStubs) { + flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; + // TODO: align changes on setting? + // 32 is correct for arm64 + default option + // x86_64 aligns to 1 for the default option + align = 32; + symbolPrefixLength = strlen(symbolPrefix); +} + +void ObjCStubsSection::addEntry(Symbol *sym) { + std::string methname = sym->getName().drop_front(symbolPrefixLength).str(); + methname.push_back('\0'); + auto map = in.objcMethnameSection->getDuplicateStringOffsetMap(); + auto methnameOffset = map.find(methname); + assert(map.end() != methnameOffset && + "ObjC symbol stub method name should be in __objc_methnames"); + + offsets.push_back(methnameOffset->second); + Defined *newSym = replaceSymbol( + sym, sym->getName(), nullptr, isec, + /*value=*/symbols.size() * + target->objcStubsFastSize, // TODO: customizable + /*size=*/target->objcStubsFastSize, // TODO: Extra size and values + /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true, + /*includeInSymtab=*/true, /*isThumb=*/false, + /*isReferencedDynamically=*/false, /*noDeadStrip=*/false); + symbols.push_back(newSym); +} + +void ObjCStubsSection::setup() { + Symbol *objcMsgSend = + symtab->addUndefined("_objc_msgSend", nullptr, /*isWeakRef=*/false); + in.got->addEntry(objcMsgSend); + assert(objcMsgSend->isInGot()); + objcMsgSendGotIndex = objcMsgSend->gotIndex; + + size_t size = offsets.size() * target->wordSize; + uint8_t *selrefsData = bAlloc().Allocate(size); + for (size_t i = 0, n = offsets.size(); i < n; ++i) + write64le(&selrefsData[i * target->wordSize], offsets[i]); + + in.objcSelrefs = + makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs, + S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP, + ArrayRef{selrefsData, size}, + /*align=*/target->wordSize); + in.objcSelrefs->live = true; + + for (size_t i = 0, n = offsets.size(); i < n; ++i) { + // TODO: Reloc type per target + in.objcSelrefs->relocs.push_back( + {/*type=*/ARM64_RELOC_UNSIGNED, + /*pcrel=*/false, /*length=*/3, + /*offset=*/static_cast(i * target->wordSize), + /*addend=*/offsets[i] * in.objcMethnameSection->align, + /*referent=*/in.objcMethnameSection->isec}); + } + + in.objcSelrefs->parent = + ConcatOutputSection::getOrCreateForInput(in.objcSelrefs); + inputSections.push_back(in.objcSelrefs); + in.objcSelrefs->isFinal = true; +} + +uint64_t ObjCStubsSection::getSize() const { + // FIXME: based on settings + // return 12; // small + // also based on arch seems like? + return target->objcStubsFastSize * symbols.size(); +} + +void ObjCStubsSection::writeTo(uint8_t *buf) const { + uint64_t stubOffset = 0; + + assert(in.objcSelrefs->live); + assert(in.objcSelrefs->isFinal); + + for (size_t i = 0, n = symbols.size(); i < n; ++i) { + Defined *sym = symbols[i]; + + target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr, + stubOffset, in.objcSelrefs->getVA(), i, + in.got->addr, objcMsgSendGotIndex); + + stubOffset += target->objcStubsFastSize; // TODO: customizable + } +} + LazyPointerSection::LazyPointerSection() : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) { align = target->wordSize; @@ -1424,9 +1513,10 @@ remove(xarPath); } -CStringSection::CStringSection() - : SyntheticSection(segment_names::text, section_names::cString) { +CStringSection::CStringSection(const char *name, bool trackStringOffsets) + : SyntheticSection(segment_names::text, name) { flags = S_CSTRING_LITERALS; + trackOffsets = trackStringOffsets; } void CStringSection::addInput(CStringInputSection *isec) { @@ -1461,6 +1551,8 @@ isec->pieces[i].outSecOff = offset; isec->isFinal = true; StringRef string = isec->getStringRef(i); + if (trackOffsets) + duplicateStringOffsetMap.insert(std::make_pair(string, offset)); offset += string.size(); } } @@ -1536,6 +1628,9 @@ offsetInfo.outSecOff = alignTo(size, 1ULL << offsetInfo.trailingZeros); size = offsetInfo.outSecOff + s.size(); } + if (trackOffsets) + duplicateStringOffsetMap.insert( + std::make_pair(isec->getStringRef(i), offsetInfo.outSecOff)); isec->pieces[i].outSecOff = offsetInfo.outSecOff; } isec->isFinal = true; diff --git a/lld/MachO/Target.h b/lld/MachO/Target.h --- a/lld/MachO/Target.h +++ b/lld/MachO/Target.h @@ -58,6 +58,12 @@ virtual void writeStubHelperEntry(uint8_t *buf, const Symbol &, uint64_t entryAddr) const = 0; + virtual void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, + uint64_t stubsAddr, uint64_t stubOffset, + uint64_t selrefsVA, uint64_t selectorIndex, + uint64_t gotAddr, + uint64_t msgSendIndex) const = 0; + // Symbols may be referenced via either the GOT or the stubs section, // depending on the relocation type. prepareSymbolRelocation() will set up the // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those @@ -105,6 +111,8 @@ size_t stubSize; size_t stubHelperHeaderSize; size_t stubHelperEntrySize; + size_t objcStubsFastSize; // TODO: Set for all archs + size_t objcStubsSmallSize; // TODO: Set for all archs uint8_t p2WordSize; size_t wordSize; diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -695,6 +695,9 @@ continue; dysym->getFile()->refState = std::max(dysym->getFile()->refState, dysym->getRefState()); + } else if (const auto *undefined = dyn_cast(sym)) { + if (sym->getName().startswith(ObjCStubsSection::symbolPrefix)) + in.objcStubs->addEntry(sym); } } @@ -1155,6 +1158,8 @@ // 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(); scanRelocations(); // Do not proceed if there was an undefined symbol. @@ -1197,10 +1202,16 @@ void macho::createSyntheticSections() { in.header = make(); - if (config->dedupLiterals) - in.cStringSection = make(); - else - in.cStringSection = make(); + if (config->dedupLiterals) { + in.cStringSection = + make(section_names::cString); + in.objcMethnameSection = make( + section_names::objcMethname, /*trackStringOffsets=*/true); + } else { + in.cStringSection = make(section_names::cString); + in.objcMethnameSection = make(section_names::objcMethname, + /*trackStringOffsets=*/true); + } in.wordLiteralSection = config->dedupLiterals ? make() : nullptr; in.rebase = make(); @@ -1213,6 +1224,7 @@ in.lazyPointers = make(); in.stubs = make(); in.stubHelper = make(); + in.objcStubs = make(); in.unwindInfo = makeUnwindInfoSection(); in.objCImageInfo = make(); diff --git a/lld/test/MachO/arm64-objcstubs.s b/lld/test/MachO/arm64-objcstubs.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/arm64-objcstubs.s @@ -0,0 +1,50 @@ +# REQUIRES: aarch64 + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o +# RUN: %lld -arch arm64 -lSystem -o %t.out %t.o +# RUN: llvm-otool -vs __TEXT __objc_stubs %t.out | FileCheck %s + +# CHECK: Contents of (__TEXT,__objc_stubs) section + +# CHECK-NEXT: _objc_msgSend$foo: +# CHECK-NEXT: adrp x1, 8 ; 0x100008000 +# CHECK-NEXT: ldr x1, [x1, #0x10] +# CHECK-NEXT: adrp x16, 4 ; 0x100004000 +# CHECK-NEXT: ldr x16, [x16] +# CHECK-NEXT: br x16 +# CHECK-NEXT: brk #0x1 +# CHECK-NEXT: brk #0x1 +# CHECK-NEXT: brk #0x1 + +# CHECK-NEXT: _objc_msgSend$length: +# CHECK-NEXT: adrp x1, 8 ; 0x100008000 +# CHECK-NEXT: ldr x1, [x1, #0x18] +# CHECK-NEXT: adrp x16, 4 ; 0x100004000 +# CHECK-NEXT: ldr x16, [x16] +# CHECK-NEXT: br x16 +# CHECK-NEXT: brk #0x1 +# CHECK-NEXT: brk #0x1 +# CHECK-NEXT: brk #0x1 + +.section __TEXT,__objc_methname,cstring_literals +selref1: + .asciz "foo" +selref2: + .asciz "bar" + +.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip +.p2align 3 +.quad selref1 +.quad selref2 + +.text +.globl _objc_msgSend +_objc_msgSend: + ret + +.globl _main +_main: + bl _objc_msgSend$length + bl _objc_msgSend$foo + bl _objc_msgSend$foo + ret diff --git a/lld/test/MachO/methname.s b/lld/test/MachO/methname.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/methname.s @@ -0,0 +1,52 @@ +# REQUIRES: aarch64 +# RUN: rm -rf %t; split-file %s %t + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/strings.s -o %t/strings.o +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/main.s -o %t/main.o + +# RUN: %lld -arch arm64 -lSystem -o %t.out %t/strings.o %t/main.o + +# RUN: llvm-otool -vs __TEXT __cstring %t.out | FileCheck %s --check-prefix=CSTRING +# RUN: llvm-otool -vs __TEXT __objc_methname %t.out | FileCheck %s --check-prefix=METHNAME + +# RUN: %lld -arch arm64 -lSystem -o %t/duplicates %t/strings.o %t/strings.o %t/main.o --deduplicate-literals + +# RUN: llvm-otool -vs __TEXT __cstring %t/duplicates | FileCheck %s --check-prefix=CSTRING +# RUN: llvm-otool -vs __TEXT __objc_methname %t/duplicates | FileCheck %s --check-prefix=METHNAME-DEDUP + +# CSTRING: Contents of (__TEXT,__cstring) section +# CSTRING-NEXT: incstr +# CSTIRNG-EMPTY: + +# METHNAME: Contents of (__TEXT,__objc_methname) section +# METHNAME-NEXT: externalmethname +# METHNAME-NEXT: inmethname +# METHNAME-NEXT: inmethname +# METHNAME-EMPTY: + +# METHNAME-DEDUP: Contents of (__TEXT,__objc_methname) section +# METHNAME-DEDUP-NEXT: externalmethname +# METHNAME-DEDUP-NEXT: inmethname +# METHNAME-DEDUP-EMPTY: + +#--- strings.s +.cstring +.p2align 2 +_some_cstr: + .asciz "incstr" + +.section __TEXT,__objc_methname,cstring_literals +_some_methname: + .asciz "inmethname" + +#--- main.s +.text +.globl _objc_msgSend +_objc_msgSend: + ret + +.globl _main +_main: + bl _objc_msgSend$inmethname + bl _objc_msgSend$externalmethname + ret diff --git a/lld/test/MachO/selrefs.s b/lld/test/MachO/selrefs.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/selrefs.s @@ -0,0 +1,50 @@ +# REQUIRES: aarch64 +# RUN: rm -rf %t; split-file %s %t + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/existing.s -o %t/existing.o +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/main.s -o %t/main.o + +# RUN: %lld -arch arm64 -lSystem -o %t/out %t/existing.o %t/main.o +# RUN: llvm-otool -vs __DATA __objc_selrefs %t/out | FileCheck %s --check-prefix=SELREFS + +# SELREFS: Contents of (__DATA,__objc_selrefs) section +# SELREFS-NEXT: __TEXT:__objc_methname:foo +# SELREFS-NEXT: __TEXT:__objc_methname:bar +# SELREFS-NEXT: __TEXT:__objc_methname:foo +# SELREFS-NEXT: __TEXT:__objc_methname:length +# SELREFS-EMPTY: + +# RUN: %lld -arch arm64 -lSystem -o %t/out %t/existing.o %t/main.o --deduplicate-literals +# RUN: llvm-otool -vs __DATA __objc_selrefs %t/out | FileCheck %s --check-prefix=DEDUP + +# DEDUP: Contents of (__DATA,__objc_selrefs) section +# DEDUP-NEXT: __TEXT:__objc_methname:foo +# DEDUP-NEXT: __TEXT:__objc_methname:bar +# NOTE: Ideally this wouldn't exist, but while it does it needs to point to the deduplicated string +# DEDUP-NEXT: __TEXT:__objc_methname:foo +# DEDUP-NEXT: __TEXT:__objc_methname:length +# DEDUP-EMPTY: + +#--- existing.s +.section __TEXT,__objc_methname,cstring_literals +selref1: + .asciz "foo" +selref2: + .asciz "bar" + +.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip +.p2align 3 +.quad selref1 +.quad selref2 + +#--- main.s +.text +.globl _objc_msgSend +_objc_msgSend: + ret + +.globl _main +_main: + bl _objc_msgSend$length + bl _objc_msgSend$foo + ret