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