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" |
Rui Ueyama | 6e5fda9 | 2017-10-26 21:37:17 +0000 | [diff] [blame] | 18 | #include "Bits.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 19 | #include "Config.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 20 | #include "InputFiles.h" |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 21 | #include "LinkerScript.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 22 | #include "OutputSections.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 23 | #include "SymbolTable.h" |
Rafael Espindola | d26b52f | 2017-12-09 16:56:18 +0000 | [diff] [blame] | 24 | #include "Symbols.h" |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 25 | #include "Target.h" |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 26 | #include "Writer.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 27 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 28 | #include "lld/Common/Memory.h" |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 29 | #include "lld/Common/Strings.h" |
Bob Haarman | 4f5c8c2 | 2017-10-13 18:22:55 +0000 | [diff] [blame] | 30 | #include "lld/Common/Threads.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 31 | #include "lld/Common/Version.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 32 | #include "llvm/BinaryFormat/Dwarf.h" |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h" |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 34 | #include "llvm/Object/Decompressor.h" |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 35 | #include "llvm/Object/ELFObjectFile.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Endian.h" |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 37 | #include "llvm/Support/LEB128.h" |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MD5.h" |
| 39 | #include "llvm/Support/RandomNumberGenerator.h" |
| 40 | #include "llvm/Support/SHA1.h" |
| 41 | #include "llvm/Support/xxhash.h" |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 42 | #include <cstdlib> |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 43 | #include <thread> |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace llvm; |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 46 | using namespace llvm::dwarf; |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 47 | using namespace llvm::ELF; |
| 48 | using namespace llvm::object; |
| 49 | using namespace llvm::support; |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace lld; |
| 52 | using namespace lld::elf; |
| 53 | |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 54 | using llvm::support::endian::write32le; |
| 55 | using llvm::support::endian::write64le; |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 56 | |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 57 | constexpr size_t MergeNoTailSection::NumShards; |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 58 | |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 59 | // Returns an LLD version string. |
| 60 | static ArrayRef<uint8_t> getVersion() { |
| 61 | // Check LLD_VERSION first for ease of testing. |
Eric Christopher | 7baac21 | 2018-03-20 18:10:30 +0000 | [diff] [blame] | 62 | // You can get consistent output by using the environment variable. |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 63 | // This is only for testing. |
| 64 | StringRef S = getenv("LLD_VERSION"); |
| 65 | if (S.empty()) |
| 66 | S = Saver.save(Twine("Linker: ") + getLLDVersion()); |
| 67 | |
| 68 | // +1 to include the terminating '\0'. |
| 69 | return {(const uint8_t *)S.data(), S.size() + 1}; |
Davide Italiano | b69f38f | 2016-11-11 00:05:41 +0000 | [diff] [blame] | 70 | } |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 71 | |
| 72 | // Creates a .comment section containing LLD version info. |
| 73 | // With this feature, you can identify LLD-generated binaries easily |
Rui Ueyama | 42fca6e | 2017-04-27 04:50:08 +0000 | [diff] [blame] | 74 | // by "readelf --string-dump .comment <file>". |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 75 | // The returned object is a mergeable string section. |
Rafael Espindola | 5c73c49 | 2017-12-21 01:21:59 +0000 | [diff] [blame] | 76 | MergeInputSection *elf::createCommentSection() { |
| 77 | return make<MergeInputSection>(SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, 1, |
| 78 | getVersion(), ".comment"); |
Rui Ueyama | 3da3f06 | 2016-11-10 20:20:37 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 81 | // .MIPS.abiflags section. |
| 82 | template <class ELFT> |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 83 | MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 84 | : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 85 | Flags(Flags) { |
| 86 | this->Entsize = sizeof(Elf_Mips_ABIFlags); |
| 87 | } |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 88 | |
| 89 | template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) { |
| 90 | memcpy(Buf, &Flags, sizeof(Flags)); |
| 91 | } |
| 92 | |
| 93 | template <class ELFT> |
| 94 | MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() { |
| 95 | Elf_Mips_ABIFlags Flags = {}; |
| 96 | bool Create = false; |
| 97 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 +0000 | [diff] [blame] | 98 | for (InputSectionBase *Sec : InputSections) { |
Simon Atanasyan | 462f84a | 2017-03-17 14:27:55 +0000 | [diff] [blame] | 99 | if (Sec->Type != SHT_MIPS_ABIFLAGS) |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 100 | continue; |
| 101 | Sec->Live = false; |
| 102 | Create = true; |
| 103 | |
Rui Ueyama | 25f3088 | 2017-10-27 03:25:04 +0000 | [diff] [blame] | 104 | std::string Filename = toString(Sec->File); |
Simon Atanasyan | 86dc60d | 2016-12-21 05:31:57 +0000 | [diff] [blame] | 105 | const size_t Size = Sec->Data.size(); |
| 106 | // Older version of BFD (such as the default FreeBSD linker) concatenate |
| 107 | // .MIPS.abiflags instead of merging. To allow for this case (or potential |
| 108 | // zero padding) we ignore everything after the first Elf_Mips_ABIFlags |
| 109 | if (Size < sizeof(Elf_Mips_ABIFlags)) { |
| 110 | error(Filename + ": invalid size of .MIPS.abiflags section: got " + |
| 111 | Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags))); |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 112 | return nullptr; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 113 | } |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 114 | auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data()); |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 115 | if (S->version != 0) { |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 116 | error(Filename + ": unexpected .MIPS.abiflags version " + |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 117 | Twine(S->version)); |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 118 | return nullptr; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 119 | } |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 120 | |
Simon Atanasyan | 649e4d3 | 2017-10-02 14:56:41 +0000 | [diff] [blame] | 121 | // LLD checks ISA compatibility in calcMipsEFlags(). Here we just |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 122 | // select the highest number of ISA/Rev/Ext. |
| 123 | Flags.isa_level = std::max(Flags.isa_level, S->isa_level); |
| 124 | Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev); |
| 125 | Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext); |
| 126 | Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size); |
| 127 | Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size); |
| 128 | Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size); |
| 129 | Flags.ases |= S->ases; |
| 130 | Flags.flags1 |= S->flags1; |
| 131 | Flags.flags2 |= S->flags2; |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 132 | Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename); |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 133 | }; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 134 | |
Rui Ueyama | 12f2da8 | 2016-11-22 03:57:06 +0000 | [diff] [blame] | 135 | if (Create) |
| 136 | return make<MipsAbiFlagsSection<ELFT>>(Flags); |
| 137 | return nullptr; |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 140 | // .MIPS.options section. |
| 141 | template <class ELFT> |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 142 | MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 143 | : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 144 | Reginfo(Reginfo) { |
| 145 | this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo); |
| 146 | } |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 147 | |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 148 | template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) { |
| 149 | auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf); |
| 150 | Options->kind = ODK_REGINFO; |
| 151 | Options->size = getSize(); |
| 152 | |
| 153 | if (!Config->Relocatable) |
Rafael Espindola | b3aa2c9 | 2017-05-11 21:33:30 +0000 | [diff] [blame] | 154 | Reginfo.ri_gp_value = InX::MipsGot->getGp(); |
Rafael Espindola | 4862ae8 | 2016-11-24 16:38:35 +0000 | [diff] [blame] | 155 | memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo)); |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 158 | template <class ELFT> |
| 159 | MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() { |
| 160 | // N64 ABI only. |
| 161 | if (!ELFT::Is64Bits) |
| 162 | return nullptr; |
| 163 | |
Rui Ueyama | f9c66e4 | 2017-10-27 04:15:28 +0000 | [diff] [blame] | 164 | std::vector<InputSectionBase *> Sections; |
| 165 | for (InputSectionBase *Sec : InputSections) |
| 166 | if (Sec->Type == SHT_MIPS_OPTIONS) |
| 167 | Sections.push_back(Sec); |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 168 | |
Rui Ueyama | f9c66e4 | 2017-10-27 04:15:28 +0000 | [diff] [blame] | 169 | if (Sections.empty()) |
| 170 | return nullptr; |
| 171 | |
| 172 | Elf_Mips_RegInfo Reginfo = {}; |
| 173 | for (InputSectionBase *Sec : Sections) { |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 174 | Sec->Live = false; |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 175 | |
Rui Ueyama | 25f3088 | 2017-10-27 03:25:04 +0000 | [diff] [blame] | 176 | std::string Filename = toString(Sec->File); |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 177 | ArrayRef<uint8_t> D = Sec->Data; |
| 178 | |
| 179 | while (!D.empty()) { |
| 180 | if (D.size() < sizeof(Elf_Mips_Options)) { |
| 181 | error(Filename + ": invalid size of .MIPS.options section"); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data()); |
| 186 | if (Opt->kind == ODK_REGINFO) { |
| 187 | if (Config->Relocatable && Opt->getRegInfo().ri_gp_value) |
| 188 | error(Filename + ": unsupported non-zero ri_gp_value"); |
| 189 | Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 190 | Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value; |
Rui Ueyama | 9cfac8a | 2016-11-22 04:13:09 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | |
| 194 | if (!Opt->size) |
| 195 | fatal(Filename + ": zero option descriptor size"); |
| 196 | D = D.slice(Opt->size); |
| 197 | } |
| 198 | }; |
| 199 | |
Rui Ueyama | f9c66e4 | 2017-10-27 04:15:28 +0000 | [diff] [blame] | 200 | return make<MipsOptionsSection<ELFT>>(Reginfo); |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // MIPS .reginfo section. |
| 204 | template <class ELFT> |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 205 | MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 206 | : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 207 | Reginfo(Reginfo) { |
| 208 | this->Entsize = sizeof(Elf_Mips_RegInfo); |
| 209 | } |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 210 | |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 211 | template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) { |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 212 | if (!Config->Relocatable) |
Rafael Espindola | b3aa2c9 | 2017-05-11 21:33:30 +0000 | [diff] [blame] | 213 | Reginfo.ri_gp_value = InX::MipsGot->getGp(); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 214 | memcpy(Buf, &Reginfo, sizeof(Reginfo)); |
| 215 | } |
| 216 | |
| 217 | template <class ELFT> |
| 218 | MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() { |
| 219 | // Section should be alive for O32 and N32 ABIs only. |
| 220 | if (ELFT::Is64Bits) |
| 221 | return nullptr; |
| 222 | |
Rui Ueyama | f9c66e4 | 2017-10-27 04:15:28 +0000 | [diff] [blame] | 223 | std::vector<InputSectionBase *> Sections; |
| 224 | for (InputSectionBase *Sec : InputSections) |
| 225 | if (Sec->Type == SHT_MIPS_REGINFO) |
| 226 | Sections.push_back(Sec); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 227 | |
Rui Ueyama | f9c66e4 | 2017-10-27 04:15:28 +0000 | [diff] [blame] | 228 | if (Sections.empty()) |
| 229 | return nullptr; |
| 230 | |
| 231 | Elf_Mips_RegInfo Reginfo = {}; |
| 232 | for (InputSectionBase *Sec : Sections) { |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 233 | Sec->Live = false; |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 234 | |
| 235 | if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) { |
Rui Ueyama | 25f3088 | 2017-10-27 03:25:04 +0000 | [diff] [blame] | 236 | error(toString(Sec->File) + ": invalid size of .reginfo section"); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 237 | return nullptr; |
| 238 | } |
| 239 | auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data()); |
| 240 | if (Config->Relocatable && R->ri_gp_value) |
Rui Ueyama | 25f3088 | 2017-10-27 03:25:04 +0000 | [diff] [blame] | 241 | error(toString(Sec->File) + ": unsupported non-zero ri_gp_value"); |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 242 | |
| 243 | Reginfo.ri_gprmask |= R->ri_gprmask; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 244 | Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value; |
Rui Ueyama | b71cae9 | 2016-11-22 03:57:08 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Rui Ueyama | f9c66e4 | 2017-10-27 04:15:28 +0000 | [diff] [blame] | 247 | return make<MipsReginfoSection<ELFT>>(Reginfo); |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Rui Ueyama | 3255a52 | 2017-02-27 02:32:49 +0000 | [diff] [blame] | 250 | InputSection *elf::createInterpSection() { |
Rui Ueyama | 81a4b26 | 2016-11-22 04:33:01 +0000 | [diff] [blame] | 251 | // StringSaver guarantees that the returned string ends with '\0'. |
| 252 | StringRef S = Saver.save(Config->DynamicLinker); |
Rui Ueyama | 6e50fd5 | 2017-03-01 07:39:06 +0000 | [diff] [blame] | 253 | ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1}; |
| 254 | |
Rafael Espindola | ce3b52c | 2017-12-21 02:11:51 +0000 | [diff] [blame] | 255 | auto *Sec = make<InputSection>(nullptr, SHF_ALLOC, SHT_PROGBITS, 1, Contents, |
| 256 | ".interp"); |
Rui Ueyama | 6e50fd5 | 2017-03-01 07:39:06 +0000 | [diff] [blame] | 257 | Sec->Live = true; |
| 258 | return Sec; |
Rui Ueyama | a9ee8d6 | 2016-11-04 22:25:39 +0000 | [diff] [blame] | 259 | } |
Rui Ueyama | e288eef | 2016-11-02 18:58:44 +0000 | [diff] [blame] | 260 | |
Peter Collingbourne | c5391ce | 2018-03-29 22:32:13 +0000 | [diff] [blame] | 261 | Defined *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value, |
| 262 | uint64_t Size, InputSectionBase &Section) { |
Rafael Espindola | 1037eef | 2017-12-19 23:59:35 +0000 | [diff] [blame] | 263 | auto *S = make<Defined>(Section.File, Name, STB_LOCAL, STV_DEFAULT, Type, |
| 264 | Value, Size, &Section); |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 265 | if (InX::SymTab) |
| 266 | InX::SymTab->addSymbol(S); |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 267 | return S; |
| 268 | } |
| 269 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 270 | static size_t getHashSize() { |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 271 | switch (Config->BuildId) { |
| 272 | case BuildIdKind::Fast: |
| 273 | return 8; |
| 274 | case BuildIdKind::Md5: |
| 275 | case BuildIdKind::Uuid: |
| 276 | return 16; |
| 277 | case BuildIdKind::Sha1: |
| 278 | return 20; |
| 279 | case BuildIdKind::Hexstring: |
| 280 | return Config->BuildIdVector.size(); |
| 281 | default: |
| 282 | llvm_unreachable("unknown BuildIdKind"); |
| 283 | } |
| 284 | } |
| 285 | |
George Rimar | 6c2949d | 2017-03-20 16:40:21 +0000 | [diff] [blame] | 286 | BuildIdSection::BuildIdSection() |
Jake Ehrlich | 1128dc5 | 2017-10-30 22:08:11 +0000 | [diff] [blame] | 287 | : SyntheticSection(SHF_ALLOC, SHT_NOTE, 4, ".note.gnu.build-id"), |
Rui Ueyama | bb536fe | 2016-11-22 01:36:19 +0000 | [diff] [blame] | 288 | HashSize(getHashSize()) {} |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 289 | |
George Rimar | 6c2949d | 2017-03-20 16:40:21 +0000 | [diff] [blame] | 290 | void BuildIdSection::writeTo(uint8_t *Buf) { |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 291 | write32(Buf, 4); // Name size |
| 292 | write32(Buf + 4, HashSize); // Content size |
| 293 | write32(Buf + 8, NT_GNU_BUILD_ID); // Type |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 294 | memcpy(Buf + 12, "GNU", 4); // Name string |
| 295 | HashBuf = Buf + 16; |
| 296 | } |
| 297 | |
Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 298 | // Split one uint8 array into small pieces of uint8 arrays. |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 299 | static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr, |
| 300 | size_t ChunkSize) { |
| 301 | std::vector<ArrayRef<uint8_t>> Ret; |
| 302 | while (Arr.size() > ChunkSize) { |
| 303 | Ret.push_back(Arr.take_front(ChunkSize)); |
| 304 | Arr = Arr.drop_front(ChunkSize); |
| 305 | } |
| 306 | if (!Arr.empty()) |
| 307 | Ret.push_back(Arr); |
| 308 | return Ret; |
| 309 | } |
| 310 | |
Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 311 | // Computes a hash value of Data using a given hash function. |
| 312 | // In order to utilize multiple cores, we first split data into 1MB |
| 313 | // chunks, compute a hash for each chunk, and then compute a hash value |
| 314 | // of the hash values. |
George Rimar | 6c2949d | 2017-03-20 16:40:21 +0000 | [diff] [blame] | 315 | void BuildIdSection::computeHash( |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 316 | llvm::ArrayRef<uint8_t> Data, |
| 317 | std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) { |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 318 | std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024); |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 319 | std::vector<uint8_t> Hashes(Chunks.size() * HashSize); |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 320 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 321 | // Compute hash values. |
Rui Ueyama | 33d903d | 2017-05-10 20:02:19 +0000 | [diff] [blame] | 322 | parallelForEachN(0, Chunks.size(), [&](size_t I) { |
Rui Ueyama | 4995afd | 2017-03-22 23:03:35 +0000 | [diff] [blame] | 323 | HashFn(Hashes.data() + I * HashSize, Chunks[I]); |
| 324 | }); |
Rui Ueyama | 35e0075 | 2016-11-10 00:12:28 +0000 | [diff] [blame] | 325 | |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 326 | // Write to the final output buffer. |
| 327 | HashFn(HashBuf, Hashes); |
George Rimar | 364b59e2 | 2016-11-06 07:42:55 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Rui Ueyama | 732f4e2 | 2017-10-04 00:21:17 +0000 | [diff] [blame] | 330 | BssSection::BssSection(StringRef Name, uint64_t Size, uint32_t Alignment) |
| 331 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, Alignment, Name) { |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 +0000 | [diff] [blame] | 332 | this->Bss = true; |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 333 | if (OutputSection *Sec = getParent()) |
Rui Ueyama | 8befefb | 2017-10-07 00:58:34 +0000 | [diff] [blame] | 334 | Sec->Alignment = std::max(Sec->Alignment, Alignment); |
Rui Ueyama | 732f4e2 | 2017-10-04 00:21:17 +0000 | [diff] [blame] | 335 | this->Size = Size; |
George Rimar | 1ab9cf4 | 2017-03-17 10:14:53 +0000 | [diff] [blame] | 336 | } |
Peter Smith | ebfe994 | 2017-02-09 10:27:57 +0000 | [diff] [blame] | 337 | |
George Rimar | 6c2949d | 2017-03-20 16:40:21 +0000 | [diff] [blame] | 338 | void BuildIdSection::writeBuildId(ArrayRef<uint8_t> Buf) { |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 339 | switch (Config->BuildId) { |
| 340 | case BuildIdKind::Fast: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 341 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 342 | write64le(Dest, xxHash64(toStringRef(Arr))); |
| 343 | }); |
| 344 | break; |
| 345 | case BuildIdKind::Md5: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 346 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
Rui Ueyama | 28590b6 | 2016-11-23 18:11:38 +0000 | [diff] [blame] | 347 | memcpy(Dest, MD5::hash(Arr).data(), 16); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 348 | }); |
| 349 | break; |
| 350 | case BuildIdKind::Sha1: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 351 | computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) { |
Rui Ueyama | 28590b6 | 2016-11-23 18:11:38 +0000 | [diff] [blame] | 352 | memcpy(Dest, SHA1::hash(Arr).data(), 20); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 353 | }); |
| 354 | break; |
| 355 | case BuildIdKind::Uuid: |
Rui Ueyama | 88f0568 | 2017-10-27 01:25:29 +0000 | [diff] [blame] | 356 | if (auto EC = getRandomBytes(HashBuf, HashSize)) |
| 357 | error("entropy source failure: " + EC.message()); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 358 | break; |
| 359 | case BuildIdKind::Hexstring: |
Rui Ueyama | 2d98fea | 2016-11-22 01:31:32 +0000 | [diff] [blame] | 360 | memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size()); |
Rui Ueyama | c4030a1 | 2016-11-22 00:54:15 +0000 | [diff] [blame] | 361 | break; |
| 362 | default: |
| 363 | llvm_unreachable("unknown BuildIdKind"); |
| 364 | } |
Rui Ueyama | 6dc7fcb | 2016-11-01 20:28:21 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 367 | EhFrameSection::EhFrameSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 368 | : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {} |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 369 | |
| 370 | // Search for an existing CIE record or create a new one. |
| 371 | // CIE records from input object files are uniquified by their contents |
| 372 | // and where their relocations point to. |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 373 | template <class ELFT, class RelTy> |
| 374 | CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) { |
NAKAMURA Takumi | 169dbde | 2017-09-20 08:03:18 +0000 | [diff] [blame] | 375 | auto *Sec = cast<EhInputSection>(Cie.Sec); |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 376 | if (read32(Cie.data().data() + 4) != 0) |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 377 | fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame"); |
| 378 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 379 | Symbol *Personality = nullptr; |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 380 | unsigned FirstRelI = Cie.FirstRelocation; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 381 | if (FirstRelI != (unsigned)-1) |
| 382 | Personality = |
| 383 | &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]); |
| 384 | |
| 385 | // Search for an existing CIE by CIE contents/relocation target pair. |
George Rimar | 94444b9 | 2017-09-20 09:27:41 +0000 | [diff] [blame] | 386 | CieRecord *&Rec = CieMap[{Cie.data(), Personality}]; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 387 | |
| 388 | // If not found, create a new one. |
George Rimar | 94444b9 | 2017-09-20 09:27:41 +0000 | [diff] [blame] | 389 | if (!Rec) { |
| 390 | Rec = make<CieRecord>(); |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 391 | Rec->Cie = &Cie; |
| 392 | CieRecords.push_back(Rec); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 393 | } |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 394 | return Rec; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // There is one FDE per function. Returns true if a given FDE |
| 398 | // points to a live function. |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 399 | template <class ELFT, class RelTy> |
| 400 | bool EhFrameSection::isFdeLive(EhSectionPiece &Fde, ArrayRef<RelTy> Rels) { |
NAKAMURA Takumi | 169dbde | 2017-09-20 08:03:18 +0000 | [diff] [blame] | 401 | auto *Sec = cast<EhInputSection>(Fde.Sec); |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 402 | unsigned FirstRelI = Fde.FirstRelocation; |
Rui Ueyama | 56614e4 | 2017-09-12 23:43:45 +0000 | [diff] [blame] | 403 | |
| 404 | // An FDE should point to some function because FDEs are to describe |
| 405 | // functions. That's however not always the case due to an issue of |
| 406 | // ld.gold with -r. ld.gold may discard only functions and leave their |
| 407 | // corresponding FDEs, which results in creating bad .eh_frame sections. |
| 408 | // To deal with that, we ignore such FDEs. |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 409 | if (FirstRelI == (unsigned)-1) |
| 410 | return false; |
Rui Ueyama | 56614e4 | 2017-09-12 23:43:45 +0000 | [diff] [blame] | 411 | |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 412 | const RelTy &Rel = Rels[FirstRelI]; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 413 | Symbol &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel); |
George Rimar | d605f41 | 2017-10-26 09:13:19 +0000 | [diff] [blame] | 414 | |
| 415 | // FDEs for garbage-collected or merged-by-ICF sections are dead. |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 +0000 | [diff] [blame] | 416 | if (auto *D = dyn_cast<Defined>(&B)) |
Rafael Espindola | 13dbf94 | 2017-12-13 17:36:53 +0000 | [diff] [blame] | 417 | if (SectionBase *Sec = D->Section) |
| 418 | return Sec->Live; |
Rui Ueyama | 27a357c | 2017-09-18 19:15:54 +0000 | [diff] [blame] | 419 | return false; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // .eh_frame is a sequence of CIE or FDE records. In general, there |
| 423 | // is one CIE record per input object file which is followed by |
| 424 | // a list of FDEs. This function searches an existing CIE or create a new |
| 425 | // one and associates FDEs to the CIE. |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 426 | template <class ELFT, class RelTy> |
| 427 | void EhFrameSection::addSectionAux(EhInputSection *Sec, ArrayRef<RelTy> Rels) { |
Rafael Espindola | bd4d2ac | 2018-04-27 20:19:28 +0000 | [diff] [blame^] | 428 | OffsetToCie.clear(); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 429 | for (EhSectionPiece &Piece : Sec->Pieces) { |
| 430 | // The empty record is the end marker. |
Rui Ueyama | a6ff617 | 2017-09-18 23:07:09 +0000 | [diff] [blame] | 431 | if (Piece.Size == 4) |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 432 | return; |
| 433 | |
| 434 | size_t Offset = Piece.InputOff; |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 435 | uint32_t ID = read32(Piece.data().data() + 4); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 436 | if (ID == 0) { |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 437 | OffsetToCie[Offset] = addCie<ELFT>(Piece, Rels); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
| 441 | uint32_t CieOffset = Offset + 4 - ID; |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 442 | CieRecord *Rec = OffsetToCie[CieOffset]; |
| 443 | if (!Rec) |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 444 | fatal(toString(Sec) + ": invalid CIE reference"); |
| 445 | |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 446 | if (!isFdeLive<ELFT>(Piece, Rels)) |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 447 | continue; |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 448 | Rec->Fdes.push_back(&Piece); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 449 | NumFdes++; |
| 450 | } |
| 451 | } |
| 452 | |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 453 | template <class ELFT> void EhFrameSection::addSection(InputSectionBase *C) { |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 454 | auto *Sec = cast<EhInputSection>(C); |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 455 | Sec->Parent = this; |
Rui Ueyama | 8befefb | 2017-10-07 00:58:34 +0000 | [diff] [blame] | 456 | |
| 457 | Alignment = std::max(Alignment, Sec->Alignment); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 458 | Sections.push_back(Sec); |
Rui Ueyama | 8befefb | 2017-10-07 00:58:34 +0000 | [diff] [blame] | 459 | |
Petr Hosek | 7b79321 | 2017-03-10 20:00:42 +0000 | [diff] [blame] | 460 | for (auto *DS : Sec->DependentSections) |
| 461 | DependentSections.push_back(DS); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 462 | |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 463 | if (Sec->Pieces.empty()) |
| 464 | return; |
| 465 | |
Rui Ueyama | bfa8432 | 2017-10-26 22:30:25 +0000 | [diff] [blame] | 466 | if (Sec->AreRelocsRela) |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 467 | addSectionAux<ELFT>(Sec, Sec->template relas<ELFT>()); |
Rui Ueyama | faa3802 | 2017-09-19 20:28:03 +0000 | [diff] [blame] | 468 | else |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 469 | addSectionAux<ELFT>(Sec, Sec->template rels<ELFT>()); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 472 | static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { |
| 473 | memcpy(Buf, D.data(), D.size()); |
| 474 | |
Rui Ueyama | 076e2bb | 2017-10-27 03:13:09 +0000 | [diff] [blame] | 475 | size_t Aligned = alignTo(D.size(), Config->Wordsize); |
Andrew Ng | 6dee736 | 2017-09-07 08:43:56 +0000 | [diff] [blame] | 476 | |
| 477 | // Zero-clear trailing padding if it exists. |
| 478 | memset(Buf + D.size(), 0, Aligned - D.size()); |
| 479 | |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 480 | // Fix the size field. -4 since size does not include the size field itself. |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 481 | write32(Buf, Aligned - 4); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 484 | void EhFrameSection::finalizeContents() { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 485 | if (this->Size) |
| 486 | return; // Already finalized. |
| 487 | |
| 488 | size_t Off = 0; |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 489 | for (CieRecord *Rec : CieRecords) { |
| 490 | Rec->Cie->OutputOff = Off; |
| 491 | Off += alignTo(Rec->Cie->Size, Config->Wordsize); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 492 | |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 493 | for (EhSectionPiece *Fde : Rec->Fdes) { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 494 | Fde->OutputOff = Off; |
Rui Ueyama | a6ff617 | 2017-09-18 23:07:09 +0000 | [diff] [blame] | 495 | Off += alignTo(Fde->Size, Config->Wordsize); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
Rafael Espindola | a8a1a4f | 2017-05-02 15:45:31 +0000 | [diff] [blame] | 498 | |
| 499 | // The LSB standard does not allow a .eh_frame section with zero |
| 500 | // Call Frame Information records. Therefore add a CIE record length |
| 501 | // 0 as a terminator if this .eh_frame section is empty. |
| 502 | if (Off == 0) |
| 503 | Off = 4; |
| 504 | |
Rafael Espindola | b691ccf | 2017-02-28 18:55:08 +0000 | [diff] [blame] | 505 | this->Size = Off; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Rui Ueyama | c055225 | 2017-10-27 03:13:24 +0000 | [diff] [blame] | 508 | // Returns data for .eh_frame_hdr. .eh_frame_hdr is a binary search table |
| 509 | // to get an FDE from an address to which FDE is applied. This function |
| 510 | // returns a list of such pairs. |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 511 | std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const { |
Rui Ueyama | c055225 | 2017-10-27 03:13:24 +0000 | [diff] [blame] | 512 | uint8_t *Buf = getParent()->Loc + OutSecOff; |
| 513 | std::vector<FdeData> Ret; |
| 514 | |
| 515 | for (CieRecord *Rec : CieRecords) { |
Rui Ueyama | 8629752 | 2017-10-27 03:14:09 +0000 | [diff] [blame] | 516 | uint8_t Enc = getFdeEncoding(Rec->Cie); |
Rui Ueyama | c055225 | 2017-10-27 03:13:24 +0000 | [diff] [blame] | 517 | for (EhSectionPiece *Fde : Rec->Fdes) { |
| 518 | uint32_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); |
| 519 | uint32_t FdeVA = getParent()->Addr + Fde->OutputOff; |
| 520 | Ret.push_back({Pc, FdeVA}); |
| 521 | } |
| 522 | } |
| 523 | return Ret; |
| 524 | } |
| 525 | |
Rui Ueyama | 076e2bb | 2017-10-27 03:13:09 +0000 | [diff] [blame] | 526 | static uint64_t readFdeAddr(uint8_t *Buf, int Size) { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 527 | switch (Size) { |
| 528 | case DW_EH_PE_udata2: |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 529 | return read16(Buf); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 530 | case DW_EH_PE_udata4: |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 531 | return read32(Buf); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 532 | case DW_EH_PE_udata8: |
Fangrui Song | 0c48302 | 2018-03-09 18:03:22 +0000 | [diff] [blame] | 533 | return read64(Buf); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 534 | case DW_EH_PE_absptr: |
Rui Ueyama | 076e2bb | 2017-10-27 03:13:09 +0000 | [diff] [blame] | 535 | return readUint(Buf); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 536 | } |
| 537 | fatal("unknown FDE size encoding"); |
| 538 | } |
| 539 | |
| 540 | // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. |
| 541 | // We need it to create .eh_frame_hdr section. |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 542 | uint64_t EhFrameSection::getFdePc(uint8_t *Buf, size_t FdeOff, |
| 543 | uint8_t Enc) const { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 544 | // The starting address to which this FDE applies is |
| 545 | // stored at FDE + 8 byte. |
| 546 | size_t Off = FdeOff + 8; |
Rui Ueyama | 076e2bb | 2017-10-27 03:13:09 +0000 | [diff] [blame] | 547 | uint64_t Addr = readFdeAddr(Buf + Off, Enc & 0x7); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 548 | if ((Enc & 0x70) == DW_EH_PE_absptr) |
| 549 | return Addr; |
| 550 | if ((Enc & 0x70) == DW_EH_PE_pcrel) |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 551 | return Addr + getParent()->Addr + Off; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 552 | fatal("unknown FDE size relative encoding"); |
| 553 | } |
| 554 | |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 555 | void EhFrameSection::writeTo(uint8_t *Buf) { |
Rui Ueyama | c9a4d1c | 2017-10-10 03:58:18 +0000 | [diff] [blame] | 556 | // Write CIE and FDE records. |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 557 | for (CieRecord *Rec : CieRecords) { |
| 558 | size_t CieOffset = Rec->Cie->OutputOff; |
Rui Ueyama | 076e2bb | 2017-10-27 03:13:09 +0000 | [diff] [blame] | 559 | writeCieFde(Buf + CieOffset, Rec->Cie->data()); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 560 | |
Rui Ueyama | 74ea1f0 | 2017-09-19 21:31:57 +0000 | [diff] [blame] | 561 | for (EhSectionPiece *Fde : Rec->Fdes) { |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 562 | size_t Off = Fde->OutputOff; |
Rui Ueyama | 076e2bb | 2017-10-27 03:13:09 +0000 | [diff] [blame] | 563 | writeCieFde(Buf + Off, Fde->data()); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 564 | |
| 565 | // FDE's second word should have the offset to an associated CIE. |
| 566 | // Write it. |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 567 | write32(Buf + Off + 4, Off + 4 - CieOffset); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Rui Ueyama | c9a4d1c | 2017-10-10 03:58:18 +0000 | [diff] [blame] | 571 | // Apply relocations. .eh_frame section contents are not contiguous |
| 572 | // in the output buffer, but relocateAlloc() still works because |
| 573 | // getOffset() takes care of discontiguous section pieces. |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 574 | for (EhInputSection *S : Sections) |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 575 | S->relocateAlloc(Buf, nullptr); |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 578 | GotSection::GotSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 579 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
Zaara Syeda | c65ae14 | 2018-03-26 17:50:52 +0000 | [diff] [blame] | 580 | Target->GotEntrySize, ".got") { |
| 581 | // PPC64 saves the ElfSym::GlobalOffsetTable .TOC. as the first entry in the |
| 582 | // .got. If there are no references to .TOC. in the symbol table, |
| 583 | // ElfSym::GlobalOffsetTable will not be defined and we won't need to save |
| 584 | // .TOC. in the .got. When it is defined, we increase NumEntries by the number |
| 585 | // of entries used to emit ElfSym::GlobalOffsetTable. |
| 586 | if (ElfSym::GlobalOffsetTable && !Target->GotBaseSymInGotPlt) |
| 587 | NumEntries += Target->GotHeaderEntriesNum; |
| 588 | } |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 589 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 590 | void GotSection::addEntry(Symbol &Sym) { |
Zaara Syeda | c65ae14 | 2018-03-26 17:50:52 +0000 | [diff] [blame] | 591 | Sym.GotIndex = NumEntries; |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 592 | ++NumEntries; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 595 | bool GotSection::addDynTlsEntry(Symbol &Sym) { |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 596 | if (Sym.GlobalDynIndex != -1U) |
| 597 | return false; |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 598 | Sym.GlobalDynIndex = NumEntries; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 599 | // Global Dynamic TLS entries take two GOT slots. |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 600 | NumEntries += 2; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 601 | return true; |
| 602 | } |
| 603 | |
| 604 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 605 | // In total it takes two GOT slots. |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 606 | bool GotSection::addTlsIndex() { |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 607 | if (TlsIndexOff != uint32_t(-1)) |
| 608 | return false; |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 609 | TlsIndexOff = NumEntries * Config->Wordsize; |
Rafael Espindola | f1e2453 | 2016-11-29 03:45:36 +0000 | [diff] [blame] | 610 | NumEntries += 2; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 611 | return true; |
| 612 | } |
| 613 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 614 | uint64_t GotSection::getGlobalDynAddr(const Symbol &B) const { |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 615 | return this->getVA() + B.GlobalDynIndex * Config->Wordsize; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 618 | uint64_t GotSection::getGlobalDynOffset(const Symbol &B) const { |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 619 | return B.GlobalDynIndex * Config->Wordsize; |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Zaara Syeda | 52ed6eb | 2018-03-19 17:40:14 +0000 | [diff] [blame] | 622 | void GotSection::finalizeContents() { |
Zaara Syeda | c65ae14 | 2018-03-26 17:50:52 +0000 | [diff] [blame] | 623 | Size = NumEntries * Config->Wordsize; |
Zaara Syeda | 52ed6eb | 2018-03-19 17:40:14 +0000 | [diff] [blame] | 624 | } |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 625 | |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 626 | bool GotSection::empty() const { |
George Rimar | 19d6ce9 | 2017-09-25 09:46:33 +0000 | [diff] [blame] | 627 | // We need to emit a GOT even if it's empty if there's a relocation that is |
| 628 | // relative to GOT(such as GOTOFFREL) or there's a symbol that points to a GOT |
Peter Smith | 3d044f5 | 2018-03-19 06:52:51 +0000 | [diff] [blame] | 629 | // (i.e. _GLOBAL_OFFSET_TABLE_) that the target defines relative to the .got. |
| 630 | return NumEntries == 0 && !HasGotOffRel && |
| 631 | !(ElfSym::GlobalOffsetTable && !Target->GotBaseSymInGotPlt); |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Igor Kudrin | 202a9f6 | 2017-07-14 08:10:45 +0000 | [diff] [blame] | 634 | void GotSection::writeTo(uint8_t *Buf) { |
| 635 | // Buf points to the start of this section's buffer, |
| 636 | // whereas InputSectionBase::relocateAlloc() expects its argument |
| 637 | // to point to the start of the output section. |
Zaara Syeda | 52ed6eb | 2018-03-19 17:40:14 +0000 | [diff] [blame] | 638 | Target->writeGotHeader(Buf); |
| 639 | Buf += Target->GotHeaderEntriesNum * Target->GotEntrySize; |
Igor Kudrin | 202a9f6 | 2017-07-14 08:10:45 +0000 | [diff] [blame] | 640 | relocateAlloc(Buf - OutSecOff, Buf - OutSecOff + Size); |
| 641 | } |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 642 | |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 643 | MipsGotSection::MipsGotSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 644 | : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16, |
| 645 | ".got") {} |
Simon Atanasyan | 725dc14 | 2016-11-16 21:01:02 +0000 | [diff] [blame] | 646 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 647 | void MipsGotSection::addEntry(Symbol &Sym, int64_t Addend, RelExpr Expr) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 648 | // For "true" local symbols which can be referenced from the same module |
| 649 | // only compiler creates two instructions for address loading: |
| 650 | // |
| 651 | // lw $8, 0($gp) # R_MIPS_GOT16 |
| 652 | // addi $8, $8, 0 # R_MIPS_LO16 |
| 653 | // |
| 654 | // The first instruction loads high 16 bits of the symbol address while |
| 655 | // the second adds an offset. That allows to reduce number of required |
| 656 | // GOT entries because only one global offset table entry is necessary |
| 657 | // for every 64 KBytes of local data. So for local symbols we need to |
| 658 | // allocate number of GOT entries to hold all required "page" addresses. |
| 659 | // |
| 660 | // All global symbols (hidden and regular) considered by compiler uniformly. |
| 661 | // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation |
| 662 | // to load address of the symbol. So for each such symbol we need to |
| 663 | // allocate dedicated GOT entry to store its address. |
| 664 | // |
| 665 | // If a symbol is preemptible we need help of dynamic linker to get its |
| 666 | // final address. The corresponding GOT entries are allocated in the |
| 667 | // "global" part of GOT. Entries for non preemptible global symbol allocated |
| 668 | // in the "local" part of GOT. |
| 669 | // |
| 670 | // See "Global Offset Table" in Chapter 5: |
| 671 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 672 | if (Expr == R_MIPS_GOT_LOCAL_PAGE) { |
| 673 | // At this point we do not know final symbol value so to reduce number |
| 674 | // of allocated GOT entries do the following trick. Save all output |
| 675 | // sections referenced by GOT relocations. Then later in the `finalize` |
| 676 | // method calculate number of "pages" required to cover all saved output |
| 677 | // section and allocate appropriate number of GOT entries. |
Rafael Espindola | 23db636 | 2017-05-31 19:22:01 +0000 | [diff] [blame] | 678 | PageIndexMap.insert({Sym.getOutputSection(), 0}); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | if (Sym.isTls()) { |
| 682 | // GOT entries created for MIPS TLS relocations behave like |
| 683 | // almost GOT entries from other ABIs. They go to the end |
| 684 | // of the global offset table. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 685 | Sym.GotIndex = TlsEntries.size(); |
| 686 | TlsEntries.push_back(&Sym); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 687 | return; |
| 688 | } |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 689 | auto AddEntry = [&](Symbol &S, uint64_t A, GotEntries &Items) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 690 | if (S.isInGot() && !A) |
| 691 | return; |
| 692 | size_t NewIndex = Items.size(); |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 693 | if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second) |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 694 | return; |
| 695 | Items.emplace_back(&S, A); |
| 696 | if (!A) |
| 697 | S.GotIndex = NewIndex; |
| 698 | }; |
Rui Ueyama | ca05b6f | 2017-10-12 19:10:41 +0000 | [diff] [blame] | 699 | if (Sym.IsPreemptible) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 700 | // Ignore addends for preemptible symbols. They got single GOT entry anyway. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 701 | AddEntry(Sym, 0, GlobalEntries); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 702 | Sym.IsInGlobalMipsGot = true; |
| 703 | } else if (Expr == R_MIPS_GOT_OFF32) { |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 704 | AddEntry(Sym, Addend, LocalEntries32); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 705 | Sym.Is32BitMipsGot = true; |
| 706 | } else { |
| 707 | // Hold local GOT entries accessed via a 16-bit index separately. |
| 708 | // That allows to write them in the beginning of the GOT and keep |
| 709 | // their indexes as less as possible to escape relocation's overflow. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 710 | AddEntry(Sym, Addend, LocalEntries); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 714 | bool MipsGotSection::addDynTlsEntry(Symbol &Sym) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 715 | if (Sym.GlobalDynIndex != -1U) |
| 716 | return false; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 717 | Sym.GlobalDynIndex = TlsEntries.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 718 | // Global Dynamic TLS entries take two GOT slots. |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 719 | TlsEntries.push_back(nullptr); |
| 720 | TlsEntries.push_back(&Sym); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 721 | return true; |
| 722 | } |
| 723 | |
| 724 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 725 | // In total it takes two GOT slots. |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 726 | bool MipsGotSection::addTlsIndex() { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 727 | if (TlsIndexOff != uint32_t(-1)) |
| 728 | return false; |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 729 | TlsIndexOff = TlsEntries.size() * Config->Wordsize; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 730 | TlsEntries.push_back(nullptr); |
| 731 | TlsEntries.push_back(nullptr); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 735 | static uint64_t getMipsPageAddr(uint64_t Addr) { |
| 736 | return (Addr + 0x8000) & ~0xffff; |
| 737 | } |
| 738 | |
| 739 | static uint64_t getMipsPageCount(uint64_t Size) { |
| 740 | return (Size + 0xfffe) / 0xffff + 1; |
| 741 | } |
| 742 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 743 | uint64_t MipsGotSection::getPageEntryOffset(const Symbol &B, |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 744 | int64_t Addend) const { |
Rafael Espindola | 0dc2510 | 2017-05-31 19:26:37 +0000 | [diff] [blame] | 745 | const OutputSection *OutSec = B.getOutputSection(); |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 746 | uint64_t SecAddr = getMipsPageAddr(OutSec->Addr); |
| 747 | uint64_t SymAddr = getMipsPageAddr(B.getVA(Addend)); |
| 748 | uint64_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff; |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 749 | assert(Index < PageEntriesNum); |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 750 | return (HeaderEntriesNum + Index) * Config->Wordsize; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 753 | uint64_t MipsGotSection::getSymEntryOffset(const Symbol &B, |
| 754 | int64_t Addend) const { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 755 | // Calculate offset of the GOT entries block: TLS, global, local. |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 756 | uint64_t Index = HeaderEntriesNum + PageEntriesNum; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 757 | if (B.isTls()) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 758 | Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 759 | else if (B.IsInGlobalMipsGot) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 760 | Index += LocalEntries.size() + LocalEntries32.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 761 | else if (B.Is32BitMipsGot) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 762 | Index += LocalEntries.size(); |
| 763 | // Calculate offset of the GOT entry in the block. |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 764 | if (B.isInGot()) |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 765 | Index += B.GotIndex; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 766 | else { |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 767 | auto It = EntryIndexMap.find({&B, Addend}); |
| 768 | assert(It != EntryIndexMap.end()); |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 769 | Index += It->second; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 770 | } |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 771 | return Index * Config->Wordsize; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 772 | } |
| 773 | |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 774 | uint64_t MipsGotSection::getTlsOffset() const { |
| 775 | return (getLocalEntriesNum() + GlobalEntries.size()) * Config->Wordsize; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 778 | uint64_t MipsGotSection::getGlobalDynOffset(const Symbol &B) const { |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 779 | return B.GlobalDynIndex * Config->Wordsize; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 782 | const Symbol *MipsGotSection::getFirstGlobalEntry() const { |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 783 | return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 784 | } |
| 785 | |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 786 | unsigned MipsGotSection::getLocalEntriesNum() const { |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 787 | return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() + |
| 788 | LocalEntries32.size(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 789 | } |
| 790 | |
George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 791 | void MipsGotSection::finalizeContents() { updateAllocSize(); } |
Peter Smith | 1ec42d9 | 2017-03-08 14:06:24 +0000 | [diff] [blame] | 792 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 793 | bool MipsGotSection::updateAllocSize() { |
Simon Atanasyan | a0efc42 | 2016-11-29 10:23:50 +0000 | [diff] [blame] | 794 | PageEntriesNum = 0; |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 795 | for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) { |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 796 | // For each output section referenced by GOT page relocations calculate |
| 797 | // and save into PageIndexMap an upper bound of MIPS GOT entries required |
| 798 | // to store page addresses of local symbols. We assume the worst case - |
| 799 | // each 64kb page of the output section has at least one GOT relocation |
| 800 | // against it. And take in account the case when the section intersects |
| 801 | // page boundaries. |
| 802 | P.second = PageEntriesNum; |
| 803 | PageEntriesNum += getMipsPageCount(P.first->Size); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 804 | } |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 805 | Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) * |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 806 | Config->Wordsize; |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 807 | return false; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 808 | } |
| 809 | |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 810 | bool MipsGotSection::empty() const { |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 811 | // We add the .got section to the result for dynamic MIPS target because |
| 812 | // its address and properties are mentioned in the .dynamic section. |
| 813 | return Config->Relocatable; |
| 814 | } |
| 815 | |
George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 816 | uint64_t MipsGotSection::getGp() const { return ElfSym::MipsGp->getVA(0); } |
Simon Atanasyan | 8469b88 | 2016-11-23 22:22:16 +0000 | [diff] [blame] | 817 | |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 818 | void MipsGotSection::writeTo(uint8_t *Buf) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 819 | // Set the MSB of the second GOT slot. This is not required by any |
| 820 | // MIPS ABI documentation, though. |
| 821 | // |
| 822 | // There is a comment in glibc saying that "The MSB of got[1] of a |
| 823 | // gnu object is set to identify gnu objects," and in GNU gold it |
| 824 | // says "the second entry will be used by some runtime loaders". |
| 825 | // But how this field is being used is unclear. |
| 826 | // |
| 827 | // We are not really willing to mimic other linkers behaviors |
| 828 | // without understanding why they do that, but because all files |
| 829 | // generated by GNU tools have this special GOT value, and because |
| 830 | // we've been doing this for years, it is probably a safe bet to |
| 831 | // keep doing this for now. We really need to revisit this to see |
| 832 | // if we had to do this. |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 833 | writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1)); |
| 834 | Buf += HeaderEntriesNum * Config->Wordsize; |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 835 | // Write 'page address' entries to the local part of the GOT. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 836 | for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) { |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 837 | size_t PageCount = getMipsPageCount(L.first->Size); |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 838 | uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr); |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 839 | for (size_t PI = 0; PI < PageCount; ++PI) { |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 840 | uint8_t *Entry = Buf + (L.second + PI) * Config->Wordsize; |
| 841 | writeUint(Entry, FirstPageAddr + PI * 0x10000); |
Simon Atanasyan | 9fae3b8 | 2016-11-29 10:23:56 +0000 | [diff] [blame] | 842 | } |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 843 | } |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 844 | Buf += PageEntriesNum * Config->Wordsize; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 845 | auto AddEntry = [&](const GotEntry &SA) { |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 846 | uint8_t *Entry = Buf; |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 847 | Buf += Config->Wordsize; |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 848 | const Symbol *Sym = SA.first; |
| 849 | uint64_t VA = Sym->getVA(SA.second); |
Simon Atanasyan | 5a4e213 | 2017-11-08 23:34:34 +0000 | [diff] [blame] | 850 | if (Sym->StOther & STO_MIPS_MICROMIPS) |
| 851 | VA |= 1; |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 852 | writeUint(Entry, VA); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 853 | }; |
Simon Atanasyan | b8bfec6 | 2016-11-17 21:49:14 +0000 | [diff] [blame] | 854 | std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry); |
| 855 | std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry); |
| 856 | std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 857 | // Initialize TLS-related GOT entries. If the entry has a corresponding |
| 858 | // dynamic relocations, leave it initialized by zero. Write down adjusted |
| 859 | // TLS symbol's values otherwise. To calculate the adjustments use offsets |
| 860 | // for thread-local storage. |
| 861 | // https://www.linux-mips.org/wiki/NPTL |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 862 | if (TlsIndexOff != -1U && !Config->Pic) |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 863 | writeUint(Buf + TlsIndexOff, 1); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 864 | for (const Symbol *B : TlsEntries) { |
Rui Ueyama | ca05b6f | 2017-10-12 19:10:41 +0000 | [diff] [blame] | 865 | if (!B || B->IsPreemptible) |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 866 | continue; |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 867 | uint64_t VA = B->getVA(); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 868 | if (B->GotIndex != -1U) { |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 869 | uint8_t *Entry = Buf + B->GotIndex * Config->Wordsize; |
| 870 | writeUint(Entry, VA - 0x7000); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 871 | } |
| 872 | if (B->GlobalDynIndex != -1U) { |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 873 | uint8_t *Entry = Buf + B->GlobalDynIndex * Config->Wordsize; |
| 874 | writeUint(Entry, 1); |
| 875 | Entry += Config->Wordsize; |
| 876 | writeUint(Entry, VA - 0x8000); |
Eugene Leviant | ad4439e | 2016-11-11 11:33:32 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
George Rimar | 10f74fc | 2017-03-15 09:12:56 +0000 | [diff] [blame] | 881 | GotPltSection::GotPltSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 882 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 883 | Target->GotPltEntrySize, ".got.plt") {} |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 884 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 885 | void GotPltSection::addEntry(Symbol &Sym) { |
Rafael Espindola | f4a9d56 | 2018-04-26 16:09:30 +0000 | [diff] [blame] | 886 | assert(Sym.PltIndex == Entries.size()); |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 887 | Entries.push_back(&Sym); |
| 888 | } |
| 889 | |
George Rimar | 10f74fc | 2017-03-15 09:12:56 +0000 | [diff] [blame] | 890 | size_t GotPltSection::getSize() const { |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 891 | return (Target->GotPltHeaderEntriesNum + Entries.size()) * |
| 892 | Target->GotPltEntrySize; |
| 893 | } |
| 894 | |
George Rimar | 10f74fc | 2017-03-15 09:12:56 +0000 | [diff] [blame] | 895 | void GotPltSection::writeTo(uint8_t *Buf) { |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 896 | Target->writeGotPltHeader(Buf); |
| 897 | Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 898 | for (const Symbol *B : Entries) { |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 899 | Target->writeGotPlt(Buf, *B); |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 900 | Buf += Config->Wordsize; |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Peter Smith | 3d044f5 | 2018-03-19 06:52:51 +0000 | [diff] [blame] | 904 | bool GotPltSection::empty() const { |
| 905 | // We need to emit a GOT.PLT even if it's empty if there's a symbol that |
| 906 | // references the _GLOBAL_OFFSET_TABLE_ and the Target defines the symbol |
| 907 | // relative to the .got.plt section. |
| 908 | return Entries.empty() && |
| 909 | !(ElfSym::GlobalOffsetTable && Target->GotBaseSymInGotPlt); |
| 910 | } |
| 911 | |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 912 | // On ARM the IgotPltSection is part of the GotSection, on other Targets it is |
| 913 | // part of the .got.plt |
George Rimar | 10f74fc | 2017-03-15 09:12:56 +0000 | [diff] [blame] | 914 | IgotPltSection::IgotPltSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 915 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, |
| 916 | Target->GotPltEntrySize, |
| 917 | Config->EMachine == EM_ARM ? ".got" : ".got.plt") {} |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 918 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 919 | void IgotPltSection::addEntry(Symbol &Sym) { |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 920 | Sym.IsInIgot = true; |
Rafael Espindola | f4a9d56 | 2018-04-26 16:09:30 +0000 | [diff] [blame] | 921 | assert(Sym.PltIndex == Entries.size()); |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 922 | Entries.push_back(&Sym); |
| 923 | } |
| 924 | |
George Rimar | 10f74fc | 2017-03-15 09:12:56 +0000 | [diff] [blame] | 925 | size_t IgotPltSection::getSize() const { |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 926 | return Entries.size() * Target->GotPltEntrySize; |
| 927 | } |
| 928 | |
George Rimar | 10f74fc | 2017-03-15 09:12:56 +0000 | [diff] [blame] | 929 | void IgotPltSection::writeTo(uint8_t *Buf) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 930 | for (const Symbol *B : Entries) { |
Peter Smith | 4b36029 | 2016-12-09 09:59:54 +0000 | [diff] [blame] | 931 | Target->writeIgotPlt(Buf, *B); |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 932 | Buf += Config->Wordsize; |
Peter Smith | baffdb8 | 2016-12-08 12:58:55 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
George Rimar | 4964800 | 2017-03-15 09:32:36 +0000 | [diff] [blame] | 936 | StringTableSection::StringTableSection(StringRef Name, bool Dynamic) |
| 937 | : SyntheticSection(Dynamic ? (uint64_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name), |
Rafael Espindola | 1b36eea | 2017-02-15 00:23:09 +0000 | [diff] [blame] | 938 | Dynamic(Dynamic) { |
| 939 | // ELF string tables start with a NUL byte. |
| 940 | addString(""); |
| 941 | } |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 942 | |
| 943 | // Adds a string to the string table. If HashIt is true we hash and check for |
| 944 | // duplicates. It is optional because the name of global symbols are already |
| 945 | // uniqued and hashing them again has a big cost for a small value: uniquing |
| 946 | // them with some other string that happens to be the same. |
George Rimar | 4964800 | 2017-03-15 09:32:36 +0000 | [diff] [blame] | 947 | unsigned StringTableSection::addString(StringRef S, bool HashIt) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 948 | if (HashIt) { |
| 949 | auto R = StringMap.insert(std::make_pair(S, this->Size)); |
| 950 | if (!R.second) |
| 951 | return R.first->second; |
| 952 | } |
| 953 | unsigned Ret = this->Size; |
| 954 | this->Size = this->Size + S.size() + 1; |
| 955 | Strings.push_back(S); |
| 956 | return Ret; |
| 957 | } |
| 958 | |
George Rimar | 4964800 | 2017-03-15 09:32:36 +0000 | [diff] [blame] | 959 | void StringTableSection::writeTo(uint8_t *Buf) { |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 960 | for (StringRef S : Strings) { |
| 961 | memcpy(Buf, S.data(), S.size()); |
James Henderson | a5bc09a | 2017-08-04 09:07:55 +0000 | [diff] [blame] | 962 | Buf[S.size()] = '\0'; |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 963 | Buf += S.size() + 1; |
| 964 | } |
| 965 | } |
| 966 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 967 | // Returns the number of version definition entries. Because the first entry |
| 968 | // is for the version definition itself, it is the number of versioned symbols |
| 969 | // plus one. Note that we don't support multiple versions yet. |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 970 | static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } |
| 971 | |
| 972 | template <class ELFT> |
| 973 | DynamicSection<ELFT>::DynamicSection() |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 974 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, Config->Wordsize, |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 975 | ".dynamic") { |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 976 | this->Entsize = ELFT::Is64Bits ? 16 : 8; |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 977 | |
Petr Hosek | ffa786f | 2017-05-26 19:12:38 +0000 | [diff] [blame] | 978 | // .dynamic section is not writable on MIPS and on Fuchsia OS |
| 979 | // which passes -z rodynamic. |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 980 | // See "Special Section" in Chapter 4 in the following document: |
| 981 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Petr Hosek | ffa786f | 2017-05-26 19:12:38 +0000 | [diff] [blame] | 982 | if (Config->EMachine == EM_MIPS || Config->ZRodynamic) |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 983 | this->Flags = SHF_ALLOC; |
| 984 | |
Rui Ueyama | 22e5551 | 2017-12-15 19:39:59 +0000 | [diff] [blame] | 985 | // Add strings to .dynstr early so that .dynstr's size will be |
| 986 | // fixed early. |
| 987 | for (StringRef S : Config->FilterList) |
| 988 | addInt(DT_FILTER, InX::DynStrTab->addString(S)); |
| 989 | for (StringRef S : Config->AuxiliaryList) |
| 990 | addInt(DT_AUXILIARY, InX::DynStrTab->addString(S)); |
| 991 | |
| 992 | if (!Config->Rpath.empty()) |
| 993 | addInt(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
| 994 | InX::DynStrTab->addString(Config->Rpath)); |
| 995 | |
| 996 | for (InputFile *File : SharedFiles) { |
| 997 | SharedFile<ELFT> *F = cast<SharedFile<ELFT>>(File); |
| 998 | if (F->IsNeeded) |
| 999 | addInt(DT_NEEDED, InX::DynStrTab->addString(F->SoName)); |
| 1000 | } |
| 1001 | if (!Config->SoName.empty()) |
| 1002 | addInt(DT_SONAME, InX::DynStrTab->addString(Config->SoName)); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1005 | template <class ELFT> |
| 1006 | void DynamicSection<ELFT>::add(int32_t Tag, std::function<uint64_t()> Fn) { |
| 1007 | Entries.push_back({Tag, Fn}); |
| 1008 | } |
| 1009 | |
| 1010 | template <class ELFT> |
| 1011 | void DynamicSection<ELFT>::addInt(int32_t Tag, uint64_t Val) { |
| 1012 | Entries.push_back({Tag, [=] { return Val; }}); |
| 1013 | } |
| 1014 | |
| 1015 | template <class ELFT> |
| 1016 | void DynamicSection<ELFT>::addInSec(int32_t Tag, InputSection *Sec) { |
Rafael Espindola | 4f058a2 | 2018-03-24 00:35:11 +0000 | [diff] [blame] | 1017 | Entries.push_back({Tag, [=] { return Sec->getVA(0); }}); |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | template <class ELFT> |
Simon Atanasyan | 1ba1942 | 2018-04-13 08:15:01 +0000 | [diff] [blame] | 1021 | void DynamicSection<ELFT>::addInSecRelative(int32_t Tag, InputSection *Sec) { |
| 1022 | size_t TagOffset = Entries.size() * Entsize; |
| 1023 | Entries.push_back( |
| 1024 | {Tag, [=] { return Sec->getVA(0) - (getVA() + TagOffset); }}); |
| 1025 | } |
| 1026 | |
| 1027 | template <class ELFT> |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1028 | void DynamicSection<ELFT>::addOutSec(int32_t Tag, OutputSection *Sec) { |
| 1029 | Entries.push_back({Tag, [=] { return Sec->Addr; }}); |
| 1030 | } |
| 1031 | |
| 1032 | template <class ELFT> |
| 1033 | void DynamicSection<ELFT>::addSize(int32_t Tag, OutputSection *Sec) { |
| 1034 | Entries.push_back({Tag, [=] { return Sec->Size; }}); |
| 1035 | } |
| 1036 | |
| 1037 | template <class ELFT> |
| 1038 | void DynamicSection<ELFT>::addSym(int32_t Tag, Symbol *Sym) { |
| 1039 | Entries.push_back({Tag, [=] { return Sym->getVA(); }}); |
| 1040 | } |
| 1041 | |
Rui Ueyama | 22e5551 | 2017-12-15 19:39:59 +0000 | [diff] [blame] | 1042 | // Add remaining entries to complete .dynamic contents. |
| 1043 | template <class ELFT> void DynamicSection<ELFT>::finalizeContents() { |
| 1044 | if (this->Size) |
| 1045 | return; // Already finalized. |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1046 | |
| 1047 | // Set DT_FLAGS and DT_FLAGS_1. |
| 1048 | uint32_t DtFlags = 0; |
| 1049 | uint32_t DtFlags1 = 0; |
| 1050 | if (Config->Bsymbolic) |
| 1051 | DtFlags |= DF_SYMBOLIC; |
| 1052 | if (Config->ZNodelete) |
| 1053 | DtFlags1 |= DF_1_NODELETE; |
Davide Italiano | 7690721 | 2017-03-23 00:54:16 +0000 | [diff] [blame] | 1054 | if (Config->ZNodlopen) |
| 1055 | DtFlags1 |= DF_1_NOOPEN; |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1056 | if (Config->ZNow) { |
| 1057 | DtFlags |= DF_BIND_NOW; |
| 1058 | DtFlags1 |= DF_1_NOW; |
| 1059 | } |
| 1060 | if (Config->ZOrigin) { |
| 1061 | DtFlags |= DF_ORIGIN; |
| 1062 | DtFlags1 |= DF_1_ORIGIN; |
| 1063 | } |
Rui Ueyama | 7c18abf | 2018-03-01 22:56:52 +0000 | [diff] [blame] | 1064 | if (!Config->ZText) |
| 1065 | DtFlags |= DF_TEXTREL; |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (DtFlags) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1068 | addInt(DT_FLAGS, DtFlags); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1069 | if (DtFlags1) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1070 | addInt(DT_FLAGS_1, DtFlags1); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1071 | |
Petr Hosek | ffa786f | 2017-05-26 19:12:38 +0000 | [diff] [blame] | 1072 | // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We |
| 1073 | // need it for each process, so we don't write it for DSOs. The loader writes |
| 1074 | // the pointer into this entry. |
| 1075 | // |
| 1076 | // DT_DEBUG is the only .dynamic entry that needs to be written to. Some |
| 1077 | // systems (currently only Fuchsia OS) provide other means to give the |
| 1078 | // debugger this information. Such systems may choose make .dynamic read-only. |
| 1079 | // If the target is such a system (used -z rodynamic) don't write DT_DEBUG. |
| 1080 | if (!Config->Shared && !Config->Relocatable && !Config->ZRodynamic) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1081 | addInt(DT_DEBUG, 0); |
George Rimar | b4081bb | 2017-05-12 08:04:58 +0000 | [diff] [blame] | 1082 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 1083 | this->Link = InX::DynStrTab->getParent()->SectionIndex; |
George Rimar | 3208588 | 2017-12-30 08:40:45 +0000 | [diff] [blame] | 1084 | if (!InX::RelaDyn->empty()) { |
Rafael Espindola | 58946cd | 2017-12-10 19:44:42 +0000 | [diff] [blame] | 1085 | addInSec(InX::RelaDyn->DynamicTag, InX::RelaDyn); |
| 1086 | addSize(InX::RelaDyn->SizeDynamicTag, InX::RelaDyn->getParent()); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1087 | |
| 1088 | bool IsRela = Config->IsRela; |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1089 | addInt(IsRela ? DT_RELAENT : DT_RELENT, |
| 1090 | IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1091 | |
| 1092 | // MIPS dynamic loader does not support RELCOUNT tag. |
| 1093 | // The problem is in the tight relation between dynamic |
| 1094 | // relocations and GOT. So do not emit this tag on MIPS. |
| 1095 | if (Config->EMachine != EM_MIPS) { |
Rafael Espindola | 58946cd | 2017-12-10 19:44:42 +0000 | [diff] [blame] | 1096 | size_t NumRelativeRels = InX::RelaDyn->getRelativeRelocCount(); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1097 | if (Config->ZCombreloc && NumRelativeRels) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1098 | addInt(IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
George Rimar | edb6116 | 2017-12-31 07:42:54 +0000 | [diff] [blame] | 1101 | // .rel[a].plt section usually consists of two parts, containing plt and |
| 1102 | // iplt relocations. It is possible to have only iplt relocations in the |
| 1103 | // output. In that case RelaPlt is empty and have zero offset, the same offset |
| 1104 | // as RelaIplt have. And we still want to emit proper dynamic tags for that |
| 1105 | // case, so here we always use RelaPlt as marker for the begining of |
| 1106 | // .rel[a].plt section. |
| 1107 | if (InX::RelaPlt->getParent()->Live) { |
Rafael Espindola | 87e0dea | 2017-12-10 20:07:03 +0000 | [diff] [blame] | 1108 | addInSec(DT_JMPREL, InX::RelaPlt); |
| 1109 | addSize(DT_PLTRELSZ, InX::RelaPlt->getParent()); |
Rui Ueyama | 0cc1483 | 2017-06-28 17:05:39 +0000 | [diff] [blame] | 1110 | switch (Config->EMachine) { |
| 1111 | case EM_MIPS: |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1112 | addInSec(DT_MIPS_PLTGOT, InX::GotPlt); |
Rui Ueyama | 0cc1483 | 2017-06-28 17:05:39 +0000 | [diff] [blame] | 1113 | break; |
| 1114 | case EM_SPARCV9: |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1115 | addInSec(DT_PLTGOT, InX::Plt); |
Rui Ueyama | 0cc1483 | 2017-06-28 17:05:39 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | default: |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1118 | addInSec(DT_PLTGOT, InX::GotPlt); |
Rui Ueyama | 0cc1483 | 2017-06-28 17:05:39 +0000 | [diff] [blame] | 1119 | break; |
| 1120 | } |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1121 | addInt(DT_PLTREL, Config->IsRela ? DT_RELA : DT_REL); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1124 | addInSec(DT_SYMTAB, InX::DynSymTab); |
| 1125 | addInt(DT_SYMENT, sizeof(Elf_Sym)); |
| 1126 | addInSec(DT_STRTAB, InX::DynStrTab); |
| 1127 | addInt(DT_STRSZ, InX::DynStrTab->getSize()); |
George Rimar | 0a7412f | 2017-03-09 08:48:34 +0000 | [diff] [blame] | 1128 | if (!Config->ZText) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1129 | addInt(DT_TEXTREL, 0); |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 1130 | if (InX::GnuHashTab) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1131 | addInSec(DT_GNU_HASH, InX::GnuHashTab); |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 1132 | if (InX::HashTab) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1133 | addInSec(DT_HASH, InX::HashTab); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1134 | |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1135 | if (Out::PreinitArray) { |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1136 | addOutSec(DT_PREINIT_ARRAY, Out::PreinitArray); |
| 1137 | addSize(DT_PREINIT_ARRAYSZ, Out::PreinitArray); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1138 | } |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1139 | if (Out::InitArray) { |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1140 | addOutSec(DT_INIT_ARRAY, Out::InitArray); |
| 1141 | addSize(DT_INIT_ARRAYSZ, Out::InitArray); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1142 | } |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 1143 | if (Out::FiniArray) { |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1144 | addOutSec(DT_FINI_ARRAY, Out::FiniArray); |
| 1145 | addSize(DT_FINI_ARRAYSZ, Out::FiniArray); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1148 | if (Symbol *B = Symtab->find(Config->Init)) |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 +0000 | [diff] [blame] | 1149 | if (B->isDefined()) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1150 | addSym(DT_INIT, B); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1151 | if (Symbol *B = Symtab->find(Config->Fini)) |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 +0000 | [diff] [blame] | 1152 | if (B->isDefined()) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1153 | addSym(DT_FINI, B); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1154 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1155 | bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0; |
| 1156 | if (HasVerNeed || In<ELFT>::VerDef) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1157 | addInSec(DT_VERSYM, In<ELFT>::VerSym); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 1158 | if (In<ELFT>::VerDef) { |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1159 | addInSec(DT_VERDEF, In<ELFT>::VerDef); |
| 1160 | addInt(DT_VERDEFNUM, getVerDefNum()); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1161 | } |
| 1162 | if (HasVerNeed) { |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1163 | addInSec(DT_VERNEED, In<ELFT>::VerNeed); |
| 1164 | addInt(DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | if (Config->EMachine == EM_MIPS) { |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1168 | addInt(DT_MIPS_RLD_VERSION, 1); |
| 1169 | addInt(DT_MIPS_FLAGS, RHF_NOTPOT); |
| 1170 | addInt(DT_MIPS_BASE_ADDRESS, Target->getImageBase()); |
| 1171 | addInt(DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()); |
James Henderson | f70c5be | 2017-11-22 12:04:21 +0000 | [diff] [blame] | 1172 | |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1173 | add(DT_MIPS_LOCAL_GOTNO, [] { return InX::MipsGot->getLocalEntriesNum(); }); |
James Henderson | f70c5be | 2017-11-22 12:04:21 +0000 | [diff] [blame] | 1174 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1175 | if (const Symbol *B = InX::MipsGot->getFirstGlobalEntry()) |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1176 | addInt(DT_MIPS_GOTSYM, B->DynsymIndex); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1177 | else |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1178 | addInt(DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()); |
| 1179 | addInSec(DT_PLTGOT, InX::MipsGot); |
Simon Atanasyan | 1ba1942 | 2018-04-13 08:15:01 +0000 | [diff] [blame] | 1180 | if (InX::MipsRldMap) { |
| 1181 | if (!Config->Pie) |
| 1182 | addInSec(DT_MIPS_RLD_MAP, InX::MipsRldMap); |
| 1183 | // Store the offset to the .rld_map section |
| 1184 | // relative to the address of the tag. |
| 1185 | addInSecRelative(DT_MIPS_RLD_MAP_REL, InX::MipsRldMap); |
| 1186 | } |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1189 | addInt(DT_NULL, 0); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1190 | |
George Rimar | 8f3a6c8 | 2017-10-06 10:06:13 +0000 | [diff] [blame] | 1191 | getParent()->Link = this->Link; |
| 1192 | this->Size = Entries.size() * this->Entsize; |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1196 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 1197 | |
Rui Ueyama | 15475e9 | 2017-11-24 02:15:51 +0000 | [diff] [blame] | 1198 | for (std::pair<int32_t, std::function<uint64_t()>> &KV : Entries) { |
| 1199 | P->d_tag = KV.first; |
| 1200 | P->d_un.d_val = KV.second(); |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 1201 | ++P; |
| 1202 | } |
| 1203 | } |
| 1204 | |
George Rimar | 97def8c | 2017-03-17 12:07:44 +0000 | [diff] [blame] | 1205 | uint64_t DynamicReloc::getOffset() const { |
Rafael Espindola | 4f058a2 | 2018-03-24 00:35:11 +0000 | [diff] [blame] | 1206 | return InputSec->getVA(OffsetInSec); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Alexander Richardson | 048e250 | 2018-02-19 11:00:15 +0000 | [diff] [blame] | 1209 | int64_t DynamicReloc::computeAddend() const { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1210 | if (UseSymVA) |
George Rimar | f64618a | 2017-03-17 11:56:54 +0000 | [diff] [blame] | 1211 | return Sym->getVA(Addend); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1212 | return Addend; |
| 1213 | } |
| 1214 | |
George Rimar | 97def8c | 2017-03-17 12:07:44 +0000 | [diff] [blame] | 1215 | uint32_t DynamicReloc::getSymIndex() const { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1216 | if (Sym && !UseSymVA) |
| 1217 | return Sym->DynsymIndex; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1221 | RelocationBaseSection::RelocationBaseSection(StringRef Name, uint32_t Type, |
| 1222 | int32_t DynamicTag, |
| 1223 | int32_t SizeDynamicTag) |
| 1224 | : SyntheticSection(SHF_ALLOC, Type, Config->Wordsize, Name), |
| 1225 | DynamicTag(DynamicTag), SizeDynamicTag(SizeDynamicTag) {} |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1226 | |
Rafael Espindola | d49866e | 2018-02-13 16:06:11 +0000 | [diff] [blame] | 1227 | void RelocationBaseSection::addReloc(RelType DynType, InputSectionBase *IS, |
Rafael Espindola | 35cf8bb | 2018-02-13 16:03:52 +0000 | [diff] [blame] | 1228 | uint64_t OffsetInSec, Symbol *Sym) { |
| 1229 | addReloc({DynType, IS, OffsetInSec, false, Sym, 0}); |
| 1230 | } |
| 1231 | |
Rafael Espindola | d49866e | 2018-02-13 16:06:11 +0000 | [diff] [blame] | 1232 | void RelocationBaseSection::addReloc(RelType DynType, |
Rafael Espindola | 73584cb | 2018-01-05 20:08:38 +0000 | [diff] [blame] | 1233 | InputSectionBase *InputSec, |
Rafael Espindola | b960325 | 2018-02-16 16:53:04 +0000 | [diff] [blame] | 1234 | uint64_t OffsetInSec, Symbol *Sym, |
| 1235 | int64_t Addend, RelExpr Expr, |
Rafael Espindola | 73584cb | 2018-01-05 20:08:38 +0000 | [diff] [blame] | 1236 | RelType Type) { |
Alexander Richardson | cfb6093 | 2018-02-16 10:01:17 +0000 | [diff] [blame] | 1237 | // Write the addends to the relocated address if required. We skip |
| 1238 | // it if the written value would be zero. |
Rafael Espindola | b960325 | 2018-02-16 16:53:04 +0000 | [diff] [blame] | 1239 | if (Config->WriteAddends && (Expr != R_ADDEND || Addend != 0)) |
Rafael Espindola | 73584cb | 2018-01-05 20:08:38 +0000 | [diff] [blame] | 1240 | InputSec->Relocations.push_back({Expr, Type, OffsetInSec, Addend, Sym}); |
Rafael Espindola | b960325 | 2018-02-16 16:53:04 +0000 | [diff] [blame] | 1241 | addReloc({DynType, InputSec, OffsetInSec, Expr != R_ADDEND, Sym, Addend}); |
Rafael Espindola | 73584cb | 2018-01-05 20:08:38 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1244 | void RelocationBaseSection::addReloc(const DynamicReloc &Reloc) { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1245 | if (Reloc.Type == Target->RelativeRel) |
| 1246 | ++NumRelativeRelocs; |
| 1247 | Relocs.push_back(Reloc); |
| 1248 | } |
| 1249 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1250 | void RelocationBaseSection::finalizeContents() { |
| 1251 | // If all relocations are R_*_RELATIVE they don't refer to any |
| 1252 | // dynamic symbol and we don't need a dynamic symbol table. If that |
| 1253 | // is the case, just use 0 as the link. |
Rafael Espindola | 6d907105c | 2017-12-10 19:28:32 +0000 | [diff] [blame] | 1254 | Link = InX::DynSymTab ? InX::DynSymTab->getParent()->SectionIndex : 0; |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1255 | |
| 1256 | // Set required output section properties. |
Rafael Espindola | 6d907105c | 2017-12-10 19:28:32 +0000 | [diff] [blame] | 1257 | getParent()->Link = Link; |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | template <class ELFT> |
| 1261 | static void encodeDynamicReloc(typename ELFT::Rela *P, |
| 1262 | const DynamicReloc &Rel) { |
| 1263 | if (Config->IsRela) |
Alexander Richardson | 048e250 | 2018-02-19 11:00:15 +0000 | [diff] [blame] | 1264 | P->r_addend = Rel.computeAddend(); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1265 | P->r_offset = Rel.getOffset(); |
| 1266 | if (Config->EMachine == EM_MIPS && Rel.getInputSec() == InX::MipsGot) |
| 1267 | // The MIPS GOT section contains dynamic relocations that correspond to TLS |
| 1268 | // entries. These entries are placed after the global and local sections of |
| 1269 | // the GOT. At the point when we create these relocations, the size of the |
| 1270 | // global and local sections is unknown, so the offset that we store in the |
| 1271 | // TLS entry's DynamicReloc is relative to the start of the TLS section of |
| 1272 | // the GOT, rather than being relative to the start of the GOT. This line of |
| 1273 | // code adds the size of the global and local sections to the virtual |
| 1274 | // address computed by getOffset() in order to adjust it into the TLS |
| 1275 | // section. |
| 1276 | P->r_offset += InX::MipsGot->getTlsOffset(); |
| 1277 | P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL); |
| 1278 | } |
| 1279 | |
| 1280 | template <class ELFT> |
| 1281 | RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort) |
| 1282 | : RelocationBaseSection(Name, Config->IsRela ? SHT_RELA : SHT_REL, |
| 1283 | Config->IsRela ? DT_RELA : DT_REL, |
| 1284 | Config->IsRela ? DT_RELASZ : DT_RELSZ), |
| 1285 | Sort(Sort) { |
| 1286 | this->Entsize = Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 1287 | } |
| 1288 | |
Rafael Espindola | 7ce2b4c | 2018-02-01 03:17:12 +0000 | [diff] [blame] | 1289 | static bool compRelocations(const DynamicReloc &A, const DynamicReloc &B) { |
| 1290 | bool AIsRel = A.Type == Target->RelativeRel; |
| 1291 | bool BIsRel = B.Type == Target->RelativeRel; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1292 | if (AIsRel != BIsRel) |
| 1293 | return AIsRel; |
Rafael Espindola | 7ce2b4c | 2018-02-01 03:17:12 +0000 | [diff] [blame] | 1294 | return A.getSymIndex() < B.getSymIndex(); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 7ce2b4c | 2018-02-01 03:17:12 +0000 | [diff] [blame] | 1298 | if (Sort) |
| 1299 | std::stable_sort(Relocs.begin(), Relocs.end(), compRelocations); |
| 1300 | |
George Rimar | 97def8c | 2017-03-17 12:07:44 +0000 | [diff] [blame] | 1301 | for (const DynamicReloc &Rel : Relocs) { |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1302 | encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel); |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 1303 | Buf += Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1304 | } |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { |
| 1308 | return this->Entsize * Relocs.size(); |
| 1309 | } |
| 1310 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1311 | template <class ELFT> |
| 1312 | AndroidPackedRelocationSection<ELFT>::AndroidPackedRelocationSection( |
| 1313 | StringRef Name) |
| 1314 | : RelocationBaseSection( |
| 1315 | Name, Config->IsRela ? SHT_ANDROID_RELA : SHT_ANDROID_REL, |
| 1316 | Config->IsRela ? DT_ANDROID_RELA : DT_ANDROID_REL, |
| 1317 | Config->IsRela ? DT_ANDROID_RELASZ : DT_ANDROID_RELSZ) { |
| 1318 | this->Entsize = 1; |
| 1319 | } |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1320 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1321 | template <class ELFT> |
| 1322 | bool AndroidPackedRelocationSection<ELFT>::updateAllocSize() { |
| 1323 | // This function computes the contents of an Android-format packed relocation |
| 1324 | // section. |
| 1325 | // |
| 1326 | // This format compresses relocations by using relocation groups to factor out |
| 1327 | // fields that are common between relocations and storing deltas from previous |
| 1328 | // relocations in SLEB128 format (which has a short representation for small |
| 1329 | // numbers). A good example of a relocation type with common fields is |
| 1330 | // R_*_RELATIVE, which is normally used to represent function pointers in |
| 1331 | // vtables. In the REL format, each relative relocation has the same r_info |
| 1332 | // field, and is only different from other relative relocations in terms of |
| 1333 | // the r_offset field. By sorting relocations by offset, grouping them by |
| 1334 | // r_info and representing each relocation with only the delta from the |
| 1335 | // previous offset, each 8-byte relocation can be compressed to as little as 1 |
| 1336 | // byte (or less with run-length encoding). This relocation packer was able to |
| 1337 | // reduce the size of the relocation section in an Android Chromium DSO from |
| 1338 | // 2,911,184 bytes to 174,693 bytes, or 6% of the original size. |
| 1339 | // |
| 1340 | // A relocation section consists of a header containing the literal bytes |
| 1341 | // 'APS2' followed by a sequence of SLEB128-encoded integers. The first two |
| 1342 | // elements are the total number of relocations in the section and an initial |
| 1343 | // r_offset value. The remaining elements define a sequence of relocation |
| 1344 | // groups. Each relocation group starts with a header consisting of the |
| 1345 | // following elements: |
| 1346 | // |
| 1347 | // - the number of relocations in the relocation group |
| 1348 | // - flags for the relocation group |
| 1349 | // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is set) the r_offset delta |
| 1350 | // for each relocation in the group. |
| 1351 | // - (if RELOCATION_GROUPED_BY_INFO_FLAG is set) the value of the r_info |
| 1352 | // field for each relocation in the group. |
| 1353 | // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG and |
| 1354 | // RELOCATION_GROUPED_BY_ADDEND_FLAG are set) the r_addend delta for |
| 1355 | // each relocation in the group. |
| 1356 | // |
| 1357 | // Following the relocation group header are descriptions of each of the |
| 1358 | // relocations in the group. They consist of the following elements: |
| 1359 | // |
| 1360 | // - (if RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG is not set) the r_offset |
| 1361 | // delta for this relocation. |
| 1362 | // - (if RELOCATION_GROUPED_BY_INFO_FLAG is not set) the value of the r_info |
| 1363 | // field for this relocation. |
| 1364 | // - (if RELOCATION_GROUP_HAS_ADDEND_FLAG is set and |
| 1365 | // RELOCATION_GROUPED_BY_ADDEND_FLAG is not set) the r_addend delta for |
| 1366 | // this relocation. |
| 1367 | |
| 1368 | size_t OldSize = RelocData.size(); |
| 1369 | |
| 1370 | RelocData = {'A', 'P', 'S', '2'}; |
| 1371 | raw_svector_ostream OS(RelocData); |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1372 | auto Add = [&](int64_t V) { encodeSLEB128(V, OS); }; |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1373 | |
| 1374 | // The format header includes the number of relocations and the initial |
| 1375 | // offset (we set this to zero because the first relocation group will |
| 1376 | // perform the initial adjustment). |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1377 | Add(Relocs.size()); |
| 1378 | Add(0); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1379 | |
| 1380 | std::vector<Elf_Rela> Relatives, NonRelatives; |
| 1381 | |
| 1382 | for (const DynamicReloc &Rel : Relocs) { |
| 1383 | Elf_Rela R; |
| 1384 | encodeDynamicReloc<ELFT>(&R, Rel); |
| 1385 | |
| 1386 | if (R.getType(Config->IsMips64EL) == Target->RelativeRel) |
| 1387 | Relatives.push_back(R); |
| 1388 | else |
| 1389 | NonRelatives.push_back(R); |
| 1390 | } |
| 1391 | |
George Rimar | de83cbf | 2018-04-24 09:55:39 +0000 | [diff] [blame] | 1392 | llvm::sort(Relatives.begin(), Relatives.end(), |
| 1393 | [](const Elf_Rel &A, const Elf_Rel &B) { |
| 1394 | return A.r_offset < B.r_offset; |
| 1395 | }); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1396 | |
| 1397 | // Try to find groups of relative relocations which are spaced one word |
| 1398 | // apart from one another. These generally correspond to vtable entries. The |
| 1399 | // format allows these groups to be encoded using a sort of run-length |
| 1400 | // encoding, but each group will cost 7 bytes in addition to the offset from |
| 1401 | // the previous group, so it is only profitable to do this for groups of |
| 1402 | // size 8 or larger. |
| 1403 | std::vector<Elf_Rela> UngroupedRelatives; |
| 1404 | std::vector<std::vector<Elf_Rela>> RelativeGroups; |
| 1405 | for (auto I = Relatives.begin(), E = Relatives.end(); I != E;) { |
| 1406 | std::vector<Elf_Rela> Group; |
| 1407 | do { |
| 1408 | Group.push_back(*I++); |
| 1409 | } while (I != E && (I - 1)->r_offset + Config->Wordsize == I->r_offset); |
| 1410 | |
| 1411 | if (Group.size() < 8) |
| 1412 | UngroupedRelatives.insert(UngroupedRelatives.end(), Group.begin(), |
| 1413 | Group.end()); |
| 1414 | else |
| 1415 | RelativeGroups.emplace_back(std::move(Group)); |
| 1416 | } |
| 1417 | |
| 1418 | unsigned HasAddendIfRela = |
| 1419 | Config->IsRela ? RELOCATION_GROUP_HAS_ADDEND_FLAG : 0; |
| 1420 | |
| 1421 | uint64_t Offset = 0; |
| 1422 | uint64_t Addend = 0; |
| 1423 | |
| 1424 | // Emit the run-length encoding for the groups of adjacent relative |
| 1425 | // relocations. Each group is represented using two groups in the packed |
| 1426 | // format. The first is used to set the current offset to the start of the |
| 1427 | // group (and also encodes the first relocation), and the second encodes the |
| 1428 | // remaining relocations. |
| 1429 | for (std::vector<Elf_Rela> &G : RelativeGroups) { |
| 1430 | // The first relocation in the group. |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1431 | Add(1); |
| 1432 | Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | |
| 1433 | RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela); |
| 1434 | Add(G[0].r_offset - Offset); |
| 1435 | Add(Target->RelativeRel); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1436 | if (Config->IsRela) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1437 | Add(G[0].r_addend - Addend); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1438 | Addend = G[0].r_addend; |
| 1439 | } |
| 1440 | |
| 1441 | // The remaining relocations. |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1442 | Add(G.size() - 1); |
| 1443 | Add(RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG | |
| 1444 | RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela); |
| 1445 | Add(Config->Wordsize); |
| 1446 | Add(Target->RelativeRel); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1447 | if (Config->IsRela) { |
| 1448 | for (auto I = G.begin() + 1, E = G.end(); I != E; ++I) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1449 | Add(I->r_addend - Addend); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1450 | Addend = I->r_addend; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | Offset = G.back().r_offset; |
| 1455 | } |
| 1456 | |
| 1457 | // Now the ungrouped relatives. |
| 1458 | if (!UngroupedRelatives.empty()) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1459 | Add(UngroupedRelatives.size()); |
| 1460 | Add(RELOCATION_GROUPED_BY_INFO_FLAG | HasAddendIfRela); |
| 1461 | Add(Target->RelativeRel); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1462 | for (Elf_Rela &R : UngroupedRelatives) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1463 | Add(R.r_offset - Offset); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1464 | Offset = R.r_offset; |
| 1465 | if (Config->IsRela) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1466 | Add(R.r_addend - Addend); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1467 | Addend = R.r_addend; |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | // Finally the non-relative relocations. |
George Rimar | de83cbf | 2018-04-24 09:55:39 +0000 | [diff] [blame] | 1473 | llvm::sort(NonRelatives.begin(), NonRelatives.end(), |
| 1474 | [](const Elf_Rela &A, const Elf_Rela &B) { |
| 1475 | return A.r_offset < B.r_offset; |
| 1476 | }); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1477 | if (!NonRelatives.empty()) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1478 | Add(NonRelatives.size()); |
| 1479 | Add(HasAddendIfRela); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1480 | for (Elf_Rela &R : NonRelatives) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1481 | Add(R.r_offset - Offset); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1482 | Offset = R.r_offset; |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1483 | Add(R.r_info); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1484 | if (Config->IsRela) { |
Rui Ueyama | 04c821c | 2017-12-08 02:20:50 +0000 | [diff] [blame] | 1485 | Add(R.r_addend - Addend); |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 1486 | Addend = R.r_addend; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // Returns whether the section size changed. We need to keep recomputing both |
| 1492 | // section layout and the contents of this section until the size converges |
| 1493 | // because changing this section's size can affect section layout, which in |
| 1494 | // turn can affect the sizes of the LEB-encoded integers stored in this |
| 1495 | // section. |
| 1496 | return RelocData.size() != OldSize; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1499 | SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &StrTabSec) |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 1500 | : SyntheticSection(StrTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0, |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1501 | StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 1502 | Config->Wordsize, |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1503 | StrTabSec.isDynamic() ? ".dynsym" : ".symtab"), |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1504 | StrTabSec(StrTabSec) {} |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1505 | |
| 1506 | // Orders symbols according to their positions in the GOT, |
| 1507 | // in compliance with MIPS ABI rules. |
| 1508 | // See "Global Offset Table" in Chapter 5 in the following document |
| 1509 | // for detailed description: |
| 1510 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Simon Atanasyan | 8c75311 | 2017-03-19 19:32:51 +0000 | [diff] [blame] | 1511 | static bool sortMipsSymbols(const SymbolTableEntry &L, |
| 1512 | const SymbolTableEntry &R) { |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1513 | // Sort entries related to non-local preemptible symbols by GOT indexes. |
| 1514 | // All other entries go to the first part of GOT in arbitrary order. |
Zachary Turner | a104d55 | 2017-11-03 22:33:49 +0000 | [diff] [blame] | 1515 | bool LIsInLocalGot = !L.Sym->IsInGlobalMipsGot; |
| 1516 | bool RIsInLocalGot = !R.Sym->IsInGlobalMipsGot; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1517 | if (LIsInLocalGot || RIsInLocalGot) |
| 1518 | return !RIsInLocalGot; |
Zachary Turner | a104d55 | 2017-11-03 22:33:49 +0000 | [diff] [blame] | 1519 | return L.Sym->GotIndex < R.Sym->GotIndex; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1522 | void SymbolTableBaseSection::finalizeContents() { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 1523 | getParent()->Link = StrTabSec.getParent()->SectionIndex; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1524 | |
George Rimar | 55d7178 | 2018-04-04 12:36:21 +0000 | [diff] [blame] | 1525 | if (this->Type != SHT_DYNSYM) |
| 1526 | return; |
| 1527 | |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1528 | // If it is a .dynsym, there should be no local symbols, but we need |
| 1529 | // to do a few things for the dynamic linker. |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1530 | |
George Rimar | 55d7178 | 2018-04-04 12:36:21 +0000 | [diff] [blame] | 1531 | // Section's Info field has the index of the first non-local symbol. |
| 1532 | // Because the first symbol entry is a null entry, 1 is the first. |
| 1533 | getParent()->Info = 1; |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1534 | |
George Rimar | 55d7178 | 2018-04-04 12:36:21 +0000 | [diff] [blame] | 1535 | if (InX::GnuHashTab) { |
| 1536 | // NB: It also sorts Symbols to meet the GNU hash table requirements. |
| 1537 | InX::GnuHashTab->addSymbols(Symbols); |
| 1538 | } else if (Config->EMachine == EM_MIPS) { |
| 1539 | std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols); |
Peter Smith | 5586543 | 2017-02-20 11:12:33 +0000 | [diff] [blame] | 1540 | } |
George Rimar | 55d7178 | 2018-04-04 12:36:21 +0000 | [diff] [blame] | 1541 | |
| 1542 | size_t I = 0; |
| 1543 | for (const SymbolTableEntry &S : Symbols) |
| 1544 | S.Sym->DynsymIndex = ++I; |
Peter Smith | 1ec42d9 | 2017-03-08 14:06:24 +0000 | [diff] [blame] | 1545 | } |
Peter Smith | 5586543 | 2017-02-20 11:12:33 +0000 | [diff] [blame] | 1546 | |
George Rimar | 0d6f30e | 2017-10-18 13:06:18 +0000 | [diff] [blame] | 1547 | // The ELF spec requires that all local symbols precede global symbols, so we |
| 1548 | // sort symbol entries in this function. (For .dynsym, we don't do that because |
| 1549 | // symbols for dynamic linking are inherently all globals.) |
George Rimar | c552619 | 2018-04-11 09:24:27 +0000 | [diff] [blame] | 1550 | // |
| 1551 | // Aside from above, we put local symbols in groups starting with the STT_FILE |
| 1552 | // symbol. That is convenient for purpose of identifying where are local symbols |
| 1553 | // coming from. |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1554 | void SymbolTableBaseSection::postThunkContents() { |
Peter Smith | 1ec42d9 | 2017-03-08 14:06:24 +0000 | [diff] [blame] | 1555 | if (this->Type == SHT_DYNSYM) |
| 1556 | return; |
George Rimar | c552619 | 2018-04-11 09:24:27 +0000 | [diff] [blame] | 1557 | |
| 1558 | // Move all local symbols before global symbols. |
| 1559 | auto E = std::stable_partition( |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1560 | Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) { |
Zachary Turner | a104d55 | 2017-11-03 22:33:49 +0000 | [diff] [blame] | 1561 | return S.Sym->isLocal() || S.Sym->computeBinding() == STB_LOCAL; |
Rui Ueyama | 6e96734 | 2017-02-28 03:29:12 +0000 | [diff] [blame] | 1562 | }); |
George Rimar | c552619 | 2018-04-11 09:24:27 +0000 | [diff] [blame] | 1563 | size_t NumLocals = E - Symbols.begin(); |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 1564 | getParent()->Info = NumLocals + 1; |
George Rimar | c552619 | 2018-04-11 09:24:27 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Assign the growing unique ID for each local symbol's file. |
| 1567 | DenseMap<InputFile *, unsigned> FileIDs; |
| 1568 | for (auto I = Symbols.begin(); I != E; ++I) |
| 1569 | FileIDs.insert({I->Sym->File, FileIDs.size()}); |
| 1570 | |
| 1571 | // Sort the local symbols to group them by file. We do not need to care about |
| 1572 | // the STT_FILE symbols, they are already naturally placed first in each group. |
| 1573 | // That happens because STT_FILE is always the first symbol in the object and |
| 1574 | // hence precede all other local symbols we add for a file. |
| 1575 | std::stable_sort(Symbols.begin(), E, |
| 1576 | [&](const SymbolTableEntry &L, const SymbolTableEntry &R) { |
| 1577 | return FileIDs[L.Sym->File] < FileIDs[R.Sym->File]; |
| 1578 | }); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1581 | void SymbolTableBaseSection::addSymbol(Symbol *B) { |
Rui Ueyama | b8dcdb5 | 2017-02-28 04:20:16 +0000 | [diff] [blame] | 1582 | // Adding a local symbol to a .dynsym is a bug. |
| 1583 | assert(this->Type != SHT_DYNSYM || !B->isLocal()); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1584 | |
Rui Ueyama | b8dcdb5 | 2017-02-28 04:20:16 +0000 | [diff] [blame] | 1585 | bool HashIt = B->isLocal(); |
| 1586 | Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)}); |
George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1589 | size_t SymbolTableBaseSection::getSymbolIndex(Symbol *Sym) { |
George Rimar | 5d6efd1 | 2017-09-27 09:08:53 +0000 | [diff] [blame] | 1590 | // Initializes symbol lookup tables lazily. This is used only |
| 1591 | // for -r or -emit-relocs. |
| 1592 | llvm::call_once(OnceFlag, [&] { |
| 1593 | SymbolIndexMap.reserve(Symbols.size()); |
| 1594 | size_t I = 0; |
| 1595 | for (const SymbolTableEntry &E : Symbols) { |
Zachary Turner | a104d55 | 2017-11-03 22:33:49 +0000 | [diff] [blame] | 1596 | if (E.Sym->Type == STT_SECTION) |
| 1597 | SectionIndexMap[E.Sym->getOutputSection()] = ++I; |
George Rimar | 5d6efd1 | 2017-09-27 09:08:53 +0000 | [diff] [blame] | 1598 | else |
Zachary Turner | a104d55 | 2017-11-03 22:33:49 +0000 | [diff] [blame] | 1599 | SymbolIndexMap[E.Sym] = ++I; |
George Rimar | 5d6efd1 | 2017-09-27 09:08:53 +0000 | [diff] [blame] | 1600 | } |
Rafael Espindola | 08d6a3f | 2017-02-11 01:40:49 +0000 | [diff] [blame] | 1601 | }); |
George Rimar | 5d6efd1 | 2017-09-27 09:08:53 +0000 | [diff] [blame] | 1602 | |
| 1603 | // Section symbols are mapped based on their output sections |
| 1604 | // to maintain their semantics. |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1605 | if (Sym->Type == STT_SECTION) |
| 1606 | return SectionIndexMap.lookup(Sym->getOutputSection()); |
| 1607 | return SymbolIndexMap.lookup(Sym); |
George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1610 | template <class ELFT> |
| 1611 | SymbolTableSection<ELFT>::SymbolTableSection(StringTableSection &StrTabSec) |
| 1612 | : SymbolTableBaseSection(StrTabSec) { |
| 1613 | this->Entsize = sizeof(Elf_Sym); |
| 1614 | } |
| 1615 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1616 | // Write the internal symbol table contents to the output symbol table. |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1617 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1618 | // The first entry is a null entry as per the ELF spec. |
George Rimar | 2727ce2 | 2017-10-06 09:56:24 +0000 | [diff] [blame] | 1619 | memset(Buf, 0, sizeof(Elf_Sym)); |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1620 | Buf += sizeof(Elf_Sym); |
| 1621 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1622 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
George Rimar | 190bac5 | 2017-01-23 14:07:23 +0000 | [diff] [blame] | 1623 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1624 | for (SymbolTableEntry &Ent : Symbols) { |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1625 | Symbol *Sym = Ent.Sym; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1626 | |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1627 | // Set st_info and st_other. |
George Rimar | 2727ce2 | 2017-10-06 09:56:24 +0000 | [diff] [blame] | 1628 | ESym->st_other = 0; |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1629 | if (Sym->isLocal()) { |
| 1630 | ESym->setBindingAndType(STB_LOCAL, Sym->Type); |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1631 | } else { |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1632 | ESym->setBindingAndType(Sym->computeBinding(), Sym->Type); |
| 1633 | ESym->setVisibility(Sym->Visibility); |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | ESym->st_name = Ent.StrTabOffset; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1637 | |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1638 | // Set a section index. |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 +0000 | [diff] [blame] | 1639 | BssSection *CommonSec = nullptr; |
| 1640 | if (!Config->DefineCommon) |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 +0000 | [diff] [blame] | 1641 | if (auto *D = dyn_cast<Defined>(Sym)) |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 +0000 | [diff] [blame] | 1642 | CommonSec = dyn_cast_or_null<BssSection>(D->Section); |
| 1643 | if (CommonSec) |
| 1644 | ESym->st_shndx = SHN_COMMON; |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 +0000 | [diff] [blame] | 1645 | else if (Sym->NeedsPltAddr) |
| 1646 | ESym->st_shndx = SHN_UNDEF; |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 +0000 | [diff] [blame] | 1647 | else if (const OutputSection *OutSec = Sym->getOutputSection()) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1648 | ESym->st_shndx = OutSec->SectionIndex; |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 +0000 | [diff] [blame] | 1649 | else if (isa<Defined>(Sym)) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1650 | ESym->st_shndx = SHN_ABS; |
George Rimar | 2727ce2 | 2017-10-06 09:56:24 +0000 | [diff] [blame] | 1651 | else |
| 1652 | ESym->st_shndx = SHN_UNDEF; |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1653 | |
George Rimar | dbe843d | 2017-06-28 09:51:33 +0000 | [diff] [blame] | 1654 | // Copy symbol size if it is a defined symbol. st_size is not significant |
| 1655 | // for undefined symbols, so whether copying it or not is up to us if that's |
| 1656 | // the case. We'll leave it as zero because by not setting a value, we can |
| 1657 | // get the exact same outputs for two sets of input files that differ only |
| 1658 | // in undefined symbol size in DSOs. |
George Rimar | 2727ce2 | 2017-10-06 09:56:24 +0000 | [diff] [blame] | 1659 | if (ESym->st_shndx == SHN_UNDEF) |
| 1660 | ESym->st_size = 0; |
| 1661 | else |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1662 | ESym->st_size = Sym->getSize(); |
George Rimar | dbe843d | 2017-06-28 09:51:33 +0000 | [diff] [blame] | 1663 | |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1664 | // st_value is usually an address of a symbol, but that has a |
| 1665 | // special meaining for uninstantiated common symbols (this can |
| 1666 | // occur if -r is given). |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 +0000 | [diff] [blame] | 1667 | if (CommonSec) |
| 1668 | ESym->st_value = CommonSec->Alignment; |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1669 | else |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1670 | ESym->st_value = Sym->getVA(); |
Rui Ueyama | 1b00318 | 2017-02-28 19:22:09 +0000 | [diff] [blame] | 1671 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1672 | ++ESym; |
| 1673 | } |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1674 | |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1675 | // On MIPS we need to mark symbol which has a PLT entry and requires |
| 1676 | // pointer equality by STO_MIPS_PLT flag. That is necessary to help |
| 1677 | // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. |
| 1678 | // https://sourceware.org/ml/binutils/2008-07/txt00000.txt |
| 1679 | if (Config->EMachine == EM_MIPS) { |
| 1680 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
| 1681 | |
| 1682 | for (SymbolTableEntry &Ent : Symbols) { |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1683 | Symbol *Sym = Ent.Sym; |
| 1684 | if (Sym->isInPlt() && Sym->NeedsPltAddr) |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1685 | ESym->st_other |= STO_MIPS_PLT; |
Simon Atanasyan | cfa8aa7e | 2017-11-13 22:40:36 +0000 | [diff] [blame] | 1686 | if (isMicroMips()) { |
| 1687 | // Set STO_MIPS_MICROMIPS flag and less-significant bit for |
| 1688 | // defined microMIPS symbols and shared symbols with PLT record. |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 +0000 | [diff] [blame] | 1689 | if (Sym->isDefined() && (Sym->StOther & STO_MIPS_MICROMIPS)) { |
Simon Atanasyan | cfa8aa7e | 2017-11-13 22:40:36 +0000 | [diff] [blame] | 1690 | if (StrTabSec.isDynamic()) |
| 1691 | ESym->st_value |= 1; |
| 1692 | ESym->st_other |= STO_MIPS_MICROMIPS; |
| 1693 | } |
| 1694 | } |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1695 | if (Config->Relocatable) |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 +0000 | [diff] [blame] | 1696 | if (auto *D = dyn_cast<Defined>(Sym)) |
Rui Ueyama | 7957b08 | 2017-11-07 00:04:22 +0000 | [diff] [blame] | 1697 | if (isMipsPIC<ELFT>(D)) |
Rui Ueyama | 1f03253 | 2017-02-28 01:56:36 +0000 | [diff] [blame] | 1698 | ESym->st_other |= STO_MIPS_PIC; |
| 1699 | ++ESym; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1700 | } |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
Rui Ueyama | e412063 | 2017-02-28 22:05:13 +0000 | [diff] [blame] | 1704 | // .hash and .gnu.hash sections contain on-disk hash tables that map |
| 1705 | // symbol names to their dynamic symbol table indices. Their purpose |
| 1706 | // is to help the dynamic linker resolve symbols quickly. If ELF files |
| 1707 | // don't have them, the dynamic linker has to do linear search on all |
| 1708 | // dynamic symbols, which makes programs slower. Therefore, a .hash |
| 1709 | // section is added to a DSO by default. A .gnu.hash is added if you |
| 1710 | // give the -hash-style=gnu or -hash-style=both option. |
| 1711 | // |
| 1712 | // The Unix semantics of resolving dynamic symbols is somewhat expensive. |
| 1713 | // Each ELF file has a list of DSOs that the ELF file depends on and a |
| 1714 | // list of dynamic symbols that need to be resolved from any of the |
| 1715 | // DSOs. That means resolving all dynamic symbols takes O(m)*O(n) |
| 1716 | // where m is the number of DSOs and n is the number of dynamic |
| 1717 | // symbols. For modern large programs, both m and n are large. So |
| 1718 | // making each step faster by using hash tables substiantially |
| 1719 | // improves time to load programs. |
| 1720 | // |
| 1721 | // (Note that this is not the only way to design the shared library. |
| 1722 | // For instance, the Windows DLL takes a different approach. On |
| 1723 | // Windows, each dynamic symbol has a name of DLL from which the symbol |
| 1724 | // has to be resolved. That makes the cost of symbol resolution O(n). |
| 1725 | // This disables some hacky techniques you can use on Unix such as |
| 1726 | // LD_PRELOAD, but this is arguably better semantics than the Unix ones.) |
| 1727 | // |
| 1728 | // Due to historical reasons, we have two different hash tables, .hash |
| 1729 | // and .gnu.hash. They are for the same purpose, and .gnu.hash is a new |
| 1730 | // and better version of .hash. .hash is just an on-disk hash table, but |
| 1731 | // .gnu.hash has a bloom filter in addition to a hash table to skip |
| 1732 | // DSOs very quickly. If you are sure that your dynamic linker knows |
| 1733 | // about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a |
| 1734 | // safe bet is to specify -hash-style=both for backward compatibilty. |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1735 | GnuHashTableSection::GnuHashTableSection() |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1736 | : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, Config->Wordsize, ".gnu.hash") { |
| 1737 | } |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1738 | |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1739 | void GnuHashTableSection::finalizeContents() { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 1740 | getParent()->Link = InX::DynSymTab->getParent()->SectionIndex; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1741 | |
Rui Ueyama | 517366c | 2018-01-19 23:54:31 +0000 | [diff] [blame] | 1742 | // Computes bloom filter size in word size. We want to allocate 12 |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1743 | // bits for each symbol. It must be a power of two. |
Rui Ueyama | 517366c | 2018-01-19 23:54:31 +0000 | [diff] [blame] | 1744 | if (Symbols.empty()) { |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1745 | MaskWords = 1; |
Rui Ueyama | 517366c | 2018-01-19 23:54:31 +0000 | [diff] [blame] | 1746 | } else { |
| 1747 | uint64_t NumBits = Symbols.size() * 12; |
| 1748 | MaskWords = NextPowerOf2(NumBits / (Config->Wordsize * 8)); |
| 1749 | } |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1750 | |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1751 | Size = 16; // Header |
| 1752 | Size += Config->Wordsize * MaskWords; // Bloom filter |
| 1753 | Size += NBuckets * 4; // Hash buckets |
| 1754 | Size += Symbols.size() * 4; // Hash values |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1757 | void GnuHashTableSection::writeTo(uint8_t *Buf) { |
Rui Ueyama | c4e50bf | 2017-12-06 00:49:48 +0000 | [diff] [blame] | 1758 | // The output buffer is not guaranteed to be zero-cleared because we pre- |
| 1759 | // fill executable sections with trap instructions. This is a precaution |
| 1760 | // for that case, which happens only when -no-rosegment is given. |
| 1761 | memset(Buf, 0, Size); |
| 1762 | |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1763 | // Write a header. |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 1764 | write32(Buf, NBuckets); |
| 1765 | write32(Buf + 4, InX::DynSymTab->getNumSymbols() - Symbols.size()); |
| 1766 | write32(Buf + 8, MaskWords); |
Rui Ueyama | ba32e73 | 2018-03-19 23:04:13 +0000 | [diff] [blame] | 1767 | write32(Buf + 12, Shift2); |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1768 | Buf += 16; |
| 1769 | |
Rui Ueyama | 7986b45 | 2017-03-01 18:09:09 +0000 | [diff] [blame] | 1770 | // Write a bloom filter and a hash table. |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1771 | writeBloomFilter(Buf); |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1772 | Buf += Config->Wordsize * MaskWords; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1773 | writeHashTable(Buf); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Rui Ueyama | 7986b45 | 2017-03-01 18:09:09 +0000 | [diff] [blame] | 1776 | // This function writes a 2-bit bloom filter. This bloom filter alone |
| 1777 | // usually filters out 80% or more of all symbol lookups [1]. |
| 1778 | // The dynamic linker uses the hash table only when a symbol is not |
| 1779 | // filtered out by a bloom filter. |
| 1780 | // |
| 1781 | // [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2), |
| 1782 | // p.9, https://www.akkadia.org/drepper/dsohowto.pdf |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1783 | void GnuHashTableSection::writeBloomFilter(uint8_t *Buf) { |
Rui Ueyama | 2f8af79 | 2018-01-20 00:14:16 +0000 | [diff] [blame] | 1784 | unsigned C = Config->Is64 ? 64 : 32; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1785 | for (const Entry &Sym : Symbols) { |
| 1786 | size_t I = (Sym.Hash / C) & (MaskWords - 1); |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1787 | uint64_t Val = readUint(Buf + I * Config->Wordsize); |
| 1788 | Val |= uint64_t(1) << (Sym.Hash % C); |
Rui Ueyama | ba32e73 | 2018-03-19 23:04:13 +0000 | [diff] [blame] | 1789 | Val |= uint64_t(1) << ((Sym.Hash >> Shift2) % C); |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1790 | writeUint(Buf + I * Config->Wordsize, Val); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1791 | } |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1794 | void GnuHashTableSection::writeHashTable(uint8_t *Buf) { |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1795 | uint32_t *Buckets = reinterpret_cast<uint32_t *>(Buf); |
Rafael Espindola | f9f2abe | 2017-12-07 18:51:19 +0000 | [diff] [blame] | 1796 | uint32_t OldBucket = -1; |
George Rimar | 5f73bc9 | 2017-03-29 15:23:28 +0000 | [diff] [blame] | 1797 | uint32_t *Values = Buckets + NBuckets; |
Rafael Espindola | 50ca10b | 2017-12-07 18:46:03 +0000 | [diff] [blame] | 1798 | for (auto I = Symbols.begin(), E = Symbols.end(); I != E; ++I) { |
Rafael Espindola | d182aaa | 2017-12-07 18:59:29 +0000 | [diff] [blame] | 1799 | // Write a hash value. It represents a sequence of chains that share the |
| 1800 | // same hash modulo value. The last element of each chain is terminated by |
| 1801 | // LSB 1. |
Rafael Espindola | 50ca10b | 2017-12-07 18:46:03 +0000 | [diff] [blame] | 1802 | uint32_t Hash = I->Hash; |
| 1803 | bool IsLastInChain = (I + 1) == E || I->BucketIdx != (I + 1)->BucketIdx; |
| 1804 | Hash = IsLastInChain ? Hash | 1 : Hash & ~1; |
| 1805 | write32(Values++, Hash); |
Rafael Espindola | d182aaa | 2017-12-07 18:59:29 +0000 | [diff] [blame] | 1806 | |
| 1807 | if (I->BucketIdx == OldBucket) |
| 1808 | continue; |
| 1809 | // Write a hash bucket. Hash buckets contain indices in the following hash |
| 1810 | // value table. |
| 1811 | write32(Buckets + I->BucketIdx, I->Sym->DynsymIndex); |
| 1812 | OldBucket = I->BucketIdx; |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1813 | } |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | static uint32_t hashGnu(StringRef Name) { |
| 1817 | uint32_t H = 5381; |
| 1818 | for (uint8_t C : Name) |
| 1819 | H = (H << 5) + H + C; |
| 1820 | return H; |
| 1821 | } |
| 1822 | |
| 1823 | // Add symbols to this symbol hash table. Note that this function |
| 1824 | // destructively sort a given vector -- which is needed because |
| 1825 | // GNU-style hash table places some sorting requirements. |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 1826 | void GnuHashTableSection::addSymbols(std::vector<SymbolTableEntry> &V) { |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1827 | // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce |
| 1828 | // its type correctly. |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1829 | std::vector<SymbolTableEntry>::iterator Mid = |
| 1830 | std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) { |
Peter Collingbourne | b472aa0 | 2017-11-06 04:39:07 +0000 | [diff] [blame] | 1831 | return !S.Sym->isDefined(); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1832 | }); |
George Rimar | 2ab9362 | 2018-03-14 08:52:39 +0000 | [diff] [blame] | 1833 | |
Rui Ueyama | 4e6b822 | 2018-03-14 19:01:00 +0000 | [diff] [blame] | 1834 | // We chose load factor 4 for the on-disk hash table. For each hash |
| 1835 | // collision, the dynamic linker will compare a uint32_t hash value. |
| 1836 | // Since the integer comparison is quite fast, we believe we can |
| 1837 | // make the load factor even larger. 4 is just a conservative choice. |
| 1838 | // |
| 1839 | // Note that we don't want to create a zero-sized hash table because |
| 1840 | // Android loader as of 2018 doesn't like a .gnu.hash containing such |
| 1841 | // table. If that's the case, we create a hash table with one unused |
| 1842 | // dummy slot. |
George Rimar | 2ab9362 | 2018-03-14 08:52:39 +0000 | [diff] [blame] | 1843 | NBuckets = std::max<size_t>((V.end() - Mid) / 4, 1); |
| 1844 | |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1845 | if (Mid == V.end()) |
| 1846 | return; |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1847 | |
Rui Ueyama | 2278826 | 2017-12-02 00:37:13 +0000 | [diff] [blame] | 1848 | for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) { |
| 1849 | Symbol *B = Ent.Sym; |
| 1850 | uint32_t Hash = hashGnu(B->getName()); |
| 1851 | uint32_t BucketIdx = Hash % NBuckets; |
| 1852 | Symbols.push_back({B, Ent.StrTabOffset, Hash, BucketIdx}); |
| 1853 | } |
| 1854 | |
| 1855 | std::stable_sort( |
| 1856 | Symbols.begin(), Symbols.end(), |
| 1857 | [](const Entry &L, const Entry &R) { return L.BucketIdx < R.BucketIdx; }); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1858 | |
| 1859 | V.erase(Mid, V.end()); |
Rui Ueyama | e13373b | 2017-03-01 02:51:42 +0000 | [diff] [blame] | 1860 | for (const Entry &Ent : Symbols) |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1861 | V.push_back({Ent.Sym, Ent.StrTabOffset}); |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 1864 | HashTableSection::HashTableSection() |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1865 | : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") { |
| 1866 | this->Entsize = 4; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 1869 | void HashTableSection::finalizeContents() { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 1870 | getParent()->Link = InX::DynSymTab->getParent()->SectionIndex; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1871 | |
George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 1872 | unsigned NumEntries = 2; // nbucket and nchain. |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 1873 | NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries. |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Create as many buckets as there are symbols. |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 1876 | NumEntries += InX::DynSymTab->getNumSymbols(); |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1877 | this->Size = NumEntries * 4; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 1880 | void HashTableSection::writeTo(uint8_t *Buf) { |
Shoaib Meenai | d79bbf4 | 2018-01-11 06:57:01 +0000 | [diff] [blame] | 1881 | // See comment in GnuHashTableSection::writeTo. |
| 1882 | memset(Buf, 0, Size); |
| 1883 | |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 1884 | unsigned NumSymbols = InX::DynSymTab->getNumSymbols(); |
Rui Ueyama | 6b776ad | 2017-02-28 05:53:47 +0000 | [diff] [blame] | 1885 | |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 1886 | uint32_t *P = reinterpret_cast<uint32_t *>(Buf); |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 1887 | write32(P++, NumSymbols); // nbucket |
| 1888 | write32(P++, NumSymbols); // nchain |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1889 | |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 1890 | uint32_t *Buckets = P; |
| 1891 | uint32_t *Chains = P + NumSymbols; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1892 | |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 1893 | for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) { |
Rui Ueyama | 4888224 | 2017-11-04 00:31:04 +0000 | [diff] [blame] | 1894 | Symbol *Sym = S.Sym; |
| 1895 | StringRef Name = Sym->getName(); |
| 1896 | unsigned I = Sym->DynsymIndex; |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1897 | uint32_t Hash = hashSysV(Name) % NumSymbols; |
| 1898 | Chains[I] = Buckets[Hash]; |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 1899 | write32(Buckets + Hash, I); |
Eugene Leviant | b96e809 | 2016-11-18 09:06:47 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Rui Ueyama | e2dfdbf | 2018-01-12 22:29:29 +0000 | [diff] [blame] | 1903 | PltSection::PltSection(bool IsIplt) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 1904 | : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"), |
Rui Ueyama | e2dfdbf | 2018-01-12 22:29:29 +0000 | [diff] [blame] | 1905 | HeaderSize(IsIplt ? 0 : Target->PltHeaderSize), IsIplt(IsIplt) { |
Rui Ueyama | 0cc1483 | 2017-06-28 17:05:39 +0000 | [diff] [blame] | 1906 | // The PLT needs to be writable on SPARC as the dynamic linker will |
| 1907 | // modify the instructions in the PLT entries. |
| 1908 | if (Config->EMachine == EM_SPARCV9) |
| 1909 | this->Flags |= SHF_WRITE; |
| 1910 | } |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1911 | |
George Rimar | dfc020e | 2017-03-17 11:01:57 +0000 | [diff] [blame] | 1912 | void PltSection::writeTo(uint8_t *Buf) { |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1913 | // At beginning of PLT but not the IPLT, we have code to call the dynamic |
| 1914 | // linker to resolve dynsyms at runtime. Write such code. |
George Rimar | 9fc2c64 | 2018-01-12 09:35:57 +0000 | [diff] [blame] | 1915 | if (!IsIplt) |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1916 | Target->writePltHeader(Buf); |
| 1917 | size_t Off = HeaderSize; |
| 1918 | // The IPlt is immediately after the Plt, account for this in RelOff |
| 1919 | unsigned PltOff = getPltRelocOff(); |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1920 | |
| 1921 | for (auto &I : Entries) { |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1922 | const Symbol *B = I.first; |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1923 | unsigned RelOff = I.second + PltOff; |
George Rimar | 4670bb0 | 2017-03-16 12:58:11 +0000 | [diff] [blame] | 1924 | uint64_t Got = B->getGotPltVA(); |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1925 | uint64_t Plt = this->getVA() + Off; |
| 1926 | Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); |
| 1927 | Off += Target->PltEntrySize; |
| 1928 | } |
| 1929 | } |
| 1930 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 1931 | template <class ELFT> void PltSection::addEntry(Symbol &Sym) { |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1932 | Sym.PltIndex = Entries.size(); |
Rafael Espindola | 87e0dea | 2017-12-10 20:07:03 +0000 | [diff] [blame] | 1933 | RelocationBaseSection *PltRelocSection = InX::RelaPlt; |
George Rimar | 9fc2c64 | 2018-01-12 09:35:57 +0000 | [diff] [blame] | 1934 | if (IsIplt) { |
Rafael Espindola | 87e0dea | 2017-12-10 20:07:03 +0000 | [diff] [blame] | 1935 | PltRelocSection = InX::RelaIplt; |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1936 | Sym.IsInIplt = true; |
| 1937 | } |
Rafael Espindola | 87e0dea | 2017-12-10 20:07:03 +0000 | [diff] [blame] | 1938 | unsigned RelOff = |
| 1939 | static_cast<RelocationSection<ELFT> *>(PltRelocSection)->getRelocOffset(); |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1940 | Entries.push_back(std::make_pair(&Sym, RelOff)); |
| 1941 | } |
| 1942 | |
George Rimar | dfc020e | 2017-03-17 11:01:57 +0000 | [diff] [blame] | 1943 | size_t PltSection::getSize() const { |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1944 | return HeaderSize + Entries.size() * Target->PltEntrySize; |
Eugene Leviant | ff23d3e | 2016-11-18 14:35:03 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1947 | // Some architectures such as additional symbols in the PLT section. For |
| 1948 | // example ARM uses mapping symbols to aid disassembly |
George Rimar | dfc020e | 2017-03-17 11:01:57 +0000 | [diff] [blame] | 1949 | void PltSection::addSymbols() { |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1950 | // The PLT may have symbols defined for the Header, the IPLT has no header |
George Rimar | 9fc2c64 | 2018-01-12 09:35:57 +0000 | [diff] [blame] | 1951 | if (!IsIplt) |
Rafael Espindola | 1037eef | 2017-12-19 23:59:35 +0000 | [diff] [blame] | 1952 | Target->addPltHeaderSymbols(*this); |
Peter Smith | f09245a | 2017-02-09 10:56:15 +0000 | [diff] [blame] | 1953 | size_t Off = HeaderSize; |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1954 | for (size_t I = 0; I < Entries.size(); ++I) { |
Rafael Espindola | 1037eef | 2017-12-19 23:59:35 +0000 | [diff] [blame] | 1955 | Target->addPltSymbols(*this, Off); |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1956 | Off += Target->PltEntrySize; |
| 1957 | } |
| 1958 | } |
| 1959 | |
George Rimar | dfc020e | 2017-03-17 11:01:57 +0000 | [diff] [blame] | 1960 | unsigned PltSection::getPltRelocOff() const { |
George Rimar | 9fc2c64 | 2018-01-12 09:35:57 +0000 | [diff] [blame] | 1961 | return IsIplt ? InX::Plt->getSize() : 0; |
Peter Smith | 9694376 | 2017-01-25 10:31:16 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 1964 | // The string hash function for .gdb_index. |
| 1965 | static uint32_t computeGdbHash(StringRef S) { |
| 1966 | uint32_t H = 0; |
| 1967 | for (uint8_t C : S) |
| 1968 | H = H * 67 + tolower(C) - 113; |
| 1969 | return H; |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 1972 | static std::vector<GdbIndexChunk::CuEntry> readCuList(DWARFContext &Dwarf) { |
| 1973 | std::vector<GdbIndexChunk::CuEntry> Ret; |
| 1974 | for (std::unique_ptr<DWARFCompileUnit> &Cu : Dwarf.compile_units()) |
| 1975 | Ret.push_back({Cu->getOffset(), Cu->getLength() + 4}); |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1976 | return Ret; |
| 1977 | } |
| 1978 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 1979 | static std::vector<GdbIndexChunk::AddressEntry> |
| 1980 | readAddressAreas(DWARFContext &Dwarf, InputSection *Sec) { |
| 1981 | std::vector<GdbIndexChunk::AddressEntry> Ret; |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1982 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 1983 | uint32_t CuIdx = 0; |
| 1984 | for (std::unique_ptr<DWARFCompileUnit> &Cu : Dwarf.compile_units()) { |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1985 | DWARFAddressRangesVector Ranges; |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 1986 | Cu->collectAddressRanges(Ranges); |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 1987 | |
George Rimar | 35e846e | 2017-03-21 08:19:34 +0000 | [diff] [blame] | 1988 | ArrayRef<InputSectionBase *> Sections = Sec->File->getSections(); |
George Rimar | 0641b8b | 2017-06-07 10:52:02 +0000 | [diff] [blame] | 1989 | for (DWARFAddressRange &R : Ranges) { |
| 1990 | InputSectionBase *S = Sections[R.SectionIndex]; |
| 1991 | if (!S || S == &InputSection::Discarded || !S->Live) |
| 1992 | continue; |
| 1993 | // Range list with zero size has no effect. |
| 1994 | if (R.LowPC == R.HighPC) |
| 1995 | continue; |
Rafael Espindola | 8b1afd5 | 2017-07-19 22:27:35 +0000 | [diff] [blame] | 1996 | auto *IS = cast<InputSection>(S); |
| 1997 | uint64_t Offset = IS->getOffsetInFile(); |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 1998 | Ret.push_back({IS, R.LowPC - Offset, R.HighPC - Offset, CuIdx}); |
George Rimar | 0641b8b | 2017-06-07 10:52:02 +0000 | [diff] [blame] | 1999 | } |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2000 | ++CuIdx; |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 2001 | } |
| 2002 | return Ret; |
| 2003 | } |
| 2004 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2005 | static std::vector<GdbIndexChunk::NameTypeEntry> |
| 2006 | readPubNamesAndTypes(DWARFContext &Dwarf) { |
| 2007 | StringRef Sec1 = Dwarf.getDWARFObj().getGnuPubNamesSection(); |
| 2008 | StringRef Sec2 = Dwarf.getDWARFObj().getGnuPubTypesSection(); |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 2009 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2010 | std::vector<GdbIndexChunk::NameTypeEntry> Ret; |
| 2011 | for (StringRef Sec : {Sec1, Sec2}) { |
| 2012 | DWARFDebugPubTable Table(Sec, Config->IsLE, true); |
Rui Ueyama | 22125d8 | 2017-09-25 01:42:57 +0000 | [diff] [blame] | 2013 | for (const DWARFDebugPubTable::Set &Set : Table.getData()) { |
| 2014 | for (const DWARFDebugPubTable::Entry &Ent : Set.Entries) { |
| 2015 | CachedHashStringRef S(Ent.Name, computeGdbHash(Ent.Name)); |
| 2016 | Ret.push_back({S, Ent.Descriptor.toBits()}); |
| 2017 | } |
| 2018 | } |
Rui Ueyama | ac2d815 | 2017-03-01 22:54:50 +0000 | [diff] [blame] | 2019 | } |
| 2020 | return Ret; |
| 2021 | } |
| 2022 | |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2023 | static std::vector<InputSection *> getDebugInfoSections() { |
| 2024 | std::vector<InputSection *> Ret; |
| 2025 | for (InputSectionBase *S : InputSections) |
| 2026 | if (InputSection *IS = dyn_cast<InputSection>(S)) |
Rafael Espindola | 300b386 | 2017-07-12 23:56:53 +0000 | [diff] [blame] | 2027 | if (IS->Name == ".debug_info") |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2028 | Ret.push_back(IS); |
| 2029 | return Ret; |
| 2030 | } |
| 2031 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2032 | void GdbIndexSection::fixCuIndex() { |
| 2033 | uint32_t Idx = 0; |
| 2034 | for (GdbIndexChunk &Chunk : Chunks) { |
| 2035 | for (GdbIndexChunk::AddressEntry &Ent : Chunk.AddressAreas) |
| 2036 | Ent.CuIndex += Idx; |
| 2037 | Idx += Chunk.CompilationUnits.size(); |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
Rui Ueyama | 17cc6f6 | 2017-09-25 04:55:27 +0000 | [diff] [blame] | 2041 | std::vector<std::vector<uint32_t>> GdbIndexSection::createCuVectors() { |
| 2042 | std::vector<std::vector<uint32_t>> Ret; |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2043 | uint32_t Idx = 0; |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2044 | uint32_t Off = 0; |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2045 | |
| 2046 | for (GdbIndexChunk &Chunk : Chunks) { |
| 2047 | for (GdbIndexChunk::NameTypeEntry &Ent : Chunk.NamesAndTypes) { |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2048 | GdbSymbol *&Sym = Symbols[Ent.Name]; |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2049 | if (!Sym) { |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2050 | Sym = make<GdbSymbol>(GdbSymbol{Ent.Name.hash(), Off, Ret.size()}); |
| 2051 | Off += Ent.Name.size() + 1; |
Rui Ueyama | 17cc6f6 | 2017-09-25 04:55:27 +0000 | [diff] [blame] | 2052 | Ret.push_back({}); |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2053 | } |
Rui Ueyama | f9da2fd | 2017-09-25 05:30:39 +0000 | [diff] [blame] | 2054 | |
| 2055 | // gcc 5.4.1 produces a buggy .debug_gnu_pubnames that contains |
| 2056 | // duplicate entries, so we want to dedup them. |
| 2057 | std::vector<uint32_t> &Vec = Ret[Sym->CuVectorIndex]; |
| 2058 | uint32_t Val = (Ent.Type << 24) | Idx; |
| 2059 | if (Vec.empty() || Vec.back() != Val) |
| 2060 | Vec.push_back(Val); |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2061 | } |
| 2062 | Idx += Chunk.CompilationUnits.size(); |
| 2063 | } |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2064 | |
| 2065 | StringPoolSize = Off; |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2066 | return Ret; |
| 2067 | } |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 2068 | |
Rafael Espindola | 300b386 | 2017-07-12 23:56:53 +0000 | [diff] [blame] | 2069 | template <class ELFT> GdbIndexSection *elf::createGdbIndex() { |
Rui Ueyama | a1b79df | 2017-10-10 22:59:32 +0000 | [diff] [blame] | 2070 | // Gather debug info to create a .gdb_index section. |
George Rimar | 2d23da0 | 2017-08-01 14:57:13 +0000 | [diff] [blame] | 2071 | std::vector<InputSection *> Sections = getDebugInfoSections(); |
| 2072 | std::vector<GdbIndexChunk> Chunks(Sections.size()); |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2073 | |
George Rimar | 2d23da0 | 2017-08-01 14:57:13 +0000 | [diff] [blame] | 2074 | parallelForEachN(0, Chunks.size(), [&](size_t I) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2075 | ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>(); |
| 2076 | DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File)); |
| 2077 | |
| 2078 | Chunks[I].DebugInfoSec = Sections[I]; |
| 2079 | Chunks[I].CompilationUnits = readCuList(Dwarf); |
| 2080 | Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]); |
| 2081 | Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf); |
George Rimar | 2d23da0 | 2017-08-01 14:57:13 +0000 | [diff] [blame] | 2082 | }); |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2083 | |
Rui Ueyama | a1b79df | 2017-10-10 22:59:32 +0000 | [diff] [blame] | 2084 | // .debug_gnu_pub{names,types} are useless in executables. |
| 2085 | // They are present in input object files solely for creating |
| 2086 | // a .gdb_index. So we can remove it from the output. |
| 2087 | for (InputSectionBase *S : InputSections) |
| 2088 | if (S->Name == ".debug_gnu_pubnames" || S->Name == ".debug_gnu_pubtypes") |
| 2089 | S->Live = false; |
| 2090 | |
| 2091 | // Create a .gdb_index and returns it. |
Rafael Espindola | 300b386 | 2017-07-12 23:56:53 +0000 | [diff] [blame] | 2092 | return make<GdbIndexSection>(std::move(Chunks)); |
| 2093 | } |
| 2094 | |
Rui Ueyama | e5d642c | 2017-08-15 17:01:28 +0000 | [diff] [blame] | 2095 | static size_t getCuSize(ArrayRef<GdbIndexChunk> Arr) { |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2096 | size_t Ret = 0; |
Rui Ueyama | e5d642c | 2017-08-15 17:01:28 +0000 | [diff] [blame] | 2097 | for (const GdbIndexChunk &D : Arr) |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2098 | Ret += D.CompilationUnits.size(); |
| 2099 | return Ret; |
| 2100 | } |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2101 | |
Rui Ueyama | e5d642c | 2017-08-15 17:01:28 +0000 | [diff] [blame] | 2102 | static size_t getAddressAreaSize(ArrayRef<GdbIndexChunk> Arr) { |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2103 | size_t Ret = 0; |
Rui Ueyama | e5d642c | 2017-08-15 17:01:28 +0000 | [diff] [blame] | 2104 | for (const GdbIndexChunk &D : Arr) |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2105 | Ret += D.AddressAreas.size(); |
| 2106 | return Ret; |
| 2107 | } |
| 2108 | |
| 2109 | std::vector<GdbSymbol *> GdbIndexSection::createGdbSymtab() { |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2110 | uint32_t Size = NextPowerOf2(Symbols.size() * 4 / 3); |
| 2111 | if (Size < 1024) |
| 2112 | Size = 1024; |
| 2113 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2114 | uint32_t Mask = Size - 1; |
| 2115 | std::vector<GdbSymbol *> Ret(Size); |
| 2116 | |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2117 | for (auto &KV : Symbols) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2118 | GdbSymbol *Sym = KV.second; |
| 2119 | uint32_t I = Sym->NameHash & Mask; |
| 2120 | uint32_t Step = ((Sym->NameHash * 17) & Mask) | 1; |
| 2121 | |
| 2122 | while (Ret[I]) |
| 2123 | I = (I + Step) & Mask; |
| 2124 | Ret[I] = Sym; |
| 2125 | } |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2126 | return Ret; |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Rui Ueyama | 2b6631b | 2017-08-15 17:01:39 +0000 | [diff] [blame] | 2129 | GdbIndexSection::GdbIndexSection(std::vector<GdbIndexChunk> &&C) |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2130 | : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"), Chunks(std::move(C)) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2131 | fixCuIndex(); |
| 2132 | CuVectors = createCuVectors(); |
| 2133 | GdbSymtab = createGdbSymtab(); |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2134 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2135 | // Compute offsets early to know the section size. |
| 2136 | // Each chunk size needs to be in sync with what we write in writeTo. |
| 2137 | CuTypesOffset = CuListOffset + getCuSize(Chunks) * 16; |
| 2138 | SymtabOffset = CuTypesOffset + getAddressAreaSize(Chunks) * 20; |
| 2139 | ConstantPoolOffset = SymtabOffset + GdbSymtab.size() * 8; |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2140 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2141 | size_t Off = 0; |
Rui Ueyama | 17cc6f6 | 2017-09-25 04:55:27 +0000 | [diff] [blame] | 2142 | for (ArrayRef<uint32_t> Vec : CuVectors) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2143 | CuVectorOffsets.push_back(Off); |
Rui Ueyama | 17cc6f6 | 2017-09-25 04:55:27 +0000 | [diff] [blame] | 2144 | Off += (Vec.size() + 1) * 4; |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2145 | } |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2146 | StringPoolOffset = ConstantPoolOffset + Off; |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
George Rimar | 35e846e | 2017-03-21 08:19:34 +0000 | [diff] [blame] | 2149 | size_t GdbIndexSection::getSize() const { |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2150 | return StringPoolOffset + StringPoolSize; |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
George Rimar | 35e846e | 2017-03-21 08:19:34 +0000 | [diff] [blame] | 2153 | void GdbIndexSection::writeTo(uint8_t *Buf) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2154 | // Write the section header. |
| 2155 | write32le(Buf, 7); |
| 2156 | write32le(Buf + 4, CuListOffset); |
| 2157 | write32le(Buf + 8, CuTypesOffset); |
| 2158 | write32le(Buf + 12, CuTypesOffset); |
| 2159 | write32le(Buf + 16, SymtabOffset); |
| 2160 | write32le(Buf + 20, ConstantPoolOffset); |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2161 | Buf += 24; |
| 2162 | |
| 2163 | // Write the CU list. |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2164 | for (GdbIndexChunk &D : Chunks) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2165 | for (GdbIndexChunk::CuEntry &Cu : D.CompilationUnits) { |
Rafael Espindola | 300b386 | 2017-07-12 23:56:53 +0000 | [diff] [blame] | 2166 | write64le(Buf, D.DebugInfoSec->OutSecOff + Cu.CuOffset); |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2167 | write64le(Buf + 8, Cu.CuLength); |
| 2168 | Buf += 16; |
| 2169 | } |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2170 | } |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 2171 | |
| 2172 | // Write the address area. |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2173 | for (GdbIndexChunk &D : Chunks) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2174 | for (GdbIndexChunk::AddressEntry &E : D.AddressAreas) { |
Rafael Espindola | 4f058a2 | 2018-03-24 00:35:11 +0000 | [diff] [blame] | 2175 | uint64_t BaseAddr = E.Section->getVA(0); |
George Rimar | 8666562 | 2017-06-07 16:59:11 +0000 | [diff] [blame] | 2176 | write64le(Buf, BaseAddr + E.LowAddress); |
| 2177 | write64le(Buf + 8, BaseAddr + E.HighAddress); |
| 2178 | write32le(Buf + 16, E.CuIndex); |
| 2179 | Buf += 20; |
| 2180 | } |
George Rimar | 8b54739 | 2016-12-15 09:08:13 +0000 | [diff] [blame] | 2181 | } |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Write the symbol table. |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2184 | for (GdbSymbol *Sym : GdbSymtab) { |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2185 | if (Sym) { |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2186 | write32le(Buf, Sym->NameOffset + StringPoolOffset - ConstantPoolOffset); |
| 2187 | write32le(Buf + 4, CuVectorOffsets[Sym->CuVectorIndex]); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2188 | } |
| 2189 | Buf += 8; |
| 2190 | } |
| 2191 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2192 | // Write the CU vectors. |
Rui Ueyama | 17cc6f6 | 2017-09-25 04:55:27 +0000 | [diff] [blame] | 2193 | for (ArrayRef<uint32_t> Vec : CuVectors) { |
| 2194 | write32le(Buf, Vec.size()); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2195 | Buf += 4; |
Rui Ueyama | 17cc6f6 | 2017-09-25 04:55:27 +0000 | [diff] [blame] | 2196 | for (uint32_t Val : Vec) { |
George Rimar | 5f5905e | 2017-05-26 12:01:40 +0000 | [diff] [blame] | 2197 | write32le(Buf, Val); |
George Rimar | ec02b8d | 2016-12-15 12:07:53 +0000 | [diff] [blame] | 2198 | Buf += 4; |
| 2199 | } |
| 2200 | } |
| 2201 | |
Rui Ueyama | 9f4f490 | 2017-09-24 21:45:35 +0000 | [diff] [blame] | 2202 | // Write the string pool. |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2203 | for (auto &KV : Symbols) { |
| 2204 | CachedHashStringRef S = KV.first; |
| 2205 | GdbSymbol *Sym = KV.second; |
| 2206 | size_t Off = Sym->NameOffset; |
| 2207 | memcpy(Buf + Off, S.val().data(), S.size()); |
Rui Ueyama | 8f222b8 | 2017-09-25 03:40:45 +0000 | [diff] [blame] | 2208 | Buf[Off + S.size()] = '\0'; |
Rui Ueyama | bbc477c | 2017-09-25 02:29:51 +0000 | [diff] [blame] | 2209 | } |
Eugene Leviant | a113a41 | 2016-11-21 09:24:43 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 2212 | bool GdbIndexSection::empty() const { return !Out::DebugInfo; } |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 2213 | |
Rui Ueyama | 02551ab | 2017-10-27 03:14:24 +0000 | [diff] [blame] | 2214 | EhFrameHeader::EhFrameHeader() |
Rafael Espindola | 6b2b450 | 2018-01-23 05:23:23 +0000 | [diff] [blame] | 2215 | : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 4, ".eh_frame_hdr") {} |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2216 | |
| 2217 | // .eh_frame_hdr contains a binary search table of pointers to FDEs. |
| 2218 | // Each entry of the search table consists of two values, |
| 2219 | // the starting PC from where FDEs covers, and the FDE's address. |
| 2220 | // It is sorted by PC. |
Rui Ueyama | 02551ab | 2017-10-27 03:14:24 +0000 | [diff] [blame] | 2221 | void EhFrameHeader::writeTo(uint8_t *Buf) { |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 2222 | typedef EhFrameSection::FdeData FdeData; |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2223 | |
Rui Ueyama | 02551ab | 2017-10-27 03:14:24 +0000 | [diff] [blame] | 2224 | std::vector<FdeData> Fdes = InX::EhFrame->getFdeData(); |
Rui Ueyama | c055225 | 2017-10-27 03:13:24 +0000 | [diff] [blame] | 2225 | |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2226 | // Sort the FDE list by their PC and uniqueify. Usually there is only |
| 2227 | // one FDE for a PC (i.e. function), but if ICF merges two functions |
| 2228 | // into one, there can be more than one FDEs pointing to the address. |
| 2229 | auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; |
| 2230 | std::stable_sort(Fdes.begin(), Fdes.end(), Less); |
| 2231 | auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; |
| 2232 | Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); |
| 2233 | |
| 2234 | Buf[0] = 1; |
| 2235 | Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 2236 | Buf[2] = DW_EH_PE_udata4; |
| 2237 | Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 2238 | write32(Buf + 4, InX::EhFrame->getParent()->Addr - this->getVA() - 4); |
| 2239 | write32(Buf + 8, Fdes.size()); |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2240 | Buf += 12; |
| 2241 | |
Rui Ueyama | c49bdd6 | 2017-04-14 01:34:45 +0000 | [diff] [blame] | 2242 | uint64_t VA = this->getVA(); |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2243 | for (FdeData &Fde : Fdes) { |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 2244 | write32(Buf, Fde.Pc - VA); |
| 2245 | write32(Buf + 4, Fde.FdeVA - VA); |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2246 | Buf += 8; |
| 2247 | } |
| 2248 | } |
| 2249 | |
Rui Ueyama | 02551ab | 2017-10-27 03:14:24 +0000 | [diff] [blame] | 2250 | size_t EhFrameHeader::getSize() const { |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2251 | // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 2252 | return 12 + InX::EhFrame->NumFdes * 8; |
Eugene Leviant | 952eb4d | 2016-11-21 15:52:10 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Rui Ueyama | 02551ab | 2017-10-27 03:14:24 +0000 | [diff] [blame] | 2255 | bool EhFrameHeader::empty() const { return InX::EhFrame->empty(); } |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 2256 | |
Rui Ueyama | b38ddb1 | 2016-11-21 19:46:04 +0000 | [diff] [blame] | 2257 | template <class ELFT> |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2258 | VersionDefinitionSection<ELFT>::VersionDefinitionSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2259 | : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t), |
| 2260 | ".gnu.version_d") {} |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2261 | |
| 2262 | static StringRef getFileDefName() { |
| 2263 | if (!Config->SoName.empty()) |
| 2264 | return Config->SoName; |
| 2265 | return Config->OutputFile; |
| 2266 | } |
| 2267 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 2268 | template <class ELFT> void VersionDefinitionSection<ELFT>::finalizeContents() { |
Rafael Espindola | 895aea6 | 2017-05-11 22:02:41 +0000 | [diff] [blame] | 2269 | FileDefNameOff = InX::DynStrTab->addString(getFileDefName()); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2270 | for (VersionDefinition &V : Config->VersionDefinitions) |
Rafael Espindola | 895aea6 | 2017-05-11 22:02:41 +0000 | [diff] [blame] | 2271 | V.NameOff = InX::DynStrTab->addString(V.Name); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2272 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 2273 | getParent()->Link = InX::DynStrTab->getParent()->SectionIndex; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2274 | |
| 2275 | // sh_info should be set to the number of definitions. This fact is missed in |
| 2276 | // documentation, but confirmed by binutils community: |
| 2277 | // https://sourceware.org/ml/binutils/2014-11/msg00355.html |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 2278 | getParent()->Info = getVerDefNum(); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | template <class ELFT> |
| 2282 | void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, |
| 2283 | StringRef Name, size_t NameOff) { |
| 2284 | auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
| 2285 | Verdef->vd_version = 1; |
| 2286 | Verdef->vd_cnt = 1; |
| 2287 | Verdef->vd_aux = sizeof(Elf_Verdef); |
| 2288 | Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 2289 | Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); |
| 2290 | Verdef->vd_ndx = Index; |
| 2291 | Verdef->vd_hash = hashSysV(Name); |
| 2292 | |
| 2293 | auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); |
| 2294 | Verdaux->vda_name = NameOff; |
| 2295 | Verdaux->vda_next = 0; |
| 2296 | } |
| 2297 | |
| 2298 | template <class ELFT> |
| 2299 | void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2300 | writeOne(Buf, 1, getFileDefName(), FileDefNameOff); |
| 2301 | |
| 2302 | for (VersionDefinition &V : Config->VersionDefinitions) { |
| 2303 | Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 2304 | writeOne(Buf, V.Id, V.Name, V.NameOff); |
| 2305 | } |
| 2306 | |
| 2307 | // Need to terminate the last version definition. |
| 2308 | Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
| 2309 | Verdef->vd_next = 0; |
| 2310 | } |
| 2311 | |
| 2312 | template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const { |
| 2313 | return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); |
| 2314 | } |
| 2315 | |
| 2316 | template <class ELFT> |
| 2317 | VersionTableSection<ELFT>::VersionTableSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2318 | : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t), |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 2319 | ".gnu.version") { |
| 2320 | this->Entsize = sizeof(Elf_Versym); |
| 2321 | } |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2322 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 2323 | template <class ELFT> void VersionTableSection<ELFT>::finalizeContents() { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2324 | // At the moment of june 2016 GNU docs does not mention that sh_link field |
| 2325 | // should be set, but Sun docs do. Also readelf relies on this field. |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 2326 | getParent()->Link = InX::DynSymTab->getParent()->SectionIndex; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const { |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 2330 | return sizeof(Elf_Versym) * (InX::DynSymTab->getSymbols().size() + 1); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2334 | auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; |
George Rimar | 69b17c3 | 2017-05-16 10:04:42 +0000 | [diff] [blame] | 2335 | for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) { |
Zachary Turner | a104d55 | 2017-11-03 22:33:49 +0000 | [diff] [blame] | 2336 | OutVersym->vs_index = S.Sym->VersionId; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2337 | ++OutVersym; |
| 2338 | } |
| 2339 | } |
| 2340 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 2341 | template <class ELFT> bool VersionTableSection<ELFT>::empty() const { |
| 2342 | return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty(); |
| 2343 | } |
| 2344 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2345 | template <class ELFT> |
| 2346 | VersionNeedSection<ELFT>::VersionNeedSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2347 | : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t), |
| 2348 | ".gnu.version_r") { |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2349 | // Identifiers in verneed section start at 2 because 0 and 1 are reserved |
| 2350 | // for VER_NDX_LOCAL and VER_NDX_GLOBAL. |
| 2351 | // First identifiers are reserved by verdef section if it exist. |
| 2352 | NextIndex = getVerDefNum() + 1; |
| 2353 | } |
| 2354 | |
Rafael Espindola | ab0cce5 | 2018-04-26 17:58:58 +0000 | [diff] [blame] | 2355 | template <class ELFT> void VersionNeedSection<ELFT>::addSymbol(Symbol *SS) { |
| 2356 | auto &File = cast<SharedFile<ELFT>>(*SS->File); |
Rui Ueyama | d37c33a | 2018-03-24 00:25:24 +0000 | [diff] [blame] | 2357 | if (SS->VerdefIndex == VER_NDX_GLOBAL) { |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 2358 | SS->VersionId = VER_NDX_GLOBAL; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2359 | return; |
| 2360 | } |
Rui Ueyama | 4076fa1 | 2017-02-26 23:35:34 +0000 | [diff] [blame] | 2361 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2362 | // If we don't already know that we need an Elf_Verneed for this DSO, prepare |
| 2363 | // to create one by adding it to our needed list and creating a dynstr entry |
| 2364 | // for the soname. |
Rafael Espindola | a32ddc4 | 2017-12-20 16:28:19 +0000 | [diff] [blame] | 2365 | if (File.VerdefMap.empty()) |
| 2366 | Needed.push_back({&File, InX::DynStrTab->addString(File.SoName)}); |
Rui Ueyama | d37c33a | 2018-03-24 00:25:24 +0000 | [diff] [blame] | 2367 | const typename ELFT::Verdef *Ver = File.Verdefs[SS->VerdefIndex]; |
Rafael Espindola | a32ddc4 | 2017-12-20 16:28:19 +0000 | [diff] [blame] | 2368 | typename SharedFile<ELFT>::NeededVer &NV = File.VerdefMap[Ver]; |
Rui Ueyama | d37c33a | 2018-03-24 00:25:24 +0000 | [diff] [blame] | 2369 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2370 | // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, |
| 2371 | // prepare to create one by allocating a version identifier and creating a |
| 2372 | // dynstr entry for the version name. |
| 2373 | if (NV.Index == 0) { |
Rafael Espindola | a32ddc4 | 2017-12-20 16:28:19 +0000 | [diff] [blame] | 2374 | NV.StrTab = InX::DynStrTab->addString(File.getStringTable().data() + |
Rafael Espindola | 895aea6 | 2017-05-11 22:02:41 +0000 | [diff] [blame] | 2375 | Ver->getAux()->vda_name); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2376 | NV.Index = NextIndex++; |
| 2377 | } |
Rui Ueyama | f1f0084 | 2017-10-31 16:07:41 +0000 | [diff] [blame] | 2378 | SS->VersionId = NV.Index; |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { |
| 2382 | // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. |
| 2383 | auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); |
| 2384 | auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); |
| 2385 | |
| 2386 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { |
| 2387 | // Create an Elf_Verneed for this DSO. |
| 2388 | Verneed->vn_version = 1; |
| 2389 | Verneed->vn_cnt = P.first->VerdefMap.size(); |
| 2390 | Verneed->vn_file = P.second; |
| 2391 | Verneed->vn_aux = |
| 2392 | reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); |
| 2393 | Verneed->vn_next = sizeof(Elf_Verneed); |
| 2394 | ++Verneed; |
| 2395 | |
| 2396 | // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over |
| 2397 | // VerdefMap, which will only contain references to needed version |
| 2398 | // definitions. Each Elf_Vernaux is based on the information contained in |
| 2399 | // the Elf_Verdef in the source DSO. This loop iterates over a std::map of |
| 2400 | // pointers, but is deterministic because the pointers refer to Elf_Verdef |
| 2401 | // data structures within a single input file. |
| 2402 | for (auto &NV : P.first->VerdefMap) { |
| 2403 | Vernaux->vna_hash = NV.first->vd_hash; |
| 2404 | Vernaux->vna_flags = 0; |
| 2405 | Vernaux->vna_other = NV.second.Index; |
| 2406 | Vernaux->vna_name = NV.second.StrTab; |
| 2407 | Vernaux->vna_next = sizeof(Elf_Vernaux); |
| 2408 | ++Vernaux; |
| 2409 | } |
| 2410 | |
| 2411 | Vernaux[-1].vna_next = 0; |
| 2412 | } |
| 2413 | Verneed[-1].vn_next = 0; |
| 2414 | } |
| 2415 | |
Rui Ueyama | 945055a | 2017-02-27 03:07:41 +0000 | [diff] [blame] | 2416 | template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 2417 | getParent()->Link = InX::DynStrTab->getParent()->SectionIndex; |
| 2418 | getParent()->Info = Needed.size(); |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const { |
| 2422 | unsigned Size = Needed.size() * sizeof(Elf_Verneed); |
| 2423 | for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed) |
| 2424 | Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); |
| 2425 | return Size; |
| 2426 | } |
| 2427 | |
George Rimar | 11992c86 | 2016-11-25 08:05:41 +0000 | [diff] [blame] | 2428 | template <class ELFT> bool VersionNeedSection<ELFT>::empty() const { |
| 2429 | return getNeedNum() == 0; |
| 2430 | } |
| 2431 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2432 | void MergeSyntheticSection::addSection(MergeInputSection *MS) { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 2433 | MS->Parent = this; |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2434 | Sections.push_back(MS); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2437 | MergeTailSection::MergeTailSection(StringRef Name, uint32_t Type, |
| 2438 | uint64_t Flags, uint32_t Alignment) |
| 2439 | : MergeSyntheticSection(Name, Type, Flags, Alignment), |
| 2440 | Builder(StringTableBuilder::RAW, Alignment) {} |
Rui Ueyama | e26b7aa | 2017-09-26 00:54:24 +0000 | [diff] [blame] | 2441 | |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2442 | size_t MergeTailSection::getSize() const { return Builder.getSize(); } |
| 2443 | |
| 2444 | void MergeTailSection::writeTo(uint8_t *Buf) { Builder.write(Buf); } |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2445 | |
Rui Ueyama | e26b7aa | 2017-09-26 00:54:24 +0000 | [diff] [blame] | 2446 | void MergeTailSection::finalizeContents() { |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2447 | // Add all string pieces to the string table builder to create section |
| 2448 | // contents. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 2449 | for (MergeInputSection *Sec : Sections) |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2450 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 2451 | if (Sec->Pieces[I].Live) |
| 2452 | Builder.add(Sec->getData(I)); |
| 2453 | |
| 2454 | // Fix the string table content. After this, the contents will never change. |
| 2455 | Builder.finalize(); |
| 2456 | |
| 2457 | // finalize() fixed tail-optimized strings, so we can now get |
| 2458 | // offsets of strings. Get an offset for each string and save it |
| 2459 | // to a corresponding StringPiece for easy access. |
Rafael Espindola | 7bd4550 | 2018-04-05 00:01:57 +0000 | [diff] [blame] | 2460 | for (MergeInputSection *Sec : Sections) |
| 2461 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 2462 | if (Sec->Pieces[I].Live) |
| 2463 | Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I)); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2466 | void MergeNoTailSection::writeTo(uint8_t *Buf) { |
| 2467 | for (size_t I = 0; I < NumShards; ++I) |
| 2468 | Shards[I].write(Buf + ShardOffsets[I]); |
| 2469 | } |
| 2470 | |
| 2471 | // This function is very hot (i.e. it can take several seconds to finish) |
| 2472 | // because sometimes the number of inputs is in an order of magnitude of |
| 2473 | // millions. So, we use multi-threading. |
| 2474 | // |
| 2475 | // For any strings S and T, we know S is not mergeable with T if S's hash |
| 2476 | // value is different from T's. If that's the case, we can safely put S and |
| 2477 | // T into different string builders without worrying about merge misses. |
| 2478 | // We do it in parallel. |
Rui Ueyama | e26b7aa | 2017-09-26 00:54:24 +0000 | [diff] [blame] | 2479 | void MergeNoTailSection::finalizeContents() { |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2480 | // Initializes string table builders. |
| 2481 | for (size_t I = 0; I < NumShards; ++I) |
| 2482 | Shards.emplace_back(StringTableBuilder::RAW, Alignment); |
| 2483 | |
Rui Ueyama | f3f9bae | 2017-10-03 00:45:24 +0000 | [diff] [blame] | 2484 | // Concurrency level. Must be a power of 2 to avoid expensive modulo |
| 2485 | // operations in the following tight loop. |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2486 | size_t Concurrency = 1; |
Bob Haarman | 4f5c8c2 | 2017-10-13 18:22:55 +0000 | [diff] [blame] | 2487 | if (ThreadsEnabled) |
Rafael Espindola | 0038dfa | 2017-10-04 20:35:05 +0000 | [diff] [blame] | 2488 | Concurrency = |
| 2489 | std::min<size_t>(PowerOf2Floor(hardware_concurrency()), NumShards); |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2490 | |
| 2491 | // Add section pieces to the builders. |
| 2492 | parallelForEachN(0, Concurrency, [&](size_t ThreadId) { |
| 2493 | for (MergeInputSection *Sec : Sections) { |
| 2494 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) { |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 2495 | size_t ShardId = getShardId(Sec->Pieces[I].Hash); |
Dimitry Andric | 656714a | 2018-01-11 08:03:22 +0000 | [diff] [blame] | 2496 | if ((ShardId & (Concurrency - 1)) == ThreadId && Sec->Pieces[I].Live) |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 2497 | Sec->Pieces[I].OutputOff = Shards[ShardId].add(Sec->getData(I)); |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2498 | } |
| 2499 | } |
| 2500 | }); |
| 2501 | |
| 2502 | // Compute an in-section offset for each shard. |
| 2503 | size_t Off = 0; |
| 2504 | for (size_t I = 0; I < NumShards; ++I) { |
| 2505 | Shards[I].finalizeInOrder(); |
| 2506 | if (Shards[I].getSize() > 0) |
| 2507 | Off = alignTo(Off, Alignment); |
| 2508 | ShardOffsets[I] = Off; |
| 2509 | Off += Shards[I].getSize(); |
| 2510 | } |
| 2511 | Size = Off; |
| 2512 | |
| 2513 | // So far, section pieces have offsets from beginning of shards, but |
| 2514 | // we want offsets from beginning of the whole section. Fix them. |
| 2515 | parallelForEach(Sections, [&](MergeInputSection *Sec) { |
Rafael Espindola | 7bd4550 | 2018-04-05 00:01:57 +0000 | [diff] [blame] | 2516 | for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) |
| 2517 | if (Sec->Pieces[I].Live) |
| 2518 | Sec->Pieces[I].OutputOff += |
| 2519 | ShardOffsets[getShardId(Sec->Pieces[I].Hash)]; |
Rui Ueyama | c97a70c | 2017-09-30 11:46:26 +0000 | [diff] [blame] | 2520 | }); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Rui Ueyama | e26b7aa | 2017-09-26 00:54:24 +0000 | [diff] [blame] | 2523 | static MergeSyntheticSection *createMergeSynthetic(StringRef Name, |
| 2524 | uint32_t Type, |
| 2525 | uint64_t Flags, |
| 2526 | uint32_t Alignment) { |
| 2527 | bool ShouldTailMerge = (Flags & SHF_STRINGS) && Config->Optimize >= 2; |
| 2528 | if (ShouldTailMerge) |
| 2529 | return make<MergeTailSection>(Name, Type, Flags, Alignment); |
| 2530 | return make<MergeNoTailSection>(Name, Type, Flags, Alignment); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Rui Ueyama | ac114d2 | 2018-02-12 21:56:14 +0000 | [diff] [blame] | 2533 | // Debug sections may be compressed by zlib. Decompress if exists. |
Rui Ueyama | 2b714b5 | 2017-10-11 03:12:53 +0000 | [diff] [blame] | 2534 | void elf::decompressSections() { |
Rafael Espindola | 9bf1006 | 2018-04-27 16:29:57 +0000 | [diff] [blame] | 2535 | parallelForEach(InputSections, |
| 2536 | [](InputSectionBase *Sec) { Sec->maybeDecompress(); }); |
| 2537 | } |
| 2538 | |
Rafael Espindola | f1652d4 | 2018-04-27 18:17:36 +0000 | [diff] [blame] | 2539 | template <class ELFT> void elf::splitSections() { |
Rafael Espindola | 9bf1006 | 2018-04-27 16:29:57 +0000 | [diff] [blame] | 2540 | // splitIntoPieces needs to be called on each MergeInputSection |
| 2541 | // before calling finalizeContents(). |
Rui Ueyama | 2b714b5 | 2017-10-11 03:12:53 +0000 | [diff] [blame] | 2542 | parallelForEach(InputSections, [](InputSectionBase *Sec) { |
Rafael Espindola | 9bf1006 | 2018-04-27 16:29:57 +0000 | [diff] [blame] | 2543 | if (auto *S = dyn_cast<MergeInputSection>(Sec)) |
| 2544 | S->splitIntoPieces(); |
Rafael Espindola | f1652d4 | 2018-04-27 18:17:36 +0000 | [diff] [blame] | 2545 | else if (auto *Eh = dyn_cast<EhInputSection>(Sec)) |
| 2546 | Eh->split<ELFT>(); |
Rui Ueyama | 2b714b5 | 2017-10-11 03:12:53 +0000 | [diff] [blame] | 2547 | }); |
| 2548 | } |
| 2549 | |
| 2550 | // This function scans over the inputsections to create mergeable |
| 2551 | // synthetic sections. |
| 2552 | // |
| 2553 | // It removes MergeInputSections from the input section array and adds |
| 2554 | // new synthetic sections at the location of the first input section |
| 2555 | // that it replaces. It then finalizes each synthetic section in order |
| 2556 | // to compute an output offset for each piece of each input section. |
| 2557 | void elf::mergeSections() { |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 2558 | std::vector<MergeSyntheticSection *> MergeSections; |
| 2559 | for (InputSectionBase *&S : InputSections) { |
| 2560 | MergeInputSection *MS = dyn_cast<MergeInputSection>(S); |
| 2561 | if (!MS) |
| 2562 | continue; |
| 2563 | |
| 2564 | // We do not want to handle sections that are not alive, so just remove |
| 2565 | // them instead of trying to merge. |
| 2566 | if (!MS->Live) |
| 2567 | continue; |
| 2568 | |
George Rimar | 78e27e8 | 2017-12-01 09:04:52 +0000 | [diff] [blame] | 2569 | StringRef OutsecName = getOutputSectionName(MS); |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 2570 | uint32_t Alignment = std::max<uint32_t>(MS->Alignment, MS->Entsize); |
| 2571 | |
| 2572 | auto I = llvm::find_if(MergeSections, [=](MergeSyntheticSection *Sec) { |
Rafael Espindola | a5d43d0 | 2017-11-15 16:56:20 +0000 | [diff] [blame] | 2573 | // While we could create a single synthetic section for two different |
| 2574 | // values of Entsize, it is better to take Entsize into consideration. |
| 2575 | // |
| 2576 | // With a single synthetic section no two pieces with different Entsize |
| 2577 | // could be equal, so we may as well have two sections. |
| 2578 | // |
| 2579 | // Using Entsize in here also allows us to propagate it to the synthetic |
| 2580 | // section. |
Rui Ueyama | bc2c9e0 | 2017-07-20 21:42:30 +0000 | [diff] [blame] | 2581 | return Sec->Name == OutsecName && Sec->Flags == MS->Flags && |
Rafael Espindola | a5d43d0 | 2017-11-15 16:56:20 +0000 | [diff] [blame] | 2582 | Sec->Entsize == MS->Entsize && Sec->Alignment == Alignment; |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 2583 | }); |
| 2584 | if (I == MergeSections.end()) { |
Rui Ueyama | e26b7aa | 2017-09-26 00:54:24 +0000 | [diff] [blame] | 2585 | MergeSyntheticSection *Syn = |
| 2586 | createMergeSynthetic(OutsecName, MS->Type, MS->Flags, Alignment); |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 2587 | MergeSections.push_back(Syn); |
| 2588 | I = std::prev(MergeSections.end()); |
| 2589 | S = Syn; |
Rafael Espindola | a5d43d0 | 2017-11-15 16:56:20 +0000 | [diff] [blame] | 2590 | Syn->Entsize = MS->Entsize; |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 2591 | } else { |
| 2592 | S = nullptr; |
| 2593 | } |
| 2594 | (*I)->addSection(MS); |
| 2595 | } |
Rafael Espindola | 6cd7af5 | 2018-04-03 21:38:18 +0000 | [diff] [blame] | 2596 | for (auto *MS : MergeSections) |
Peter Collingbourne | dc7936e | 2017-06-12 00:00:51 +0000 | [diff] [blame] | 2597 | MS->finalizeContents(); |
| 2598 | |
| 2599 | std::vector<InputSectionBase *> &V = InputSections; |
| 2600 | V.erase(std::remove(V.begin(), V.end(), nullptr), V.end()); |
| 2601 | } |
| 2602 | |
George Rimar | 42886c4 | 2017-03-15 12:02:31 +0000 | [diff] [blame] | 2603 | MipsRldMapSection::MipsRldMapSection() |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 2604 | : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, Config->Wordsize, |
| 2605 | ".rld_map") {} |
Eugene Leviant | 17b7a57 | 2016-11-22 17:49:14 +0000 | [diff] [blame] | 2606 | |
George Rimar | 90a528b | 2017-03-21 09:01:39 +0000 | [diff] [blame] | 2607 | ARMExidxSentinelSection::ARMExidxSentinelSection() |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2608 | : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX, |
George Rimar | 90a528b | 2017-03-21 09:01:39 +0000 | [diff] [blame] | 2609 | Config->Wordsize, ".ARM.exidx") {} |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2610 | |
| 2611 | // Write a terminating sentinel entry to the end of the .ARM.exidx table. |
| 2612 | // This section will have been sorted last in the .ARM.exidx table. |
| 2613 | // This table entry will have the form: |
| 2614 | // | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND | |
Peter Smith | ea79b21 | 2017-05-31 09:02:21 +0000 | [diff] [blame] | 2615 | // The sentinel must have the PREL31 value of an address higher than any |
| 2616 | // address described by any other table entry. |
George Rimar | 90a528b | 2017-03-21 09:01:39 +0000 | [diff] [blame] | 2617 | void ARMExidxSentinelSection::writeTo(uint8_t *Buf) { |
Igor Kudrin | f01caab | 2017-12-14 06:23:50 +0000 | [diff] [blame] | 2618 | assert(Highest); |
Rafael Espindola | 4f058a2 | 2018-03-24 00:35:11 +0000 | [diff] [blame] | 2619 | uint64_t S = Highest->getVA(Highest->getSize()); |
Peter Smith | ea79b21 | 2017-05-31 09:02:21 +0000 | [diff] [blame] | 2620 | uint64_t P = getVA(); |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2621 | Target->relocateOne(Buf, R_ARM_PREL31, S - P); |
Rui Ueyama | 79048e4 | 2017-10-27 03:59:34 +0000 | [diff] [blame] | 2622 | write32le(Buf + 4, 1); |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Igor Kudrin | 5966d15 | 2017-12-20 08:56:10 +0000 | [diff] [blame] | 2625 | // The sentinel has to be removed if there are no other .ARM.exidx entries. |
| 2626 | bool ARMExidxSentinelSection::empty() const { |
George Rimar | 563e4f2 | 2018-02-22 09:55:28 +0000 | [diff] [blame] | 2627 | for (InputSection *IS : getInputSections(getParent())) |
| 2628 | if (!isa<ARMExidxSentinelSection>(IS)) |
| 2629 | return false; |
Igor Kudrin | 5966d15 | 2017-12-20 08:56:10 +0000 | [diff] [blame] | 2630 | return true; |
| 2631 | } |
| 2632 | |
George Rimar | 7b82704 | 2017-03-16 10:40:50 +0000 | [diff] [blame] | 2633 | ThunkSection::ThunkSection(OutputSection *OS, uint64_t Off) |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 2634 | : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 2635 | Config->Wordsize, ".text.thunk") { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 2636 | this->Parent = OS; |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2637 | this->OutSecOff = Off; |
| 2638 | } |
| 2639 | |
George Rimar | 7b82704 | 2017-03-16 10:40:50 +0000 | [diff] [blame] | 2640 | void ThunkSection::addThunk(Thunk *T) { |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2641 | Thunks.push_back(T); |
| 2642 | T->addSymbols(*this); |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
George Rimar | 7b82704 | 2017-03-16 10:40:50 +0000 | [diff] [blame] | 2645 | void ThunkSection::writeTo(uint8_t *Buf) { |
Peter Collingbourne | cebab4a | 2018-03-28 21:33:31 +0000 | [diff] [blame] | 2646 | for (Thunk *T : Thunks) |
| 2647 | T->writeTo(Buf + T->Offset); |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
George Rimar | 7b82704 | 2017-03-16 10:40:50 +0000 | [diff] [blame] | 2650 | InputSection *ThunkSection::getTargetInputSection() const { |
Peter Smith | f0c70f8 | 2017-10-27 08:58:28 +0000 | [diff] [blame] | 2651 | if (Thunks.empty()) |
| 2652 | return nullptr; |
George Rimar | 7b82704 | 2017-03-16 10:40:50 +0000 | [diff] [blame] | 2653 | const Thunk *T = Thunks.front(); |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 2654 | return T->getTargetInputSection(); |
| 2655 | } |
| 2656 | |
Peter Collingbourne | c5391ce | 2018-03-29 22:32:13 +0000 | [diff] [blame] | 2657 | bool ThunkSection::assignOffsets() { |
| 2658 | uint64_t Off = 0; |
| 2659 | for (Thunk *T : Thunks) { |
| 2660 | Off = alignTo(Off, T->Alignment); |
| 2661 | T->setOffset(Off); |
| 2662 | uint32_t Size = T->size(); |
| 2663 | T->getThunkTargetSym()->Size = Size; |
| 2664 | Off += Size; |
| 2665 | } |
| 2666 | bool Changed = Off != Size; |
| 2667 | Size = Off; |
| 2668 | return Changed; |
| 2669 | } |
| 2670 | |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2671 | InputSection *InX::ARMAttributes; |
George Rimar | 1ab9cf4 | 2017-03-17 10:14:53 +0000 | [diff] [blame] | 2672 | BssSection *InX::Bss; |
| 2673 | BssSection *InX::BssRelRo; |
George Rimar | 6c2949d | 2017-03-20 16:40:21 +0000 | [diff] [blame] | 2674 | BuildIdSection *InX::BuildId; |
Rui Ueyama | 02551ab | 2017-10-27 03:14:24 +0000 | [diff] [blame] | 2675 | EhFrameHeader *InX::EhFrameHdr; |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 2676 | EhFrameSection *InX::EhFrame; |
Rafael Espindola | 5ab1989 | 2017-05-11 23:16:43 +0000 | [diff] [blame] | 2677 | SyntheticSection *InX::Dynamic; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2678 | StringTableSection *InX::DynStrTab; |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 2679 | SymbolTableBaseSection *InX::DynSymTab; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2680 | InputSection *InX::Interp; |
George Rimar | 35e846e | 2017-03-21 08:19:34 +0000 | [diff] [blame] | 2681 | GdbIndexSection *InX::GdbIndex; |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 2682 | GotSection *InX::Got; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2683 | GotPltSection *InX::GotPlt; |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 2684 | GnuHashTableSection *InX::GnuHashTab; |
George Rimar | aaf5471 | 2017-09-27 09:14:59 +0000 | [diff] [blame] | 2685 | HashTableSection *InX::HashTab; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2686 | IgotPltSection *InX::IgotPlt; |
George Rimar | 14534eb | 2017-03-20 16:44:28 +0000 | [diff] [blame] | 2687 | MipsGotSection *InX::MipsGot; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2688 | MipsRldMapSection *InX::MipsRldMap; |
George Rimar | dfc020e | 2017-03-17 11:01:57 +0000 | [diff] [blame] | 2689 | PltSection *InX::Plt; |
| 2690 | PltSection *InX::Iplt; |
Rafael Espindola | 58946cd | 2017-12-10 19:44:42 +0000 | [diff] [blame] | 2691 | RelocationBaseSection *InX::RelaDyn; |
Rafael Espindola | 87e0dea | 2017-12-10 20:07:03 +0000 | [diff] [blame] | 2692 | RelocationBaseSection *InX::RelaPlt; |
| 2693 | RelocationBaseSection *InX::RelaIplt; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2694 | StringTableSection *InX::ShStrTab; |
| 2695 | StringTableSection *InX::StrTab; |
George Rimar | f45f681 | 2017-05-16 08:53:30 +0000 | [diff] [blame] | 2696 | SymbolTableBaseSection *InX::SymTab; |
George Rimar | 9782ca5 | 2017-03-15 15:29:29 +0000 | [diff] [blame] | 2697 | |
Rafael Espindola | 300b386 | 2017-07-12 23:56:53 +0000 | [diff] [blame] | 2698 | template GdbIndexSection *elf::createGdbIndex<ELF32LE>(); |
| 2699 | template GdbIndexSection *elf::createGdbIndex<ELF32BE>(); |
| 2700 | template GdbIndexSection *elf::createGdbIndex<ELF64LE>(); |
| 2701 | template GdbIndexSection *elf::createGdbIndex<ELF64BE>(); |
| 2702 | |
Rafael Espindola | f1652d4 | 2018-04-27 18:17:36 +0000 | [diff] [blame] | 2703 | template void elf::splitSections<ELF32LE>(); |
| 2704 | template void elf::splitSections<ELF32BE>(); |
| 2705 | template void elf::splitSections<ELF64LE>(); |
| 2706 | template void elf::splitSections<ELF64BE>(); |
| 2707 | |
Rui Ueyama | 572247f | 2017-10-27 03:13:39 +0000 | [diff] [blame] | 2708 | template void EhFrameSection::addSection<ELF32LE>(InputSectionBase *); |
| 2709 | template void EhFrameSection::addSection<ELF32BE>(InputSectionBase *); |
| 2710 | template void EhFrameSection::addSection<ELF64LE>(InputSectionBase *); |
| 2711 | template void EhFrameSection::addSection<ELF64BE>(InputSectionBase *); |
| 2712 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 2713 | template void PltSection::addEntry<ELF32LE>(Symbol &Sym); |
| 2714 | template void PltSection::addEntry<ELF32BE>(Symbol &Sym); |
| 2715 | template void PltSection::addEntry<ELF64LE>(Symbol &Sym); |
| 2716 | template void PltSection::addEntry<ELF64BE>(Symbol &Sym); |
George Rimar | a918957 | 2017-03-17 16:50:07 +0000 | [diff] [blame] | 2717 | |
Simon Atanasyan | fa03b0f | 2016-11-09 21:37:06 +0000 | [diff] [blame] | 2718 | template class elf::MipsAbiFlagsSection<ELF32LE>; |
| 2719 | template class elf::MipsAbiFlagsSection<ELF32BE>; |
| 2720 | template class elf::MipsAbiFlagsSection<ELF64LE>; |
| 2721 | template class elf::MipsAbiFlagsSection<ELF64BE>; |
| 2722 | |
Simon Atanasyan | ce02cf0 | 2016-11-09 21:36:56 +0000 | [diff] [blame] | 2723 | template class elf::MipsOptionsSection<ELF32LE>; |
| 2724 | template class elf::MipsOptionsSection<ELF32BE>; |
| 2725 | template class elf::MipsOptionsSection<ELF64LE>; |
| 2726 | template class elf::MipsOptionsSection<ELF64BE>; |
| 2727 | |
| 2728 | template class elf::MipsReginfoSection<ELF32LE>; |
| 2729 | template class elf::MipsReginfoSection<ELF32BE>; |
| 2730 | template class elf::MipsReginfoSection<ELF64LE>; |
| 2731 | template class elf::MipsReginfoSection<ELF64BE>; |
| 2732 | |
Eugene Leviant | 6380ce2 | 2016-11-15 12:26:55 +0000 | [diff] [blame] | 2733 | template class elf::DynamicSection<ELF32LE>; |
| 2734 | template class elf::DynamicSection<ELF32BE>; |
| 2735 | template class elf::DynamicSection<ELF64LE>; |
| 2736 | template class elf::DynamicSection<ELF64BE>; |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 2737 | |
| 2738 | template class elf::RelocationSection<ELF32LE>; |
| 2739 | template class elf::RelocationSection<ELF32BE>; |
| 2740 | template class elf::RelocationSection<ELF64LE>; |
| 2741 | template class elf::RelocationSection<ELF64BE>; |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 2742 | |
Peter Collingbourne | 5c54f15 | 2017-10-27 17:49:40 +0000 | [diff] [blame] | 2743 | template class elf::AndroidPackedRelocationSection<ELF32LE>; |
| 2744 | template class elf::AndroidPackedRelocationSection<ELF32BE>; |
| 2745 | template class elf::AndroidPackedRelocationSection<ELF64LE>; |
| 2746 | template class elf::AndroidPackedRelocationSection<ELF64BE>; |
| 2747 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 2748 | template class elf::SymbolTableSection<ELF32LE>; |
| 2749 | template class elf::SymbolTableSection<ELF32BE>; |
| 2750 | template class elf::SymbolTableSection<ELF64LE>; |
| 2751 | template class elf::SymbolTableSection<ELF64BE>; |
Eugene Leviant | be809a7 | 2016-11-18 06:44:18 +0000 | [diff] [blame] | 2752 | |
Eugene Leviant | e9bab5d | 2016-11-21 16:59:33 +0000 | [diff] [blame] | 2753 | template class elf::VersionTableSection<ELF32LE>; |
| 2754 | template class elf::VersionTableSection<ELF32BE>; |
| 2755 | template class elf::VersionTableSection<ELF64LE>; |
| 2756 | template class elf::VersionTableSection<ELF64BE>; |
| 2757 | |
| 2758 | template class elf::VersionNeedSection<ELF32LE>; |
| 2759 | template class elf::VersionNeedSection<ELF32BE>; |
| 2760 | template class elf::VersionNeedSection<ELF64LE>; |
| 2761 | template class elf::VersionNeedSection<ELF64BE>; |
| 2762 | |
| 2763 | template class elf::VersionDefinitionSection<ELF32LE>; |
| 2764 | template class elf::VersionDefinitionSection<ELF32BE>; |
| 2765 | template class elf::VersionDefinitionSection<ELF64LE>; |
| 2766 | template class elf::VersionDefinitionSection<ELF64BE>; |