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