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