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/Support/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 uint32_t OutputSection::getPhdrFlags() const { 45 uint32_t Ret = PF_R; 46 if (Flags & SHF_WRITE) 47 Ret |= PF_W; 48 if (Flags & SHF_EXECINSTR) 49 Ret |= PF_X; 50 return Ret; 51 } 52 53 template <class ELFT> 54 void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) { 55 Shdr->sh_entsize = Entsize; 56 Shdr->sh_addralign = Alignment; 57 Shdr->sh_type = Type; 58 Shdr->sh_offset = Offset; 59 Shdr->sh_flags = Flags; 60 Shdr->sh_info = Info; 61 Shdr->sh_link = Link; 62 Shdr->sh_addr = Addr; 63 Shdr->sh_size = Size; 64 Shdr->sh_name = ShName; 65 } 66 67 OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags) 68 : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type, 69 /*Info*/ 0, 70 /*Link*/ 0) {} 71 72 static bool compareByFilePosition(InputSection *A, InputSection *B) { 73 // Synthetic doesn't have link order dependecy, stable_sort will keep it last 74 if (A->kind() == InputSectionBase::Synthetic || 75 B->kind() == InputSectionBase::Synthetic) 76 return false; 77 auto *LA = cast<InputSection>(A->getLinkOrderDep()); 78 auto *LB = cast<InputSection>(B->getLinkOrderDep()); 79 OutputSection *AOut = LA->OutSec; 80 OutputSection *BOut = LB->OutSec; 81 if (AOut != BOut) 82 return AOut->SectionIndex < BOut->SectionIndex; 83 return LA->OutSecOff < LB->OutSecOff; 84 } 85 86 template <class ELFT> void OutputSection::finalize() { 87 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) { 88 std::sort(Sections.begin(), Sections.end(), compareByFilePosition); 89 assignOffsets(); 90 91 // We must preserve the link order dependency of sections with the 92 // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We 93 // need to translate the InputSection sh_link to the OutputSection sh_link, 94 // all InputSections in the OutputSection have the same dependency. 95 if (auto *D = this->Sections.front()->getLinkOrderDep()) 96 this->Link = D->OutSec->SectionIndex; 97 } 98 99 uint32_t Type = this->Type; 100 if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL)) 101 return; 102 103 InputSection *First = Sections[0]; 104 if (isa<SyntheticSection>(First)) 105 return; 106 107 this->Link = In<ELFT>::SymTab->OutSec->SectionIndex; 108 // sh_info for SHT_REL[A] sections should contain the section header index of 109 // the section to which the relocation applies. 110 InputSectionBase *S = First->getRelocatedSection(); 111 this->Info = S->OutSec->SectionIndex; 112 } 113 114 void OutputSection::addSection(InputSectionBase *C) { 115 assert(C->Live); 116 auto *S = cast<InputSection>(C); 117 Sections.push_back(S); 118 S->OutSec = this; 119 this->updateAlignment(S->Alignment); 120 121 // If this section contains a table of fixed-size entries, sh_entsize 122 // holds the element size. Consequently, if this contains two or more 123 // input sections, all of them must have the same sh_entsize. However, 124 // you can put different types of input sections into one output 125 // sectin by using linker scripts. I don't know what to do here. 126 // Probably we sholuld handle that as an error. But for now we just 127 // pick the largest sh_entsize. 128 this->Entsize = std::max(this->Entsize, S->Entsize); 129 } 130 131 // This function is called after we sort input sections 132 // and scan relocations to setup sections' offsets. 133 void OutputSection::assignOffsets() { 134 uint64_t Off = 0; 135 for (InputSection *S : Sections) { 136 Off = alignTo(Off, S->Alignment); 137 S->OutSecOff = Off; 138 Off += S->getSize(); 139 } 140 this->Size = Off; 141 } 142 143 void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) { 144 typedef std::pair<unsigned, InputSection *> Pair; 145 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; 146 147 std::vector<Pair> V; 148 for (InputSection *S : Sections) 149 V.push_back({Order(S), S}); 150 std::stable_sort(V.begin(), V.end(), Comp); 151 Sections.clear(); 152 for (Pair &P : V) 153 Sections.push_back(P.second); 154 } 155 156 // Sorts input sections by section name suffixes, so that .foo.N comes 157 // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. 158 // We want to keep the original order if the priorities are the same 159 // because the compiler keeps the original initialization order in a 160 // translation unit and we need to respect that. 161 // For more detail, read the section of the GCC's manual about init_priority. 162 void OutputSection::sortInitFini() { 163 // Sort sections by priority. 164 sort([](InputSectionBase *S) { return getPriority(S->Name); }); 165 } 166 167 // Returns true if S matches /Filename.?\.o$/. 168 static bool isCrtBeginEnd(StringRef S, StringRef Filename) { 169 if (!S.endswith(".o")) 170 return false; 171 S = S.drop_back(2); 172 if (S.endswith(Filename)) 173 return true; 174 return !S.empty() && S.drop_back().endswith(Filename); 175 } 176 177 static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } 178 static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } 179 180 // .ctors and .dtors are sorted by this priority from highest to lowest. 181 // 182 // 1. The section was contained in crtbegin (crtbegin contains 183 // some sentinel value in its .ctors and .dtors so that the runtime 184 // can find the beginning of the sections.) 185 // 186 // 2. The section has an optional priority value in the form of ".ctors.N" 187 // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, 188 // they are compared as string rather than number. 189 // 190 // 3. The section is just ".ctors" or ".dtors". 191 // 192 // 4. The section was contained in crtend, which contains an end marker. 193 // 194 // In an ideal world, we don't need this function because .init_array and 195 // .ctors are duplicate features (and .init_array is newer.) However, there 196 // are too many real-world use cases of .ctors, so we had no choice to 197 // support that with this rather ad-hoc semantics. 198 static bool compCtors(const InputSection *A, const InputSection *B) { 199 bool BeginA = isCrtbegin(A->File->getName()); 200 bool BeginB = isCrtbegin(B->File->getName()); 201 if (BeginA != BeginB) 202 return BeginA; 203 bool EndA = isCrtend(A->File->getName()); 204 bool EndB = isCrtend(B->File->getName()); 205 if (EndA != EndB) 206 return EndB; 207 StringRef X = A->Name; 208 StringRef Y = B->Name; 209 assert(X.startswith(".ctors") || X.startswith(".dtors")); 210 assert(Y.startswith(".ctors") || Y.startswith(".dtors")); 211 X = X.substr(6); 212 Y = Y.substr(6); 213 if (X.empty() && Y.empty()) 214 return false; 215 return X < Y; 216 } 217 218 // Sorts input sections by the special rules for .ctors and .dtors. 219 // Unfortunately, the rules are different from the one for .{init,fini}_array. 220 // Read the comment above. 221 void OutputSection::sortCtorsDtors() { 222 std::stable_sort(Sections.begin(), Sections.end(), compCtors); 223 } 224 225 // Fill [Buf, Buf + Size) with Filler. Filler is written in big 226 // endian order. This is used for linker script "=fillexp" command. 227 void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { 228 uint8_t V[4]; 229 write32be(V, Filler); 230 size_t I = 0; 231 for (; I + 4 < Size; I += 4) 232 memcpy(Buf + I, V, 4); 233 memcpy(Buf + I, V, Size - I); 234 } 235 236 template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) { 237 Loc = Buf; 238 if (uint32_t Filler = Script->getFiller(this->Name)) 239 fill(Buf, this->Size, Filler); 240 241 auto Fn = [=](InputSection *IS) { IS->writeTo<ELFT>(Buf); }; 242 forEach(Sections.begin(), Sections.end(), Fn); 243 244 // Linker scripts may have BYTE()-family commands with which you 245 // can write arbitrary bytes to the output. Process them if any. 246 Script->writeDataBytes(this->Name, Buf); 247 } 248 249 static uint64_t getOutFlags(InputSectionBase *S) { 250 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; 251 } 252 253 static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) { 254 // The ELF spec just says 255 // ---------------------------------------------------------------- 256 // In the first phase, input sections that match in name, type and 257 // attribute flags should be concatenated into single sections. 258 // ---------------------------------------------------------------- 259 // 260 // However, it is clear that at least some flags have to be ignored for 261 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be 262 // ignored. We should not have two output .text sections just because one was 263 // in a group and another was not for example. 264 // 265 // It also seems that that wording was a late addition and didn't get the 266 // necessary scrutiny. 267 // 268 // Merging sections with different flags is expected by some users. One 269 // reason is that if one file has 270 // 271 // int *const bar __attribute__((section(".foo"))) = (int *)0; 272 // 273 // gcc with -fPIC will produce a read only .foo section. But if another 274 // file has 275 // 276 // int zed; 277 // int *const bar __attribute__((section(".foo"))) = (int *)&zed; 278 // 279 // gcc with -fPIC will produce a read write section. 280 // 281 // Last but not least, when using linker script the merge rules are forced by 282 // the script. Unfortunately, linker scripts are name based. This means that 283 // expressions like *(.foo*) can refer to multiple input sections with 284 // different flags. We cannot put them in different output sections or we 285 // would produce wrong results for 286 // 287 // start = .; *(.foo.*) end = .; *(.bar) 288 // 289 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to 290 // another. The problem is that there is no way to layout those output 291 // sections such that the .foo sections are the only thing between the start 292 // and end symbols. 293 // 294 // Given the above issues, we instead merge sections by name and error on 295 // incompatible types and flags. 296 297 uint32_t Alignment = 0; 298 uint64_t Flags = 0; 299 if (Config->Relocatable && (C->Flags & SHF_MERGE)) { 300 Alignment = std::max<uint64_t>(C->Alignment, C->Entsize); 301 Flags = C->Flags & (SHF_MERGE | SHF_STRINGS); 302 } 303 304 return SectionKey{OutsecName, Flags, Alignment}; 305 } 306 307 OutputSectionFactory::OutputSectionFactory( 308 std::vector<OutputSection *> &OutputSections) 309 : OutputSections(OutputSections) {} 310 311 static uint64_t getIncompatibleFlags(uint64_t Flags) { 312 return Flags & (SHF_ALLOC | SHF_TLS); 313 } 314 315 // We allow sections of types listed below to merged into a 316 // single progbits section. This is typically done by linker 317 // scripts. Merging nobits and progbits will force disk space 318 // to be allocated for nobits sections. Other ones don't require 319 // any special treatment on top of progbits, so there doesn't 320 // seem to be a harm in merging them. 321 static bool canMergeToProgbits(unsigned Type) { 322 return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY || 323 Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY || 324 Type == SHT_NOTE; 325 } 326 327 static void reportDiscarded(InputSectionBase *IS) { 328 if (!Config->PrintGcSections) 329 return; 330 message("removing unused section from '" + IS->Name + "' in file '" + 331 IS->File->getName()); 332 } 333 334 void OutputSectionFactory::addInputSec(InputSectionBase *IS, 335 StringRef OutsecName) { 336 if (!IS->Live) { 337 reportDiscarded(IS); 338 return; 339 } 340 341 SectionKey Key = createKey(IS, OutsecName); 342 uint64_t Flags = getOutFlags(IS); 343 OutputSection *&Sec = Map[Key]; 344 if (Sec) { 345 if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags)) 346 error("Section has flags incompatible with others with the same name " + 347 toString(IS)); 348 if (Sec->Type != IS->Type) { 349 if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type)) 350 Sec->Type = SHT_PROGBITS; 351 else 352 error("Section has different type from others with the same name " + 353 toString(IS)); 354 } 355 Sec->Flags |= Flags; 356 } else { 357 Sec = make<OutputSection>(Key.Name, IS->Type, Flags); 358 OutputSections.push_back(Sec); 359 } 360 361 Sec->addSection(IS); 362 } 363 364 OutputSectionFactory::~OutputSectionFactory() {} 365 366 SectionKey DenseMapInfo<SectionKey>::getEmptyKey() { 367 return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; 368 } 369 370 SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() { 371 return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0}; 372 } 373 374 unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) { 375 return hash_combine(Val.Name, Val.Flags, Val.Alignment); 376 } 377 378 bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS, 379 const SectionKey &RHS) { 380 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && 381 LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment; 382 } 383 384 uint64_t elf::getHeaderSize() { 385 if (Config->OFormatBinary) 386 return 0; 387 return Out::ElfHeader->Size + Out::ProgramHeaders->Size; 388 } 389 390 namespace lld { 391 namespace elf { 392 393 template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); 394 template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); 395 template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); 396 template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); 397 398 template void OutputSection::finalize<ELF32LE>(); 399 template void OutputSection::finalize<ELF32BE>(); 400 template void OutputSection::finalize<ELF64LE>(); 401 template void OutputSection::finalize<ELF64BE>(); 402 403 template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf); 404 template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf); 405 template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf); 406 template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf); 407 408 } 409 } 410