xref: /llvm-project/lld/MachO/ICF.cpp (revision e04fde193bc2acbaf3ece851479fbd9928c1e280)
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 #include "UnwindInfoSection.h"
16 
17 #include "lld/Common/CommonLinkerContext.h"
18 #include "llvm/Support/LEB128.h"
19 #include "llvm/Support/Parallel.h"
20 #include "llvm/Support/TimeProfiler.h"
21 #include "llvm/Support/xxhash.h"
22 
23 #include <atomic>
24 
25 using namespace llvm;
26 using namespace lld;
27 using namespace lld::macho;
28 
29 static constexpr bool verboseDiagnostics = false;
30 
31 class ICF {
32 public:
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 
60 ICF::ICF(std::vector<ConcatInputSection *> &inputs) {
61   icfInputs.assign(inputs.begin(), 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.
96 bool 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 Reloc &ra, const Reloc &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 *>(ra.referent) != isa<Symbol *>(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 *>(ra.referent)) {
126       const auto *sa = cast<Symbol *>(ra.referent);
127       const auto *sb = cast<Symbol *>(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>(sa) || isa<Undefined>(sa))
133         return sa == sb && ra.addend == rb.addend;
134       assert(isa<Defined>(sa));
135       const auto *da = cast<Defined>(sa);
136       const auto *db = cast<Defined>(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 *>(ra.referent);
147       isecB = cast<InputSection *>(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>(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 *>(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(valueA) == isecB->getOffset(valueB) &&
175              ra.addend == rb.addend;
176     else {
177       assert(valueA == 0 && valueB == 0);
178       // For section relocs, we compare the content at the section offset.
179       return isecA->getOffset(ra.addend) == isecB->getOffset(rb.addend);
180     }
181   };
182   return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
183                     f);
184 }
185 
186 // Compare the "moving" parts of two ConcatInputSections -- i.e. everything not
187 // handled by equalsConstant().
188 bool ICF::equalsVariable(const ConcatInputSection *ia,
189                          const ConcatInputSection *ib) {
190   if (verboseDiagnostics)
191     ++equalsVariableCount;
192   assert(ia->relocs.size() == ib->relocs.size());
193   auto f = [this](const Reloc &ra, const Reloc &rb) {
194     // We already filtered out mismatching values/addends in equalsConstant.
195     if (ra.referent == rb.referent)
196       return true;
197     const ConcatInputSection *isecA, *isecB;
198     if (isa<Symbol *>(ra.referent)) {
199       // Matching DylibSymbols are already filtered out by the
200       // identical-referent check above. Non-matching DylibSymbols were filtered
201       // out in equalsConstant(). So we can safely cast to Defined here.
202       const auto *da = cast<Defined>(cast<Symbol *>(ra.referent));
203       const auto *db = cast<Defined>(cast<Symbol *>(rb.referent));
204       if (da->isAbsolute())
205         return true;
206       isecA = dyn_cast<ConcatInputSection>(da->isec());
207       if (!isecA)
208         return true; // literal sections were checked in equalsConstant.
209       isecB = cast<ConcatInputSection>(db->isec());
210     } else {
211       const auto *sa = cast<InputSection *>(ra.referent);
212       const auto *sb = cast<InputSection *>(rb.referent);
213       isecA = dyn_cast<ConcatInputSection>(sa);
214       if (!isecA)
215         return true;
216       isecB = cast<ConcatInputSection>(sb);
217     }
218     return isecA->icfEqClass[icfPass % 2] == isecB->icfEqClass[icfPass % 2];
219   };
220   if (!std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(), f))
221     return false;
222 
223   // If there are symbols with associated unwind info, check that the unwind
224   // info matches. For simplicity, we only handle the case where there are only
225   // symbols at offset zero within the section (which is typically the case with
226   // .subsections_via_symbols.)
227   auto hasUnwind = [](Defined *d) { return d->unwindEntry() != nullptr; };
228   const auto *itA = llvm::find_if(ia->symbols, hasUnwind);
229   const auto *itB = llvm::find_if(ib->symbols, hasUnwind);
230   if (itA == ia->symbols.end())
231     return itB == ib->symbols.end();
232   if (itB == ib->symbols.end())
233     return false;
234   const Defined *da = *itA;
235   const Defined *db = *itB;
236   if (da->unwindEntry()->icfEqClass[icfPass % 2] !=
237           db->unwindEntry()->icfEqClass[icfPass % 2] ||
238       da->value != 0 || db->value != 0)
239     return false;
240   auto isZero = [](Defined *d) { return d->value == 0; };
241   return std::find_if_not(std::next(itA), ia->symbols.end(), isZero) ==
242              ia->symbols.end() &&
243          std::find_if_not(std::next(itB), ib->symbols.end(), isZero) ==
244              ib->symbols.end();
245 }
246 
247 // Find the first InputSection after BEGIN whose equivalence class differs
248 size_t ICF::findBoundary(size_t begin, size_t end) {
249   uint64_t beginHash = icfInputs[begin]->icfEqClass[icfPass % 2];
250   for (size_t i = begin + 1; i < end; ++i)
251     if (beginHash != icfInputs[i]->icfEqClass[icfPass % 2])
252       return i;
253   return end;
254 }
255 
256 // Invoke FUNC on subranges with matching equivalence class
257 void ICF::forEachClassRange(size_t begin, size_t end,
258                             llvm::function_ref<void(size_t, size_t)> func) {
259   while (begin < end) {
260     size_t mid = findBoundary(begin, end);
261     func(begin, mid);
262     begin = mid;
263   }
264 }
265 
266 // Given a range of identical icfInputs, replace address significant functions
267 // with a thunk that is just a direct branch to the first function in the
268 // series. This way we keep only one main body of the function but we still
269 // retain the address uniqueness of relevant functions by having them be a
270 // direct branch thunk rather than containing a full copy of the actual function
271 // body.
272 void ICF::applySafeThunksToRange(size_t begin, size_t end) {
273   // If the functions we're dealing with are smaller than the thunk size, then
274   // just leave them all as-is - creating thunks would be a net loss.
275   uint32_t thunkSize = target->getICFSafeThunkSize();
276   if (icfInputs[begin]->data.size() <= thunkSize)
277     return;
278 
279   // When creating a unique ICF thunk, use the first section as the section that
280   // all thunks will branch to.
281   ConcatInputSection *masterIsec = icfInputs[begin];
282 
283   for (size_t i = begin + 1; i < end; ++i) {
284     ConcatInputSection *isec = icfInputs[i];
285     // When we're done processing keepUnique entries, we can stop. Sorting
286     // guaratees that all keepUnique will be at the front.
287     if (!isec->keepUnique)
288       break;
289 
290     ConcatInputSection *thunk =
291         makeSyntheticInputSection(isec->getSegName(), isec->getName());
292     addInputSection(thunk);
293 
294     target->initICFSafeThunkBody(thunk, masterIsec);
295     thunk->foldIdentical(isec, Symbol::ICFFoldKind::Thunk);
296 
297     // Since we're folding the target function into a thunk, we need to adjust
298     // the symbols that now got relocated from the target function to the thunk.
299     // Since the thunk is only one branch, we move all symbols to offset 0 and
300     // make sure that the size of all non-zero-size symbols is equal to the size
301     // of the branch.
302     for (auto *sym : thunk->symbols) {
303       sym->value = 0;
304       if (sym->size != 0)
305         sym->size = thunkSize;
306     }
307   }
308 }
309 
310 // Split icfInputs into shards, then parallelize invocation of FUNC on subranges
311 // with matching equivalence class
312 void ICF::forEachClass(llvm::function_ref<void(size_t, size_t)> func) {
313   // Only use threads when the benefits outweigh the overhead.
314   const size_t threadingThreshold = 1024;
315   if (icfInputs.size() < threadingThreshold) {
316     forEachClassRange(0, icfInputs.size(), func);
317     ++icfPass;
318     return;
319   }
320 
321   // Shard into non-overlapping intervals, and call FUNC in parallel.  The
322   // sharding must be completed before any calls to FUNC are made so that FUNC
323   // can modify the InputSection in its shard without causing data races.
324   const size_t shards = 256;
325   size_t step = icfInputs.size() / shards;
326   size_t boundaries[shards + 1];
327   boundaries[0] = 0;
328   boundaries[shards] = icfInputs.size();
329   parallelFor(1, shards, [&](size_t i) {
330     boundaries[i] = findBoundary((i - 1) * step, icfInputs.size());
331   });
332   parallelFor(1, shards + 1, [&](size_t i) {
333     if (boundaries[i - 1] < boundaries[i]) {
334       forEachClassRange(boundaries[i - 1], boundaries[i], func);
335     }
336   });
337   ++icfPass;
338 }
339 
340 void ICF::run() {
341   // Into each origin-section hash, combine all reloc referent section hashes.
342   for (icfPass = 0; icfPass < 2; ++icfPass) {
343     parallelForEach(icfInputs, [&](ConcatInputSection *isec) {
344       uint32_t hash = isec->icfEqClass[icfPass % 2];
345       for (const Reloc &r : isec->relocs) {
346         if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
347           if (auto *defined = dyn_cast<Defined>(sym)) {
348             if (defined->isec()) {
349               if (auto *referentIsec =
350                       dyn_cast<ConcatInputSection>(defined->isec()))
351                 hash += defined->value + referentIsec->icfEqClass[icfPass % 2];
352               else
353                 hash += defined->isec()->kind() +
354                         defined->isec()->getOffset(defined->value);
355             } else {
356               hash += defined->value;
357             }
358           } else {
359             // ICF runs before Undefined diags
360             assert(isa<Undefined>(sym) || isa<DylibSymbol>(sym));
361           }
362         }
363       }
364       // Set MSB to 1 to avoid collisions with non-hashed classes.
365       isec->icfEqClass[(icfPass + 1) % 2] = hash | (1ull << 31);
366     });
367   }
368 
369   llvm::stable_sort(
370       icfInputs, [](const ConcatInputSection *a, const ConcatInputSection *b) {
371         // When using safe_thunks, ensure that we first sort by icfEqClass and
372         // then by keepUnique (descending). This guarantees that within an
373         // equivalence class, the keepUnique inputs are always first.
374         if (config->icfLevel == ICFLevel::safe_thunks)
375           if (a->icfEqClass[0] == b->icfEqClass[0])
376             return a->keepUnique > b->keepUnique;
377         return a->icfEqClass[0] < b->icfEqClass[0];
378       });
379   forEachClass([&](size_t begin, size_t end) {
380     segregate(begin, end, &ICF::equalsConstant);
381   });
382 
383   // Split equivalence groups by comparing relocations until convergence
384   do {
385     icfRepeat = false;
386     forEachClass([&](size_t begin, size_t end) {
387       segregate(begin, end, &ICF::equalsVariable);
388     });
389   } while (icfRepeat);
390   log("ICF needed " + Twine(icfPass) + " iterations");
391   if (verboseDiagnostics) {
392     log("equalsConstant() called " + Twine(equalsConstantCount) + " times");
393     log("equalsVariable() called " + Twine(equalsVariableCount) + " times");
394   }
395 
396   // When using safe_thunks, we need to create thunks for all keepUnique
397   // functions that can be deduplicated. Since we're creating / adding new
398   // InputSections, we can't paralellize this.
399   if (config->icfLevel == ICFLevel::safe_thunks)
400     forEachClassRange(0, icfInputs.size(), [&](size_t begin, size_t end) {
401       applySafeThunksToRange(begin, end);
402     });
403 
404   // Fold sections within equivalence classes
405   forEachClass([&](size_t begin, size_t end) {
406     if (end - begin < 2)
407       return;
408     bool useSafeThunks = config->icfLevel == ICFLevel::safe_thunks;
409 
410     // For ICF level safe_thunks, replace keepUnique function bodies with
411     // thunks. For all other ICF levles, directly merge the functions.
412 
413     ConcatInputSection *beginIsec = icfInputs[begin];
414     for (size_t i = begin + 1; i < end; ++i) {
415       // Skip keepUnique inputs when using safe_thunks (already handeled above)
416       if (useSafeThunks && icfInputs[i]->keepUnique) {
417         // Assert keepUnique sections are either small or replaced with thunks.
418         assert(!icfInputs[i]->live ||
419                icfInputs[i]->data.size() <= target->getICFSafeThunkSize());
420         assert(!icfInputs[i]->replacement ||
421                icfInputs[i]->replacement->data.size() ==
422                    target->getICFSafeThunkSize());
423         continue;
424       }
425       beginIsec->foldIdentical(icfInputs[i]);
426     }
427   });
428 }
429 
430 // Split an equivalence class into smaller classes.
431 void ICF::segregate(size_t begin, size_t end, EqualsFn equals) {
432   while (begin < end) {
433     // Divide [begin, end) into two. Let mid be the start index of the
434     // second group.
435     auto bound = std::stable_partition(
436         icfInputs.begin() + begin + 1, icfInputs.begin() + end,
437         [&](ConcatInputSection *isec) {
438           return (this->*equals)(icfInputs[begin], isec);
439         });
440     size_t mid = bound - icfInputs.begin();
441 
442     // Split [begin, end) into [begin, mid) and [mid, end). We use mid as an
443     // equivalence class ID because every group ends with a unique index.
444     for (size_t i = begin; i < mid; ++i)
445       icfInputs[i]->icfEqClass[(icfPass + 1) % 2] = mid;
446 
447     // If we created a group, we need to iterate the main loop again.
448     if (mid != end)
449       icfRepeat = true;
450 
451     begin = mid;
452   }
453 }
454 
455 void macho::markSymAsAddrSig(Symbol *s) {
456   if (auto *d = dyn_cast_or_null<Defined>(s))
457     if (d->isec())
458       d->isec()->keepUnique = true;
459 }
460 
461 void macho::markAddrSigSymbols() {
462   TimeTraceScope timeScope("Mark addrsig symbols");
463   for (InputFile *file : inputFiles) {
464     ObjFile *obj = dyn_cast<ObjFile>(file);
465     if (!obj)
466       continue;
467 
468     Section *addrSigSection = obj->addrSigSection;
469     if (!addrSigSection)
470       continue;
471     assert(addrSigSection->subsections.size() == 1);
472 
473     const InputSection *isec = addrSigSection->subsections[0].isec;
474 
475     for (const Reloc &r : isec->relocs) {
476       if (auto *sym = r.referent.dyn_cast<Symbol *>())
477         markSymAsAddrSig(sym);
478       else
479         error(toString(isec) + ": unexpected section relocation");
480     }
481   }
482 }
483 
484 // Given a symbol that was folded into a thunk, return the symbol pointing to
485 // the actual body of the function. We use this approach rather than storing the
486 // needed info in the Defined itself in order to minimize memory usage.
487 Defined *macho::getBodyForThunkFoldedSym(Defined *foldedSym) {
488   assert(isa<ConcatInputSection>(foldedSym->originalIsec) &&
489          "thunk-folded ICF symbol expected to be on a ConcatInputSection");
490   // foldedSec is the InputSection that was marked as deleted upon fold
491   ConcatInputSection *foldedSec =
492       cast<ConcatInputSection>(foldedSym->originalIsec);
493 
494   // thunkBody is the actual live thunk, containing the code that branches to
495   // the actual body of the function.
496   InputSection *thunkBody = foldedSec->replacement;
497 
498   // The actual (merged) body of the function that the thunk jumps to. This will
499   // end up in the final binary.
500   InputSection *functionBody = target->getThunkBranchTarget(thunkBody);
501 
502   for (Symbol *sym : functionBody->symbols) {
503     Defined *d = dyn_cast<Defined>(sym);
504     // The symbol needs to be at the start of the InputSection
505     if (d && d->value == 0)
506       return d;
507   }
508 
509   llvm_unreachable("could not find body symbol for ICF-generated thunk");
510 }
511 void macho::foldIdenticalSections(bool onlyCfStrings) {
512   TimeTraceScope timeScope("Fold Identical Code Sections");
513   // The ICF equivalence-class segregation algorithm relies on pre-computed
514   // hashes of InputSection::data for the ConcatOutputSection::inputs and all
515   // sections referenced by their relocs. We could recursively traverse the
516   // relocs to find every referenced InputSection, but that precludes easy
517   // parallelization. Therefore, we hash every InputSection here where we have
518   // them all accessible as simple vectors.
519 
520   // If an InputSection is ineligible for ICF, we give it a unique ID to force
521   // it into an unfoldable singleton equivalence class.  Begin the unique-ID
522   // space at inputSections.size(), so that it will never intersect with
523   // equivalence-class IDs which begin at 0. Since hashes & unique IDs never
524   // coexist with equivalence-class IDs, this is not necessary, but might help
525   // someone keep the numbers straight in case we ever need to debug the
526   // ICF::segregate()
527   std::vector<ConcatInputSection *> foldable;
528   uint64_t icfUniqueID = inputSections.size();
529   for (ConcatInputSection *isec : inputSections) {
530     bool isFoldableWithAddendsRemoved = isCfStringSection(isec) ||
531                                         isClassRefsSection(isec) ||
532                                         isSelRefsSection(isec);
533     // NOTE: __objc_selrefs is typically marked as no_dead_strip by MC, but we
534     // can still fold it.
535     bool hasFoldableFlags = (isSelRefsSection(isec) ||
536                              sectionType(isec->getFlags()) == MachO::S_REGULAR);
537 
538     bool isCodeSec = isCodeSection(isec);
539 
540     // When keepUnique is true, the section is not foldable. Unless we are at
541     // icf level safe_thunks, in which case we still want to fold code sections.
542     // When using safe_thunks we'll apply the safe_thunks logic at merge time
543     // based on the 'keepUnique' flag.
544     bool noUniqueRequirement =
545         !isec->keepUnique ||
546         ((config->icfLevel == ICFLevel::safe_thunks) && isCodeSec);
547 
548     // FIXME: consider non-code __text sections as foldable?
549     bool isFoldable = (!onlyCfStrings || isCfStringSection(isec)) &&
550                       (isCodeSec || isFoldableWithAddendsRemoved ||
551                        isGccExceptTabSection(isec)) &&
552                       noUniqueRequirement && !isec->hasAltEntry &&
553                       !isec->shouldOmitFromOutput() && hasFoldableFlags;
554     if (isFoldable) {
555       foldable.push_back(isec);
556       for (Defined *d : isec->symbols)
557         if (d->unwindEntry())
558           foldable.push_back(d->unwindEntry());
559 
560       // Some sections have embedded addends that foil ICF's hashing / equality
561       // checks. (We can ignore embedded addends when doing ICF because the same
562       // information gets recorded in our Reloc structs.) We therefore create a
563       // mutable copy of the section data and zero out the embedded addends
564       // before performing any hashing / equality checks.
565       if (isFoldableWithAddendsRemoved) {
566         // We have to do this copying serially as the BumpPtrAllocator is not
567         // thread-safe. FIXME: Make a thread-safe allocator.
568         MutableArrayRef<uint8_t> copy = isec->data.copy(bAlloc());
569         for (const Reloc &r : isec->relocs)
570           target->relocateOne(copy.data() + r.offset, r, /*va=*/0,
571                               /*relocVA=*/0);
572         isec->data = copy;
573       }
574     } else if (!isEhFrameSection(isec)) {
575       // EH frames are gathered as foldables from unwindEntry above; give a
576       // unique ID to everything else.
577       isec->icfEqClass[0] = ++icfUniqueID;
578     }
579   }
580   parallelForEach(foldable, [](ConcatInputSection *isec) {
581     assert(isec->icfEqClass[0] == 0); // don't overwrite a unique ID!
582     // Turn-on the top bit to guarantee that valid hashes have no collisions
583     // with the small-integer unique IDs for ICF-ineligible sections
584     isec->icfEqClass[0] = xxh3_64bits(isec->data) | (1ull << 31);
585   });
586   // Now that every input section is either hashed or marked as unique, run the
587   // segregation algorithm to detect foldable subsections.
588   ICF(foldable).run();
589 }
590