1 //===- OutputSections.cpp -------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "OutputSections.h"
12 #include "LinkerScript.h"
15 #include "SymbolTable.h"
16 #include "SyntheticSections.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"
25 using namespace llvm::dwarf;
26 using namespace llvm::object;
27 using namespace llvm::support::endian;
28 using namespace llvm::ELF;
31 using namespace lld::elf;
34 OutputSection *Out::Opd;
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;
44 std::vector<OutputSection *> elf::OutputSections;
45 std::vector<OutputSectionCommand *> elf::OutputSectionCommands;
47 uint32_t OutputSection::getPhdrFlags() const {
49 if (Flags & SHF_WRITE)
51 if (Flags & SHF_EXECINSTR)
57 void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) {
58 Shdr->sh_entsize = Entsize;
59 Shdr->sh_addralign = Alignment;
61 Shdr->sh_offset = Offset;
62 Shdr->sh_flags = Flags;
67 Shdr->sh_name = ShName;
70 OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags)
71 : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type,
74 SectionIndex(INT_MAX) {}
76 static uint64_t updateOffset(uint64_t Off, InputSection *S) {
77 Off = alignTo(Off, S->Alignment);
79 return Off + S->getSize();
82 void OutputSection::addSection(InputSection *S) {
84 Sections.push_back(S);
86 this->updateAlignment(S->Alignment);
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);
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);
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 // ----------------------------------------------------------------
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.
116 // It also seems that that wording was a late addition and didn't get the
117 // necessary scrutiny.
119 // Merging sections with different flags is expected by some users. One
120 // reason is that if one file has
122 // int *const bar __attribute__((section(".foo"))) = (int *)0;
124 // gcc with -fPIC will produce a read only .foo section. But if another
128 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
130 // gcc with -fPIC will produce a read write section.
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
138 // start = .; *(.foo.*) end = .; *(.bar)
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
145 // Given the above issues, we instead merge sections by name and error on
146 // incompatible types and flags.
148 uint32_t Alignment = 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);
155 return SectionKey{OutsecName, Flags, Alignment};
158 OutputSectionFactory::OutputSectionFactory() {}
160 static uint64_t getIncompatibleFlags(uint64_t Flags) {
161 return Flags & (SHF_ALLOC | SHF_TLS);
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 ||
176 void elf::reportDiscarded(InputSectionBase *IS) {
177 if (!Config->PrintGcSections)
179 message("removing unused section from '" + IS->Name + "' in file '" +
180 IS->File->getName() + "'");
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);
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);
210 SectionKey Key = createKey(IS, OutsecName);
211 OutputSection *&Sec = Map[Key];
212 addInputSec(IS, OutsecName, Sec);
215 void OutputSectionFactory::addInputSec(InputSectionBase *IS,
216 StringRef OutsecName,
217 OutputSection *&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;
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));
239 Sec->Flags |= IS->Flags;
241 Sec = make<OutputSection>(OutsecName, IS->Type, IS->Flags);
242 OutputSections.push_back(Sec);
245 Sec->addSection(cast<InputSection>(IS));
248 OutputSectionFactory::~OutputSectionFactory() {}
250 SectionKey DenseMapInfo<SectionKey>::getEmptyKey() {
251 return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0};
254 SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() {
255 return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0};
258 unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) {
259 return hash_combine(Val.Name, Val.Flags, Val.Alignment);
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;
268 uint64_t elf::getHeaderSize() {
269 if (Config->OFormatBinary)
271 return Out::ElfHeader->Size + Out::ProgramHeaders->Size;
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);