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