]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/OutputSections.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r307894, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / OutputSections.cpp
1 //===- OutputSections.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 #include "OutputSections.h"
11 #include "Config.h"
12 #include "LinkerScript.h"
13 #include "Memory.h"
14 #include "Strings.h"
15 #include "SymbolTable.h"
16 #include "SyntheticSections.h"
17 #include "Target.h"
18 #include "Threads.h"
19 #include "llvm/BinaryFormat/Dwarf.h"
20 #include "llvm/Support/MD5.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/SHA1.h"
23
24 using namespace llvm;
25 using namespace llvm::dwarf;
26 using namespace llvm::object;
27 using namespace llvm::support::endian;
28 using namespace llvm::ELF;
29
30 using namespace lld;
31 using namespace lld::elf;
32
33 uint8_t Out::First;
34 OutputSection *Out::Opd;
35 uint8_t *Out::OpdBuf;
36 PhdrEntry *Out::TlsPhdr;
37 OutputSection *Out::DebugInfo;
38 OutputSection *Out::ElfHeader;
39 OutputSection *Out::ProgramHeaders;
40 OutputSection *Out::PreinitArray;
41 OutputSection *Out::InitArray;
42 OutputSection *Out::FiniArray;
43
44 std::vector<OutputSection *> elf::OutputSections;
45 std::vector<OutputSectionCommand *> elf::OutputSectionCommands;
46
47 uint32_t OutputSection::getPhdrFlags() const {
48   uint32_t Ret = PF_R;
49   if (Flags & SHF_WRITE)
50     Ret |= PF_W;
51   if (Flags & SHF_EXECINSTR)
52     Ret |= PF_X;
53   return Ret;
54 }
55
56 template <class ELFT>
57 void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58   Shdr->sh_entsize = Entsize;
59   Shdr->sh_addralign = Alignment;
60   Shdr->sh_type = Type;
61   Shdr->sh_offset = Offset;
62   Shdr->sh_flags = Flags;
63   Shdr->sh_info = Info;
64   Shdr->sh_link = Link;
65   Shdr->sh_addr = Addr;
66   Shdr->sh_size = Size;
67   Shdr->sh_name = ShName;
68 }
69
70 OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
71     : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
72                   /*Info*/ 0,
73                   /*Link*/ 0),
74       SectionIndex(INT_MAX) {}
75
76 static uint64_t updateOffset(uint64_t Off, InputSection *S) {
77   Off = alignTo(Off, S->Alignment);
78   S->OutSecOff = Off;
79   return Off + S->getSize();
80 }
81
82 void OutputSection::addSection(InputSection *S) {
83   assert(S->Live);
84   Sections.push_back(S);
85   S->Parent = this;
86   this->updateAlignment(S->Alignment);
87
88   // The actual offsets will be computed by assignAddresses. For now, use
89   // crude approximation so that it is at least easy for other code to know the
90   // section order. It is also used to calculate the output section size early
91   // for compressed debug sections.
92   this->Size = updateOffset(Size, S);
93
94   // If this section contains a table of fixed-size entries, sh_entsize
95   // holds the element size. Consequently, if this contains two or more
96   // input sections, all of them must have the same sh_entsize. However,
97   // you can put different types of input sections into one output
98   // sectin by using linker scripts. I don't know what to do here.
99   // Probably we sholuld handle that as an error. But for now we just
100   // pick the largest sh_entsize.
101   this->Entsize = std::max(this->Entsize, S->Entsize);
102 }
103
104 static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) {
105   //  The ELF spec just says
106   // ----------------------------------------------------------------
107   // In the first phase, input sections that match in name, type and
108   // attribute flags should be concatenated into single sections.
109   // ----------------------------------------------------------------
110   //
111   // However, it is clear that at least some flags have to be ignored for
112   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
113   // ignored. We should not have two output .text sections just because one was
114   // in a group and another was not for example.
115   //
116   // It also seems that that wording was a late addition and didn't get the
117   // necessary scrutiny.
118   //
119   // Merging sections with different flags is expected by some users. One
120   // reason is that if one file has
121   //
122   // int *const bar __attribute__((section(".foo"))) = (int *)0;
123   //
124   // gcc with -fPIC will produce a read only .foo section. But if another
125   // file has
126   //
127   // int zed;
128   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
129   //
130   // gcc with -fPIC will produce a read write section.
131   //
132   // Last but not least, when using linker script the merge rules are forced by
133   // the script. Unfortunately, linker scripts are name based. This means that
134   // expressions like *(.foo*) can refer to multiple input sections with
135   // different flags. We cannot put them in different output sections or we
136   // would produce wrong results for
137   //
138   // start = .; *(.foo.*) end = .; *(.bar)
139   //
140   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
141   // another. The problem is that there is no way to layout those output
142   // sections such that the .foo sections are the only thing between the start
143   // and end symbols.
144   //
145   // Given the above issues, we instead merge sections by name and error on
146   // incompatible types and flags.
147
148   uint32_t Alignment = 0;
149   uint64_t Flags = 0;
150   if (Config->Relocatable && (C->Flags & SHF_MERGE)) {
151     Alignment = std::max<uint64_t>(C->Alignment, C->Entsize);
152     Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
153   }
154
155   return SectionKey{OutsecName, Flags, Alignment};
156 }
157
158 OutputSectionFactory::OutputSectionFactory() {}
159
160 static uint64_t getIncompatibleFlags(uint64_t Flags) {
161   return Flags & (SHF_ALLOC | SHF_TLS);
162 }
163
164 // We allow sections of types listed below to merged into a
165 // single progbits section. This is typically done by linker
166 // scripts. Merging nobits and progbits will force disk space
167 // to be allocated for nobits sections. Other ones don't require
168 // any special treatment on top of progbits, so there doesn't
169 // seem to be a harm in merging them.
170 static bool canMergeToProgbits(unsigned Type) {
171   return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY ||
172          Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY ||
173          Type == SHT_NOTE;
174 }
175
176 void elf::reportDiscarded(InputSectionBase *IS) {
177   if (!Config->PrintGcSections)
178     return;
179   message("removing unused section from '" + IS->Name + "' in file '" +
180           IS->File->getName() + "'");
181 }
182
183 void OutputSectionFactory::addInputSec(InputSectionBase *IS,
184                                        StringRef OutsecName) {
185   // Sections with the SHT_GROUP attribute reach here only when the - r option
186   // is given. Such sections define "section groups", and InputFiles.cpp has
187   // dedup'ed section groups by their signatures. For the -r, we want to pass
188   // through all SHT_GROUP sections without merging them because merging them
189   // creates broken section contents.
190   if (IS->Type == SHT_GROUP) {
191     OutputSection *Out = nullptr;
192     addInputSec(IS, OutsecName, Out);
193     return;
194   }
195
196   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
197   // relocation sections .rela.foo and .rela.bar for example. Most tools do
198   // not allow multiple REL[A] sections for output section. Hence we
199   // should combine these relocation sections into single output.
200   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
201   // other REL[A] sections created by linker itself.
202   if (!isa<SyntheticSection>(IS) &&
203       (IS->Type == SHT_REL || IS->Type == SHT_RELA)) {
204     auto *Sec = cast<InputSection>(IS);
205     OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
206     addInputSec(IS, OutsecName, Out->RelocationSection);
207     return;
208   }
209
210   SectionKey Key = createKey(IS, OutsecName);
211   OutputSection *&Sec = Map[Key];
212   addInputSec(IS, OutsecName, Sec);
213 }
214
215 void OutputSectionFactory::addInputSec(InputSectionBase *IS,
216                                        StringRef OutsecName,
217                                        OutputSection *&Sec) {
218   if (!IS->Live) {
219     reportDiscarded(IS);
220     return;
221   }
222
223   if (Sec) {
224     if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags))
225       error("incompatible section flags for " + Sec->Name +
226             "\n>>> " + toString(IS) + ": 0x" + utohexstr(IS->Flags) +
227             "\n>>> output section " + Sec->Name + ": 0x" +
228             utohexstr(Sec->Flags));
229     if (Sec->Type != IS->Type) {
230       if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type))
231         Sec->Type = SHT_PROGBITS;
232       else
233         error("section type mismatch for " + IS->Name +
234               "\n>>> " + toString(IS) + ": " +
235               getELFSectionTypeName(Config->EMachine, IS->Type) +
236               "\n>>> output section " + Sec->Name + ": " +
237               getELFSectionTypeName(Config->EMachine, Sec->Type));
238     }
239     Sec->Flags |= IS->Flags;
240   } else {
241     Sec = make<OutputSection>(OutsecName, IS->Type, IS->Flags);
242     OutputSections.push_back(Sec);
243   }
244
245   Sec->addSection(cast<InputSection>(IS));
246 }
247
248 OutputSectionFactory::~OutputSectionFactory() {}
249
250 SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
251   return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
252 }
253
254 SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
255   return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
256 }
257
258 unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
259   return hash_combine(Val.Name, Val.Flags, Val.Alignment);
260 }
261
262 bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS,
263                                        const SectionKey &RHS) {
264   return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
265          LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment;
266 }
267
268 uint64_t elf::getHeaderSize() {
269   if (Config->OFormatBinary)
270     return 0;
271   return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
272 }
273
274 template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
275 template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
276 template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
277 template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);