blob: 444ce89d8d7befdb27d569d7fd545080e352f40f [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
98 void segregate(InputSection<ELFT> **Begin, InputSection<ELFT> **End,
99 Comparator Eq);
100
101 void forEachGroup(std::vector<InputSection<ELFT> *> &V, Comparator Eq);
102
103 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000104 static bool relocationEq(ArrayRef<RelTy> RA, ArrayRef<RelTy> RB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000105
106 template <class RelTy>
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000107 static bool variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RA,
108 const InputSection<ELFT> *B, ArrayRef<RelTy> RB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000109
110 static bool equalsConstant(const InputSection<ELFT> *A,
111 const InputSection<ELFT> *B);
112
113 static bool equalsVariable(const InputSection<ELFT> *A,
114 const InputSection<ELFT> *B);
115};
116}
117}
118
Rui Ueyama0b289522016-02-25 18:43:51 +0000119// Returns a hash value for S. Note that the information about
120// relocation targets is not included in the hash value.
121template <class ELFT> uint64_t ICF<ELFT>::getHash(InputSection<ELFT> *S) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000122 return hash_combine(S->Flags, S->getSize(), S->NumRelocations);
Rui Ueyama0b289522016-02-25 18:43:51 +0000123}
124
125// Returns true if Sec is subject of ICF.
126template <class ELFT> bool ICF<ELFT>::isEligible(InputSectionBase<ELFT> *Sec) {
Rafael Espindola8f9026b2016-11-08 18:23:02 +0000127 if (!Sec->Live)
Rui Ueyama0b289522016-02-25 18:43:51 +0000128 return false;
129 auto *S = dyn_cast<InputSection<ELFT>>(Sec);
130 if (!S)
131 return false;
132
133 // .init and .fini contains instructions that must be executed to
134 // initialize and finalize the process. They cannot and should not
135 // be merged.
Rafael Espindola042a3f22016-09-08 14:06:08 +0000136 StringRef Name = S->Name;
Rui Ueyama0b289522016-02-25 18:43:51 +0000137 if (Name == ".init" || Name == ".fini")
138 return false;
139
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000140 return (S->Flags & SHF_ALLOC) && !(S->Flags & SHF_WRITE);
Rui Ueyama0b289522016-02-25 18:43:51 +0000141}
142
143template <class ELFT>
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000144std::vector<InputSection<ELFT> *> ICF<ELFT>::getSections() {
Rui Ueyama0b289522016-02-25 18:43:51 +0000145 std::vector<InputSection<ELFT> *> V;
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000146 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
147 if (isEligible(S))
148 V.push_back(cast<InputSection<ELFT>>(S));
Rui Ueyama0b289522016-02-25 18:43:51 +0000149 return V;
150}
151
Rui Ueyama0b289522016-02-25 18:43:51 +0000152// All sections between Begin and End must have the same group ID before
153// you call this function. This function compare sections between Begin
154// and End using Eq and assign new group IDs for new groups.
155template <class ELFT>
156void ICF<ELFT>::segregate(InputSection<ELFT> **Begin, InputSection<ELFT> **End,
157 Comparator Eq) {
158 // This loop rearranges [Begin, End) so that all sections that are
159 // equal in terms of Eq are contiguous. The algorithm is quadratic in
160 // the worst case, but that is not an issue in practice because the
161 // number of distinct sections in [Begin, End) is usually very small.
162 InputSection<ELFT> **I = Begin;
163 for (;;) {
164 InputSection<ELFT> *Head = *I;
165 auto Bound = std::stable_partition(
166 I + 1, End, [&](InputSection<ELFT> *S) { return Eq(Head, S); });
167 if (Bound == End)
168 return;
169 uint64_t Id = NextId++;
170 for (; I != Bound; ++I)
171 (*I)->GroupId = Id;
172 }
173}
174
175template <class ELFT>
176void ICF<ELFT>::forEachGroup(std::vector<InputSection<ELFT> *> &V,
177 Comparator Eq) {
Rui Ueyamaeec23eb2016-02-26 15:13:24 +0000178 for (InputSection<ELFT> **I = V.data(), **E = I + V.size(); I != E;) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000179 InputSection<ELFT> *Head = *I;
180 auto Bound = std::find_if(I + 1, E, [&](InputSection<ELFT> *S) {
181 return S->GroupId != Head->GroupId;
182 });
Rui Ueyamaeec23eb2016-02-26 15:13:24 +0000183 segregate(I, Bound, Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000184 I = Bound;
185 }
186}
187
188// Compare two lists of relocations.
189template <class ELFT>
190template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000191bool ICF<ELFT>::relocationEq(ArrayRef<RelTy> RelsA, ArrayRef<RelTy> RelsB) {
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000192 auto Eq = [](const RelTy &A, const RelTy &B) {
193 return A.r_offset == B.r_offset &&
194 A.getType(Config->Mips64EL) == B.getType(Config->Mips64EL) &&
195 getAddend<ELFT>(A) == getAddend<ELFT>(B);
196 };
197
198 return RelsA.size() == RelsB.size() &&
199 std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000200}
201
202// Compare "non-moving" part of two InputSections, namely everything
203// except relocation targets.
204template <class ELFT>
205bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A,
206 const InputSection<ELFT> *B) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000207 if (A->NumRelocations != B->NumRelocations)
Rui Ueyama0b289522016-02-25 18:43:51 +0000208 return false;
209
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000210 if (A->AreRelocsRela) {
211 if (!relocationEq(A->relas(), B->relas()))
212 return false;
213 } else {
214 if (!relocationEq(A->rels(), B->rels()))
215 return false;
Rui Ueyama0b289522016-02-25 18:43:51 +0000216 }
217
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000218 return A->Flags == B->Flags && A->getSize() == B->getSize() &&
Rafael Espindola58139d12016-10-25 16:14:25 +0000219 A->Data == B->Data;
Rui Ueyama0b289522016-02-25 18:43:51 +0000220}
221
222template <class ELFT>
223template <class RelTy>
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000224bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, ArrayRef<RelTy> RelsA,
225 const InputSection<ELFT> *B, ArrayRef<RelTy> RelsB) {
226 auto Eq = [&](const RelTy &RA, const RelTy &RB) {
227 SymbolBody &SA = A->File->getRelocTargetSym(RA);
228 SymbolBody &SB = B->File->getRelocTargetSym(RB);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000229 if (&SA == &SB)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000230 return true;
Rui Ueyama0b289522016-02-25 18:43:51 +0000231
232 // Or, the symbols should be pointing to the same section
233 // in terms of the group ID.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000234 auto *DA = dyn_cast<DefinedRegular<ELFT>>(&SA);
235 auto *DB = dyn_cast<DefinedRegular<ELFT>>(&SB);
Rui Ueyama0b289522016-02-25 18:43:51 +0000236 if (!DA || !DB)
237 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000238 if (DA->Value != DB->Value)
Rui Ueyama0b289522016-02-25 18:43:51 +0000239 return false;
240 InputSection<ELFT> *X = dyn_cast<InputSection<ELFT>>(DA->Section);
241 InputSection<ELFT> *Y = dyn_cast<InputSection<ELFT>>(DB->Section);
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000242 return X && Y && X->GroupId && X->GroupId == Y->GroupId;
243 };
244
245 return std::equal(RelsA.begin(), RelsA.end(), RelsB.begin(), Eq);
Rui Ueyama0b289522016-02-25 18:43:51 +0000246}
247
248// Compare "moving" part of two InputSections, namely relocation targets.
249template <class ELFT>
250bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A,
251 const InputSection<ELFT> *B) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000252 if (A->AreRelocsRela)
Rui Ueyamaa05134e2016-11-19 20:15:55 +0000253 return variableEq(A, A->relas(), B, B->relas());
254 return variableEq(A, A->rels(), B, B->rels());
Rui Ueyama0b289522016-02-25 18:43:51 +0000255}
256
257// The main function of ICF.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000258template <class ELFT> void ICF<ELFT>::run() {
Rui Ueyama0b289522016-02-25 18:43:51 +0000259 // Initially, we use hash values as section group IDs. Therefore,
260 // if two sections have the same ID, they are likely (but not
261 // guaranteed) to have the same static contents in terms of ICF.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000262 std::vector<InputSection<ELFT> *> V = getSections();
Rui Ueyama0b289522016-02-25 18:43:51 +0000263 for (InputSection<ELFT> *S : V)
264 // Set MSB on to avoid collisions with serial group IDs
265 S->GroupId = getHash(S) | (uint64_t(1) << 63);
266
267 // From now on, sections in V are ordered so that sections in
268 // the same group are consecutive in the vector.
269 std::stable_sort(V.begin(), V.end(),
270 [](InputSection<ELFT> *A, InputSection<ELFT> *B) {
Petr Hosek901948a2016-08-22 18:53:09 +0000271 if (A->GroupId != B->GroupId)
272 return A->GroupId < B->GroupId;
273 // Within a group, put the highest alignment
274 // requirement first, so that's the one we'll keep.
275 return B->Alignment < A->Alignment;
Rui Ueyama0b289522016-02-25 18:43:51 +0000276 });
277
278 // Compare static contents and assign unique IDs for each static content.
279 forEachGroup(V, equalsConstant);
280
281 // Split groups by comparing relocations until we get a convergence.
282 int Cnt = 1;
283 for (;;) {
284 ++Cnt;
285 uint64_t Id = NextId;
286 forEachGroup(V, equalsVariable);
287 if (Id == NextId)
288 break;
289 }
Rui Ueyama10bd2832016-02-25 18:56:01 +0000290 log("ICF needed " + Twine(Cnt) + " iterations.");
Rui Ueyama0b289522016-02-25 18:43:51 +0000291
292 // Merge sections in the same group.
293 for (auto I = V.begin(), E = V.end(); I != E;) {
294 InputSection<ELFT> *Head = *I++;
295 auto Bound = std::find_if(I, E, [&](InputSection<ELFT> *S) {
296 return Head->GroupId != S->GroupId;
297 });
298 if (I == Bound)
299 continue;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000300 log("selected " + Head->Name);
Rui Ueyama0b289522016-02-25 18:43:51 +0000301 while (I != Bound) {
302 InputSection<ELFT> *S = *I++;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000303 log(" removed " + S->Name);
Rui Ueyama0b289522016-02-25 18:43:51 +0000304 Head->replace(S);
305 }
306 }
307}
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>();