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