blob: 6680f69504124fe311d8d2d49fd3ea63e6c9e853 [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"
Peter Collingbourne676c7cd2016-04-26 23:52:44 +000028#include "Target.h"
Rui Ueyamac4aaed92015-10-22 18:49:53 +000029#include "Writer.h"
30#include "llvm/ADT/STLExtras.h"
31#include "llvm/Object/ELF.h"
32#include <functional>
33#include <vector>
34
35using namespace llvm;
36using namespace llvm::ELF;
37using namespace llvm::object;
38
39using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000040using namespace lld::elf;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000041
Peter Collingbourne676c7cd2016-04-26 23:52:44 +000042// A resolved relocation. The Sec and Offset fields are set if the relocation
43// was resolved to an offset within a section.
44template <class ELFT>
45struct ResolvedReloc {
46 InputSectionBase<ELFT> *Sec;
47 typename ELFT::uint Offset;
48};
49
50template <class ELFT>
51static typename ELFT::uint getAddend(InputSectionBase<ELFT> *Sec,
52 const typename ELFT::Rel &Rel) {
53 return Target->getImplicitAddend(Sec->getSectionData().begin(),
54 Rel.getType(Config->Mips64EL));
55}
56
57template <class ELFT>
58static typename ELFT::uint getAddend(InputSectionBase<ELFT> *Sec,
59 const typename ELFT::Rela &Rel) {
60 return Rel.r_addend;
61}
62
63template <class ELFT, class RelT>
George Rimaraf90d542016-05-02 13:49:42 +000064static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> *Sec,
65 RelT &Rel) {
Peter Collingbourne676c7cd2016-04-26 23:52:44 +000066 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
67 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
68 if (!D || !D->Section)
69 return {nullptr, 0};
70 typename ELFT::uint Offset = D->Value;
71 if (D->isSection())
72 Offset += getAddend(Sec, Rel);
73 return {D->Section->Repl, Offset};
74}
75
George Rimaraf90d542016-05-02 13:49:42 +000076template <class ELFT, class Elf_Shdr>
77static void run(ELFFile<ELFT> &Obj, InputSectionBase<ELFT> *Sec,
78 Elf_Shdr *RelSec, std::function<void(ResolvedReloc<ELFT>)> Fn) {
79 if (RelSec->sh_type == SHT_RELA) {
80 for (const typename ELFT::Rela &RI : Obj.relas(RelSec))
81 Fn(resolveReloc(Sec, RI));
82 } else {
83 for (const typename ELFT::Rel &RI : Obj.rels(RelSec))
84 Fn(resolveReloc(Sec, RI));
85 }
86}
87
Rui Ueyamaf53b1b72016-01-05 16:35:46 +000088// Calls Fn for each section that Sec refers to via relocations.
Rui Ueyamac4aaed92015-10-22 18:49:53 +000089template <class ELFT>
Peter Collingbourne676c7cd2016-04-26 23:52:44 +000090static void forEachSuccessor(InputSection<ELFT> *Sec,
91 std::function<void(ResolvedReloc<ELFT>)> Fn) {
Rafael Espindola197d6a82016-04-22 16:39:59 +000092 ELFFile<ELFT> &Obj = Sec->getFile()->getObj();
George Rimaraf90d542016-05-02 13:49:42 +000093 for (const typename ELFT::Shdr *RelSec : Sec->RelocSections)
94 run(Obj, Sec, RelSec, Fn);
95}
96
Rafael Espindolad89fbca2016-05-05 13:51:14 +000097template <class ELFT>
98static void scanEhFrameSection(EHInputSection<ELFT> &EH,
99 std::function<void(ResolvedReloc<ELFT>)> Fn) {
George Rimaraf90d542016-05-02 13:49:42 +0000100 if (!EH.RelocSection)
101 return;
102 ELFFile<ELFT> &EObj = EH.getFile()->getObj();
103 run<ELFT>(EObj, &EH, EH.RelocSection, [&](ResolvedReloc<ELFT> R) {
104 if (!R.Sec || R.Sec == &InputSection<ELFT>::Discarded)
105 return;
106 if (R.Sec->getSectionHdr()->sh_flags & SHF_EXECINSTR)
107 return;
Rafael Espindolad89fbca2016-05-05 13:51:14 +0000108 Fn({R.Sec, 0});
George Rimaraf90d542016-05-02 13:49:42 +0000109 });
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000110}
111
112// Sections listed below are special because they are used by the loader
113// just by being in an ELF file. They should not be garbage-collected.
114template <class ELFT> static bool isReserved(InputSectionBase<ELFT> *Sec) {
115 switch (Sec->getSectionHdr()->sh_type) {
116 case SHT_FINI_ARRAY:
117 case SHT_INIT_ARRAY:
118 case SHT_NOTE:
119 case SHT_PREINIT_ARRAY:
120 return true;
121 default:
122 StringRef S = Sec->getSectionName();
George Rimar12737b72016-02-25 08:40:26 +0000123
124 // We do not want to reclaim sections if they can be referred
125 // by __start_* and __stop_* symbols.
126 if (isValidCIdentifier(S))
127 return true;
128
Davide Italianobcbfedc2015-12-24 09:52:11 +0000129 return S.startswith(".ctors") || S.startswith(".dtors") ||
130 S.startswith(".init") || S.startswith(".fini") ||
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000131 S.startswith(".jcr");
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000132 }
133}
134
Rui Ueyama7924fb82015-12-24 09:50:22 +0000135// This is the main function of the garbage collector.
136// Starting from GC-root sections, this function visits all reachable
137// sections to set their "Live" bits.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000138template <class ELFT> void elf::markLive() {
Rafael Espindola0b9531c2016-04-22 22:09:35 +0000139 typedef typename ELFT::uint uintX_t;
Rui Ueyama92ce0e92015-11-12 19:42:43 +0000140 SmallVector<InputSection<ELFT> *, 256> Q;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000141
Peter Collingbourne676c7cd2016-04-26 23:52:44 +0000142 auto Enqueue = [&](ResolvedReloc<ELFT> R) {
143 if (!R.Sec)
Rafael Espindola0b9531c2016-04-22 22:09:35 +0000144 return;
Peter Collingbourne676c7cd2016-04-26 23:52:44 +0000145 if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(R.Sec)) {
Rui Ueyama3ea87272016-05-22 00:13:04 +0000146 std::pair<SectionPiece *, uintX_t> T = MS->getRangeAndSize(R.Offset);
147 T.first->Live = true;
Rafael Espindola0b9531c2016-04-22 22:09:35 +0000148 }
Peter Collingbourne676c7cd2016-04-26 23:52:44 +0000149 if (R.Sec->Live)
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000150 return;
Peter Collingbourne676c7cd2016-04-26 23:52:44 +0000151 R.Sec->Live = true;
152 if (InputSection<ELFT> *S = dyn_cast<InputSection<ELFT>>(R.Sec))
Rui Ueyama92ce0e92015-11-12 19:42:43 +0000153 Q.push_back(S);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000154 };
155
Peter Collingbourne4f952702016-05-01 04:55:03 +0000156 auto MarkSymbol = [&](const SymbolBody *Sym) {
157 if (auto *D = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym))
158 Enqueue({D->Section, D->Value});
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000159 };
160
161 // Add GC root symbols.
Rafael Espindola38c67a22016-04-15 14:41:56 +0000162 if (Config->EntrySym)
Peter Collingbourne4f952702016-05-01 04:55:03 +0000163 MarkSymbol(Config->EntrySym->body());
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000164 MarkSymbol(Symtab<ELFT>::X->find(Config->Init));
165 MarkSymbol(Symtab<ELFT>::X->find(Config->Fini));
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000166 for (StringRef S : Config->Undefined)
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000167 MarkSymbol(Symtab<ELFT>::X->find(S));
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000168
169 // Preserve externally-visible symbols if the symbols defined by this
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000170 // file can interrupt other ELF file's symbols at runtime.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000171 for (const Symbol *S : Symtab<ELFT>::X->getSymbols())
Rafael Espindola1b65ad12016-04-26 16:26:45 +0000172 if (S->includeInDynsym())
Peter Collingbourne4f952702016-05-01 04:55:03 +0000173 MarkSymbol(S->body());
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000174
George Rimar481c2ce2016-02-23 07:47:54 +0000175 // Preserve special sections and those which are specified in linker
176 // script KEEP command.
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000177 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
178 Symtab<ELFT>::X->getObjectFiles())
Rui Ueyama74f598b2015-12-24 10:14:05 +0000179 for (InputSectionBase<ELFT> *Sec : F->getSections())
George Rimaraf90d542016-05-02 13:49:42 +0000180 if (Sec && Sec != &InputSection<ELFT>::Discarded) {
181 // .eh_frame is always marked as live now, but also it can reference to
182 // sections that contain personality. We preserve all non-text sections
183 // referred by .eh_frame here.
184 if (auto *EH = dyn_cast_or_null<EHInputSection<ELFT>>(Sec))
Rafael Espindolad89fbca2016-05-05 13:51:14 +0000185 scanEhFrameSection<ELFT>(*EH, Enqueue);
Rui Ueyama07320e42016-04-20 20:13:41 +0000186 if (isReserved(Sec) || Script<ELFT>::X->shouldKeep(Sec))
Peter Collingbourne676c7cd2016-04-26 23:52:44 +0000187 Enqueue({Sec, 0});
George Rimaraf90d542016-05-02 13:49:42 +0000188 }
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000189
190 // Mark all reachable sections.
191 while (!Q.empty())
Rui Ueyama92ce0e92015-11-12 19:42:43 +0000192 forEachSuccessor<ELFT>(Q.pop_back_val(), Enqueue);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000193}
194
Rui Ueyama4f8d21f2016-05-02 19:30:42 +0000195template void elf::markLive<ELF32LE>();
196template void elf::markLive<ELF32BE>();
197template void elf::markLive<ELF64LE>();
198template void elf::markLive<ELF64BE>();