11cf9926bSpatrick //===- ICF.cpp ------------------------------------------------------------===//
21cf9926bSpatrick //
31cf9926bSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41cf9926bSpatrick // See https://llvm.org/LICENSE.txt for license information.
51cf9926bSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61cf9926bSpatrick //
71cf9926bSpatrick //===----------------------------------------------------------------------===//
81cf9926bSpatrick
91cf9926bSpatrick #include "ICF.h"
101cf9926bSpatrick #include "ConcatOutputSection.h"
11*dfe94b16Srobert #include "Config.h"
121cf9926bSpatrick #include "InputSection.h"
13*dfe94b16Srobert #include "SymbolTable.h"
141cf9926bSpatrick #include "Symbols.h"
151cf9926bSpatrick #include "UnwindInfoSection.h"
161cf9926bSpatrick
17*dfe94b16Srobert #include "lld/Common/CommonLinkerContext.h"
18*dfe94b16Srobert #include "llvm/Support/LEB128.h"
191cf9926bSpatrick #include "llvm/Support/Parallel.h"
201cf9926bSpatrick #include "llvm/Support/TimeProfiler.h"
21*dfe94b16Srobert #include "llvm/Support/xxhash.h"
221cf9926bSpatrick
231cf9926bSpatrick #include <atomic>
241cf9926bSpatrick
251cf9926bSpatrick using namespace llvm;
261cf9926bSpatrick using namespace lld;
271cf9926bSpatrick using namespace lld::macho;
281cf9926bSpatrick
29*dfe94b16Srobert static constexpr bool verboseDiagnostics = false;
30*dfe94b16Srobert
311cf9926bSpatrick class ICF {
321cf9926bSpatrick public:
331cf9926bSpatrick ICF(std::vector<ConcatInputSection *> &inputs);
341cf9926bSpatrick void run();
35*dfe94b16Srobert
36*dfe94b16Srobert using EqualsFn = bool (ICF::*)(const ConcatInputSection *,
37*dfe94b16Srobert const ConcatInputSection *);
38*dfe94b16Srobert void segregate(size_t begin, size_t end, EqualsFn);
391cf9926bSpatrick size_t findBoundary(size_t begin, size_t end);
401cf9926bSpatrick void forEachClassRange(size_t begin, size_t end,
41*dfe94b16Srobert llvm::function_ref<void(size_t, size_t)> func);
42*dfe94b16Srobert void forEachClass(llvm::function_ref<void(size_t, size_t)> func);
43*dfe94b16Srobert
44*dfe94b16Srobert bool equalsConstant(const ConcatInputSection *ia,
45*dfe94b16Srobert const ConcatInputSection *ib);
46*dfe94b16Srobert bool equalsVariable(const ConcatInputSection *ia,
47*dfe94b16Srobert const ConcatInputSection *ib);
481cf9926bSpatrick
491cf9926bSpatrick // ICF needs a copy of the inputs vector because its equivalence-class
501cf9926bSpatrick // segregation algorithm destroys the proper sequence.
511cf9926bSpatrick std::vector<ConcatInputSection *> icfInputs;
52*dfe94b16Srobert
53*dfe94b16Srobert unsigned icfPass = 0;
54*dfe94b16Srobert std::atomic<bool> icfRepeat{false};
55*dfe94b16Srobert std::atomic<uint64_t> equalsConstantCount{0};
56*dfe94b16Srobert std::atomic<uint64_t> equalsVariableCount{0};
571cf9926bSpatrick };
581cf9926bSpatrick
ICF(std::vector<ConcatInputSection * > & inputs)591cf9926bSpatrick ICF::ICF(std::vector<ConcatInputSection *> &inputs) {
601cf9926bSpatrick icfInputs.assign(inputs.begin(), inputs.end());
611cf9926bSpatrick }
621cf9926bSpatrick
631cf9926bSpatrick // ICF = Identical Code Folding
641cf9926bSpatrick //
651cf9926bSpatrick // We only fold __TEXT,__text, so this is really "code" folding, and not
661cf9926bSpatrick // "COMDAT" folding. String and scalar constant literals are deduplicated
671cf9926bSpatrick // elsewhere.
681cf9926bSpatrick //
691cf9926bSpatrick // Summary of segments & sections:
701cf9926bSpatrick //
711cf9926bSpatrick // The __TEXT segment is readonly at the MMU. Some sections are already
721cf9926bSpatrick // deduplicated elsewhere (__TEXT,__cstring & __TEXT,__literal*) and some are
731cf9926bSpatrick // synthetic and inherently free of duplicates (__TEXT,__stubs &
741cf9926bSpatrick // __TEXT,__unwind_info). Note that we don't yet run ICF on __TEXT,__const,
751cf9926bSpatrick // because doing so induces many test failures.
761cf9926bSpatrick //
771cf9926bSpatrick // The __LINKEDIT segment is readonly at the MMU, yet entirely synthetic, and
781cf9926bSpatrick // thus ineligible for ICF.
791cf9926bSpatrick //
801cf9926bSpatrick // The __DATA_CONST segment is read/write at the MMU, but is logically const to
811cf9926bSpatrick // the application after dyld applies fixups to pointer data. We currently
821cf9926bSpatrick // fold only the __DATA_CONST,__cfstring section.
831cf9926bSpatrick //
841cf9926bSpatrick // The __DATA segment is read/write at the MMU, and as application-writeable
851cf9926bSpatrick // data, none of its sections are eligible for ICF.
861cf9926bSpatrick //
871cf9926bSpatrick // Please see the large block comment in lld/ELF/ICF.cpp for an explanation
881cf9926bSpatrick // of the segregation algorithm.
891cf9926bSpatrick //
901cf9926bSpatrick // FIXME(gkm): implement keep-unique attributes
911cf9926bSpatrick // FIXME(gkm): implement address-significance tables for MachO object files
921cf9926bSpatrick
931cf9926bSpatrick // Compare "non-moving" parts of two ConcatInputSections, namely everything
941cf9926bSpatrick // except references to other ConcatInputSections.
equalsConstant(const ConcatInputSection * ia,const ConcatInputSection * ib)95*dfe94b16Srobert bool ICF::equalsConstant(const ConcatInputSection *ia,
961cf9926bSpatrick const ConcatInputSection *ib) {
97*dfe94b16Srobert if (verboseDiagnostics)
98*dfe94b16Srobert ++equalsConstantCount;
991cf9926bSpatrick // We can only fold within the same OutputSection.
1001cf9926bSpatrick if (ia->parent != ib->parent)
1011cf9926bSpatrick return false;
1021cf9926bSpatrick if (ia->data.size() != ib->data.size())
1031cf9926bSpatrick return false;
1041cf9926bSpatrick if (ia->data != ib->data)
1051cf9926bSpatrick return false;
1061cf9926bSpatrick if (ia->relocs.size() != ib->relocs.size())
1071cf9926bSpatrick return false;
1081cf9926bSpatrick auto f = [](const Reloc &ra, const Reloc &rb) {
1091cf9926bSpatrick if (ra.type != rb.type)
1101cf9926bSpatrick return false;
1111cf9926bSpatrick if (ra.pcrel != rb.pcrel)
1121cf9926bSpatrick return false;
1131cf9926bSpatrick if (ra.length != rb.length)
1141cf9926bSpatrick return false;
1151cf9926bSpatrick if (ra.offset != rb.offset)
1161cf9926bSpatrick return false;
1171cf9926bSpatrick if (ra.referent.is<Symbol *>() != rb.referent.is<Symbol *>())
1181cf9926bSpatrick return false;
1191cf9926bSpatrick
1201cf9926bSpatrick InputSection *isecA, *isecB;
121*dfe94b16Srobert
122*dfe94b16Srobert uint64_t valueA = 0;
123*dfe94b16Srobert uint64_t valueB = 0;
1241cf9926bSpatrick if (ra.referent.is<Symbol *>()) {
1251cf9926bSpatrick const auto *sa = ra.referent.get<Symbol *>();
1261cf9926bSpatrick const auto *sb = rb.referent.get<Symbol *>();
1271cf9926bSpatrick if (sa->kind() != sb->kind())
1281cf9926bSpatrick return false;
129*dfe94b16Srobert // ICF runs before Undefineds are treated (and potentially converted into
130*dfe94b16Srobert // DylibSymbols).
131*dfe94b16Srobert if (isa<DylibSymbol>(sa) || isa<Undefined>(sa))
132*dfe94b16Srobert return sa == sb && ra.addend == rb.addend;
133*dfe94b16Srobert assert(isa<Defined>(sa));
1341cf9926bSpatrick const auto *da = cast<Defined>(sa);
1351cf9926bSpatrick const auto *db = cast<Defined>(sb);
136*dfe94b16Srobert if (!da->isec || !db->isec) {
1371cf9926bSpatrick assert(da->isAbsolute() && db->isAbsolute());
138*dfe94b16Srobert return da->value + ra.addend == db->value + rb.addend;
1391cf9926bSpatrick }
140*dfe94b16Srobert isecA = da->isec;
141*dfe94b16Srobert valueA = da->value;
142*dfe94b16Srobert isecB = db->isec;
143*dfe94b16Srobert valueB = db->value;
1441cf9926bSpatrick } else {
1451cf9926bSpatrick isecA = ra.referent.get<InputSection *>();
1461cf9926bSpatrick isecB = rb.referent.get<InputSection *>();
1471cf9926bSpatrick }
1481cf9926bSpatrick
1491cf9926bSpatrick if (isecA->parent != isecB->parent)
1501cf9926bSpatrick return false;
1511cf9926bSpatrick // Sections with identical parents should be of the same kind.
1521cf9926bSpatrick assert(isecA->kind() == isecB->kind());
1531cf9926bSpatrick // We will compare ConcatInputSection contents in equalsVariable.
1541cf9926bSpatrick if (isa<ConcatInputSection>(isecA))
155*dfe94b16Srobert return ra.addend == rb.addend;
1561cf9926bSpatrick // Else we have two literal sections. References to them are equal iff their
1571cf9926bSpatrick // offsets in the output section are equal.
158*dfe94b16Srobert if (ra.referent.is<Symbol *>())
159*dfe94b16Srobert // For symbol relocs, we compare the contents at the symbol address. We
160*dfe94b16Srobert // don't do `getOffset(value + addend)` because value + addend may not be
161*dfe94b16Srobert // a valid offset in the literal section.
162*dfe94b16Srobert return isecA->getOffset(valueA) == isecB->getOffset(valueB) &&
163*dfe94b16Srobert ra.addend == rb.addend;
164*dfe94b16Srobert else {
165*dfe94b16Srobert assert(valueA == 0 && valueB == 0);
166*dfe94b16Srobert // For section relocs, we compare the content at the section offset.
1671cf9926bSpatrick return isecA->getOffset(ra.addend) == isecB->getOffset(rb.addend);
168*dfe94b16Srobert }
1691cf9926bSpatrick };
1701cf9926bSpatrick return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
1711cf9926bSpatrick f);
1721cf9926bSpatrick }
1731cf9926bSpatrick
1741cf9926bSpatrick // Compare the "moving" parts of two ConcatInputSections -- i.e. everything not
1751cf9926bSpatrick // handled by equalsConstant().
equalsVariable(const ConcatInputSection * ia,const ConcatInputSection * ib)176*dfe94b16Srobert bool ICF::equalsVariable(const ConcatInputSection *ia,
1771cf9926bSpatrick const ConcatInputSection *ib) {
178*dfe94b16Srobert if (verboseDiagnostics)
179*dfe94b16Srobert ++equalsVariableCount;
1801cf9926bSpatrick assert(ia->relocs.size() == ib->relocs.size());
181*dfe94b16Srobert auto f = [this](const Reloc &ra, const Reloc &rb) {
1821cf9926bSpatrick // We already filtered out mismatching values/addends in equalsConstant.
1831cf9926bSpatrick if (ra.referent == rb.referent)
1841cf9926bSpatrick return true;
1851cf9926bSpatrick const ConcatInputSection *isecA, *isecB;
1861cf9926bSpatrick if (ra.referent.is<Symbol *>()) {
1871cf9926bSpatrick // Matching DylibSymbols are already filtered out by the
1881cf9926bSpatrick // identical-referent check above. Non-matching DylibSymbols were filtered
1891cf9926bSpatrick // out in equalsConstant(). So we can safely cast to Defined here.
1901cf9926bSpatrick const auto *da = cast<Defined>(ra.referent.get<Symbol *>());
1911cf9926bSpatrick const auto *db = cast<Defined>(rb.referent.get<Symbol *>());
1921cf9926bSpatrick if (da->isAbsolute())
1931cf9926bSpatrick return true;
1941cf9926bSpatrick isecA = dyn_cast<ConcatInputSection>(da->isec);
1951cf9926bSpatrick if (!isecA)
1961cf9926bSpatrick return true; // literal sections were checked in equalsConstant.
1971cf9926bSpatrick isecB = cast<ConcatInputSection>(db->isec);
1981cf9926bSpatrick } else {
1991cf9926bSpatrick const auto *sa = ra.referent.get<InputSection *>();
2001cf9926bSpatrick const auto *sb = rb.referent.get<InputSection *>();
2011cf9926bSpatrick isecA = dyn_cast<ConcatInputSection>(sa);
2021cf9926bSpatrick if (!isecA)
2031cf9926bSpatrick return true;
2041cf9926bSpatrick isecB = cast<ConcatInputSection>(sb);
2051cf9926bSpatrick }
2061cf9926bSpatrick return isecA->icfEqClass[icfPass % 2] == isecB->icfEqClass[icfPass % 2];
2071cf9926bSpatrick };
208*dfe94b16Srobert if (!std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(), f))
209*dfe94b16Srobert return false;
210*dfe94b16Srobert
211*dfe94b16Srobert // If there are symbols with associated unwind info, check that the unwind
212*dfe94b16Srobert // info matches. For simplicity, we only handle the case where there are only
213*dfe94b16Srobert // symbols at offset zero within the section (which is typically the case with
214*dfe94b16Srobert // .subsections_via_symbols.)
215*dfe94b16Srobert auto hasUnwind = [](Defined *d) { return d->unwindEntry != nullptr; };
216*dfe94b16Srobert auto itA = std::find_if(ia->symbols.begin(), ia->symbols.end(), hasUnwind);
217*dfe94b16Srobert auto itB = std::find_if(ib->symbols.begin(), ib->symbols.end(), hasUnwind);
218*dfe94b16Srobert if (itA == ia->symbols.end())
219*dfe94b16Srobert return itB == ib->symbols.end();
220*dfe94b16Srobert if (itB == ib->symbols.end())
221*dfe94b16Srobert return false;
222*dfe94b16Srobert const Defined *da = *itA;
223*dfe94b16Srobert const Defined *db = *itB;
224*dfe94b16Srobert if (da->unwindEntry->icfEqClass[icfPass % 2] !=
225*dfe94b16Srobert db->unwindEntry->icfEqClass[icfPass % 2] ||
226*dfe94b16Srobert da->value != 0 || db->value != 0)
227*dfe94b16Srobert return false;
228*dfe94b16Srobert auto isZero = [](Defined *d) { return d->value == 0; };
229*dfe94b16Srobert return std::find_if_not(std::next(itA), ia->symbols.end(), isZero) ==
230*dfe94b16Srobert ia->symbols.end() &&
231*dfe94b16Srobert std::find_if_not(std::next(itB), ib->symbols.end(), isZero) ==
232*dfe94b16Srobert ib->symbols.end();
2331cf9926bSpatrick }
2341cf9926bSpatrick
2351cf9926bSpatrick // Find the first InputSection after BEGIN whose equivalence class differs
findBoundary(size_t begin,size_t end)2361cf9926bSpatrick size_t ICF::findBoundary(size_t begin, size_t end) {
2371cf9926bSpatrick uint64_t beginHash = icfInputs[begin]->icfEqClass[icfPass % 2];
2381cf9926bSpatrick for (size_t i = begin + 1; i < end; ++i)
2391cf9926bSpatrick if (beginHash != icfInputs[i]->icfEqClass[icfPass % 2])
2401cf9926bSpatrick return i;
2411cf9926bSpatrick return end;
2421cf9926bSpatrick }
2431cf9926bSpatrick
2441cf9926bSpatrick // Invoke FUNC on subranges with matching equivalence class
forEachClassRange(size_t begin,size_t end,llvm::function_ref<void (size_t,size_t)> func)2451cf9926bSpatrick void ICF::forEachClassRange(size_t begin, size_t end,
246*dfe94b16Srobert llvm::function_ref<void(size_t, size_t)> func) {
2471cf9926bSpatrick while (begin < end) {
2481cf9926bSpatrick size_t mid = findBoundary(begin, end);
2491cf9926bSpatrick func(begin, mid);
2501cf9926bSpatrick begin = mid;
2511cf9926bSpatrick }
2521cf9926bSpatrick }
2531cf9926bSpatrick
2541cf9926bSpatrick // Split icfInputs into shards, then parallelize invocation of FUNC on subranges
2551cf9926bSpatrick // with matching equivalence class
forEachClass(llvm::function_ref<void (size_t,size_t)> func)256*dfe94b16Srobert void ICF::forEachClass(llvm::function_ref<void(size_t, size_t)> func) {
2571cf9926bSpatrick // Only use threads when the benefits outweigh the overhead.
2581cf9926bSpatrick const size_t threadingThreshold = 1024;
2591cf9926bSpatrick if (icfInputs.size() < threadingThreshold) {
2601cf9926bSpatrick forEachClassRange(0, icfInputs.size(), func);
2611cf9926bSpatrick ++icfPass;
2621cf9926bSpatrick return;
2631cf9926bSpatrick }
2641cf9926bSpatrick
2651cf9926bSpatrick // Shard into non-overlapping intervals, and call FUNC in parallel. The
2661cf9926bSpatrick // sharding must be completed before any calls to FUNC are made so that FUNC
2671cf9926bSpatrick // can modify the InputSection in its shard without causing data races.
2681cf9926bSpatrick const size_t shards = 256;
2691cf9926bSpatrick size_t step = icfInputs.size() / shards;
2701cf9926bSpatrick size_t boundaries[shards + 1];
2711cf9926bSpatrick boundaries[0] = 0;
2721cf9926bSpatrick boundaries[shards] = icfInputs.size();
273*dfe94b16Srobert parallelFor(1, shards, [&](size_t i) {
2741cf9926bSpatrick boundaries[i] = findBoundary((i - 1) * step, icfInputs.size());
2751cf9926bSpatrick });
276*dfe94b16Srobert parallelFor(1, shards + 1, [&](size_t i) {
2771cf9926bSpatrick if (boundaries[i - 1] < boundaries[i]) {
2781cf9926bSpatrick forEachClassRange(boundaries[i - 1], boundaries[i], func);
2791cf9926bSpatrick }
2801cf9926bSpatrick });
2811cf9926bSpatrick ++icfPass;
2821cf9926bSpatrick }
2831cf9926bSpatrick
run()2841cf9926bSpatrick void ICF::run() {
2851cf9926bSpatrick // Into each origin-section hash, combine all reloc referent section hashes.
2861cf9926bSpatrick for (icfPass = 0; icfPass < 2; ++icfPass) {
2871cf9926bSpatrick parallelForEach(icfInputs, [&](ConcatInputSection *isec) {
288*dfe94b16Srobert uint32_t hash = isec->icfEqClass[icfPass % 2];
2891cf9926bSpatrick for (const Reloc &r : isec->relocs) {
2901cf9926bSpatrick if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
291*dfe94b16Srobert if (auto *defined = dyn_cast<Defined>(sym)) {
2921cf9926bSpatrick if (defined->isec) {
293*dfe94b16Srobert if (auto referentIsec =
294*dfe94b16Srobert dyn_cast<ConcatInputSection>(defined->isec))
295*dfe94b16Srobert hash += defined->value + referentIsec->icfEqClass[icfPass % 2];
2961cf9926bSpatrick else
2971cf9926bSpatrick hash += defined->isec->kind() +
2981cf9926bSpatrick defined->isec->getOffset(defined->value);
2991cf9926bSpatrick } else {
3001cf9926bSpatrick hash += defined->value;
3011cf9926bSpatrick }
302*dfe94b16Srobert } else {
303*dfe94b16Srobert // ICF runs before Undefined diags
304*dfe94b16Srobert assert(isa<Undefined>(sym) || isa<DylibSymbol>(sym));
305*dfe94b16Srobert }
3061cf9926bSpatrick }
3071cf9926bSpatrick }
3081cf9926bSpatrick // Set MSB to 1 to avoid collisions with non-hashed classes.
309*dfe94b16Srobert isec->icfEqClass[(icfPass + 1) % 2] = hash | (1ull << 31);
3101cf9926bSpatrick });
3111cf9926bSpatrick }
3121cf9926bSpatrick
3131cf9926bSpatrick llvm::stable_sort(
3141cf9926bSpatrick icfInputs, [](const ConcatInputSection *a, const ConcatInputSection *b) {
3151cf9926bSpatrick return a->icfEqClass[0] < b->icfEqClass[0];
3161cf9926bSpatrick });
317*dfe94b16Srobert forEachClass([&](size_t begin, size_t end) {
318*dfe94b16Srobert segregate(begin, end, &ICF::equalsConstant);
319*dfe94b16Srobert });
3201cf9926bSpatrick
3211cf9926bSpatrick // Split equivalence groups by comparing relocations until convergence
3221cf9926bSpatrick do {
3231cf9926bSpatrick icfRepeat = false;
3241cf9926bSpatrick forEachClass([&](size_t begin, size_t end) {
325*dfe94b16Srobert segregate(begin, end, &ICF::equalsVariable);
3261cf9926bSpatrick });
3271cf9926bSpatrick } while (icfRepeat);
3281cf9926bSpatrick log("ICF needed " + Twine(icfPass) + " iterations");
329*dfe94b16Srobert if (verboseDiagnostics) {
330*dfe94b16Srobert log("equalsConstant() called " + Twine(equalsConstantCount) + " times");
331*dfe94b16Srobert log("equalsVariable() called " + Twine(equalsVariableCount) + " times");
332*dfe94b16Srobert }
3331cf9926bSpatrick
3341cf9926bSpatrick // Fold sections within equivalence classes
3351cf9926bSpatrick forEachClass([&](size_t begin, size_t end) {
3361cf9926bSpatrick if (end - begin < 2)
3371cf9926bSpatrick return;
3381cf9926bSpatrick ConcatInputSection *beginIsec = icfInputs[begin];
3391cf9926bSpatrick for (size_t i = begin + 1; i < end; ++i)
3401cf9926bSpatrick beginIsec->foldIdentical(icfInputs[i]);
3411cf9926bSpatrick });
3421cf9926bSpatrick }
3431cf9926bSpatrick
3441cf9926bSpatrick // Split an equivalence class into smaller classes.
segregate(size_t begin,size_t end,EqualsFn equals)345*dfe94b16Srobert void ICF::segregate(size_t begin, size_t end, EqualsFn equals) {
3461cf9926bSpatrick while (begin < end) {
3471cf9926bSpatrick // Divide [begin, end) into two. Let mid be the start index of the
3481cf9926bSpatrick // second group.
349*dfe94b16Srobert auto bound = std::stable_partition(
350*dfe94b16Srobert icfInputs.begin() + begin + 1, icfInputs.begin() + end,
3511cf9926bSpatrick [&](ConcatInputSection *isec) {
352*dfe94b16Srobert return (this->*equals)(icfInputs[begin], isec);
3531cf9926bSpatrick });
3541cf9926bSpatrick size_t mid = bound - icfInputs.begin();
3551cf9926bSpatrick
3561cf9926bSpatrick // Split [begin, end) into [begin, mid) and [mid, end). We use mid as an
3571cf9926bSpatrick // equivalence class ID because every group ends with a unique index.
3581cf9926bSpatrick for (size_t i = begin; i < mid; ++i)
3591cf9926bSpatrick icfInputs[i]->icfEqClass[(icfPass + 1) % 2] = mid;
3601cf9926bSpatrick
3611cf9926bSpatrick // If we created a group, we need to iterate the main loop again.
3621cf9926bSpatrick if (mid != end)
3631cf9926bSpatrick icfRepeat = true;
3641cf9926bSpatrick
3651cf9926bSpatrick begin = mid;
3661cf9926bSpatrick }
3671cf9926bSpatrick }
3681cf9926bSpatrick
markSymAsAddrSig(Symbol * s)369*dfe94b16Srobert void macho::markSymAsAddrSig(Symbol *s) {
370*dfe94b16Srobert if (auto *d = dyn_cast_or_null<Defined>(s))
371*dfe94b16Srobert if (d->isec)
372*dfe94b16Srobert d->isec->keepUnique = true;
3731cf9926bSpatrick }
3741cf9926bSpatrick
markAddrSigSymbols()375*dfe94b16Srobert void macho::markAddrSigSymbols() {
376*dfe94b16Srobert TimeTraceScope timeScope("Mark addrsig symbols");
377*dfe94b16Srobert for (InputFile *file : inputFiles) {
378*dfe94b16Srobert ObjFile *obj = dyn_cast<ObjFile>(file);
379*dfe94b16Srobert if (!obj)
380*dfe94b16Srobert continue;
381*dfe94b16Srobert
382*dfe94b16Srobert Section *addrSigSection = obj->addrSigSection;
383*dfe94b16Srobert if (!addrSigSection)
384*dfe94b16Srobert continue;
385*dfe94b16Srobert assert(addrSigSection->subsections.size() == 1);
386*dfe94b16Srobert
387*dfe94b16Srobert const InputSection *isec = addrSigSection->subsections[0].isec;
388*dfe94b16Srobert
389*dfe94b16Srobert for (const Reloc &r : isec->relocs) {
390*dfe94b16Srobert if (auto *sym = r.referent.dyn_cast<Symbol *>())
391*dfe94b16Srobert markSymAsAddrSig(sym);
392*dfe94b16Srobert else
393*dfe94b16Srobert error(toString(isec) + ": unexpected section relocation");
394*dfe94b16Srobert }
395*dfe94b16Srobert }
396*dfe94b16Srobert }
397*dfe94b16Srobert
foldIdenticalSections(bool onlyCfStrings)398*dfe94b16Srobert void macho::foldIdenticalSections(bool onlyCfStrings) {
3991cf9926bSpatrick TimeTraceScope timeScope("Fold Identical Code Sections");
4001cf9926bSpatrick // The ICF equivalence-class segregation algorithm relies on pre-computed
4011cf9926bSpatrick // hashes of InputSection::data for the ConcatOutputSection::inputs and all
4021cf9926bSpatrick // sections referenced by their relocs. We could recursively traverse the
4031cf9926bSpatrick // relocs to find every referenced InputSection, but that precludes easy
4041cf9926bSpatrick // parallelization. Therefore, we hash every InputSection here where we have
4051cf9926bSpatrick // them all accessible as simple vectors.
4061cf9926bSpatrick
4071cf9926bSpatrick // If an InputSection is ineligible for ICF, we give it a unique ID to force
4081cf9926bSpatrick // it into an unfoldable singleton equivalence class. Begin the unique-ID
4091cf9926bSpatrick // space at inputSections.size(), so that it will never intersect with
4101cf9926bSpatrick // equivalence-class IDs which begin at 0. Since hashes & unique IDs never
4111cf9926bSpatrick // coexist with equivalence-class IDs, this is not necessary, but might help
4121cf9926bSpatrick // someone keep the numbers straight in case we ever need to debug the
4131cf9926bSpatrick // ICF::segregate()
414*dfe94b16Srobert std::vector<ConcatInputSection *> foldable;
4151cf9926bSpatrick uint64_t icfUniqueID = inputSections.size();
4161cf9926bSpatrick for (ConcatInputSection *isec : inputSections) {
417*dfe94b16Srobert bool isFoldableWithAddendsRemoved = isCfStringSection(isec) ||
418*dfe94b16Srobert isClassRefsSection(isec) ||
419*dfe94b16Srobert isSelRefsSection(isec);
420*dfe94b16Srobert // NOTE: __objc_selrefs is typically marked as no_dead_strip by MC, but we
421*dfe94b16Srobert // can still fold it.
422*dfe94b16Srobert bool hasFoldableFlags = (isSelRefsSection(isec) ||
423*dfe94b16Srobert sectionType(isec->getFlags()) == MachO::S_REGULAR);
424*dfe94b16Srobert // FIXME: consider non-code __text sections as foldable?
425*dfe94b16Srobert bool isFoldable = (!onlyCfStrings || isCfStringSection(isec)) &&
426*dfe94b16Srobert (isCodeSection(isec) || isFoldableWithAddendsRemoved ||
427*dfe94b16Srobert isGccExceptTabSection(isec)) &&
428*dfe94b16Srobert !isec->keepUnique && !isec->hasAltEntry &&
429*dfe94b16Srobert !isec->shouldOmitFromOutput() && hasFoldableFlags;
430*dfe94b16Srobert if (isFoldable) {
431*dfe94b16Srobert foldable.push_back(isec);
432*dfe94b16Srobert for (Defined *d : isec->symbols)
433*dfe94b16Srobert if (d->unwindEntry)
434*dfe94b16Srobert foldable.push_back(d->unwindEntry);
435*dfe94b16Srobert
436*dfe94b16Srobert // Some sections have embedded addends that foil ICF's hashing / equality
437*dfe94b16Srobert // checks. (We can ignore embedded addends when doing ICF because the same
438*dfe94b16Srobert // information gets recorded in our Reloc structs.) We therefore create a
439*dfe94b16Srobert // mutable copy of the section data and zero out the embedded addends
440*dfe94b16Srobert // before performing any hashing / equality checks.
441*dfe94b16Srobert if (isFoldableWithAddendsRemoved) {
442*dfe94b16Srobert // We have to do this copying serially as the BumpPtrAllocator is not
443*dfe94b16Srobert // thread-safe. FIXME: Make a thread-safe allocator.
444*dfe94b16Srobert MutableArrayRef<uint8_t> copy = isec->data.copy(bAlloc());
445*dfe94b16Srobert for (const Reloc &r : isec->relocs)
446*dfe94b16Srobert target->relocateOne(copy.data() + r.offset, r, /*va=*/0,
447*dfe94b16Srobert /*relocVA=*/0);
448*dfe94b16Srobert isec->data = copy;
449*dfe94b16Srobert }
450*dfe94b16Srobert } else if (!isEhFrameSection(isec)) {
451*dfe94b16Srobert // EH frames are gathered as foldables from unwindEntry above; give a
452*dfe94b16Srobert // unique ID to everything else.
4531cf9926bSpatrick isec->icfEqClass[0] = ++icfUniqueID;
4541cf9926bSpatrick }
455*dfe94b16Srobert }
456*dfe94b16Srobert parallelForEach(foldable, [](ConcatInputSection *isec) {
457*dfe94b16Srobert assert(isec->icfEqClass[0] == 0); // don't overwrite a unique ID!
458*dfe94b16Srobert // Turn-on the top bit to guarantee that valid hashes have no collisions
459*dfe94b16Srobert // with the small-integer unique IDs for ICF-ineligible sections
460*dfe94b16Srobert isec->icfEqClass[0] = xxHash64(isec->data) | (1ull << 31);
461*dfe94b16Srobert });
4621cf9926bSpatrick // Now that every input section is either hashed or marked as unique, run the
4631cf9926bSpatrick // segregation algorithm to detect foldable subsections.
464*dfe94b16Srobert ICF(foldable).run();
4651cf9926bSpatrick }
466