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