1//===- ICF.cpp ------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ICF.h"
10#include "ConcatOutputSection.h"
11#include "Config.h"
12#include "InputSection.h"
13#include "SymbolTable.h"
14#include "Symbols.h"
15
16#include "lld/Common/CommonLinkerContext.h"
17#include "llvm/Support/Parallel.h"
18#include "llvm/Support/TimeProfiler.h"
19#include "llvm/Support/xxhash.h"
20
21#include <atomic>
22
23using namespace llvm;
24using namespace lld;
25using namespace lld::macho;
26
27static constexpr bool verboseDiagnostics = false;
28// This counter is used to generate unique thunk names.
29static uint64_t icfThunkCounter = 0;
30
31class ICF {
32public:
33 ICF(std::vector<ConcatInputSection *> &inputs);
34 void run();
35
36 using EqualsFn = bool (ICF::*)(const ConcatInputSection *,
37 const ConcatInputSection *);
38 void segregate(size_t begin, size_t end, EqualsFn);
39 size_t findBoundary(size_t begin, size_t end);
40 void forEachClassRange(size_t begin, size_t end,
41 llvm::function_ref<void(size_t, size_t)> func);
42 void forEachClass(llvm::function_ref<void(size_t, size_t)> func);
43
44 bool equalsConstant(const ConcatInputSection *ia,
45 const ConcatInputSection *ib);
46 bool equalsVariable(const ConcatInputSection *ia,
47 const ConcatInputSection *ib);
48 void applySafeThunksToRange(size_t begin, size_t end);
49
50 // ICF needs a copy of the inputs vector because its equivalence-class
51 // segregation algorithm destroys the proper sequence.
52 std::vector<ConcatInputSection *> icfInputs;
53
54 unsigned icfPass = 0;
55 std::atomic<bool> icfRepeat{false};
56 std::atomic<uint64_t> equalsConstantCount{0};
57 std::atomic<uint64_t> equalsVariableCount{0};
58};
59
60ICF::ICF(std::vector<ConcatInputSection *> &inputs) {
61 icfInputs.assign(first: inputs.begin(), last: inputs.end());
62}
63
64// ICF = Identical Code Folding
65//
66// We only fold __TEXT,__text, so this is really "code" folding, and not
67// "COMDAT" folding. String and scalar constant literals are deduplicated
68// elsewhere.
69//
70// Summary of segments & sections:
71//
72// The __TEXT segment is readonly at the MMU. Some sections are already
73// deduplicated elsewhere (__TEXT,__cstring & __TEXT,__literal*) and some are
74// synthetic and inherently free of duplicates (__TEXT,__stubs &
75// __TEXT,__unwind_info). Note that we don't yet run ICF on __TEXT,__const,
76// because doing so induces many test failures.
77//
78// The __LINKEDIT segment is readonly at the MMU, yet entirely synthetic, and
79// thus ineligible for ICF.
80//
81// The __DATA_CONST segment is read/write at the MMU, but is logically const to
82// the application after dyld applies fixups to pointer data. We currently
83// fold only the __DATA_CONST,__cfstring section.
84//
85// The __DATA segment is read/write at the MMU, and as application-writeable
86// data, none of its sections are eligible for ICF.
87//
88// Please see the large block comment in lld/ELF/ICF.cpp for an explanation
89// of the segregation algorithm.
90//
91// FIXME(gkm): implement keep-unique attributes
92// FIXME(gkm): implement address-significance tables for MachO object files
93
94// Compare "non-moving" parts of two ConcatInputSections, namely everything
95// except references to other ConcatInputSections.
96bool ICF::equalsConstant(const ConcatInputSection *ia,
97 const ConcatInputSection *ib) {
98 if (verboseDiagnostics)
99 ++equalsConstantCount;
100 // We can only fold within the same OutputSection.
101 if (ia->parent != ib->parent)
102 return false;
103 if (ia->data.size() != ib->data.size())
104 return false;
105 if (ia->data != ib->data)
106 return false;
107 if (ia->relocs.size() != ib->relocs.size())
108 return false;
109 auto f = [](const Relocation &ra, const Relocation &rb) {
110 if (ra.type != rb.type)
111 return false;
112 if (ra.pcrel != rb.pcrel)
113 return false;
114 if (ra.length != rb.length)
115 return false;
116 if (ra.offset != rb.offset)
117 return false;
118 if (isa<Symbol *>(Val: ra.referent) != isa<Symbol *>(Val: rb.referent))
119 return false;
120
121 InputSection *isecA, *isecB;
122
123 uint64_t valueA = 0;
124 uint64_t valueB = 0;
125 if (isa<Symbol *>(Val: ra.referent)) {
126 const auto *sa = cast<Symbol *>(Val: ra.referent);
127 const auto *sb = cast<Symbol *>(Val: rb.referent);
128 if (sa->kind() != sb->kind())
129 return false;
130 // ICF runs before Undefineds are treated (and potentially converted into
131 // DylibSymbols).
132 if (isa<DylibSymbol>(Val: sa) || isa<Undefined>(Val: sa))
133 return sa == sb && ra.addend == rb.addend;
134 assert(isa<Defined>(sa));
135 const auto *da = cast<Defined>(Val: sa);
136 const auto *db = cast<Defined>(Val: sb);
137 if (!da->isec() || !db->isec()) {
138 assert(da->isAbsolute() && db->isAbsolute());
139 return da->value + ra.addend == db->value + rb.addend;
140 }
141 isecA = da->isec();
142 valueA = da->value;
143 isecB = db->isec();
144 valueB = db->value;
145 } else {
146 isecA = cast<InputSection *>(Val: ra.referent);
147 isecB = cast<InputSection *>(Val: rb.referent);
148 }
149
150 // Typically, we should not encounter sections marked with `keepUnique` at
151 // this point as they would have resulted in different hashes and therefore
152 // no need for a full comparison.
153 // However, in `safe_thunks` mode, it's possible for two different
154 // relocations to reference identical `keepUnique` functions that will be
155 // distinguished later via thunks - so we need to handle this case
156 // explicitly.
157 if ((isecA != isecB) && ((isecA->keepUnique && isCodeSection(isecA)) ||
158 (isecB->keepUnique && isCodeSection(isecB))))
159 return false;
160
161 if (isecA->parent != isecB->parent)
162 return false;
163 // Sections with identical parents should be of the same kind.
164 assert(isecA->kind() == isecB->kind());
165 // We will compare ConcatInputSection contents in equalsVariable.
166 if (isa<ConcatInputSection>(Val: isecA))
167 return ra.addend == rb.addend;
168 // Else we have two literal sections. References to them are equal iff their
169 // offsets in the output section are equal.
170 if (isa<Symbol *>(Val: ra.referent))
171 // For symbol relocs, we compare the contents at the symbol address. We
172 // don't do `getOffset(value + addend)` because value + addend may not be
173 // a valid offset in the literal section.
174 return isecA->getOffset(off: valueA) == isecB->getOffset(off: valueB) &&
175 ra.addend == rb.addend;
176 assert(valueA == 0 && valueB == 0);
177 // For section relocs, we compare the content at the section offset.
178 return isecA->getOffset(off: ra.addend) == isecB->getOffset(off: rb.addend);
179 };
180 if (!llvm::equal(LRange: ia->relocs, RRange: ib->relocs, P: f))
181 return false;
182
183 // Check unwind info structural compatibility: if there are symbols with
184 // associated unwind info, check that both sections have compatible symbol
185 // layouts. For simplicity, we only attempt folding when all symbols are at
186 // offset zero within the section (which is typically the case with
187 // .subsections_via_symbols.)
188 auto hasUnwind = [](Defined *d) { return d->unwindEntry() != nullptr; };
189 const auto *itA = llvm::find_if(Range: ia->symbols, P: hasUnwind);
190 const auto *itB = llvm::find_if(Range: ib->symbols, P: hasUnwind);
191 if (itA == ia->symbols.end())
192 return itB == ib->symbols.end();
193 if (itB == ib->symbols.end())
194 return false;
195 const Defined *da = *itA;
196 const Defined *db = *itB;
197 if (da->value != 0 || db->value != 0)
198 return false;
199 auto isZero = [](Defined *d) { return d->value == 0; };
200 // Since symbols are stored in order of value, and since we have already
201 // checked that da/db have value zero, we just need to do the isZero check on
202 // the subsequent symbols.
203 return std::find_if_not(first: std::next(x: itA), last: ia->symbols.end(), pred: isZero) ==
204 ia->symbols.end() &&
205 std::find_if_not(first: std::next(x: itB), last: ib->symbols.end(), pred: isZero) ==
206 ib->symbols.end();
207}
208
209// Compare the "moving" parts of two ConcatInputSections -- i.e. everything not
210// handled by equalsConstant().
211bool ICF::equalsVariable(const ConcatInputSection *ia,
212 const ConcatInputSection *ib) {
213 if (verboseDiagnostics)
214 ++equalsVariableCount;
215 assert(ia->relocs.size() == ib->relocs.size());
216 auto f = [this](const Relocation &ra, const Relocation &rb) {
217 // We already filtered out mismatching values/addends in equalsConstant.
218 if (ra.referent == rb.referent)
219 return true;
220 const ConcatInputSection *isecA, *isecB;
221 if (isa<Symbol *>(Val: ra.referent)) {
222 // Matching DylibSymbols are already filtered out by the
223 // identical-referent check above. Non-matching DylibSymbols were filtered
224 // out in equalsConstant(). So we can safely cast to Defined here.
225 const auto *da = cast<Defined>(Val: cast<Symbol *>(Val: ra.referent));
226 const auto *db = cast<Defined>(Val: cast<Symbol *>(Val: rb.referent));
227 if (da->isAbsolute())
228 return true;
229 isecA = dyn_cast<ConcatInputSection>(Val: da->isec());
230 if (!isecA)
231 return true; // literal sections were checked in equalsConstant.
232 isecB = cast<ConcatInputSection>(Val: db->isec());
233 } else {
234 const auto *sa = cast<InputSection *>(Val: ra.referent);
235 const auto *sb = cast<InputSection *>(Val: rb.referent);
236 isecA = dyn_cast<ConcatInputSection>(Val: sa);
237 if (!isecA)
238 return true;
239 isecB = cast<ConcatInputSection>(Val: sb);
240 }
241 return isecA->icfEqClass[icfPass % 2] == isecB->icfEqClass[icfPass % 2];
242 };
243 if (!llvm::equal(LRange: ia->relocs, RRange: ib->relocs, P: f))
244 return false;
245
246 // Compare unwind info equivalence classes.
247 auto hasUnwind = [](Defined *d) { return d->unwindEntry() != nullptr; };
248 const auto *itA = llvm::find_if(Range: ia->symbols, P: hasUnwind);
249 if (itA == ia->symbols.end())
250 return true;
251 const Defined *da = *itA;
252 // equalsConstant() guarantees that both sections have unwind info.
253 const Defined *db = *llvm::find_if(Range: ib->symbols, P: hasUnwind);
254 return da->unwindEntry()->icfEqClass[icfPass % 2] ==
255 db->unwindEntry()->icfEqClass[icfPass % 2];
256}
257
258// Find the first InputSection after BEGIN whose equivalence class differs
259size_t ICF::findBoundary(size_t begin, size_t end) {
260 uint64_t beginHash = icfInputs[begin]->icfEqClass[icfPass % 2];
261 for (size_t i = begin + 1; i < end; ++i)
262 if (beginHash != icfInputs[i]->icfEqClass[icfPass % 2])
263 return i;
264 return end;
265}
266
267// Invoke FUNC on subranges with matching equivalence class
268void ICF::forEachClassRange(size_t begin, size_t end,
269 llvm::function_ref<void(size_t, size_t)> func) {
270 while (begin < end) {
271 size_t mid = findBoundary(begin, end);
272 func(begin, mid);
273 begin = mid;
274 }
275}
276
277// Find or create a symbol at offset 0 in the given section
278static Symbol *getThunkTargetSymbol(ConcatInputSection *isec) {
279 for (Symbol *sym : isec->symbols)
280 if (auto *d = dyn_cast<Defined>(Val: sym))
281 if (d->value == 0)
282 return sym;
283
284 std::string thunkName;
285 if (isec->symbols.size() == 0)
286 thunkName = isec->getName().str() + ".icf.0";
287 else
288 thunkName = isec->getName().str() + "icf.thunk.target" +
289 std::to_string(val: icfThunkCounter++);
290
291 // If no symbol found at offset 0, create one
292 auto *sym = make<Defined>(args&: thunkName, /*file=*/args: nullptr, args&: isec,
293 /*value=*/args: 0, /*size=*/args: isec->getSize(),
294 /*isWeakDef=*/args: false, /*isExternal=*/args: false,
295 /*isPrivateExtern=*/args: false, /*isThumb=*/args: false,
296 /*isReferencedDynamically=*/args: false,
297 /*noDeadStrip=*/args: false);
298 isec->symbols.push_back(NewVal: sym);
299 return sym;
300}
301
302// Given a range of identical icfInputs, replace address significant functions
303// with a thunk that is just a direct branch to the first function in the
304// series. This way we keep only one main body of the function but we still
305// retain the address uniqueness of relevant functions by having them be a
306// direct branch thunk rather than containing a full copy of the actual function
307// body.
308void ICF::applySafeThunksToRange(size_t begin, size_t end) {
309 // When creating a unique ICF thunk, use the first section as the section that
310 // all thunks will branch to.
311 ConcatInputSection *masterIsec = icfInputs[begin];
312
313 // If the first section is not address significant, sorting guarantees that
314 // there are no address significant functions. So we can skip this range.
315 if (!masterIsec->keepUnique)
316 return;
317
318 // Skip anything that is not a code section.
319 if (!isCodeSection(masterIsec))
320 return;
321
322 // If the functions we're dealing with are smaller than the thunk size, then
323 // just leave them all as-is - creating thunks would be a net loss.
324 uint32_t thunkSize = target->getICFSafeThunkSize();
325 if (masterIsec->data.size() <= thunkSize)
326 return;
327
328 // Get the symbol that all thunks will branch to.
329 Symbol *masterSym = getThunkTargetSymbol(isec: masterIsec);
330
331 for (size_t i = begin + 1; i < end; ++i) {
332 ConcatInputSection *isec = icfInputs[i];
333 // When we're done processing keepUnique entries, we can stop. Sorting
334 // guaratees that all keepUnique will be at the front.
335 if (!isec->keepUnique)
336 break;
337
338 ConcatInputSection *thunk =
339 makeSyntheticInputSection(segName: isec->getSegName(), sectName: isec->getName());
340 // A thunk-folded cold function has a cold thunk.
341 thunk->isCold = isec->isCold;
342 addInputSection(inputSection: thunk);
343
344 target->initICFSafeThunkBody(thunk, targetSym: masterSym);
345 thunk->foldIdentical(redundant: isec, foldKind: Symbol::ICFFoldKind::Thunk);
346
347 // Since we're folding the target function into a thunk, we need to adjust
348 // the symbols that now got relocated from the target function to the thunk.
349 // Since the thunk is only one branch, we move all symbols to offset 0 and
350 // make sure that the size of all non-zero-size symbols is equal to the size
351 // of the branch.
352 for (auto *sym : thunk->symbols) {
353 sym->value = 0;
354 if (sym->size != 0)
355 sym->size = thunkSize;
356 }
357 }
358}
359
360// Split icfInputs into shards, then parallelize invocation of FUNC on subranges
361// with matching equivalence class
362void ICF::forEachClass(llvm::function_ref<void(size_t, size_t)> func) {
363 // Only use threads when the benefits outweigh the overhead.
364 const size_t threadingThreshold = 1024;
365 if (icfInputs.size() < threadingThreshold) {
366 forEachClassRange(begin: 0, end: icfInputs.size(), func);
367 ++icfPass;
368 return;
369 }
370
371 // Shard into non-overlapping intervals, and call FUNC in parallel. The
372 // sharding must be completed before any calls to FUNC are made so that FUNC
373 // can modify the InputSection in its shard without causing data races.
374 const size_t shards = 256;
375 size_t step = icfInputs.size() / shards;
376 size_t boundaries[shards + 1];
377 boundaries[0] = 0;
378 boundaries[shards] = icfInputs.size();
379 parallelFor(Begin: 1, End: shards, Fn: [&](size_t i) {
380 boundaries[i] = findBoundary(begin: (i - 1) * step, end: icfInputs.size());
381 });
382 parallelFor(Begin: 1, End: shards + 1, Fn: [&](size_t i) {
383 if (boundaries[i - 1] < boundaries[i]) {
384 forEachClassRange(begin: boundaries[i - 1], end: boundaries[i], func);
385 }
386 });
387 ++icfPass;
388}
389
390void ICF::run() {
391 // Into each origin-section hash, combine all reloc referent section hashes.
392 for (icfPass = 0; icfPass < 2; ++icfPass) {
393 parallelForEach(R&: icfInputs, Fn: [&](ConcatInputSection *isec) {
394 uint32_t hash = isec->icfEqClass[icfPass % 2];
395 for (const Relocation &r : isec->relocs) {
396 if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
397 if (auto *defined = dyn_cast<Defined>(Val: sym)) {
398 if (defined->isec()) {
399 if (auto *referentIsec =
400 dyn_cast<ConcatInputSection>(Val: defined->isec()))
401 hash += defined->value + referentIsec->icfEqClass[icfPass % 2];
402 else
403 hash += defined->isec()->kind() +
404 defined->isec()->getOffset(off: defined->value);
405 } else {
406 hash += defined->value;
407 }
408 } else {
409 // ICF runs before Undefined diags
410 assert(isa<Undefined>(sym) || isa<DylibSymbol>(sym));
411 }
412 }
413 }
414 // Set MSB to 1 to avoid collisions with non-hashed classes.
415 isec->icfEqClass[(icfPass + 1) % 2] = hash | (1ull << 31);
416 });
417 }
418
419 llvm::stable_sort(
420 Range&: icfInputs, C: [](const ConcatInputSection *a, const ConcatInputSection *b) {
421 // When using safe_thunks, ensure that we first sort by icfEqClass and
422 // then by keepUnique (descending). This guarantees that within an
423 // equivalence class, the keepUnique inputs are always first.
424 if (config->icfLevel == ICFLevel::safe_thunks)
425 if (a->icfEqClass[0] == b->icfEqClass[0])
426 return a->keepUnique > b->keepUnique;
427 return a->icfEqClass[0] < b->icfEqClass[0];
428 });
429 forEachClass(func: [&](size_t begin, size_t end) {
430 segregate(begin, end, &ICF::equalsConstant);
431 });
432
433 // Split equivalence groups by comparing relocations until convergence
434 do {
435 icfRepeat = false;
436 forEachClass(func: [&](size_t begin, size_t end) {
437 segregate(begin, end, &ICF::equalsVariable);
438 });
439 } while (icfRepeat);
440 log(msg: "ICF needed " + Twine(icfPass) + " iterations");
441 if (verboseDiagnostics) {
442 log(msg: "equalsConstant() called " + Twine(equalsConstantCount) + " times");
443 log(msg: "equalsVariable() called " + Twine(equalsVariableCount) + " times");
444 }
445
446 // When using safe_thunks, we need to create thunks for all keepUnique
447 // functions that can be deduplicated. Since we're creating / adding new
448 // InputSections, we can't paralellize this.
449 if (config->icfLevel == ICFLevel::safe_thunks)
450 forEachClassRange(begin: 0, end: icfInputs.size(), func: [&](size_t begin, size_t end) {
451 applySafeThunksToRange(begin, end);
452 });
453
454 // Fold sections within equivalence classes
455 forEachClass(func: [&](size_t begin, size_t end) {
456 if (end - begin < 2)
457 return;
458 // For ICF level safe_thunks, replace keepUnique function bodies with
459 // thunks. For all other ICF levels, directly merge the functions.
460
461 ConcatInputSection *beginIsec = icfInputs[begin];
462 bool useSafeThunks =
463 config->icfLevel == ICFLevel::safe_thunks && isCodeSection(beginIsec);
464 for (size_t i = begin + 1; i < end; ++i) {
465 // Skip keepUnique inputs when using safe_thunks (already handled above)
466 if (useSafeThunks && icfInputs[i]->keepUnique) {
467 // Assert keepUnique sections are either small or replaced with thunks.
468 assert(!icfInputs[i]->live ||
469 icfInputs[i]->data.size() <= target->getICFSafeThunkSize());
470 assert(!icfInputs[i]->replacement ||
471 icfInputs[i]->replacement->data.size() ==
472 target->getICFSafeThunkSize());
473 continue;
474 }
475 beginIsec->foldIdentical(redundant: icfInputs[i]);
476 // Make sure we don't fold hot code into cold regions.
477 if (!icfInputs[i]->isCold)
478 beginIsec->isCold = false;
479 }
480 });
481}
482
483// Split an equivalence class into smaller classes.
484void ICF::segregate(size_t begin, size_t end, EqualsFn equals) {
485 while (begin < end) {
486 // Divide [begin, end) into two. Let mid be the start index of the
487 // second group.
488 auto bound = std::stable_partition(
489 first: icfInputs.begin() + begin + 1, last: icfInputs.begin() + end,
490 pred: [&](ConcatInputSection *isec) {
491 return (this->*equals)(icfInputs[begin], isec);
492 });
493 size_t mid = bound - icfInputs.begin();
494
495 // Split [begin, end) into [begin, mid) and [mid, end). We use mid as an
496 // equivalence class ID because every group ends with a unique index.
497 for (size_t i = begin; i < mid; ++i)
498 icfInputs[i]->icfEqClass[(icfPass + 1) % 2] = mid;
499
500 // If we created a group, we need to iterate the main loop again.
501 if (mid != end)
502 icfRepeat = true;
503
504 begin = mid;
505 }
506}
507
508void macho::markSymAsAddrSig(Symbol *s) {
509 if (auto *d = dyn_cast_or_null<Defined>(Val: s))
510 if (d->isec())
511 d->isec()->keepUnique = true;
512}
513
514void macho::markAddrSigSymbols() {
515 TimeTraceScope timeScope("Mark addrsig symbols");
516 for (InputFile *file : inputFiles) {
517 ObjFile *obj = dyn_cast<ObjFile>(Val: file);
518 if (!obj)
519 continue;
520
521 Section *addrSigSection = obj->addrSigSection;
522 if (!addrSigSection) {
523 for (Symbol *sym : obj->symbols)
524 markSymAsAddrSig(s: sym);
525 continue;
526 }
527 assert(addrSigSection->subsections.size() == 1);
528
529 const InputSection *isec = addrSigSection->subsections[0].isec;
530
531 for (const Relocation &r : isec->relocs) {
532 if (auto *sym = r.referent.dyn_cast<Symbol *>())
533 markSymAsAddrSig(s: sym);
534 else
535 error(msg: toString(isec) + ": unexpected section relocation");
536 }
537 }
538}
539
540// Given a symbol that was folded into a thunk, return the symbol pointing to
541// the actual body of the function. We use this approach rather than storing the
542// needed info in the Defined itself in order to minimize memory usage.
543Defined *macho::getBodyForThunkFoldedSym(Defined *foldedSym) {
544 assert(isa<ConcatInputSection>(foldedSym->originalIsec) &&
545 "thunk-folded ICF symbol expected to be on a ConcatInputSection");
546 // foldedSec is the InputSection that was marked as deleted upon fold
547 ConcatInputSection *foldedSec =
548 cast<ConcatInputSection>(Val: foldedSym->originalIsec);
549
550 // thunkBody is the actual live thunk, containing the code that branches to
551 // the actual body of the function.
552 InputSection *thunkBody = foldedSec->replacement;
553
554 // The symbol of the merged body of the function that the thunk jumps to. This
555 // will end up in the final binary.
556 Symbol *targetSym = target->getThunkBranchTarget(thunk: thunkBody);
557
558 return cast<Defined>(Val: targetSym);
559}
560void macho::foldIdenticalSections(bool onlyCfStrings) {
561 TimeTraceScope timeScope("Fold Identical Code Sections");
562 // The ICF equivalence-class segregation algorithm relies on pre-computed
563 // hashes of InputSection::data for the ConcatOutputSection::inputs and all
564 // sections referenced by their relocs. We could recursively traverse the
565 // relocs to find every referenced InputSection, but that precludes easy
566 // parallelization. Therefore, we hash every InputSection here where we have
567 // them all accessible as simple vectors.
568
569 // If an InputSection is ineligible for ICF, we give it a unique ID to force
570 // it into an unfoldable singleton equivalence class. Begin the unique-ID
571 // space at inputSections.size(), so that it will never intersect with
572 // equivalence-class IDs which begin at 0. Since hashes & unique IDs never
573 // coexist with equivalence-class IDs, this is not necessary, but might help
574 // someone keep the numbers straight in case we ever need to debug the
575 // ICF::segregate()
576 std::vector<ConcatInputSection *> foldable;
577 uint64_t icfUniqueID = inputSections.size();
578 // Reset the thunk counter for each run of ICF.
579 icfThunkCounter = 0;
580 for (ConcatInputSection *isec : inputSections) {
581 bool isFoldableWithAddendsRemoved = isCfStringSection(isec) ||
582 isClassRefsSection(isec) ||
583 isSelRefsSection(isec);
584 // NOTE: __objc_selrefs is typically marked as no_dead_strip by MC, but we
585 // can still fold it.
586 bool hasFoldableFlags = (isSelRefsSection(isec) ||
587 sectionType(flags: isec->getFlags()) == MachO::S_REGULAR);
588
589 bool isCodeSec = isCodeSection(isec);
590
591 // Determine whether keepUnique forbids folding this section.
592 // - __cfstring / __objc_classrefs / __objc_selrefs always fold
593 // regardless of keepUnique. Compilers currently emit over-broad
594 // __llvm_addrsig entries that can cover non-address-significant data
595 // symbols in these sections; ld64 coalesces them unconditionally, and
596 // we match that behavior.
597 // - Under safe_thunks, keepUnique code sections still fold; the
598 // safe_thunks logic is applied later at merge time based on the
599 // keepUnique flag.
600 // - Otherwise, keepUnique sections are not foldable.
601 // Happens to match isFoldableWithAddendsRemoved today, but expresses a
602 // different intent (ld64's coalescing semantics, not addend stripping),
603 // so the two may diverge as either list grows.
604 bool isUnconditionallyCoalescedData = isFoldableWithAddendsRemoved;
605 bool isSafeThunksCode =
606 config->icfLevel == ICFLevel::safe_thunks && isCodeSec;
607 bool keepUniqueAllowsFolding =
608 !isec->keepUnique || isUnconditionallyCoalescedData || isSafeThunksCode;
609
610 // FIXME: consider non-code __text sections as foldable?
611 bool isFoldable = (!onlyCfStrings || isCfStringSection(isec)) &&
612 (isCodeSec || isFoldableWithAddendsRemoved ||
613 isGccExceptTabSection(isec)) &&
614 keepUniqueAllowsFolding && !isec->hasAltEntry &&
615 !isec->shouldOmitFromOutput() && hasFoldableFlags;
616 if (isFoldable) {
617 foldable.push_back(x: isec);
618 for (Defined *d : isec->symbols)
619 if (d->unwindEntry())
620 foldable.push_back(x: d->unwindEntry());
621
622 // Some sections have embedded addends that foil ICF's hashing / equality
623 // checks. (We can ignore embedded addends when doing ICF because the same
624 // information gets recorded in our Reloc structs.) We therefore create a
625 // mutable copy of the section data and zero out the embedded addends
626 // before performing any hashing / equality checks.
627 if (isFoldableWithAddendsRemoved) {
628 // We have to do this copying serially as the BumpPtrAllocator is not
629 // thread-safe. FIXME: Make a thread-safe allocator.
630 MutableArrayRef<uint8_t> copy = isec->data.copy(A&: bAlloc());
631 for (const Relocation &r : isec->relocs)
632 target->relocateOne(loc: copy.data() + r.offset, r, /*va=*/0,
633 /*relocVA=*/0);
634 isec->data = copy;
635 }
636 } else if (!isEhFrameSection(isec)) {
637 // EH frames are gathered as foldables from unwindEntry above; give a
638 // unique ID to everything else.
639 isec->icfEqClass[0] = ++icfUniqueID;
640 }
641 }
642 parallelForEach(R&: foldable, Fn: [](ConcatInputSection *isec) {
643 assert(isec->icfEqClass[0] == 0); // don't overwrite a unique ID!
644 // Turn-on the top bit to guarantee that valid hashes have no collisions
645 // with the small-integer unique IDs for ICF-ineligible sections
646 isec->icfEqClass[0] = xxh3_64bits(data: isec->data) | (1ull << 31);
647 });
648 // Now that every input section is either hashed or marked as unique, run the
649 // segregation algorithm to detect foldable subsections.
650 ICF(foldable).run();
651}
652