blob: c9a7911fc624696e5bf157e0beb1de55b5c90d67 [file] [log] [blame]
Rui Ueyama0b289522016-02-25 18:43:51 +00001//===- ICF.cpp ------------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Identical Code Folding is a feature to merge sections not by name (which
11// is regular comdat handling) but by contents. If two non-writable sections
12// have the same data, relocations, attributes, etc., then the two
13// are considered identical and merged by the linker. This optimization
14// makes outputs smaller.
15//
16// ICF is theoretically a problem of reducing graphs by merging as many
17// identical subgraphs as possible if we consider sections as vertices and
18// relocations as edges. It may sound simple, but it is a bit more
19// complicated than you might think. The order of processing sections
20// matters because merging two sections can make other sections, whose
21// relocations now point to the same section, mergeable. Graphs may contain
22// cycles. We need a sophisticated algorithm to do this properly and
23// efficiently.
24//
25// What we do in this file is this. We split sections into groups. Sections
26// in the same group are considered identical.
27//
28// We begin by optimistically putting all sections into a single equivalence
29// class. Then we apply a series of checks that split this initial
30// equivalence class into more and more refined equivalence classes based on
31// the properties by which a section can be distinguished.
32//
33// We begin by checking that the section contents and flags are the
34// same. This only needs to be done once since these properties don't depend
35// on the current equivalence class assignment.
36//
37// Then we split the equivalence classes based on checking that their
38// relocations are the same, where relocation targets are compared by their
39// equivalence class, not the concrete section. This may need to be done
40// multiple times because as the equivalence classes are refined, two
41// sections that had a relocation target in the same equivalence class may
42// now target different equivalence classes, and hence these two sections
43// must be put in different equivalence classes (whereas in the previous
44// iteration they were not since the relocation target was the same.)
45//
46// Our algorithm is smart enough to merge the following mutually-recursive
47// functions.
48//
49// void foo() { bar(); }
50// void bar() { foo(); }
51//
52// This algorithm is so-called "optimistic" algorithm described in
53// http://research.google.com/pubs/pub36912.html. (Note that what GNU
54// gold implemented is different from the optimistic algorithm.)
55//
56//===----------------------------------------------------------------------===//
57
58#include "ICF.h"
59#include "Config.h"
60#include "OutputSections.h"
61#include "SymbolTable.h"
62
63#include "llvm/ADT/Hashing.h"
64#include "llvm/Object/ELF.h"
65#include "llvm/Support/ELF.h"
66#include "llvm/Support/raw_ostream.h"
Rui Ueyamaa05134e2016-11-19 20:15:55 +000067#include <algorithm>
Rui Ueyama0b289522016-02-25 18:43:51 +000068
69using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000070using namespace lld::elf;
Rui Ueyama0b289522016-02-25 18:43:51 +000071using namespace llvm;
72using namespace llvm::ELF;
73using namespace llvm::object;
74
75namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000076namespace elf {
Rui Ueyama0b289522016-02-25 18:43:51 +000077template <class ELFT> class ICF {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000078 typedef typename ELFT::Shdr Elf_Shdr;
79 typedef typename ELFT::Sym Elf_Sym;
80 typedef typename ELFT::uint uintX_t;
Rui Ueyama0b289522016-02-25 18:43:51 +000081 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
82
83 using Comparator = std::function<bool(const InputSection<ELFT> *,
84 const InputSection<ELFT> *)>;
85
86public:
Rui Ueyama4f8d21f2016-05-02 19:30:42 +000087 void run();
Rui Ueyama0b289522016-02-25 18:43:51 +000088
89private:
90 uint64_t NextId = 1;
91
92 static void setLive(SymbolTable<ELFT> *S);
93 static uint64_t relSize(InputSection<ELFT> *S);
94 static uint64_t getHash(InputSection<ELFT> *S);
95 static bool isEligible(InputSectionBase<ELFT> *Sec);
Rui Ueyama4f8d21f2016-05-02 19:30:42 +000096 static std::vector<InputSection<ELFT> *> getSections();
Rui Ueyama0b289522016-02-25 18:43:51 +000097
Rui Ueyamae2dfbc12016-11-19 23:14:23 +000098 void segregate(MutableArrayRef<InputSection<ELFT> *> Arr, Comparator Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +000099
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000100 void
101 forEachGroup(std::vector<InputSection<ELFT> *> &V,
102 std::function<void(MutableArrayRef<InputSection<ELFT> *>)> Fn);
Rui Ueyama0b289522016-02-25 18:43:51 +0000103
104 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000105 static bool relocationEq(ArrayRef<RelTy> RA, ArrayRef<RelTy> RB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000106
107 template <class RelTy>
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000108 static bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RA,
109 const InputSection<ELFT> *B, ArrayRef<RelTy> RB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000110
111 static bool equalsConstant(const InputSection<ELFT> *A,
112 const InputSection<ELFT> *B);
113
114 static bool equalsVariable(const InputSection<ELFT> *A,
115 const InputSection<ELFT> *B);
116};
117}
118}
119
Rui Ueyama0b289522016-02-25 18:43:51 +0000120// Returns a hash value for S. Note that the information about
121// relocation targets is not included in the hash value.
122template <class ELFT> uint64_t ICF<ELFT>::getHash(InputSection<ELFT> *S) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000123 return hash_combine(S->Flags, S->getSize(), S->NumRelocations);
Rui Ueyama0b289522016-02-25 18:43:51 +0000124}
125
126// Returns true if Sec is subject of ICF.
127template <class ELFT> bool ICF<ELFT>::isEligible(InputSectionBase<ELFT> *Sec) {
Rafael Espindola8f9026b2016-11-08 18:23:02 +0000128 if (!Sec->Live)
Rui Ueyama0b289522016-02-25 18:43:51 +0000129 return false;
130 auto *S = dyn_cast<InputSection<ELFT>>(Sec);
131 if (!S)
132 return false;
133
134 // .init and .fini contains instructions that must be executed to
135 // initialize and finalize the process. They cannot and should not
136 // be merged.
Rafael Espindola042a3f22016-09-08 14:06:08 +0000137 StringRef Name = S->Name;
Rui Ueyama0b289522016-02-25 18:43:51 +0000138 if (Name == ".init" || Name == ".fini")
139 return false;
140
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000141 return (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE);
Rui Ueyama0b289522016-02-25 18:43:51 +0000142}
143
144template <class ELFT>
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000145std::vector<InputSection<ELFT> *> ICF<ELFT>::getSections() {
Rui Ueyama0b289522016-02-25 18:43:51 +0000146 std::vector<InputSection<ELFT> *> V;
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000147 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
148 if (isEligible(S))
149 V.push_back(cast<InputSection<ELFT>>(S));
Rui Ueyama0b289522016-02-25 18:43:51 +0000150 return V;
151}
152
Rui Ueyama0b289522016-02-25 18:43:51 +0000153// All sections between Begin and End must have the same group ID before
154// you call this function. This function compare sections between Begin
155// and End using Eq and assign new group IDs for new groups.
156template <class ELFT>
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000157void ICF<ELFT>::segregate(MutableArrayRef<InputSection<ELFT> *> Arr,
Rui Ueyama0b289522016-02-25 18:43:51 +0000158 Comparator Eq) {
159 // This loop rearranges [Begin, End) so that all sections that are
160 // equal in terms of Eq are contiguous. The algorithm is quadratic in
161 // the worst case, but that is not an issue in practice because the
162 // number of distinct sections in [Begin, End) is usually very small.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000163 InputSection<ELFT> **I = Arr.begin();
Rui Ueyama0b289522016-02-25 18:43:51 +0000164 for (;;) {
165 InputSection<ELFT> *Head = *I;
166 auto Bound = std::stable_partition(
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000167 I + 1, Arr.end(), [&](InputSection<ELFT> *S) { return Eq(Head, S); });
168 if (Bound == Arr.end())
Rui Ueyama0b289522016-02-25 18:43:51 +0000169 return;
170 uint64_t Id = NextId++;
171 for (; I != Bound; ++I)
172 (*I)->GroupId = Id;
173 }
174}
175
176template <class ELFT>
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000177void ICF<ELFT>::forEachGroup(
178 std::vector<InputSection<ELFT> *> &V,
179 std::function<void(MutableArrayRef<InputSection<ELFT> *>)> Fn) {
Rui Ueyamaeec23eb2016-02-26 15:13:24 +0000180 for (InputSection<ELFT> **I = V.data(), **E = I + V.size(); I != E;) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000181 InputSection<ELFT> *Head = *I;
182 auto Bound = std::find_if(I + 1, E, [&](InputSection<ELFT> *S) {
183 return S->GroupId != Head->GroupId;
184 });
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000185 Fn({I, Bound});
Rui Ueyama0b289522016-02-25 18:43:51 +0000186 I = Bound;
187 }
188}
189
190// Compare two lists of relocations.
191template <class ELFT>
192template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000193bool ICF<ELFT>::relocationEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000194 auto Eq = [](const RelTy &A, const RelTy &B) {
195 return A.r_offset == B.r_offset &&
196 A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) &&
197 getAddend<ELFT>(A) == getAddend<ELFT>(B);
198 };
199
200 return RelsA.size() == RelsB.size() &&
201 std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000202}
203
204// Compare "non-moving" part of two InputSections, namely everything
205// except relocation targets.
206template <class ELFT>
207bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A,
208 const InputSection<ELFT> *B) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000209 if (A->NumRelocations != B->NumRelocations)
Rui Ueyama0b289522016-02-25 18:43:51 +0000210 return false;
211
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000212 if (A->AreRelocsRela) {
213 if (!relocationEq(A->relas(), B->relas()))
214 return false;
215 } else {
216 if (!relocationEq(A->rels(), B->rels()))
217 return false;
Rui Ueyama0b289522016-02-25 18:43:51 +0000218 }
219
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000220 return A->Flags == B->Flags && A->getSize() == B->getSize() &&
Rafael Espindola58139d12016-10-25 16:14:25 +0000221 A->Data == B->Data;
Rui Ueyama0b289522016-02-25 18:43:51 +0000222}
223
224template <class ELFT>
225template <class RelTy>
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000226bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
227 const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) {
228 auto Eq = [&](const RelTy &RA, const RelTy &RB) {
229 SymbolBody &SA = A->File->getRelocTargetSym(RA);
230 SymbolBody &SB = B->File->getRelocTargetSym(RB);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000231 if (&SA == &SB)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000232 return true;
Rui Ueyama0b289522016-02-25 18:43:51 +0000233
234 // Or, the symbols should be pointing to the same section
235 // in terms of the group ID.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000236 auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA);
237 auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000238 if (!DA || !DB)
239 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000240 if (DA->Value != DB->Value)
Rui Ueyama0b289522016-02-25 18:43:51 +0000241 return false;
242 InputSection<ELFT> *X = dyn_cast<InputSection<ELFT>>(DA->Section);
243 InputSection<ELFT> *Y = dyn_cast<InputSection<ELFT>>(DB->Section);
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000244 return X && Y && X->GroupId && X->GroupId == Y->GroupId;
245 };
246
247 return std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000248}
249
250// Compare "moving" part of two InputSections, namely relocation targets.
251template <class ELFT>
252bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A,
253 const InputSection<ELFT> *B) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000254 if (A->AreRelocsRela)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000255 return variableEq(A, A->relas(), B, B->relas());
256 return variableEq(A, A->rels(), B, B->rels());
Rui Ueyama0b289522016-02-25 18:43:51 +0000257}
258
259// The main function of ICF.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000260template <class ELFT> void ICF<ELFT>::run() {
Rui Ueyama0b289522016-02-25 18:43:51 +0000261 // Initially, we use hash values as section group IDs. Therefore,
262 // if two sections have the same ID, they are likely (but not
263 // guaranteed) to have the same static contents in terms of ICF.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000264 std::vector<InputSection<ELFT> *> Sections = getSections();
265 for (InputSection<ELFT> *S : Sections)
Rui Ueyama0b289522016-02-25 18:43:51 +0000266 // Set MSB on to avoid collisions with serial group IDs
267 S->GroupId = getHash(S) | (uint64_t(1) << 63);
268
269 // From now on, sections in V are ordered so that sections in
270 // the same group are consecutive in the vector.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000271 std::stable_sort(Sections.begin(), Sections.end(),
Rui Ueyama0b289522016-02-25 18:43:51 +0000272 [](InputSection<ELFT> *A, InputSection<ELFT> *B) {
Petr Hosek901948a2016-08-22 18:53:09 +0000273 if (A->GroupId != B->GroupId)
274 return A->GroupId < B->GroupId;
275 // Within a group, put the highest alignment
276 // requirement first, so that's the one we'll keep.
277 return B->Alignment < A->Alignment;
Rui Ueyama0b289522016-02-25 18:43:51 +0000278 });
279
280 // Compare static contents and assign unique IDs for each static content.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000281 forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
282 segregate(V, equalsConstant);
283 });
Rui Ueyama0b289522016-02-25 18:43:51 +0000284
285 // Split groups by comparing relocations until we get a convergence.
286 int Cnt = 1;
287 for (;;) {
288 ++Cnt;
289 uint64_t Id = NextId;
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000290 forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
291 segregate(V, equalsVariable);
292 });
Rui Ueyama0b289522016-02-25 18:43:51 +0000293 if (Id == NextId)
294 break;
295 }
Rui Ueyama10bd2832016-02-25 18:56:01 +0000296 log("ICF needed " + Twine(Cnt) + " iterations.");
Rui Ueyama0b289522016-02-25 18:43:51 +0000297
298 // Merge sections in the same group.
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000299 forEachGroup(Sections, [&](MutableArrayRef<InputSection<ELFT> *> V) {
300 InputSection<ELFT> *Head = V[0];
Rafael Espindola042a3f22016-09-08 14:06:08 +0000301 log("selected " + Head->Name);
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000302 for (InputSection<ELFT> *S : V.slice(1)) {
Rafael Espindola042a3f22016-09-08 14:06:08 +0000303 log(" removed " + S->Name);
Rui Ueyama0b289522016-02-25 18:43:51 +0000304 Head->replace(S);
305 }
Rui Ueyamae2dfbc12016-11-19 23:14:23 +0000306 });
Rui Ueyama0b289522016-02-25 18:43:51 +0000307}
308
309// ICF entry point function.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000310template <class ELFT> void elf::doIcf() { ICF<ELFT>().run(); }
Rui Ueyama0b289522016-02-25 18:43:51 +0000311
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000312template void elf::doIcf<ELF32LE>();
313template void elf::doIcf<ELF32BE>();
314template void elf::doIcf<ELF64LE>();
315template void elf::doIcf<ELF64BE>();