Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 1 | //===- 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" |
| 67 | |
| 68 | using namespace lld; |
| 69 | using namespace lld::elf2; |
| 70 | using namespace llvm; |
| 71 | using namespace llvm::ELF; |
| 72 | using namespace llvm::object; |
| 73 | |
| 74 | namespace lld { |
| 75 | namespace elf2 { |
| 76 | template <class ELFT> class ICF { |
| 77 | typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 78 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 79 | typedef typename ELFFile<ELFT>::uintX_t uintX_t; |
| 80 | typedef Elf_Rel_Impl<ELFT, false> Elf_Rel; |
| 81 | |
| 82 | using Comparator = std::function<bool(const InputSection<ELFT> *, |
| 83 | const InputSection<ELFT> *)>; |
| 84 | |
| 85 | public: |
| 86 | void run(SymbolTable<ELFT> *Symtab); |
| 87 | |
| 88 | private: |
| 89 | uint64_t NextId = 1; |
| 90 | |
| 91 | static void setLive(SymbolTable<ELFT> *S); |
| 92 | static uint64_t relSize(InputSection<ELFT> *S); |
| 93 | static uint64_t getHash(InputSection<ELFT> *S); |
| 94 | static bool isEligible(InputSectionBase<ELFT> *Sec); |
| 95 | static std::vector<InputSection<ELFT> *> getSections(SymbolTable<ELFT> *S); |
| 96 | static SymbolBody *getSymbol(const InputSection<ELFT> *Sec, |
| 97 | const Elf_Rel *Rel); |
| 98 | |
| 99 | void segregate(InputSection<ELFT> **Begin, InputSection<ELFT> **End, |
| 100 | Comparator Eq); |
| 101 | |
| 102 | void forEachGroup(std::vector<InputSection<ELFT> *> &V, Comparator Eq); |
| 103 | |
| 104 | template <class RelTy> |
| 105 | static bool relocationEq(iterator_range<const RelTy *> RA, |
| 106 | iterator_range<const RelTy *> RB); |
| 107 | |
| 108 | template <class RelTy> |
| 109 | static bool variableEq(const InputSection<ELFT> *A, |
| 110 | const InputSection<ELFT> *B, |
| 111 | iterator_range<const RelTy *> RA, |
| 112 | iterator_range<const RelTy *> RB); |
| 113 | |
| 114 | static bool equalsConstant(const InputSection<ELFT> *A, |
| 115 | const InputSection<ELFT> *B); |
| 116 | |
| 117 | static bool equalsVariable(const InputSection<ELFT> *A, |
| 118 | const InputSection<ELFT> *B); |
| 119 | }; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Returns a hash seed for relocation sections for S. |
| 124 | template <class ELFT> uint64_t ICF<ELFT>::relSize(InputSection<ELFT> *S) { |
| 125 | uint64_t Ret = 0; |
| 126 | for (const Elf_Shdr *H : S->RelocSections) |
| 127 | Ret += H->sh_size; |
| 128 | return Ret; |
| 129 | } |
| 130 | |
| 131 | // Returns a hash value for S. Note that the information about |
| 132 | // relocation targets is not included in the hash value. |
| 133 | template <class ELFT> uint64_t ICF<ELFT>::getHash(InputSection<ELFT> *S) { |
| 134 | uint64_t Flags = S->getSectionHdr()->sh_flags; |
| 135 | uint64_t H = hash_combine(Flags, S->getSize()); |
| 136 | if (S->RelocSections.empty()) |
| 137 | return H; |
| 138 | return hash_combine(H, relSize(S)); |
| 139 | } |
| 140 | |
| 141 | // Returns true if Sec is subject of ICF. |
| 142 | template <class ELFT> bool ICF<ELFT>::isEligible(InputSectionBase<ELFT> *Sec) { |
| 143 | if (!Sec || Sec == InputSection<ELFT>::Discarded || !Sec->Live) |
| 144 | return false; |
| 145 | auto *S = dyn_cast<InputSection<ELFT>>(Sec); |
| 146 | if (!S) |
| 147 | return false; |
| 148 | |
| 149 | // .init and .fini contains instructions that must be executed to |
| 150 | // initialize and finalize the process. They cannot and should not |
| 151 | // be merged. |
| 152 | StringRef Name = S->getSectionName(); |
| 153 | if (Name == ".init" || Name == ".fini") |
| 154 | return false; |
| 155 | |
| 156 | const Elf_Shdr &H = *S->getSectionHdr(); |
| 157 | return (H.sh_flags & SHF_ALLOC) && (~H.sh_flags & SHF_WRITE); |
| 158 | } |
| 159 | |
| 160 | template <class ELFT> |
| 161 | std::vector<InputSection<ELFT> *> |
| 162 | ICF<ELFT>::getSections(SymbolTable<ELFT> *Symtab) { |
| 163 | std::vector<InputSection<ELFT> *> V; |
| 164 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab->getObjectFiles()) |
| 165 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 166 | if (isEligible(S)) |
| 167 | V.push_back(cast<InputSection<ELFT>>(S)); |
| 168 | return V; |
| 169 | } |
| 170 | |
| 171 | template <class ELFT> |
| 172 | SymbolBody *ICF<ELFT>::getSymbol(const InputSection<ELFT> *Sec, |
| 173 | const Elf_Rel *Rel) { |
| 174 | uint32_t SymIdx = Rel->getSymbol(Config->Mips64EL); |
| 175 | return Sec->File->getSymbolBody(SymIdx); |
| 176 | } |
| 177 | |
| 178 | // All sections between Begin and End must have the same group ID before |
| 179 | // you call this function. This function compare sections between Begin |
| 180 | // and End using Eq and assign new group IDs for new groups. |
| 181 | template <class ELFT> |
| 182 | void ICF<ELFT>::segregate(InputSection<ELFT> **Begin, InputSection<ELFT> **End, |
| 183 | Comparator Eq) { |
| 184 | // This loop rearranges [Begin, End) so that all sections that are |
| 185 | // equal in terms of Eq are contiguous. The algorithm is quadratic in |
| 186 | // the worst case, but that is not an issue in practice because the |
| 187 | // number of distinct sections in [Begin, End) is usually very small. |
| 188 | InputSection<ELFT> **I = Begin; |
| 189 | for (;;) { |
| 190 | InputSection<ELFT> *Head = *I; |
| 191 | auto Bound = std::stable_partition( |
| 192 | I + 1, End, [&](InputSection<ELFT> *S) { return Eq(Head, S); }); |
| 193 | if (Bound == End) |
| 194 | return; |
| 195 | uint64_t Id = NextId++; |
| 196 | for (; I != Bound; ++I) |
| 197 | (*I)->GroupId = Id; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | template <class ELFT> |
| 202 | void ICF<ELFT>::forEachGroup(std::vector<InputSection<ELFT> *> &V, |
| 203 | Comparator Eq) { |
| 204 | for (auto I = V.begin(), E = V.end(); I != E;) { |
| 205 | InputSection<ELFT> *Head = *I; |
| 206 | auto Bound = std::find_if(I + 1, E, [&](InputSection<ELFT> *S) { |
| 207 | return S->GroupId != Head->GroupId; |
| 208 | }); |
| 209 | segregate(&*I, &*Bound, Eq); |
| 210 | I = Bound; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Compare two lists of relocations. |
| 215 | template <class ELFT> |
| 216 | template <class RelTy> |
| 217 | bool ICF<ELFT>::relocationEq(iterator_range<const RelTy *> RelsA, |
| 218 | iterator_range<const RelTy *> RelsB) { |
| 219 | const RelTy *IA = RelsA.begin(); |
| 220 | const RelTy *EA = RelsA.end(); |
| 221 | const RelTy *IB = RelsB.begin(); |
| 222 | const RelTy *EB = RelsB.end(); |
| 223 | if (EA - IA != EB - IB) |
| 224 | return false; |
| 225 | for (; IA != EA; ++IA, ++IB) |
| 226 | if (IA->r_offset != IB->r_offset || |
| 227 | IA->getType(Config->Mips64EL) != IB->getType(Config->Mips64EL) || |
| 228 | getAddend<ELFT>(*IA) != getAddend<ELFT>(*IB)) |
| 229 | return false; |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | // Compare "non-moving" part of two InputSections, namely everything |
| 234 | // except relocation targets. |
| 235 | template <class ELFT> |
| 236 | bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A, |
| 237 | const InputSection<ELFT> *B) { |
| 238 | if (A->RelocSections.size() != B->RelocSections.size()) |
| 239 | return false; |
| 240 | |
| 241 | for (size_t I = 0, E = A->RelocSections.size(); I != E; ++I) { |
| 242 | const Elf_Shdr *RA = A->RelocSections[I]; |
| 243 | const Elf_Shdr *RB = B->RelocSections[I]; |
| 244 | ELFFile<ELFT> &FileA = A->File->getObj(); |
| 245 | ELFFile<ELFT> &FileB = B->File->getObj(); |
| 246 | if (RA->sh_type == SHT_RELA) { |
| 247 | if (!relocationEq(FileA.relas(RA), FileB.relas(RB))) |
| 248 | return false; |
| 249 | } else { |
| 250 | if (!relocationEq(FileA.rels(RA), FileB.rels(RB))) |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return A->getSectionHdr()->sh_flags == B->getSectionHdr()->sh_flags && |
| 256 | A->getSize() == B->getSize() && |
| 257 | A->getSectionData() == B->getSectionData(); |
| 258 | } |
| 259 | |
| 260 | template <class ELFT> |
| 261 | template <class RelTy> |
| 262 | bool ICF<ELFT>::variableEq(const InputSection<ELFT> *A, |
| 263 | const InputSection<ELFT> *B, |
| 264 | iterator_range<const RelTy *> RelsA, |
| 265 | iterator_range<const RelTy *> RelsB) { |
| 266 | const RelTy *IA = RelsA.begin(); |
| 267 | const RelTy *EA = RelsA.end(); |
| 268 | const RelTy *IB = RelsB.begin(); |
| 269 | for (; IA != EA; ++IA, ++IB) { |
Rui Ueyama | bca6eb8 | 2016-02-26 00:10:01 +0000 | [diff] [blame^] | 270 | // If both IA and IB are pointing to the same local symbol, |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 271 | // this "if" condition must be true. |
| 272 | if (A->File == B->File && |
| 273 | IA->getSymbol(Config->Mips64EL) == IB->getSymbol(Config->Mips64EL)) |
| 274 | continue; |
| 275 | |
Rui Ueyama | bca6eb8 | 2016-02-26 00:10:01 +0000 | [diff] [blame^] | 276 | // Otherwise, IA and IB must be pointing to the global symbols. |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 277 | SymbolBody *SA = getSymbol(A, (const Elf_Rel *)IA); |
| 278 | SymbolBody *SB = getSymbol(B, (const Elf_Rel *)IB); |
| 279 | if (!SA || !SB) |
| 280 | return false; |
| 281 | |
| 282 | // The global symbols should be simply the same. |
| 283 | if (SA->repl() == SB->repl()) |
| 284 | continue; |
| 285 | |
| 286 | // Or, the symbols should be pointing to the same section |
| 287 | // in terms of the group ID. |
| 288 | auto *DA = dyn_cast<DefinedRegular<ELFT>>(SA->repl()); |
| 289 | auto *DB = dyn_cast<DefinedRegular<ELFT>>(SB->repl()); |
| 290 | if (!DA || !DB) |
| 291 | return false; |
| 292 | if (DA->Sym.st_value != DB->Sym.st_value) |
| 293 | return false; |
| 294 | InputSection<ELFT> *X = dyn_cast<InputSection<ELFT>>(DA->Section); |
| 295 | InputSection<ELFT> *Y = dyn_cast<InputSection<ELFT>>(DB->Section); |
| 296 | if (X && Y && X->GroupId && X->GroupId == Y->GroupId) |
| 297 | continue; |
| 298 | return false; |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | // Compare "moving" part of two InputSections, namely relocation targets. |
| 304 | template <class ELFT> |
| 305 | bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A, |
| 306 | const InputSection<ELFT> *B) { |
| 307 | for (size_t I = 0, E = A->RelocSections.size(); I != E; ++I) { |
| 308 | const Elf_Shdr *RA = A->RelocSections[I]; |
| 309 | const Elf_Shdr *RB = B->RelocSections[I]; |
| 310 | ELFFile<ELFT> &FileA = A->File->getObj(); |
| 311 | ELFFile<ELFT> &FileB = B->File->getObj(); |
| 312 | if (RA->sh_type == SHT_RELA) { |
| 313 | if (!variableEq(A, B, FileA.relas(RA), FileB.relas(RB))) |
| 314 | return false; |
| 315 | } else { |
| 316 | if (!variableEq(A, B, FileA.rels(RA), FileB.rels(RB))) |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | // The main function of ICF. |
| 324 | template <class ELFT> void ICF<ELFT>::run(SymbolTable<ELFT> *Symtab) { |
| 325 | // Initially, we use hash values as section group IDs. Therefore, |
| 326 | // if two sections have the same ID, they are likely (but not |
| 327 | // guaranteed) to have the same static contents in terms of ICF. |
| 328 | std::vector<InputSection<ELFT> *> V = getSections(Symtab); |
| 329 | for (InputSection<ELFT> *S : V) |
| 330 | // Set MSB on to avoid collisions with serial group IDs |
| 331 | S->GroupId = getHash(S) | (uint64_t(1) << 63); |
| 332 | |
| 333 | // From now on, sections in V are ordered so that sections in |
| 334 | // the same group are consecutive in the vector. |
| 335 | std::stable_sort(V.begin(), V.end(), |
| 336 | [](InputSection<ELFT> *A, InputSection<ELFT> *B) { |
| 337 | return A->GroupId < B->GroupId; |
| 338 | }); |
| 339 | |
| 340 | // Compare static contents and assign unique IDs for each static content. |
| 341 | forEachGroup(V, equalsConstant); |
| 342 | |
| 343 | // Split groups by comparing relocations until we get a convergence. |
| 344 | int Cnt = 1; |
| 345 | for (;;) { |
| 346 | ++Cnt; |
| 347 | uint64_t Id = NextId; |
| 348 | forEachGroup(V, equalsVariable); |
| 349 | if (Id == NextId) |
| 350 | break; |
| 351 | } |
Rui Ueyama | 10bd283 | 2016-02-25 18:56:01 +0000 | [diff] [blame] | 352 | log("ICF needed " + Twine(Cnt) + " iterations."); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 353 | |
| 354 | // Merge sections in the same group. |
| 355 | for (auto I = V.begin(), E = V.end(); I != E;) { |
| 356 | InputSection<ELFT> *Head = *I++; |
| 357 | auto Bound = std::find_if(I, E, [&](InputSection<ELFT> *S) { |
| 358 | return Head->GroupId != S->GroupId; |
| 359 | }); |
| 360 | if (I == Bound) |
| 361 | continue; |
Rui Ueyama | 10bd283 | 2016-02-25 18:56:01 +0000 | [diff] [blame] | 362 | log("Selected " + Head->getSectionName()); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 363 | while (I != Bound) { |
| 364 | InputSection<ELFT> *S = *I++; |
Rui Ueyama | 10bd283 | 2016-02-25 18:56:01 +0000 | [diff] [blame] | 365 | log(" Removed " + S->getSectionName()); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 366 | Head->replace(S); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // ICF entry point function. |
| 372 | template <class ELFT> void elf2::doIcf(SymbolTable<ELFT> *Symtab) { |
| 373 | ICF<ELFT>().run(Symtab); |
| 374 | } |
| 375 | |
| 376 | template void elf2::doIcf(SymbolTable<ELF32LE> *); |
| 377 | template void elf2::doIcf(SymbolTable<ELF32BE> *); |
| 378 | template void elf2::doIcf(SymbolTable<ELF64LE> *); |
| 379 | template void elf2::doIcf(SymbolTable<ELF64BE> *); |