| 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 | f5febef | 2016-05-24 02:55:45 +0000 | [diff] [blame] | 12 | #include "EhFrame.h" |
| Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 13 | #include "LinkerScript.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" |
| Rui Ueyama | 520d916 | 2016-12-08 18:31:13 +0000 | [diff] [blame] | 19 | #include "lld/Support/Memory.h" |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Dwarf.h" |
| Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MD5.h" |
| Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
| Rafael Espindola | a42b3bc | 2016-09-27 16:43:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SHA1.h" |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 24 | |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| Rui Ueyama | dbcfedb | 2016-02-09 21:46:11 +0000 | [diff] [blame] | 26 | using namespace llvm::dwarf; |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 27 | using namespace llvm::object; |
| Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 28 | using namespace llvm::support::endian; |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | using namespace llvm::ELF; |
| 30 | |
| 31 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 32 | using namespace lld::elf; |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 33 | |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 34 | OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type, |
| 35 | uint64_t Flags) |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 36 | : Name(Name) { |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 37 | this->Type = Type; |
| 38 | this->Flags = Flags; |
| 39 | this->Addralign = 1; |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 42 | uint32_t OutputSectionBase::getPhdrFlags() const { |
| Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 43 | uint32_t Ret = PF_R; |
| 44 | if (Flags & SHF_WRITE) |
| 45 | Ret |= PF_W; |
| 46 | if (Flags & SHF_EXECINSTR) |
| 47 | Ret |= PF_X; |
| 48 | return Ret; |
| 49 | } |
| 50 | |
| Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 51 | template <class ELFT> |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 52 | void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) { |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 53 | Shdr->sh_entsize = Entsize; |
| 54 | Shdr->sh_addralign = Addralign; |
| 55 | Shdr->sh_type = Type; |
| 56 | Shdr->sh_offset = Offset; |
| 57 | Shdr->sh_flags = Flags; |
| 58 | Shdr->sh_info = Info; |
| 59 | Shdr->sh_link = Link; |
| 60 | Shdr->sh_addr = Addr; |
| 61 | Shdr->sh_size = Size; |
| 62 | Shdr->sh_name = ShName; |
| Rui Ueyama | c63c1db | 2016-03-13 06:50:33 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 65 | template <class ELFT> static uint64_t getEntsize(uint32_t Type) { |
| 66 | switch (Type) { |
| 67 | case SHT_RELA: |
| 68 | return sizeof(typename ELFT::Rela); |
| 69 | case SHT_REL: |
| 70 | return sizeof(typename ELFT::Rel); |
| 71 | case SHT_MIPS_REGINFO: |
| 72 | return sizeof(Elf_Mips_RegInfo<ELFT>); |
| 73 | case SHT_MIPS_OPTIONS: |
| 74 | return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>); |
| 75 | case SHT_MIPS_ABIFLAGS: |
| 76 | return sizeof(Elf_Mips_ABIFlags<ELFT>); |
| 77 | default: |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 | |
| George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 82 | template <class ELFT> |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 83 | OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags) |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 84 | : OutputSectionBase(Name, Type, Flags) { |
| Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 85 | this->Entsize = getEntsize<ELFT>(Type); |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 88 | template <typename ELFT> |
| 89 | static bool compareByFilePosition(InputSection<ELFT> *A, |
| 90 | InputSection<ELFT> *B) { |
| Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 91 | // Synthetic doesn't have link order dependecy, stable_sort will keep it last |
| 92 | if (A->kind() == InputSectionData::Synthetic || |
| 93 | B->kind() == InputSectionData::Synthetic) |
| 94 | return false; |
| Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 95 | auto *LA = cast<InputSection<ELFT>>(A->getLinkOrderDep()); |
| 96 | auto *LB = cast<InputSection<ELFT>>(B->getLinkOrderDep()); |
| 97 | OutputSectionBase *AOut = LA->OutSec; |
| 98 | OutputSectionBase *BOut = LB->OutSec; |
| 99 | if (AOut != BOut) |
| 100 | return AOut->SectionIndex < BOut->SectionIndex; |
| 101 | return LA->OutSecOff < LB->OutSecOff; |
| 102 | } |
| 103 | |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 104 | template <class ELFT> void OutputSection<ELFT>::finalize() { |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 105 | if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) { |
| Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 106 | std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>); |
| 107 | Size = 0; |
| 108 | assignOffsets(); |
| 109 | |
| Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 110 | // We must preserve the link order dependency of sections with the |
| 111 | // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We |
| 112 | // need to translate the InputSection sh_link to the OutputSection sh_link, |
| 113 | // all InputSections in the OutputSection have the same dependency. |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 114 | if (auto *D = this->Sections.front()->getLinkOrderDep()) |
| 115 | this->Link = D->OutSec->SectionIndex; |
| Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 116 | } |
| Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 117 | |
| Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 118 | uint32_t Type = this->Type; |
| 119 | if (!Config->Relocatable || (Type != SHT_RELA && Type != SHT_REL)) |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 120 | return; |
| Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 121 | |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 122 | this->Link = In<ELFT>::SymTab->OutSec->SectionIndex; |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 123 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 124 | // the section to which the relocation applies. |
| 125 | InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection(); |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 126 | this->Info = S->OutSec->SectionIndex; |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 127 | } |
| Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 128 | |
| 129 | template <class ELFT> |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 130 | void OutputSection<ELFT>::addSection(InputSectionData *C) { |
| Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 131 | assert(C->Live); |
| Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 132 | auto *S = cast<InputSection<ELFT>>(C); |
| 133 | Sections.push_back(S); |
| 134 | S->OutSec = this; |
| Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 135 | this->updateAlignment(S->Alignment); |
| Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 136 | // Keep sh_entsize value of the input section to be able to perform merging |
| 137 | // later during a final linking using the generated relocatable object. |
| Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 138 | if (Config->Relocatable && (S->Flags & SHF_MERGE)) |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 139 | this->Entsize = S->Entsize; |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 142 | // This function is called after we sort input sections |
| 143 | // and scan relocations to setup sections' offsets. |
| 144 | template <class ELFT> void OutputSection<ELFT>::assignOffsets() { |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 145 | uintX_t Off = this->Size; |
| Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 146 | for (InputSection<ELFT> *S : Sections) { |
| 147 | Off = alignTo(Off, S->Alignment); |
| 148 | S->OutSecOff = Off; |
| 149 | Off += S->getSize(); |
| 150 | } |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 151 | this->Size = Off; |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 154 | template <class ELFT> |
| 155 | void OutputSection<ELFT>::sort( |
| 156 | std::function<unsigned(InputSection<ELFT> *S)> Order) { |
| 157 | typedef std::pair<unsigned, InputSection<ELFT> *> Pair; |
| 158 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 159 | |
| 160 | std::vector<Pair> V; |
| 161 | for (InputSection<ELFT> *S : Sections) |
| 162 | V.push_back({Order(S), S}); |
| 163 | std::stable_sort(V.begin(), V.end(), Comp); |
| 164 | Sections.clear(); |
| 165 | for (Pair &P : V) |
| 166 | Sections.push_back(P.second); |
| 167 | } |
| 168 | |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 169 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 170 | // 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] | 171 | // We want to keep the original order if the priorities are the same |
| 172 | // because the compiler keeps the original initialization order in a |
| 173 | // translation unit and we need to respect that. |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 174 | // For more detail, read the section of the GCC's manual about init_priority. |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 175 | template <class ELFT> void OutputSection<ELFT>::sortInitFini() { |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 176 | // Sort sections by priority. |
| George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 177 | sort([](InputSection<ELFT> *S) { return getPriority(S->Name); }); |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 178 | } |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 179 | |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 180 | // Returns true if S matches /Filename.?\.o$/. |
| 181 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 182 | if (!S.endswith(".o")) |
| 183 | return false; |
| 184 | S = S.drop_back(2); |
| 185 | if (S.endswith(Filename)) |
| 186 | return true; |
| 187 | return !S.empty() && S.drop_back().endswith(Filename); |
| 188 | } |
| 189 | |
| 190 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 191 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 192 | |
| 193 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 194 | // |
| 195 | // 1. The section was contained in crtbegin (crtbegin contains |
| 196 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 197 | // can find the beginning of the sections.) |
| 198 | // |
| 199 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 200 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 201 | // they are compared as string rather than number. |
| 202 | // |
| 203 | // 3. The section is just ".ctors" or ".dtors". |
| 204 | // |
| 205 | // 4. The section was contained in crtend, which contains an end marker. |
| 206 | // |
| 207 | // In an ideal world, we don't need this function because .init_array and |
| 208 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 209 | // are too many real-world use cases of .ctors, so we had no choice to |
| 210 | // support that with this rather ad-hoc semantics. |
| 211 | template <class ELFT> |
| 212 | static bool compCtors(const InputSection<ELFT> *A, |
| 213 | const InputSection<ELFT> *B) { |
| 214 | bool BeginA = isCrtbegin(A->getFile()->getName()); |
| 215 | bool BeginB = isCrtbegin(B->getFile()->getName()); |
| 216 | if (BeginA != BeginB) |
| 217 | return BeginA; |
| 218 | bool EndA = isCrtend(A->getFile()->getName()); |
| 219 | bool EndB = isCrtend(B->getFile()->getName()); |
| 220 | if (EndA != EndB) |
| 221 | return EndB; |
| Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 222 | StringRef X = A->Name; |
| 223 | StringRef Y = B->Name; |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 224 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 225 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 226 | X = X.substr(6); |
| 227 | Y = Y.substr(6); |
| Rui Ueyama | 24b794e | 2016-02-12 00:38:46 +0000 | [diff] [blame] | 228 | if (X.empty() && Y.empty()) |
| 229 | return false; |
| Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 230 | return X < Y; |
| 231 | } |
| 232 | |
| 233 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 234 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 235 | // Read the comment above. |
| 236 | template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() { |
| 237 | std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>); |
| Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 240 | // Fill [Buf, Buf + Size) with Filler. Filler is written in big |
| 241 | // endian order. This is used for linker script "=fillexp" command. |
| 242 | void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { |
| 243 | uint8_t V[4]; |
| 244 | write32be(V, Filler); |
| George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 245 | size_t I = 0; |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 246 | for (; I + 4 < Size; I += 4) |
| 247 | memcpy(Buf + I, V, 4); |
| 248 | memcpy(Buf + I, V, Size - I); |
| George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 251 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
| Eugene Leviant | 84569e6 | 2016-11-29 08:05:44 +0000 | [diff] [blame] | 252 | Loc = Buf; |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 253 | if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name)) |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 254 | fill(Buf, this->Size, Filler); |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 255 | |
| Davide Italiano | 2e8b2a7 | 2016-11-18 02:23:48 +0000 | [diff] [blame] | 256 | auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); }; |
| Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 257 | forEach(Sections.begin(), Sections.end(), Fn); |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 258 | |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 259 | // Linker scripts may have BYTE()-family commands with which you |
| 260 | // can write arbitrary bytes to the output. Process them if any. |
| 261 | Script<ELFT>::X->writeDataBytes(this->Name, Buf); |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 264 | template <class ELFT> |
| Rui Ueyama | f86cb90 | 2016-05-23 15:12:41 +0000 | [diff] [blame] | 265 | EhOutputSection<ELFT>::EhOutputSection() |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 266 | : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {} |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 267 | |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 268 | // Search for an existing CIE record or create a new one. |
| 269 | // CIE records from input object files are uniquified by their contents |
| 270 | // and where their relocations point to. |
| 271 | template <class ELFT> |
| 272 | template <class RelTy> |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 273 | CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece, |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 274 | ArrayRef<RelTy> Rels) { |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 275 | auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 276 | const endianness E = ELFT::TargetEndianness; |
| Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 277 | if (read32<E>(Piece.data().data() + 4) != 0) |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 278 | fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame"); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 279 | |
| 280 | SymbolBody *Personality = nullptr; |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 281 | unsigned FirstRelI = Piece.FirstRelocation; |
| 282 | if (FirstRelI != (unsigned)-1) |
| 283 | Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 284 | |
| 285 | // Search for an existing CIE by CIE contents/relocation target pair. |
| Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 286 | CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 287 | |
| 288 | // If not found, create a new one. |
| Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 289 | if (Cie->Piece == nullptr) { |
| 290 | Cie->Piece = &Piece; |
| Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 291 | Cies.push_back(Cie); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 292 | } |
| 293 | return Cie; |
| 294 | } |
| 295 | |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 296 | // There is one FDE per function. Returns true if a given FDE |
| 297 | // points to a live function. |
| 298 | template <class ELFT> |
| 299 | template <class RelTy> |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 300 | bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece, |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 301 | ArrayRef<RelTy> Rels) { |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 302 | auto *Sec = cast<EhInputSection<ELFT>>(Piece.ID); |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 303 | unsigned FirstRelI = Piece.FirstRelocation; |
| 304 | if (FirstRelI == (unsigned)-1) |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 305 | fatal(toString(Sec) + ": FDE doesn't reference another section"); |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 306 | const RelTy &Rel = Rels[FirstRelI]; |
| 307 | SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 308 | auto *D = dyn_cast<DefinedRegular<ELFT>>(&B); |
| 309 | if (!D || !D->Section) |
| 310 | return false; |
| 311 | InputSectionBase<ELFT> *Target = D->Section->Repl; |
| 312 | return Target && Target->Live; |
| 313 | } |
| 314 | |
| 315 | // .eh_frame is a sequence of CIE or FDE records. In general, there |
| 316 | // is one CIE record per input object file which is followed by |
| 317 | // a list of FDEs. This function searches an existing CIE or create a new |
| 318 | // one and associates FDEs to the CIE. |
| Rui Ueyama | c0c9260 | 2016-02-05 22:56:03 +0000 | [diff] [blame] | 319 | template <class ELFT> |
| Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 320 | template <class RelTy> |
| Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 321 | void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec, |
| Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 322 | ArrayRef<RelTy> Rels) { |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 323 | const endianness E = ELFT::TargetEndianness; |
| Rafael Espindola | 820f4bb | 2016-05-24 15:17:47 +0000 | [diff] [blame] | 324 | |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 325 | DenseMap<size_t, CieRecord *> OffsetToCie; |
| Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 326 | for (EhSectionPiece &Piece : Sec->Pieces) { |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 327 | // The empty record is the end marker. |
| Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 328 | if (Piece.size() == 4) |
| Rafael Espindola | 5ee9e7f | 2016-05-24 19:14:09 +0000 | [diff] [blame] | 329 | return; |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 330 | |
| 331 | size_t Offset = Piece.InputOff; |
| Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 332 | uint32_t ID = read32<E>(Piece.data().data() + 4); |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 333 | if (ID == 0) { |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 334 | OffsetToCie[Offset] = addCie(Piece, Rels); |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 335 | continue; |
| 336 | } |
| 337 | |
| 338 | uint32_t CieOffset = Offset + 4 - ID; |
| 339 | CieRecord *Cie = OffsetToCie[CieOffset]; |
| 340 | if (!Cie) |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 341 | fatal(toString(Sec) + ": invalid CIE reference"); |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 342 | |
| Eugene Leviant | c8c1b7b | 2016-11-25 08:27:15 +0000 | [diff] [blame] | 343 | if (!isFdeLive(Piece, Rels)) |
| Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 344 | continue; |
| 345 | Cie->FdePieces.push_back(&Piece); |
| Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 346 | NumFdes++; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | template <class ELFT> |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 351 | void EhOutputSection<ELFT>::addSection(InputSectionData *C) { |
| Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 352 | auto *Sec = cast<EhInputSection<ELFT>>(C); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 353 | Sec->OutSec = this; |
| Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 354 | this->updateAlignment(Sec->Alignment); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 355 | Sections.push_back(Sec); |
| 356 | |
| 357 | // .eh_frame is a sequence of CIE or FDE records. This function |
| 358 | // splits it into pieces so that we can call |
| 359 | // SplitInputSection::getSectionPiece on the section. |
| Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 360 | Sec->split(); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 361 | if (Sec->Pieces.empty()) |
| 362 | return; |
| 363 | |
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 364 | if (Sec->NumRelocations) { |
| 365 | if (Sec->AreRelocsRela) |
| 366 | addSectionAux(Sec, Sec->relas()); |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 367 | else |
| Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 368 | addSectionAux(Sec, Sec->rels()); |
| Rui Ueyama | 0de86c1 | 2016-01-27 22:23:44 +0000 | [diff] [blame] | 369 | return; |
| 370 | } |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 371 | addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 374 | template <class ELFT> |
| Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 375 | static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { |
| 376 | memcpy(Buf, D.data(), D.size()); |
| Rui Ueyama | c0449a6 | 2016-05-21 18:10:13 +0000 | [diff] [blame] | 377 | |
| 378 | // Fix the size field. -4 since size does not include the size field itself. |
| Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 379 | const endianness E = ELFT::TargetEndianness; |
| Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 380 | write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 383 | template <class ELFT> void EhOutputSection<ELFT>::finalize() { |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 384 | if (this->Size) |
| Rui Ueyama | e9381bd | 2016-07-16 02:47:42 +0000 | [diff] [blame] | 385 | return; // Already finalized. |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 386 | |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 387 | size_t Off = 0; |
| 388 | for (CieRecord *Cie : Cies) { |
| 389 | Cie->Piece->OutputOff = Off; |
| 390 | Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 391 | |
| Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 392 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 393 | Fde->OutputOff = Off; |
| 394 | Off += alignTo(Fde->size(), sizeof(uintX_t)); |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 397 | this->Size = Off; |
| Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 400 | template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { |
| 401 | const endianness E = ELFT::TargetEndianness; |
| 402 | switch (Size) { |
| 403 | case DW_EH_PE_udata2: |
| 404 | return read16<E>(Buf); |
| 405 | case DW_EH_PE_udata4: |
| 406 | return read32<E>(Buf); |
| 407 | case DW_EH_PE_udata8: |
| 408 | return read64<E>(Buf); |
| 409 | case DW_EH_PE_absptr: |
| 410 | if (ELFT::Is64Bits) |
| 411 | return read64<E>(Buf); |
| 412 | return read32<E>(Buf); |
| 413 | } |
| 414 | fatal("unknown FDE size encoding"); |
| 415 | } |
| 416 | |
| 417 | // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. |
| 418 | // We need it to create .eh_frame_hdr section. |
| 419 | template <class ELFT> |
| Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 420 | typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 421 | uint8_t Enc) { |
| Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 422 | // The starting address to which this FDE applies is |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 423 | // stored at FDE + 8 byte. |
| 424 | size_t Off = FdeOff + 8; |
| 425 | uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); |
| 426 | if ((Enc & 0x70) == DW_EH_PE_absptr) |
| 427 | return Addr; |
| 428 | if ((Enc & 0x70) == DW_EH_PE_pcrel) |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 429 | return Addr + this->Addr + Off; |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 430 | fatal("unknown FDE size relative encoding"); |
| 431 | } |
| 432 | |
| Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 433 | template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 434 | const endianness E = ELFT::TargetEndianness; |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 435 | for (CieRecord *Cie : Cies) { |
| 436 | size_t CieOffset = Cie->Piece->OutputOff; |
| Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 437 | writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 438 | |
| Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 439 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 440 | size_t Off = Fde->OutputOff; |
| Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 441 | writeCieFde<ELFT>(Buf + Off, Fde->data()); |
| Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 442 | |
| Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 443 | // FDE's second word should have the offset to an associated CIE. |
| 444 | // Write it. |
| 445 | write32<E>(Buf + Off + 4, Off + 4 - CieOffset); |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 449 | for (EhInputSection<ELFT> *S : Sections) |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 450 | S->relocate(Buf, nullptr); |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 451 | |
| 452 | // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table |
| Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 453 | // to get a FDE from an address to which FDE is applied. So here |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 454 | // we obtain two addresses and pass them to EhFrameHdr object. |
| Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 455 | if (In<ELFT>::EhFrameHdr) { |
| Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 456 | for (CieRecord *Cie : Cies) { |
| Eugene Leviant | 531df4f | 2016-11-23 09:45:17 +0000 | [diff] [blame] | 457 | uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece); |
| Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 458 | for (SectionPiece *Fde : Cie->FdePieces) { |
| Rui Ueyama | 6de2e68 | 2016-05-24 02:08:38 +0000 | [diff] [blame] | 459 | uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 460 | uintX_t FdeVA = this->Addr + Fde->OutputOff; |
| Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 461 | In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); |
| Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 462 | } |
| Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | template <class ELFT> |
| Rui Ueyama | d97e5c4 | 2016-01-07 18:33:11 +0000 | [diff] [blame] | 468 | MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type, |
| Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 469 | uintX_t Flags, uintX_t Alignment) |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 470 | : OutputSectionBase(Name, Type, Flags), |
| Rui Ueyama | 818bb2f | 2016-07-16 18:55:47 +0000 | [diff] [blame] | 471 | Builder(StringTableBuilder::RAW, Alignment) {} |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 472 | |
| 473 | template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
| Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 474 | Builder.write(Buf); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 477 | template <class ELFT> |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 478 | void MergeOutputSection<ELFT>::addSection(InputSectionData *C) { |
| Rui Ueyama | 1080351 | 2016-05-22 00:25:30 +0000 | [diff] [blame] | 479 | auto *Sec = cast<MergeInputSection<ELFT>>(C); |
| 480 | Sec->OutSec = this; |
| Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 481 | this->updateAlignment(Sec->Alignment); |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 482 | this->Entsize = Sec->Entsize; |
| Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 483 | Sections.push_back(Sec); |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { |
| Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 487 | return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2; |
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| Rui Ueyama | 1880bbe | 2016-11-26 15:09:58 +0000 | [diff] [blame] | 490 | template <class ELFT> void MergeOutputSection<ELFT>::finalizeTailMerge() { |
| Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 491 | // Add all string pieces to the string table builder to create section |
| Rui Ueyama | 1880bbe | 2016-11-26 15:09:58 +0000 | [diff] [blame] | 492 | // contents. |
| 493 | for (MergeInputSection<ELFT> *Sec : Sections) |
| 494 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 495 | if (Sec->Pieces[I].Live) |
| 496 | Builder.add(Sec->getData(I)); |
| Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 497 | |
| Rui Ueyama | 1880bbe | 2016-11-26 15:09:58 +0000 | [diff] [blame] | 498 | // Fix the string table content. After this, the contents will never change. |
| 499 | Builder.finalize(); |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 500 | this->Size = Builder.getSize(); |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 501 | |
| Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 502 | // finalize() fixed tail-optimized strings, so we can now get |
| 503 | // offsets of strings. Get an offset for each string and save it |
| 504 | // to a corresponding StringPiece for easy access. |
| Rui Ueyama | 1880bbe | 2016-11-26 15:09:58 +0000 | [diff] [blame] | 505 | for (MergeInputSection<ELFT> *Sec : Sections) |
| 506 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 507 | if (Sec->Pieces[I].Live) |
| 508 | Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); |
| 509 | } |
| 510 | |
| 511 | template <class ELFT> void MergeOutputSection<ELFT>::finalizeNoTailMerge() { |
| 512 | // Add all string pieces to the string table builder to create section |
| 513 | // contents. Because we are not tail-optimizing, offsets of strings are |
| 514 | // fixed when they are added to the builder (string table builder contains |
| 515 | // a hash table from strings to offsets). |
| 516 | for (MergeInputSection<ELFT> *Sec : Sections) |
| 517 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 518 | if (Sec->Pieces[I].Live) |
| 519 | Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I)); |
| 520 | |
| 521 | Builder.finalizeInOrder(); |
| 522 | this->Size = Builder.getSize(); |
| 523 | } |
| 524 | |
| 525 | template <class ELFT> void MergeOutputSection<ELFT>::finalize() { |
| Rui Ueyama | 5c851a5 | 2016-11-18 19:45:04 +0000 | [diff] [blame] | 526 | if (shouldTailMerge()) |
| Rui Ueyama | 1880bbe | 2016-11-26 15:09:58 +0000 | [diff] [blame] | 527 | finalizeTailMerge(); |
| 528 | else |
| 529 | finalizeNoTailMerge(); |
| Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 532 | template <class ELFT> |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 533 | static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) { |
| Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 534 | return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | template <class ELFT> |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 538 | static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, |
| 539 | StringRef OutsecName) { |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 540 | typedef typename ELFT::uint uintX_t; |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 541 | uintX_t Flags = getOutFlags(C); |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 542 | |
| 543 | // For SHF_MERGE we create different output sections for each alignment. |
| 544 | // This makes each output section simple and keeps a single level mapping from |
| 545 | // input to output. |
| Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 546 | // In case of relocatable object generation we do not try to perform merging |
| 547 | // and treat SHF_MERGE sections as regular ones, but also create different |
| 548 | // output sections for them to allow merging at final linking stage. |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 549 | uintX_t Alignment = 0; |
| Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 550 | if (isa<MergeInputSection<ELFT>>(C) || |
| Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 551 | (Config->Relocatable && (C->Flags & SHF_MERGE))) |
| 552 | Alignment = std::max<uintX_t>(C->Alignment, C->Entsize); |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 553 | |
| Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 554 | return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment}; |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | template <class ELFT> |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 558 | std::pair<OutputSectionBase *, bool> |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 559 | OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C, |
| 560 | StringRef OutsecName) { |
| 561 | SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName); |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 562 | return create(Key, C); |
| 563 | } |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 564 | |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 565 | template <class ELFT> |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 566 | std::pair<OutputSectionBase *, bool> |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 567 | OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key, |
| 568 | InputSectionBase<ELFT> *C) { |
| 569 | uintX_t Flags = getOutFlags(C); |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 570 | OutputSectionBase *&Sec = Map[Key]; |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 571 | if (Sec) { |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 572 | Sec->Flags |= Flags; |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 573 | return {Sec, false}; |
| 574 | } |
| 575 | |
| Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 576 | uint32_t Type = C->Type; |
| Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 577 | switch (C->kind()) { |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 578 | case InputSectionBase<ELFT>::Regular: |
| Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 579 | case InputSectionBase<ELFT>::Synthetic: |
| Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 580 | Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags); |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 581 | break; |
| 582 | case InputSectionBase<ELFT>::EHFrame: |
| 583 | return {Out<ELFT>::EhFrame, false}; |
| 584 | case InputSectionBase<ELFT>::Merge: |
| Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 585 | Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment); |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 586 | break; |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 587 | } |
| 588 | return {Sec, true}; |
| 589 | } |
| 590 | |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 591 | template <bool Is64Bits> |
| 592 | typename lld::elf::SectionKey<Is64Bits> |
| 593 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() { |
| 594 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0}; |
| 595 | } |
| 596 | |
| 597 | template <bool Is64Bits> |
| 598 | typename lld::elf::SectionKey<Is64Bits> |
| 599 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() { |
| 600 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0, |
| 601 | 0}; |
| 602 | } |
| 603 | |
| 604 | template <bool Is64Bits> |
| 605 | unsigned |
| 606 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) { |
| 607 | return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment); |
| 608 | } |
| 609 | |
| 610 | template <bool Is64Bits> |
| 611 | bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS, |
| 612 | const Key &RHS) { |
| 613 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| 614 | LHS.Type == RHS.Type && LHS.Flags == RHS.Flags && |
| 615 | LHS.Alignment == RHS.Alignment; |
| 616 | } |
| 617 | |
| 618 | namespace llvm { |
| 619 | template struct DenseMapInfo<SectionKey<true>>; |
| 620 | template struct DenseMapInfo<SectionKey<false>>; |
| 621 | } |
| 622 | |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 623 | namespace lld { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 624 | namespace elf { |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 625 | |
| 626 | template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 627 | template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 628 | template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 629 | template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 630 | |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 631 | template class OutputSection<ELF32LE>; |
| 632 | template class OutputSection<ELF32BE>; |
| 633 | template class OutputSection<ELF64LE>; |
| 634 | template class OutputSection<ELF64BE>; |
| 635 | |
| Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 636 | template class EhOutputSection<ELF32LE>; |
| 637 | template class EhOutputSection<ELF32BE>; |
| 638 | template class EhOutputSection<ELF64LE>; |
| 639 | template class EhOutputSection<ELF64BE>; |
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 640 | |
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 641 | template class MergeOutputSection<ELF32LE>; |
| 642 | template class MergeOutputSection<ELF32BE>; |
| 643 | template class MergeOutputSection<ELF64LE>; |
| 644 | template class MergeOutputSection<ELF64BE>; |
| 645 | |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 646 | template class OutputSectionFactory<ELF32LE>; |
| 647 | template class OutputSectionFactory<ELF32BE>; |
| 648 | template class OutputSectionFactory<ELF64LE>; |
| 649 | template class OutputSectionFactory<ELF64BE>; |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 650 | } |
| 651 | } |