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