Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1 | //===- OutputSections.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 | #include "OutputSections.h" |
| 11 | #include "Config.h" |
Rui Ueyama | f5febef | 2016-05-24 02:55:45 +0000 | [diff] [blame] | 12 | #include "EhFrame.h" |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 13 | #include "GdbIndex.h" |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 14 | #include "LinkerScript.h" |
| 15 | #include "Memory.h" |
Rui Ueyama | fbbde54 | 2016-06-29 09:08:02 +0000 | [diff] [blame] | 16 | #include "Strings.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 17 | #include "SymbolTable.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 18 | #include "SyntheticSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 19 | #include "Target.h" |
Rui Ueyama | e980950 | 2016-03-11 04:23:12 +0000 | [diff] [blame] | 20 | #include "lld/Core/Parallel.h" |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Dwarf.h" |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MD5.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | a42b3bc | 2016-09-27 16:43:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SHA1.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 25 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Rui Ueyama | dbcfedb | 2016-02-09 21:46:11 +0000 | [diff] [blame] | 27 | using namespace llvm::dwarf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 28 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 29 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 30 | using namespace llvm::ELF; |
| 31 | |
| 32 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 33 | using namespace lld::elf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 34 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 35 | OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type, |
| 36 | uint64_t Flags) |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 37 | : Name(Name) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 38 | this->Type = Type; |
| 39 | this->Flags = Flags; |
| 40 | this->Addralign = 1; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 43 | uint32_t OutputSectionBase::getPhdrFlags() const { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 44 | uint32_t Ret = PF_R; |
| 45 | if (Flags & SHF_WRITE) |
| 46 | Ret |= PF_W; |
| 47 | if (Flags & SHF_EXECINSTR) |
| 48 | Ret |= PF_X; |
| 49 | return Ret; |
| 50 | } |
| 51 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 52 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 53 | void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 54 | Shdr->sh_entsize = Entsize; |
| 55 | Shdr->sh_addralign = Addralign; |
| 56 | Shdr->sh_type = Type; |
| 57 | Shdr->sh_offset = Offset; |
| 58 | Shdr->sh_flags = Flags; |
| 59 | Shdr->sh_info = Info; |
| 60 | Shdr->sh_link = Link; |
| 61 | Shdr->sh_addr = Addr; |
| 62 | Shdr->sh_size = Size; |
| 63 | Shdr->sh_name = ShName; |
Rui Ueyama | c63c1db | 2016-03-13 06:50:33 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | template <class ELFT> |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 67 | GdbIndexSection<ELFT>::GdbIndexSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 68 | : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {} |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 69 | |
| 70 | template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() { |
| 71 | std::vector<InputSection<ELFT> *> &IS = |
| 72 | static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections; |
| 73 | |
| 74 | for (InputSection<ELFT> *I : IS) |
| 75 | readDwarf(I); |
| 76 | } |
| 77 | |
| 78 | template <class ELFT> |
| 79 | void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) { |
| 80 | std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I); |
| 81 | CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end()); |
| 82 | } |
| 83 | |
| 84 | template <class ELFT> void GdbIndexSection<ELFT>::finalize() { |
| 85 | parseDebugSections(); |
| 86 | |
| 87 | // GdbIndex header consist from version fields |
| 88 | // and 5 more fields with different kinds of offsets. |
| 89 | CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 90 | this->Size = CuTypesOffset; |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) { |
| 94 | write32le(Buf, 7); // Write Version |
| 95 | write32le(Buf + 4, CuListOffset); // CU list offset |
| 96 | write32le(Buf + 8, CuTypesOffset); // Types CU list offset |
| 97 | write32le(Buf + 12, CuTypesOffset); // Address area offset |
| 98 | write32le(Buf + 16, CuTypesOffset); // Symbol table offset |
| 99 | write32le(Buf + 20, CuTypesOffset); // Constant pool offset |
| 100 | Buf += 24; |
| 101 | |
| 102 | // Write the CU list. |
| 103 | for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) { |
| 104 | write64le(Buf, CU.first); |
| 105 | write64le(Buf + 8, CU.second); |
| 106 | Buf += 16; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 111 | GotPltSection<ELFT>::GotPltSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 112 | : OutputSectionBase(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 113 | this->Addralign = Target->GotPltEntrySize; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 116 | template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 117 | Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size(); |
| 118 | Entries.push_back(&Sym); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | template <class ELFT> bool GotPltSection<ELFT>::empty() const { |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 122 | return Entries.empty(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 123 | } |
| 124 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 125 | template <class ELFT> void GotPltSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 126 | this->Size = (Target->GotPltHeaderEntriesNum + Entries.size()) * |
| 127 | Target->GotPltEntrySize; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 131 | Target->writeGotPltHeader(Buf); |
Rui Ueyama | 803b120 | 2016-07-13 18:55:14 +0000 | [diff] [blame] | 132 | Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 133 | for (const SymbolBody *B : Entries) { |
Rui Ueyama | c9fee5f | 2016-06-16 16:14:50 +0000 | [diff] [blame] | 134 | Target->writeGotPlt(Buf, *B); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 135 | Buf += sizeof(uintX_t); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 140 | GotSection<ELFT>::GotSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 141 | : OutputSectionBase(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 142 | if (Config->EMachine == EM_MIPS) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 143 | this->Flags |= SHF_MIPS_GPREL; |
| 144 | this->Addralign = Target->GotEntrySize; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 147 | template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) { |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 148 | Sym.GotIndex = Entries.size(); |
| 149 | Entries.push_back(&Sym); |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 152 | template <class ELFT> |
| 153 | void GotSection<ELFT>::addMipsEntry(SymbolBody &Sym, uintX_t Addend, |
| 154 | RelExpr Expr) { |
| 155 | // For "true" local symbols which can be referenced from the same module |
| 156 | // only compiler creates two instructions for address loading: |
| 157 | // |
| 158 | // lw $8, 0($gp) # R_MIPS_GOT16 |
| 159 | // addi $8, $8, 0 # R_MIPS_LO16 |
| 160 | // |
| 161 | // The first instruction loads high 16 bits of the symbol address while |
| 162 | // the second adds an offset. That allows to reduce number of required |
| 163 | // GOT entries because only one global offset table entry is necessary |
| 164 | // for every 64 KBytes of local data. So for local symbols we need to |
| 165 | // allocate number of GOT entries to hold all required "page" addresses. |
| 166 | // |
| 167 | // All global symbols (hidden and regular) considered by compiler uniformly. |
| 168 | // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation |
| 169 | // to load address of the symbol. So for each such symbol we need to |
| 170 | // allocate dedicated GOT entry to store its address. |
| 171 | // |
| 172 | // If a symbol is preemptible we need help of dynamic linker to get its |
| 173 | // final address. The corresponding GOT entries are allocated in the |
| 174 | // "global" part of GOT. Entries for non preemptible global symbol allocated |
| 175 | // in the "local" part of GOT. |
| 176 | // |
| 177 | // See "Global Offset Table" in Chapter 5: |
| 178 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 179 | if (Expr == R_MIPS_GOT_LOCAL_PAGE) { |
| 180 | // At this point we do not know final symbol value so to reduce number |
| 181 | // of allocated GOT entries do the following trick. Save all output |
| 182 | // sections referenced by GOT relocations. Then later in the `finalize` |
| 183 | // method calculate number of "pages" required to cover all saved output |
| 184 | // section and allocate appropriate number of GOT entries. |
| 185 | auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec; |
| 186 | MipsOutSections.insert(OutSec); |
| 187 | return; |
| 188 | } |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 189 | if (Sym.isTls()) { |
| 190 | // GOT entries created for MIPS TLS relocations behave like |
| 191 | // almost GOT entries from other ABIs. They go to the end |
| 192 | // of the global offset table. |
| 193 | Sym.GotIndex = Entries.size(); |
| 194 | Entries.push_back(&Sym); |
| 195 | return; |
| 196 | } |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 197 | auto AddEntry = [&](SymbolBody &S, uintX_t A, MipsGotEntries &Items) { |
| 198 | if (S.isInGot() && !A) |
| 199 | return; |
| 200 | size_t NewIndex = Items.size(); |
| 201 | if (!MipsGotMap.insert({{&S, A}, NewIndex}).second) |
| 202 | return; |
| 203 | Items.emplace_back(&S, A); |
| 204 | if (!A) |
| 205 | S.GotIndex = NewIndex; |
| 206 | }; |
| 207 | if (Sym.isPreemptible()) { |
| 208 | // Ignore addends for preemptible symbols. They got single GOT entry anyway. |
| 209 | AddEntry(Sym, 0, MipsGlobal); |
| 210 | Sym.IsInGlobalMipsGot = true; |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 211 | } else if (Expr == R_MIPS_GOT_OFF32) { |
| 212 | AddEntry(Sym, Addend, MipsLocal32); |
| 213 | Sym.Is32BitMipsGot = true; |
| 214 | } else { |
| 215 | // Hold local GOT entries accessed via a 16-bit index separately. |
| 216 | // That allows to write them in the beginning of the GOT and keep |
| 217 | // their indexes as less as possible to escape relocation's overflow. |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 218 | AddEntry(Sym, Addend, MipsLocal); |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 219 | } |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 222 | template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) { |
Rafael Espindola | 6211d9a | 2016-06-05 19:03:28 +0000 | [diff] [blame] | 223 | if (Sym.GlobalDynIndex != -1U) |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 224 | return false; |
Rafael Espindola | 6211d9a | 2016-06-05 19:03:28 +0000 | [diff] [blame] | 225 | Sym.GlobalDynIndex = Entries.size(); |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 226 | // Global Dynamic TLS entries take two GOT slots. |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 227 | Entries.push_back(nullptr); |
Rafael Espindola | a8777c2 | 2016-06-08 21:31:59 +0000 | [diff] [blame] | 228 | Entries.push_back(&Sym); |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 229 | return true; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 232 | // Reserves TLS entries for a TLS module ID and a TLS block offset. |
| 233 | // In total it takes two GOT slots. |
| 234 | template <class ELFT> bool GotSection<ELFT>::addTlsIndex() { |
| 235 | if (TlsIndexOff != uint32_t(-1)) |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 236 | return false; |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 237 | TlsIndexOff = Entries.size() * sizeof(uintX_t); |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 238 | Entries.push_back(nullptr); |
| 239 | Entries.push_back(nullptr); |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 240 | return true; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 243 | template <class ELFT> |
| 244 | typename GotSection<ELFT>::uintX_t |
Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 245 | GotSection<ELFT>::getMipsLocalPageOffset(uintX_t EntryValue) { |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 246 | // Initialize the entry by the %hi(EntryValue) expression |
| 247 | // but without right-shifting. |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 248 | EntryValue = (EntryValue + 0x8000) & ~0xffff; |
Simon Atanasyan | 1ef1bf8 | 2016-04-25 20:25:05 +0000 | [diff] [blame] | 249 | // Take into account MIPS GOT header. |
| 250 | // See comment in the GotSection::writeTo. |
| 251 | size_t NewIndex = MipsLocalGotPos.size() + 2; |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 252 | auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex)); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 253 | assert(!P.second || MipsLocalGotPos.size() <= MipsPageEntries); |
George Rimar | 71e64b2 | 2016-05-11 09:41:15 +0000 | [diff] [blame] | 254 | return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 257 | template <class ELFT> |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 258 | typename GotSection<ELFT>::uintX_t |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 259 | GotSection<ELFT>::getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const { |
Simon Atanasyan | f4415a3 | 2016-10-20 17:53:55 +0000 | [diff] [blame] | 260 | // Calculate offset of the GOT entries block: TLS, global, local. |
| 261 | uintX_t GotBlockOff; |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 262 | if (B.isTls()) |
Simon Atanasyan | f4415a3 | 2016-10-20 17:53:55 +0000 | [diff] [blame] | 263 | GotBlockOff = getMipsTlsOffset(); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 264 | else if (B.IsInGlobalMipsGot) |
Simon Atanasyan | f4415a3 | 2016-10-20 17:53:55 +0000 | [diff] [blame] | 265 | GotBlockOff = getMipsLocalEntriesNum() * sizeof(uintX_t); |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 266 | else if (B.Is32BitMipsGot) |
| 267 | GotBlockOff = (MipsPageEntries + MipsLocal.size()) * sizeof(uintX_t); |
Simon Atanasyan | f4415a3 | 2016-10-20 17:53:55 +0000 | [diff] [blame] | 268 | else |
| 269 | GotBlockOff = MipsPageEntries * sizeof(uintX_t); |
| 270 | // Calculate index of the GOT entry in the block. |
| 271 | uintX_t GotIndex; |
| 272 | if (B.isInGot()) |
| 273 | GotIndex = B.GotIndex; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 274 | else { |
| 275 | auto It = MipsGotMap.find({&B, Addend}); |
| 276 | assert(It != MipsGotMap.end()); |
Simon Atanasyan | f4415a3 | 2016-10-20 17:53:55 +0000 | [diff] [blame] | 277 | GotIndex = It->second; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 278 | } |
Simon Atanasyan | f4415a3 | 2016-10-20 17:53:55 +0000 | [diff] [blame] | 279 | return GotBlockOff + GotIndex * sizeof(uintX_t) - MipsGPOffset; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | template <class ELFT> |
Simon Atanasyan | bc94693 | 2016-10-19 17:13:43 +0000 | [diff] [blame] | 283 | typename GotSection<ELFT>::uintX_t GotSection<ELFT>::getMipsTlsOffset() const { |
Simon Atanasyan | badd5b3 | 2016-10-20 17:53:59 +0000 | [diff] [blame] | 284 | return (getMipsLocalEntriesNum() + MipsGlobal.size()) * sizeof(uintX_t); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | template <class ELFT> |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 288 | typename GotSection<ELFT>::uintX_t |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 289 | GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 290 | return this->Addr + B.GlobalDynIndex * sizeof(uintX_t); |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | template <class ELFT> |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 294 | typename GotSection<ELFT>::uintX_t |
| 295 | GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const { |
Rafael Espindola | 6211d9a | 2016-06-05 19:03:28 +0000 | [diff] [blame] | 296 | return B.GlobalDynIndex * sizeof(uintX_t); |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | template <class ELFT> |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 300 | const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const { |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 301 | return MipsGlobal.empty() ? nullptr : MipsGlobal.front().first; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | template <class ELFT> |
| 305 | unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const { |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 306 | return MipsPageEntries + MipsLocal.size() + MipsLocal32.size(); |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 309 | template <class ELFT> void GotSection<ELFT>::finalize() { |
Simon Atanasyan | 311b4b1 | 2016-06-10 12:26:28 +0000 | [diff] [blame] | 310 | size_t EntriesNum = Entries.size(); |
| 311 | if (Config->EMachine == EM_MIPS) { |
Simon Atanasyan | 1ef1bf8 | 2016-04-25 20:25:05 +0000 | [diff] [blame] | 312 | // Take into account MIPS GOT header. |
| 313 | // See comment in the GotSection::writeTo. |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 314 | MipsPageEntries += 2; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 315 | for (const OutputSectionBase *OutSec : MipsOutSections) { |
Simon Atanasyan | 311b4b1 | 2016-06-10 12:26:28 +0000 | [diff] [blame] | 316 | // Calculate an upper bound of MIPS GOT entries required to store page |
| 317 | // addresses of local symbols. We assume the worst case - each 64kb |
| 318 | // page of the output section has at least one GOT relocation against it. |
| 319 | // Add 0x8000 to the section's size because the page address stored |
| 320 | // in the GOT entry is calculated as (value + 0x8000) & ~0xffff. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 321 | MipsPageEntries += (OutSec->Size + 0x8000 + 0xfffe) / 0xffff; |
Simon Atanasyan | 311b4b1 | 2016-06-10 12:26:28 +0000 | [diff] [blame] | 322 | } |
Simon Atanasyan | badd5b3 | 2016-10-20 17:53:59 +0000 | [diff] [blame] | 323 | EntriesNum += getMipsLocalEntriesNum() + MipsGlobal.size(); |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 324 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 325 | this->Size = EntriesNum * sizeof(uintX_t); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 328 | template <class ELFT> |
| 329 | static void writeUint(uint8_t *Buf, typename ELFT::uint Val) { |
| 330 | typedef typename ELFT::uint uintX_t; |
| 331 | write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val); |
| 332 | } |
| 333 | |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 334 | template <class ELFT> void GotSection<ELFT>::writeMipsGot(uint8_t *Buf) { |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 335 | // Set the MSB of the second GOT slot. This is not required by any |
| 336 | // MIPS ABI documentation, though. |
| 337 | // |
| 338 | // There is a comment in glibc saying that "The MSB of got[1] of a |
| 339 | // gnu object is set to identify gnu objects," and in GNU gold it |
| 340 | // says "the second entry will be used by some runtime loaders". |
| 341 | // But how this field is being used is unclear. |
| 342 | // |
| 343 | // We are not really willing to mimic other linkers behaviors |
| 344 | // without understanding why they do that, but because all files |
| 345 | // generated by GNU tools have this special GOT value, and because |
| 346 | // we've been doing this for years, it is probably a safe bet to |
| 347 | // keep doing this for now. We really need to revisit this to see |
| 348 | // if we had to do this. |
| 349 | auto *P = reinterpret_cast<typename ELFT::Off *>(Buf); |
| 350 | P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31); |
| 351 | // Write 'page address' entries to the local part of the GOT. |
| 352 | for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) { |
| 353 | uint8_t *Entry = Buf + L.second * sizeof(uintX_t); |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 354 | writeUint<ELFT>(Entry, L.first); |
Simon Atanasyan | 1ef1bf8 | 2016-04-25 20:25:05 +0000 | [diff] [blame] | 355 | } |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 356 | Buf += MipsPageEntries * sizeof(uintX_t); |
| 357 | auto AddEntry = [&](const MipsGotEntry &SA) { |
| 358 | uint8_t *Entry = Buf; |
| 359 | Buf += sizeof(uintX_t); |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 360 | const SymbolBody *Body = SA.first; |
George Rimar | 06e930d | 2016-06-20 10:01:50 +0000 | [diff] [blame] | 361 | uintX_t VA = Body->template getVA<ELFT>(SA.second); |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 362 | writeUint<ELFT>(Entry, VA); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 363 | }; |
| 364 | std::for_each(std::begin(MipsLocal), std::end(MipsLocal), AddEntry); |
Simon Atanasyan | bed04bf | 2016-10-21 07:22:30 +0000 | [diff] [blame] | 365 | std::for_each(std::begin(MipsLocal32), std::end(MipsLocal32), AddEntry); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 366 | std::for_each(std::begin(MipsGlobal), std::end(MipsGlobal), AddEntry); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 367 | // Initialize TLS-related GOT entries. If the entry has a corresponding |
| 368 | // dynamic relocations, leave it initialized by zero. Write down adjusted |
| 369 | // TLS symbol's values otherwise. To calculate the adjustments use offsets |
| 370 | // for thread-local storage. |
| 371 | // https://www.linux-mips.org/wiki/NPTL |
| 372 | if (TlsIndexOff != -1U && !Config->Pic) |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 373 | writeUint<ELFT>(Buf + TlsIndexOff, 1); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 374 | for (const SymbolBody *B : Entries) { |
| 375 | if (!B || B->isPreemptible()) |
| 376 | continue; |
| 377 | uintX_t VA = B->getVA<ELFT>(); |
| 378 | if (B->GotIndex != -1U) { |
| 379 | uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t); |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 380 | writeUint<ELFT>(Entry, VA - 0x7000); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 381 | } |
| 382 | if (B->GlobalDynIndex != -1U) { |
| 383 | uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t); |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 384 | writeUint<ELFT>(Entry, 1); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 385 | Entry += sizeof(uintX_t); |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 386 | writeUint<ELFT>(Entry, VA - 0x8000); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) { |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 392 | if (Config->EMachine == EM_MIPS) { |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 393 | writeMipsGot(Buf); |
Simon Atanasyan | 919a58c | 2016-09-08 09:07:19 +0000 | [diff] [blame] | 394 | return; |
| 395 | } |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 396 | for (const SymbolBody *B : Entries) { |
Rafael Espindola | e782f67 | 2015-10-07 03:56:05 +0000 | [diff] [blame] | 397 | uint8_t *Entry = Buf; |
| 398 | Buf += sizeof(uintX_t); |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 399 | if (!B) |
| 400 | continue; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 401 | if (B->isPreemptible()) |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 402 | continue; // The dynamic linker will take care of it. |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 403 | uintX_t VA = B->getVA<ELFT>(); |
Rui Ueyama | c442cff | 2016-09-08 21:46:21 +0000 | [diff] [blame] | 404 | writeUint<ELFT>(Entry, VA); |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 408 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 409 | PltSection<ELFT>::PltSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 410 | : OutputSectionBase(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 411 | this->Addralign = 16; |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 414 | template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | e4c86d83 | 2016-05-18 21:03:36 +0000 | [diff] [blame] | 415 | // At beginning of PLT, we have code to call the dynamic linker |
| 416 | // to resolve dynsyms at runtime. Write such code. |
Rui Ueyama | 4a90f57 | 2016-06-16 16:28:50 +0000 | [diff] [blame] | 417 | Target->writePltHeader(Buf); |
| 418 | size_t Off = Target->PltHeaderSize; |
Rui Ueyama | f57a590 | 2016-05-20 21:39:07 +0000 | [diff] [blame] | 419 | |
George Rimar | fb5d7f2 | 2015-11-26 19:58:51 +0000 | [diff] [blame] | 420 | for (auto &I : Entries) { |
Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 421 | const SymbolBody *B = I.first; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 422 | unsigned RelOff = I.second; |
Rafael Espindola | e4c86d83 | 2016-05-18 21:03:36 +0000 | [diff] [blame] | 423 | uint64_t Got = B->getGotPltVA<ELFT>(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 424 | uint64_t Plt = this->Addr + Off; |
Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 425 | Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff); |
Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 426 | Off += Target->PltEntrySize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 430 | template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) { |
| 431 | Sym.PltIndex = Entries.size(); |
Rafael Espindola | e4c86d83 | 2016-05-18 21:03:36 +0000 | [diff] [blame] | 432 | unsigned RelOff = Out<ELFT>::RelaPlt->getRelocOffset(); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 433 | Entries.push_back(std::make_pair(&Sym, RelOff)); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 434 | } |
| 435 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 436 | template <class ELFT> void PltSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 437 | this->Size = Target->PltHeaderSize + Entries.size() * Target->PltEntrySize; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | template <class ELFT> |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 441 | RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 442 | : OutputSectionBase(Name, Config->Rela ? SHT_RELA : SHT_REL, SHF_ALLOC), |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 443 | Sort(Sort) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 444 | this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 445 | this->Addralign = sizeof(uintX_t); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 446 | } |
| 447 | |
George Rimar | 5828c23 | 2015-11-30 17:49:19 +0000 | [diff] [blame] | 448 | template <class ELFT> |
Rafael Espindola | d30eb7d | 2016-02-05 15:03:10 +0000 | [diff] [blame] | 449 | void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) { |
Eugene Leviant | aa49819 | 2016-08-31 08:51:39 +0000 | [diff] [blame] | 450 | if (Reloc.Type == Target->RelativeRel) |
| 451 | ++NumRelativeRelocs; |
Rafael Espindola | d30eb7d | 2016-02-05 15:03:10 +0000 | [diff] [blame] | 452 | Relocs.push_back(Reloc); |
| 453 | } |
| 454 | |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 455 | template <class ELFT, class RelTy> |
| 456 | static bool compRelocations(const RelTy &A, const RelTy &B) { |
Eugene Leviant | aa49819 | 2016-08-31 08:51:39 +0000 | [diff] [blame] | 457 | bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel; |
| 458 | bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel; |
| 459 | if (AIsRel != BIsRel) |
| 460 | return AIsRel; |
| 461 | |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 462 | return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL); |
| 463 | } |
| 464 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 465 | template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) { |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 466 | uint8_t *BufBegin = Buf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 467 | for (const DynamicReloc<ELFT> &Rel : Relocs) { |
Rui Ueyama | 614be59 | 2016-03-13 05:23:40 +0000 | [diff] [blame] | 468 | auto *P = reinterpret_cast<Elf_Rela *>(Buf); |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 469 | Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
Rui Ueyama | b0210e83 | 2016-01-26 00:24:57 +0000 | [diff] [blame] | 470 | |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 471 | if (Config->Rela) |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 472 | P->r_addend = Rel.getAddend(); |
| 473 | P->r_offset = Rel.getOffset(); |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 474 | if (Config->EMachine == EM_MIPS && Rel.getOutputSec() == Out<ELFT>::Got) |
| 475 | // Dynamic relocation against MIPS GOT section make deal TLS entries |
| 476 | // allocated in the end of the GOT. We need to adjust the offset to take |
| 477 | // in account 'local' and 'global' GOT entries. |
| 478 | P->r_offset += Out<ELFT>::Got->getMipsTlsOffset(); |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 479 | P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 480 | } |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 481 | |
| 482 | if (Sort) { |
| 483 | if (Config->Rela) |
| 484 | std::stable_sort((Elf_Rela *)BufBegin, |
| 485 | (Elf_Rela *)BufBegin + Relocs.size(), |
| 486 | compRelocations<ELFT, Elf_Rela>); |
| 487 | else |
| 488 | std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(), |
| 489 | compRelocations<ELFT, Elf_Rel>); |
| 490 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 491 | } |
| 492 | |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 493 | template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 494 | return this->Entsize * Relocs.size(); |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 497 | template <class ELFT> void RelocationSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 498 | this->Link = Out<ELFT>::DynSymTab ? Out<ELFT>::DynSymTab->SectionIndex |
| 499 | : Out<ELFT>::SymTab->SectionIndex; |
| 500 | this->Size = Relocs.size() * this->Entsize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 503 | template <class ELFT> |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 504 | HashTableSection<ELFT>::HashTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 505 | : OutputSectionBase(".hash", SHT_HASH, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 506 | this->Entsize = sizeof(Elf_Word); |
| 507 | this->Addralign = sizeof(Elf_Word); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 510 | template <class ELFT> void HashTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 511 | this->Link = Out<ELFT>::DynSymTab->SectionIndex; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 512 | |
George Rimar | e9e1d32 | 2016-02-18 15:17:01 +0000 | [diff] [blame] | 513 | unsigned NumEntries = 2; // nbucket and nchain. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 514 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries. |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 515 | |
| 516 | // Create as many buckets as there are symbols. |
| 517 | // FIXME: This is simplistic. We can try to optimize it, but implementing |
| 518 | // support for SHT_GNU_HASH is probably even more profitable. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 519 | NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 520 | this->Size = NumEntries * sizeof(Elf_Word); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 524 | unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 525 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 526 | *P++ = NumSymbols; // nbucket |
| 527 | *P++ = NumSymbols; // nchain |
| 528 | |
| 529 | Elf_Word *Buckets = P; |
| 530 | Elf_Word *Chains = P + NumSymbols; |
| 531 | |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 532 | for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) { |
| 533 | SymbolBody *Body = S.Symbol; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 534 | StringRef Name = Body->getName(); |
Rui Ueyama | 572a6f7 | 2016-01-29 01:49:33 +0000 | [diff] [blame] | 535 | unsigned I = Body->DynsymIndex; |
Davide Italiano | e7fd0be | 2016-11-07 21:56:56 +0000 | [diff] [blame] | 536 | uint32_t Hash = hashSysV(Name) % NumSymbols; |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 537 | Chains[I] = Buckets[Hash]; |
| 538 | Buckets[Hash] = I; |
| 539 | } |
| 540 | } |
| 541 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 542 | static uint32_t hashGnu(StringRef Name) { |
| 543 | uint32_t H = 5381; |
| 544 | for (uint8_t C : Name) |
| 545 | H = (H << 5) + H + C; |
| 546 | return H; |
| 547 | } |
| 548 | |
| 549 | template <class ELFT> |
| 550 | GnuHashTableSection<ELFT>::GnuHashTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 551 | : OutputSectionBase(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 552 | this->Entsize = ELFT::Is64Bits ? 0 : 4; |
| 553 | this->Addralign = sizeof(uintX_t); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | template <class ELFT> |
| 557 | unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) { |
| 558 | if (!NumHashed) |
| 559 | return 0; |
| 560 | |
| 561 | // These values are prime numbers which are not greater than 2^(N-1) + 1. |
| 562 | // In result, for any particular NumHashed we return a prime number |
| 563 | // which is not greater than NumHashed. |
| 564 | static const unsigned Primes[] = { |
| 565 | 1, 1, 3, 3, 7, 13, 31, 61, 127, 251, |
| 566 | 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071}; |
| 567 | |
| 568 | return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed), |
| 569 | array_lengthof(Primes) - 1)]; |
| 570 | } |
| 571 | |
| 572 | // Bloom filter estimation: at least 8 bits for each hashed symbol. |
| 573 | // GNU Hash table requirement: it should be a power of 2, |
| 574 | // the minimum value is 1, even for an empty table. |
| 575 | // Expected results for a 32-bit target: |
| 576 | // calcMaskWords(0..4) = 1 |
| 577 | // calcMaskWords(5..8) = 2 |
| 578 | // calcMaskWords(9..16) = 4 |
| 579 | // For a 64-bit target: |
| 580 | // calcMaskWords(0..8) = 1 |
| 581 | // calcMaskWords(9..16) = 2 |
| 582 | // calcMaskWords(17..32) = 4 |
| 583 | template <class ELFT> |
| 584 | unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) { |
| 585 | if (!NumHashed) |
| 586 | return 1; |
| 587 | return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off)); |
| 588 | } |
| 589 | |
| 590 | template <class ELFT> void GnuHashTableSection<ELFT>::finalize() { |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 591 | unsigned NumHashed = Symbols.size(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 592 | NBuckets = calcNBuckets(NumHashed); |
| 593 | MaskWords = calcMaskWords(NumHashed); |
| 594 | // Second hash shift estimation: just predefined values. |
| 595 | Shift2 = ELFT::Is64Bits ? 6 : 5; |
| 596 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 597 | this->Link = Out<ELFT>::DynSymTab->SectionIndex; |
| 598 | this->Size = sizeof(Elf_Word) * 4 // Header |
| 599 | + sizeof(Elf_Off) * MaskWords // Bloom Filter |
| 600 | + sizeof(Elf_Word) * NBuckets // Hash Buckets |
| 601 | + sizeof(Elf_Word) * NumHashed; // Hash Values |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 605 | writeHeader(Buf); |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 606 | if (Symbols.empty()) |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 607 | return; |
| 608 | writeBloomFilter(Buf); |
| 609 | writeHashTable(Buf); |
| 610 | } |
| 611 | |
| 612 | template <class ELFT> |
| 613 | void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) { |
| 614 | auto *P = reinterpret_cast<Elf_Word *>(Buf); |
| 615 | *P++ = NBuckets; |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 616 | *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size(); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 617 | *P++ = MaskWords; |
| 618 | *P++ = Shift2; |
| 619 | Buf = reinterpret_cast<uint8_t *>(P); |
| 620 | } |
| 621 | |
| 622 | template <class ELFT> |
| 623 | void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) { |
| 624 | unsigned C = sizeof(Elf_Off) * 8; |
| 625 | |
| 626 | auto *Masks = reinterpret_cast<Elf_Off *>(Buf); |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 627 | for (const SymbolData &Sym : Symbols) { |
| 628 | size_t Pos = (Sym.Hash / C) & (MaskWords - 1); |
| 629 | uintX_t V = (uintX_t(1) << (Sym.Hash % C)) | |
| 630 | (uintX_t(1) << ((Sym.Hash >> Shift2) % C)); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 631 | Masks[Pos] |= V; |
| 632 | } |
| 633 | Buf += sizeof(Elf_Off) * MaskWords; |
| 634 | } |
| 635 | |
| 636 | template <class ELFT> |
| 637 | void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) { |
| 638 | Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf); |
| 639 | Elf_Word *Values = Buckets + NBuckets; |
| 640 | |
| 641 | int PrevBucket = -1; |
| 642 | int I = 0; |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 643 | for (const SymbolData &Sym : Symbols) { |
| 644 | int Bucket = Sym.Hash % NBuckets; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 645 | assert(PrevBucket <= Bucket); |
| 646 | if (Bucket != PrevBucket) { |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 647 | Buckets[Bucket] = Sym.Body->DynsymIndex; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 648 | PrevBucket = Bucket; |
| 649 | if (I > 0) |
| 650 | Values[I - 1] |= 1; |
| 651 | } |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 652 | Values[I] = Sym.Hash & ~1; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 653 | ++I; |
| 654 | } |
| 655 | if (I > 0) |
| 656 | Values[I - 1] |= 1; |
| 657 | } |
| 658 | |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 659 | // Add symbols to this symbol hash table. Note that this function |
| 660 | // destructively sort a given vector -- which is needed because |
| 661 | // GNU-style hash table places some sorting requirements. |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 662 | template <class ELFT> |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 663 | void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) { |
Davide Italiano | b4b68b6 | 2016-07-04 19:49:55 +0000 | [diff] [blame] | 664 | // Ideally this will just be 'auto' but GCC 6.1 is not able |
| 665 | // to deduce it correctly. |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 666 | std::vector<SymbolTableEntry>::iterator Mid = |
| 667 | std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) { |
| 668 | return S.Symbol->isUndefined(); |
| 669 | }); |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 670 | if (Mid == V.end()) |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 671 | return; |
Davide Italiano | f8591cf | 2016-07-06 17:41:55 +0000 | [diff] [blame] | 672 | for (auto I = Mid, E = V.end(); I != E; ++I) { |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 673 | SymbolBody *B = I->Symbol; |
Reid Kleckner | 2918d0b | 2016-10-20 00:13:34 +0000 | [diff] [blame] | 674 | size_t StrOff = I->StrTabOffset; |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 675 | Symbols.push_back({B, StrOff, hashGnu(B->getName())}); |
| 676 | } |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 677 | |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 678 | unsigned NBuckets = calcNBuckets(Symbols.size()); |
| 679 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 680 | [&](const SymbolData &L, const SymbolData &R) { |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 681 | return L.Hash % NBuckets < R.Hash % NBuckets; |
| 682 | }); |
| 683 | |
Rui Ueyama | 91c0a5d | 2016-02-17 05:40:01 +0000 | [diff] [blame] | 684 | V.erase(Mid, V.end()); |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 685 | for (const SymbolData &Sym : Symbols) |
| 686 | V.push_back({Sym.Body, Sym.STName}); |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 687 | } |
| 688 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 689 | // Returns the number of version definition entries. Because the first entry |
| 690 | // is for the version definition itself, it is the number of versioned symbols |
| 691 | // plus one. Note that we don't support multiple versions yet. |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 692 | static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 693 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 694 | template <class ELFT> |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 695 | DynamicSection<ELFT>::DynamicSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 696 | : OutputSectionBase(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 697 | this->Addralign = sizeof(uintX_t); |
| 698 | this->Entsize = ELFT::Is64Bits ? 16 : 8; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 699 | |
| 700 | // .dynamic section is not writable on MIPS. |
| 701 | // See "Special Section" in Chapter 4 in the following document: |
| 702 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 703 | if (Config->EMachine == EM_MIPS) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 704 | this->Flags = SHF_ALLOC; |
Rui Ueyama | a959393 | 2016-11-02 02:18:01 +0000 | [diff] [blame] | 705 | |
| 706 | addEntries(); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Rui Ueyama | a959393 | 2016-11-02 02:18:01 +0000 | [diff] [blame] | 709 | // There are some dynamic entries that don't depend on other sections. |
| 710 | // Such entries can be set early. |
| 711 | template <class ELFT> void DynamicSection<ELFT>::addEntries() { |
| 712 | // Add strings to .dynstr early so that .dynstr's size will be |
| 713 | // fixed early. |
George Rimar | b952ece | 2016-09-02 09:13:05 +0000 | [diff] [blame] | 714 | for (StringRef S : Config->AuxiliaryList) |
| 715 | Add({DT_AUXILIARY, Out<ELFT>::DynStrTab->addString(S)}); |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 716 | if (!Config->RPath.empty()) |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 717 | Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH, |
| 718 | Out<ELFT>::DynStrTab->addString(Config->RPath)}); |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 719 | for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles()) |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 720 | if (F->isNeeded()) |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 721 | Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())}); |
| 722 | if (!Config->SoName.empty()) |
| 723 | Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)}); |
| 724 | |
Rui Ueyama | a959393 | 2016-11-02 02:18:01 +0000 | [diff] [blame] | 725 | // Set DT_FLAGS and DT_FLAGS_1. |
| 726 | uint32_t DtFlags = 0; |
| 727 | uint32_t DtFlags1 = 0; |
| 728 | if (Config->Bsymbolic) |
| 729 | DtFlags |= DF_SYMBOLIC; |
| 730 | if (Config->ZNodelete) |
| 731 | DtFlags1 |= DF_1_NODELETE; |
| 732 | if (Config->ZNow) { |
| 733 | DtFlags |= DF_BIND_NOW; |
| 734 | DtFlags1 |= DF_1_NOW; |
| 735 | } |
| 736 | if (Config->ZOrigin) { |
| 737 | DtFlags |= DF_ORIGIN; |
| 738 | DtFlags1 |= DF_1_ORIGIN; |
| 739 | } |
| 740 | |
| 741 | if (DtFlags) |
| 742 | Add({DT_FLAGS, DtFlags}); |
| 743 | if (DtFlags1) |
| 744 | Add({DT_FLAGS_1, DtFlags1}); |
| 745 | |
| 746 | if (!Config->Entry.empty()) |
| 747 | Add({DT_DEBUG, (uint64_t)0}); |
| 748 | } |
| 749 | |
| 750 | // Add remaining entries to complete .dynamic contents. |
| 751 | template <class ELFT> void DynamicSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 752 | if (this->Size) |
Rui Ueyama | a959393 | 2016-11-02 02:18:01 +0000 | [diff] [blame] | 753 | return; // Already finalized. |
| 754 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 755 | this->Link = Out<ELFT>::DynStrTab->SectionIndex; |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 756 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 757 | if (Out<ELFT>::RelaDyn->hasRelocs()) { |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 758 | bool IsRela = Config->Rela; |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 759 | Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn}); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 760 | Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->Size}); |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 761 | Add({IsRela ? DT_RELAENT : DT_RELENT, |
| 762 | uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))}); |
Eugene Leviant | aa49819 | 2016-08-31 08:51:39 +0000 | [diff] [blame] | 763 | |
Simon Atanasyan | 65b2461 | 2016-09-04 17:40:12 +0000 | [diff] [blame] | 764 | // MIPS dynamic loader does not support RELCOUNT tag. |
| 765 | // The problem is in the tight relation between dynamic |
| 766 | // relocations and GOT. So do not emit this tag on MIPS. |
| 767 | if (Config->EMachine != EM_MIPS) { |
| 768 | size_t NumRelativeRels = Out<ELFT>::RelaDyn->getRelativeRelocCount(); |
| 769 | if (Config->ZCombreloc && NumRelativeRels) |
| 770 | Add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels}); |
| 771 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 772 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 773 | if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) { |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 774 | Add({DT_JMPREL, Out<ELFT>::RelaPlt}); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 775 | Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->Size}); |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 776 | Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT, |
| 777 | Out<ELFT>::GotPlt}); |
Rui Ueyama | 6c5638b | 2016-03-13 20:10:20 +0000 | [diff] [blame] | 778 | Add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)}); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 781 | Add({DT_SYMTAB, Out<ELFT>::DynSymTab}); |
| 782 | Add({DT_SYMENT, sizeof(Elf_Sym)}); |
| 783 | Add({DT_STRTAB, Out<ELFT>::DynStrTab}); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 784 | Add({DT_STRSZ, Out<ELFT>::DynStrTab->Size}); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 785 | if (Out<ELFT>::GnuHashTab) |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 786 | Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab}); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 787 | if (Out<ELFT>::HashTab) |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 788 | Add({DT_HASH, Out<ELFT>::HashTab}); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 789 | |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 790 | if (Out<ELFT>::PreinitArray) { |
| 791 | Add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray}); |
George Rimar | 03e0560 | 2016-08-19 15:23:39 +0000 | [diff] [blame] | 792 | Add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize}); |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 793 | } |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 794 | if (Out<ELFT>::InitArray) { |
| 795 | Add({DT_INIT_ARRAY, Out<ELFT>::InitArray}); |
George Rimar | 03e0560 | 2016-08-19 15:23:39 +0000 | [diff] [blame] | 796 | Add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize}); |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 797 | } |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 798 | if (Out<ELFT>::FiniArray) { |
| 799 | Add({DT_FINI_ARRAY, Out<ELFT>::FiniArray}); |
George Rimar | 03e0560 | 2016-08-19 15:23:39 +0000 | [diff] [blame] | 800 | Add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize}); |
Rui Ueyama | 7de3f37 | 2015-10-01 19:36:04 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 803 | if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init)) |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 804 | Add({DT_INIT, B}); |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 805 | if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini)) |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 806 | Add({DT_FINI, B}); |
Rui Ueyama | c96d0dd | 2015-10-21 17:47:10 +0000 | [diff] [blame] | 807 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 808 | bool HasVerNeed = Out<ELFT>::VerNeed->getNeedNum() != 0; |
| 809 | if (HasVerNeed || Out<ELFT>::VerDef) |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 810 | Add({DT_VERSYM, Out<ELFT>::VerSym}); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 811 | if (Out<ELFT>::VerDef) { |
| 812 | Add({DT_VERDEF, Out<ELFT>::VerDef}); |
| 813 | Add({DT_VERDEFNUM, getVerDefNum()}); |
| 814 | } |
| 815 | if (HasVerNeed) { |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 816 | Add({DT_VERNEED, Out<ELFT>::VerNeed}); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 817 | Add({DT_VERNEEDNUM, Out<ELFT>::VerNeed->getNeedNum()}); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 820 | if (Config->EMachine == EM_MIPS) { |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 821 | Add({DT_MIPS_RLD_VERSION, 1}); |
| 822 | Add({DT_MIPS_FLAGS, RHF_NOTPOT}); |
Rui Ueyama | cafc0f2e | 2016-07-14 17:40:18 +0000 | [diff] [blame] | 823 | Add({DT_MIPS_BASE_ADDRESS, Config->ImageBase}); |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 824 | Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()}); |
| 825 | Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()}); |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 826 | if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry()) |
Rui Ueyama | 572a6f7 | 2016-01-29 01:49:33 +0000 | [diff] [blame] | 827 | Add({DT_MIPS_GOTSYM, B->DynsymIndex}); |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 828 | else |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 829 | Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()}); |
| 830 | Add({DT_PLTGOT, Out<ELFT>::Got}); |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 831 | if (Out<ELFT>::MipsRldMap) |
Rui Ueyama | ac9fb45 | 2016-01-25 23:38:34 +0000 | [diff] [blame] | 832 | Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap}); |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 835 | // +1 for DT_NULL |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 836 | this->Size = (Entries.size() + 1) * this->Entsize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 840 | auto *P = reinterpret_cast<Elf_Dyn *>(Buf); |
| 841 | |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 842 | for (const Entry &E : Entries) { |
| 843 | P->d_tag = E.Tag; |
| 844 | switch (E.Kind) { |
| 845 | case Entry::SecAddr: |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 846 | P->d_un.d_ptr = E.OutSec->Addr; |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 847 | break; |
George Rimar | 03e0560 | 2016-08-19 15:23:39 +0000 | [diff] [blame] | 848 | case Entry::SecSize: |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 849 | P->d_un.d_val = E.OutSec->Size; |
George Rimar | 03e0560 | 2016-08-19 15:23:39 +0000 | [diff] [blame] | 850 | break; |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 851 | case Entry::SymAddr: |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 852 | P->d_un.d_ptr = E.Sym->template getVA<ELFT>(); |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 853 | break; |
| 854 | case Entry::PlainInt: |
| 855 | P->d_un.d_val = E.Val; |
| 856 | break; |
| 857 | } |
Rui Ueyama | 8c205d5 | 2015-10-02 01:33:31 +0000 | [diff] [blame] | 858 | ++P; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 859 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | template <class ELFT> |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 863 | EhFrameHeader<ELFT>::EhFrameHeader() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 864 | : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {} |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 865 | |
Rui Ueyama | 95a232e | 2016-05-23 01:45:05 +0000 | [diff] [blame] | 866 | // .eh_frame_hdr contains a binary search table of pointers to FDEs. |
| 867 | // Each entry of the search table consists of two values, |
| 868 | // the starting PC from where FDEs covers, and the FDE's address. |
| 869 | // It is sorted by PC. |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 870 | template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) { |
| 871 | const endianness E = ELFT::TargetEndianness; |
| 872 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 873 | // Sort the FDE list by their PC and uniqueify. Usually there is only |
| 874 | // one FDE for a PC (i.e. function), but if ICF merges two functions |
| 875 | // into one, there can be more than one FDEs pointing to the address. |
| 876 | auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; }; |
| 877 | std::stable_sort(Fdes.begin(), Fdes.end(), Less); |
| 878 | auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; }; |
| 879 | Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end()); |
Peter Collingbourne | c98de13 | 2016-04-11 16:40:08 +0000 | [diff] [blame] | 880 | |
Rui Ueyama | 1b2936f | 2016-05-23 01:31:10 +0000 | [diff] [blame] | 881 | Buf[0] = 1; |
| 882 | Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4; |
| 883 | Buf[2] = DW_EH_PE_udata4; |
| 884 | Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 885 | write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4); |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 886 | write32<E>(Buf + 8, Fdes.size()); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 887 | Buf += 12; |
| 888 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 889 | uintX_t VA = this->Addr; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 890 | for (FdeData &Fde : Fdes) { |
| 891 | write32<E>(Buf, Fde.Pc - VA); |
| 892 | write32<E>(Buf + 4, Fde.FdeVA - VA); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 893 | Buf += 8; |
| 894 | } |
| 895 | } |
| 896 | |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 897 | template <class ELFT> void EhFrameHeader<ELFT>::finalize() { |
| 898 | // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 899 | this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8; |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 900 | } |
| 901 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 902 | template <class ELFT> |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 903 | void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) { |
| 904 | Fdes.push_back({Pc, FdeVA}); |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 907 | template <class ELFT> static uint64_t getEntsize(uint32_t Type) { |
| 908 | switch (Type) { |
| 909 | case SHT_RELA: |
| 910 | return sizeof(typename ELFT::Rela); |
| 911 | case SHT_REL: |
| 912 | return sizeof(typename ELFT::Rel); |
| 913 | case SHT_MIPS_REGINFO: |
| 914 | return sizeof(Elf_Mips_RegInfo<ELFT>); |
| 915 | case SHT_MIPS_OPTIONS: |
| 916 | return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>); |
| 917 | case SHT_MIPS_ABIFLAGS: |
| 918 | return sizeof(Elf_Mips_ABIFlags<ELFT>); |
| 919 | default: |
| 920 | return 0; |
| 921 | } |
| 922 | } |
| 923 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 924 | template <class ELFT> |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 925 | OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 926 | : OutputSectionBase(Name, Type, Flags) { |
Rui Ueyama | df5d14d | 2016-11-09 22:32:42 +0000 | [diff] [blame] | 927 | this->Entsize = getEntsize<ELFT>(Type); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | template <class ELFT> void OutputSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 931 | uint32_t Type = this->Type; |
| 932 | if (this->Flags & SHF_LINK_ORDER) { |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 933 | if (!Config->Relocatable) { |
| 934 | // SHF_LINK_ORDER only has meaning in relocatable objects |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 935 | this->Flags &= ~SHF_LINK_ORDER; |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 936 | } |
| 937 | else if (!this->Sections.empty()) { |
| 938 | // When doing a relocatable link we must preserve the link order |
| 939 | // dependency of sections with the SHF_LINK_ORDER flag. The dependency |
| 940 | // is indicated by the sh_link field. We need to translate the |
| 941 | // InputSection sh_link to the OutputSection sh_link, all InputSections |
| 942 | // in the OutputSection have the same dependency. |
| 943 | if (auto *D = this->Sections.front()->getLinkOrderDep()) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 944 | this->Link = D->OutSec->SectionIndex; |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 947 | if (Type != SHT_RELA && Type != SHT_REL) |
| 948 | return; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 949 | this->Link = Out<ELFT>::SymTab->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 950 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 951 | // the section to which the relocation applies. |
| 952 | InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 953 | this->Info = S->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 954 | } |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 955 | |
| 956 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 957 | void OutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 958 | assert(C->Live); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 959 | auto *S = cast<InputSection<ELFT>>(C); |
| 960 | Sections.push_back(S); |
| 961 | S->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 962 | this->updateAlignment(S->Alignment); |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 963 | // Keep sh_entsize value of the input section to be able to perform merging |
| 964 | // later during a final linking using the generated relocatable object. |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 965 | if (Config->Relocatable && (S->Flags & SHF_MERGE)) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 966 | this->Entsize = S->Entsize; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 969 | // This function is called after we sort input sections |
| 970 | // and scan relocations to setup sections' offsets. |
| 971 | template <class ELFT> void OutputSection<ELFT>::assignOffsets() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 972 | uintX_t Off = this->Size; |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 973 | for (InputSection<ELFT> *S : Sections) { |
| 974 | Off = alignTo(Off, S->Alignment); |
| 975 | S->OutSecOff = Off; |
| 976 | Off += S->getSize(); |
| 977 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 978 | this->Size = Off; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 981 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 982 | // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 983 | // We want to keep the original order if the priorities are the same |
| 984 | // because the compiler keeps the original initialization order in a |
| 985 | // translation unit and we need to respect that. |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 986 | // For more detail, read the section of the GCC's manual about init_priority. |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 987 | template <class ELFT> void OutputSection<ELFT>::sortInitFini() { |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 988 | // Sort sections by priority. |
| 989 | typedef std::pair<int, InputSection<ELFT> *> Pair; |
Rui Ueyama | 704da02 | 2016-02-10 23:43:16 +0000 | [diff] [blame] | 990 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 991 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 992 | std::vector<Pair> V; |
| 993 | for (InputSection<ELFT> *S : Sections) |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 994 | V.push_back({getPriority(S->Name), S}); |
Rui Ueyama | 704da02 | 2016-02-10 23:43:16 +0000 | [diff] [blame] | 995 | std::stable_sort(V.begin(), V.end(), Comp); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 996 | Sections.clear(); |
| 997 | for (Pair &P : V) |
| 998 | Sections.push_back(P.second); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 999 | } |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1000 | |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 1001 | // Returns true if S matches /Filename.?\.o$/. |
| 1002 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 1003 | if (!S.endswith(".o")) |
| 1004 | return false; |
| 1005 | S = S.drop_back(2); |
| 1006 | if (S.endswith(Filename)) |
| 1007 | return true; |
| 1008 | return !S.empty() && S.drop_back().endswith(Filename); |
| 1009 | } |
| 1010 | |
| 1011 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 1012 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 1013 | |
| 1014 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 1015 | // |
| 1016 | // 1. The section was contained in crtbegin (crtbegin contains |
| 1017 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 1018 | // can find the beginning of the sections.) |
| 1019 | // |
| 1020 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 1021 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 1022 | // they are compared as string rather than number. |
| 1023 | // |
| 1024 | // 3. The section is just ".ctors" or ".dtors". |
| 1025 | // |
| 1026 | // 4. The section was contained in crtend, which contains an end marker. |
| 1027 | // |
| 1028 | // In an ideal world, we don't need this function because .init_array and |
| 1029 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 1030 | // are too many real-world use cases of .ctors, so we had no choice to |
| 1031 | // support that with this rather ad-hoc semantics. |
| 1032 | template <class ELFT> |
| 1033 | static bool compCtors(const InputSection<ELFT> *A, |
| 1034 | const InputSection<ELFT> *B) { |
| 1035 | bool BeginA = isCrtbegin(A->getFile()->getName()); |
| 1036 | bool BeginB = isCrtbegin(B->getFile()->getName()); |
| 1037 | if (BeginA != BeginB) |
| 1038 | return BeginA; |
| 1039 | bool EndA = isCrtend(A->getFile()->getName()); |
| 1040 | bool EndB = isCrtend(B->getFile()->getName()); |
| 1041 | if (EndA != EndB) |
| 1042 | return EndB; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 1043 | StringRef X = A->Name; |
| 1044 | StringRef Y = B->Name; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 1045 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 1046 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 1047 | X = X.substr(6); |
| 1048 | Y = Y.substr(6); |
Rui Ueyama | 24b794e | 2016-02-12 00:38:46 +0000 | [diff] [blame] | 1049 | if (X.empty() && Y.empty()) |
| 1050 | return false; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 1051 | return X < Y; |
| 1052 | } |
| 1053 | |
| 1054 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 1055 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 1056 | // Read the comment above. |
| 1057 | template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() { |
| 1058 | std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 1061 | static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) { |
| 1062 | size_t I = 0; |
| 1063 | for (; I + A.size() < Size; I += A.size()) |
| 1064 | memcpy(Buf + I, A.data(), A.size()); |
| 1065 | memcpy(Buf + I, A.data(), Size - I); |
| 1066 | } |
| 1067 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1068 | template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1069 | ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 1070 | if (!Filler.empty()) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1071 | fill(Buf, this->Size, Filler); |
Rui Ueyama | eb943e8 | 2016-11-04 18:22:36 +0000 | [diff] [blame] | 1072 | if (Config->Threads) { |
| 1073 | parallel_for_each(Sections.begin(), Sections.end(), |
| 1074 | [=](InputSection<ELFT> *C) { C->writeTo(Buf); }); |
| 1075 | } else { |
| 1076 | for (InputSection<ELFT> *C : Sections) |
| 1077 | C->writeTo(Buf); |
| 1078 | } |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 1079 | // Linker scripts may have BYTE()-family commands with which you |
| 1080 | // can write arbitrary bytes to the output. Process them if any. |
| 1081 | Script<ELFT>::X->writeDataBytes(this->Name, Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1084 | template <class ELFT> |
Rui Ueyama | f86cb90 | 2016-05-23 15:12:41 +0000 | [diff] [blame] | 1085 | EhOutputSection<ELFT>::EhOutputSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1086 | : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {} |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1087 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1088 | // Search for an existing CIE record or create a new one. |
| 1089 | // CIE records from input object files are uniquified by their contents |
| 1090 | // and where their relocations point to. |
| 1091 | template <class ELFT> |
| 1092 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1093 | CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece, |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 1094 | EhInputSection<ELFT> *Sec, |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1095 | ArrayRef<RelTy> Rels) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1096 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1097 | if (read32<E>(Piece.data().data() + 4) != 0) |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 1098 | fatal("CIE expected at beginning of .eh_frame: " + Sec->Name); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1099 | |
| 1100 | SymbolBody *Personality = nullptr; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1101 | unsigned FirstRelI = Piece.FirstRelocation; |
| 1102 | if (FirstRelI != (unsigned)-1) |
| 1103 | Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Search for an existing CIE by CIE contents/relocation target pair. |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1106 | CieRecord *Cie = &CieMap[{Piece.data(), Personality}]; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1107 | |
| 1108 | // If not found, create a new one. |
Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 1109 | if (Cie->Piece == nullptr) { |
| 1110 | Cie->Piece = &Piece; |
Rui Ueyama | e2060aa | 2016-05-22 23:52:56 +0000 | [diff] [blame] | 1111 | Cies.push_back(Cie); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1112 | } |
| 1113 | return Cie; |
| 1114 | } |
| 1115 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1116 | // There is one FDE per function. Returns true if a given FDE |
| 1117 | // points to a live function. |
| 1118 | template <class ELFT> |
| 1119 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1120 | bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece, |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 1121 | EhInputSection<ELFT> *Sec, |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1122 | ArrayRef<RelTy> Rels) { |
| 1123 | unsigned FirstRelI = Piece.FirstRelocation; |
| 1124 | if (FirstRelI == (unsigned)-1) |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1125 | fatal("FDE doesn't reference another section"); |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1126 | const RelTy &Rel = Rels[FirstRelI]; |
| 1127 | SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1128 | auto *D = dyn_cast<DefinedRegular<ELFT>>(&B); |
| 1129 | if (!D || !D->Section) |
| 1130 | return false; |
| 1131 | InputSectionBase<ELFT> *Target = D->Section->Repl; |
| 1132 | return Target && Target->Live; |
| 1133 | } |
| 1134 | |
| 1135 | // .eh_frame is a sequence of CIE or FDE records. In general, there |
| 1136 | // is one CIE record per input object file which is followed by |
| 1137 | // a list of FDEs. This function searches an existing CIE or create a new |
| 1138 | // one and associates FDEs to the CIE. |
Rui Ueyama | c0c9260 | 2016-02-05 22:56:03 +0000 | [diff] [blame] | 1139 | template <class ELFT> |
Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 1140 | template <class RelTy> |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 1141 | void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec, |
Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 1142 | ArrayRef<RelTy> Rels) { |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 1143 | const endianness E = ELFT::TargetEndianness; |
Rafael Espindola | 820f4bb | 2016-05-24 15:17:47 +0000 | [diff] [blame] | 1144 | |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 1145 | DenseMap<size_t, CieRecord *> OffsetToCie; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 1146 | for (EhSectionPiece &Piece : Sec->Pieces) { |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 1147 | // The empty record is the end marker. |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1148 | if (Piece.size() == 4) |
Rafael Espindola | 5ee9e7f | 2016-05-24 19:14:09 +0000 | [diff] [blame] | 1149 | return; |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 1150 | |
| 1151 | size_t Offset = Piece.InputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1152 | uint32_t ID = read32<E>(Piece.data().data() + 4); |
Rafael Espindola | 1f5696f | 2016-05-24 16:03:27 +0000 | [diff] [blame] | 1153 | if (ID == 0) { |
| 1154 | OffsetToCie[Offset] = addCie(Piece, Sec, Rels); |
| 1155 | continue; |
| 1156 | } |
| 1157 | |
| 1158 | uint32_t CieOffset = Offset + 4 - ID; |
| 1159 | CieRecord *Cie = OffsetToCie[CieOffset]; |
| 1160 | if (!Cie) |
| 1161 | fatal("invalid CIE reference"); |
| 1162 | |
| 1163 | if (!isFdeLive(Piece, Sec, Rels)) |
| 1164 | continue; |
| 1165 | Cie->FdePieces.push_back(&Piece); |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 1166 | NumFdes++; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1171 | void EhOutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 1172 | auto *Sec = cast<EhInputSection<ELFT>>(C); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1173 | Sec->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 1174 | this->updateAlignment(Sec->Alignment); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1175 | Sections.push_back(Sec); |
| 1176 | |
| 1177 | // .eh_frame is a sequence of CIE or FDE records. This function |
| 1178 | // splits it into pieces so that we can call |
| 1179 | // SplitInputSection::getSectionPiece on the section. |
Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 1180 | Sec->split(); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1181 | if (Sec->Pieces.empty()) |
| 1182 | return; |
| 1183 | |
| 1184 | if (const Elf_Shdr *RelSec = Sec->RelocSection) { |
Rafael Espindola | e19abab | 2016-11-03 20:44:50 +0000 | [diff] [blame] | 1185 | ELFFile<ELFT> Obj = Sec->getFile()->getObj(); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1186 | if (RelSec->sh_type == SHT_RELA) |
Rafael Espindola | 454fe15 | 2016-11-03 19:07:44 +0000 | [diff] [blame] | 1187 | addSectionAux(Sec, check(Obj.relas(RelSec))); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1188 | else |
Rafael Espindola | 454fe15 | 2016-11-03 19:07:44 +0000 | [diff] [blame] | 1189 | addSectionAux(Sec, check(Obj.rels(RelSec))); |
Rui Ueyama | 0de86c1 | 2016-01-27 22:23:44 +0000 | [diff] [blame] | 1190 | return; |
| 1191 | } |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1192 | addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr)); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1195 | template <class ELFT> |
Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 1196 | static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) { |
| 1197 | memcpy(Buf, D.data(), D.size()); |
Rui Ueyama | c0449a6 | 2016-05-21 18:10:13 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Fix the size field. -4 since size does not include the size field itself. |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1200 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | 644ac65 | 2016-05-22 00:17:11 +0000 | [diff] [blame] | 1201 | write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 1204 | template <class ELFT> void EhOutputSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1205 | if (this->Size) |
Rui Ueyama | e9381bd | 2016-07-16 02:47:42 +0000 | [diff] [blame] | 1206 | return; // Already finalized. |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1207 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1208 | size_t Off = 0; |
| 1209 | for (CieRecord *Cie : Cies) { |
| 1210 | Cie->Piece->OutputOff = Off; |
| 1211 | Off += alignTo(Cie->Piece->size(), sizeof(uintX_t)); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1212 | |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 1213 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1214 | Fde->OutputOff = Off; |
| 1215 | Off += alignTo(Fde->size(), sizeof(uintX_t)); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1218 | this->Size = Off; |
Rafael Espindola | 91bd48a | 2015-12-24 20:44:06 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1221 | template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) { |
| 1222 | const endianness E = ELFT::TargetEndianness; |
| 1223 | switch (Size) { |
| 1224 | case DW_EH_PE_udata2: |
| 1225 | return read16<E>(Buf); |
| 1226 | case DW_EH_PE_udata4: |
| 1227 | return read32<E>(Buf); |
| 1228 | case DW_EH_PE_udata8: |
| 1229 | return read64<E>(Buf); |
| 1230 | case DW_EH_PE_absptr: |
| 1231 | if (ELFT::Is64Bits) |
| 1232 | return read64<E>(Buf); |
| 1233 | return read32<E>(Buf); |
| 1234 | } |
| 1235 | fatal("unknown FDE size encoding"); |
| 1236 | } |
| 1237 | |
| 1238 | // Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to. |
| 1239 | // We need it to create .eh_frame_hdr section. |
| 1240 | template <class ELFT> |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 1241 | typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff, |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1242 | uint8_t Enc) { |
Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 1243 | // The starting address to which this FDE applies is |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1244 | // stored at FDE + 8 byte. |
| 1245 | size_t Off = FdeOff + 8; |
| 1246 | uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7); |
| 1247 | if ((Enc & 0x70) == DW_EH_PE_absptr) |
| 1248 | return Addr; |
| 1249 | if ((Enc & 0x70) == DW_EH_PE_pcrel) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1250 | return Addr + this->Addr + Off; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1251 | fatal("unknown FDE size relative encoding"); |
| 1252 | } |
| 1253 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 1254 | template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1255 | const endianness E = ELFT::TargetEndianness; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1256 | for (CieRecord *Cie : Cies) { |
| 1257 | size_t CieOffset = Cie->Piece->OutputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1258 | writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data()); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1259 | |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 1260 | for (EhSectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1261 | size_t Off = Fde->OutputOff; |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1262 | writeCieFde<ELFT>(Buf + Off, Fde->data()); |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 1263 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 1264 | // FDE's second word should have the offset to an associated CIE. |
| 1265 | // Write it. |
| 1266 | write32<E>(Buf + Off + 4, Off + 4 - CieOffset); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 1270 | for (EhInputSection<ELFT> *S : Sections) |
Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1271 | S->relocate(Buf, nullptr); |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table |
Rui Ueyama | 2ab3d20 | 2016-05-23 16:36:47 +0000 | [diff] [blame] | 1274 | // to get a FDE from an address to which FDE is applied. So here |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1275 | // we obtain two addresses and pass them to EhFrameHdr object. |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 1276 | if (Out<ELFT>::EhFrameHdr) { |
| 1277 | for (CieRecord *Cie : Cies) { |
Rui Ueyama | d884927 | 2016-05-25 16:37:01 +0000 | [diff] [blame] | 1278 | uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data()); |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 1279 | for (SectionPiece *Fde : Cie->FdePieces) { |
Rui Ueyama | 6de2e68 | 2016-05-24 02:08:38 +0000 | [diff] [blame] | 1280 | uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1281 | uintX_t FdeVA = this->Addr + Fde->OutputOff; |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 1282 | Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA); |
| 1283 | } |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | template <class ELFT> |
Rui Ueyama | d97e5c4 | 2016-01-07 18:33:11 +0000 | [diff] [blame] | 1289 | MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type, |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 1290 | uintX_t Flags, uintX_t Alignment) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1291 | : OutputSectionBase(Name, Type, Flags), |
Rui Ueyama | 818bb2f | 2016-07-16 18:55:47 +0000 | [diff] [blame] | 1292 | Builder(StringTableBuilder::RAW, Alignment) {} |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1293 | |
| 1294 | template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 1295 | Builder.write(Buf); |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1298 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1299 | void MergeOutputSection<ELFT>::addSection(InputSectionData *C) { |
Rui Ueyama | 1080351 | 2016-05-22 00:25:30 +0000 | [diff] [blame] | 1300 | auto *Sec = cast<MergeInputSection<ELFT>>(C); |
| 1301 | Sec->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 1302 | this->updateAlignment(Sec->Alignment); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1303 | this->Entsize = Sec->Entsize; |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 1304 | Sections.push_back(Sec); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1305 | |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 1306 | auto HashI = Sec->Hashes.begin(); |
| 1307 | for (auto I = Sec->Pieces.begin(), E = Sec->Pieces.end(); I != E; ++I) { |
| 1308 | SectionPiece &Piece = *I; |
| 1309 | uint32_t Hash = *HashI; |
| 1310 | ++HashI; |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 1311 | if (!Piece.Live) |
Rafael Espindola | 0b9531c | 2016-04-22 22:09:35 +0000 | [diff] [blame] | 1312 | continue; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 1313 | StringRef Data = toStringRef(Sec->getData(I)); |
| 1314 | CachedHashStringRef V(Data, Hash); |
Rafael Espindola | 5fc2b1d | 2016-10-05 19:36:02 +0000 | [diff] [blame] | 1315 | uintX_t OutputOffset = Builder.add(V); |
Rafael Espindola | 7b8bb53 | 2016-10-05 15:35:18 +0000 | [diff] [blame] | 1316 | if (!shouldTailMerge()) |
Rui Ueyama | c6ebb02 | 2016-05-22 01:03:41 +0000 | [diff] [blame] | 1317 | Piece.OutputOff = OutputOffset; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1318 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | template <class ELFT> |
Justin Lebar | ee34a73 | 2016-10-17 22:24:36 +0000 | [diff] [blame] | 1322 | unsigned MergeOutputSection<ELFT>::getOffset(CachedHashStringRef Val) { |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1323 | return Builder.getOffset(Val); |
| 1324 | } |
| 1325 | |
| 1326 | template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1327 | return Config->Optimize >= 2 && this->Flags & SHF_STRINGS; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | template <class ELFT> void MergeOutputSection<ELFT>::finalize() { |
| 1331 | if (shouldTailMerge()) |
| 1332 | Builder.finalize(); |
Rafael Espindola | 259d645 | 2016-10-04 22:43:38 +0000 | [diff] [blame] | 1333 | else |
| 1334 | Builder.finalizeInOrder(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1335 | this->Size = Builder.getSize(); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 1338 | template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() { |
| 1339 | for (MergeInputSection<ELFT> *Sec : Sections) |
| 1340 | Sec->finalizePieces(); |
| 1341 | } |
| 1342 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1343 | template <class ELFT> |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 1344 | StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1345 | : OutputSectionBase(Name, SHT_STRTAB, Dynamic ? (uintX_t)SHF_ALLOC : 0), |
Rafael Espindola | 0811385 | 2016-11-08 20:25:44 +0000 | [diff] [blame] | 1346 | Dynamic(Dynamic) { |
| 1347 | // ELF string tables start with a NUL byte, so 1. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1348 | this->Size = 1; |
Rafael Espindola | 0811385 | 2016-11-08 20:25:44 +0000 | [diff] [blame] | 1349 | } |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 1350 | |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 1351 | // Adds a string to the string table. If HashIt is true we hash and check for |
| 1352 | // duplicates. It is optional because the name of global symbols are already |
| 1353 | // uniqued and hashing them again has a big cost for a small value: uniquing |
| 1354 | // them with some other string that happens to be the same. |
| 1355 | template <class ELFT> |
| 1356 | unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) { |
| 1357 | if (HashIt) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1358 | auto R = StringMap.insert(std::make_pair(S, this->Size)); |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 1359 | if (!R.second) |
| 1360 | return R.first->second; |
| 1361 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1362 | unsigned Ret = this->Size; |
| 1363 | this->Size = this->Size + S.size() + 1; |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1364 | Strings.push_back(S); |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 1365 | return Ret; |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1368 | template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 1369 | // ELF string tables start with NUL byte, so advance the pointer by one. |
| 1370 | ++Buf; |
| 1371 | for (StringRef S : Strings) { |
| 1372 | memcpy(Buf, S.data(), S.size()); |
| 1373 | Buf += S.size() + 1; |
| 1374 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Rafael Espindola | d1cf421 | 2015-10-05 16:25:43 +0000 | [diff] [blame] | 1377 | template <class ELFT> |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 1378 | typename ELFT::uint DynamicReloc<ELFT>::getOffset() const { |
| 1379 | if (OutputSec) |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1380 | return OutputSec->Addr + OffsetInSec; |
| 1381 | return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec); |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | template <class ELFT> |
| 1385 | typename ELFT::uint DynamicReloc<ELFT>::getAddend() const { |
| 1386 | if (UseSymVA) |
| 1387 | return Sym->getVA<ELFT>(Addend); |
| 1388 | return Addend; |
| 1389 | } |
| 1390 | |
| 1391 | template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const { |
| 1392 | if (Sym && !UseSymVA) |
| 1393 | return Sym->DynsymIndex; |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | template <class ELFT> |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 1398 | SymbolTableSection<ELFT>::SymbolTableSection( |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 1399 | StringTableSection<ELFT> &StrTabSec) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1400 | : OutputSectionBase(StrTabSec.isDynamic() ? ".dynsym" : ".symtab", |
| 1401 | StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB, |
| 1402 | StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0), |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 1403 | StrTabSec(StrTabSec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1404 | this->Entsize = sizeof(Elf_Sym); |
| 1405 | this->Addralign = sizeof(uintX_t); |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 1408 | // Orders symbols according to their positions in the GOT, |
| 1409 | // in compliance with MIPS ABI rules. |
| 1410 | // See "Global Offset Table" in Chapter 5 in the following document |
| 1411 | // for detailed description: |
| 1412 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1413 | static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) { |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 1414 | // Sort entries related to non-local preemptible symbols by GOT indexes. |
| 1415 | // All other entries go to the first part of GOT in arbitrary order. |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1416 | bool LIsInLocalGot = !L->IsInGlobalMipsGot; |
| 1417 | bool RIsInLocalGot = !R->IsInGlobalMipsGot; |
Simon Atanasyan | 7b8481b | 2016-06-20 11:37:56 +0000 | [diff] [blame] | 1418 | if (LIsInLocalGot || RIsInLocalGot) |
| 1419 | return !RIsInLocalGot; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1420 | return L->GotIndex < R->GotIndex; |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 1423 | static uint8_t getSymbolBinding(SymbolBody *Body) { |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 1424 | Symbol *S = Body->symbol(); |
George Rimar | 11f83b7 | 2016-08-19 08:31:02 +0000 | [diff] [blame] | 1425 | if (Config->Relocatable) |
| 1426 | return S->Binding; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 1427 | uint8_t Visibility = S->Visibility; |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 1428 | if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) |
| 1429 | return STB_LOCAL; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 1430 | if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE) |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 1431 | return STB_GLOBAL; |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 1432 | return S->Binding; |
Rafael Espindola | badd397 | 2016-04-08 15:30:56 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1435 | template <class ELFT> void SymbolTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1436 | if (this->Size) |
Igor Kudrin | 2169b1b | 2015-11-02 10:46:14 +0000 | [diff] [blame] | 1437 | return; // Already finalized. |
| 1438 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1439 | this->Size = getNumSymbols() * sizeof(Elf_Sym); |
| 1440 | this->Link = StrTabSec.SectionIndex; |
| 1441 | this->Info = NumLocals + 1; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1442 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 1443 | if (Config->Relocatable) { |
| 1444 | size_t I = NumLocals; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1445 | for (const SymbolTableEntry &S : Symbols) |
| 1446 | S.Symbol->DynsymIndex = ++I; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 1447 | return; |
| 1448 | } |
| 1449 | |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1450 | if (!StrTabSec.isDynamic()) { |
| 1451 | std::stable_sort(Symbols.begin(), Symbols.end(), |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1452 | [](const SymbolTableEntry &L, const SymbolTableEntry &R) { |
| 1453 | return getSymbolBinding(L.Symbol) == STB_LOCAL && |
| 1454 | getSymbolBinding(R.Symbol) != STB_LOCAL; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1455 | }); |
| 1456 | return; |
| 1457 | } |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 1458 | if (Out<ELFT>::GnuHashTab) |
| 1459 | // NB: It also sorts Symbols to meet the GNU hash table requirements. |
| 1460 | Out<ELFT>::GnuHashTab->addSymbols(Symbols); |
Igor Kudrin | f4cdfe88 | 2015-11-12 04:08:12 +0000 | [diff] [blame] | 1461 | else if (Config->EMachine == EM_MIPS) |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1462 | std::stable_sort(Symbols.begin(), Symbols.end(), |
| 1463 | [](const SymbolTableEntry &L, const SymbolTableEntry &R) { |
| 1464 | return sortMipsSymbols(L.Symbol, R.Symbol); |
| 1465 | }); |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1466 | size_t I = 0; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1467 | for (const SymbolTableEntry &S : Symbols) |
| 1468 | S.Symbol->DynsymIndex = ++I; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 1471 | template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) { |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 1472 | Symbols.push_back({B, StrTabSec.addString(B->getName(), false)}); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1475 | template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1476 | Buf += sizeof(Elf_Sym); |
| 1477 | |
| 1478 | // All symbols with STB_LOCAL binding precede the weak and global symbols. |
| 1479 | // .dynsym only contains global symbols. |
George Rimar | 9503f6d | 2016-08-31 08:46:30 +0000 | [diff] [blame] | 1480 | if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic()) |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1481 | writeLocalSymbols(Buf); |
| 1482 | |
| 1483 | writeGlobalSymbols(Buf); |
| 1484 | } |
| 1485 | |
| 1486 | template <class ELFT> |
| 1487 | void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) { |
| 1488 | // Iterate over all input object files to copy their local symbols |
| 1489 | // to the output symbol table pointed by Buf. |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 1490 | for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) { |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1491 | for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P : |
| 1492 | File->KeptLocalSyms) { |
| 1493 | const DefinedRegular<ELFT> &Body = *P.first; |
| 1494 | InputSectionBase<ELFT> *Section = Body.Section; |
Rui Ueyama | c55733e | 2015-09-30 00:54:29 +0000 | [diff] [blame] | 1495 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (!Section) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1498 | ESym->st_shndx = SHN_ABS; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1499 | ESym->st_value = Body.Value; |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1500 | } else { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1501 | const OutputSectionBase *OutSec = Section->OutSec; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1502 | ESym->st_shndx = OutSec->SectionIndex; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1503 | ESym->st_value = OutSec->Addr + Section->getOffset(Body); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1504 | } |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 1505 | ESym->st_name = P.second; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1506 | ESym->st_size = Body.template getSize<ELFT>(); |
Rafael Espindola | 9e32e4f | 2016-04-26 13:50:46 +0000 | [diff] [blame] | 1507 | ESym->setBindingAndType(STB_LOCAL, Body.Type); |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1508 | Buf += sizeof(*ESym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1511 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1512 | |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1513 | template <class ELFT> |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 1514 | void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) { |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 1515 | // Write the internal symbol table contents to the output symbol table |
| 1516 | // pointed by Buf. |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1517 | auto *ESym = reinterpret_cast<Elf_Sym *>(Buf); |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1518 | for (const SymbolTableEntry &S : Symbols) { |
| 1519 | SymbolBody *Body = S.Symbol; |
| 1520 | size_t StrOff = S.StrTabOffset; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 1521 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 1522 | uint8_t Type = Body->Type; |
| 1523 | uintX_t Size = Body->getSize<ELFT>(); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1524 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1525 | ESym->setBindingAndType(getSymbolBinding(Body), Type); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 1526 | ESym->st_size = Size; |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 1527 | ESym->st_name = StrOff; |
Peter Collingbourne | 4f95270 | 2016-05-01 04:55:03 +0000 | [diff] [blame] | 1528 | ESym->setVisibility(Body->symbol()->Visibility); |
Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 1529 | ESym->st_value = Body->getVA<ELFT>(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1530 | |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1531 | if (const OutputSectionBase *OutSec = getOutputSection(Body)) |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 1532 | ESym->st_shndx = OutSec->SectionIndex; |
Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 1533 | else if (isa<DefinedRegular<ELFT>>(Body)) |
| 1534 | ESym->st_shndx = SHN_ABS; |
Simon Atanasyan | d040a58 | 2016-02-25 16:19:15 +0000 | [diff] [blame] | 1535 | |
Simon Atanasyan | f967f09 | 2016-09-29 12:58:36 +0000 | [diff] [blame] | 1536 | if (Config->EMachine == EM_MIPS) { |
| 1537 | // On MIPS we need to mark symbol which has a PLT entry and requires |
| 1538 | // pointer equality by STO_MIPS_PLT flag. That is necessary to help |
| 1539 | // dynamic linker distinguish such symbols and MIPS lazy-binding stubs. |
| 1540 | // https://sourceware.org/ml/binutils/2008-07/txt00000.txt |
| 1541 | if (Body->isInPlt() && Body->NeedsCopyOrPltAddr) |
| 1542 | ESym->st_other |= STO_MIPS_PLT; |
| 1543 | if (Config->Relocatable) { |
| 1544 | auto *D = dyn_cast<DefinedRegular<ELFT>>(Body); |
| 1545 | if (D && D->isMipsPIC()) |
| 1546 | ESym->st_other |= STO_MIPS_PIC; |
| 1547 | } |
| 1548 | } |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 1549 | ++ESym; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 1553 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1554 | const OutputSectionBase * |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1555 | SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) { |
| 1556 | switch (Sym->kind()) { |
| 1557 | case SymbolBody::DefinedSyntheticKind: |
Peter Collingbourne | 6a42259 | 2016-05-03 01:21:08 +0000 | [diff] [blame] | 1558 | return cast<DefinedSynthetic<ELFT>>(Sym)->Section; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1559 | case SymbolBody::DefinedRegularKind: { |
Rafael Espindola | 5ffa13c | 2016-04-15 00:15:02 +0000 | [diff] [blame] | 1560 | auto &D = cast<DefinedRegular<ELFT>>(*Sym); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 1561 | if (D.Section) |
| 1562 | return D.Section->OutSec; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1563 | break; |
| 1564 | } |
| 1565 | case SymbolBody::DefinedCommonKind: |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 1566 | return In<ELFT>::Common->OutSec; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1567 | case SymbolBody::SharedKind: |
| 1568 | if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy()) |
| 1569 | return Out<ELFT>::Bss; |
| 1570 | break; |
Peter Collingbourne | 60976ed | 2016-04-27 00:05:06 +0000 | [diff] [blame] | 1571 | case SymbolBody::UndefinedKind: |
Rui Ueyama | f8baa66 | 2016-04-07 19:24:51 +0000 | [diff] [blame] | 1572 | case SymbolBody::LazyArchiveKind: |
| 1573 | case SymbolBody::LazyObjectKind: |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1574 | break; |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 1575 | } |
| 1576 | return nullptr; |
| 1577 | } |
| 1578 | |
| 1579 | template <class ELFT> |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1580 | VersionDefinitionSection<ELFT>::VersionDefinitionSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1581 | : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1582 | this->Addralign = sizeof(uint32_t); |
Rui Ueyama | f524464 | 2016-07-16 02:36:00 +0000 | [diff] [blame] | 1583 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1584 | |
| 1585 | static StringRef getFileDefName() { |
| 1586 | if (!Config->SoName.empty()) |
| 1587 | return Config->SoName; |
| 1588 | return Config->OutputFile; |
| 1589 | } |
| 1590 | |
| 1591 | template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() { |
| 1592 | FileDefNameOff = Out<ELFT>::DynStrTab->addString(getFileDefName()); |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 1593 | for (VersionDefinition &V : Config->VersionDefinitions) |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1594 | V.NameOff = Out<ELFT>::DynStrTab->addString(V.Name); |
| 1595 | |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1596 | this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum(); |
| 1597 | this->Link = Out<ELFT>::DynStrTab->SectionIndex; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1598 | |
| 1599 | // sh_info should be set to the number of definitions. This fact is missed in |
| 1600 | // documentation, but confirmed by binutils community: |
| 1601 | // https://sourceware.org/ml/binutils/2014-11/msg00355.html |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1602 | this->Info = getVerDefNum(); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1605 | template <class ELFT> |
| 1606 | void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index, |
| 1607 | StringRef Name, size_t NameOff) { |
| 1608 | auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1609 | Verdef->vd_version = 1; |
George Rimar | 33b9de4 | 2016-07-01 11:45:10 +0000 | [diff] [blame] | 1610 | Verdef->vd_cnt = 1; |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1611 | Verdef->vd_aux = sizeof(Elf_Verdef); |
| 1612 | Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1613 | Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1614 | Verdef->vd_ndx = Index; |
Davide Italiano | e7fd0be | 2016-11-07 21:56:56 +0000 | [diff] [blame] | 1615 | Verdef->vd_hash = hashSysV(Name); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1616 | |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1617 | auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef)); |
| 1618 | Verdaux->vda_name = NameOff; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1619 | Verdaux->vda_next = 0; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | template <class ELFT> |
| 1623 | void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) { |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1624 | writeOne(Buf, 1, getFileDefName(), FileDefNameOff); |
| 1625 | |
Rui Ueyama | af469d4 | 2016-07-16 04:09:27 +0000 | [diff] [blame] | 1626 | for (VersionDefinition &V : Config->VersionDefinitions) { |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1627 | Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux); |
| 1628 | writeOne(Buf, V.Id, V.Name, V.NameOff); |
| 1629 | } |
| 1630 | |
| 1631 | // Need to terminate the last version definition. |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1632 | Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf); |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 1633 | Verdef->vd_next = 0; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | template <class ELFT> |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1637 | VersionTableSection<ELFT>::VersionTableSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1638 | : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1639 | this->Addralign = sizeof(uint16_t); |
Rafael Espindola | eaaec4a | 2016-04-29 17:19:45 +0000 | [diff] [blame] | 1640 | } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1641 | |
| 1642 | template <class ELFT> void VersionTableSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1643 | this->Size = |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1644 | sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1645 | this->Entsize = sizeof(Elf_Versym); |
George Rimar | 8b3c5f2 | 2016-06-06 08:04:53 +0000 | [diff] [blame] | 1646 | // At the moment of june 2016 GNU docs does not mention that sh_link field |
| 1647 | // should be set, but Sun docs do. Also readelf relies on this field. |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1648 | this->Link = Out<ELFT>::DynSymTab->SectionIndex; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1652 | auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1; |
Michael J. Spencer | f8a8148 | 2016-10-19 23:49:27 +0000 | [diff] [blame] | 1653 | for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) { |
| 1654 | OutVersym->vs_index = S.Symbol->symbol()->VersionId; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1655 | ++OutVersym; |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | template <class ELFT> |
| 1660 | VersionNeedSection<ELFT>::VersionNeedSection() |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1661 | : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1662 | this->Addralign = sizeof(uint32_t); |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1663 | |
| 1664 | // Identifiers in verneed section start at 2 because 0 and 1 are reserved |
| 1665 | // for VER_NDX_LOCAL and VER_NDX_GLOBAL. |
| 1666 | // First identifiers are reserved by verdef section if it exist. |
| 1667 | NextIndex = getVerDefNum() + 1; |
Rafael Espindola | eaaec4a | 2016-04-29 17:19:45 +0000 | [diff] [blame] | 1668 | } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1669 | |
| 1670 | template <class ELFT> |
| 1671 | void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) { |
| 1672 | if (!SS->Verdef) { |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1673 | SS->symbol()->VersionId = VER_NDX_GLOBAL; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1674 | return; |
| 1675 | } |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 1676 | SharedFile<ELFT> *F = SS->file(); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1677 | // If we don't already know that we need an Elf_Verneed for this DSO, prepare |
| 1678 | // to create one by adding it to our needed list and creating a dynstr entry |
| 1679 | // for the soname. |
| 1680 | if (F->VerdefMap.empty()) |
| 1681 | Needed.push_back({F, Out<ELFT>::DynStrTab->addString(F->getSoName())}); |
| 1682 | typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef]; |
| 1683 | // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, |
| 1684 | // prepare to create one by allocating a version identifier and creating a |
| 1685 | // dynstr entry for the version name. |
| 1686 | if (NV.Index == 0) { |
| 1687 | NV.StrTab = Out<ELFT>::DynStrTab->addString( |
Rui Ueyama | 434b561 | 2016-07-17 03:11:46 +0000 | [diff] [blame] | 1688 | SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1689 | NV.Index = NextIndex++; |
| 1690 | } |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1691 | SS->symbol()->VersionId = NV.Index; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) { |
| 1695 | // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs. |
| 1696 | auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf); |
| 1697 | auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size()); |
| 1698 | |
| 1699 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) { |
| 1700 | // Create an Elf_Verneed for this DSO. |
| 1701 | Verneed->vn_version = 1; |
| 1702 | Verneed->vn_cnt = P.first->VerdefMap.size(); |
| 1703 | Verneed->vn_file = P.second; |
| 1704 | Verneed->vn_aux = |
| 1705 | reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed); |
| 1706 | Verneed->vn_next = sizeof(Elf_Verneed); |
| 1707 | ++Verneed; |
| 1708 | |
| 1709 | // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over |
| 1710 | // VerdefMap, which will only contain references to needed version |
| 1711 | // definitions. Each Elf_Vernaux is based on the information contained in |
| 1712 | // the Elf_Verdef in the source DSO. This loop iterates over a std::map of |
| 1713 | // pointers, but is deterministic because the pointers refer to Elf_Verdef |
| 1714 | // data structures within a single input file. |
| 1715 | for (auto &NV : P.first->VerdefMap) { |
| 1716 | Vernaux->vna_hash = NV.first->vd_hash; |
| 1717 | Vernaux->vna_flags = 0; |
| 1718 | Vernaux->vna_other = NV.second.Index; |
| 1719 | Vernaux->vna_name = NV.second.StrTab; |
| 1720 | Vernaux->vna_next = sizeof(Elf_Vernaux); |
| 1721 | ++Vernaux; |
| 1722 | } |
| 1723 | |
| 1724 | Vernaux[-1].vna_next = 0; |
| 1725 | } |
| 1726 | Verneed[-1].vn_next = 0; |
| 1727 | } |
| 1728 | |
| 1729 | template <class ELFT> void VersionNeedSection<ELFT>::finalize() { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1730 | this->Link = Out<ELFT>::DynStrTab->SectionIndex; |
| 1731 | this->Info = Needed.size(); |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1732 | unsigned Size = Needed.size() * sizeof(Elf_Verneed); |
| 1733 | for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) |
| 1734 | Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1735 | this->Size = Size; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | template <class ELFT> |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1739 | static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1740 | return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | template <class ELFT> |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1744 | static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, |
| 1745 | StringRef OutsecName) { |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1746 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1747 | uintX_t Flags = getOutFlags(C); |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1748 | |
| 1749 | // For SHF_MERGE we create different output sections for each alignment. |
| 1750 | // This makes each output section simple and keeps a single level mapping from |
| 1751 | // input to output. |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 1752 | // In case of relocatable object generation we do not try to perform merging |
| 1753 | // and treat SHF_MERGE sections as regular ones, but also create different |
| 1754 | // output sections for them to allow merging at final linking stage. |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1755 | uintX_t Alignment = 0; |
Simon Atanasyan | 02b9c3f | 2016-10-05 07:49:18 +0000 | [diff] [blame] | 1756 | if (isa<MergeInputSection<ELFT>>(C) || |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1757 | (Config->Relocatable && (C->Flags & SHF_MERGE))) |
| 1758 | Alignment = std::max<uintX_t>(C->Alignment, C->Entsize); |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1759 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1760 | return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment}; |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1764 | std::pair<OutputSectionBase *, bool> |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1765 | OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C, |
| 1766 | StringRef OutsecName) { |
| 1767 | SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName); |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1768 | return create(Key, C); |
| 1769 | } |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1770 | |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1771 | template <class ELFT> |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1772 | std::pair<OutputSectionBase *, bool> |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1773 | OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key, |
| 1774 | InputSectionBase<ELFT> *C) { |
| 1775 | uintX_t Flags = getOutFlags(C); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1776 | OutputSectionBase *&Sec = Map[Key]; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1777 | if (Sec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 1778 | Sec->Flags |= Flags; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 1779 | return {Sec, false}; |
| 1780 | } |
| 1781 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 1782 | uint32_t Type = C->Type; |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 1783 | switch (C->kind()) { |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1784 | case InputSectionBase<ELFT>::Regular: |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 1785 | Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1786 | break; |
| 1787 | case InputSectionBase<ELFT>::EHFrame: |
| 1788 | return {Out<ELFT>::EhFrame, false}; |
| 1789 | case InputSectionBase<ELFT>::Merge: |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 1790 | Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1791 | break; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1792 | } |
| 1793 | return {Sec, true}; |
| 1794 | } |
| 1795 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1796 | template <bool Is64Bits> |
| 1797 | typename lld::elf::SectionKey<Is64Bits> |
| 1798 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() { |
| 1799 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0}; |
| 1800 | } |
| 1801 | |
| 1802 | template <bool Is64Bits> |
| 1803 | typename lld::elf::SectionKey<Is64Bits> |
| 1804 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() { |
| 1805 | return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0, |
| 1806 | 0}; |
| 1807 | } |
| 1808 | |
| 1809 | template <bool Is64Bits> |
| 1810 | unsigned |
| 1811 | DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) { |
| 1812 | return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment); |
| 1813 | } |
| 1814 | |
| 1815 | template <bool Is64Bits> |
| 1816 | bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS, |
| 1817 | const Key &RHS) { |
| 1818 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| 1819 | LHS.Type == RHS.Type && LHS.Flags == RHS.Flags && |
| 1820 | LHS.Alignment == RHS.Alignment; |
| 1821 | } |
| 1822 | |
| 1823 | namespace llvm { |
| 1824 | template struct DenseMapInfo<SectionKey<true>>; |
| 1825 | template struct DenseMapInfo<SectionKey<false>>; |
| 1826 | } |
| 1827 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1828 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 1829 | namespace elf { |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame^] | 1830 | |
| 1831 | template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 1832 | template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 1833 | template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 1834 | template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1835 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 1836 | template class EhFrameHeader<ELF32LE>; |
| 1837 | template class EhFrameHeader<ELF32BE>; |
| 1838 | template class EhFrameHeader<ELF64LE>; |
| 1839 | template class EhFrameHeader<ELF64BE>; |
| 1840 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 1841 | template class GotPltSection<ELF32LE>; |
| 1842 | template class GotPltSection<ELF32BE>; |
| 1843 | template class GotPltSection<ELF64LE>; |
| 1844 | template class GotPltSection<ELF64BE>; |
| 1845 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1846 | template class GotSection<ELF32LE>; |
| 1847 | template class GotSection<ELF32BE>; |
| 1848 | template class GotSection<ELF64LE>; |
| 1849 | template class GotSection<ELF64BE>; |
| 1850 | |
| 1851 | template class PltSection<ELF32LE>; |
| 1852 | template class PltSection<ELF32BE>; |
| 1853 | template class PltSection<ELF64LE>; |
| 1854 | template class PltSection<ELF64BE>; |
| 1855 | |
| 1856 | template class RelocationSection<ELF32LE>; |
| 1857 | template class RelocationSection<ELF32BE>; |
| 1858 | template class RelocationSection<ELF64LE>; |
| 1859 | template class RelocationSection<ELF64BE>; |
| 1860 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 1861 | template class GnuHashTableSection<ELF32LE>; |
| 1862 | template class GnuHashTableSection<ELF32BE>; |
| 1863 | template class GnuHashTableSection<ELF64LE>; |
| 1864 | template class GnuHashTableSection<ELF64BE>; |
| 1865 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1866 | template class HashTableSection<ELF32LE>; |
| 1867 | template class HashTableSection<ELF32BE>; |
| 1868 | template class HashTableSection<ELF64LE>; |
| 1869 | template class HashTableSection<ELF64BE>; |
| 1870 | |
| 1871 | template class DynamicSection<ELF32LE>; |
| 1872 | template class DynamicSection<ELF32BE>; |
| 1873 | template class DynamicSection<ELF64LE>; |
| 1874 | template class DynamicSection<ELF64BE>; |
| 1875 | |
| 1876 | template class OutputSection<ELF32LE>; |
| 1877 | template class OutputSection<ELF32BE>; |
| 1878 | template class OutputSection<ELF64LE>; |
| 1879 | template class OutputSection<ELF64BE>; |
| 1880 | |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 1881 | template class EhOutputSection<ELF32LE>; |
| 1882 | template class EhOutputSection<ELF32BE>; |
| 1883 | template class EhOutputSection<ELF64LE>; |
| 1884 | template class EhOutputSection<ELF64BE>; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 1885 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 1886 | template class MergeOutputSection<ELF32LE>; |
| 1887 | template class MergeOutputSection<ELF32BE>; |
| 1888 | template class MergeOutputSection<ELF64LE>; |
| 1889 | template class MergeOutputSection<ELF64BE>; |
| 1890 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 1891 | template class StringTableSection<ELF32LE>; |
| 1892 | template class StringTableSection<ELF32BE>; |
| 1893 | template class StringTableSection<ELF64LE>; |
| 1894 | template class StringTableSection<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1895 | |
| 1896 | template class SymbolTableSection<ELF32LE>; |
| 1897 | template class SymbolTableSection<ELF32BE>; |
| 1898 | template class SymbolTableSection<ELF64LE>; |
| 1899 | template class SymbolTableSection<ELF64BE>; |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 1900 | |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 1901 | template class VersionTableSection<ELF32LE>; |
| 1902 | template class VersionTableSection<ELF32BE>; |
| 1903 | template class VersionTableSection<ELF64LE>; |
| 1904 | template class VersionTableSection<ELF64BE>; |
| 1905 | |
| 1906 | template class VersionNeedSection<ELF32LE>; |
| 1907 | template class VersionNeedSection<ELF32BE>; |
| 1908 | template class VersionNeedSection<ELF64LE>; |
| 1909 | template class VersionNeedSection<ELF64BE>; |
| 1910 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 1911 | template class VersionDefinitionSection<ELF32LE>; |
| 1912 | template class VersionDefinitionSection<ELF32BE>; |
| 1913 | template class VersionDefinitionSection<ELF64LE>; |
| 1914 | template class VersionDefinitionSection<ELF64BE>; |
| 1915 | |
George Rimar | 58fa524 | 2016-10-20 09:19:48 +0000 | [diff] [blame] | 1916 | template class GdbIndexSection<ELF32LE>; |
| 1917 | template class GdbIndexSection<ELF32BE>; |
| 1918 | template class GdbIndexSection<ELF64LE>; |
| 1919 | template class GdbIndexSection<ELF64BE>; |
| 1920 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 1921 | template class OutputSectionFactory<ELF32LE>; |
| 1922 | template class OutputSectionFactory<ELF32BE>; |
| 1923 | template class OutputSectionFactory<ELF64LE>; |
| 1924 | template class OutputSectionFactory<ELF64BE>; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |