blob: 0fdf512e8a31fddc12e613377a9ee330ce5ad449 [file] [log] [blame]
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001//===- MarkLive.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// This file implements --gc-sections, which is a feature to remove unused
11// sections from output. Unused sections are sections that are not reachable
12// from known GC-root symbols or sections. Naturally the feature is
13// implemented as a mark-sweep garbage collector.
14//
15// Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
16// by default. Starting with GC-root symbols or sections, markLive function
17// defined in this file visits all reachable sections to set their Live
18// bits. Writer will then ignore sections whose Live bits are off, so that
Rui Ueyamaf53b1b72016-01-05 16:35:46 +000019// such sections are not included into output.
Rui Ueyamac4aaed92015-10-22 18:49:53 +000020//
21//===----------------------------------------------------------------------===//
22
23#include "InputSection.h"
George Rimar481c2ce2016-02-23 07:47:54 +000024#include "LinkerScript.h"
Rui Ueyamac4aaed92015-10-22 18:49:53 +000025#include "OutputSections.h"
26#include "SymbolTable.h"
27#include "Symbols.h"
28#include "Writer.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/Object/ELF.h"
31#include <functional>
32#include <vector>
33
34using namespace llvm;
35using namespace llvm::ELF;
36using namespace llvm::object;
37
38using namespace lld;
39using namespace lld::elf2;
40
Rui Ueyamaf53b1b72016-01-05 16:35:46 +000041// Calls Fn for each section that Sec refers to via relocations.
Rui Ueyamac4aaed92015-10-22 18:49:53 +000042template <class ELFT>
43static void forEachSuccessor(InputSection<ELFT> *Sec,
44 std::function<void(InputSectionBase<ELFT> *)> Fn) {
Rui Ueyama12504642015-10-27 21:51:13 +000045 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
46 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000047 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama12504642015-10-27 21:51:13 +000048
49 ELFFile<ELFT> &Obj = Sec->getFile()->getObj();
Rui Ueyamac4aaed92015-10-22 18:49:53 +000050 for (const Elf_Shdr *RelSec : Sec->RelocSections) {
Rui Ueyama12504642015-10-27 21:51:13 +000051 if (RelSec->sh_type == SHT_RELA) {
52 for (const Elf_Rela &RI : Obj.relas(RelSec))
53 if (InputSectionBase<ELFT> *Succ = Sec->getRelocTarget(RI))
54 Fn(Succ);
55 } else {
56 for (const Elf_Rel &RI : Obj.rels(RelSec))
57 if (InputSectionBase<ELFT> *Succ = Sec->getRelocTarget(RI))
58 Fn(Succ);
59 }
Rui Ueyamac4aaed92015-10-22 18:49:53 +000060 }
61}
62
63// Sections listed below are special because they are used by the loader
64// just by being in an ELF file. They should not be garbage-collected.
65template <class ELFT> static bool isReserved(InputSectionBase<ELFT> *Sec) {
66 switch (Sec->getSectionHdr()->sh_type) {
67 case SHT_FINI_ARRAY:
68 case SHT_INIT_ARRAY:
69 case SHT_NOTE:
70 case SHT_PREINIT_ARRAY:
71 return true;
72 default:
73 StringRef S = Sec->getSectionName();
Davide Italianobcbfedc2015-12-24 09:52:11 +000074 return S.startswith(".ctors") || S.startswith(".dtors") ||
75 S.startswith(".init") || S.startswith(".fini") ||
Rafael Espindola8ea46e02015-11-09 17:44:10 +000076 S.startswith(".jcr");
Rui Ueyamac4aaed92015-10-22 18:49:53 +000077 }
78}
79
Rui Ueyama7924fb82015-12-24 09:50:22 +000080// This is the main function of the garbage collector.
81// Starting from GC-root sections, this function visits all reachable
82// sections to set their "Live" bits.
Rui Ueyama83cd6e02016-01-06 20:11:55 +000083template <class ELFT> void elf2::markLive(SymbolTable<ELFT> *Symtab) {
Rui Ueyama92ce0e92015-11-12 19:42:43 +000084 SmallVector<InputSection<ELFT> *, 256> Q;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000085
86 auto Enqueue = [&](InputSectionBase<ELFT> *Sec) {
87 if (!Sec || Sec->Live)
88 return;
89 Sec->Live = true;
Rui Ueyama92ce0e92015-11-12 19:42:43 +000090 if (InputSection<ELFT> *S = dyn_cast<InputSection<ELFT>>(Sec))
91 Q.push_back(S);
Rui Ueyamac4aaed92015-10-22 18:49:53 +000092 };
93
94 auto MarkSymbol = [&](SymbolBody *Sym) {
95 if (Sym)
Rui Ueyama2beabc92015-10-22 23:10:25 +000096 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(Sym->repl()))
Rafael Espindola02ce26a2015-12-24 14:22:24 +000097 Enqueue(D->Section);
Rui Ueyamac4aaed92015-10-22 18:49:53 +000098 };
99
100 // Add GC root symbols.
101 MarkSymbol(Config->EntrySym);
102 MarkSymbol(Symtab->find(Config->Init));
103 MarkSymbol(Symtab->find(Config->Fini));
104 for (StringRef S : Config->Undefined)
105 MarkSymbol(Symtab->find(S));
106
107 // Preserve externally-visible symbols if the symbols defined by this
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000108 // file can interrupt other ELF file's symbols at runtime.
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000109 if (Config->Shared || Config->ExportDynamic) {
110 for (const std::pair<StringRef, Symbol *> &P : Symtab->getSymbols()) {
111 SymbolBody *B = P.second->Body;
112 if (B->getVisibility() == STV_DEFAULT)
113 MarkSymbol(B);
114 }
115 }
116
George Rimar481c2ce2016-02-23 07:47:54 +0000117 // Preserve special sections and those which are specified in linker
118 // script KEEP command.
Rui Ueyama74f598b2015-12-24 10:14:05 +0000119 for (const std::unique_ptr<ObjectFile<ELFT>> &F : Symtab->getObjectFiles())
120 for (InputSectionBase<ELFT> *Sec : F->getSections())
George Rimar481c2ce2016-02-23 07:47:54 +0000121 if (Sec && Sec != &InputSection<ELFT>::Discarded)
122 if (isReserved(Sec) || Script->shouldKeep<ELFT>(Sec))
123 Enqueue(Sec);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000124
125 // Mark all reachable sections.
126 while (!Q.empty())
Rui Ueyama92ce0e92015-11-12 19:42:43 +0000127 forEachSuccessor<ELFT>(Q.pop_back_val(), Enqueue);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000128}
129
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000130template void elf2::markLive<ELF32LE>(SymbolTable<ELF32LE> *);
131template void elf2::markLive<ELF32BE>(SymbolTable<ELF32BE> *);
132template void elf2::markLive<ELF64LE>(SymbolTable<ELF64LE> *);
133template void elf2::markLive<ELF64BE>(SymbolTable<ELF64BE> *);