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