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" |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 13 | #include "GdbIndex.h" |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 14 | #include "LinkerScript.h" |
| 15 | #include "Memory.h" |
Rui Ueyama | fbbde54 | 2016-06-29 09:08:02 +0000 | [diff] [blame] | 16 | #include "Strings.h" |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 17 | #include "SymbolListFile.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 18 | #include "SymbolTable.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 19 | #include "SyntheticSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 20 | #include "Target.h" |
Rui Ueyama | e980950 | 2016-03-11 04:23:12 +0000 | [diff] [blame] | 21 | #include "lld/Core/Parallel.h" |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Dwarf.h" |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MD5.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | a42b3bc | 2016-09-27 16:43:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/SHA1.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 26 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Rui Ueyama | dbcfedb | 2016-02-09 21:46:11 +0000 | [diff] [blame] | 28 | using namespace llvm::dwarf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 30 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 31 | using namespace llvm::ELF; |
| 32 | |
| 33 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 34 | using namespace lld::elf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 36 | OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type, |
| 37 | uint64_t Flags) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 38 | : Name(Name) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 39 | this->Type = Type; |
| 40 | this->Flags = Flags; |
| 41 | this->Addralign = 1; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 44 | uint32_t OutputSectionBase::getPhdrFlags() const { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 45 | uint32_t Ret = PF_R; |
| 46 | if (Flags & SHF_WRITE) |
| 47 | Ret |= PF_W; |
| 48 | if (Flags & SHF_EXECINSTR) |
| 49 | Ret |= PF_X; |
| 50 | return Ret; |
| 51 | } |
| 52 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 53 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 54 | void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 55 | Shdr->sh_entsize = Entsize; |
| 56 | Shdr->sh_addralign = Addralign; |
| 57 | Shdr->sh_type = Type; |
| 58 | Shdr->sh_offset = Offset; |
| 59 | Shdr->sh_flags = Flags; |
| 60 | Shdr->sh_info = Info; |
| 61 | Shdr->sh_link = Link; |
| 62 | Shdr->sh_addr = Addr; |
| 63 | Shdr->sh_size = Size; |
| 64 | Shdr->sh_name = ShName; |
Rui Ueyama | c63c1db | 2016-03-13 06:50:33 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | template <class ELFT> |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 68 | GdbIndexSection<ELFT>::GdbIndexSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 69 | : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {} |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 70 | |
| 71 | template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() { |
| 72 | std::vector<InputSection<ELFT> *> &IS = |
| 73 | static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections; |
| 74 | |
| 75 | for (InputSection<ELFT> *I : IS) |
| 76 | readDwarf(I); |
| 77 | } |
| 78 | |
| 79 | template <class ELFT> |
| 80 | void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) { |
| 81 | std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I); |
| 82 | CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end()); |
| 83 | } |
| 84 | |
| 85 | template <class ELFT> void GdbIndexSection<ELFT>::finalize() { |
| 86 | parseDebugSections(); |
| 87 | |
| 88 | // GdbIndex header consist from version fields |
| 89 | // and 5 more fields with different kinds of offsets. |
| 90 | CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 91 | this->Size = CuTypesOffset; |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) { |
| 95 | write32le(Buf, 7); // Write Version |
| 96 | write32le(Buf + 4, CuListOffset); // CU list offset |
| 97 | write32le(Buf + 8, CuTypesOffset); // Types CU list offset |
| 98 | write32le(Buf + 12, CuTypesOffset); // Address area offset |
| 99 | write32le(Buf + 16, CuTypesOffset); // Symbol table offset |
| 100 | write32le(Buf + 20, CuTypesOffset); // Constant pool offset |
| 101 | Buf += 24; |
| 102 | |
| 103 | // Write the CU list. |
| 104 | for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) { |
| 105 | write64le(Buf, CU.first); |
| 106 | write64le(Buf + 8, CU.second); |
| 107 | Buf += 16; |
| 108 | } |
| 109 | } |
| 110 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 111 | // Returns the number of version definition entries. Because the first entry |
| 112 | // is for the version definition itself, it is the number of versioned symbols |
| 113 | // plus one. Note that we don't support multiple versions yet. |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 114 | static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 115 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 116 | template <class ELFT> |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 117 | EhFrameHeader<ELFT>::EhFrameHeader() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 118 | : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {} |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 119 | |
Rui Ueyama | 95a232e | 2016-05-23 01:45:05 +0000 | [diff] [blame] | 120 | // .eh_frame_hdr contains a binary search table of pointers to FDEs. |
| 121 | // Each entry of the search table consists of two values, |
| 122 | // the starting PC from where FDEs covers, and the FDE's address. |
| 123 | // It is sorted by PC. |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 124 | template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { |
| 125 | const endianness E = ELFT::TargetEndianness; |
| 126 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 127 | // Sort the FDE list by their PC and uniqueify. Usually there is only |
| 128 | // one FDE for a PC (i.e. function), but if ICF merges two functions |
| 129 | // into one, there can be more than one FDEs pointing to the address. |
| 130 | auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; |
| 131 | std::stable_sort(Fdes.begin(), Fdes.end(), Less); |
| 132 | auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; |
| 133 | Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); |
Peter Collingbourne | c98de13 | 2016-04-11 16:40:08 +0000 | [diff] [blame] | 134 | |
Rui Ueyama | 1b2936f | 2016-05-23 01:31:10 +0000 | [diff] [blame] | 135 | Buf[0] = 1; |
| 136 | Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 137 | Buf[2] = DW_EH_PE_udata4; |
| 138 | Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 139 | write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4); |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 140 | write32<E>(Buf + 8, Fdes.size()); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 141 | Buf += 12; |
| 142 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 143 | uintX_t VA = this->Addr; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 144 | for (FdeData &Fde : Fdes) { |
| 145 | write32<E>(Buf, Fde.Pc - VA); |
| 146 | write32<E>(Buf + 4, Fde.FdeVA - VA); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 147 | Buf += 8; |
| 148 | } |
| 149 | } |
| 150 | |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 151 | template <class ELFT> void EhFrameHeader<ELFT>::finalize() { |
| 152 | // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 153 | this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8; |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 154 | } |
| 155 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 156 | template <class ELFT> |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 157 | void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { |
| 158 | Fdes.push_back({Pc, FdeVA}); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 161 | template <class ELFT> static uint64_t getEntsize(uint32_t Type) { |
| 162 | switch (Type) { |
| 163 | case SHT_RELA: |
| 164 | return sizeof(typename ELFT::Rela); |
| 165 | case SHT_REL: |
| 166 | return sizeof(typename ELFT::Rel); |
| 167 | case SHT_MIPS_REGINFO: |
| 168 | return sizeof(Elf_Mips_RegInfo<ELFT>); |
| 169 | case SHT_MIPS_OPTIONS: |
| 170 | return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>); |
| 171 | case SHT_MIPS_ABIFLAGS: |
| 172 | return sizeof(Elf_Mips_ABIFlags<ELFT>); |
| 173 | default: |
| 174 | return 0; |
| 175 | } |
| 176 | } |
| 177 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 178 | template <class ELFT> |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 179 | OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 180 | : OutputSectionBase(Name, Type, Flags) { |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 181 | this->Entsize = getEntsize<ELFT>(Type); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 184 | template <typename ELFT> |
| 185 | static bool compareByFilePosition(InputSection<ELFT> *A, |
| 186 | InputSection<ELFT> *B) { |
| 187 | auto *LA = cast<InputSection<ELFT>>(A->getLinkOrderDep()); |
| 188 | auto *LB = cast<InputSection<ELFT>>(B->getLinkOrderDep()); |
| 189 | OutputSectionBase *AOut = LA->OutSec; |
| 190 | OutputSectionBase *BOut = LB->OutSec; |
| 191 | if (AOut != BOut) |
| 192 | return AOut->SectionIndex < BOut->SectionIndex; |
| 193 | return LA->OutSecOff < LB->OutSecOff; |
| 194 | } |
| 195 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 196 | template <class ELFT> void OutputSection<ELFT>::finalize() { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 197 | if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) { |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 198 | std::sort(Sections.begin(), Sections.end(), compareByFilePosition<ELFT>); |
| 199 | Size = 0; |
| 200 | assignOffsets(); |
| 201 | |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 202 | // We must preserve the link order dependency of sections with the |
| 203 | // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We |
| 204 | // need to translate the InputSection sh_link to the OutputSection sh_link, |
| 205 | // all InputSections in the OutputSection have the same dependency. |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 206 | if (auto *D = this->Sections.front()->getLinkOrderDep()) |
| 207 | this->Link = D->OutSec->SectionIndex; |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 208 | } |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 209 | |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 210 | uint32_t Type = this->Type; |
| 211 | if (!Config->Relocatable || (Type != SHT_RELA && Type != SHT_REL)) |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 212 | return; |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 213 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 214 | this->Link = In<ELFT>::SymTab->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 215 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 216 | // the section to which the relocation applies. |
| 217 | InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 218 | this->Info = S->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 219 | } |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 220 | |
| 221 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 222 | void OutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 223 | assert(C->Live); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 224 | auto *S = cast<InputSection<ELFT>>(C); |
| 225 | Sections.push_back(S); |
| 226 | S->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 227 | this->updateAlignment(S->Alignment); |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 228 | // Keep sh_entsize value of the input section to be able to perform merging |
| 229 | // later during a final linking using the generated relocatable object. |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 230 | if (Config->Relocatable && (S->Flags & SHF_MERGE)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 231 | this->Entsize = S->Entsize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 234 | // This function is called after we sort input sections |
| 235 | // and scan relocations to setup sections' offsets. |
| 236 | template <class ELFT> void OutputSection<ELFT>::assignOffsets() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 237 | uintX_t Off = this->Size; |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 238 | for (InputSection<ELFT> *S : Sections) { |
| 239 | Off = alignTo(Off, S->Alignment); |
| 240 | S->OutSecOff = Off; |
| 241 | Off += S->getSize(); |
| 242 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 243 | this->Size = Off; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 244 | } |
| 245 | |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 246 | template <class ELFT> |
| 247 | void OutputSection<ELFT>::sort( |
| 248 | std::function<unsigned(InputSection<ELFT> *S)> Order) { |
| 249 | typedef std::pair<unsigned, InputSection<ELFT> *> Pair; |
| 250 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 251 | |
| 252 | std::vector<Pair> V; |
| 253 | for (InputSection<ELFT> *S : Sections) |
| 254 | V.push_back({Order(S), S}); |
| 255 | std::stable_sort(V.begin(), V.end(), Comp); |
| 256 | Sections.clear(); |
| 257 | for (Pair &P : V) |
| 258 | Sections.push_back(P.second); |
| 259 | } |
| 260 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 261 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 262 | // 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] | 263 | // We want to keep the original order if the priorities are the same |
| 264 | // because the compiler keeps the original initialization order in a |
| 265 | // translation unit and we need to respect that. |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 266 | // 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] | 267 | template <class ELFT> void OutputSection<ELFT>::sortInitFini() { |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 268 | // Sort sections by priority. |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 269 | sort([](InputSection<ELFT> *S) { return getPriority(S->Name); }); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 270 | } |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 271 | |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 272 | // Returns true if S matches /Filename.?\.o$/. |
| 273 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 274 | if (!S.endswith(".o")) |
| 275 | return false; |
| 276 | S = S.drop_back(2); |
| 277 | if (S.endswith(Filename)) |
| 278 | return true; |
| 279 | return !S.empty() && S.drop_back().endswith(Filename); |
| 280 | } |
| 281 | |
| 282 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 283 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 284 | |
| 285 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 286 | // |
| 287 | // 1. The section was contained in crtbegin (crtbegin contains |
| 288 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 289 | // can find the beginning of the sections.) |
| 290 | // |
| 291 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 292 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 293 | // they are compared as string rather than number. |
| 294 | // |
| 295 | // 3. The section is just ".ctors" or ".dtors". |
| 296 | // |
| 297 | // 4. The section was contained in crtend, which contains an end marker. |
| 298 | // |
| 299 | // In an ideal world, we don't need this function because .init_array and |
| 300 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 301 | // are too many real-world use cases of .ctors, so we had no choice to |
| 302 | // support that with this rather ad-hoc semantics. |
| 303 | template <class ELFT> |
| 304 | static bool compCtors(const InputSection<ELFT> *A, |
| 305 | const InputSection<ELFT> *B) { |
| 306 | bool BeginA = isCrtbegin(A->getFile()->getName()); |
| 307 | bool BeginB = isCrtbegin(B->getFile()->getName()); |
| 308 | if (BeginA != BeginB) |
| 309 | return BeginA; |
| 310 | bool EndA = isCrtend(A->getFile()->getName()); |
| 311 | bool EndB = isCrtend(B->getFile()->getName()); |
| 312 | if (EndA != EndB) |
| 313 | return EndB; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 314 | StringRef X = A->Name; |
| 315 | StringRef Y = B->Name; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 316 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 317 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 318 | X = X.substr(6); |
| 319 | Y = Y.substr(6); |
Rui Ueyama | 24b794e | 2016-02-12 00:38:46 +0000 | [diff] [blame] | 320 | if (X.empty() && Y.empty()) |
| 321 | return false; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 322 | return X < Y; |
| 323 | } |
| 324 | |
| 325 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 326 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 327 | // Read the comment above. |
| 328 | template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() { |
| 329 | std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame^] | 332 | // Fill [Buf, Buf + Size) with Filler. Filler is written in big |
| 333 | // endian order. This is used for linker script "=fillexp" command. |
| 334 | void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { |
| 335 | uint8_t V[4]; |
| 336 | write32be(V, Filler); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 337 | size_t I = 0; |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame^] | 338 | for (; I + 4 < Size; I += 4) |
| 339 | memcpy(Buf + I, V, 4); |
| 340 | memcpy(Buf + I, V, Size - I); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 343 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame^] | 344 | if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 345 | fill(Buf, this->Size, Filler); |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame^] | 346 | |
Davide Italiano | 2e8b2a7 | 2016-11-18 02:23:48 +0000 | [diff] [blame] | 347 | auto Fn = [=](InputSection<ELFT> *IS) { IS->writeTo(Buf); }; |
Davide Italiano | 44665e7 | 2016-11-18 02:18:04 +0000 | [diff] [blame] | 348 | if (Config->Threads) |
| 349 | parallel_for_each(Sections.begin(), Sections.end(), Fn); |
| 350 | else |
| 351 | std::for_each(Sections.begin(), Sections.end(), Fn); |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame^] | 352 | |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 353 | // Linker scripts may have BYTE()-family commands with which you |
| 354 | // can write arbitrary bytes to the output. Process them if any. |
| 355 | Script<ELFT>::X->writeDataBytes(this->Name, Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 358 | template <class ELFT> |
Rui Ueyama | f86cb90 | 2016-05-23 15:12:41 +0000 | [diff] [blame] | 359 | EhOutputSection<ELFT>::EhOutputSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 360 | : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {} |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 361 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 362 | // Search for an existing CIE record or create a new one. |
| 363 | // CIE records from input object files are uniquified by their contents |
| 364 | // and where their relocations point to. |
| 365 | template <class ELFT> |
| 366 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 367 | CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece, |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 368 | EhInputSection<ELFT> *Sec, |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 369 | ArrayRef<RelTy> Rels) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 370 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 371 | if (read32<E>(Piece.data().data() + 4) != 0) |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 372 | fatal("CIE expected at beginning of .eh_frame: " + Sec->Name); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 373 | |
| 374 | SymbolBody *Personality = nullptr; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 375 | unsigned FirstRelI = Piece.FirstRelocation; |
| 376 | if (FirstRelI != (unsigned)-1) |
| 377 | Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 378 | |
| 379 | // Search for an existing CIE by CIE contents/relocation target pair. |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 380 | CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 381 | |
| 382 | // If not found, create a new one. |
Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 383 | if (Cie->Piece == nullptr) { |
| 384 | Cie->Piece = &Piece; |
Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 385 | Cies.push_back(Cie); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 386 | } |
| 387 | return Cie; |
| 388 | } |
| 389 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 390 | // There is one FDE per function. Returns true if a given FDE |
| 391 | // points to a live function. |
| 392 | template <class ELFT> |
| 393 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 394 | bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece, |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 395 | EhInputSection<ELFT> *Sec, |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 396 | ArrayRef<RelTy> Rels) { |
| 397 | unsigned FirstRelI = Piece.FirstRelocation; |
| 398 | if (FirstRelI == (unsigned)-1) |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 399 | fatal("FDE doesn't reference another section"); |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 400 | const RelTy &Rel = Rels[FirstRelI]; |
| 401 | SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 402 | auto *D = dyn_cast<DefinedRegular<ELFT>>(&B); |
| 403 | if (!D || !D->Section) |
| 404 | return false; |
| 405 | InputSectionBase<ELFT> *Target = D->Section->Repl; |
| 406 | return Target && Target->Live; |
| 407 | } |
| 408 | |
| 409 | // .eh_frame is a sequence of CIE or FDE records. In general, there |
| 410 | // is one CIE record per input object file which is followed by |
| 411 | // a list of FDEs. This function searches an existing CIE or create a new |
| 412 | // one and associates FDEs to the CIE. |
Rui Ueyama | c0c9260 | 2016-02-05 22:56:03 +0000 | [diff] [blame] | 413 | template <class ELFT> |
Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 414 | template <class RelTy> |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 415 | void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec, |
Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 416 | ArrayRef<RelTy> Rels) { |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 417 | const endianness E = ELFT::TargetEndianness; |
Rafael Espindola | 820f4bb | 2016-05-24 15:17:47 +0000 | [diff] [blame] | 418 | |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 419 | DenseMap<size_t, CieRecord *> OffsetToCie; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 420 | for (EhSectionPiece &Piece : Sec->Pieces) { |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 421 | // The empty record is the end marker. |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 422 | if (Piece.size() == 4) |
Rafael Espindola | 5ee9e7f | 2016-05-24 19:14:09 +0000 | [diff] [blame] | 423 | return; |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 424 | |
| 425 | size_t Offset = Piece.InputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 426 | uint32_t ID = read32<E>(Piece.data().data() + 4); |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 427 | if (ID == 0) { |
| 428 | OffsetToCie[Offset] = addCie(Piece, Sec, Rels); |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | uint32_t CieOffset = Offset + 4 - ID; |
| 433 | CieRecord *Cie = OffsetToCie[CieOffset]; |
| 434 | if (!Cie) |
| 435 | fatal("invalid CIE reference"); |
| 436 | |
| 437 | if (!isFdeLive(Piece, Sec, Rels)) |
| 438 | continue; |
| 439 | Cie->FdePieces.push_back(&Piece); |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 440 | NumFdes++; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 445 | void EhOutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 446 | auto *Sec = cast<EhInputSection<ELFT>>(C); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 447 | Sec->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 448 | this->updateAlignment(Sec->Alignment); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 449 | Sections.push_back(Sec); |
| 450 | |
| 451 | // .eh_frame is a sequence of CIE or FDE records. This function |
| 452 | // splits it into pieces so that we can call |
| 453 | // SplitInputSection::getSectionPiece on the section. |
Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 454 | Sec->split(); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 455 | if (Sec->Pieces.empty()) |
| 456 | return; |
| 457 | |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 458 | if (Sec->NumRelocations) { |
| 459 | if (Sec->AreRelocsRela) |
| 460 | addSectionAux(Sec, Sec->relas()); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 461 | else |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 462 | addSectionAux(Sec, Sec->rels()); |
Rui Ueyama | 0de86c1 | 2016-01-27 22:23:44 +0000 | [diff] [blame] | 463 | return; |
| 464 | } |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 465 | addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 468 | template <class ELFT> |
Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 469 | static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { |
| 470 | memcpy(Buf, D.data(), D.size()); |
Rui Ueyama | c0449a6 | 2016-05-21 18:10:13 +0000 | [diff] [blame] | 471 | |
| 472 | // 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] | 473 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 474 | write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 477 | template <class ELFT> void EhOutputSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 478 | if (this->Size) |
Rui Ueyama | e9381bd | 2016-07-16 02:47:42 +0000 | [diff] [blame] | 479 | return; // Already finalized. |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 480 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 481 | size_t Off = 0; |
| 482 | for (CieRecord *Cie : Cies) { |
| 483 | Cie->Piece->OutputOff = Off; |
| 484 | Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 485 | |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 486 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 487 | Fde->OutputOff = Off; |
| 488 | Off += alignTo(Fde->size(), sizeof(uintX_t)); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 491 | this->Size = Off; |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 494 | template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { |
| 495 | const endianness E = ELFT::TargetEndianness; |
| 496 | switch (Size) { |
| 497 | case DW_EH_PE_udata2: |
| 498 | return read16<E>(Buf); |
| 499 | case DW_EH_PE_udata4: |
| 500 | return read32<E>(Buf); |
| 501 | case DW_EH_PE_udata8: |
| 502 | return read64<E>(Buf); |
| 503 | case DW_EH_PE_absptr: |
| 504 | if (ELFT::Is64Bits) |
| 505 | return read64<E>(Buf); |
| 506 | return read32<E>(Buf); |
| 507 | } |
| 508 | fatal("unknown FDE size encoding"); |
| 509 | } |
| 510 | |
| 511 | // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. |
| 512 | // We need it to create .eh_frame_hdr section. |
| 513 | template <class ELFT> |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 514 | typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 515 | uint8_t Enc) { |
Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 516 | // The starting address to which this FDE applies is |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 517 | // stored at FDE + 8 byte. |
| 518 | size_t Off = FdeOff + 8; |
| 519 | uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); |
| 520 | if ((Enc & 0x70) == DW_EH_PE_absptr) |
| 521 | return Addr; |
| 522 | if ((Enc & 0x70) == DW_EH_PE_pcrel) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 523 | return Addr + this->Addr + Off; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 524 | fatal("unknown FDE size relative encoding"); |
| 525 | } |
| 526 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 527 | template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 528 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 529 | for (CieRecord *Cie : Cies) { |
| 530 | size_t CieOffset = Cie->Piece->OutputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 531 | writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 532 | |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 533 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 534 | size_t Off = Fde->OutputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 535 | writeCieFde<ELFT>(Buf + Off, Fde->data()); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 536 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 537 | // FDE's second word should have the offset to an associated CIE. |
| 538 | // Write it. |
| 539 | write32<E>(Buf + Off + 4, Off + 4 - CieOffset); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 543 | for (EhInputSection<ELFT> *S : Sections) |
Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 544 | S->relocate(Buf, nullptr); |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 545 | |
| 546 | // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table |
Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 547 | // 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] | 548 | // we obtain two addresses and pass them to EhFrameHdr object. |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 549 | if (Out<ELFT>::EhFrameHdr) { |
| 550 | for (CieRecord *Cie : Cies) { |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 551 | uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data()); |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 552 | for (SectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | 6de2e68 | 2016-05-24 02:08:38 +0000 | [diff] [blame] | 553 | uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 554 | uintX_t FdeVA = this->Addr + Fde->OutputOff; |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 555 | Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); |
| 556 | } |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | template <class ELFT> |
Rui Ueyama | d97e5c4 | 2016-01-07 18:33:11 +0000 | [diff] [blame] | 562 | MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type, |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 563 | uintX_t Flags, uintX_t Alignment) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 564 | : OutputSectionBase(Name, Type, Flags), |
Rui Ueyama | 818bb2f | 2016-07-16 18:55:47 +0000 | [diff] [blame] | 565 | Builder(StringTableBuilder::RAW, Alignment) {} |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 566 | |
| 567 | template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 568 | Builder.write(Buf); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 571 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 572 | void MergeOutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 1080351 | 2016-05-22 00:25:30 +0000 | [diff] [blame] | 573 | auto *Sec = cast<MergeInputSection<ELFT>>(C); |
| 574 | Sec->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 575 | this->updateAlignment(Sec->Alignment); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 576 | this->Entsize = Sec->Entsize; |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 577 | Sections.push_back(Sec); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 581 | return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | template <class ELFT> void MergeOutputSection<ELFT>::finalize() { |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 585 | // Add all string pieces to the string table builder to create section |
| 586 | // contents. If we are not tail-optimizing, offsets of strings are fixed |
| 587 | // when they are added to the builder (string table builder contains a |
| 588 | // hash table from strings to offsets), so we record them if available. |
| 589 | for (MergeInputSection<ELFT> *Sec : Sections) { |
| 590 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) { |
| 591 | if (!Sec->Pieces[I].Live) |
| 592 | continue; |
| 593 | uint32_t OutputOffset = Builder.add(Sec->getData(I)); |
| 594 | |
| 595 | // Save the offset in the generated string table. |
| 596 | if (!shouldTailMerge()) |
| 597 | Sec->Pieces[I].OutputOff = OutputOffset; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // Fix the string table content. After this, the contents |
| 602 | // will never change. |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 603 | if (shouldTailMerge()) |
| 604 | Builder.finalize(); |
Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 605 | else |
| 606 | Builder.finalizeInOrder(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 607 | this->Size = Builder.getSize(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 608 | |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 609 | // finalize() fixed tail-optimized strings, so we can now get |
| 610 | // offsets of strings. Get an offset for each string and save it |
| 611 | // to a corresponding StringPiece for easy access. |
Rui Ueyama | 5c851a5 | 2016-11-18 19:45:04 +0000 | [diff] [blame] | 612 | if (shouldTailMerge()) |
| 613 | for (MergeInputSection<ELFT> *Sec : Sections) |
| 614 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 615 | if (Sec->Pieces[I].Live) |
| 616 | Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 619 | template <class ELFT> |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 620 | VersionDefinitionSection<ELFT>::VersionDefinitionSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 621 | : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 622 | this->Addralign = sizeof(uint32_t); |
Rui Ueyama | f524464 | 2016-07-16 02:36:00 +0000 | [diff] [blame] | 623 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 624 | |
| 625 | static StringRef getFileDefName() { |
| 626 | if (!Config->SoName.empty()) |
| 627 | return Config->SoName; |
| 628 | return Config->OutputFile; |
| 629 | } |
| 630 | |
| 631 | template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 632 | FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName()); |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 633 | for (VersionDefinition &V : Config->VersionDefinitions) |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 634 | V.NameOff = In<ELFT>::DynStrTab->addString(V.Name); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 635 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 636 | this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 637 | this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 638 | |
| 639 | // sh_info should be set to the number of definitions. This fact is missed in |
| 640 | // documentation, but confirmed by binutils community: |
| 641 | // https://sourceware.org/ml/binutils/2014-11/msg00355.html |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 642 | this->Info = getVerDefNum(); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 645 | template <class ELFT> |
| 646 | void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, |
| 647 | StringRef Name, size_t NameOff) { |
| 648 | auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 649 | Verdef->vd_version = 1; |
George Rimar | 33b9de4 | 2016-07-01 11:45:10 +0000 | [diff] [blame] | 650 | Verdef->vd_cnt = 1; |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 651 | Verdef->vd_aux = sizeof(Elf_Verdef); |
| 652 | Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 653 | Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 654 | Verdef->vd_ndx = Index; |
Davide Italiano | e7fd0be | 2016-11-07 21:56:56 +0000 | [diff] [blame] | 655 | Verdef->vd_hash = hashSysV(Name); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 656 | |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 657 | auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); |
| 658 | Verdaux->vda_name = NameOff; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 659 | Verdaux->vda_next = 0; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | template <class ELFT> |
| 663 | void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 664 | writeOne(Buf, 1, getFileDefName(), FileDefNameOff); |
| 665 | |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 666 | for (VersionDefinition &V : Config->VersionDefinitions) { |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 667 | Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 668 | writeOne(Buf, V.Id, V.Name, V.NameOff); |
| 669 | } |
| 670 | |
| 671 | // Need to terminate the last version definition. |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 672 | Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 673 | Verdef->vd_next = 0; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | template <class ELFT> |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 677 | VersionTableSection<ELFT>::VersionTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 678 | : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 679 | this->Addralign = sizeof(uint16_t); |
Rafael Espindola | eaaec4a | 2016-04-29 17:19:45 +0000 | [diff] [blame] | 680 | } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 681 | |
| 682 | template <class ELFT> void VersionTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 683 | this->Size = |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 684 | sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 685 | this->Entsize = sizeof(Elf_Versym); |
George Rimar | 8b3c5f2 | 2016-06-06 08:04:53 +0000 | [diff] [blame] | 686 | // At the moment of june 2016 GNU docs does not mention that sh_link field |
| 687 | // should be set, but Sun docs do. Also readelf relies on this field. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 688 | this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 692 | auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 693 | for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 694 | OutVersym->vs_index = S.Symbol->symbol()->VersionId; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 695 | ++OutVersym; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | template <class ELFT> |
| 700 | VersionNeedSection<ELFT>::VersionNeedSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 701 | : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 702 | this->Addralign = sizeof(uint32_t); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 703 | |
| 704 | // Identifiers in verneed section start at 2 because 0 and 1 are reserved |
| 705 | // for VER_NDX_LOCAL and VER_NDX_GLOBAL. |
| 706 | // First identifiers are reserved by verdef section if it exist. |
| 707 | NextIndex = getVerDefNum() + 1; |
Rafael Espindola | eaaec4a | 2016-04-29 17:19:45 +0000 | [diff] [blame] | 708 | } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 709 | |
| 710 | template <class ELFT> |
| 711 | void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) { |
| 712 | if (!SS->Verdef) { |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 713 | SS->symbol()->VersionId = VER_NDX_GLOBAL; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 714 | return; |
| 715 | } |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 716 | SharedFile<ELFT> *F = SS->file(); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 717 | // If we don't already know that we need an Elf_Verneed for this DSO, prepare |
| 718 | // to create one by adding it to our needed list and creating a dynstr entry |
| 719 | // for the soname. |
| 720 | if (F->VerdefMap.empty()) |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 721 | Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())}); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 722 | typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef]; |
| 723 | // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, |
| 724 | // prepare to create one by allocating a version identifier and creating a |
| 725 | // dynstr entry for the version name. |
| 726 | if (NV.Index == 0) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 727 | NV.StrTab = In<ELFT>::DynStrTab->addString( |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 728 | SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 729 | NV.Index = NextIndex++; |
| 730 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 731 | SS->symbol()->VersionId = NV.Index; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { |
| 735 | // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. |
| 736 | auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); |
| 737 | auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); |
| 738 | |
| 739 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { |
| 740 | // Create an Elf_Verneed for this DSO. |
| 741 | Verneed->vn_version = 1; |
| 742 | Verneed->vn_cnt = P.first->VerdefMap.size(); |
| 743 | Verneed->vn_file = P.second; |
| 744 | Verneed->vn_aux = |
| 745 | reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); |
| 746 | Verneed->vn_next = sizeof(Elf_Verneed); |
| 747 | ++Verneed; |
| 748 | |
| 749 | // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over |
| 750 | // VerdefMap, which will only contain references to needed version |
| 751 | // definitions. Each Elf_Vernaux is based on the information contained in |
| 752 | // the Elf_Verdef in the source DSO. This loop iterates over a std::map of |
| 753 | // pointers, but is deterministic because the pointers refer to Elf_Verdef |
| 754 | // data structures within a single input file. |
| 755 | for (auto &NV : P.first->VerdefMap) { |
| 756 | Vernaux->vna_hash = NV.first->vd_hash; |
| 757 | Vernaux->vna_flags = 0; |
| 758 | Vernaux->vna_other = NV.second.Index; |
| 759 | Vernaux->vna_name = NV.second.StrTab; |
| 760 | Vernaux->vna_next = sizeof(Elf_Vernaux); |
| 761 | ++Vernaux; |
| 762 | } |
| 763 | |
| 764 | Vernaux[-1].vna_next = 0; |
| 765 | } |
| 766 | Verneed[-1].vn_next = 0; |
| 767 | } |
| 768 | |
| 769 | template <class ELFT> void VersionNeedSection<ELFT>::finalize() { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 770 | this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 771 | this->Info = Needed.size(); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 772 | unsigned Size = Needed.size() * sizeof(Elf_Verneed); |
| 773 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) |
| 774 | Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 775 | this->Size = Size; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | template <class ELFT> |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 779 | static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 780 | return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | template <class ELFT> |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 784 | static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, |
| 785 | StringRef OutsecName) { |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 786 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 787 | uintX_t Flags = getOutFlags(C); |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 788 | |
| 789 | // For SHF_MERGE we create different output sections for each alignment. |
| 790 | // This makes each output section simple and keeps a single level mapping from |
| 791 | // input to output. |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 792 | // In case of relocatable object generation we do not try to perform merging |
| 793 | // and treat SHF_MERGE sections as regular ones, but also create different |
| 794 | // output sections for them to allow merging at final linking stage. |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 795 | uintX_t Alignment = 0; |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 796 | if (isa<MergeInputSection<ELFT>>(C) || |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 797 | (Config->Relocatable && (C->Flags & SHF_MERGE))) |
| 798 | Alignment = std::max<uintX_t>(C->Alignment, C->Entsize); |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 799 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 800 | return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment}; |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 804 | std::pair<OutputSectionBase *, bool> |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 805 | OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C, |
| 806 | StringRef OutsecName) { |
| 807 | SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName); |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 808 | return create(Key, C); |
| 809 | } |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 810 | |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 811 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 812 | std::pair<OutputSectionBase *, bool> |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 813 | OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key, |
| 814 | InputSectionBase<ELFT> *C) { |
| 815 | uintX_t Flags = getOutFlags(C); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 816 | OutputSectionBase *&Sec = Map[Key]; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 817 | if (Sec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 818 | Sec->Flags |= Flags; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 819 | return {Sec, false}; |
| 820 | } |
| 821 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 822 | uint32_t Type = C->Type; |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 823 | switch (C->kind()) { |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 824 | case InputSectionBase<ELFT>::Regular: |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 825 | case InputSectionBase<ELFT>::Synthetic: |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 826 | Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 827 | break; |
| 828 | case InputSectionBase<ELFT>::EHFrame: |
| 829 | return {Out<ELFT>::EhFrame, false}; |
| 830 | case InputSectionBase<ELFT>::Merge: |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 831 | Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 832 | break; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 833 | } |
| 834 | return {Sec, true}; |
| 835 | } |
| 836 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 837 | template <bool Is64Bits> |
| 838 | typename lld::elf::SectionKey<Is64Bits> |
| 839 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() { |
| 840 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0}; |
| 841 | } |
| 842 | |
| 843 | template <bool Is64Bits> |
| 844 | typename lld::elf::SectionKey<Is64Bits> |
| 845 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() { |
| 846 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0, |
| 847 | 0}; |
| 848 | } |
| 849 | |
| 850 | template <bool Is64Bits> |
| 851 | unsigned |
| 852 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) { |
| 853 | return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment); |
| 854 | } |
| 855 | |
| 856 | template <bool Is64Bits> |
| 857 | bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS, |
| 858 | const Key &RHS) { |
| 859 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| 860 | LHS.Type == RHS.Type && LHS.Flags == RHS.Flags && |
| 861 | LHS.Alignment == RHS.Alignment; |
| 862 | } |
| 863 | |
| 864 | namespace llvm { |
| 865 | template struct DenseMapInfo<SectionKey<true>>; |
| 866 | template struct DenseMapInfo<SectionKey<false>>; |
| 867 | } |
| 868 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 869 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 870 | namespace elf { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 871 | |
| 872 | template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 873 | template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 874 | template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 875 | template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 876 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 877 | template class EhFrameHeader<ELF32LE>; |
| 878 | template class EhFrameHeader<ELF32BE>; |
| 879 | template class EhFrameHeader<ELF64LE>; |
| 880 | template class EhFrameHeader<ELF64BE>; |
| 881 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 882 | template class OutputSection<ELF32LE>; |
| 883 | template class OutputSection<ELF32BE>; |
| 884 | template class OutputSection<ELF64LE>; |
| 885 | template class OutputSection<ELF64BE>; |
| 886 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 887 | template class EhOutputSection<ELF32LE>; |
| 888 | template class EhOutputSection<ELF32BE>; |
| 889 | template class EhOutputSection<ELF64LE>; |
| 890 | template class EhOutputSection<ELF64BE>; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 891 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 892 | template class MergeOutputSection<ELF32LE>; |
| 893 | template class MergeOutputSection<ELF32BE>; |
| 894 | template class MergeOutputSection<ELF64LE>; |
| 895 | template class MergeOutputSection<ELF64BE>; |
| 896 | |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 897 | template class VersionTableSection<ELF32LE>; |
| 898 | template class VersionTableSection<ELF32BE>; |
| 899 | template class VersionTableSection<ELF64LE>; |
| 900 | template class VersionTableSection<ELF64BE>; |
| 901 | |
| 902 | template class VersionNeedSection<ELF32LE>; |
| 903 | template class VersionNeedSection<ELF32BE>; |
| 904 | template class VersionNeedSection<ELF64LE>; |
| 905 | template class VersionNeedSection<ELF64BE>; |
| 906 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 907 | template class VersionDefinitionSection<ELF32LE>; |
| 908 | template class VersionDefinitionSection<ELF32BE>; |
| 909 | template class VersionDefinitionSection<ELF64LE>; |
| 910 | template class VersionDefinitionSection<ELF64BE>; |
| 911 | |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 912 | template class GdbIndexSection<ELF32LE>; |
| 913 | template class GdbIndexSection<ELF32BE>; |
| 914 | template class GdbIndexSection<ELF64LE>; |
| 915 | template class GdbIndexSection<ELF64BE>; |
| 916 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 917 | template class OutputSectionFactory<ELF32LE>; |
| 918 | template class OutputSectionFactory<ELF32BE>; |
| 919 | template class OutputSectionFactory<ELF64LE>; |
| 920 | template class OutputSectionFactory<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 921 | } |
| 922 | } |