| 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" |
| Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Dwarf.h" |
| Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Endian.h" |
| 32 | #include "llvm/Support/MD5.h" |
| 33 | #include "llvm/Support/RandomNumberGenerator.h" |
| 34 | #include "llvm/Support/SHA1.h" |
| 35 | #include "llvm/Support/xxhash.h" |
| Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 36 | #include <cstdlib> |
| Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 39 | using namespace llvm::dwarf; |
| Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 40 | using namespace llvm::ELF; |
| 41 | using namespace llvm::object; |
| 42 | using namespace llvm::support; |
| 43 | using namespace llvm::support::endian; |
| 44 | |
| 45 | using namespace lld; |
| 46 | using namespace lld::elf; |
| 47 | |
| Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 48 | template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() { |
| 49 | std::vector<DefinedCommon *> V; |
| 50 | for (Symbol *S : Symtab<ELFT>::X->getSymbols()) |
| 51 | if (auto *B = dyn_cast<DefinedCommon>(S->body())) |
| 52 | V.push_back(B); |
| 53 | return V; |
| 54 | } |
| 55 | |
| 56 | // Find all common symbols and allocate space for them. |
| Rafael Espindola | 682a5bc | 2016-11-08 14:42:34 +0000 | [diff] [blame] | 57 | template <class ELFT> InputSection<ELFT> *elf::createCommonSection() { |
| 58 | auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1, |
| 59 | ArrayRef<uint8_t>(), "COMMON"); |
| 60 | Ret->Live = true; |
| Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 61 | |
| Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 62 | if (!Config->DefineCommon) |
| 63 | return Ret; |
| 64 | |
| Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 65 | // Sort the common symbols by alignment as an heuristic to pack them better. |
| 66 | std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>(); |
| 67 | std::stable_sort(Syms.begin(), Syms.end(), |
| 68 | [](const DefinedCommon *A, const DefinedCommon *B) { |
| 69 | return A->Alignment > B->Alignment; |
| 70 | }); |
| 71 | |
| 72 | // Assign offsets to symbols. |
| 73 | size_t Size = 0; |
| 74 | size_t Alignment = 1; |
| 75 | for (DefinedCommon *Sym : Syms) { |
| Rui Ueyama | 1c78682 | 2016-11-05 23:14:54 +0000 | [diff] [blame] | 76 | Alignment = std::max<size_t>(Alignment, Sym->Alignment); |
| Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 77 | Size = alignTo(Size, Sym->Alignment); |
| 78 | |
| 79 | // Compute symbol offset relative to beginning of input section. |
| 80 | Sym->Offset = Size; |
| 81 | Size += Sym->Size; |
| 82 | } |
| Rafael Espindola | 682a5bc | 2016-11-08 14:42:34 +0000 | [diff] [blame] | 83 | Ret->Alignment = Alignment; |
| 84 | Ret->Data = makeArrayRef<uint8_t>(nullptr, Size); |
| 85 | return Ret; |
| Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 88 | // Returns an LLD version string. |
| 89 | static ArrayRef<uint8_t> getVersion() { |
| 90 | // Check LLD_VERSION first for ease of testing. |
| 91 | // You can get consitent output by using the environment variable. |
| 92 | // This is only for testing. |
| 93 | StringRef S = getenv("LLD_VERSION"); |
| 94 | if (S.empty()) |
| 95 | S = Saver.save(Twine("Linker: ") + getLLDVersion()); |
| 96 | |
| 97 | // +1 to include the terminating '\0'. |
| 98 | return {(const uint8_t *)S.data(), S.size() + 1}; |
| Davide Italiano | b69f38f | 2016-11-11 00:05:41 +0000 | [diff] [blame] | 99 | } |
| Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 100 | |
| 101 | // Creates a .comment section containing LLD version info. |
| 102 | // With this feature, you can identify LLD-generated binaries easily |
| 103 | // by "objdump -s -j .comment <file>". |
| 104 | // The returned object is a mergeable string section. |
| 105 | template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() { |
| 106 | typename ELFT::Shdr Hdr = {}; |
| 107 | Hdr.sh_flags = SHF_MERGE | SHF_STRINGS; |
| 108 | Hdr.sh_type = SHT_PROGBITS; |
| 109 | Hdr.sh_entsize = 1; |
| 110 | Hdr.sh_addralign = 1; |
| 111 | |
| 112 | auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment"); |
| 113 | Ret->Data = getVersion(); |
| 114 | Ret->splitIntoPieces(); |
| 115 | return Ret; |
| 116 | } |
| 117 | |
| Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 118 | // .MIPS.abiflags section. |
| 119 | template <class ELFT> |
| Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 120 | MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags) |
| 121 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), |
| 122 | Flags(Flags) {} |
| 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 | |
| 133 | for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) { |
| 134 | if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS) |
| 135 | continue; |
| 136 | Sec->Live = false; |
| 137 | Create = true; |
| 138 | |
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 139 | std::string Filename = toString(Sec->getFile()); |
| 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) |
| 178 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), |
| 179 | Reginfo(Reginfo) {} |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 180 | |
| Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 181 | template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) { |
| 182 | auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf); |
| 183 | Options->kind = ODK_REGINFO; |
| 184 | Options->size = getSize(); |
| 185 | |
| 186 | if (!Config->Relocatable) |
| Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 187 | Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp(); |
| Rafael Espindola | 4862ae8 | 2016-11-24 16:38:35 +0000 | [diff] [blame] | 188 | memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo)); |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 191 | template <class ELFT> |
| 192 | MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() { |
| 193 | // N64 ABI only. |
| 194 | if (!ELFT::Is64Bits) |
| 195 | return nullptr; |
| 196 | |
| 197 | Elf_Mips_RegInfo Reginfo = {}; |
| 198 | bool Create = false; |
| 199 | |
| 200 | for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) { |
| 201 | if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS) |
| 202 | continue; |
| 203 | Sec->Live = false; |
| 204 | Create = true; |
| 205 | |
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 206 | std::string Filename = toString(Sec->getFile()); |
| Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 207 | ArrayRef<uint8_t> D = Sec->Data; |
| 208 | |
| 209 | while (!D.empty()) { |
| 210 | if (D.size() < sizeof(Elf_Mips_Options)) { |
| 211 | error(Filename + ": invalid size of .MIPS.options section"); |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data()); |
| 216 | if (Opt->kind == ODK_REGINFO) { |
| 217 | if (Config->Relocatable && Opt->getRegInfo().ri_gp_value) |
| 218 | error(Filename + ": unsupported non-zero ri_gp_value"); |
| 219 | Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask; |
| 220 | Sec->getFile()->MipsGp0 = Opt->getRegInfo().ri_gp_value; |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | if (!Opt->size) |
| 225 | fatal(Filename + ": zero option descriptor size"); |
| 226 | D = D.slice(Opt->size); |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | if (Create) |
| Rui Ueyama | 3cc93d7 | 2016-11-22 23:13:08 +0000 | [diff] [blame] | 231 | return make<MipsOptionsSection<ELFT>>(Reginfo); |
| Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 232 | return nullptr; |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // MIPS .reginfo section. |
| 236 | template <class ELFT> |
| Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 237 | MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo) |
| 238 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), |
| 239 | Reginfo(Reginfo) {} |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 240 | |
| Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 241 | template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) { |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 242 | if (!Config->Relocatable) |
| Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 243 | Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp(); |
| Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 244 | memcpy(Buf, &Reginfo, sizeof(Reginfo)); |
| 245 | } |
| 246 | |
| 247 | template <class ELFT> |
| 248 | MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() { |
| 249 | // Section should be alive for O32 and N32 ABIs only. |
| 250 | if (ELFT::Is64Bits) |
| 251 | return nullptr; |
| 252 | |
| 253 | Elf_Mips_RegInfo Reginfo = {}; |
| 254 | bool Create = false; |
| 255 | |
| 256 | for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) { |
| 257 | if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO) |
| 258 | continue; |
| 259 | Sec->Live = false; |
| 260 | Create = true; |
| 261 | |
| 262 | if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) { |
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 263 | error(toString(Sec->getFile()) + ": invalid size of .reginfo section"); |
| Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 264 | return nullptr; |
| 265 | } |
| 266 | auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data()); |
| 267 | if (Config->Relocatable && R->ri_gp_value) |
| Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 268 | error(toString(Sec->getFile()) + ": unsupported non-zero ri_gp_value"); |
| Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 269 | |
| 270 | Reginfo.ri_gprmask |= R->ri_gprmask; |
| 271 | Sec->getFile()->MipsGp0 = R->ri_gp_value; |
| 272 | }; |
| 273 | |
| 274 | if (Create) |
| 275 | return make<MipsReginfoSection<ELFT>>(Reginfo); |
| 276 | return nullptr; |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| Rafael Espindola | c0e47fb | 2016-11-08 14:56:27 +0000 | [diff] [blame] | 279 | template <class ELFT> InputSection<ELFT> *elf::createInterpSection() { |
| 280 | auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC, SHT_PROGBITS, 1, |
| Rui Ueyama | 81a4b26 | 2016-11-22 04:33:01 +0000 | [diff] [blame] | 281 | ArrayRef<uint8_t>(), ".interp"); |
| Rafael Espindola | c0e47fb | 2016-11-08 14:56:27 +0000 | [diff] [blame] | 282 | Ret->Live = true; |
| Rui Ueyama | 81a4b26 | 2016-11-22 04:33:01 +0000 | [diff] [blame] | 283 | |
| 284 | // StringSaver guarantees that the returned string ends with '\0'. |
| 285 | StringRef S = Saver.save(Config->DynamicLinker); |
| 286 | Ret->Data = {(const uint8_t *)S.data(), S.size() + 1}; |
| Rafael Espindola | c0e47fb | 2016-11-08 14:56:27 +0000 | [diff] [blame] | 287 | return Ret; |
| Rui Ueyama | a9ee8d6 | 2016-11-04 22:25:39 +0000 | [diff] [blame] | 288 | } |
| Rui Ueyama | e288eef | 2016-11-02 18:58:44 +0000 | [diff] [blame] | 289 | |
| Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame^] | 290 | template <class ELFT> |
| 291 | SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, |
| 292 | typename ELFT::uint Value, |
| 293 | typename ELFT::uint Size, |
| 294 | InputSectionBase<ELFT> *Section) { |
| 295 | auto *S = make<DefinedRegular<ELFT>>(Name, /*IsLocal*/ true, STV_DEFAULT, |
| 296 | Type, Value, Size, Section, nullptr); |
| 297 | if (In<ELFT>::SymTab) |
| 298 | In<ELFT>::SymTab->addLocal(S); |
| 299 | return S; |
| 300 | } |
| 301 | |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 302 | static size_t getHashSize() { |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 303 | switch (Config->BuildId) { |
| 304 | case BuildIdKind::Fast: |
| 305 | return 8; |
| 306 | case BuildIdKind::Md5: |
| 307 | case BuildIdKind::Uuid: |
| 308 | return 16; |
| 309 | case BuildIdKind::Sha1: |
| 310 | return 20; |
| 311 | case BuildIdKind::Hexstring: |
| 312 | return Config->BuildIdVector.size(); |
| 313 | default: |
| 314 | llvm_unreachable("unknown BuildIdKind"); |
| 315 | } |
| 316 | } |
| 317 | |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 318 | template <class ELFT> |
| 319 | BuildIdSection<ELFT>::BuildIdSection() |
| 320 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"), |
| Rui Ueyama | bb536fe | 2016-11-22 01:36:19 +0000 | [diff] [blame] | 321 | HashSize(getHashSize()) {} |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 322 | |
| 323 | template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) { |
| 324 | const endianness E = ELFT::TargetEndianness; |
| 325 | write32<E>(Buf, 4); // Name size |
| 326 | write32<E>(Buf + 4, HashSize); // Content size |
| 327 | write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type |
| 328 | memcpy(Buf + 12, "GNU", 4); // Name string |
| 329 | HashBuf = Buf + 16; |
| 330 | } |
| 331 | |
| Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 332 | // Split one uint8 array into small pieces of uint8 arrays. |
| George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 333 | static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr, |
| 334 | size_t ChunkSize) { |
| 335 | std::vector<ArrayRef<uint8_t>> Ret; |
| 336 | while (Arr.size() > ChunkSize) { |
| 337 | Ret.push_back(Arr.take_front(ChunkSize)); |
| 338 | Arr = Arr.drop_front(ChunkSize); |
| 339 | } |
| 340 | if (!Arr.empty()) |
| 341 | Ret.push_back(Arr); |
| 342 | return Ret; |
| 343 | } |
| 344 | |
| Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 345 | // Computes a hash value of Data using a given hash function. |
| 346 | // In order to utilize multiple cores, we first split data into 1MB |
| 347 | // chunks, compute a hash for each chunk, and then compute a hash value |
| 348 | // of the hash values. |
| George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 349 | template <class ELFT> |
| 350 | void BuildIdSection<ELFT>::computeHash( |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 351 | llvm::ArrayRef<uint8_t> Data, |
| 352 | std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) { |
| George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 353 | std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024); |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 354 | std::vector<uint8_t> Hashes(Chunks.size() * HashSize); |
| George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 355 | |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 356 | // Compute hash values. |
| Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 357 | forLoop(0, Chunks.size(), |
| 358 | [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); }); |
| Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 359 | |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 360 | // Write to the final output buffer. |
| 361 | HashFn(HashBuf, Hashes); |
| George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 364 | template <class ELFT> |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 365 | void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) { |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 366 | switch (Config->BuildId) { |
| 367 | case BuildIdKind::Fast: |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 368 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 369 | write64le(Dest, xxHash64(toStringRef(Arr))); |
| 370 | }); |
| 371 | break; |
| 372 | case BuildIdKind::Md5: |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 373 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
| Rui Ueyama | 28590b6 | 2016-11-23 18:11:38 +0000 | [diff] [blame] | 374 | memcpy(Dest, MD5::hash(Arr).data(), 16); |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 375 | }); |
| 376 | break; |
| 377 | case BuildIdKind::Sha1: |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 378 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
| Rui Ueyama | 28590b6 | 2016-11-23 18:11:38 +0000 | [diff] [blame] | 379 | memcpy(Dest, SHA1::hash(Arr).data(), 20); |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 380 | }); |
| 381 | break; |
| 382 | case BuildIdKind::Uuid: |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 383 | if (getRandomBytes(HashBuf, HashSize)) |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 384 | error("entropy source failure"); |
| 385 | break; |
| 386 | case BuildIdKind::Hexstring: |
| Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 387 | memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size()); |
| Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 388 | break; |
| 389 | default: |
| 390 | llvm_unreachable("unknown BuildIdKind"); |
| 391 | } |
| Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 394 | template <class ELFT> |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 395 | GotSection<ELFT>::GotSection() |
| 396 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 397 | Target->GotEntrySize, ".got") {} |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 398 | |
| 399 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) { |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 400 | Sym.GotIndex = NumEntries; |
| 401 | ++NumEntries; |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 404 | template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { |
| 405 | if (Sym.GlobalDynIndex != -1U) |
| 406 | return false; |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 407 | Sym.GlobalDynIndex = NumEntries; |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 408 | // Global Dynamic TLS entries take two GOT slots. |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 409 | NumEntries += 2; |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | |
| 413 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 414 | // In total it takes two GOT slots. |
| 415 | template <class ELFT> bool GotSection<ELFT>::addTlsIndex() { |
| 416 | if (TlsIndexOff != uint32_t(-1)) |
| 417 | return false; |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 418 | TlsIndexOff = NumEntries * sizeof(uintX_t); |
| 419 | NumEntries += 2; |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 420 | return true; |
| 421 | } |
| 422 | |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 423 | template <class ELFT> |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 424 | typename GotSection<ELFT>::uintX_t |
| 425 | GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const { |
| 426 | return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t); |
| 427 | } |
| 428 | |
| 429 | template <class ELFT> |
| 430 | typename GotSection<ELFT>::uintX_t |
| 431 | GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { |
| 432 | return B.GlobalDynIndex * sizeof(uintX_t); |
| 433 | } |
| 434 | |
| 435 | template <class ELFT> void GotSection<ELFT>::finalize() { |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 436 | Size = NumEntries * sizeof(uintX_t); |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 439 | template <class ELFT> bool GotSection<ELFT>::empty() const { |
| 440 | // If we have a relocation that is relative to GOT (such as GOTOFFREL), |
| 441 | // we need to emit a GOT even if it's empty. |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 442 | return NumEntries == 0 && !HasGotOffRel; |
| George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 445 | template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { |
| Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 446 | this->relocate(Buf, Buf + Size); |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | template <class ELFT> |
| 450 | MipsGotSection<ELFT>::MipsGotSection() |
| 451 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, |
| Simon Atanasyan | 2bd98af | 2017-01-16 21:17:23 +0000 | [diff] [blame] | 452 | SHT_PROGBITS, 16, ".got") {} |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 453 | |
| 454 | template <class ELFT> |
| 455 | void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, uintX_t Addend, |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 456 | RelExpr Expr) { |
| 457 | // For "true" local symbols which can be referenced from the same module |
| 458 | // only compiler creates two instructions for address loading: |
| 459 | // |
| 460 | // lw $8, 0($gp) # R_MIPS_GOT16 |
| 461 | // addi $8, $8, 0 # R_MIPS_LO16 |
| 462 | // |
| 463 | // The first instruction loads high 16 bits of the symbol address while |
| 464 | // the second adds an offset. That allows to reduce number of required |
| 465 | // GOT entries because only one global offset table entry is necessary |
| 466 | // for every 64 KBytes of local data. So for local symbols we need to |
| 467 | // allocate number of GOT entries to hold all required "page" addresses. |
| 468 | // |
| 469 | // All global symbols (hidden and regular) considered by compiler uniformly. |
| 470 | // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation |
| 471 | // to load address of the symbol. So for each such symbol we need to |
| 472 | // allocate dedicated GOT entry to store its address. |
| 473 | // |
| 474 | // If a symbol is preemptible we need help of dynamic linker to get its |
| 475 | // final address. The corresponding GOT entries are allocated in the |
| 476 | // "global" part of GOT. Entries for non preemptible global symbol allocated |
| 477 | // in the "local" part of GOT. |
| 478 | // |
| 479 | // See "Global Offset Table" in Chapter 5: |
| 480 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 481 | if (Expr == R_MIPS_GOT_LOCAL_PAGE) { |
| 482 | // At this point we do not know final symbol value so to reduce number |
| 483 | // of allocated GOT entries do the following trick. Save all output |
| 484 | // sections referenced by GOT relocations. Then later in the `finalize` |
| 485 | // method calculate number of "pages" required to cover all saved output |
| 486 | // section and allocate appropriate number of GOT entries. |
| Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 487 | PageIndexMap.insert({cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec, 0}); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | if (Sym.isTls()) { |
| 491 | // GOT entries created for MIPS TLS relocations behave like |
| 492 | // almost GOT entries from other ABIs. They go to the end |
| 493 | // of the global offset table. |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 494 | Sym.GotIndex = TlsEntries.size(); |
| 495 | TlsEntries.push_back(&Sym); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 496 | return; |
| 497 | } |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 498 | auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 499 | if (S.isInGot() && !A) |
| 500 | return; |
| 501 | size_t NewIndex = Items.size(); |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 502 | if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second) |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 503 | return; |
| 504 | Items.emplace_back(&S, A); |
| 505 | if (!A) |
| 506 | S.GotIndex = NewIndex; |
| 507 | }; |
| 508 | if (Sym.isPreemptible()) { |
| 509 | // Ignore addends for preemptible symbols. They got single GOT entry anyway. |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 510 | AddEntry(Sym, 0, GlobalEntries); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 511 | Sym.IsInGlobalMipsGot = true; |
| 512 | } else if (Expr == R_MIPS_GOT_OFF32) { |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 513 | AddEntry(Sym, Addend, LocalEntries32); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 514 | Sym.Is32BitMipsGot = true; |
| 515 | } else { |
| 516 | // Hold local GOT entries accessed via a 16-bit index separately. |
| 517 | // That allows to write them in the beginning of the GOT and keep |
| 518 | // their indexes as less as possible to escape relocation's overflow. |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 519 | AddEntry(Sym, Addend, LocalEntries); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| George Rimar | 879a657 | 2016-12-15 15:38:58 +0000 | [diff] [blame] | 523 | template <class ELFT> |
| 524 | bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 525 | if (Sym.GlobalDynIndex != -1U) |
| 526 | return false; |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 527 | Sym.GlobalDynIndex = TlsEntries.size(); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 528 | // Global Dynamic TLS entries take two GOT slots. |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 529 | TlsEntries.push_back(nullptr); |
| 530 | TlsEntries.push_back(&Sym); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 531 | return true; |
| 532 | } |
| 533 | |
| 534 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 535 | // In total it takes two GOT slots. |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 536 | template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 537 | if (TlsIndexOff != uint32_t(-1)) |
| 538 | return false; |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 539 | TlsIndexOff = TlsEntries.size() * sizeof(uintX_t); |
| 540 | TlsEntries.push_back(nullptr); |
| 541 | TlsEntries.push_back(nullptr); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
| Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 545 | static uint64_t getMipsPageAddr(uint64_t Addr) { |
| 546 | return (Addr + 0x8000) & ~0xffff; |
| 547 | } |
| 548 | |
| 549 | static uint64_t getMipsPageCount(uint64_t Size) { |
| 550 | return (Size + 0xfffe) / 0xffff + 1; |
| 551 | } |
| 552 | |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 553 | template <class ELFT> |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 554 | typename MipsGotSection<ELFT>::uintX_t |
| Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 555 | MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B, |
| 556 | uintX_t Addend) const { |
| 557 | const OutputSectionBase *OutSec = |
| 558 | cast<DefinedRegular<ELFT>>(&B)->Section->OutSec; |
| 559 | uintX_t SecAddr = getMipsPageAddr(OutSec->Addr); |
| 560 | uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend)); |
| 561 | uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff; |
| 562 | assert(Index < PageEntriesNum); |
| 563 | return (HeaderEntriesNum + Index) * sizeof(uintX_t); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | template <class ELFT> |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 567 | typename MipsGotSection<ELFT>::uintX_t |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 568 | MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B, |
| 569 | uintX_t Addend) const { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 570 | // Calculate offset of the GOT entries block: TLS, global, local. |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 571 | uintX_t Index = HeaderEntriesNum + PageEntriesNum; |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 572 | if (B.isTls()) |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 573 | Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size(); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 574 | else if (B.IsInGlobalMipsGot) |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 575 | Index += LocalEntries.size() + LocalEntries32.size(); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 576 | else if (B.Is32BitMipsGot) |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 577 | Index += LocalEntries.size(); |
| 578 | // Calculate offset of the GOT entry in the block. |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 579 | if (B.isInGot()) |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 580 | Index += B.GotIndex; |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 581 | else { |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 582 | auto It = EntryIndexMap.find({&B, Addend}); |
| 583 | assert(It != EntryIndexMap.end()); |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 584 | Index += It->second; |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 585 | } |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 586 | return Index * sizeof(uintX_t); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | template <class ELFT> |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 590 | typename MipsGotSection<ELFT>::uintX_t |
| 591 | MipsGotSection<ELFT>::getTlsOffset() const { |
| 592 | return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | template <class ELFT> |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 596 | typename MipsGotSection<ELFT>::uintX_t |
| 597 | MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 598 | return B.GlobalDynIndex * sizeof(uintX_t); |
| 599 | } |
| 600 | |
| 601 | template <class ELFT> |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 602 | const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const { |
| 603 | return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first; |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | template <class ELFT> |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 607 | unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const { |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 608 | return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() + |
| 609 | LocalEntries32.size(); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 612 | template <class ELFT> void MipsGotSection<ELFT>::finalize() { |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 613 | PageEntriesNum = 0; |
| Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 614 | for (std::pair<const OutputSectionBase *, size_t> &P : PageIndexMap) { |
| 615 | // For each output section referenced by GOT page relocations calculate |
| 616 | // and save into PageIndexMap an upper bound of MIPS GOT entries required |
| 617 | // to store page addresses of local symbols. We assume the worst case - |
| 618 | // each 64kb page of the output section has at least one GOT relocation |
| 619 | // against it. And take in account the case when the section intersects |
| 620 | // page boundaries. |
| 621 | P.second = PageEntriesNum; |
| 622 | PageEntriesNum += getMipsPageCount(P.first->Size); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 623 | } |
| Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 624 | Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) * |
| 625 | sizeof(uintX_t); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 628 | template <class ELFT> bool MipsGotSection<ELFT>::empty() const { |
| 629 | // We add the .got section to the result for dynamic MIPS target because |
| 630 | // its address and properties are mentioned in the .dynamic section. |
| 631 | return Config->Relocatable; |
| 632 | } |
| 633 | |
| Simon Atanasyan | b966665 | 2016-12-12 14:30:18 +0000 | [diff] [blame] | 634 | template <class ELFT> |
| 635 | typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const { |
| Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 636 | return ElfSym<ELFT>::MipsGp->template getVA<ELFT>(0); |
| 637 | } |
| 638 | |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 639 | template <class ELFT> |
| 640 | static void writeUint(uint8_t *Buf, typename ELFT::uint Val) { |
| 641 | typedef typename ELFT::uint uintX_t; |
| 642 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val); |
| 643 | } |
| 644 | |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 645 | template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 646 | // Set the MSB of the second GOT slot. This is not required by any |
| 647 | // MIPS ABI documentation, though. |
| 648 | // |
| 649 | // There is a comment in glibc saying that "The MSB of got[1] of a |
| 650 | // gnu object is set to identify gnu objects," and in GNU gold it |
| 651 | // says "the second entry will be used by some runtime loaders". |
| 652 | // But how this field is being used is unclear. |
| 653 | // |
| 654 | // We are not really willing to mimic other linkers behaviors |
| 655 | // without understanding why they do that, but because all files |
| 656 | // generated by GNU tools have this special GOT value, and because |
| 657 | // we've been doing this for years, it is probably a safe bet to |
| 658 | // keep doing this for now. We really need to revisit this to see |
| 659 | // if we had to do this. |
| 660 | auto *P = reinterpret_cast<typename ELFT::Off *>(Buf); |
| 661 | P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31); |
| Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 662 | Buf += HeaderEntriesNum * sizeof(uintX_t); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 663 | // Write 'page address' entries to the local part of the GOT. |
| Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 664 | for (std::pair<const OutputSectionBase *, size_t> &L : PageIndexMap) { |
| 665 | size_t PageCount = getMipsPageCount(L.first->Size); |
| 666 | uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr); |
| 667 | for (size_t PI = 0; PI < PageCount; ++PI) { |
| 668 | uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t); |
| 669 | writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000); |
| 670 | } |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 671 | } |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 672 | Buf += PageEntriesNum * sizeof(uintX_t); |
| 673 | auto AddEntry = [&](const GotEntry &SA) { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 674 | uint8_t *Entry = Buf; |
| 675 | Buf += sizeof(uintX_t); |
| 676 | const SymbolBody *Body = SA.first; |
| 677 | uintX_t VA = Body->template getVA<ELFT>(SA.second); |
| 678 | writeUint<ELFT>(Entry, VA); |
| 679 | }; |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 680 | std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry); |
| 681 | std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry); |
| 682 | std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry); |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 683 | // Initialize TLS-related GOT entries. If the entry has a corresponding |
| 684 | // dynamic relocations, leave it initialized by zero. Write down adjusted |
| 685 | // TLS symbol's values otherwise. To calculate the adjustments use offsets |
| 686 | // for thread-local storage. |
| 687 | // https://www.linux-mips.org/wiki/NPTL |
| 688 | if (TlsIndexOff != -1U && !Config->Pic) |
| 689 | writeUint<ELFT>(Buf + TlsIndexOff, 1); |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 690 | for (const SymbolBody *B : TlsEntries) { |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 691 | if (!B || B->isPreemptible()) |
| 692 | continue; |
| 693 | uintX_t VA = B->getVA<ELFT>(); |
| 694 | if (B->GotIndex != -1U) { |
| 695 | uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t); |
| 696 | writeUint<ELFT>(Entry, VA - 0x7000); |
| 697 | } |
| 698 | if (B->GlobalDynIndex != -1U) { |
| 699 | uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t); |
| 700 | writeUint<ELFT>(Entry, 1); |
| 701 | Entry += sizeof(uintX_t); |
| 702 | writeUint<ELFT>(Entry, VA - 0x8000); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 707 | template <class ELFT> |
| Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 708 | GotPltSection<ELFT>::GotPltSection() |
| 709 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| George Rimar | d8b2776 | 2016-11-14 10:14:18 +0000 | [diff] [blame] | 710 | Target->GotPltEntrySize, ".got.plt") {} |
| Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 711 | |
| 712 | template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 713 | Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size(); |
| 714 | Entries.push_back(&Sym); |
| 715 | } |
| 716 | |
| Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 717 | template <class ELFT> size_t GotPltSection<ELFT>::getSize() const { |
| 718 | return (Target->GotPltHeaderEntriesNum + Entries.size()) * |
| 719 | Target->GotPltEntrySize; |
| 720 | } |
| 721 | |
| 722 | template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 723 | Target->writeGotPltHeader(Buf); |
| 724 | Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize; |
| 725 | for (const SymbolBody *B : Entries) { |
| 726 | Target->writeGotPlt(Buf, *B); |
| 727 | Buf += sizeof(uintX_t); |
| 728 | } |
| 729 | } |
| 730 | |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 731 | // On ARM the IgotPltSection is part of the GotSection, on other Targets it is |
| 732 | // part of the .got.plt |
| 733 | template <class ELFT> |
| 734 | IgotPltSection<ELFT>::IgotPltSection() |
| 735 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 736 | Target->GotPltEntrySize, |
| 737 | Config->EMachine == EM_ARM ? ".got" : ".got.plt") { |
| 738 | } |
| 739 | |
| 740 | template <class ELFT> void IgotPltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 741 | Sym.IsInIgot = true; |
| 742 | Sym.GotPltIndex = Entries.size(); |
| 743 | Entries.push_back(&Sym); |
| 744 | } |
| 745 | |
| 746 | template <class ELFT> size_t IgotPltSection<ELFT>::getSize() const { |
| 747 | return Entries.size() * Target->GotPltEntrySize; |
| 748 | } |
| 749 | |
| 750 | template <class ELFT> void IgotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 751 | for (const SymbolBody *B : Entries) { |
| Peter Smith | 4b36029 | 2016-12-09 09:59:54 +0000 | [diff] [blame] | 752 | Target->writeIgotPlt(Buf, *B); |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 753 | Buf += sizeof(uintX_t); |
| 754 | } |
| 755 | } |
| 756 | |
| Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 757 | template <class ELFT> |
| 758 | StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) |
| 759 | : SyntheticSection<ELFT>(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1, |
| 760 | Name), |
| 761 | Dynamic(Dynamic) {} |
| 762 | |
| 763 | // Adds a string to the string table. If HashIt is true we hash and check for |
| 764 | // duplicates. It is optional because the name of global symbols are already |
| 765 | // uniqued and hashing them again has a big cost for a small value: uniquing |
| 766 | // them with some other string that happens to be the same. |
| 767 | template <class ELFT> |
| 768 | unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) { |
| 769 | if (HashIt) { |
| 770 | auto R = StringMap.insert(std::make_pair(S, this->Size)); |
| 771 | if (!R.second) |
| 772 | return R.first->second; |
| 773 | } |
| 774 | unsigned Ret = this->Size; |
| 775 | this->Size = this->Size + S.size() + 1; |
| 776 | Strings.push_back(S); |
| 777 | return Ret; |
| 778 | } |
| 779 | |
| 780 | template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 781 | // ELF string tables start with NUL byte, so advance the pointer by one. |
| 782 | ++Buf; |
| 783 | for (StringRef S : Strings) { |
| 784 | memcpy(Buf, S.data(), S.size()); |
| 785 | Buf += S.size() + 1; |
| 786 | } |
| 787 | } |
| 788 | |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 789 | // Returns the number of version definition entries. Because the first entry |
| 790 | // is for the version definition itself, it is the number of versioned symbols |
| 791 | // plus one. Note that we don't support multiple versions yet. |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 792 | static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } |
| 793 | |
| 794 | template <class ELFT> |
| 795 | DynamicSection<ELFT>::DynamicSection() |
| 796 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, |
| 797 | sizeof(uintX_t), ".dynamic") { |
| 798 | this->Entsize = ELFT::Is64Bits ? 16 : 8; |
| 799 | // .dynamic section is not writable on MIPS. |
| 800 | // See "Special Section" in Chapter 4 in the following document: |
| 801 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 802 | if (Config->EMachine == EM_MIPS) |
| 803 | this->Flags = SHF_ALLOC; |
| 804 | |
| 805 | addEntries(); |
| 806 | } |
| 807 | |
| 808 | // There are some dynamic entries that don't depend on other sections. |
| 809 | // Such entries can be set early. |
| 810 | template <class ELFT> void DynamicSection<ELFT>::addEntries() { |
| 811 | // Add strings to .dynstr early so that .dynstr's size will be |
| 812 | // fixed early. |
| 813 | for (StringRef S : Config->AuxiliaryList) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 814 | add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 815 | if (!Config->RPath.empty()) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 816 | add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 817 | In<ELFT>::DynStrTab->addString(Config->RPath)}); |
| 818 | for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles()) |
| 819 | if (F->isNeeded()) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 820 | add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 821 | if (!Config->SoName.empty()) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 822 | add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 823 | |
| 824 | // Set DT_FLAGS and DT_FLAGS_1. |
| 825 | uint32_t DtFlags = 0; |
| 826 | uint32_t DtFlags1 = 0; |
| 827 | if (Config->Bsymbolic) |
| 828 | DtFlags |= DF_SYMBOLIC; |
| 829 | if (Config->ZNodelete) |
| 830 | DtFlags1 |= DF_1_NODELETE; |
| 831 | if (Config->ZNow) { |
| 832 | DtFlags |= DF_BIND_NOW; |
| 833 | DtFlags1 |= DF_1_NOW; |
| 834 | } |
| 835 | if (Config->ZOrigin) { |
| 836 | DtFlags |= DF_ORIGIN; |
| 837 | DtFlags1 |= DF_1_ORIGIN; |
| 838 | } |
| 839 | |
| 840 | if (DtFlags) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 841 | add({DT_FLAGS, DtFlags}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 842 | if (DtFlags1) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 843 | add({DT_FLAGS_1, DtFlags1}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 844 | |
| Petr Hosek | 668bebe | 2016-12-07 02:05:42 +0000 | [diff] [blame] | 845 | if (!Config->Shared && !Config->Relocatable) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 846 | add({DT_DEBUG, (uint64_t)0}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | // Add remaining entries to complete .dynamic contents. |
| 850 | template <class ELFT> void DynamicSection<ELFT>::finalize() { |
| 851 | if (this->Size) |
| 852 | return; // Already finalized. |
| 853 | |
| 854 | this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 855 | if (In<ELFT>::RelaDyn->OutSec->Size > 0) { |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 856 | bool IsRela = Config->Rela; |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 857 | add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn}); |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 858 | add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->OutSec->Size}); |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 859 | add({IsRela ? DT_RELAENT : DT_RELENT, |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 860 | uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))}); |
| 861 | |
| 862 | // MIPS dynamic loader does not support RELCOUNT tag. |
| 863 | // The problem is in the tight relation between dynamic |
| 864 | // relocations and GOT. So do not emit this tag on MIPS. |
| 865 | if (Config->EMachine != EM_MIPS) { |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 866 | size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount(); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 867 | if (Config->ZCombreloc && NumRelativeRels) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 868 | add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 871 | if (In<ELFT>::RelaPlt->OutSec->Size > 0) { |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 872 | add({DT_JMPREL, In<ELFT>::RelaPlt}); |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 873 | add({DT_PLTRELSZ, In<ELFT>::RelaPlt->OutSec->Size}); |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 874 | add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT, |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 875 | In<ELFT>::GotPlt}); |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 876 | add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 879 | add({DT_SYMTAB, In<ELFT>::DynSymTab}); |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 880 | add({DT_SYMENT, sizeof(Elf_Sym)}); |
| 881 | add({DT_STRTAB, In<ELFT>::DynStrTab}); |
| 882 | add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()}); |
| Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 883 | if (In<ELFT>::GnuHashTab) |
| 884 | add({DT_GNU_HASH, In<ELFT>::GnuHashTab}); |
| Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 885 | if (In<ELFT>::HashTab) |
| 886 | add({DT_HASH, In<ELFT>::HashTab}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 887 | |
| 888 | if (Out<ELFT>::PreinitArray) { |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 889 | add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray}); |
| 890 | add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 891 | } |
| 892 | if (Out<ELFT>::InitArray) { |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 893 | add({DT_INIT_ARRAY, Out<ELFT>::InitArray}); |
| 894 | add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 895 | } |
| 896 | if (Out<ELFT>::FiniArray) { |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 897 | add({DT_FINI_ARRAY, Out<ELFT>::FiniArray}); |
| 898 | add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 +0000 | [diff] [blame] | 901 | if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Init)) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 902 | add({DT_INIT, B}); |
| Rafael Espindola | 1d6d1b4 | 2017-01-17 16:08:06 +0000 | [diff] [blame] | 903 | if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Fini)) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 904 | add({DT_FINI, B}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 905 | |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 906 | bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0; |
| 907 | if (HasVerNeed || In<ELFT>::VerDef) |
| 908 | add({DT_VERSYM, In<ELFT>::VerSym}); |
| 909 | if (In<ELFT>::VerDef) { |
| 910 | add({DT_VERDEF, In<ELFT>::VerDef}); |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 911 | add({DT_VERDEFNUM, getVerDefNum()}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 912 | } |
| 913 | if (HasVerNeed) { |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 914 | add({DT_VERNEED, In<ELFT>::VerNeed}); |
| 915 | add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | if (Config->EMachine == EM_MIPS) { |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 919 | add({DT_MIPS_RLD_VERSION, 1}); |
| 920 | add({DT_MIPS_FLAGS, RHF_NOTPOT}); |
| 921 | add({DT_MIPS_BASE_ADDRESS, Config->ImageBase}); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 922 | add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()}); |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 923 | add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()}); |
| 924 | if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry()) |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 925 | add({DT_MIPS_GOTSYM, B->DynsymIndex}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 926 | else |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 927 | add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()}); |
| Rui Ueyama | 729ac79 | 2016-11-17 04:10:09 +0000 | [diff] [blame] | 928 | add({DT_PLTGOT, In<ELFT>::MipsGot}); |
| Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 929 | if (In<ELFT>::MipsRldMap) |
| 930 | add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap}); |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | this->OutSec->Entsize = this->Entsize; |
| 934 | this->OutSec->Link = this->Link; |
| 935 | |
| 936 | // +1 for DT_NULL |
| 937 | this->Size = (Entries.size() + 1) * this->Entsize; |
| 938 | } |
| 939 | |
| 940 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
| 941 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 942 | |
| 943 | for (const Entry &E : Entries) { |
| 944 | P->d_tag = E.Tag; |
| 945 | switch (E.Kind) { |
| 946 | case Entry::SecAddr: |
| 947 | P->d_un.d_ptr = E.OutSec->Addr; |
| 948 | break; |
| 949 | case Entry::InSecAddr: |
| 950 | P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff; |
| 951 | break; |
| 952 | case Entry::SecSize: |
| 953 | P->d_un.d_val = E.OutSec->Size; |
| 954 | break; |
| 955 | case Entry::SymAddr: |
| 956 | P->d_un.d_ptr = E.Sym->template getVA<ELFT>(); |
| 957 | break; |
| 958 | case Entry::PlainInt: |
| 959 | P->d_un.d_val = E.Val; |
| 960 | break; |
| 961 | } |
| 962 | ++P; |
| 963 | } |
| 964 | } |
| 965 | |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 966 | template <class ELFT> |
| 967 | typename ELFT::uint DynamicReloc<ELFT>::getOffset() const { |
| 968 | if (OutputSec) |
| 969 | return OutputSec->Addr + OffsetInSec; |
| 970 | return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec); |
| 971 | } |
| 972 | |
| 973 | template <class ELFT> |
| 974 | typename ELFT::uint DynamicReloc<ELFT>::getAddend() const { |
| 975 | if (UseSymVA) |
| 976 | return Sym->getVA<ELFT>(Addend); |
| 977 | return Addend; |
| 978 | } |
| 979 | |
| 980 | template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const { |
| 981 | if (Sym && !UseSymVA) |
| 982 | return Sym->DynsymIndex; |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | template <class ELFT> |
| 987 | RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort) |
| 988 | : SyntheticSection<ELFT>(SHF_ALLOC, Config->Rela ? SHT_RELA : SHT_REL, |
| 989 | sizeof(uintX_t), Name), |
| 990 | Sort(Sort) { |
| 991 | this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 992 | } |
| 993 | |
| 994 | template <class ELFT> |
| 995 | void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) { |
| 996 | if (Reloc.Type == Target->RelativeRel) |
| 997 | ++NumRelativeRelocs; |
| 998 | Relocs.push_back(Reloc); |
| 999 | } |
| 1000 | |
| 1001 | template <class ELFT, class RelTy> |
| 1002 | static bool compRelocations(const RelTy &A, const RelTy &B) { |
| 1003 | bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel; |
| 1004 | bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel; |
| 1005 | if (AIsRel != BIsRel) |
| 1006 | return AIsRel; |
| 1007 | |
| 1008 | return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL); |
| 1009 | } |
| 1010 | |
| 1011 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1012 | uint8_t *BufBegin = Buf; |
| 1013 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
| 1014 | auto *P = reinterpret_cast<Elf_Rela *>(Buf); |
| 1015 | Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 1016 | |
| 1017 | if (Config->Rela) |
| 1018 | P->r_addend = Rel.getAddend(); |
| 1019 | P->r_offset = Rel.getOffset(); |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 1020 | if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot) |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1021 | // Dynamic relocation against MIPS GOT section make deal TLS entries |
| 1022 | // allocated in the end of the GOT. We need to adjust the offset to take |
| 1023 | // in account 'local' and 'global' GOT entries. |
| Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 1024 | P->r_offset += In<ELFT>::MipsGot->getTlsOffset(); |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1025 | P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL); |
| 1026 | } |
| 1027 | |
| 1028 | if (Sort) { |
| 1029 | if (Config->Rela) |
| 1030 | std::stable_sort((Elf_Rela *)BufBegin, |
| 1031 | (Elf_Rela *)BufBegin + Relocs.size(), |
| 1032 | compRelocations<ELFT, Elf_Rela>); |
| 1033 | else |
| 1034 | std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(), |
| 1035 | compRelocations<ELFT, Elf_Rel>); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { |
| 1040 | return this->Entsize * Relocs.size(); |
| 1041 | } |
| 1042 | |
| 1043 | template <class ELFT> void RelocationSection<ELFT>::finalize() { |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1044 | this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex |
| 1045 | : In<ELFT>::SymTab->OutSec->SectionIndex; |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1046 | |
| 1047 | // Set required output section properties. |
| 1048 | this->OutSec->Link = this->Link; |
| 1049 | this->OutSec->Entsize = this->Entsize; |
| 1050 | } |
| 1051 | |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1052 | template <class ELFT> |
| 1053 | SymbolTableSection<ELFT>::SymbolTableSection( |
| 1054 | StringTableSection<ELFT> &StrTabSec) |
| 1055 | : SyntheticSection<ELFT>(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0, |
| 1056 | StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, |
| 1057 | sizeof(uintX_t), |
| 1058 | StrTabSec.isDynamic() ? ".dynsym" : ".symtab"), |
| 1059 | StrTabSec(StrTabSec) { |
| 1060 | this->Entsize = sizeof(Elf_Sym); |
| 1061 | } |
| 1062 | |
| 1063 | // Orders symbols according to their positions in the GOT, |
| 1064 | // in compliance with MIPS ABI rules. |
| 1065 | // See "Global Offset Table" in Chapter 5 in the following document |
| 1066 | // for detailed description: |
| 1067 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 1068 | static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) { |
| 1069 | // Sort entries related to non-local preemptible symbols by GOT indexes. |
| 1070 | // All other entries go to the first part of GOT in arbitrary order. |
| 1071 | bool LIsInLocalGot = !L->IsInGlobalMipsGot; |
| 1072 | bool RIsInLocalGot = !R->IsInGlobalMipsGot; |
| 1073 | if (LIsInLocalGot || RIsInLocalGot) |
| 1074 | return !RIsInLocalGot; |
| 1075 | return L->GotIndex < R->GotIndex; |
| 1076 | } |
| 1077 | |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1078 | template <class ELFT> void SymbolTableSection<ELFT>::finalize() { |
| 1079 | this->OutSec->Link = this->Link = StrTabSec.OutSec->SectionIndex; |
| 1080 | this->OutSec->Info = this->Info = NumLocals + 1; |
| 1081 | this->OutSec->Entsize = this->Entsize; |
| 1082 | |
| George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1083 | if (Config->Relocatable) |
| 1084 | return; |
| 1085 | |
| 1086 | if (!StrTabSec.isDynamic()) { |
| 1087 | auto GlobBegin = Symbols.begin() + NumLocals; |
| Peter Smith | ccb34ef | 2017-01-24 10:43:40 +0000 | [diff] [blame] | 1088 | auto It = std::stable_partition( |
| 1089 | GlobBegin, Symbols.end(), [](const SymbolTableEntry &S) { |
| 1090 | return S.Symbol->symbol()->computeBinding() == STB_LOCAL; |
| 1091 | }); |
| 1092 | // update sh_info with number of Global symbols output with computed |
| 1093 | // binding of STB_LOCAL |
| George Rimar | 3a934ab | 2017-01-24 16:07:18 +0000 | [diff] [blame] | 1094 | this->OutSec->Info = this->Info = 1 + (It - Symbols.begin()); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | |
| Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1098 | if (In<ELFT>::GnuHashTab) |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1099 | // NB: It also sorts Symbols to meet the GNU hash table requirements. |
| Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1100 | In<ELFT>::GnuHashTab->addSymbols(Symbols); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1101 | else if (Config->EMachine == EM_MIPS) |
| 1102 | std::stable_sort(Symbols.begin(), Symbols.end(), |
| 1103 | [](const SymbolTableEntry &L, const SymbolTableEntry &R) { |
| 1104 | return sortMipsSymbols(L.Symbol, R.Symbol); |
| 1105 | }); |
| 1106 | size_t I = 0; |
| 1107 | for (const SymbolTableEntry &S : Symbols) |
| 1108 | S.Symbol->DynsymIndex = ++I; |
| 1109 | } |
| 1110 | |
| George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1111 | template <class ELFT> void SymbolTableSection<ELFT>::addGlobal(SymbolBody *B) { |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1112 | Symbols.push_back({B, StrTabSec.addString(B->getName(), false)}); |
| 1113 | } |
| 1114 | |
| George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1115 | template <class ELFT> void SymbolTableSection<ELFT>::addLocal(SymbolBody *B) { |
| 1116 | assert(!StrTabSec.isDynamic()); |
| 1117 | ++NumLocals; |
| 1118 | Symbols.push_back({B, StrTabSec.addString(B->getName())}); |
| 1119 | } |
| 1120 | |
| 1121 | template <class ELFT> |
| 1122 | size_t SymbolTableSection<ELFT>::getSymbolIndex(SymbolBody *Body) { |
| 1123 | auto I = llvm::find_if( |
| 1124 | Symbols, [&](const SymbolTableEntry &E) { return E.Symbol == Body; }); |
| 1125 | return I - Symbols.begin() + 1; |
| 1126 | } |
| 1127 | |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1128 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1129 | Buf += sizeof(Elf_Sym); |
| 1130 | |
| 1131 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 1132 | // .dynsym only contains global symbols. |
| 1133 | if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic()) |
| 1134 | writeLocalSymbols(Buf); |
| 1135 | |
| 1136 | writeGlobalSymbols(Buf); |
| 1137 | } |
| 1138 | |
| 1139 | template <class ELFT> |
| 1140 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 1141 | // Iterate over all input object files to copy their local symbols |
| 1142 | // to the output symbol table pointed by Buf. |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1143 | |
| George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1144 | for (auto I = Symbols.begin(); I != Symbols.begin() + NumLocals; ++I) { |
| 1145 | const DefinedRegular<ELFT> &Body = *cast<DefinedRegular<ELFT>>(I->Symbol); |
| 1146 | InputSectionBase<ELFT> *Section = Body.Section; |
| 1147 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| 1148 | |
| 1149 | if (!Section) { |
| 1150 | ESym->st_shndx = SHN_ABS; |
| 1151 | ESym->st_value = Body.Value; |
| 1152 | } else { |
| 1153 | const OutputSectionBase *OutSec = Section->OutSec; |
| 1154 | ESym->st_shndx = OutSec->SectionIndex; |
| 1155 | ESym->st_value = OutSec->Addr + Section->getOffset(Body); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1156 | } |
| George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1157 | ESym->st_name = I->StrTabOffset; |
| 1158 | ESym->st_size = Body.template getSize<ELFT>(); |
| 1159 | ESym->setBindingAndType(STB_LOCAL, Body.Type); |
| 1160 | Buf += sizeof(*ESym); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | template <class ELFT> |
| 1165 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) { |
| 1166 | // Write the internal symbol table contents to the output symbol table |
| 1167 | // pointed by Buf. |
| 1168 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1169 | |
| 1170 | for (auto I = Symbols.begin() + NumLocals; I != Symbols.end(); ++I) { |
| 1171 | const SymbolTableEntry &S = *I; |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1172 | SymbolBody *Body = S.Symbol; |
| 1173 | size_t StrOff = S.StrTabOffset; |
| 1174 | |
| 1175 | uint8_t Type = Body->Type; |
| 1176 | uintX_t Size = Body->getSize<ELFT>(); |
| 1177 | |
| Rui Ueyama | 9f0f8b8 | 2017-01-10 21:52:56 +0000 | [diff] [blame] | 1178 | ESym->setBindingAndType(Body->symbol()->computeBinding(), Type); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1179 | ESym->st_size = Size; |
| 1180 | ESym->st_name = StrOff; |
| 1181 | ESym->setVisibility(Body->symbol()->Visibility); |
| 1182 | ESym->st_value = Body->getVA<ELFT>(); |
| 1183 | |
| Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 1184 | if (const OutputSectionBase *OutSec = getOutputSection(Body)) { |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1185 | ESym->st_shndx = OutSec->SectionIndex; |
| Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 1186 | } else if (isa<DefinedRegular<ELFT>>(Body)) { |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1187 | ESym->st_shndx = SHN_ABS; |
| Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 1188 | } else if (isa<DefinedCommon>(Body)) { |
| 1189 | ESym->st_shndx = SHN_COMMON; |
| 1190 | ESym->st_value = cast<DefinedCommon>(Body)->Alignment; |
| 1191 | } |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1192 | |
| 1193 | if (Config->EMachine == EM_MIPS) { |
| 1194 | // On MIPS we need to mark symbol which has a PLT entry and requires |
| 1195 | // pointer equality by STO_MIPS_PLT flag. That is necessary to help |
| 1196 | // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. |
| 1197 | // https://sourceware.org/ml/binutils/2008-07/txt00000.txt |
| 1198 | if (Body->isInPlt() && Body->NeedsCopyOrPltAddr) |
| 1199 | ESym->st_other |= STO_MIPS_PLT; |
| 1200 | if (Config->Relocatable) { |
| 1201 | auto *D = dyn_cast<DefinedRegular<ELFT>>(Body); |
| 1202 | if (D && D->isMipsPIC()) |
| 1203 | ESym->st_other |= STO_MIPS_PIC; |
| 1204 | } |
| 1205 | } |
| 1206 | ++ESym; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | template <class ELFT> |
| 1211 | const OutputSectionBase * |
| 1212 | SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) { |
| 1213 | switch (Sym->kind()) { |
| 1214 | case SymbolBody::DefinedSyntheticKind: |
| Rui Ueyama | 4f2f50d | 2016-12-21 08:40:09 +0000 | [diff] [blame] | 1215 | return cast<DefinedSynthetic>(Sym)->Section; |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1216 | case SymbolBody::DefinedRegularKind: { |
| 1217 | auto &D = cast<DefinedRegular<ELFT>>(*Sym); |
| 1218 | if (D.Section) |
| 1219 | return D.Section->OutSec; |
| 1220 | break; |
| 1221 | } |
| 1222 | case SymbolBody::DefinedCommonKind: |
| Rui Ueyama | b2a23cf | 2017-01-24 03:41:20 +0000 | [diff] [blame] | 1223 | if (!Config->DefineCommon) |
| 1224 | return nullptr; |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1225 | return In<ELFT>::Common->OutSec; |
| Peter Collingbourne | feb6629 | 2017-01-10 01:21:50 +0000 | [diff] [blame] | 1226 | case SymbolBody::SharedKind: { |
| 1227 | auto &SS = cast<SharedSymbol<ELFT>>(*Sym); |
| 1228 | if (SS.needsCopy()) |
| 1229 | return SS.getBssSectionForCopy(); |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1230 | break; |
| Peter Collingbourne | feb6629 | 2017-01-10 01:21:50 +0000 | [diff] [blame] | 1231 | } |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1232 | case SymbolBody::UndefinedKind: |
| 1233 | case SymbolBody::LazyArchiveKind: |
| 1234 | case SymbolBody::LazyObjectKind: |
| 1235 | break; |
| 1236 | } |
| 1237 | return nullptr; |
| 1238 | } |
| 1239 | |
| Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1240 | template <class ELFT> |
| 1241 | GnuHashTableSection<ELFT>::GnuHashTableSection() |
| 1242 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t), |
| 1243 | ".gnu.hash") { |
| 1244 | this->Entsize = ELFT::Is64Bits ? 0 : 4; |
| 1245 | } |
| 1246 | |
| 1247 | template <class ELFT> |
| 1248 | unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) { |
| 1249 | if (!NumHashed) |
| 1250 | return 0; |
| 1251 | |
| 1252 | // These values are prime numbers which are not greater than 2^(N-1) + 1. |
| 1253 | // In result, for any particular NumHashed we return a prime number |
| 1254 | // which is not greater than NumHashed. |
| 1255 | static const unsigned Primes[] = { |
| 1256 | 1, 1, 3, 3, 7, 13, 31, 61, 127, 251, |
| 1257 | 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071}; |
| 1258 | |
| 1259 | return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed), |
| 1260 | array_lengthof(Primes) - 1)]; |
| 1261 | } |
| 1262 | |
| 1263 | // Bloom filter estimation: at least 8 bits for each hashed symbol. |
| 1264 | // GNU Hash table requirement: it should be a power of 2, |
| 1265 | // the minimum value is 1, even for an empty table. |
| 1266 | // Expected results for a 32-bit target: |
| 1267 | // calcMaskWords(0..4) = 1 |
| 1268 | // calcMaskWords(5..8) = 2 |
| 1269 | // calcMaskWords(9..16) = 4 |
| 1270 | // For a 64-bit target: |
| 1271 | // calcMaskWords(0..8) = 1 |
| 1272 | // calcMaskWords(9..16) = 2 |
| 1273 | // calcMaskWords(17..32) = 4 |
| 1274 | template <class ELFT> |
| 1275 | unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) { |
| 1276 | if (!NumHashed) |
| 1277 | return 1; |
| 1278 | return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off)); |
| 1279 | } |
| 1280 | |
| 1281 | template <class ELFT> void GnuHashTableSection<ELFT>::finalize() { |
| 1282 | unsigned NumHashed = Symbols.size(); |
| 1283 | NBuckets = calcNBuckets(NumHashed); |
| 1284 | MaskWords = calcMaskWords(NumHashed); |
| 1285 | // Second hash shift estimation: just predefined values. |
| 1286 | Shift2 = ELFT::Is64Bits ? 6 : 5; |
| 1287 | |
| 1288 | this->OutSec->Entsize = this->Entsize; |
| 1289 | this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
| 1290 | this->Size = sizeof(Elf_Word) * 4 // Header |
| 1291 | + sizeof(Elf_Off) * MaskWords // Bloom Filter |
| 1292 | + sizeof(Elf_Word) * NBuckets // Hash Buckets |
| 1293 | + sizeof(Elf_Word) * NumHashed; // Hash Values |
| 1294 | } |
| 1295 | |
| 1296 | template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1297 | writeHeader(Buf); |
| 1298 | if (Symbols.empty()) |
| 1299 | return; |
| 1300 | writeBloomFilter(Buf); |
| 1301 | writeHashTable(Buf); |
| 1302 | } |
| 1303 | |
| 1304 | template <class ELFT> |
| 1305 | void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) { |
| 1306 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 1307 | *P++ = NBuckets; |
| 1308 | *P++ = In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size(); |
| 1309 | *P++ = MaskWords; |
| 1310 | *P++ = Shift2; |
| 1311 | Buf = reinterpret_cast<uint8_t *>(P); |
| 1312 | } |
| 1313 | |
| 1314 | template <class ELFT> |
| 1315 | void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) { |
| 1316 | unsigned C = sizeof(Elf_Off) * 8; |
| 1317 | |
| 1318 | auto *Masks = reinterpret_cast<Elf_Off *>(Buf); |
| 1319 | for (const SymbolData &Sym : Symbols) { |
| 1320 | size_t Pos = (Sym.Hash / C) & (MaskWords - 1); |
| 1321 | uintX_t V = (uintX_t(1) << (Sym.Hash % C)) | |
| 1322 | (uintX_t(1) << ((Sym.Hash >> Shift2) % C)); |
| 1323 | Masks[Pos] |= V; |
| 1324 | } |
| 1325 | Buf += sizeof(Elf_Off) * MaskWords; |
| 1326 | } |
| 1327 | |
| 1328 | template <class ELFT> |
| 1329 | void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { |
| 1330 | Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); |
| 1331 | Elf_Word *Values = Buckets + NBuckets; |
| 1332 | |
| 1333 | int PrevBucket = -1; |
| 1334 | int I = 0; |
| 1335 | for (const SymbolData &Sym : Symbols) { |
| 1336 | int Bucket = Sym.Hash % NBuckets; |
| 1337 | assert(PrevBucket <= Bucket); |
| 1338 | if (Bucket != PrevBucket) { |
| 1339 | Buckets[Bucket] = Sym.Body->DynsymIndex; |
| 1340 | PrevBucket = Bucket; |
| 1341 | if (I > 0) |
| 1342 | Values[I - 1] |= 1; |
| 1343 | } |
| 1344 | Values[I] = Sym.Hash & ~1; |
| 1345 | ++I; |
| 1346 | } |
| 1347 | if (I > 0) |
| 1348 | Values[I - 1] |= 1; |
| 1349 | } |
| 1350 | |
| 1351 | static uint32_t hashGnu(StringRef Name) { |
| 1352 | uint32_t H = 5381; |
| 1353 | for (uint8_t C : Name) |
| 1354 | H = (H << 5) + H + C; |
| 1355 | return H; |
| 1356 | } |
| 1357 | |
| 1358 | // Add symbols to this symbol hash table. Note that this function |
| 1359 | // destructively sort a given vector -- which is needed because |
| 1360 | // GNU-style hash table places some sorting requirements. |
| 1361 | template <class ELFT> |
| 1362 | void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) { |
| 1363 | // Ideally this will just be 'auto' but GCC 6.1 is not able |
| 1364 | // to deduce it correctly. |
| 1365 | std::vector<SymbolTableEntry>::iterator Mid = |
| 1366 | std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) { |
| 1367 | return S.Symbol->isUndefined(); |
| 1368 | }); |
| 1369 | if (Mid == V.end()) |
| 1370 | return; |
| 1371 | for (auto I = Mid, E = V.end(); I != E; ++I) { |
| 1372 | SymbolBody *B = I->Symbol; |
| 1373 | size_t StrOff = I->StrTabOffset; |
| 1374 | Symbols.push_back({B, StrOff, hashGnu(B->getName())}); |
| 1375 | } |
| 1376 | |
| 1377 | unsigned NBuckets = calcNBuckets(Symbols.size()); |
| 1378 | std::stable_sort(Symbols.begin(), Symbols.end(), |
| 1379 | [&](const SymbolData &L, const SymbolData &R) { |
| 1380 | return L.Hash % NBuckets < R.Hash % NBuckets; |
| 1381 | }); |
| 1382 | |
| 1383 | V.erase(Mid, V.end()); |
| 1384 | for (const SymbolData &Sym : Symbols) |
| 1385 | V.push_back({Sym.Body, Sym.STName}); |
| 1386 | } |
| 1387 | |
| Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1388 | template <class ELFT> |
| 1389 | HashTableSection<ELFT>::HashTableSection() |
| 1390 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_HASH, sizeof(Elf_Word), ".hash") { |
| 1391 | this->Entsize = sizeof(Elf_Word); |
| 1392 | } |
| 1393 | |
| 1394 | template <class ELFT> void HashTableSection<ELFT>::finalize() { |
| 1395 | this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
| 1396 | this->OutSec->Entsize = this->Entsize; |
| 1397 | |
| 1398 | unsigned NumEntries = 2; // nbucket and nchain. |
| 1399 | NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. |
| 1400 | |
| 1401 | // Create as many buckets as there are symbols. |
| 1402 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 1403 | // support for SHT_GNU_HASH is probably even more profitable. |
| 1404 | NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); |
| 1405 | this->Size = NumEntries * sizeof(Elf_Word); |
| 1406 | } |
| 1407 | |
| 1408 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1409 | unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols(); |
| 1410 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 1411 | *P++ = NumSymbols; // nbucket |
| 1412 | *P++ = NumSymbols; // nchain |
| 1413 | |
| 1414 | Elf_Word *Buckets = P; |
| 1415 | Elf_Word *Chains = P + NumSymbols; |
| 1416 | |
| 1417 | for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { |
| 1418 | SymbolBody *Body = S.Symbol; |
| 1419 | StringRef Name = Body->getName(); |
| 1420 | unsigned I = Body->DynsymIndex; |
| 1421 | uint32_t Hash = hashSysV(Name) % NumSymbols; |
| 1422 | Chains[I] = Buckets[Hash]; |
| 1423 | Buckets[Hash] = I; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1427 | template <class ELFT> |
| 1428 | PltSection<ELFT>::PltSection() |
| 1429 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, |
| 1430 | ".plt") {} |
| 1431 | |
| 1432 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1433 | // At beginning of PLT, we have code to call the dynamic linker |
| 1434 | // to resolve dynsyms at runtime. Write such code. |
| 1435 | Target->writePltHeader(Buf); |
| 1436 | size_t Off = Target->PltHeaderSize; |
| 1437 | |
| 1438 | for (auto &I : Entries) { |
| 1439 | const SymbolBody *B = I.first; |
| 1440 | unsigned RelOff = I.second; |
| 1441 | uint64_t Got = B->getGotPltVA<ELFT>(); |
| 1442 | uint64_t Plt = this->getVA() + Off; |
| 1443 | Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); |
| 1444 | Off += Target->PltEntrySize; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 1449 | Sym.PltIndex = Entries.size(); |
| 1450 | unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset(); |
| 1451 | Entries.push_back(std::make_pair(&Sym, RelOff)); |
| 1452 | } |
| 1453 | |
| 1454 | template <class ELFT> size_t PltSection<ELFT>::getSize() const { |
| 1455 | return Target->PltHeaderSize + Entries.size() * Target->PltEntrySize; |
| 1456 | } |
| 1457 | |
| Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame^] | 1458 | // Some architectures such as additional symbols in the PLT section. For |
| 1459 | // example ARM uses mapping symbols to aid disassembly |
| 1460 | template <class ELFT> void PltSection<ELFT>::addSymbols() { |
| 1461 | Target->addPltHeaderSymbols(this); |
| 1462 | size_t Off = Target->PltHeaderSize; |
| 1463 | for (size_t I = 0; I < Entries.size(); ++I) { |
| 1464 | Target->addPltSymbols(this, Off); |
| 1465 | Off += Target->PltEntrySize; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1469 | template <class ELFT> |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1470 | IpltSection<ELFT>::IpltSection() |
| 1471 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, |
| 1472 | ".plt") {} |
| 1473 | |
| 1474 | template <class ELFT> void IpltSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1475 | // The IRelative relocations do not support lazy binding so no header is |
| 1476 | // needed |
| 1477 | size_t Off = 0; |
| 1478 | for (auto &I : Entries) { |
| 1479 | const SymbolBody *B = I.first; |
| 1480 | unsigned RelOff = I.second + In<ELFT>::Plt->getSize(); |
| 1481 | uint64_t Got = B->getGotPltVA<ELFT>(); |
| 1482 | uint64_t Plt = this->getVA() + Off; |
| 1483 | Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); |
| 1484 | Off += Target->PltEntrySize; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | template <class ELFT> void IpltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 1489 | Sym.PltIndex = Entries.size(); |
| 1490 | Sym.IsInIplt = true; |
| 1491 | unsigned RelOff = In<ELFT>::RelaIplt->getRelocOffset(); |
| 1492 | Entries.push_back(std::make_pair(&Sym, RelOff)); |
| 1493 | } |
| 1494 | |
| 1495 | template <class ELFT> size_t IpltSection<ELFT>::getSize() const { |
| 1496 | return Entries.size() * Target->PltEntrySize; |
| 1497 | } |
| 1498 | |
| Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame^] | 1499 | template <class ELFT> void IpltSection<ELFT>::addSymbols() { |
| 1500 | size_t Off = 0; |
| 1501 | for (size_t I = 0; I < Entries.size(); ++I) { |
| 1502 | Target->addPltSymbols(this, Off); |
| 1503 | Off += Target->PltEntrySize; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1507 | template <class ELFT> |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1508 | GdbIndexSection<ELFT>::GdbIndexSection() |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1509 | : SyntheticSection<ELFT>(0, SHT_PROGBITS, 1, ".gdb_index"), |
| 1510 | StringPool(llvm::StringTableBuilder::ELF) {} |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1511 | |
| 1512 | template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() { |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1513 | for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) |
| 1514 | if (InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S)) |
| 1515 | if (IS->OutSec && IS->Name == ".debug_info") |
| 1516 | readDwarf(IS); |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1519 | // Iterative hash function for symbol's name is described in .gdb_index format |
| 1520 | // specification. Note that we use one for version 5 to 7 here, it is different |
| 1521 | // for version 4. |
| 1522 | static uint32_t hash(StringRef Str) { |
| 1523 | uint32_t R = 0; |
| 1524 | for (uint8_t C : Str) |
| 1525 | R = R * 67 + tolower(C) - 113; |
| 1526 | return R; |
| 1527 | } |
| 1528 | |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1529 | template <class ELFT> |
| 1530 | void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) { |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1531 | GdbIndexBuilder<ELFT> Builder(I); |
| 1532 | if (ErrorCount) |
| 1533 | return; |
| 1534 | |
| 1535 | size_t CuId = CompilationUnits.size(); |
| 1536 | std::vector<std::pair<uintX_t, uintX_t>> CuList = Builder.readCUList(); |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1537 | CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end()); |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1538 | |
| 1539 | std::vector<AddressEntry<ELFT>> AddrArea = Builder.readAddressArea(CuId); |
| 1540 | AddressArea.insert(AddressArea.end(), AddrArea.begin(), AddrArea.end()); |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1541 | |
| 1542 | std::vector<std::pair<StringRef, uint8_t>> NamesAndTypes = |
| 1543 | Builder.readPubNamesAndTypes(); |
| 1544 | |
| 1545 | for (std::pair<StringRef, uint8_t> &Pair : NamesAndTypes) { |
| 1546 | uint32_t Hash = hash(Pair.first); |
| 1547 | size_t Offset = StringPool.add(Pair.first); |
| 1548 | |
| 1549 | bool IsNew; |
| 1550 | GdbSymbol *Sym; |
| 1551 | std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset); |
| 1552 | if (IsNew) { |
| 1553 | Sym->CuVectorIndex = CuVectors.size(); |
| 1554 | CuVectors.push_back({{CuId, Pair.second}}); |
| 1555 | continue; |
| 1556 | } |
| 1557 | |
| 1558 | std::vector<std::pair<uint32_t, uint8_t>> &CuVec = |
| 1559 | CuVectors[Sym->CuVectorIndex]; |
| 1560 | CuVec.push_back({CuId, Pair.second}); |
| 1561 | } |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | template <class ELFT> void GdbIndexSection<ELFT>::finalize() { |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1565 | if (Finalized) |
| 1566 | return; |
| 1567 | Finalized = true; |
| 1568 | |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1569 | parseDebugSections(); |
| 1570 | |
| 1571 | // GdbIndex header consist from version fields |
| 1572 | // and 5 more fields with different kinds of offsets. |
| 1573 | CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1574 | SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize; |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1575 | |
| 1576 | ConstantPoolOffset = |
| 1577 | SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize; |
| 1578 | |
| 1579 | for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) { |
| 1580 | CuVectorsOffset.push_back(CuVectorsSize); |
| 1581 | CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1); |
| 1582 | } |
| 1583 | StringPoolOffset = ConstantPoolOffset + CuVectorsSize; |
| 1584 | |
| 1585 | StringPool.finalizeInOrder(); |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | template <class ELFT> size_t GdbIndexSection<ELFT>::getSize() const { |
| 1589 | const_cast<GdbIndexSection<ELFT> *>(this)->finalize(); |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1590 | return StringPoolOffset + StringPool.getSize(); |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) { |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1594 | write32le(Buf, 7); // Write version. |
| 1595 | write32le(Buf + 4, CuListOffset); // CU list offset. |
| 1596 | write32le(Buf + 8, CuTypesOffset); // Types CU list offset. |
| 1597 | write32le(Buf + 12, CuTypesOffset); // Address area offset. |
| 1598 | write32le(Buf + 16, SymTabOffset); // Symbol table offset. |
| 1599 | write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset. |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1600 | Buf += 24; |
| 1601 | |
| 1602 | // Write the CU list. |
| 1603 | for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) { |
| 1604 | write64le(Buf, CU.first); |
| 1605 | write64le(Buf + 8, CU.second); |
| 1606 | Buf += 16; |
| 1607 | } |
| George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Write the address area. |
| 1610 | for (AddressEntry<ELFT> &E : AddressArea) { |
| 1611 | uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0); |
| 1612 | write64le(Buf, BaseAddr + E.LowAddress); |
| 1613 | write64le(Buf + 8, BaseAddr + E.HighAddress); |
| 1614 | write32le(Buf + 16, E.CuIndex); |
| 1615 | Buf += 20; |
| 1616 | } |
| George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1617 | |
| 1618 | // Write the symbol table. |
| 1619 | for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) { |
| 1620 | GdbSymbol *Sym = SymbolTable.getSymbol(I); |
| 1621 | if (Sym) { |
| 1622 | size_t NameOffset = |
| 1623 | Sym->NameOffset + StringPoolOffset - ConstantPoolOffset; |
| 1624 | size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex]; |
| 1625 | write32le(Buf, NameOffset); |
| 1626 | write32le(Buf + 4, CuVectorOffset); |
| 1627 | } |
| 1628 | Buf += 8; |
| 1629 | } |
| 1630 | |
| 1631 | // Write the CU vectors into the constant pool. |
| 1632 | for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) { |
| 1633 | write32le(Buf, CuVec.size()); |
| 1634 | Buf += 4; |
| 1635 | for (std::pair<uint32_t, uint8_t> &P : CuVec) { |
| 1636 | uint32_t Index = P.first; |
| 1637 | uint8_t Flags = P.second; |
| 1638 | Index |= Flags << 24; |
| 1639 | write32le(Buf, Index); |
| 1640 | Buf += 4; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | StringPool.write(Buf); |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 1647 | template <class ELFT> bool GdbIndexSection<ELFT>::empty() const { |
| 1648 | return !Out<ELFT>::DebugInfo; |
| 1649 | } |
| 1650 | |
| Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 1651 | template <class ELFT> |
| 1652 | EhFrameHeader<ELFT>::EhFrameHeader() |
| 1653 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {} |
| 1654 | |
| 1655 | // .eh_frame_hdr contains a binary search table of pointers to FDEs. |
| 1656 | // Each entry of the search table consists of two values, |
| 1657 | // the starting PC from where FDEs covers, and the FDE's address. |
| 1658 | // It is sorted by PC. |
| 1659 | template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { |
| 1660 | const endianness E = ELFT::TargetEndianness; |
| 1661 | |
| 1662 | // Sort the FDE list by their PC and uniqueify. Usually there is only |
| 1663 | // one FDE for a PC (i.e. function), but if ICF merges two functions |
| 1664 | // into one, there can be more than one FDEs pointing to the address. |
| 1665 | auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; |
| 1666 | std::stable_sort(Fdes.begin(), Fdes.end(), Less); |
| 1667 | auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; |
| 1668 | Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); |
| 1669 | |
| 1670 | Buf[0] = 1; |
| 1671 | Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 1672 | Buf[2] = DW_EH_PE_udata4; |
| 1673 | Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; |
| 1674 | write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->getVA() - 4); |
| 1675 | write32<E>(Buf + 8, Fdes.size()); |
| 1676 | Buf += 12; |
| 1677 | |
| 1678 | uintX_t VA = this->getVA(); |
| 1679 | for (FdeData &Fde : Fdes) { |
| 1680 | write32<E>(Buf, Fde.Pc - VA); |
| 1681 | write32<E>(Buf + 4, Fde.FdeVA - VA); |
| 1682 | Buf += 8; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const { |
| 1687 | // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. |
| 1688 | return 12 + Out<ELFT>::EhFrame->NumFdes * 8; |
| 1689 | } |
| 1690 | |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1691 | template <class ELFT> |
| Rui Ueyama | b38ddb1 | 2016-11-21 19:46:04 +0000 | [diff] [blame] | 1692 | void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { |
| 1693 | Fdes.push_back({Pc, FdeVA}); |
| 1694 | } |
| 1695 | |
| George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 1696 | template <class ELFT> bool EhFrameHeader<ELFT>::empty() const { |
| 1697 | return Out<ELFT>::EhFrame->empty(); |
| 1698 | } |
| 1699 | |
| Rui Ueyama | b38ddb1 | 2016-11-21 19:46:04 +0000 | [diff] [blame] | 1700 | template <class ELFT> |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1701 | VersionDefinitionSection<ELFT>::VersionDefinitionSection() |
| 1702 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), |
| 1703 | ".gnu.version_d") {} |
| 1704 | |
| 1705 | static StringRef getFileDefName() { |
| 1706 | if (!Config->SoName.empty()) |
| 1707 | return Config->SoName; |
| 1708 | return Config->OutputFile; |
| 1709 | } |
| 1710 | |
| 1711 | template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() { |
| 1712 | FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName()); |
| 1713 | for (VersionDefinition &V : Config->VersionDefinitions) |
| 1714 | V.NameOff = In<ELFT>::DynStrTab->addString(V.Name); |
| 1715 | |
| 1716 | this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
| 1717 | |
| 1718 | // sh_info should be set to the number of definitions. This fact is missed in |
| 1719 | // documentation, but confirmed by binutils community: |
| 1720 | // https://sourceware.org/ml/binutils/2014-11/msg00355.html |
| 1721 | this->OutSec->Info = this->Info = getVerDefNum(); |
| 1722 | } |
| 1723 | |
| 1724 | template <class ELFT> |
| 1725 | void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, |
| 1726 | StringRef Name, size_t NameOff) { |
| 1727 | auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
| 1728 | Verdef->vd_version = 1; |
| 1729 | Verdef->vd_cnt = 1; |
| 1730 | Verdef->vd_aux = sizeof(Elf_Verdef); |
| 1731 | Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1732 | Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); |
| 1733 | Verdef->vd_ndx = Index; |
| 1734 | Verdef->vd_hash = hashSysV(Name); |
| 1735 | |
| 1736 | auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); |
| 1737 | Verdaux->vda_name = NameOff; |
| 1738 | Verdaux->vda_next = 0; |
| 1739 | } |
| 1740 | |
| 1741 | template <class ELFT> |
| 1742 | void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1743 | writeOne(Buf, 1, getFileDefName(), FileDefNameOff); |
| 1744 | |
| 1745 | for (VersionDefinition &V : Config->VersionDefinitions) { |
| 1746 | Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1747 | writeOne(Buf, V.Id, V.Name, V.NameOff); |
| 1748 | } |
| 1749 | |
| 1750 | // Need to terminate the last version definition. |
| 1751 | Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
| 1752 | Verdef->vd_next = 0; |
| 1753 | } |
| 1754 | |
| 1755 | template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const { |
| 1756 | return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); |
| 1757 | } |
| 1758 | |
| 1759 | template <class ELFT> |
| 1760 | VersionTableSection<ELFT>::VersionTableSection() |
| 1761 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), |
| 1762 | ".gnu.version") {} |
| 1763 | |
| 1764 | template <class ELFT> void VersionTableSection<ELFT>::finalize() { |
| 1765 | this->OutSec->Entsize = this->Entsize = sizeof(Elf_Versym); |
| 1766 | // At the moment of june 2016 GNU docs does not mention that sh_link field |
| 1767 | // should be set, but Sun docs do. Also readelf relies on this field. |
| 1768 | this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex; |
| 1769 | } |
| 1770 | |
| 1771 | template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const { |
| 1772 | return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1); |
| 1773 | } |
| 1774 | |
| 1775 | template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1776 | auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; |
| 1777 | for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) { |
| 1778 | OutVersym->vs_index = S.Symbol->symbol()->VersionId; |
| 1779 | ++OutVersym; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 1783 | template <class ELFT> bool VersionTableSection<ELFT>::empty() const { |
| 1784 | return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty(); |
| 1785 | } |
| 1786 | |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1787 | template <class ELFT> |
| 1788 | VersionNeedSection<ELFT>::VersionNeedSection() |
| 1789 | : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), |
| 1790 | ".gnu.version_r") { |
| 1791 | // Identifiers in verneed section start at 2 because 0 and 1 are reserved |
| 1792 | // for VER_NDX_LOCAL and VER_NDX_GLOBAL. |
| 1793 | // First identifiers are reserved by verdef section if it exist. |
| 1794 | NextIndex = getVerDefNum() + 1; |
| 1795 | } |
| 1796 | |
| 1797 | template <class ELFT> |
| 1798 | void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) { |
| 1799 | if (!SS->Verdef) { |
| 1800 | SS->symbol()->VersionId = VER_NDX_GLOBAL; |
| 1801 | return; |
| 1802 | } |
| 1803 | SharedFile<ELFT> *F = SS->file(); |
| 1804 | // If we don't already know that we need an Elf_Verneed for this DSO, prepare |
| 1805 | // to create one by adding it to our needed list and creating a dynstr entry |
| 1806 | // for the soname. |
| 1807 | if (F->VerdefMap.empty()) |
| 1808 | Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())}); |
| 1809 | typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef]; |
| 1810 | // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, |
| 1811 | // prepare to create one by allocating a version identifier and creating a |
| 1812 | // dynstr entry for the version name. |
| 1813 | if (NV.Index == 0) { |
| 1814 | NV.StrTab = In<ELFT>::DynStrTab->addString( |
| 1815 | SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name); |
| 1816 | NV.Index = NextIndex++; |
| 1817 | } |
| 1818 | SS->symbol()->VersionId = NV.Index; |
| 1819 | } |
| 1820 | |
| 1821 | template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1822 | // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. |
| 1823 | auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); |
| 1824 | auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); |
| 1825 | |
| 1826 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { |
| 1827 | // Create an Elf_Verneed for this DSO. |
| 1828 | Verneed->vn_version = 1; |
| 1829 | Verneed->vn_cnt = P.first->VerdefMap.size(); |
| 1830 | Verneed->vn_file = P.second; |
| 1831 | Verneed->vn_aux = |
| 1832 | reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); |
| 1833 | Verneed->vn_next = sizeof(Elf_Verneed); |
| 1834 | ++Verneed; |
| 1835 | |
| 1836 | // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over |
| 1837 | // VerdefMap, which will only contain references to needed version |
| 1838 | // definitions. Each Elf_Vernaux is based on the information contained in |
| 1839 | // the Elf_Verdef in the source DSO. This loop iterates over a std::map of |
| 1840 | // pointers, but is deterministic because the pointers refer to Elf_Verdef |
| 1841 | // data structures within a single input file. |
| 1842 | for (auto &NV : P.first->VerdefMap) { |
| 1843 | Vernaux->vna_hash = NV.first->vd_hash; |
| 1844 | Vernaux->vna_flags = 0; |
| 1845 | Vernaux->vna_other = NV.second.Index; |
| 1846 | Vernaux->vna_name = NV.second.StrTab; |
| 1847 | Vernaux->vna_next = sizeof(Elf_Vernaux); |
| 1848 | ++Vernaux; |
| 1849 | } |
| 1850 | |
| 1851 | Vernaux[-1].vna_next = 0; |
| 1852 | } |
| 1853 | Verneed[-1].vn_next = 0; |
| 1854 | } |
| 1855 | |
| 1856 | template <class ELFT> void VersionNeedSection<ELFT>::finalize() { |
| 1857 | this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex; |
| 1858 | this->OutSec->Info = this->Info = Needed.size(); |
| 1859 | } |
| 1860 | |
| 1861 | template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { |
| 1862 | unsigned Size = Needed.size() * sizeof(Elf_Verneed); |
| 1863 | for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed) |
| 1864 | Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); |
| 1865 | return Size; |
| 1866 | } |
| 1867 | |
| George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 1868 | template <class ELFT> bool VersionNeedSection<ELFT>::empty() const { |
| 1869 | return getNeedNum() == 0; |
| 1870 | } |
| 1871 | |
| Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 1872 | template <class ELFT> |
| Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 1873 | MipsRldMapSection<ELFT>::MipsRldMapSection() |
| Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 1874 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 1875 | sizeof(typename ELFT::uint), ".rld_map") {} |
| 1876 | |
| Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 1877 | template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) { |
| Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 1878 | // Apply filler from linker script. |
| 1879 | uint64_t Filler = Script<ELFT>::X->getFiller(this->Name); |
| 1880 | Filler = (Filler << 32) | Filler; |
| 1881 | memcpy(Buf, &Filler, getSize()); |
| 1882 | } |
| 1883 | |
| Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 1884 | template <class ELFT> |
| 1885 | ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection() |
| 1886 | : SyntheticSection<ELFT>(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, |
| 1887 | sizeof(typename ELFT::uint), ".ARM.exidx") {} |
| 1888 | |
| 1889 | // Write a terminating sentinel entry to the end of the .ARM.exidx table. |
| 1890 | // This section will have been sorted last in the .ARM.exidx table. |
| 1891 | // This table entry will have the form: |
| 1892 | // | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND | |
| George Rimar | 879a657 | 2016-12-15 15:38:58 +0000 | [diff] [blame] | 1893 | template <class ELFT> |
| 1894 | void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf) { |
| Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 1895 | // Get the InputSection before us, we are by definition last |
| 1896 | auto RI = cast<OutputSection<ELFT>>(this->OutSec)->Sections.rbegin(); |
| 1897 | InputSection<ELFT> *LE = *(++RI); |
| 1898 | InputSection<ELFT> *LC = cast<InputSection<ELFT>>(LE->getLinkOrderDep()); |
| 1899 | uint64_t S = LC->OutSec->Addr + LC->getOffset(LC->getSize()); |
| 1900 | uint64_t P = this->getVA(); |
| 1901 | Target->relocateOne(Buf, R_ARM_PREL31, S - P); |
| 1902 | write32le(Buf + 4, 0x1); |
| 1903 | } |
| 1904 | |
| Rafael Espindola | 682a5bc | 2016-11-08 14:42:34 +0000 | [diff] [blame] | 1905 | template InputSection<ELF32LE> *elf::createCommonSection(); |
| 1906 | template InputSection<ELF32BE> *elf::createCommonSection(); |
| 1907 | template InputSection<ELF64LE> *elf::createCommonSection(); |
| 1908 | template InputSection<ELF64BE> *elf::createCommonSection(); |
| Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 1909 | |
| Rafael Espindola | c0e47fb | 2016-11-08 14:56:27 +0000 | [diff] [blame] | 1910 | template InputSection<ELF32LE> *elf::createInterpSection(); |
| 1911 | template InputSection<ELF32BE> *elf::createInterpSection(); |
| 1912 | template InputSection<ELF64LE> *elf::createInterpSection(); |
| 1913 | template InputSection<ELF64BE> *elf::createInterpSection(); |
| Rui Ueyama | e288eef | 2016-11-02 18:58:44 +0000 | [diff] [blame] | 1914 | |
| Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 1915 | template MergeInputSection<ELF32LE> *elf::createCommentSection(); |
| 1916 | template MergeInputSection<ELF32BE> *elf::createCommentSection(); |
| 1917 | template MergeInputSection<ELF64LE> *elf::createCommentSection(); |
| 1918 | template MergeInputSection<ELF64BE> *elf::createCommentSection(); |
| 1919 | |
| Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame^] | 1920 | template SymbolBody * |
| 1921 | elf::addSyntheticLocal<ELF32LE>(StringRef, uint8_t, ELF32LE::uint, |
| 1922 | ELF32LE::uint, InputSectionBase<ELF32LE> *); |
| 1923 | template SymbolBody * |
| 1924 | elf::addSyntheticLocal<ELF32BE>(StringRef, uint8_t, ELF32BE::uint, |
| 1925 | ELF32BE::uint, InputSectionBase<ELF32BE> *); |
| 1926 | template SymbolBody * |
| 1927 | elf::addSyntheticLocal<ELF64LE>(StringRef, uint8_t, ELF64LE::uint, |
| 1928 | ELF64LE::uint, InputSectionBase<ELF64LE> *); |
| 1929 | template SymbolBody * |
| 1930 | elf::addSyntheticLocal<ELF64BE>(StringRef, uint8_t, ELF64BE::uint, |
| 1931 | ELF64BE::uint, InputSectionBase<ELF64BE> *); |
| 1932 | |
| Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 1933 | template class elf::MipsAbiFlagsSection<ELF32LE>; |
| 1934 | template class elf::MipsAbiFlagsSection<ELF32BE>; |
| 1935 | template class elf::MipsAbiFlagsSection<ELF64LE>; |
| 1936 | template class elf::MipsAbiFlagsSection<ELF64BE>; |
| 1937 | |
| Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 1938 | template class elf::MipsOptionsSection<ELF32LE>; |
| 1939 | template class elf::MipsOptionsSection<ELF32BE>; |
| 1940 | template class elf::MipsOptionsSection<ELF64LE>; |
| 1941 | template class elf::MipsOptionsSection<ELF64BE>; |
| 1942 | |
| 1943 | template class elf::MipsReginfoSection<ELF32LE>; |
| 1944 | template class elf::MipsReginfoSection<ELF32BE>; |
| 1945 | template class elf::MipsReginfoSection<ELF64LE>; |
| 1946 | template class elf::MipsReginfoSection<ELF64BE>; |
| 1947 | |
| Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 1948 | template class elf::BuildIdSection<ELF32LE>; |
| 1949 | template class elf::BuildIdSection<ELF32BE>; |
| 1950 | template class elf::BuildIdSection<ELF64LE>; |
| 1951 | template class elf::BuildIdSection<ELF64BE>; |
| 1952 | |
| Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 1953 | template class elf::GotSection<ELF32LE>; |
| 1954 | template class elf::GotSection<ELF32BE>; |
| 1955 | template class elf::GotSection<ELF64LE>; |
| 1956 | template class elf::GotSection<ELF64BE>; |
| 1957 | |
| Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 1958 | template class elf::MipsGotSection<ELF32LE>; |
| 1959 | template class elf::MipsGotSection<ELF32BE>; |
| 1960 | template class elf::MipsGotSection<ELF64LE>; |
| 1961 | template class elf::MipsGotSection<ELF64BE>; |
| 1962 | |
| Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 1963 | template class elf::GotPltSection<ELF32LE>; |
| 1964 | template class elf::GotPltSection<ELF32BE>; |
| 1965 | template class elf::GotPltSection<ELF64LE>; |
| 1966 | template class elf::GotPltSection<ELF64BE>; |
| Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1967 | |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 1968 | template class elf::IgotPltSection<ELF32LE>; |
| 1969 | template class elf::IgotPltSection<ELF32BE>; |
| 1970 | template class elf::IgotPltSection<ELF64LE>; |
| 1971 | template class elf::IgotPltSection<ELF64BE>; |
| 1972 | |
| Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 1973 | template class elf::StringTableSection<ELF32LE>; |
| 1974 | template class elf::StringTableSection<ELF32BE>; |
| 1975 | template class elf::StringTableSection<ELF64LE>; |
| 1976 | template class elf::StringTableSection<ELF64BE>; |
| Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1977 | |
| 1978 | template class elf::DynamicSection<ELF32LE>; |
| 1979 | template class elf::DynamicSection<ELF32BE>; |
| 1980 | template class elf::DynamicSection<ELF64LE>; |
| 1981 | template class elf::DynamicSection<ELF64BE>; |
| Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1982 | |
| 1983 | template class elf::RelocationSection<ELF32LE>; |
| 1984 | template class elf::RelocationSection<ELF32BE>; |
| 1985 | template class elf::RelocationSection<ELF64LE>; |
| 1986 | template class elf::RelocationSection<ELF64BE>; |
| Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1987 | |
| 1988 | template class elf::SymbolTableSection<ELF32LE>; |
| 1989 | template class elf::SymbolTableSection<ELF32BE>; |
| 1990 | template class elf::SymbolTableSection<ELF64LE>; |
| 1991 | template class elf::SymbolTableSection<ELF64BE>; |
| Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1992 | |
| 1993 | template class elf::GnuHashTableSection<ELF32LE>; |
| 1994 | template class elf::GnuHashTableSection<ELF32BE>; |
| 1995 | template class elf::GnuHashTableSection<ELF64LE>; |
| 1996 | template class elf::GnuHashTableSection<ELF64BE>; |
| Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1997 | |
| 1998 | template class elf::HashTableSection<ELF32LE>; |
| 1999 | template class elf::HashTableSection<ELF32BE>; |
| 2000 | template class elf::HashTableSection<ELF64LE>; |
| 2001 | template class elf::HashTableSection<ELF64BE>; |
| Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 2002 | |
| 2003 | template class elf::PltSection<ELF32LE>; |
| 2004 | template class elf::PltSection<ELF32BE>; |
| 2005 | template class elf::PltSection<ELF64LE>; |
| 2006 | template class elf::PltSection<ELF64BE>; |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2007 | |
| Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 2008 | template class elf::IpltSection<ELF32LE>; |
| 2009 | template class elf::IpltSection<ELF32BE>; |
| 2010 | template class elf::IpltSection<ELF64LE>; |
| 2011 | template class elf::IpltSection<ELF64BE>; |
| 2012 | |
| Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2013 | template class elf::GdbIndexSection<ELF32LE>; |
| 2014 | template class elf::GdbIndexSection<ELF32BE>; |
| 2015 | template class elf::GdbIndexSection<ELF64LE>; |
| 2016 | template class elf::GdbIndexSection<ELF64BE>; |
| Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2017 | |
| 2018 | template class elf::EhFrameHeader<ELF32LE>; |
| 2019 | template class elf::EhFrameHeader<ELF32BE>; |
| 2020 | template class elf::EhFrameHeader<ELF64LE>; |
| 2021 | template class elf::EhFrameHeader<ELF64BE>; |
| Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2022 | |
| 2023 | template class elf::VersionTableSection<ELF32LE>; |
| 2024 | template class elf::VersionTableSection<ELF32BE>; |
| 2025 | template class elf::VersionTableSection<ELF64LE>; |
| 2026 | template class elf::VersionTableSection<ELF64BE>; |
| 2027 | |
| 2028 | template class elf::VersionNeedSection<ELF32LE>; |
| 2029 | template class elf::VersionNeedSection<ELF32BE>; |
| 2030 | template class elf::VersionNeedSection<ELF64LE>; |
| 2031 | template class elf::VersionNeedSection<ELF64BE>; |
| 2032 | |
| 2033 | template class elf::VersionDefinitionSection<ELF32LE>; |
| 2034 | template class elf::VersionDefinitionSection<ELF32BE>; |
| 2035 | template class elf::VersionDefinitionSection<ELF64LE>; |
| 2036 | template class elf::VersionDefinitionSection<ELF64BE>; |
| Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 2037 | |
| Rui Ueyama | bdfa155 | 2016-11-22 19:24:52 +0000 | [diff] [blame] | 2038 | template class elf::MipsRldMapSection<ELF32LE>; |
| 2039 | template class elf::MipsRldMapSection<ELF32BE>; |
| 2040 | template class elf::MipsRldMapSection<ELF64LE>; |
| 2041 | template class elf::MipsRldMapSection<ELF64BE>; |
| Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2042 | |
| 2043 | template class elf::ARMExidxSentinelSection<ELF32LE>; |
| 2044 | template class elf::ARMExidxSentinelSection<ELF32BE>; |
| 2045 | template class elf::ARMExidxSentinelSection<ELF64LE>; |
| 2046 | template class elf::ARMExidxSentinelSection<ELF64BE>; |