Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 1 | //===- SyntheticSections.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 | // This file contains linker-synthesized sections. Currently, |
| 11 | // synthetic sections are created either output sections or input sections, |
| 12 | // but we are rewriting code so that all synthetic sections are created as |
| 13 | // input sections. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "SyntheticSections.h" |
| 18 | #include "Config.h" |
| 19 | #include "Error.h" |
| 20 | #include "InputFiles.h" |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 21 | #include "LinkerScript.h" |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 22 | #include "Memory.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 23 | #include "OutputSections.h" |
| 24 | #include "Strings.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 25 | #include "SymbolTable.h" |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 26 | #include "Target.h" |
Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 27 | #include "Threads.h" |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 28 | #include "Writer.h" |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 29 | #include "lld/Config/Version.h" |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" |
| 31 | #include "llvm/Object/ELFObjectFile.h" |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Dwarf.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Endian.h" |
| 34 | #include "llvm/Support/MD5.h" |
| 35 | #include "llvm/Support/RandomNumberGenerator.h" |
| 36 | #include "llvm/Support/SHA1.h" |
| 37 | #include "llvm/Support/xxhash.h" |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 38 | #include <cstdlib> |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 41 | using namespace llvm::dwarf; |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 42 | using namespace llvm::ELF; |
| 43 | using namespace llvm::object; |
| 44 | using namespace llvm::support; |
| 45 | using namespace llvm::support::endian; |
| 46 | |
| 47 | using namespace lld; |
| 48 | using namespace lld::elf; |
| 49 | |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 50 | uint64_t SyntheticSection::getVA() const { |
| 51 | if (this->OutSec) |
| 52 | return this->OutSec->Addr + this->OutSecOff; |
| 53 | return 0; |
| 54 | } |
| 55 | |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 56 | template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() { |
| 57 | std::vector<DefinedCommon *> V; |
| 58 | for (Symbol *S : Symtab<ELFT>::X->getSymbols()) |
| 59 | if (auto *B = dyn_cast<DefinedCommon>(S->body())) |
| 60 | V.push_back(B); |
| 61 | return V; |
| 62 | } |
| 63 | |
| 64 | // Find all common symbols and allocate space for them. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 65 | template <class ELFT> InputSection *elf::createCommonSection() { |
| 66 | auto *Ret = make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1, |
| 67 | ArrayRef<uint8_t>(), "COMMON"); |
Rafael Espindola | 682a5bc | 2016-11-08 14:42:34 +0000 | [diff] [blame] | 68 | Ret->Live = true; |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 69 | |
Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 70 | if (!Config->DefineCommon) |
| 71 | return Ret; |
| 72 | |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 73 | // Sort the common symbols by alignment as an heuristic to pack them better. |
| 74 | std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>(); |
| 75 | std::stable_sort(Syms.begin(), Syms.end(), |
| 76 | [](const DefinedCommon *A, const DefinedCommon *B) { |
| 77 | return A->Alignment > B->Alignment; |
| 78 | }); |
| 79 | |
| 80 | // Assign offsets to symbols. |
| 81 | size_t Size = 0; |
| 82 | size_t Alignment = 1; |
| 83 | for (DefinedCommon *Sym : Syms) { |
Rui Ueyama | 1c78682 | 2016-11-05 23:14:54 +0000 | [diff] [blame] | 84 | Alignment = std::max<size_t>(Alignment, Sym->Alignment); |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 85 | Size = alignTo(Size, Sym->Alignment); |
| 86 | |
| 87 | // Compute symbol offset relative to beginning of input section. |
| 88 | Sym->Offset = Size; |
| 89 | Size += Sym->Size; |
| 90 | } |
Rafael Espindola | 682a5bc | 2016-11-08 14:42:34 +0000 | [diff] [blame] | 91 | Ret->Alignment = Alignment; |
| 92 | Ret->Data = makeArrayRef<uint8_t>(nullptr, Size); |
| 93 | return Ret; |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 96 | // Returns an LLD version string. |
| 97 | static ArrayRef<uint8_t> getVersion() { |
| 98 | // Check LLD_VERSION first for ease of testing. |
| 99 | // You can get consitent output by using the environment variable. |
| 100 | // This is only for testing. |
| 101 | StringRef S = getenv("LLD_VERSION"); |
| 102 | if (S.empty()) |
| 103 | S = Saver.save(Twine("Linker: ") + getLLDVersion()); |
| 104 | |
| 105 | // +1 to include the terminating '\0'. |
| 106 | return {(const uint8_t *)S.data(), S.size() + 1}; |
Davide Italiano | b69f38f | 2016-11-11 00:05:41 +0000 | [diff] [blame] | 107 | } |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 108 | |
| 109 | // Creates a .comment section containing LLD version info. |
| 110 | // With this feature, you can identify LLD-generated binaries easily |
| 111 | // by "objdump -s -j .comment <file>". |
| 112 | // The returned object is a mergeable string section. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 113 | template <class ELFT> MergeInputSection *elf::createCommentSection() { |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 114 | typename ELFT::Shdr Hdr = {}; |
| 115 | Hdr.sh_flags = SHF_MERGE | SHF_STRINGS; |
| 116 | Hdr.sh_type = SHT_PROGBITS; |
| 117 | Hdr.sh_entsize = 1; |
| 118 | Hdr.sh_addralign = 1; |
| 119 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 120 | auto *Ret = |
| 121 | make<MergeInputSection>((ObjectFile<ELFT> *)nullptr, &Hdr, ".comment"); |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 122 | Ret->Data = getVersion(); |
| 123 | Ret->splitIntoPieces(); |
| 124 | return Ret; |
| 125 | } |
| 126 | |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 127 | // .MIPS.abiflags section. |
| 128 | template <class ELFT> |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 129 | MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 130 | : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 131 | Flags(Flags) { |
| 132 | this->Entsize = sizeof(Elf_Mips_ABIFlags); |
| 133 | } |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 134 | |
| 135 | template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) { |
| 136 | memcpy(Buf, &Flags, sizeof(Flags)); |
| 137 | } |
| 138 | |
| 139 | template <class ELFT> |
| 140 | MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() { |
| 141 | Elf_Mips_ABIFlags Flags = {}; |
| 142 | bool Create = false; |
| 143 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 +0000 | [diff] [blame] | 144 | for (InputSectionBase *Sec : InputSections) { |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 145 | if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS) |
| 146 | continue; |
| 147 | Sec->Live = false; |
| 148 | Create = true; |
| 149 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 150 | std::string Filename = toString(Sec->getFile<ELFT>()); |
Simon Atanasyan | 86dc60d | 2016-12-21 05:31:57 +0000 | [diff] [blame] | 151 | const size_t Size = Sec->Data.size(); |
| 152 | // Older version of BFD (such as the default FreeBSD linker) concatenate |
| 153 | // .MIPS.abiflags instead of merging. To allow for this case (or potential |
| 154 | // zero padding) we ignore everything after the first Elf_Mips_ABIFlags |
| 155 | if (Size < sizeof(Elf_Mips_ABIFlags)) { |
| 156 | error(Filename + ": invalid size of .MIPS.abiflags section: got " + |
| 157 | Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags))); |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 158 | return nullptr; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 159 | } |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 160 | auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data()); |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 161 | if (S->version != 0) { |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 162 | error(Filename + ": unexpected .MIPS.abiflags version " + |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 163 | Twine(S->version)); |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 164 | return nullptr; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 165 | } |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 166 | |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 167 | // LLD checks ISA compatibility in getMipsEFlags(). Here we just |
| 168 | // select the highest number of ISA/Rev/Ext. |
| 169 | Flags.isa_level = std::max(Flags.isa_level, S->isa_level); |
| 170 | Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev); |
| 171 | Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext); |
| 172 | Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size); |
| 173 | Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size); |
| 174 | Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size); |
| 175 | Flags.ases |= S->ases; |
| 176 | Flags.flags1 |= S->flags1; |
| 177 | Flags.flags2 |= S->flags2; |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 178 | Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename); |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 179 | }; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 180 | |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 181 | if (Create) |
| 182 | return make<MipsAbiFlagsSection<ELFT>>(Flags); |
| 183 | return nullptr; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 186 | // .MIPS.options section. |
| 187 | template <class ELFT> |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 188 | MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 189 | : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 190 | Reginfo(Reginfo) { |
| 191 | this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); |
| 192 | } |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 193 | |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 194 | template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) { |
| 195 | auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf); |
| 196 | Options->kind = ODK_REGINFO; |
| 197 | Options->size = getSize(); |
| 198 | |
| 199 | if (!Config->Relocatable) |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 200 | Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp(); |
Rafael Espindola | 4862ae8 | 2016-11-24 16:38:35 +0000 | [diff] [blame] | 201 | memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo)); |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 204 | template <class ELFT> |
| 205 | MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() { |
| 206 | // N64 ABI only. |
| 207 | if (!ELFT::Is64Bits) |
| 208 | return nullptr; |
| 209 | |
| 210 | Elf_Mips_RegInfo Reginfo = {}; |
| 211 | bool Create = false; |
| 212 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 +0000 | [diff] [blame] | 213 | for (InputSectionBase *Sec : InputSections) { |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 214 | if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS) |
| 215 | continue; |
| 216 | Sec->Live = false; |
| 217 | Create = true; |
| 218 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 219 | std::string Filename = toString(Sec->getFile<ELFT>()); |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 220 | ArrayRef<uint8_t> D = Sec->Data; |
| 221 | |
| 222 | while (!D.empty()) { |
| 223 | if (D.size() < sizeof(Elf_Mips_Options)) { |
| 224 | error(Filename + ": invalid size of .MIPS.options section"); |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data()); |
| 229 | if (Opt->kind == ODK_REGINFO) { |
| 230 | if (Config->Relocatable && Opt->getRegInfo().ri_gp_value) |
| 231 | error(Filename + ": unsupported non-zero ri_gp_value"); |
| 232 | Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 233 | Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value; |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | |
| 237 | if (!Opt->size) |
| 238 | fatal(Filename + ": zero option descriptor size"); |
| 239 | D = D.slice(Opt->size); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | if (Create) |
Rui Ueyama | 3cc93d7 | 2016-11-22 23:13:08 +0000 | [diff] [blame] | 244 | return make<MipsOptionsSection<ELFT>>(Reginfo); |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 245 | return nullptr; |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // MIPS .reginfo section. |
| 249 | template <class ELFT> |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 250 | MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 251 | : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 252 | Reginfo(Reginfo) { |
| 253 | this->Entsize = sizeof(Elf_Mips_RegInfo); |
| 254 | } |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 255 | |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 256 | template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) { |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 257 | if (!Config->Relocatable) |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 258 | Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp(); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 259 | memcpy(Buf, &Reginfo, sizeof(Reginfo)); |
| 260 | } |
| 261 | |
| 262 | template <class ELFT> |
| 263 | MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() { |
| 264 | // Section should be alive for O32 and N32 ABIs only. |
| 265 | if (ELFT::Is64Bits) |
| 266 | return nullptr; |
| 267 | |
| 268 | Elf_Mips_RegInfo Reginfo = {}; |
| 269 | bool Create = false; |
| 270 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 +0000 | [diff] [blame] | 271 | for (InputSectionBase *Sec : InputSections) { |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 272 | if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO) |
| 273 | continue; |
| 274 | Sec->Live = false; |
| 275 | Create = true; |
| 276 | |
| 277 | if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 278 | error(toString(Sec->getFile<ELFT>()) + |
| 279 | ": invalid size of .reginfo section"); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 280 | return nullptr; |
| 281 | } |
| 282 | auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data()); |
| 283 | if (Config->Relocatable && R->ri_gp_value) |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 284 | error(toString(Sec->getFile<ELFT>()) + |
| 285 | ": unsupported non-zero ri_gp_value"); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 286 | |
| 287 | Reginfo.ri_gprmask |= R->ri_gprmask; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 288 | Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value; |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | if (Create) |
| 292 | return make<MipsReginfoSection<ELFT>>(Reginfo); |
| 293 | return nullptr; |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Rui Ueyama | 3255a52 | 2017-02-27 02:32:49 +0000 | [diff] [blame] | 296 | InputSection *elf::createInterpSection() { |
Rui Ueyama | 81a4b26 | 2016-11-22 04:33:01 +0000 | [diff] [blame] | 297 | // StringSaver guarantees that the returned string ends with '\0'. |
| 298 | StringRef S = Saver.save(Config->DynamicLinker); |
Rui Ueyama | 6e50fd5 | 2017-03-01 07:39:06 +0000 | [diff] [blame] | 299 | ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1}; |
| 300 | |
| 301 | auto *Sec = |
| 302 | make<InputSection>(SHF_ALLOC, SHT_PROGBITS, 1, Contents, ".interp"); |
| 303 | Sec->Live = true; |
| 304 | return Sec; |
Rui Ueyama | a9ee8d6 | 2016-11-04 22:25:39 +0000 | [diff] [blame] | 305 | } |
Rui Ueyama | e288eef | 2016-11-02 18:58:44 +0000 | [diff] [blame] | 306 | |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 307 | template <class ELFT> |
Rui Ueyama | 65316d7 | 2017-02-23 03:15:57 +0000 | [diff] [blame] | 308 | SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value, |
| 309 | uint64_t Size, InputSectionBase *Section) { |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 310 | auto *S = make<DefinedRegular>(Name, /*IsLocal*/ true, STV_DEFAULT, Type, |
| 311 | Value, Size, Section, nullptr); |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 312 | if (In<ELFT>::SymTab) |
Rui Ueyama | b8dcdb5 | 2017-02-28 04:20:16 +0000 | [diff] [blame] | 313 | In<ELFT>::SymTab->addSymbol(S); |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 314 | return S; |
| 315 | } |
| 316 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 317 | static size_t getHashSize() { |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 318 | switch (Config->BuildId) { |
| 319 | case BuildIdKind::Fast: |
| 320 | return 8; |
| 321 | case BuildIdKind::Md5: |
| 322 | case BuildIdKind::Uuid: |
| 323 | return 16; |
| 324 | case BuildIdKind::Sha1: |
| 325 | return 20; |
| 326 | case BuildIdKind::Hexstring: |
| 327 | return Config->BuildIdVector.size(); |
| 328 | default: |
| 329 | llvm_unreachable("unknown BuildIdKind"); |
| 330 | } |
| 331 | } |
| 332 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 333 | template <class ELFT> |
| 334 | BuildIdSection<ELFT>::BuildIdSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 335 | : SyntheticSection(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"), |
Rui Ueyama | bb536fe | 2016-11-22 01:36:19 +0000 | [diff] [blame] | 336 | HashSize(getHashSize()) {} |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 337 | |
| 338 | template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) { |
| 339 | const endianness E = ELFT::TargetEndianness; |
| 340 | write32<E>(Buf, 4); // Name size |
| 341 | write32<E>(Buf + 4, HashSize); // Content size |
| 342 | write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type |
| 343 | memcpy(Buf + 12, "GNU", 4); // Name string |
| 344 | HashBuf = Buf + 16; |
| 345 | } |
| 346 | |
Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 347 | // Split one uint8 array into small pieces of uint8 arrays. |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 348 | static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr, |
| 349 | size_t ChunkSize) { |
| 350 | std::vector<ArrayRef<uint8_t>> Ret; |
| 351 | while (Arr.size() > ChunkSize) { |
| 352 | Ret.push_back(Arr.take_front(ChunkSize)); |
| 353 | Arr = Arr.drop_front(ChunkSize); |
| 354 | } |
| 355 | if (!Arr.empty()) |
| 356 | Ret.push_back(Arr); |
| 357 | return Ret; |
| 358 | } |
| 359 | |
Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 360 | // Computes a hash value of Data using a given hash function. |
| 361 | // In order to utilize multiple cores, we first split data into 1MB |
| 362 | // chunks, compute a hash for each chunk, and then compute a hash value |
| 363 | // of the hash values. |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 364 | template <class ELFT> |
| 365 | void BuildIdSection<ELFT>::computeHash( |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 366 | llvm::ArrayRef<uint8_t> Data, |
| 367 | std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) { |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 368 | std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024); |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 369 | std::vector<uint8_t> Hashes(Chunks.size() * HashSize); |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 370 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 371 | // Compute hash values. |
Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 372 | forLoop(0, Chunks.size(), |
| 373 | [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); }); |
Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 374 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 375 | // Write to the final output buffer. |
| 376 | HashFn(HashBuf, Hashes); |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 379 | template <class ELFT> |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 380 | CopyRelSection<ELFT>::CopyRelSection(bool ReadOnly, uintX_t AddrAlign, size_t S) |
| 381 | : SyntheticSection(SHF_ALLOC, SHT_NOBITS, AddrAlign, |
| 382 | ReadOnly ? ".bss.rel.ro" : ".bss"), |
| 383 | Size(S) {} |
Peter Smith | ebfe994 | 2017-02-09 10:27:57 +0000 | [diff] [blame] | 384 | |
| 385 | template <class ELFT> |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 386 | void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) { |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 387 | switch (Config->BuildId) { |
| 388 | case BuildIdKind::Fast: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 389 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 390 | write64le(Dest, xxHash64(toStringRef(Arr))); |
| 391 | }); |
| 392 | break; |
| 393 | case BuildIdKind::Md5: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 394 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
Rui Ueyama | 28590b6 | 2016-11-23 18:11:38 +0000 | [diff] [blame] | 395 | memcpy(Dest, MD5::hash(Arr).data(), 16); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 396 | }); |
| 397 | break; |
| 398 | case BuildIdKind::Sha1: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 399 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
Rui Ueyama | 28590b6 | 2016-11-23 18:11:38 +0000 | [diff] [blame] | 400 | memcpy(Dest, SHA1::hash(Arr).data(), 20); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 401 | }); |
| 402 | break; |
| 403 | case BuildIdKind::Uuid: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 404 | if (getRandomBytes(HashBuf, HashSize)) |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 405 | error("entropy source failure"); |
| 406 | break; |
| 407 | case BuildIdKind::Hexstring: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 408 | memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size()); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 409 | break; |
| 410 | default: |
| 411 | llvm_unreachable("unknown BuildIdKind"); |
| 412 | } |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 415 | template <class ELFT> |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 416 | EhFrameSection<ELFT>::EhFrameSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 417 | : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {} |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 418 | |
| 419 | // Search for an existing CIE record or create a new one. |
| 420 | // CIE records from input object files are uniquified by their contents |
| 421 | // and where their relocations point to. |
| 422 | template <class ELFT> |
| 423 | template <class RelTy> |
| 424 | CieRecord *EhFrameSection<ELFT>::addCie(EhSectionPiece &Piece, |
| 425 | ArrayRef<RelTy> Rels) { |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 426 | auto *Sec = cast<EhInputSection>(Piece.ID); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 427 | const endianness E = ELFT::TargetEndianness; |
| 428 | if (read32<E>(Piece.data().data() + 4) != 0) |
| 429 | fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame"); |
| 430 | |
| 431 | SymbolBody *Personality = nullptr; |
| 432 | unsigned FirstRelI = Piece.FirstRelocation; |
| 433 | if (FirstRelI != (unsigned)-1) |
| 434 | Personality = |
| 435 | &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]); |
| 436 | |
| 437 | // Search for an existing CIE by CIE contents/relocation target pair. |
| 438 | CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; |
| 439 | |
| 440 | // If not found, create a new one. |
| 441 | if (Cie->Piece == nullptr) { |
| 442 | Cie->Piece = &Piece; |
| 443 | Cies.push_back(Cie); |
| 444 | } |
| 445 | return Cie; |
| 446 | } |
| 447 | |
| 448 | // There is one FDE per function. Returns true if a given FDE |
| 449 | // points to a live function. |
| 450 | template <class ELFT> |
| 451 | template <class RelTy> |
| 452 | bool EhFrameSection<ELFT>::isFdeLive(EhSectionPiece &Piece, |
| 453 | ArrayRef<RelTy> Rels) { |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 454 | auto *Sec = cast<EhInputSection>(Piece.ID); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 455 | unsigned FirstRelI = Piece.FirstRelocation; |
| 456 | if (FirstRelI == (unsigned)-1) |
| 457 | return false; |
| 458 | const RelTy &Rel = Rels[FirstRelI]; |
| 459 | SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel); |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 460 | auto *D = dyn_cast<DefinedRegular>(&B); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 461 | if (!D || !D->Section) |
| 462 | return false; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 463 | auto *Target = |
| 464 | cast<InputSectionBase>(cast<InputSectionBase>(D->Section)->Repl); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 465 | return Target && Target->Live; |
| 466 | } |
| 467 | |
| 468 | // .eh_frame is a sequence of CIE or FDE records. In general, there |
| 469 | // is one CIE record per input object file which is followed by |
| 470 | // a list of FDEs. This function searches an existing CIE or create a new |
| 471 | // one and associates FDEs to the CIE. |
| 472 | template <class ELFT> |
| 473 | template <class RelTy> |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 474 | void EhFrameSection<ELFT>::addSectionAux(EhInputSection *Sec, |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 475 | ArrayRef<RelTy> Rels) { |
| 476 | const endianness E = ELFT::TargetEndianness; |
| 477 | |
| 478 | DenseMap<size_t, CieRecord *> OffsetToCie; |
| 479 | for (EhSectionPiece &Piece : Sec->Pieces) { |
| 480 | // The empty record is the end marker. |
| 481 | if (Piece.size() == 4) |
| 482 | return; |
| 483 | |
| 484 | size_t Offset = Piece.InputOff; |
| 485 | uint32_t ID = read32<E>(Piece.data().data() + 4); |
| 486 | if (ID == 0) { |
| 487 | OffsetToCie[Offset] = addCie(Piece, Rels); |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | uint32_t CieOffset = Offset + 4 - ID; |
| 492 | CieRecord *Cie = OffsetToCie[CieOffset]; |
| 493 | if (!Cie) |
| 494 | fatal(toString(Sec) + ": invalid CIE reference"); |
| 495 | |
| 496 | if (!isFdeLive(Piece, Rels)) |
| 497 | continue; |
| 498 | Cie->FdePieces.push_back(&Piece); |
| 499 | NumFdes++; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | template <class ELFT> |
| 504 | void EhFrameSection<ELFT>::addSection(InputSectionBase *C) { |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 505 | auto *Sec = cast<EhInputSection>(C); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 506 | Sec->EHSec = this; |
| 507 | updateAlignment(Sec->Alignment); |
| 508 | Sections.push_back(Sec); |
| 509 | |
| 510 | // .eh_frame is a sequence of CIE or FDE records. This function |
| 511 | // splits it into pieces so that we can call |
| 512 | // SplitInputSection::getSectionPiece on the section. |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 513 | Sec->split<ELFT>(); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 514 | if (Sec->Pieces.empty()) |
| 515 | return; |
| 516 | |
| 517 | if (Sec->NumRelocations) { |
| 518 | if (Sec->AreRelocsRela) |
| 519 | addSectionAux(Sec, Sec->template relas<ELFT>()); |
| 520 | else |
| 521 | addSectionAux(Sec, Sec->template rels<ELFT>()); |
| 522 | return; |
| 523 | } |
| 524 | addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); |
| 525 | } |
| 526 | |
| 527 | template <class ELFT> |
| 528 | static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { |
| 529 | memcpy(Buf, D.data(), D.size()); |
| 530 | |
| 531 | // Fix the size field. -4 since size does not include the size field itself. |
| 532 | const endianness E = ELFT::TargetEndianness; |
| 533 | write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); |
| 534 | } |
| 535 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 536 | template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 537 | if (this->Size) |
| 538 | return; // Already finalized. |
| 539 | |
| 540 | size_t Off = 0; |
| 541 | for (CieRecord *Cie : Cies) { |
| 542 | Cie->Piece->OutputOff = Off; |
| 543 | Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); |
| 544 | |
| 545 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
| 546 | Fde->OutputOff = Off; |
| 547 | Off += alignTo(Fde->size(), sizeof(uintX_t)); |
| 548 | } |
| 549 | } |
Rafael Espindola | b691ccf | 2017-02-28 18:55:08 +0000 | [diff] [blame] | 550 | this->Size = Off; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { |
| 554 | const endianness E = ELFT::TargetEndianness; |
| 555 | switch (Size) { |
| 556 | case DW_EH_PE_udata2: |
| 557 | return read16<E>(Buf); |
| 558 | case DW_EH_PE_udata4: |
| 559 | return read32<E>(Buf); |
| 560 | case DW_EH_PE_udata8: |
| 561 | return read64<E>(Buf); |
| 562 | case DW_EH_PE_absptr: |
| 563 | if (ELFT::Is64Bits) |
| 564 | return read64<E>(Buf); |
| 565 | return read32<E>(Buf); |
| 566 | } |
| 567 | fatal("unknown FDE size encoding"); |
| 568 | } |
| 569 | |
| 570 | // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. |
| 571 | // We need it to create .eh_frame_hdr section. |
| 572 | template <class ELFT> |
| 573 | typename ELFT::uint EhFrameSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, |
| 574 | uint8_t Enc) { |
| 575 | // The starting address to which this FDE applies is |
| 576 | // stored at FDE + 8 byte. |
| 577 | size_t Off = FdeOff + 8; |
| 578 | uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); |
| 579 | if ((Enc & 0x70) == DW_EH_PE_absptr) |
| 580 | return Addr; |
| 581 | if ((Enc & 0x70) == DW_EH_PE_pcrel) |
| 582 | return Addr + this->OutSec->Addr + Off; |
| 583 | fatal("unknown FDE size relative encoding"); |
| 584 | } |
| 585 | |
| 586 | template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) { |
| 587 | const endianness E = ELFT::TargetEndianness; |
| 588 | for (CieRecord *Cie : Cies) { |
| 589 | size_t CieOffset = Cie->Piece->OutputOff; |
| 590 | writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); |
| 591 | |
| 592 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
| 593 | size_t Off = Fde->OutputOff; |
| 594 | writeCieFde<ELFT>(Buf + Off, Fde->data()); |
| 595 | |
| 596 | // FDE's second word should have the offset to an associated CIE. |
| 597 | // Write it. |
| 598 | write32<E>(Buf + Off + 4, Off + 4 - CieOffset); |
| 599 | } |
| 600 | } |
| 601 | |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 602 | for (EhInputSection *S : Sections) |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 603 | S->template relocate<ELFT>(Buf, nullptr); |
| 604 | |
| 605 | // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table |
| 606 | // to get a FDE from an address to which FDE is applied. So here |
| 607 | // we obtain two addresses and pass them to EhFrameHdr object. |
| 608 | if (In<ELFT>::EhFrameHdr) { |
| 609 | for (CieRecord *Cie : Cies) { |
| 610 | uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece); |
| 611 | for (SectionPiece *Fde : Cie->FdePieces) { |
| 612 | uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); |
| 613 | uintX_t FdeVA = this->OutSec->Addr + Fde->OutputOff; |
| 614 | In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | template <class ELFT> |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 621 | GotSection<ELFT>::GotSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 622 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 623 | Target->GotEntrySize, ".got") {} |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 624 | |
| 625 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) { |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 626 | Sym.GotIndex = NumEntries; |
| 627 | ++NumEntries; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 630 | template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { |
| 631 | if (Sym.GlobalDynIndex != -1U) |
| 632 | return false; |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 633 | Sym.GlobalDynIndex = NumEntries; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 634 | // Global Dynamic TLS entries take two GOT slots. |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 635 | NumEntries += 2; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 636 | return true; |
| 637 | } |
| 638 | |
| 639 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 640 | // In total it takes two GOT slots. |
| 641 | template <class ELFT> bool GotSection<ELFT>::addTlsIndex() { |
| 642 | if (TlsIndexOff != uint32_t(-1)) |
| 643 | return false; |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 644 | TlsIndexOff = NumEntries * sizeof(uintX_t); |
| 645 | NumEntries += 2; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 646 | return true; |
| 647 | } |
| 648 | |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 649 | template <class ELFT> |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 650 | typename GotSection<ELFT>::uintX_t |
| 651 | GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const { |
| 652 | return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t); |
| 653 | } |
| 654 | |
| 655 | template <class ELFT> |
| 656 | typename GotSection<ELFT>::uintX_t |
| 657 | GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { |
| 658 | return B.GlobalDynIndex * sizeof(uintX_t); |
| 659 | } |
| 660 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 661 | template <class ELFT> void GotSection<ELFT>::finalizeContents() { |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 662 | Size = NumEntries * sizeof(uintX_t); |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 663 | } |
| 664 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 665 | template <class ELFT> bool GotSection<ELFT>::empty() const { |
| 666 | // If we have a relocation that is relative to GOT (such as GOTOFFREL), |
| 667 | // we need to emit a GOT even if it's empty. |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 668 | return NumEntries == 0 && !HasGotOffRel; |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 671 | template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 672 | this->template relocate<ELFT>(Buf, Buf + Size); |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | template <class ELFT> |
| 676 | MipsGotSection<ELFT>::MipsGotSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 677 | : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16, |
| 678 | ".got") {} |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 679 | |
| 680 | template <class ELFT> |
Rafael Espindola | 7386cea | 2017-02-16 00:12:34 +0000 | [diff] [blame] | 681 | void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, int64_t Addend, |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 682 | RelExpr Expr) { |
| 683 | // For "true" local symbols which can be referenced from the same module |
| 684 | // only compiler creates two instructions for address loading: |
| 685 | // |
| 686 | // lw $8, 0($gp) # R_MIPS_GOT16 |
| 687 | // addi $8, $8, 0 # R_MIPS_LO16 |
| 688 | // |
| 689 | // The first instruction loads high 16 bits of the symbol address while |
| 690 | // the second adds an offset. That allows to reduce number of required |
| 691 | // GOT entries because only one global offset table entry is necessary |
| 692 | // for every 64 KBytes of local data. So for local symbols we need to |
| 693 | // allocate number of GOT entries to hold all required "page" addresses. |
| 694 | // |
| 695 | // All global symbols (hidden and regular) considered by compiler uniformly. |
| 696 | // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation |
| 697 | // to load address of the symbol. So for each such symbol we need to |
| 698 | // allocate dedicated GOT entry to store its address. |
| 699 | // |
| 700 | // If a symbol is preemptible we need help of dynamic linker to get its |
| 701 | // final address. The corresponding GOT entries are allocated in the |
| 702 | // "global" part of GOT. Entries for non preemptible global symbol allocated |
| 703 | // in the "local" part of GOT. |
| 704 | // |
| 705 | // See "Global Offset Table" in Chapter 5: |
| 706 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 707 | if (Expr == R_MIPS_GOT_LOCAL_PAGE) { |
| 708 | // At this point we do not know final symbol value so to reduce number |
| 709 | // of allocated GOT entries do the following trick. Save all output |
| 710 | // sections referenced by GOT relocations. Then later in the `finalize` |
| 711 | // method calculate number of "pages" required to cover all saved output |
| 712 | // section and allocate appropriate number of GOT entries. |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 713 | auto *DefSym = cast<DefinedRegular>(&Sym); |
Rafael Espindola | 5e434b3 | 2017-03-08 16:08:36 +0000 | [diff] [blame] | 714 | PageIndexMap.insert({DefSym->Section->getOutputSection(), 0}); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | if (Sym.isTls()) { |
| 718 | // GOT entries created for MIPS TLS relocations behave like |
| 719 | // almost GOT entries from other ABIs. They go to the end |
| 720 | // of the global offset table. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 721 | Sym.GotIndex = TlsEntries.size(); |
| 722 | TlsEntries.push_back(&Sym); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 725 | auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 726 | if (S.isInGot() && !A) |
| 727 | return; |
| 728 | size_t NewIndex = Items.size(); |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 729 | if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second) |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 730 | return; |
| 731 | Items.emplace_back(&S, A); |
| 732 | if (!A) |
| 733 | S.GotIndex = NewIndex; |
| 734 | }; |
| 735 | if (Sym.isPreemptible()) { |
| 736 | // Ignore addends for preemptible symbols. They got single GOT entry anyway. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 737 | AddEntry(Sym, 0, GlobalEntries); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 738 | Sym.IsInGlobalMipsGot = true; |
| 739 | } else if (Expr == R_MIPS_GOT_OFF32) { |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 740 | AddEntry(Sym, Addend, LocalEntries32); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 741 | Sym.Is32BitMipsGot = true; |
| 742 | } else { |
| 743 | // Hold local GOT entries accessed via a 16-bit index separately. |
| 744 | // That allows to write them in the beginning of the GOT and keep |
| 745 | // their indexes as less as possible to escape relocation's overflow. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 746 | AddEntry(Sym, Addend, LocalEntries); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
George Rimar | 879a657 | 2016-12-15 15:38:58 +0000 | [diff] [blame] | 750 | template <class ELFT> |
| 751 | bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 752 | if (Sym.GlobalDynIndex != -1U) |
| 753 | return false; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 754 | Sym.GlobalDynIndex = TlsEntries.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 755 | // Global Dynamic TLS entries take two GOT slots. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 756 | TlsEntries.push_back(nullptr); |
| 757 | TlsEntries.push_back(&Sym); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | |
| 761 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 762 | // In total it takes two GOT slots. |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 763 | template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 764 | if (TlsIndexOff != uint32_t(-1)) |
| 765 | return false; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 766 | TlsIndexOff = TlsEntries.size() * sizeof(uintX_t); |
| 767 | TlsEntries.push_back(nullptr); |
| 768 | TlsEntries.push_back(nullptr); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 769 | return true; |
| 770 | } |
| 771 | |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 772 | static uint64_t getMipsPageAddr(uint64_t Addr) { |
| 773 | return (Addr + 0x8000) & ~0xffff; |
| 774 | } |
| 775 | |
| 776 | static uint64_t getMipsPageCount(uint64_t Size) { |
| 777 | return (Size + 0xfffe) / 0xffff + 1; |
| 778 | } |
| 779 | |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 780 | template <class ELFT> |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 781 | typename MipsGotSection<ELFT>::uintX_t |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 782 | MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B, |
Rafael Espindola | 7386cea | 2017-02-16 00:12:34 +0000 | [diff] [blame] | 783 | int64_t Addend) const { |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 784 | const OutputSection *OutSec = |
Rafael Espindola | 5e434b3 | 2017-03-08 16:08:36 +0000 | [diff] [blame] | 785 | cast<DefinedRegular>(&B)->Section->getOutputSection(); |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 786 | uintX_t SecAddr = getMipsPageAddr(OutSec->Addr); |
| 787 | uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend)); |
| 788 | uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff; |
| 789 | assert(Index < PageEntriesNum); |
| 790 | return (HeaderEntriesNum + Index) * sizeof(uintX_t); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | template <class ELFT> |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 794 | typename MipsGotSection<ELFT>::uintX_t |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 795 | MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B, |
Rafael Espindola | 7386cea | 2017-02-16 00:12:34 +0000 | [diff] [blame] | 796 | int64_t Addend) const { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 797 | // Calculate offset of the GOT entries block: TLS, global, local. |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 798 | uintX_t Index = HeaderEntriesNum + PageEntriesNum; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 799 | if (B.isTls()) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 800 | Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 801 | else if (B.IsInGlobalMipsGot) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 802 | Index += LocalEntries.size() + LocalEntries32.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 803 | else if (B.Is32BitMipsGot) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 804 | Index += LocalEntries.size(); |
| 805 | // Calculate offset of the GOT entry in the block. |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 806 | if (B.isInGot()) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 807 | Index += B.GotIndex; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 808 | else { |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 809 | auto It = EntryIndexMap.find({&B, Addend}); |
| 810 | assert(It != EntryIndexMap.end()); |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 811 | Index += It->second; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 812 | } |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 813 | return Index * sizeof(uintX_t); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | template <class ELFT> |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 817 | typename MipsGotSection<ELFT>::uintX_t |
| 818 | MipsGotSection<ELFT>::getTlsOffset() const { |
| 819 | return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | template <class ELFT> |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 823 | typename MipsGotSection<ELFT>::uintX_t |
| 824 | MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 825 | return B.GlobalDynIndex * sizeof(uintX_t); |
| 826 | } |
| 827 | |
| 828 | template <class ELFT> |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 829 | const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const { |
| 830 | return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | template <class ELFT> |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 834 | unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const { |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 835 | return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() + |
| 836 | LocalEntries32.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 839 | template <class ELFT> void MipsGotSection<ELFT>::finalizeContents() { |
Peter Smith | 1ec42d9 | 2017-03-08 14:06:24 +0000 | [diff] [blame] | 840 | updateAllocSize(); |
| 841 | } |
| 842 | |
| 843 | template <class ELFT> void MipsGotSection<ELFT>::updateAllocSize() { |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 844 | PageEntriesNum = 0; |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 845 | for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) { |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 846 | // For each output section referenced by GOT page relocations calculate |
| 847 | // and save into PageIndexMap an upper bound of MIPS GOT entries required |
| 848 | // to store page addresses of local symbols. We assume the worst case - |
| 849 | // each 64kb page of the output section has at least one GOT relocation |
| 850 | // against it. And take in account the case when the section intersects |
| 851 | // page boundaries. |
| 852 | P.second = PageEntriesNum; |
| 853 | PageEntriesNum += getMipsPageCount(P.first->Size); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 854 | } |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 855 | Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) * |
| 856 | sizeof(uintX_t); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 857 | } |
| 858 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 859 | template <class ELFT> bool MipsGotSection<ELFT>::empty() const { |
| 860 | // We add the .got section to the result for dynamic MIPS target because |
| 861 | // its address and properties are mentioned in the .dynamic section. |
| 862 | return Config->Relocatable; |
| 863 | } |
| 864 | |
Simon Atanasyan | b966665 | 2016-12-12 14:30:18 +0000 | [diff] [blame] | 865 | template <class ELFT> |
| 866 | typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const { |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 867 | return ElfSym::MipsGp->template getVA<ELFT>(0); |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 870 | template <class ELFT> |
| 871 | static void writeUint(uint8_t *Buf, typename ELFT::uint Val) { |
| 872 | typedef typename ELFT::uint uintX_t; |
| 873 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val); |
| 874 | } |
| 875 | |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 876 | template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 877 | // Set the MSB of the second GOT slot. This is not required by any |
| 878 | // MIPS ABI documentation, though. |
| 879 | // |
| 880 | // There is a comment in glibc saying that "The MSB of got[1] of a |
| 881 | // gnu object is set to identify gnu objects," and in GNU gold it |
| 882 | // says "the second entry will be used by some runtime loaders". |
| 883 | // But how this field is being used is unclear. |
| 884 | // |
| 885 | // We are not really willing to mimic other linkers behaviors |
| 886 | // without understanding why they do that, but because all files |
| 887 | // generated by GNU tools have this special GOT value, and because |
| 888 | // we've been doing this for years, it is probably a safe bet to |
| 889 | // keep doing this for now. We really need to revisit this to see |
| 890 | // if we had to do this. |
| 891 | auto *P = reinterpret_cast<typename ELFT::Off *>(Buf); |
| 892 | P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31); |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 893 | Buf += HeaderEntriesNum * sizeof(uintX_t); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 894 | // Write 'page address' entries to the local part of the GOT. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 895 | for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) { |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 896 | size_t PageCount = getMipsPageCount(L.first->Size); |
| 897 | uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr); |
| 898 | for (size_t PI = 0; PI < PageCount; ++PI) { |
| 899 | uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t); |
| 900 | writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000); |
| 901 | } |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 902 | } |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 903 | Buf += PageEntriesNum * sizeof(uintX_t); |
| 904 | auto AddEntry = [&](const GotEntry &SA) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 905 | uint8_t *Entry = Buf; |
| 906 | Buf += sizeof(uintX_t); |
| 907 | const SymbolBody *Body = SA.first; |
| 908 | uintX_t VA = Body->template getVA<ELFT>(SA.second); |
| 909 | writeUint<ELFT>(Entry, VA); |
| 910 | }; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 911 | std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry); |
| 912 | std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry); |
| 913 | std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 914 | // Initialize TLS-related GOT entries. If the entry has a corresponding |
| 915 | // dynamic relocations, leave it initialized by zero. Write down adjusted |
| 916 | // TLS symbol's values otherwise. To calculate the adjustments use offsets |
| 917 | // for thread-local storage. |
| 918 | // https://www.linux-mips.org/wiki/NPTL |
Rui Ueyama | 104e235 | 2017-02-14 05:45:47 +0000 | [diff] [blame] | 919 | if (TlsIndexOff != -1U && !Config->pic()) |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 920 | writeUint<ELFT>(Buf + TlsIndexOff, 1); |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 921 | for (const SymbolBody *B : TlsEntries) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 922 | if (!B || B->isPreemptible()) |
| 923 | continue; |
| 924 | uintX_t VA = B->getVA<ELFT>(); |
| 925 | if (B->GotIndex != -1U) { |
| 926 | uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t); |
| 927 | writeUint<ELFT>(Entry, VA - 0x7000); |
| 928 | } |
| 929 | if (B->GlobalDynIndex != -1U) { |
| 930 | uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t); |
| 931 | writeUint<ELFT>(Entry, 1); |
| 932 | Entry += sizeof(uintX_t); |
| 933 | writeUint<ELFT>(Entry, VA - 0x8000); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 938 | template <class ELFT> |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 939 | GotPltSection<ELFT>::GotPltSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 940 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 941 | Target->GotPltEntrySize, ".got.plt") {} |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 942 | |
| 943 | template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 944 | Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size(); |
| 945 | Entries.push_back(&Sym); |
| 946 | } |
| 947 | |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 948 | template <class ELFT> size_t GotPltSection<ELFT>::getSize() const { |
| 949 | return (Target->GotPltHeaderEntriesNum + Entries.size()) * |
| 950 | Target->GotPltEntrySize; |
| 951 | } |
| 952 | |
| 953 | template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 954 | Target->writeGotPltHeader(Buf); |
| 955 | Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize; |
| 956 | for (const SymbolBody *B : Entries) { |
| 957 | Target->writeGotPlt(Buf, *B); |
| 958 | Buf += sizeof(uintX_t); |
| 959 | } |
| 960 | } |
| 961 | |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 962 | // On ARM the IgotPltSection is part of the GotSection, on other Targets it is |
| 963 | // part of the .got.plt |
| 964 | template <class ELFT> |
| 965 | IgotPltSection<ELFT>::IgotPltSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 966 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 967 | Target->GotPltEntrySize, |
| 968 | Config->EMachine == EM_ARM ? ".got" : ".got.plt") {} |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 969 | |
| 970 | template <class ELFT> void IgotPltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 971 | Sym.IsInIgot = true; |
| 972 | Sym.GotPltIndex = Entries.size(); |
| 973 | Entries.push_back(&Sym); |
| 974 | } |
| 975 | |
| 976 | template <class ELFT> size_t IgotPltSection<ELFT>::getSize() const { |
| 977 | return Entries.size() * Target->GotPltEntrySize; |
| 978 | } |
| 979 | |
| 980 | template <class ELFT> void IgotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 981 | for (const SymbolBody *B : Entries) { |
Peter Smith | 4b36029 | 2016-12-09 09:59:54 +0000 | [diff] [blame] | 982 | Target->writeIgotPlt(Buf, *B); |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 983 | Buf += sizeof(uintX_t); |
| 984 | } |
| 985 | } |
| 986 | |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 987 | template <class ELFT> |
| 988 | StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 989 | : SyntheticSection(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name), |
Rafael Espindola | 1b36eea | 2017-02-15 00:23:09 +0000 | [diff] [blame] | 990 | Dynamic(Dynamic) { |
| 991 | // ELF string tables start with a NUL byte. |
| 992 | addString(""); |
| 993 | } |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 994 | |
| 995 | // Adds a string to the string table. If HashIt is true we hash and check for |
| 996 | // duplicates. It is optional because the name of global symbols are already |
| 997 | // uniqued and hashing them again has a big cost for a small value: uniquing |
| 998 | // them with some other string that happens to be the same. |
| 999 | template <class ELFT> |
| 1000 | unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) { |
| 1001 | if (HashIt) { |
| 1002 | auto R = StringMap.insert(std::make_pair(S, this->Size)); |
| 1003 | if (!R.second) |
| 1004 | return R.first->second; |
| 1005 | } |
| 1006 | unsigned Ret = this->Size; |
| 1007 | this->Size = this->Size + S.size() + 1; |
| 1008 | Strings.push_back(S); |
| 1009 | return Ret; |
| 1010 | } |
| 1011 | |
| 1012 | template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1013 | for (StringRef S : Strings) { |
| 1014 | memcpy(Buf, S.data(), S.size()); |
| 1015 | Buf += S.size() + 1; |
| 1016 | } |
| 1017 | } |
| 1018 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1019 | // Returns the number of version definition entries. Because the first entry |
| 1020 | // is for the version definition itself, it is the number of versioned symbols |
| 1021 | // plus one. Note that we don't support multiple versions yet. |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1022 | static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } |
| 1023 | |
| 1024 | template <class ELFT> |
| 1025 | DynamicSection<ELFT>::DynamicSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1026 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, sizeof(uintX_t), |
| 1027 | ".dynamic") { |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1028 | this->Entsize = ELFT::Is64Bits ? 16 : 8; |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 1029 | |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1030 | // .dynamic section is not writable on MIPS. |
| 1031 | // See "Special Section" in Chapter 4 in the following document: |
| 1032 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 1033 | if (Config->EMachine == EM_MIPS) |
| 1034 | this->Flags = SHF_ALLOC; |
| 1035 | |
| 1036 | addEntries(); |
| 1037 | } |
| 1038 | |
| 1039 | // There are some dynamic entries that don't depend on other sections. |
| 1040 | // Such entries can be set early. |
| 1041 | template <class ELFT> void DynamicSection<ELFT>::addEntries() { |
| 1042 | // Add strings to .dynstr early so that .dynstr's size will be |
| 1043 | // fixed early. |
| 1044 | for (StringRef S : Config->AuxiliaryList) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1045 | add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1046 | if (!Config->RPath.empty()) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1047 | add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1048 | In<ELFT>::DynStrTab->addString(Config->RPath)}); |
| 1049 | for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles()) |
| 1050 | if (F->isNeeded()) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1051 | add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1052 | if (!Config->SoName.empty()) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1053 | add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Set DT_FLAGS and DT_FLAGS_1. |
| 1056 | uint32_t DtFlags = 0; |
| 1057 | uint32_t DtFlags1 = 0; |
| 1058 | if (Config->Bsymbolic) |
| 1059 | DtFlags |= DF_SYMBOLIC; |
| 1060 | if (Config->ZNodelete) |
| 1061 | DtFlags1 |= DF_1_NODELETE; |
| 1062 | if (Config->ZNow) { |
| 1063 | DtFlags |= DF_BIND_NOW; |
| 1064 | DtFlags1 |= DF_1_NOW; |
| 1065 | } |
| 1066 | if (Config->ZOrigin) { |
| 1067 | DtFlags |= DF_ORIGIN; |
| 1068 | DtFlags1 |= DF_1_ORIGIN; |
| 1069 | } |
| 1070 | |
| 1071 | if (DtFlags) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1072 | add({DT_FLAGS, DtFlags}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1073 | if (DtFlags1) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1074 | add({DT_FLAGS_1, DtFlags1}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1075 | |
Petr Hosek | 668bebe | 2016-12-07 02:05:42 +0000 | [diff] [blame] | 1076 | if (!Config->Shared && !Config->Relocatable) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1077 | add({DT_DEBUG, (uint64_t)0}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | // Add remaining entries to complete .dynamic contents. |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1081 | template <class ELFT> void DynamicSection<ELFT>::finalizeContents() { |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1082 | if (this->Size) |
| 1083 | return; // Already finalized. |
| 1084 | |
| 1085 | this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1086 | if (In<ELFT>::RelaDyn->OutSec->Size > 0) { |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1087 | bool IsRela = Config->isRela(); |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1088 | add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn}); |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1089 | add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->OutSec->Size}); |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1090 | add({IsRela ? DT_RELAENT : DT_RELENT, |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1091 | uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))}); |
| 1092 | |
| 1093 | // MIPS dynamic loader does not support RELCOUNT tag. |
| 1094 | // The problem is in the tight relation between dynamic |
| 1095 | // relocations and GOT. So do not emit this tag on MIPS. |
| 1096 | if (Config->EMachine != EM_MIPS) { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1097 | size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount(); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1098 | if (Config->ZCombreloc && NumRelativeRels) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1099 | add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1102 | if (In<ELFT>::RelaPlt->OutSec->Size > 0) { |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1103 | add({DT_JMPREL, In<ELFT>::RelaPlt}); |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1104 | add({DT_PLTRELSZ, In<ELFT>::RelaPlt->OutSec->Size}); |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1105 | add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT, |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1106 | In<ELFT>::GotPlt}); |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1107 | add({DT_PLTREL, uint64_t(Config->isRela() ? DT_RELA : DT_REL)}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1110 | add({DT_SYMTAB, In<ELFT>::DynSymTab}); |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1111 | add({DT_SYMENT, sizeof(Elf_Sym)}); |
| 1112 | add({DT_STRTAB, In<ELFT>::DynStrTab}); |
| 1113 | add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()}); |
George Rimar | 0a7412f | 2017-03-09 08:48:34 +0000 | [diff] [blame^] | 1114 | if (!Config->ZText) |
| 1115 | add({DT_TEXTREL, (uint64_t)0}); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1116 | if (In<ELFT>::GnuHashTab) |
| 1117 | add({DT_GNU_HASH, In<ELFT>::GnuHashTab}); |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1118 | if (In<ELFT>::HashTab) |
| 1119 | add({DT_HASH, In<ELFT>::HashTab}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1120 | |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1121 | if (Out::PreinitArray) { |
| 1122 | add({DT_PREINIT_ARRAY, Out::PreinitArray}); |
| 1123 | add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1124 | } |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1125 | if (Out::InitArray) { |
| 1126 | add({DT_INIT_ARRAY, Out::InitArray}); |
| 1127 | add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1128 | } |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1129 | if (Out::FiniArray) { |
| 1130 | add({DT_FINI_ARRAY, Out::FiniArray}); |
| 1131 | add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 +0000 | [diff] [blame] | 1134 | if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Init)) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1135 | add({DT_INIT, B}); |
Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 +0000 | [diff] [blame] | 1136 | if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Fini)) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1137 | add({DT_FINI, B}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1138 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1139 | bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0; |
| 1140 | if (HasVerNeed || In<ELFT>::VerDef) |
| 1141 | add({DT_VERSYM, In<ELFT>::VerSym}); |
| 1142 | if (In<ELFT>::VerDef) { |
| 1143 | add({DT_VERDEF, In<ELFT>::VerDef}); |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1144 | add({DT_VERDEFNUM, getVerDefNum()}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1145 | } |
| 1146 | if (HasVerNeed) { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1147 | add({DT_VERNEED, In<ELFT>::VerNeed}); |
| 1148 | add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | if (Config->EMachine == EM_MIPS) { |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1152 | add({DT_MIPS_RLD_VERSION, 1}); |
| 1153 | add({DT_MIPS_FLAGS, RHF_NOTPOT}); |
| 1154 | add({DT_MIPS_BASE_ADDRESS, Config->ImageBase}); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1155 | add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()}); |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 1156 | add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()}); |
| 1157 | if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry()) |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1158 | add({DT_MIPS_GOTSYM, B->DynsymIndex}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1159 | else |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1160 | add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()}); |
Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 1161 | add({DT_PLTGOT, In<ELFT>::MipsGot}); |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 1162 | if (In<ELFT>::MipsRldMap) |
| 1163 | add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap}); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1166 | this->OutSec->Link = this->Link; |
| 1167 | |
| 1168 | // +1 for DT_NULL |
| 1169 | this->Size = (Entries.size() + 1) * this->Entsize; |
| 1170 | } |
| 1171 | |
| 1172 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1173 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 1174 | |
| 1175 | for (const Entry &E : Entries) { |
| 1176 | P->d_tag = E.Tag; |
| 1177 | switch (E.Kind) { |
| 1178 | case Entry::SecAddr: |
| 1179 | P->d_un.d_ptr = E.OutSec->Addr; |
| 1180 | break; |
| 1181 | case Entry::InSecAddr: |
| 1182 | P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff; |
| 1183 | break; |
| 1184 | case Entry::SecSize: |
| 1185 | P->d_un.d_val = E.OutSec->Size; |
| 1186 | break; |
| 1187 | case Entry::SymAddr: |
| 1188 | P->d_un.d_ptr = E.Sym->template getVA<ELFT>(); |
| 1189 | break; |
| 1190 | case Entry::PlainInt: |
| 1191 | P->d_un.d_val = E.Val; |
| 1192 | break; |
| 1193 | } |
| 1194 | ++P; |
| 1195 | } |
| 1196 | } |
| 1197 | |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1198 | template <class ELFT> |
| 1199 | typename ELFT::uint DynamicReloc<ELFT>::getOffset() const { |
Rafael Espindola | e129409 | 2017-03-08 16:03:41 +0000 | [diff] [blame] | 1200 | return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Rafael Espindola | 7386cea | 2017-02-16 00:12:34 +0000 | [diff] [blame] | 1203 | template <class ELFT> int64_t DynamicReloc<ELFT>::getAddend() const { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1204 | if (UseSymVA) |
| 1205 | return Sym->getVA<ELFT>(Addend); |
| 1206 | return Addend; |
| 1207 | } |
| 1208 | |
| 1209 | template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const { |
| 1210 | if (Sym && !UseSymVA) |
| 1211 | return Sym->DynsymIndex; |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | template <class ELFT> |
| 1216 | RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort) |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1217 | : SyntheticSection(SHF_ALLOC, Config->isRela() ? SHT_RELA : SHT_REL, |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1218 | sizeof(uintX_t), Name), |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1219 | Sort(Sort) { |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1220 | this->Entsize = Config->isRela() ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | template <class ELFT> |
| 1224 | void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) { |
| 1225 | if (Reloc.Type == Target->RelativeRel) |
| 1226 | ++NumRelativeRelocs; |
| 1227 | Relocs.push_back(Reloc); |
| 1228 | } |
| 1229 | |
| 1230 | template <class ELFT, class RelTy> |
| 1231 | static bool compRelocations(const RelTy &A, const RelTy &B) { |
Rui Ueyama | df8eb17 | 2017-03-07 00:43:33 +0000 | [diff] [blame] | 1232 | bool AIsRel = A.getType(Config->isMips64EL()) == Target->RelativeRel; |
| 1233 | bool BIsRel = B.getType(Config->isMips64EL()) == Target->RelativeRel; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1234 | if (AIsRel != BIsRel) |
| 1235 | return AIsRel; |
| 1236 | |
Rui Ueyama | df8eb17 | 2017-03-07 00:43:33 +0000 | [diff] [blame] | 1237 | return A.getSymbol(Config->isMips64EL()) < B.getSymbol(Config->isMips64EL()); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1241 | uint8_t *BufBegin = Buf; |
| 1242 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
| 1243 | auto *P = reinterpret_cast<Elf_Rela *>(Buf); |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1244 | Buf += Config->isRela() ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1245 | |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1246 | if (Config->isRela()) |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1247 | P->r_addend = Rel.getAddend(); |
| 1248 | P->r_offset = Rel.getOffset(); |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 1249 | if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot) |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1250 | // Dynamic relocation against MIPS GOT section make deal TLS entries |
| 1251 | // allocated in the end of the GOT. We need to adjust the offset to take |
| 1252 | // in account 'local' and 'global' GOT entries. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 1253 | P->r_offset += In<ELFT>::MipsGot->getTlsOffset(); |
Rui Ueyama | df8eb17 | 2017-03-07 00:43:33 +0000 | [diff] [blame] | 1254 | P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->isMips64EL()); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | if (Sort) { |
Rui Ueyama | af6198d | 2017-03-07 00:43:53 +0000 | [diff] [blame] | 1258 | if (Config->isRela()) |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1259 | std::stable_sort((Elf_Rela *)BufBegin, |
| 1260 | (Elf_Rela *)BufBegin + Relocs.size(), |
| 1261 | compRelocations<ELFT, Elf_Rela>); |
| 1262 | else |
| 1263 | std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(), |
| 1264 | compRelocations<ELFT, Elf_Rel>); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { |
| 1269 | return this->Entsize * Relocs.size(); |
| 1270 | } |
| 1271 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1272 | template <class ELFT> void RelocationSection<ELFT>::finalizeContents() { |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1273 | this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex |
| 1274 | : In<ELFT>::SymTab->OutSec->SectionIndex; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Set required output section properties. |
| 1277 | this->OutSec->Link = this->Link; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1280 | template <class ELFT> |
| 1281 | SymbolTableSection<ELFT>::SymbolTableSection( |
| 1282 | StringTableSection<ELFT> &StrTabSec) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1283 | : SyntheticSection(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0, |
| 1284 | StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, |
| 1285 | sizeof(uintX_t), |
| 1286 | StrTabSec.isDynamic() ? ".dynsym" : ".symtab"), |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1287 | StrTabSec(StrTabSec) { |
| 1288 | this->Entsize = sizeof(Elf_Sym); |
| 1289 | } |
| 1290 | |
| 1291 | // Orders symbols according to their positions in the GOT, |
| 1292 | // in compliance with MIPS ABI rules. |
| 1293 | // See "Global Offset Table" in Chapter 5 in the following document |
| 1294 | // for detailed description: |
| 1295 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Rui Ueyama | aa5c527 | 2017-02-27 03:31:38 +0000 | [diff] [blame] | 1296 | static bool sortMipsSymbols(const SymbolTableEntry &L, const SymbolTableEntry &R) { |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1297 | // Sort entries related to non-local preemptible symbols by GOT indexes. |
| 1298 | // All other entries go to the first part of GOT in arbitrary order. |
Rui Ueyama | aa5c527 | 2017-02-27 03:31:38 +0000 | [diff] [blame] | 1299 | bool LIsInLocalGot = !L.Symbol->IsInGlobalMipsGot; |
| 1300 | bool RIsInLocalGot = !R.Symbol->IsInGlobalMipsGot; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1301 | if (LIsInLocalGot || RIsInLocalGot) |
| 1302 | return !RIsInLocalGot; |
Rui Ueyama | aa5c527 | 2017-02-27 03:31:38 +0000 | [diff] [blame] | 1303 | return L.Symbol->GotIndex < R.Symbol->GotIndex; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Rui Ueyama | bb07d10 | 2017-02-27 03:31:19 +0000 | [diff] [blame] | 1306 | // Finalize a symbol table. The ELF spec requires that all local |
| 1307 | // symbols precede global symbols, so we sort symbol entries in this |
| 1308 | // function. (For .dynsym, we don't do that because symbols for |
| 1309 | // dynamic linking are inherently all globals.) |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1310 | template <class ELFT> void SymbolTableSection<ELFT>::finalizeContents() { |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1311 | this->OutSec->Link = StrTabSec.OutSec->SectionIndex; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1312 | |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1313 | // If it is a .dynsym, there should be no local symbols, but we need |
| 1314 | // to do a few things for the dynamic linker. |
| 1315 | if (this->Type == SHT_DYNSYM) { |
| 1316 | // Section's Info field has the index of the first non-local symbol. |
| 1317 | // Because the first symbol entry is a null entry, 1 is the first. |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1318 | this->OutSec->Info = 1; |
| 1319 | |
| 1320 | if (In<ELFT>::GnuHashTab) { |
| 1321 | // NB: It also sorts Symbols to meet the GNU hash table requirements. |
| 1322 | In<ELFT>::GnuHashTab->addSymbols(Symbols); |
| 1323 | } else if (Config->EMachine == EM_MIPS) { |
| 1324 | std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols); |
| 1325 | } |
| 1326 | |
| 1327 | size_t I = 0; |
| 1328 | for (const SymbolTableEntry &S : Symbols) |
| 1329 | S.Symbol->DynsymIndex = ++I; |
Rui Ueyama | 406331e | 2017-02-28 04:11:01 +0000 | [diff] [blame] | 1330 | return; |
Peter Smith | 5586543 | 2017-02-20 11:12:33 +0000 | [diff] [blame] | 1331 | } |
Peter Smith | 1ec42d9 | 2017-03-08 14:06:24 +0000 | [diff] [blame] | 1332 | } |
Peter Smith | 5586543 | 2017-02-20 11:12:33 +0000 | [diff] [blame] | 1333 | |
Peter Smith | 1ec42d9 | 2017-03-08 14:06:24 +0000 | [diff] [blame] | 1334 | template <class ELFT> void SymbolTableSection<ELFT>::postThunkContents() { |
| 1335 | if (this->Type == SHT_DYNSYM) |
| 1336 | return; |
| 1337 | // move all local symbols before global symbols. |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1338 | auto It = std::stable_partition( |
| 1339 | Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) { |
| 1340 | return S.Symbol->isLocal() || |
| 1341 | S.Symbol->symbol()->computeBinding() == STB_LOCAL; |
| 1342 | }); |
| 1343 | size_t NumLocals = It - Symbols.begin(); |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1344 | this->OutSec->Info = NumLocals + 1; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Rui Ueyama | b8dcdb5 | 2017-02-28 04:20:16 +0000 | [diff] [blame] | 1347 | template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) { |
| 1348 | // Adding a local symbol to a .dynsym is a bug. |
| 1349 | assert(this->Type != SHT_DYNSYM || !B->isLocal()); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1350 | |
Rui Ueyama | b8dcdb5 | 2017-02-28 04:20:16 +0000 | [diff] [blame] | 1351 | bool HashIt = B->isLocal(); |
| 1352 | Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)}); |
George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | template <class ELFT> |
| 1356 | size_t SymbolTableSection<ELFT>::getSymbolIndex(SymbolBody *Body) { |
Rafael Espindola | 08d6a3f | 2017-02-11 01:40:49 +0000 | [diff] [blame] | 1357 | auto I = llvm::find_if(Symbols, [&](const SymbolTableEntry &E) { |
| 1358 | if (E.Symbol == Body) |
| 1359 | return true; |
| 1360 | // This is used for -r, so we have to handle multiple section |
| 1361 | // symbols being combined. |
| 1362 | if (Body->Type == STT_SECTION && E.Symbol->Type == STT_SECTION) |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 1363 | return cast<DefinedRegular>(Body)->Section->getOutputSection() == |
| 1364 | cast<DefinedRegular>(E.Symbol)->Section->getOutputSection(); |
Rafael Espindola | 08d6a3f | 2017-02-11 01:40:49 +0000 | [diff] [blame] | 1365 | return false; |
| 1366 | }); |
Rafael Espindola | 0b034d6 | 2017-01-26 14:09:18 +0000 | [diff] [blame] | 1367 | if (I == Symbols.end()) |
| 1368 | return 0; |
George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1369 | return I - Symbols.begin() + 1; |
| 1370 | } |
| 1371 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1372 | // Write the internal symbol table contents to the output symbol table. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1373 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1374 | // The first entry is a null entry as per the ELF spec. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1375 | Buf += sizeof(Elf_Sym); |
| 1376 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1377 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1378 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1379 | for (SymbolTableEntry &Ent : Symbols) { |
| 1380 | SymbolBody *Body = Ent.Symbol; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1381 | |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1382 | // Set st_info and st_other. |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1383 | if (Body->isLocal()) { |
| 1384 | ESym->setBindingAndType(STB_LOCAL, Body->Type); |
| 1385 | } else { |
| 1386 | ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type); |
| 1387 | ESym->setVisibility(Body->symbol()->Visibility); |
| 1388 | } |
| 1389 | |
| 1390 | ESym->st_name = Ent.StrTabOffset; |
Rui Ueyama | 3bc3901 | 2017-02-27 22:39:50 +0000 | [diff] [blame] | 1391 | ESym->st_size = Body->getSize<ELFT>(); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1392 | |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1393 | // Set a section index. |
| 1394 | if (const OutputSection *OutSec = Body->getOutputSection<ELFT>()) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1395 | ESym->st_shndx = OutSec->SectionIndex; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 1396 | else if (isa<DefinedRegular>(Body)) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1397 | ESym->st_shndx = SHN_ABS; |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1398 | else if (isa<DefinedCommon>(Body)) |
Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 1399 | ESym->st_shndx = SHN_COMMON; |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1400 | |
| 1401 | // st_value is usually an address of a symbol, but that has a |
| 1402 | // special meaining for uninstantiated common symbols (this can |
| 1403 | // occur if -r is given). |
| 1404 | if (!Config->DefineCommon && isa<DefinedCommon>(Body)) |
Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 1405 | ESym->st_value = cast<DefinedCommon>(Body)->Alignment; |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1406 | else |
| 1407 | ESym->st_value = Body->getVA<ELFT>(); |
| 1408 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1409 | ++ESym; |
| 1410 | } |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1411 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1412 | // On MIPS we need to mark symbol which has a PLT entry and requires |
| 1413 | // pointer equality by STO_MIPS_PLT flag. That is necessary to help |
| 1414 | // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. |
| 1415 | // https://sourceware.org/ml/binutils/2008-07/txt00000.txt |
| 1416 | if (Config->EMachine == EM_MIPS) { |
| 1417 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| 1418 | |
| 1419 | for (SymbolTableEntry &Ent : Symbols) { |
| 1420 | SymbolBody *Body = Ent.Symbol; |
Rui Ueyama | 924b361 | 2017-02-16 06:12:22 +0000 | [diff] [blame] | 1421 | if (Body->isInPlt() && Body->NeedsPltAddr) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1422 | ESym->st_other |= STO_MIPS_PLT; |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1423 | |
| 1424 | if (Config->Relocatable) |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 1425 | if (auto *D = dyn_cast<DefinedRegular>(Body)) |
| 1426 | if (D->isMipsPIC<ELFT>()) |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1427 | ESym->st_other |= STO_MIPS_PIC; |
| 1428 | ++ESym; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1429 | } |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Rui Ueyama | e412063 | 2017-02-28 22:05:13 +0000 | [diff] [blame] | 1433 | // .hash and .gnu.hash sections contain on-disk hash tables that map |
| 1434 | // symbol names to their dynamic symbol table indices. Their purpose |
| 1435 | // is to help the dynamic linker resolve symbols quickly. If ELF files |
| 1436 | // don't have them, the dynamic linker has to do linear search on all |
| 1437 | // dynamic symbols, which makes programs slower. Therefore, a .hash |
| 1438 | // section is added to a DSO by default. A .gnu.hash is added if you |
| 1439 | // give the -hash-style=gnu or -hash-style=both option. |
| 1440 | // |
| 1441 | // The Unix semantics of resolving dynamic symbols is somewhat expensive. |
| 1442 | // Each ELF file has a list of DSOs that the ELF file depends on and a |
| 1443 | // list of dynamic symbols that need to be resolved from any of the |
| 1444 | // DSOs. That means resolving all dynamic symbols takes O(m)*O(n) |
| 1445 | // where m is the number of DSOs and n is the number of dynamic |
| 1446 | // symbols. For modern large programs, both m and n are large. So |
| 1447 | // making each step faster by using hash tables substiantially |
| 1448 | // improves time to load programs. |
| 1449 | // |
| 1450 | // (Note that this is not the only way to design the shared library. |
| 1451 | // For instance, the Windows DLL takes a different approach. On |
| 1452 | // Windows, each dynamic symbol has a name of DLL from which the symbol |
| 1453 | // has to be resolved. That makes the cost of symbol resolution O(n). |
| 1454 | // This disables some hacky techniques you can use on Unix such as |
| 1455 | // LD_PRELOAD, but this is arguably better semantics than the Unix ones.) |
| 1456 | // |
| 1457 | // Due to historical reasons, we have two different hash tables, .hash |
| 1458 | // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new |
| 1459 | // and better version of .hash. .hash is just an on-disk hash table, but |
| 1460 | // .gnu.hash has a bloom filter in addition to a hash table to skip |
| 1461 | // DSOs very quickly. If you are sure that your dynamic linker knows |
| 1462 | // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a |
| 1463 | // safe bet is to specify -hash-style=both for backward compatibilty. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1464 | template <class ELFT> |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1465 | GnuHashTableSection<ELFT>::GnuHashTableSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1466 | : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t), ".gnu.hash") { |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1467 | this->Entsize = ELFT::Is64Bits ? 0 : 4; |
| 1468 | } |
| 1469 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1470 | template <class ELFT> void GnuHashTableSection<ELFT>::finalizeContents() { |
Rui Ueyama | c3726f8 | 2017-02-28 04:41:20 +0000 | [diff] [blame] | 1471 | this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Computes bloom filter size in word size. We want to allocate 8 |
| 1474 | // bits for each symbol. It must be a power of two. |
| 1475 | if (Symbols.empty()) |
| 1476 | MaskWords = 1; |
| 1477 | else |
| 1478 | MaskWords = NextPowerOf2((Symbols.size() - 1) / sizeof(uintX_t)); |
| 1479 | |
| 1480 | Size = 16; // Header |
| 1481 | Size += sizeof(uintX_t) * MaskWords; // Bloom filter |
| 1482 | Size += NBuckets * 4; // Hash buckets |
| 1483 | Size += Symbols.size() * 4; // Hash values |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1487 | // Write a header. |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1488 | const endianness E = ELFT::TargetEndianness; |
| 1489 | write32<E>(Buf, NBuckets); |
| 1490 | write32<E>(Buf + 4, In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size()); |
| 1491 | write32<E>(Buf + 8, MaskWords); |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1492 | write32<E>(Buf + 12, getShift2()); |
| 1493 | Buf += 16; |
| 1494 | |
Rui Ueyama | 7986b45 | 2017-03-01 18:09:09 +0000 | [diff] [blame] | 1495 | // Write a bloom filter and a hash table. |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1496 | writeBloomFilter(Buf); |
| 1497 | Buf += sizeof(uintX_t) * MaskWords; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1498 | writeHashTable(Buf); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Rui Ueyama | 7986b45 | 2017-03-01 18:09:09 +0000 | [diff] [blame] | 1501 | // This function writes a 2-bit bloom filter. This bloom filter alone |
| 1502 | // usually filters out 80% or more of all symbol lookups [1]. |
| 1503 | // The dynamic linker uses the hash table only when a symbol is not |
| 1504 | // filtered out by a bloom filter. |
| 1505 | // |
| 1506 | // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2), |
| 1507 | // p.9, https://www.akkadia.org/drepper/dsohowto.pdf |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1508 | template <class ELFT> |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1509 | void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *Buf) { |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1510 | typedef typename ELFT::Off Elf_Off; |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1511 | const unsigned C = sizeof(uintX_t) * 8; |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1512 | |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1513 | auto *Filter = reinterpret_cast<Elf_Off *>(Buf); |
| 1514 | for (const Entry &Sym : Symbols) { |
| 1515 | size_t I = (Sym.Hash / C) & (MaskWords - 1); |
| 1516 | Filter[I] |= uintX_t(1) << (Sym.Hash % C); |
| 1517 | Filter[I] |= uintX_t(1) << ((Sym.Hash >> getShift2()) % C); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1518 | } |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | template <class ELFT> |
| 1522 | void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1523 | // A 32-bit integer type in the target endianness. |
| 1524 | typedef typename ELFT::Word Elf_Word; |
| 1525 | |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1526 | // Group symbols by hash value. |
| 1527 | std::vector<std::vector<Entry>> Syms(NBuckets); |
| 1528 | for (const Entry &Ent : Symbols) |
| 1529 | Syms[Ent.Hash % NBuckets].push_back(Ent); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1530 | |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1531 | // Write hash buckets. Hash buckets contain indices in the following |
| 1532 | // hash value table. |
| 1533 | Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); |
| 1534 | for (size_t I = 0; I < NBuckets; ++I) |
| 1535 | if (!Syms[I].empty()) |
| 1536 | Buckets[I] = Syms[I][0].Body->DynsymIndex; |
| 1537 | |
| 1538 | // Write a hash value table. It represents a sequence of chains that |
| 1539 | // share the same hash modulo value. The last element of each chain |
| 1540 | // is terminated by LSB 1. |
| 1541 | Elf_Word *Values = Buckets + NBuckets; |
| 1542 | size_t I = 0; |
| 1543 | for (std::vector<Entry> &Vec : Syms) { |
| 1544 | if (Vec.empty()) |
| 1545 | continue; |
| 1546 | for (const Entry &Ent : makeArrayRef(Vec).drop_back()) |
| 1547 | Values[I++] = Ent.Hash & ~1; |
| 1548 | Values[I++] = Vec.back().Hash | 1; |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1549 | } |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | static uint32_t hashGnu(StringRef Name) { |
| 1553 | uint32_t H = 5381; |
| 1554 | for (uint8_t C : Name) |
| 1555 | H = (H << 5) + H + C; |
| 1556 | return H; |
| 1557 | } |
| 1558 | |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1559 | // Returns a number of hash buckets to accomodate given number of elements. |
| 1560 | // We want to choose a moderate number that is not too small (which |
| 1561 | // causes too many hash collisions) and not too large (which wastes |
| 1562 | // disk space.) |
| 1563 | // |
| 1564 | // We return a prime number because it (is believed to) achieve good |
| 1565 | // hash distribution. |
| 1566 | static size_t getBucketSize(size_t NumSymbols) { |
| 1567 | // List of largest prime numbers that are not greater than 2^n + 1. |
| 1568 | for (size_t N : {131071, 65521, 32749, 16381, 8191, 4093, 2039, 1021, 509, |
| 1569 | 251, 127, 61, 31, 13, 7, 3, 1}) |
| 1570 | if (N <= NumSymbols) |
| 1571 | return N; |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1575 | // Add symbols to this symbol hash table. Note that this function |
| 1576 | // destructively sort a given vector -- which is needed because |
| 1577 | // GNU-style hash table places some sorting requirements. |
| 1578 | template <class ELFT> |
| 1579 | void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) { |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1580 | // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce |
| 1581 | // its type correctly. |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1582 | std::vector<SymbolTableEntry>::iterator Mid = |
| 1583 | std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) { |
| 1584 | return S.Symbol->isUndefined(); |
| 1585 | }); |
| 1586 | if (Mid == V.end()) |
| 1587 | return; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1588 | |
| 1589 | for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) { |
| 1590 | SymbolBody *B = Ent.Symbol; |
| 1591 | Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())}); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1594 | NBuckets = getBucketSize(Symbols.size()); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1595 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1596 | [&](const Entry &L, const Entry &R) { |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1597 | return L.Hash % NBuckets < R.Hash % NBuckets; |
| 1598 | }); |
| 1599 | |
| 1600 | V.erase(Mid, V.end()); |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1601 | for (const Entry &Ent : Symbols) |
| 1602 | V.push_back({Ent.Body, Ent.StrTabOffset}); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1605 | template <class ELFT> |
| 1606 | HashTableSection<ELFT>::HashTableSection() |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1607 | : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") { |
| 1608 | this->Entsize = 4; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1611 | template <class ELFT> void HashTableSection<ELFT>::finalizeContents() { |
Rui Ueyama | c3726f8 | 2017-02-28 04:41:20 +0000 | [diff] [blame] | 1612 | this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1613 | |
| 1614 | unsigned NumEntries = 2; // nbucket and nchain. |
| 1615 | NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. |
| 1616 | |
| 1617 | // Create as many buckets as there are symbols. |
| 1618 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 1619 | // support for SHT_GNU_HASH is probably even more profitable. |
| 1620 | NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1621 | this->Size = NumEntries * 4; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1625 | // A 32-bit integer type in the target endianness. |
| 1626 | typedef typename ELFT::Word Elf_Word; |
| 1627 | |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1628 | unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1629 | |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1630 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 1631 | *P++ = NumSymbols; // nbucket |
| 1632 | *P++ = NumSymbols; // nchain |
| 1633 | |
| 1634 | Elf_Word *Buckets = P; |
| 1635 | Elf_Word *Chains = P + NumSymbols; |
| 1636 | |
| 1637 | for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { |
| 1638 | SymbolBody *Body = S.Symbol; |
| 1639 | StringRef Name = Body->getName(); |
| 1640 | unsigned I = Body->DynsymIndex; |
| 1641 | uint32_t Hash = hashSysV(Name) % NumSymbols; |
| 1642 | Chains[I] = Buckets[Hash]; |
| 1643 | Buckets[Hash] = I; |
| 1644 | } |
| 1645 | } |
| 1646 | |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1647 | template <class ELFT> |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1648 | PltSection<ELFT>::PltSection(size_t S) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1649 | : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"), |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1650 | HeaderSize(S) {} |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1651 | |
| 1652 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1653 | // At beginning of PLT but not the IPLT, we have code to call the dynamic |
| 1654 | // linker to resolve dynsyms at runtime. Write such code. |
| 1655 | if (HeaderSize != 0) |
| 1656 | Target->writePltHeader(Buf); |
| 1657 | size_t Off = HeaderSize; |
| 1658 | // The IPlt is immediately after the Plt, account for this in RelOff |
| 1659 | unsigned PltOff = getPltRelocOff(); |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1660 | |
| 1661 | for (auto &I : Entries) { |
| 1662 | const SymbolBody *B = I.first; |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1663 | unsigned RelOff = I.second + PltOff; |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1664 | uint64_t Got = B->getGotPltVA<ELFT>(); |
| 1665 | uint64_t Plt = this->getVA() + Off; |
| 1666 | Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); |
| 1667 | Off += Target->PltEntrySize; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 1672 | Sym.PltIndex = Entries.size(); |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1673 | RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt; |
| 1674 | if (HeaderSize == 0) { |
| 1675 | PltRelocSection = In<ELFT>::RelaIplt; |
| 1676 | Sym.IsInIplt = true; |
| 1677 | } |
| 1678 | unsigned RelOff = PltRelocSection->getRelocOffset(); |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1679 | Entries.push_back(std::make_pair(&Sym, RelOff)); |
| 1680 | } |
| 1681 | |
| 1682 | template <class ELFT> size_t PltSection<ELFT>::getSize() const { |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1683 | return HeaderSize + Entries.size() * Target->PltEntrySize; |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1686 | // Some architectures such as additional symbols in the PLT section. For |
| 1687 | // example ARM uses mapping symbols to aid disassembly |
| 1688 | template <class ELFT> void PltSection<ELFT>::addSymbols() { |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1689 | // The PLT may have symbols defined for the Header, the IPLT has no header |
| 1690 | if (HeaderSize != 0) |
| 1691 | Target->addPltHeaderSymbols(this); |
| 1692 | size_t Off = HeaderSize; |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1693 | for (size_t I = 0; I < Entries.size(); ++I) { |
| 1694 | Target->addPltSymbols(this, Off); |
| 1695 | Off += Target->PltEntrySize; |
| 1696 | } |
| 1697 | } |
| 1698 | |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1699 | template <class ELFT> unsigned PltSection<ELFT>::getPltRelocOff() const { |
| 1700 | return (HeaderSize == 0) ? In<ELFT>::Plt->getSize() : 0; |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1703 | template <class ELFT> |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1704 | GdbIndexSection<ELFT>::GdbIndexSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1705 | : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"), |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1706 | StringPool(llvm::StringTableBuilder::ELF) {} |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1707 | |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1708 | // Iterative hash function for symbol's name is described in .gdb_index format |
| 1709 | // specification. Note that we use one for version 5 to 7 here, it is different |
| 1710 | // for version 4. |
| 1711 | static uint32_t hash(StringRef Str) { |
| 1712 | uint32_t R = 0; |
| 1713 | for (uint8_t C : Str) |
| 1714 | R = R * 67 + tolower(C) - 113; |
| 1715 | return R; |
| 1716 | } |
| 1717 | |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1718 | static std::vector<std::pair<uint64_t, uint64_t>> |
| 1719 | readCuList(DWARFContext &Dwarf, InputSection *Sec) { |
| 1720 | std::vector<std::pair<uint64_t, uint64_t>> Ret; |
| 1721 | for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) |
| 1722 | Ret.push_back({Sec->OutSecOff + CU->getOffset(), CU->getLength() + 4}); |
| 1723 | return Ret; |
| 1724 | } |
| 1725 | |
| 1726 | template <class ELFT> |
| 1727 | static InputSectionBase *findSection(ArrayRef<InputSectionBase *> Arr, |
| 1728 | uint64_t Offset) { |
| 1729 | for (InputSectionBase *S : Arr) |
| 1730 | if (S && S != &InputSection::Discarded) |
Rafael Espindola | 35ae65e | 2017-03-08 15:57:17 +0000 | [diff] [blame] | 1731 | if (Offset >= S->getOffsetInFile() && |
| 1732 | Offset < S->getOffsetInFile() + S->getSize()) |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1733 | return S; |
| 1734 | return nullptr; |
| 1735 | } |
| 1736 | |
| 1737 | template <class ELFT> |
| 1738 | static std::vector<AddressEntry> |
| 1739 | readAddressArea(DWARFContext &Dwarf, InputSection *Sec, size_t CurrentCU) { |
| 1740 | std::vector<AddressEntry> Ret; |
| 1741 | |
| 1742 | for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) { |
| 1743 | DWARFAddressRangesVector Ranges; |
| 1744 | CU->collectAddressRanges(Ranges); |
| 1745 | |
| 1746 | ArrayRef<InputSectionBase *> Sections = |
| 1747 | Sec->template getFile<ELFT>()->getSections(); |
| 1748 | |
| 1749 | for (std::pair<uint64_t, uint64_t> &R : Ranges) |
| 1750 | if (InputSectionBase *S = findSection<ELFT>(Sections, R.first)) |
Rafael Espindola | 35ae65e | 2017-03-08 15:57:17 +0000 | [diff] [blame] | 1751 | Ret.push_back({S, R.first - S->getOffsetInFile(), |
| 1752 | R.second - S->getOffsetInFile(), CurrentCU}); |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1753 | ++CurrentCU; |
| 1754 | } |
| 1755 | return Ret; |
| 1756 | } |
| 1757 | |
| 1758 | static std::vector<std::pair<StringRef, uint8_t>> |
| 1759 | readPubNamesAndTypes(DWARFContext &Dwarf, bool IsLE) { |
| 1760 | StringRef Data[] = {Dwarf.getGnuPubNamesSection(), |
| 1761 | Dwarf.getGnuPubTypesSection()}; |
| 1762 | |
| 1763 | std::vector<std::pair<StringRef, uint8_t>> Ret; |
| 1764 | for (StringRef D : Data) { |
| 1765 | DWARFDebugPubTable PubTable(D, IsLE, true); |
| 1766 | for (const DWARFDebugPubTable::Set &Set : PubTable.getData()) |
| 1767 | for (const DWARFDebugPubTable::Entry &Ent : Set.Entries) |
| 1768 | Ret.push_back({Ent.Name, Ent.Descriptor.toBits()}); |
| 1769 | } |
| 1770 | return Ret; |
| 1771 | } |
| 1772 | |
| 1773 | class ObjInfoTy : public llvm::LoadedObjectInfo { |
| 1774 | uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override { |
| 1775 | auto &S = static_cast<const object::ELFSectionRef &>(Sec); |
| 1776 | if (S.getFlags() & ELF::SHF_ALLOC) |
| 1777 | return S.getOffset(); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | std::unique_ptr<llvm::LoadedObjectInfo> clone() const override { return {}; } |
| 1782 | }; |
| 1783 | |
Rui Ueyama | 475b1dd | 2017-03-01 22:02:17 +0000 | [diff] [blame] | 1784 | template <class ELFT> void GdbIndexSection<ELFT>::readDwarf(InputSection *Sec) { |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1785 | elf::ObjectFile<ELFT> *File = Sec->template getFile<ELFT>(); |
| 1786 | |
| 1787 | Expected<std::unique_ptr<object::ObjectFile>> Obj = |
| 1788 | object::ObjectFile::createObjectFile(File->MB); |
| 1789 | if (!Obj) { |
| 1790 | error(toString(File) + ": error creating DWARF context"); |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1791 | return; |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | ObjInfoTy ObjInfo; |
| 1795 | DWARFContextInMemory Dwarf(*Obj.get(), &ObjInfo); |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1796 | |
| 1797 | size_t CuId = CompilationUnits.size(); |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1798 | for (std::pair<uint64_t, uint64_t> &P : readCuList(Dwarf, Sec)) |
| 1799 | CompilationUnits.push_back(P); |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1800 | |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1801 | for (AddressEntry &Ent : readAddressArea<ELFT>(Dwarf, Sec, CuId)) |
| 1802 | AddressArea.push_back(Ent); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1803 | |
| 1804 | std::vector<std::pair<StringRef, uint8_t>> NamesAndTypes = |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1805 | readPubNamesAndTypes(Dwarf, ELFT::TargetEndianness == support::little); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1806 | |
| 1807 | for (std::pair<StringRef, uint8_t> &Pair : NamesAndTypes) { |
| 1808 | uint32_t Hash = hash(Pair.first); |
| 1809 | size_t Offset = StringPool.add(Pair.first); |
| 1810 | |
| 1811 | bool IsNew; |
| 1812 | GdbSymbol *Sym; |
| 1813 | std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset); |
| 1814 | if (IsNew) { |
| 1815 | Sym->CuVectorIndex = CuVectors.size(); |
| 1816 | CuVectors.push_back({{CuId, Pair.second}}); |
| 1817 | continue; |
| 1818 | } |
| 1819 | |
Rui Ueyama | aab18c0 | 2017-03-01 22:24:46 +0000 | [diff] [blame] | 1820 | CuVectors[Sym->CuVectorIndex].push_back({CuId, Pair.second}); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1821 | } |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1824 | template <class ELFT> void GdbIndexSection<ELFT>::finalizeContents() { |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1825 | if (Finalized) |
| 1826 | return; |
| 1827 | Finalized = true; |
| 1828 | |
Rui Ueyama | 475b1dd | 2017-03-01 22:02:17 +0000 | [diff] [blame] | 1829 | for (InputSectionBase *S : InputSections) |
| 1830 | if (InputSection *IS = dyn_cast<InputSection>(S)) |
| 1831 | if (IS->OutSec && IS->Name == ".debug_info") |
| 1832 | readDwarf(IS); |
| 1833 | |
Rui Ueyama | d0e07b9 | 2017-03-01 21:08:21 +0000 | [diff] [blame] | 1834 | SymbolTable.finalizeContents(); |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1835 | |
| 1836 | // GdbIndex header consist from version fields |
| 1837 | // and 5 more fields with different kinds of offsets. |
| 1838 | CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1839 | SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize; |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1840 | |
| 1841 | ConstantPoolOffset = |
| 1842 | SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize; |
| 1843 | |
| 1844 | for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) { |
| 1845 | CuVectorsOffset.push_back(CuVectorsSize); |
| 1846 | CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1); |
| 1847 | } |
| 1848 | StringPoolOffset = ConstantPoolOffset + CuVectorsSize; |
| 1849 | |
| 1850 | StringPool.finalizeInOrder(); |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | template <class ELFT> size_t GdbIndexSection<ELFT>::getSize() const { |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1854 | const_cast<GdbIndexSection<ELFT> *>(this)->finalizeContents(); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1855 | return StringPoolOffset + StringPool.getSize(); |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) { |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1859 | write32le(Buf, 7); // Write version. |
| 1860 | write32le(Buf + 4, CuListOffset); // CU list offset. |
| 1861 | write32le(Buf + 8, CuTypesOffset); // Types CU list offset. |
| 1862 | write32le(Buf + 12, CuTypesOffset); // Address area offset. |
| 1863 | write32le(Buf + 16, SymTabOffset); // Symbol table offset. |
| 1864 | write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset. |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1865 | Buf += 24; |
| 1866 | |
| 1867 | // Write the CU list. |
| 1868 | for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) { |
| 1869 | write64le(Buf, CU.first); |
| 1870 | write64le(Buf + 8, CU.second); |
| 1871 | Buf += 16; |
| 1872 | } |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Write the address area. |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1875 | for (AddressEntry &E : AddressArea) { |
Rafael Espindola | e129409 | 2017-03-08 16:03:41 +0000 | [diff] [blame] | 1876 | uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0); |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1877 | write64le(Buf, BaseAddr + E.LowAddress); |
| 1878 | write64le(Buf + 8, BaseAddr + E.HighAddress); |
| 1879 | write32le(Buf + 16, E.CuIndex); |
| 1880 | Buf += 20; |
| 1881 | } |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Write the symbol table. |
| 1884 | for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) { |
| 1885 | GdbSymbol *Sym = SymbolTable.getSymbol(I); |
| 1886 | if (Sym) { |
| 1887 | size_t NameOffset = |
| 1888 | Sym->NameOffset + StringPoolOffset - ConstantPoolOffset; |
| 1889 | size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex]; |
| 1890 | write32le(Buf, NameOffset); |
| 1891 | write32le(Buf + 4, CuVectorOffset); |
| 1892 | } |
| 1893 | Buf += 8; |
| 1894 | } |
| 1895 | |
| 1896 | // Write the CU vectors into the constant pool. |
| 1897 | for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) { |
| 1898 | write32le(Buf, CuVec.size()); |
| 1899 | Buf += 4; |
| 1900 | for (std::pair<uint32_t, uint8_t> &P : CuVec) { |
| 1901 | uint32_t Index = P.first; |
| 1902 | uint8_t Flags = P.second; |
| 1903 | Index |= Flags << 24; |
| 1904 | write32le(Buf, Index); |
| 1905 | Buf += 4; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | StringPool.write(Buf); |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 1912 | template <class ELFT> bool GdbIndexSection<ELFT>::empty() const { |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1913 | return !Out::DebugInfo; |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1916 | template <class ELFT> |
| 1917 | EhFrameHeader<ELFT>::EhFrameHeader() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1918 | : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {} |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1919 | |
| 1920 | // .eh_frame_hdr contains a binary search table of pointers to FDEs. |
| 1921 | // Each entry of the search table consists of two values, |
| 1922 | // the starting PC from where FDEs covers, and the FDE's address. |
| 1923 | // It is sorted by PC. |
| 1924 | template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { |
| 1925 | const endianness E = ELFT::TargetEndianness; |
| 1926 | |
| 1927 | // Sort the FDE list by their PC and uniqueify. Usually there is only |
| 1928 | // one FDE for a PC (i.e. function), but if ICF merges two functions |
| 1929 | // into one, there can be more than one FDEs pointing to the address. |
| 1930 | auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; |
| 1931 | std::stable_sort(Fdes.begin(), Fdes.end(), Less); |
| 1932 | auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; |
| 1933 | Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); |
| 1934 | |
| 1935 | Buf[0] = 1; |
| 1936 | Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 1937 | Buf[2] = DW_EH_PE_udata4; |
| 1938 | Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 1939 | write32<E>(Buf + 4, In<ELFT>::EhFrame->OutSec->Addr - this->getVA() - 4); |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1940 | write32<E>(Buf + 8, Fdes.size()); |
| 1941 | Buf += 12; |
| 1942 | |
| 1943 | uintX_t VA = this->getVA(); |
| 1944 | for (FdeData &Fde : Fdes) { |
| 1945 | write32<E>(Buf, Fde.Pc - VA); |
| 1946 | write32<E>(Buf + 4, Fde.FdeVA - VA); |
| 1947 | Buf += 8; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const { |
| 1952 | // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 1953 | return 12 + In<ELFT>::EhFrame->NumFdes * 8; |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1956 | template <class ELFT> |
Rui Ueyama | b38ddb1 | 2016-11-21 19:46:04 +0000 | [diff] [blame] | 1957 | void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { |
| 1958 | Fdes.push_back({Pc, FdeVA}); |
| 1959 | } |
| 1960 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 1961 | template <class ELFT> bool EhFrameHeader<ELFT>::empty() const { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 1962 | return In<ELFT>::EhFrame->empty(); |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Rui Ueyama | b38ddb1 | 2016-11-21 19:46:04 +0000 | [diff] [blame] | 1965 | template <class ELFT> |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1966 | VersionDefinitionSection<ELFT>::VersionDefinitionSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1967 | : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), |
| 1968 | ".gnu.version_d") {} |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1969 | |
| 1970 | static StringRef getFileDefName() { |
| 1971 | if (!Config->SoName.empty()) |
| 1972 | return Config->SoName; |
| 1973 | return Config->OutputFile; |
| 1974 | } |
| 1975 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 1976 | template <class ELFT> void VersionDefinitionSection<ELFT>::finalizeContents() { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1977 | FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName()); |
| 1978 | for (VersionDefinition &V : Config->VersionDefinitions) |
| 1979 | V.NameOff = In<ELFT>::DynStrTab->addString(V.Name); |
| 1980 | |
Rui Ueyama | c3726f8 | 2017-02-28 04:41:20 +0000 | [diff] [blame] | 1981 | this->OutSec->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1982 | |
| 1983 | // sh_info should be set to the number of definitions. This fact is missed in |
| 1984 | // documentation, but confirmed by binutils community: |
| 1985 | // https://sourceware.org/ml/binutils/2014-11/msg00355.html |
Rui Ueyama | c3726f8 | 2017-02-28 04:41:20 +0000 | [diff] [blame] | 1986 | this->OutSec->Info = getVerDefNum(); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | template <class ELFT> |
| 1990 | void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, |
| 1991 | StringRef Name, size_t NameOff) { |
| 1992 | auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
| 1993 | Verdef->vd_version = 1; |
| 1994 | Verdef->vd_cnt = 1; |
| 1995 | Verdef->vd_aux = sizeof(Elf_Verdef); |
| 1996 | Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1997 | Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); |
| 1998 | Verdef->vd_ndx = Index; |
| 1999 | Verdef->vd_hash = hashSysV(Name); |
| 2000 | |
| 2001 | auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); |
| 2002 | Verdaux->vda_name = NameOff; |
| 2003 | Verdaux->vda_next = 0; |
| 2004 | } |
| 2005 | |
| 2006 | template <class ELFT> |
| 2007 | void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2008 | writeOne(Buf, 1, getFileDefName(), FileDefNameOff); |
| 2009 | |
| 2010 | for (VersionDefinition &V : Config->VersionDefinitions) { |
| 2011 | Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 2012 | writeOne(Buf, V.Id, V.Name, V.NameOff); |
| 2013 | } |
| 2014 | |
| 2015 | // Need to terminate the last version definition. |
| 2016 | Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
| 2017 | Verdef->vd_next = 0; |
| 2018 | } |
| 2019 | |
| 2020 | template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const { |
| 2021 | return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); |
| 2022 | } |
| 2023 | |
| 2024 | template <class ELFT> |
| 2025 | VersionTableSection<ELFT>::VersionTableSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2026 | : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 2027 | ".gnu.version") { |
| 2028 | this->Entsize = sizeof(Elf_Versym); |
| 2029 | } |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2030 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 2031 | template <class ELFT> void VersionTableSection<ELFT>::finalizeContents() { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2032 | // At the moment of june 2016 GNU docs does not mention that sh_link field |
| 2033 | // should be set, but Sun docs do. Also readelf relies on this field. |
Rui Ueyama | c3726f8 | 2017-02-28 04:41:20 +0000 | [diff] [blame] | 2034 | this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const { |
| 2038 | return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1); |
| 2039 | } |
| 2040 | |
| 2041 | template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2042 | auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; |
| 2043 | for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { |
| 2044 | OutVersym->vs_index = S.Symbol->symbol()->VersionId; |
| 2045 | ++OutVersym; |
| 2046 | } |
| 2047 | } |
| 2048 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 2049 | template <class ELFT> bool VersionTableSection<ELFT>::empty() const { |
| 2050 | return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty(); |
| 2051 | } |
| 2052 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2053 | template <class ELFT> |
| 2054 | VersionNeedSection<ELFT>::VersionNeedSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2055 | : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), |
| 2056 | ".gnu.version_r") { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2057 | // Identifiers in verneed section start at 2 because 0 and 1 are reserved |
| 2058 | // for VER_NDX_LOCAL and VER_NDX_GLOBAL. |
| 2059 | // First identifiers are reserved by verdef section if it exist. |
| 2060 | NextIndex = getVerDefNum() + 1; |
| 2061 | } |
| 2062 | |
| 2063 | template <class ELFT> |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 2064 | void VersionNeedSection<ELFT>::addSymbol(SharedSymbol *SS) { |
| 2065 | auto *Ver = reinterpret_cast<const typename ELFT::Verdef *>(SS->Verdef); |
| 2066 | if (!Ver) { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2067 | SS->symbol()->VersionId = VER_NDX_GLOBAL; |
| 2068 | return; |
| 2069 | } |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 2070 | |
| 2071 | auto *File = cast<SharedFile<ELFT>>(SS->File); |
| 2072 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2073 | // If we don't already know that we need an Elf_Verneed for this DSO, prepare |
| 2074 | // to create one by adding it to our needed list and creating a dynstr entry |
| 2075 | // for the soname. |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 2076 | if (File->VerdefMap.empty()) |
| 2077 | Needed.push_back({File, In<ELFT>::DynStrTab->addString(File->getSoName())}); |
| 2078 | typename SharedFile<ELFT>::NeededVer &NV = File->VerdefMap[Ver]; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2079 | // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, |
| 2080 | // prepare to create one by allocating a version identifier and creating a |
| 2081 | // dynstr entry for the version name. |
| 2082 | if (NV.Index == 0) { |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 2083 | NV.StrTab = In<ELFT>::DynStrTab->addString(File->getStringTable().data() + |
| 2084 | Ver->getAux()->vda_name); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2085 | NV.Index = NextIndex++; |
| 2086 | } |
| 2087 | SS->symbol()->VersionId = NV.Index; |
| 2088 | } |
| 2089 | |
| 2090 | template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2091 | // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. |
| 2092 | auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); |
| 2093 | auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); |
| 2094 | |
| 2095 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { |
| 2096 | // Create an Elf_Verneed for this DSO. |
| 2097 | Verneed->vn_version = 1; |
| 2098 | Verneed->vn_cnt = P.first->VerdefMap.size(); |
| 2099 | Verneed->vn_file = P.second; |
| 2100 | Verneed->vn_aux = |
| 2101 | reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); |
| 2102 | Verneed->vn_next = sizeof(Elf_Verneed); |
| 2103 | ++Verneed; |
| 2104 | |
| 2105 | // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over |
| 2106 | // VerdefMap, which will only contain references to needed version |
| 2107 | // definitions. Each Elf_Vernaux is based on the information contained in |
| 2108 | // the Elf_Verdef in the source DSO. This loop iterates over a std::map of |
| 2109 | // pointers, but is deterministic because the pointers refer to Elf_Verdef |
| 2110 | // data structures within a single input file. |
| 2111 | for (auto &NV : P.first->VerdefMap) { |
| 2112 | Vernaux->vna_hash = NV.first->vd_hash; |
| 2113 | Vernaux->vna_flags = 0; |
| 2114 | Vernaux->vna_other = NV.second.Index; |
| 2115 | Vernaux->vna_name = NV.second.StrTab; |
| 2116 | Vernaux->vna_next = sizeof(Elf_Vernaux); |
| 2117 | ++Vernaux; |
| 2118 | } |
| 2119 | |
| 2120 | Vernaux[-1].vna_next = 0; |
| 2121 | } |
| 2122 | Verneed[-1].vn_next = 0; |
| 2123 | } |
| 2124 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 2125 | template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() { |
Rui Ueyama | c3726f8 | 2017-02-28 04:41:20 +0000 | [diff] [blame] | 2126 | this->OutSec->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
| 2127 | this->OutSec->Info = Needed.size(); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { |
| 2131 | unsigned Size = Needed.size() * sizeof(Elf_Verneed); |
| 2132 | for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed) |
| 2133 | Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); |
| 2134 | return Size; |
| 2135 | } |
| 2136 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 2137 | template <class ELFT> bool VersionNeedSection<ELFT>::empty() const { |
| 2138 | return getNeedNum() == 0; |
| 2139 | } |
| 2140 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2141 | MergeSyntheticSection::MergeSyntheticSection(StringRef Name, uint32_t Type, |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 2142 | uint64_t Flags, uint32_t Alignment) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2143 | : SyntheticSection(Flags, Type, Alignment, Name), |
Rafael Espindola | 2532431 | 2017-02-03 21:29:51 +0000 | [diff] [blame] | 2144 | Builder(StringTableBuilder::RAW, Alignment) {} |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2145 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2146 | void MergeSyntheticSection::addSection(MergeInputSection *MS) { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2147 | assert(!Finalized); |
| 2148 | MS->MergeSec = this; |
| 2149 | Sections.push_back(MS); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2152 | void MergeSyntheticSection::writeTo(uint8_t *Buf) { Builder.write(Buf); } |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2153 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2154 | bool MergeSyntheticSection::shouldTailMerge() const { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2155 | return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2; |
| 2156 | } |
| 2157 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2158 | void MergeSyntheticSection::finalizeTailMerge() { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2159 | // Add all string pieces to the string table builder to create section |
| 2160 | // contents. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2161 | for (MergeInputSection *Sec : Sections) |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2162 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 2163 | if (Sec->Pieces[I].Live) |
| 2164 | Builder.add(Sec->getData(I)); |
| 2165 | |
| 2166 | // Fix the string table content. After this, the contents will never change. |
| 2167 | Builder.finalize(); |
| 2168 | |
| 2169 | // finalize() fixed tail-optimized strings, so we can now get |
| 2170 | // offsets of strings. Get an offset for each string and save it |
| 2171 | // to a corresponding StringPiece for easy access. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2172 | for (MergeInputSection *Sec : Sections) |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2173 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 2174 | if (Sec->Pieces[I].Live) |
| 2175 | Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); |
| 2176 | } |
| 2177 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2178 | void MergeSyntheticSection::finalizeNoTailMerge() { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2179 | // Add all string pieces to the string table builder to create section |
| 2180 | // contents. Because we are not tail-optimizing, offsets of strings are |
| 2181 | // fixed when they are added to the builder (string table builder contains |
| 2182 | // a hash table from strings to offsets). |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2183 | for (MergeInputSection *Sec : Sections) |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2184 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 2185 | if (Sec->Pieces[I].Live) |
| 2186 | Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I)); |
| 2187 | |
| 2188 | Builder.finalizeInOrder(); |
| 2189 | } |
| 2190 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2191 | void MergeSyntheticSection::finalizeContents() { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2192 | if (Finalized) |
| 2193 | return; |
| 2194 | Finalized = true; |
| 2195 | if (shouldTailMerge()) |
| 2196 | finalizeTailMerge(); |
| 2197 | else |
| 2198 | finalizeNoTailMerge(); |
| 2199 | } |
| 2200 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2201 | size_t MergeSyntheticSection::getSize() const { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2202 | // We should finalize string builder to know the size. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2203 | const_cast<MergeSyntheticSection *>(this)->finalizeContents(); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2204 | return Builder.getSize(); |
| 2205 | } |
| 2206 | |
| 2207 | template <class ELFT> |
Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 2208 | MipsRldMapSection<ELFT>::MipsRldMapSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2209 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 2210 | sizeof(typename ELFT::uint), ".rld_map") {} |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 2211 | |
Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 2212 | template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) { |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 2213 | // Apply filler from linker script. |
| 2214 | uint64_t Filler = Script<ELFT>::X->getFiller(this->Name); |
| 2215 | Filler = (Filler << 32) | Filler; |
| 2216 | memcpy(Buf, &Filler, getSize()); |
| 2217 | } |
| 2218 | |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2219 | template <class ELFT> |
| 2220 | ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2221 | : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, |
| 2222 | sizeof(typename ELFT::uint), ".ARM.exidx") {} |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2223 | |
| 2224 | // Write a terminating sentinel entry to the end of the .ARM.exidx table. |
| 2225 | // This section will have been sorted last in the .ARM.exidx table. |
| 2226 | // This table entry will have the form: |
| 2227 | // | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND | |
George Rimar | 879a657 | 2016-12-15 15:38:58 +0000 | [diff] [blame] | 2228 | template <class ELFT> |
| 2229 | void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf) { |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2230 | // Get the InputSection before us, we are by definition last |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 2231 | auto RI = cast<OutputSection>(this->OutSec)->Sections.rbegin(); |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 2232 | InputSection *LE = *(++RI); |
| 2233 | InputSection *LC = cast<InputSection>(LE->template getLinkOrderDep<ELFT>()); |
Rafael Espindola | e129409 | 2017-03-08 16:03:41 +0000 | [diff] [blame] | 2234 | uint64_t S = LC->OutSec->Addr + LC->getOffset(LC->getSize()); |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2235 | uint64_t P = this->getVA(); |
| 2236 | Target->relocateOne(Buf, R_ARM_PREL31, S - P); |
| 2237 | write32le(Buf + 4, 0x1); |
| 2238 | } |
| 2239 | |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2240 | template <class ELFT> |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 2241 | ThunkSection<ELFT>::ThunkSection(OutputSection *OS, uint64_t Off) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2242 | : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, |
| 2243 | sizeof(typename ELFT::uint), ".text.thunk") { |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2244 | this->OutSec = OS; |
| 2245 | this->OutSecOff = Off; |
| 2246 | } |
| 2247 | |
| 2248 | template <class ELFT> void ThunkSection<ELFT>::addThunk(Thunk<ELFT> *T) { |
| 2249 | uint64_t Off = alignTo(Size, T->alignment); |
| 2250 | T->Offset = Off; |
| 2251 | Thunks.push_back(T); |
| 2252 | T->addSymbols(*this); |
| 2253 | Size = Off + T->size(); |
| 2254 | } |
| 2255 | |
| 2256 | template <class ELFT> void ThunkSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2257 | for (const Thunk<ELFT> *T : Thunks) |
| 2258 | T->writeTo(Buf + T->Offset, *this); |
| 2259 | } |
| 2260 | |
| 2261 | template <class ELFT> |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 2262 | InputSection *ThunkSection<ELFT>::getTargetInputSection() const { |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2263 | const Thunk<ELFT> *T = Thunks.front(); |
| 2264 | return T->getTargetInputSection(); |
| 2265 | } |
| 2266 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 2267 | template InputSection *elf::createCommonSection<ELF32LE>(); |
| 2268 | template InputSection *elf::createCommonSection<ELF32BE>(); |
| 2269 | template InputSection *elf::createCommonSection<ELF64LE>(); |
| 2270 | template InputSection *elf::createCommonSection<ELF64BE>(); |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 2271 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2272 | template MergeInputSection *elf::createCommentSection<ELF32LE>(); |
| 2273 | template MergeInputSection *elf::createCommentSection<ELF32BE>(); |
| 2274 | template MergeInputSection *elf::createCommentSection<ELF64LE>(); |
| 2275 | template MergeInputSection *elf::createCommentSection<ELF64BE>(); |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 2276 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 2277 | template SymbolBody *elf::addSyntheticLocal<ELF32LE>(StringRef, uint8_t, |
Rui Ueyama | 65316d7 | 2017-02-23 03:15:57 +0000 | [diff] [blame] | 2278 | uint64_t, uint64_t, |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 2279 | InputSectionBase *); |
| 2280 | template SymbolBody *elf::addSyntheticLocal<ELF32BE>(StringRef, uint8_t, |
Rui Ueyama | 65316d7 | 2017-02-23 03:15:57 +0000 | [diff] [blame] | 2281 | uint64_t, uint64_t, |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 2282 | InputSectionBase *); |
| 2283 | template SymbolBody *elf::addSyntheticLocal<ELF64LE>(StringRef, uint8_t, |
Rui Ueyama | 65316d7 | 2017-02-23 03:15:57 +0000 | [diff] [blame] | 2284 | uint64_t, uint64_t, |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 2285 | InputSectionBase *); |
| 2286 | template SymbolBody *elf::addSyntheticLocal<ELF64BE>(StringRef, uint8_t, |
Rui Ueyama | 65316d7 | 2017-02-23 03:15:57 +0000 | [diff] [blame] | 2287 | uint64_t, uint64_t, |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 2288 | InputSectionBase *); |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 2289 | |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 2290 | template class elf::MipsAbiFlagsSection<ELF32LE>; |
| 2291 | template class elf::MipsAbiFlagsSection<ELF32BE>; |
| 2292 | template class elf::MipsAbiFlagsSection<ELF64LE>; |
| 2293 | template class elf::MipsAbiFlagsSection<ELF64BE>; |
| 2294 | |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 2295 | template class elf::MipsOptionsSection<ELF32LE>; |
| 2296 | template class elf::MipsOptionsSection<ELF32BE>; |
| 2297 | template class elf::MipsOptionsSection<ELF64LE>; |
| 2298 | template class elf::MipsOptionsSection<ELF64BE>; |
| 2299 | |
| 2300 | template class elf::MipsReginfoSection<ELF32LE>; |
| 2301 | template class elf::MipsReginfoSection<ELF32BE>; |
| 2302 | template class elf::MipsReginfoSection<ELF64LE>; |
| 2303 | template class elf::MipsReginfoSection<ELF64BE>; |
| 2304 | |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 2305 | template class elf::BuildIdSection<ELF32LE>; |
| 2306 | template class elf::BuildIdSection<ELF32BE>; |
| 2307 | template class elf::BuildIdSection<ELF64LE>; |
| 2308 | template class elf::BuildIdSection<ELF64BE>; |
| 2309 | |
Rui Ueyama | 007c002 | 2017-03-08 17:24:24 +0000 | [diff] [blame] | 2310 | template class elf::CopyRelSection<ELF32LE>; |
| 2311 | template class elf::CopyRelSection<ELF32BE>; |
| 2312 | template class elf::CopyRelSection<ELF64LE>; |
| 2313 | template class elf::CopyRelSection<ELF64BE>; |
Peter Smith | ebfe994 | 2017-02-09 10:27:57 +0000 | [diff] [blame] | 2314 | |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 2315 | template class elf::GotSection<ELF32LE>; |
| 2316 | template class elf::GotSection<ELF32BE>; |
| 2317 | template class elf::GotSection<ELF64LE>; |
| 2318 | template class elf::GotSection<ELF64BE>; |
| 2319 | |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 2320 | template class elf::MipsGotSection<ELF32LE>; |
| 2321 | template class elf::MipsGotSection<ELF32BE>; |
| 2322 | template class elf::MipsGotSection<ELF64LE>; |
| 2323 | template class elf::MipsGotSection<ELF64BE>; |
| 2324 | |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 2325 | template class elf::GotPltSection<ELF32LE>; |
| 2326 | template class elf::GotPltSection<ELF32BE>; |
| 2327 | template class elf::GotPltSection<ELF64LE>; |
| 2328 | template class elf::GotPltSection<ELF64BE>; |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 2329 | |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 2330 | template class elf::IgotPltSection<ELF32LE>; |
| 2331 | template class elf::IgotPltSection<ELF32BE>; |
| 2332 | template class elf::IgotPltSection<ELF64LE>; |
| 2333 | template class elf::IgotPltSection<ELF64BE>; |
| 2334 | |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 2335 | template class elf::StringTableSection<ELF32LE>; |
| 2336 | template class elf::StringTableSection<ELF32BE>; |
| 2337 | template class elf::StringTableSection<ELF64LE>; |
| 2338 | template class elf::StringTableSection<ELF64BE>; |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 2339 | |
| 2340 | template class elf::DynamicSection<ELF32LE>; |
| 2341 | template class elf::DynamicSection<ELF32BE>; |
| 2342 | template class elf::DynamicSection<ELF64LE>; |
| 2343 | template class elf::DynamicSection<ELF64BE>; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 2344 | |
| 2345 | template class elf::RelocationSection<ELF32LE>; |
| 2346 | template class elf::RelocationSection<ELF32BE>; |
| 2347 | template class elf::RelocationSection<ELF64LE>; |
| 2348 | template class elf::RelocationSection<ELF64BE>; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 2349 | |
| 2350 | template class elf::SymbolTableSection<ELF32LE>; |
| 2351 | template class elf::SymbolTableSection<ELF32BE>; |
| 2352 | template class elf::SymbolTableSection<ELF64LE>; |
| 2353 | template class elf::SymbolTableSection<ELF64BE>; |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 2354 | |
| 2355 | template class elf::GnuHashTableSection<ELF32LE>; |
| 2356 | template class elf::GnuHashTableSection<ELF32BE>; |
| 2357 | template class elf::GnuHashTableSection<ELF64LE>; |
| 2358 | template class elf::GnuHashTableSection<ELF64BE>; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 2359 | |
| 2360 | template class elf::HashTableSection<ELF32LE>; |
| 2361 | template class elf::HashTableSection<ELF32BE>; |
| 2362 | template class elf::HashTableSection<ELF64LE>; |
| 2363 | template class elf::HashTableSection<ELF64BE>; |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 2364 | |
| 2365 | template class elf::PltSection<ELF32LE>; |
| 2366 | template class elf::PltSection<ELF32BE>; |
| 2367 | template class elf::PltSection<ELF64LE>; |
| 2368 | template class elf::PltSection<ELF64BE>; |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2369 | |
| 2370 | template class elf::GdbIndexSection<ELF32LE>; |
| 2371 | template class elf::GdbIndexSection<ELF32BE>; |
| 2372 | template class elf::GdbIndexSection<ELF64LE>; |
| 2373 | template class elf::GdbIndexSection<ELF64BE>; |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2374 | |
| 2375 | template class elf::EhFrameHeader<ELF32LE>; |
| 2376 | template class elf::EhFrameHeader<ELF32BE>; |
| 2377 | template class elf::EhFrameHeader<ELF64LE>; |
| 2378 | template class elf::EhFrameHeader<ELF64BE>; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2379 | |
| 2380 | template class elf::VersionTableSection<ELF32LE>; |
| 2381 | template class elf::VersionTableSection<ELF32BE>; |
| 2382 | template class elf::VersionTableSection<ELF64LE>; |
| 2383 | template class elf::VersionTableSection<ELF64BE>; |
| 2384 | |
| 2385 | template class elf::VersionNeedSection<ELF32LE>; |
| 2386 | template class elf::VersionNeedSection<ELF32BE>; |
| 2387 | template class elf::VersionNeedSection<ELF64LE>; |
| 2388 | template class elf::VersionNeedSection<ELF64BE>; |
| 2389 | |
| 2390 | template class elf::VersionDefinitionSection<ELF32LE>; |
| 2391 | template class elf::VersionDefinitionSection<ELF32BE>; |
| 2392 | template class elf::VersionDefinitionSection<ELF64LE>; |
| 2393 | template class elf::VersionDefinitionSection<ELF64BE>; |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 2394 | |
Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 2395 | template class elf::MipsRldMapSection<ELF32LE>; |
| 2396 | template class elf::MipsRldMapSection<ELF32BE>; |
| 2397 | template class elf::MipsRldMapSection<ELF64LE>; |
| 2398 | template class elf::MipsRldMapSection<ELF64BE>; |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2399 | |
| 2400 | template class elf::ARMExidxSentinelSection<ELF32LE>; |
| 2401 | template class elf::ARMExidxSentinelSection<ELF32BE>; |
| 2402 | template class elf::ARMExidxSentinelSection<ELF64LE>; |
| 2403 | template class elf::ARMExidxSentinelSection<ELF64BE>; |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2404 | |
| 2405 | template class elf::ThunkSection<ELF32LE>; |
| 2406 | template class elf::ThunkSection<ELF32BE>; |
| 2407 | template class elf::ThunkSection<ELF64LE>; |
| 2408 | template class elf::ThunkSection<ELF64BE>; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 2409 | |
| 2410 | template class elf::EhFrameSection<ELF32LE>; |
| 2411 | template class elf::EhFrameSection<ELF32BE>; |
| 2412 | template class elf::EhFrameSection<ELF64LE>; |
| 2413 | template class elf::EhFrameSection<ELF64BE>; |