Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 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" |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 12 | #include "LinkerScript.h" |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 13 | #include "Memory.h" |
Rui Ueyama | fbbde54 | 2016-06-29 09:08:02 +0000 | [diff] [blame] | 14 | #include "Strings.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 16 | #include "SyntheticSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 17 | #include "Target.h" |
Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 18 | #include "Threads.h" |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Dwarf.h" |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MD5.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | a42b3bc | 2016-09-27 16:43:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/SHA1.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 23 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Rui Ueyama | dbcfedb | 2016-02-09 21:46:11 +0000 | [diff] [blame] | 25 | using namespace llvm::dwarf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 26 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 27 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 28 | using namespace llvm::ELF; |
| 29 | |
| 30 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 31 | using namespace lld::elf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 32 | |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 33 | uint8_t Out::First; |
| 34 | OutputSection *Out::Bss; |
| 35 | OutputSection *Out::BssRelRo; |
| 36 | OutputSection *Out::Opd; |
| 37 | uint8_t *Out::OpdBuf; |
| 38 | PhdrEntry *Out::TlsPhdr; |
| 39 | OutputSection *Out::DebugInfo; |
| 40 | OutputSection *Out::ElfHeader; |
| 41 | OutputSection *Out::ProgramHeaders; |
| 42 | OutputSection *Out::PreinitArray; |
| 43 | OutputSection *Out::InitArray; |
| 44 | OutputSection *Out::FiniArray; |
| 45 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 46 | uint32_t OutputSection::getPhdrFlags() const { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 47 | uint32_t Ret = PF_R; |
| 48 | if (Flags & SHF_WRITE) |
| 49 | Ret |= PF_W; |
| 50 | if (Flags & SHF_EXECINSTR) |
| 51 | Ret |= PF_X; |
| 52 | return Ret; |
| 53 | } |
| 54 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 55 | template <class ELFT> |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 56 | void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 57 | Shdr->sh_entsize = Entsize; |
| 58 | Shdr->sh_addralign = Addralign; |
| 59 | Shdr->sh_type = Type; |
| 60 | Shdr->sh_offset = Offset; |
| 61 | Shdr->sh_flags = Flags; |
| 62 | Shdr->sh_info = Info; |
| 63 | Shdr->sh_link = Link; |
Rafael Espindola | ea590d9 | 2017-02-08 15:19:03 +0000 | [diff] [blame] | 64 | Shdr->sh_addr = Addr; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 65 | Shdr->sh_size = Size; |
| 66 | Shdr->sh_name = ShName; |
Rui Ueyama | c63c1db | 2016-03-13 06:50:33 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 69 | template <class ELFT> static uint64_t getEntsize(uint32_t Type) { |
| 70 | switch (Type) { |
| 71 | case SHT_RELA: |
| 72 | return sizeof(typename ELFT::Rela); |
| 73 | case SHT_REL: |
| 74 | return sizeof(typename ELFT::Rel); |
| 75 | case SHT_MIPS_REGINFO: |
| 76 | return sizeof(Elf_Mips_RegInfo<ELFT>); |
| 77 | case SHT_MIPS_OPTIONS: |
| 78 | return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>); |
| 79 | case SHT_MIPS_ABIFLAGS: |
| 80 | return sizeof(Elf_Mips_ABIFlags<ELFT>); |
| 81 | default: |
| 82 | return 0; |
| 83 | } |
| 84 | } |
| 85 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 86 | OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags) |
| 87 | : Name(Name), Addralign(1), Flags(Flags), Type(Type) { |
| 88 | switch (Config->EKind) { |
| 89 | case ELFNoneKind: |
| 90 | llvm_unreachable("unknown kind"); |
| 91 | case ELF32LEKind: |
| 92 | this->Entsize = getEntsize<ELF32LE>(Type); |
| 93 | break; |
| 94 | case ELF32BEKind: |
| 95 | this->Entsize = getEntsize<ELF32BE>(Type); |
| 96 | break; |
| 97 | case ELF64LEKind: |
| 98 | this->Entsize = getEntsize<ELF64LE>(Type); |
| 99 | break; |
| 100 | case ELF64BEKind: |
| 101 | this->Entsize = getEntsize<ELF64BE>(Type); |
| 102 | break; |
| 103 | } |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 106 | template <typename ELFT> |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 107 | static bool compareByFilePosition(InputSection *A, InputSection *B) { |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 108 | // Synthetic doesn't have link order dependecy, stable_sort will keep it last |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 109 | if (A->kind() == InputSectionBase::Synthetic || |
| 110 | B->kind() == InputSectionBase::Synthetic) |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 111 | return false; |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 112 | auto *LA = cast<InputSection>(A->template getLinkOrderDep<ELFT>()); |
| 113 | auto *LB = cast<InputSection>(B->template getLinkOrderDep<ELFT>()); |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 114 | OutputSection *AOut = LA->OutSec; |
| 115 | OutputSection *BOut = LB->OutSec; |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 116 | if (AOut != BOut) |
| 117 | return AOut->SectionIndex < BOut->SectionIndex; |
| 118 | return LA->OutSecOff < LB->OutSecOff; |
| 119 | } |
| 120 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 121 | template <class ELFT> void OutputSection::finalize() { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 122 | if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) { |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 123 | std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>); |
| 124 | Size = 0; |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 125 | assignOffsets<ELFT>(); |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 126 | |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 127 | // We must preserve the link order dependency of sections with the |
| 128 | // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We |
| 129 | // need to translate the InputSection sh_link to the OutputSection sh_link, |
| 130 | // all InputSections in the OutputSection have the same dependency. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 131 | if (auto *D = this->Sections.front()->template getLinkOrderDep<ELFT>()) |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 132 | this->Link = D->OutSec->SectionIndex; |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 133 | } |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 134 | |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 135 | uint32_t Type = this->Type; |
George Rimar | 82bd8be | 2017-02-08 16:18:10 +0000 | [diff] [blame] | 136 | if (!Config->copyRelocs() || (Type != SHT_RELA && Type != SHT_REL)) |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 137 | return; |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 138 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 139 | InputSection *First = Sections[0]; |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame^] | 140 | if (isa<SyntheticSection>(First)) |
Rafael Espindola | 82f00ec | 2017-02-16 14:23:43 +0000 | [diff] [blame] | 141 | return; |
| 142 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 143 | this->Link = In<ELFT>::SymTab->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 144 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 145 | // the section to which the relocation applies. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 146 | InputSectionBase *S = First->getRelocatedSection<ELFT>(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 147 | this->Info = S->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 148 | } |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 149 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 150 | void OutputSection::addSection(InputSectionBase *C) { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 151 | assert(C->Live); |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 152 | auto *S = cast<InputSection>(C); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 153 | Sections.push_back(S); |
| 154 | S->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 155 | this->updateAlignment(S->Alignment); |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 156 | // Keep sh_entsize value of the input section to be able to perform merging |
| 157 | // later during a final linking using the generated relocatable object. |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 158 | if (Config->Relocatable && (S->Flags & SHF_MERGE)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 159 | this->Entsize = S->Entsize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 162 | // This function is called after we sort input sections |
| 163 | // and scan relocations to setup sections' offsets. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 164 | template <class ELFT> void OutputSection::assignOffsets() { |
| 165 | uint64_t Off = this->Size; |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 166 | for (InputSection *S : Sections) { |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 167 | Off = alignTo(Off, S->Alignment); |
| 168 | S->OutSecOff = Off; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 169 | Off += S->template getSize<ELFT>(); |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 170 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 171 | this->Size = Off; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 174 | void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) { |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 175 | typedef std::pair<unsigned, InputSection *> Pair; |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 176 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 177 | |
| 178 | std::vector<Pair> V; |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 179 | for (InputSection *S : Sections) |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 180 | V.push_back({Order(S), S}); |
| 181 | std::stable_sort(V.begin(), V.end(), Comp); |
| 182 | Sections.clear(); |
| 183 | for (Pair &P : V) |
| 184 | Sections.push_back(P.second); |
| 185 | } |
| 186 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 187 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 188 | // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 189 | // We want to keep the original order if the priorities are the same |
| 190 | // because the compiler keeps the original initialization order in a |
| 191 | // translation unit and we need to respect that. |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 192 | // For more detail, read the section of the GCC's manual about init_priority. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 193 | void OutputSection::sortInitFini() { |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 194 | // Sort sections by priority. |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 195 | sort([](InputSectionBase *S) { return getPriority(S->Name); }); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 196 | } |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 197 | |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 198 | // Returns true if S matches /Filename.?\.o$/. |
| 199 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 200 | if (!S.endswith(".o")) |
| 201 | return false; |
| 202 | S = S.drop_back(2); |
| 203 | if (S.endswith(Filename)) |
| 204 | return true; |
| 205 | return !S.empty() && S.drop_back().endswith(Filename); |
| 206 | } |
| 207 | |
| 208 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 209 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 210 | |
| 211 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 212 | // |
| 213 | // 1. The section was contained in crtbegin (crtbegin contains |
| 214 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 215 | // can find the beginning of the sections.) |
| 216 | // |
| 217 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 218 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 219 | // they are compared as string rather than number. |
| 220 | // |
| 221 | // 3. The section is just ".ctors" or ".dtors". |
| 222 | // |
| 223 | // 4. The section was contained in crtend, which contains an end marker. |
| 224 | // |
| 225 | // In an ideal world, we don't need this function because .init_array and |
| 226 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 227 | // are too many real-world use cases of .ctors, so we had no choice to |
| 228 | // support that with this rather ad-hoc semantics. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 229 | static bool compCtors(const InputSection *A, const InputSection *B) { |
Rafael Espindola | a2e3fee | 2017-02-24 13:19:33 +0000 | [diff] [blame] | 230 | bool BeginA = isCrtbegin(A->File->getName()); |
| 231 | bool BeginB = isCrtbegin(B->File->getName()); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 232 | if (BeginA != BeginB) |
| 233 | return BeginA; |
Rafael Espindola | a2e3fee | 2017-02-24 13:19:33 +0000 | [diff] [blame] | 234 | bool EndA = isCrtend(A->File->getName()); |
| 235 | bool EndB = isCrtend(B->File->getName()); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 236 | if (EndA != EndB) |
| 237 | return EndB; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 238 | StringRef X = A->Name; |
| 239 | StringRef Y = B->Name; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 240 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 241 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 242 | X = X.substr(6); |
| 243 | Y = Y.substr(6); |
Rui Ueyama | 24b794e | 2016-02-12 00:38:46 +0000 | [diff] [blame] | 244 | if (X.empty() && Y.empty()) |
| 245 | return false; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 246 | return X < Y; |
| 247 | } |
| 248 | |
| 249 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 250 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 251 | // Read the comment above. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 252 | void OutputSection::sortCtorsDtors() { |
Rafael Espindola | a2e3fee | 2017-02-24 13:19:33 +0000 | [diff] [blame] | 253 | std::stable_sort(Sections.begin(), Sections.end(), compCtors); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 256 | // Fill [Buf, Buf + Size) with Filler. Filler is written in big |
| 257 | // endian order. This is used for linker script "=fillexp" command. |
| 258 | void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { |
| 259 | uint8_t V[4]; |
| 260 | write32be(V, Filler); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 261 | size_t I = 0; |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 262 | for (; I + 4 < Size; I += 4) |
| 263 | memcpy(Buf + I, V, 4); |
| 264 | memcpy(Buf + I, V, Size - I); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 267 | template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) { |
Eugene Leviant | 84569e6 | 2016-11-29 08:05:44 +0000 | [diff] [blame] | 268 | Loc = Buf; |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 269 | if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 270 | fill(Buf, this->Size, Filler); |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 271 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 272 | auto Fn = [=](InputSection *IS) { IS->writeTo<ELFT>(Buf); }; |
Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 273 | forEach(Sections.begin(), Sections.end(), Fn); |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 274 | |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 275 | // Linker scripts may have BYTE()-family commands with which you |
| 276 | // can write arbitrary bytes to the output. Process them if any. |
| 277 | Script<ELFT>::X->writeDataBytes(this->Name, Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 280 | template <class ELFT> |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 281 | static typename ELFT::uint getOutFlags(InputSectionBase *S) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 282 | return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | template <class ELFT> |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 286 | static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) { |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 287 | // The ELF spec just says |
| 288 | // ---------------------------------------------------------------- |
| 289 | // In the first phase, input sections that match in name, type and |
| 290 | // attribute flags should be concatenated into single sections. |
| 291 | // ---------------------------------------------------------------- |
| 292 | // |
| 293 | // However, it is clear that at least some flags have to be ignored for |
| 294 | // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be |
| 295 | // ignored. We should not have two output .text sections just because one was |
| 296 | // in a group and another was not for example. |
| 297 | // |
| 298 | // It also seems that that wording was a late addition and didn't get the |
| 299 | // necessary scrutiny. |
| 300 | // |
| 301 | // Merging sections with different flags is expected by some users. One |
| 302 | // reason is that if one file has |
| 303 | // |
| 304 | // int *const bar __attribute__((section(".foo"))) = (int *)0; |
| 305 | // |
| 306 | // gcc with -fPIC will produce a read only .foo section. But if another |
| 307 | // file has |
| 308 | // |
| 309 | // int zed; |
| 310 | // int *const bar __attribute__((section(".foo"))) = (int *)&zed; |
| 311 | // |
| 312 | // gcc with -fPIC will produce a read write section. |
| 313 | // |
| 314 | // Last but not least, when using linker script the merge rules are forced by |
| 315 | // the script. Unfortunately, linker scripts are name based. This means that |
| 316 | // expressions like *(.foo*) can refer to multiple input sections with |
| 317 | // different flags. We cannot put them in different output sections or we |
| 318 | // would produce wrong results for |
| 319 | // |
| 320 | // start = .; *(.foo.*) end = .; *(.bar) |
| 321 | // |
| 322 | // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to |
| 323 | // another. The problem is that there is no way to layout those output |
| 324 | // sections such that the .foo sections are the only thing between the start |
| 325 | // and end symbols. |
| 326 | // |
| 327 | // Given the above issues, we instead merge sections by name and error on |
| 328 | // incompatible types and flags. |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 329 | |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 330 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 331 | |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 332 | uintX_t Alignment = 0; |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 333 | uintX_t Flags = 0; |
| 334 | if (Config->Relocatable && (C->Flags & SHF_MERGE)) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 335 | Alignment = std::max<uintX_t>(C->Alignment, C->Entsize); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 336 | Flags = C->Flags & (SHF_MERGE | SHF_STRINGS); |
| 337 | } |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 338 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 339 | return SectionKey{OutsecName, Flags, Alignment}; |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 342 | OutputSectionFactory::OutputSectionFactory( |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 343 | std::vector<OutputSection *> &OutputSections) |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 344 | : OutputSections(OutputSections) {} |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 345 | |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 346 | static uint64_t getIncompatibleFlags(uint64_t Flags) { |
| 347 | return Flags & (SHF_ALLOC | SHF_TLS); |
| 348 | } |
| 349 | |
Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 350 | // We allow sections of types listed below to merged into a |
| 351 | // single progbits section. This is typically done by linker |
| 352 | // scripts. Merging nobits and progbits will force disk space |
| 353 | // to be allocated for nobits sections. Other ones don't require |
| 354 | // any special treatment on top of progbits, so there doesn't |
| 355 | // seem to be a harm in merging them. |
| 356 | static bool canMergeToProgbits(unsigned Type) { |
| 357 | return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY || |
| 358 | Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY || |
| 359 | Type == SHT_NOTE; |
| 360 | } |
| 361 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 362 | template <class ELFT> static void reportDiscarded(InputSectionBase *IS) { |
Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 363 | if (!Config->PrintGcSections) |
| 364 | return; |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 365 | message("removing unused section from '" + IS->Name + "' in file '" + |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 366 | IS->getFile<ELFT>()->getName()); |
Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 369 | template <class ELFT> |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 370 | void OutputSectionFactory::addInputSec(InputSectionBase *IS, |
| 371 | StringRef OutsecName) { |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 372 | if (!IS->Live) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 373 | reportDiscarded<ELFT>(IS); |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 377 | SectionKey Key = createKey<ELFT>(IS, OutsecName); |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 378 | uint64_t Flags = getOutFlags<ELFT>(IS); |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 379 | OutputSection *&Sec = Map[Key]; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 380 | if (Sec) { |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 381 | if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags)) |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 382 | error("Section has flags incompatible with others with the same name " + |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 383 | toString(IS)); |
| 384 | if (Sec->Type != IS->Type) { |
| 385 | if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type)) |
Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 386 | Sec->Type = SHT_PROGBITS; |
| 387 | else |
| 388 | error("Section has different type from others with the same name " + |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 389 | toString(IS)); |
Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 390 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 391 | Sec->Flags |= Flags; |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 392 | } else { |
| 393 | uint32_t Type = IS->Type; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 394 | if (IS->kind() == InputSectionBase::EHFrame) { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 395 | In<ELFT>::EhFrame->addSection(IS); |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 396 | return; |
| 397 | } |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 398 | Sec = make<OutputSection>(Key.Name, Type, Flags); |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 399 | OutputSections.push_back(Sec); |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 402 | Sec->addSection(IS); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 405 | OutputSectionFactory::~OutputSectionFactory() {} |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 406 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 407 | SectionKey DenseMapInfo<SectionKey>::getEmptyKey() { |
| 408 | return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 411 | SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() { |
| 412 | return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0}; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 415 | unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) { |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 416 | return hash_combine(Val.Name, Val.Flags, Val.Alignment); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 419 | bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS, |
| 420 | const SectionKey &RHS) { |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 421 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 422 | LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 425 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 426 | namespace elf { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 427 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 428 | template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 429 | template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 430 | template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 431 | template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 432 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 433 | template void OutputSection::assignOffsets<ELF32LE>(); |
| 434 | template void OutputSection::assignOffsets<ELF32BE>(); |
| 435 | template void OutputSection::assignOffsets<ELF64LE>(); |
| 436 | template void OutputSection::assignOffsets<ELF64BE>(); |
| 437 | |
| 438 | template void OutputSection::finalize<ELF32LE>(); |
| 439 | template void OutputSection::finalize<ELF32BE>(); |
| 440 | template void OutputSection::finalize<ELF64LE>(); |
| 441 | template void OutputSection::finalize<ELF64BE>(); |
| 442 | |
| 443 | template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf); |
| 444 | template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf); |
| 445 | template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf); |
| 446 | template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 447 | |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 448 | template void OutputSectionFactory::addInputSec<ELF32LE>(InputSectionBase *, |
| 449 | StringRef); |
| 450 | template void OutputSectionFactory::addInputSec<ELF32BE>(InputSectionBase *, |
| 451 | StringRef); |
| 452 | template void OutputSectionFactory::addInputSec<ELF64LE>(InputSectionBase *, |
| 453 | StringRef); |
| 454 | template void OutputSectionFactory::addInputSec<ELF64BE>(InputSectionBase *, |
| 455 | StringRef); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 456 | } |
| 457 | } |