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