Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1 | //===- OutputSections.h -----------------------------------------*- C++ -*-===// |
| 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 | #ifndef LLD_ELF_OUTPUT_SECTIONS_H |
| 11 | #define LLD_ELF_OUTPUT_SECTIONS_H |
| 12 | |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 13 | #include "Config.h" |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 14 | #include "Relocations.h" |
Davide Italiano | 85121bb | 2015-09-25 03:56:11 +0000 | [diff] [blame] | 15 | |
Rui Ueyama | a0752a5 | 2016-03-13 20:28:29 +0000 | [diff] [blame] | 16 | #include "lld/Core/LLVM.h" |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
Rui Ueyama | a0752a5 | 2016-03-13 20:28:29 +0000 | [diff] [blame] | 18 | #include "llvm/MC/StringTableBuilder.h" |
| 19 | #include "llvm/Object/ELF.h" |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MD5.h" |
Rui Ueyama | d86ec30 | 2016-04-07 23:51:56 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SHA1.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 22 | |
| 23 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 24 | namespace elf { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 25 | |
| 26 | class SymbolBody; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 27 | struct EhSectionPiece; |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 28 | template <class ELFT> class SymbolTable; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | template <class ELFT> class SymbolTableSection; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 30 | template <class ELFT> class StringTableSection; |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 31 | template <class ELFT> class EhInputSection; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 32 | template <class ELFT> class InputSection; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 33 | template <class ELFT> class InputSectionBase; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 34 | template <class ELFT> class MergeInputSection; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 35 | template <class ELFT> class MipsReginfoInputSection; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 36 | template <class ELFT> class OutputSection; |
| 37 | template <class ELFT> class ObjectFile; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 38 | template <class ELFT> class SharedFile; |
| 39 | template <class ELFT> class SharedSymbol; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 40 | template <class ELFT> class DefinedRegular; |
| 41 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 42 | // This represents a section in an output file. |
| 43 | // Different sub classes represent different types of sections. Some contain |
| 44 | // input sections, others are created by the linker. |
| 45 | // The writer creates multiple OutputSections and assign them unique, |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 46 | // non-overlapping file offsets and VAs. |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 47 | template <class ELFT> class OutputSectionBase { |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 48 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 49 | typedef typename ELFT::uint uintX_t; |
| 50 | typedef typename ELFT::Shdr Elf_Shdr; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 51 | enum Kind { |
| 52 | Base, |
| 53 | BuildId, |
| 54 | Dynamic, |
| 55 | EHFrame, |
| 56 | EHFrameHdr, |
| 57 | GnuHashTable, |
| 58 | Got, |
| 59 | GotPlt, |
| 60 | HashTable, |
| 61 | Interp, |
| 62 | Merge, |
| 63 | MipsReginfo, |
| 64 | MipsOptions, |
Simon Atanasyan | 85c6b44 | 2016-08-12 06:28:49 +0000 | [diff] [blame] | 65 | MipsAbiFlags, |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 66 | Plt, |
| 67 | Regular, |
| 68 | Reloc, |
| 69 | StrTable, |
| 70 | SymTable, |
| 71 | VersDef, |
| 72 | VersNeed, |
| 73 | VersTable |
| 74 | }; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 75 | |
George Rimar | 9bec24a | 2016-02-18 14:20:08 +0000 | [diff] [blame] | 76 | OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 77 | void setVA(uintX_t VA) { Header.sh_addr = VA; } |
| 78 | uintX_t getVA() const { return Header.sh_addr; } |
| 79 | void setFileOffset(uintX_t Off) { Header.sh_offset = Off; } |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 80 | void setSHName(unsigned Val) { Header.sh_name = Val; } |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 81 | void writeHeaderTo(Elf_Shdr *SHdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 82 | StringRef getName() { return Name; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 83 | |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 84 | virtual void addSection(InputSectionBase<ELFT> *C) {} |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 85 | virtual Kind getKind() const { return Base; } |
| 86 | static bool classof(const OutputSectionBase<ELFT> *B) { |
| 87 | return B->getKind() == Base; |
| 88 | } |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 89 | |
Rui Ueyama | 2317d0d | 2015-10-15 20:55:22 +0000 | [diff] [blame] | 90 | unsigned SectionIndex; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 91 | |
| 92 | // Returns the size of the section in the output file. |
Rafael Espindola | 7757224 | 2015-10-02 19:37:55 +0000 | [diff] [blame] | 93 | uintX_t getSize() const { return Header.sh_size; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 94 | void setSize(uintX_t Val) { Header.sh_size = Val; } |
Rafael Espindola | 571452c | 2016-04-11 13:44:05 +0000 | [diff] [blame] | 95 | uintX_t getFlags() const { return Header.sh_flags; } |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 96 | uint32_t getPhdrFlags() const; |
Rafael Espindola | 571452c | 2016-04-11 13:44:05 +0000 | [diff] [blame] | 97 | uintX_t getFileOff() const { return Header.sh_offset; } |
Rui Ueyama | 3b04d83 | 2016-07-14 05:46:24 +0000 | [diff] [blame] | 98 | uintX_t getAlignment() const { return Header.sh_addralign; } |
Rafael Espindola | 571452c | 2016-04-11 13:44:05 +0000 | [diff] [blame] | 99 | uint32_t getType() const { return Header.sh_type; } |
Rui Ueyama | 3b04d83 | 2016-07-14 05:46:24 +0000 | [diff] [blame] | 100 | |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 101 | void updateAlignment(uintX_t Alignment) { |
| 102 | if (Alignment > Header.sh_addralign) |
| 103 | Header.sh_addralign = Alignment; |
Rafael Espindola | 115f0f3 | 2015-11-03 14:13:40 +0000 | [diff] [blame] | 104 | } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 105 | |
Rui Ueyama | 4709190 | 2016-03-30 19:41:51 +0000 | [diff] [blame] | 106 | // If true, this section will be page aligned on disk. |
| 107 | // Typically the first section of each PT_LOAD segment has this flag. |
| 108 | bool PageAlign = false; |
| 109 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 110 | virtual void finalize() {} |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 111 | virtual void finalizePieces() {} |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 112 | virtual void assignOffsets() {} |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 113 | virtual void writeTo(uint8_t *Buf) {} |
Rui Ueyama | d4ea7dd | 2015-12-26 07:01:26 +0000 | [diff] [blame] | 114 | virtual ~OutputSectionBase() = default; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 115 | |
| 116 | protected: |
| 117 | StringRef Name; |
Sean Silva | 580c1b6 | 2016-04-20 04:26:16 +0000 | [diff] [blame] | 118 | Elf_Shdr Header; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 121 | template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> { |
| 122 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 123 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 124 | |
| 125 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 126 | GotSection(); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 127 | void finalize() override; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 128 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 129 | void addEntry(SymbolBody &Sym); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 130 | void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr); |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 131 | bool addDynTlsEntry(SymbolBody &Sym); |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 132 | bool addTlsIndex(); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 133 | bool empty() const { return MipsPageEntries == 0 && Entries.empty(); } |
Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 134 | uintX_t getMipsLocalPageOffset(uintX_t Addr); |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 135 | uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const; |
George Rimar | 90cd0a8 | 2015-12-01 19:20:26 +0000 | [diff] [blame] | 136 | uintX_t getGlobalDynAddr(const SymbolBody &B) const; |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 137 | uintX_t getGlobalDynOffset(const SymbolBody &B) const; |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 138 | uintX_t getNumEntries() const { return Entries.size(); } |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 139 | typename Base::Kind getKind() const override { return Base::Got; } |
| 140 | static bool classof(const Base *B) { return B->getKind() == Base::Got; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 141 | |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 142 | // Returns the symbol which corresponds to the first entry of the global part |
| 143 | // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic |
| 144 | // table properties. |
| 145 | // Returns nullptr if the global part is empty. |
| 146 | const SymbolBody *getMipsFirstGlobalEntry() const; |
| 147 | |
| 148 | // Returns the number of entries in the local part of GOT including |
| 149 | // the number of reserved entries. This method is MIPS-specific. |
| 150 | unsigned getMipsLocalEntriesNum() const; |
| 151 | |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 152 | // Returns offset of TLS part of the MIPS GOT table. This part goes |
| 153 | // after 'local' and 'global' entries. |
| 154 | uintX_t getMipsTlsOffset(); |
| 155 | |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 156 | uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; } |
Rafael Espindola | 74031ba | 2016-04-07 15:20:56 +0000 | [diff] [blame] | 157 | uint32_t getTlsIndexOff() { return TlsIndexOff; } |
George Rimar | b17f739 | 2015-12-01 18:24:07 +0000 | [diff] [blame] | 158 | |
Rui Ueyama | 022d8e8 | 2016-05-24 03:36:07 +0000 | [diff] [blame] | 159 | // Flag to force GOT to be in output if we have relocations |
| 160 | // that relies on its address. |
| 161 | bool HasGotOffRel = false; |
| 162 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 163 | private: |
| 164 | std::vector<const SymbolBody *> Entries; |
Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 165 | uint32_t TlsIndexOff = -1; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 166 | uint32_t MipsPageEntries = 0; |
Simon Atanasyan | d2980d3 | 2016-03-29 14:07:22 +0000 | [diff] [blame] | 167 | // Output sections referenced by MIPS GOT relocations. |
| 168 | llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections; |
Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 169 | llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos; |
Simon Atanasyan | 4132511 | 2016-06-19 21:39:37 +0000 | [diff] [blame] | 170 | |
| 171 | // MIPS ABI requires to create unique GOT entry for each Symbol/Addend |
| 172 | // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal` |
| 173 | // or `MipsGlobal` vectors. In general it does not have a sence to take in |
| 174 | // account addend for preemptible symbols because the corresponding |
| 175 | // GOT entries should have one-to-one mapping with dynamic symbols table. |
| 176 | // But we use the same container's types for both kind of GOT entries |
| 177 | // to handle them uniformly. |
| 178 | typedef std::pair<const SymbolBody*, uintX_t> MipsGotEntry; |
| 179 | typedef std::vector<MipsGotEntry> MipsGotEntries; |
| 180 | llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap; |
| 181 | MipsGotEntries MipsLocal; |
| 182 | MipsGotEntries MipsGlobal; |
| 183 | |
| 184 | // Write MIPS-specific parts of the GOT. |
| 185 | void writeMipsGot(uint8_t *&Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 188 | template <class ELFT> |
| 189 | class GotPltSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 190 | typedef typename ELFT::uint uintX_t; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 191 | typedef OutputSectionBase<ELFT> Base; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 192 | |
| 193 | public: |
| 194 | GotPltSection(); |
| 195 | void finalize() override; |
| 196 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 197 | void addEntry(SymbolBody &Sym); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 198 | bool empty() const; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 199 | typename Base::Kind getKind() const override { return Base::GotPlt; } |
| 200 | static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 201 | |
| 202 | private: |
| 203 | std::vector<const SymbolBody *> Entries; |
| 204 | }; |
| 205 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 206 | template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> { |
| 207 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 208 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 209 | |
| 210 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 211 | PltSection(); |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 212 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 213 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 67d72c0 | 2016-03-11 12:06:30 +0000 | [diff] [blame] | 214 | void addEntry(SymbolBody &Sym); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 215 | bool empty() const { return Entries.empty(); } |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 216 | typename Base::Kind getKind() const override { return Base::Plt; } |
| 217 | static bool classof(const Base *B) { return B->getKind() == Base::Plt; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 218 | |
| 219 | private: |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 220 | std::vector<std::pair<const SymbolBody *, unsigned>> Entries; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 223 | template <class ELFT> class DynamicReloc { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 224 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 225 | |
| 226 | public: |
| 227 | DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec, |
| 228 | uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym, |
| 229 | uintX_t Addend) |
| 230 | : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec), |
| 231 | UseSymVA(UseSymVA), Addend(Addend) {} |
| 232 | |
| 233 | DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec, |
| 234 | uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym, |
| 235 | uintX_t Addend) |
| 236 | : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec), |
| 237 | UseSymVA(UseSymVA), Addend(Addend) {} |
| 238 | |
| 239 | uintX_t getOffset() const; |
| 240 | uintX_t getAddend() const; |
| 241 | uint32_t getSymIndex() const; |
Simon Atanasyan | 002e244 | 2016-06-23 15:26:31 +0000 | [diff] [blame] | 242 | const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; } |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 243 | |
Rafael Espindola | de9857e | 2016-02-04 21:33:05 +0000 | [diff] [blame] | 244 | uint32_t Type; |
| 245 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 246 | private: |
Rafael Espindola | ac568f9 | 2016-04-11 13:47:35 +0000 | [diff] [blame] | 247 | SymbolBody *Sym; |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 248 | const InputSectionBase<ELFT> *InputSec = nullptr; |
| 249 | const OutputSectionBase<ELFT> *OutputSec = nullptr; |
Rafael Espindola | ac568f9 | 2016-04-11 13:47:35 +0000 | [diff] [blame] | 250 | uintX_t OffsetInSec; |
| 251 | bool UseSymVA; |
| 252 | uintX_t Addend; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 256 | class SymbolTableSection final : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 257 | typedef OutputSectionBase<ELFT> Base; |
| 258 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 259 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 260 | typedef typename ELFT::Shdr Elf_Shdr; |
| 261 | typedef typename ELFT::Sym Elf_Sym; |
| 262 | typedef typename ELFT::SymRange Elf_Sym_Range; |
| 263 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 264 | SymbolTableSection(StringTableSection<ELFT> &StrTabSec); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 265 | |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 266 | void finalize() override; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 267 | void writeTo(uint8_t *Buf) override; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 268 | void addSymbol(SymbolBody *Body); |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 269 | StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; } |
Rafael Espindola | 0e92f24 | 2016-01-27 16:41:24 +0000 | [diff] [blame] | 270 | unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; } |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 271 | typename Base::Kind getKind() const override { return Base::SymTable; } |
| 272 | static bool classof(const Base *B) { return B->getKind() == Base::SymTable; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 273 | |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 274 | ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const { |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 275 | return Symbols; |
| 276 | } |
| 277 | |
| 278 | unsigned NumLocals = 0; |
| 279 | StringTableSection<ELFT> &StrTabSec; |
Igor Kudrin | ab665fc | 2015-10-20 21:47:58 +0000 | [diff] [blame] | 280 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 281 | private: |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 282 | void writeLocalSymbols(uint8_t *&Buf); |
Igor Kudrin | ea6a835 | 2015-10-19 08:01:51 +0000 | [diff] [blame] | 283 | void writeGlobalSymbols(uint8_t *Buf); |
Rui Ueyama | 8ddfa81 | 2015-09-30 00:32:10 +0000 | [diff] [blame] | 284 | |
Rui Ueyama | 874e7ae | 2016-02-17 04:56:44 +0000 | [diff] [blame] | 285 | const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym); |
Igor Kudrin | 853b88d | 2015-10-20 20:52:14 +0000 | [diff] [blame] | 286 | |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 287 | // A vector of symbols and their string table offsets. |
| 288 | std::vector<std::pair<SymbolBody *, size_t>> Symbols; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 291 | // For more information about .gnu.version and .gnu.version_r see: |
| 292 | // https://www.akkadia.org/drepper/symbol-versioning |
| 293 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 294 | // The .gnu.version_d section which has a section type of SHT_GNU_verdef shall |
| 295 | // contain symbol version definitions. The number of entries in this section |
| 296 | // shall be contained in the DT_VERDEFNUM entry of the .dynamic section. |
| 297 | // The section shall contain an array of Elf_Verdef structures, optionally |
| 298 | // followed by an array of Elf_Verdaux structures. |
| 299 | template <class ELFT> |
| 300 | class VersionDefinitionSection final : public OutputSectionBase<ELFT> { |
| 301 | typedef typename ELFT::Verdef Elf_Verdef; |
| 302 | typedef typename ELFT::Verdaux Elf_Verdaux; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 303 | typedef OutputSectionBase<ELFT> Base; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 304 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 305 | public: |
| 306 | VersionDefinitionSection(); |
| 307 | void finalize() override; |
| 308 | void writeTo(uint8_t *Buf) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 309 | typename Base::Kind getKind() const override { return Base::VersDef; } |
| 310 | static bool classof(const Base *B) { return B->getKind() == Base::VersDef; } |
Rui Ueyama | 9f61964 | 2016-07-16 02:29:45 +0000 | [diff] [blame] | 311 | |
| 312 | private: |
| 313 | void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff); |
| 314 | |
| 315 | unsigned FileDefNameOff; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 318 | // The .gnu.version section specifies the required version of each symbol in the |
| 319 | // dynamic symbol table. It contains one Elf_Versym for each dynamic symbol |
| 320 | // table entry. An Elf_Versym is just a 16-bit integer that refers to a version |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 321 | // identifier defined in the either .gnu.version_r or .gnu.version_d section. |
| 322 | // The values 0 and 1 are reserved. All other values are used for versions in |
| 323 | // the own object or in any of the dependencies. |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 324 | template <class ELFT> |
| 325 | class VersionTableSection final : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 326 | typedef OutputSectionBase<ELFT> Base; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 327 | typedef typename ELFT::Versym Elf_Versym; |
| 328 | |
| 329 | public: |
| 330 | VersionTableSection(); |
| 331 | void finalize() override; |
| 332 | void writeTo(uint8_t *Buf) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 333 | typename Base::Kind getKind() const override { return Base::VersTable; } |
| 334 | static bool classof(const Base *B) { return B->getKind() == Base::VersTable; } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | // The .gnu.version_r section defines the version identifiers used by |
| 338 | // .gnu.version. It contains a linked list of Elf_Verneed data structures. Each |
| 339 | // Elf_Verneed specifies the version requirements for a single DSO, and contains |
| 340 | // a reference to a linked list of Elf_Vernaux data structures which define the |
| 341 | // mapping from version identifiers to version names. |
| 342 | template <class ELFT> |
| 343 | class VersionNeedSection final : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 344 | typedef OutputSectionBase<ELFT> Base; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 345 | typedef typename ELFT::Verneed Elf_Verneed; |
| 346 | typedef typename ELFT::Vernaux Elf_Vernaux; |
| 347 | |
| 348 | // A vector of shared files that need Elf_Verneed data structures and the |
| 349 | // string table offsets of their sonames. |
| 350 | std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed; |
| 351 | |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 352 | // The next available version identifier. |
| 353 | unsigned NextIndex; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 354 | |
| 355 | public: |
| 356 | VersionNeedSection(); |
| 357 | void addSymbol(SharedSymbol<ELFT> *SS); |
| 358 | void finalize() override; |
| 359 | void writeTo(uint8_t *Buf) override; |
| 360 | size_t getNeedNum() const { return Needed.size(); } |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 361 | typename Base::Kind getKind() const override { return Base::VersNeed; } |
| 362 | static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; } |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 365 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 366 | class RelocationSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 367 | typedef typename ELFT::Rel Elf_Rel; |
| 368 | typedef typename ELFT::Rela Elf_Rela; |
| 369 | typedef typename ELFT::uint uintX_t; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 370 | typedef OutputSectionBase<ELFT> Base; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 371 | |
| 372 | public: |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 373 | RelocationSection(StringRef Name, bool Sort); |
Rafael Espindola | d30eb7d | 2016-02-05 15:03:10 +0000 | [diff] [blame] | 374 | void addReloc(const DynamicReloc<ELFT> &Reloc); |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 375 | unsigned getRelocOffset(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 376 | void finalize() override; |
| 377 | void writeTo(uint8_t *Buf) override; |
| 378 | bool hasRelocs() const { return !Relocs.empty(); } |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 379 | typename Base::Kind getKind() const override { return Base::Reloc; } |
| 380 | static bool classof(const Base *B) { return B->getKind() == Base::Reloc; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 381 | |
| 382 | private: |
George Rimar | c191acf | 2016-05-10 15:47:57 +0000 | [diff] [blame] | 383 | bool Sort; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 384 | std::vector<DynamicReloc<ELFT>> Relocs; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 388 | class OutputSection final : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 389 | typedef OutputSectionBase<ELFT> Base; |
| 390 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 391 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 392 | typedef typename ELFT::Shdr Elf_Shdr; |
| 393 | typedef typename ELFT::Sym Elf_Sym; |
| 394 | typedef typename ELFT::Rel Elf_Rel; |
| 395 | typedef typename ELFT::Rela Elf_Rela; |
| 396 | typedef typename ELFT::uint uintX_t; |
George Rimar | 9bec24a | 2016-02-18 14:20:08 +0000 | [diff] [blame] | 397 | OutputSection(StringRef Name, uint32_t Type, uintX_t Flags); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 398 | void addSection(InputSectionBase<ELFT> *C) override; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 399 | void sortInitFini(); |
| 400 | void sortCtorsDtors(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 401 | void writeTo(uint8_t *Buf) override; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 402 | void finalize() override; |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 403 | void assignOffsets() override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 404 | typename Base::Kind getKind() const override { return Base::Regular; } |
| 405 | static bool classof(const Base *B) { return B->getKind() == Base::Regular; } |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 406 | std::vector<InputSection<ELFT> *> Sections; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 409 | template <class ELFT> |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 410 | class MergeOutputSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 411 | typedef typename ELFT::uint uintX_t; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 412 | typedef OutputSectionBase<ELFT> Base; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 413 | |
| 414 | public: |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 415 | MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags, |
| 416 | uintX_t Alignment); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 417 | void addSection(InputSectionBase<ELFT> *S) override; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 418 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 419 | unsigned getOffset(StringRef Val); |
| 420 | void finalize() override; |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 421 | void finalizePieces() override; |
Peter Collingbourne | e29e142 | 2016-05-05 04:10:12 +0000 | [diff] [blame] | 422 | bool shouldTailMerge() const; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 423 | typename Base::Kind getKind() const override { return Base::Merge; } |
| 424 | static bool classof(const Base *B) { return B->getKind() == Base::Merge; } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 425 | |
| 426 | private: |
Rafael Espindola | 7efa5be | 2016-02-19 14:17:40 +0000 | [diff] [blame] | 427 | llvm::StringTableBuilder Builder; |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 428 | std::vector<MergeInputSection<ELFT> *> Sections; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 429 | }; |
| 430 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 431 | struct CieRecord { |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 432 | EhSectionPiece *Piece = nullptr; |
| 433 | std::vector<EhSectionPiece *> FdePieces; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 434 | }; |
| 435 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 436 | // Output section for .eh_frame. |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 437 | template <class ELFT> |
Rui Ueyama | 1e479c2 | 2016-05-23 15:07:59 +0000 | [diff] [blame] | 438 | class EhOutputSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 439 | typedef typename ELFT::uint uintX_t; |
| 440 | typedef typename ELFT::Shdr Elf_Shdr; |
| 441 | typedef typename ELFT::Rel Elf_Rel; |
| 442 | typedef typename ELFT::Rela Elf_Rela; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 443 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | f86cb90 | 2016-05-23 15:12:41 +0000 | [diff] [blame] | 444 | |
| 445 | public: |
| 446 | EhOutputSection(); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 447 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | 56004c5 | 2016-04-07 14:22:09 +0000 | [diff] [blame] | 448 | void finalize() override; |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 449 | bool empty() const { return Sections.empty(); } |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 450 | |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 451 | void addSection(InputSectionBase<ELFT> *S) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 452 | typename Base::Kind getKind() const override { return Base::EHFrame; } |
| 453 | static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; } |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 454 | |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 455 | size_t NumFdes = 0; |
| 456 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 457 | private: |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 458 | template <class RelTy> |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 459 | void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 460 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 461 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 462 | CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec, |
| 463 | ArrayRef<RelTy> Rels); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 464 | |
| 465 | template <class RelTy> |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 466 | bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec, |
| 467 | ArrayRef<RelTy> Rels); |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 468 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 469 | uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc); |
| 470 | |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 471 | std::vector<EhInputSection<ELFT> *> Sections; |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 472 | std::vector<CieRecord *> Cies; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 473 | |
Rui Ueyama | f8b285c | 2016-05-22 23:16:14 +0000 | [diff] [blame] | 474 | // CIE records are uniquified by their contents and personality functions. |
| 475 | llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 478 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 479 | class InterpSection final : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 480 | typedef OutputSectionBase<ELFT> Base; |
| 481 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 482 | public: |
| 483 | InterpSection(); |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 484 | void writeTo(uint8_t *Buf) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 485 | typename Base::Kind getKind() const override { return Base::Interp; } |
| 486 | static bool classof(const Base *B) { return B->getKind() == Base::Interp; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 489 | template <class ELFT> |
| 490 | class StringTableSection final : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 491 | typedef OutputSectionBase<ELFT> Base; |
| 492 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 493 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 494 | typedef typename ELFT::uint uintX_t; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 495 | StringTableSection(StringRef Name, bool Dynamic); |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 496 | unsigned addString(StringRef S, bool HashIt = true); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 497 | void writeTo(uint8_t *Buf) override; |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 498 | unsigned getSize() const { return Size; } |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 499 | void finalize() override { this->Header.sh_size = getSize(); } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 500 | bool isDynamic() const { return Dynamic; } |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 501 | typename Base::Kind getKind() const override { return Base::StrTable; } |
| 502 | static bool classof(const Base *B) { return B->getKind() == Base::StrTable; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 503 | |
| 504 | private: |
| 505 | const bool Dynamic; |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 506 | llvm::DenseMap<StringRef, unsigned> StringMap; |
Rui Ueyama | 76c0063 | 2016-01-07 02:35:32 +0000 | [diff] [blame] | 507 | std::vector<StringRef> Strings; |
Rafael Espindola | e2c2461 | 2016-01-29 01:24:25 +0000 | [diff] [blame] | 508 | unsigned Size = 1; // ELF string tables start with a NUL byte, so 1. |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 512 | class HashTableSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 513 | typedef typename ELFT::Word Elf_Word; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 514 | typedef OutputSectionBase<ELFT> Base; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 515 | |
| 516 | public: |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 517 | HashTableSection(); |
Rui Ueyama | 0db335f | 2015-10-07 16:58:54 +0000 | [diff] [blame] | 518 | void finalize() override; |
| 519 | void writeTo(uint8_t *Buf) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 520 | typename Base::Kind getKind() const override { return Base::HashTable; } |
| 521 | static bool classof(const Base *B) { return B->getKind() == Base::HashTable; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 524 | // Outputs GNU Hash section. For detailed explanation see: |
| 525 | // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections |
| 526 | template <class ELFT> |
| 527 | class GnuHashTableSection final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 528 | typedef typename ELFT::Off Elf_Off; |
| 529 | typedef typename ELFT::Word Elf_Word; |
| 530 | typedef typename ELFT::uint uintX_t; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 531 | typedef OutputSectionBase<ELFT> Base; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 532 | |
| 533 | public: |
| 534 | GnuHashTableSection(); |
| 535 | void finalize() override; |
| 536 | void writeTo(uint8_t *Buf) override; |
| 537 | |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 538 | // Adds symbols to the hash table. |
| 539 | // Sorts the input to satisfy GNU hash section requirements. |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 540 | void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols); |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 541 | typename Base::Kind getKind() const override { return Base::GnuHashTable; } |
| 542 | static bool classof(const Base *B) { |
| 543 | return B->getKind() == Base::GnuHashTable; |
| 544 | } |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 545 | |
| 546 | private: |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 547 | static unsigned calcNBuckets(unsigned NumHashed); |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 548 | static unsigned calcMaskWords(unsigned NumHashed); |
| 549 | |
| 550 | void writeHeader(uint8_t *&Buf); |
| 551 | void writeBloomFilter(uint8_t *&Buf); |
| 552 | void writeHashTable(uint8_t *Buf); |
| 553 | |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 554 | struct SymbolData { |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 555 | SymbolBody *Body; |
Rui Ueyama | c2e863a | 2016-02-17 05:06:40 +0000 | [diff] [blame] | 556 | size_t STName; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 557 | uint32_t Hash; |
| 558 | }; |
| 559 | |
Rui Ueyama | 861c731 | 2016-02-17 05:40:03 +0000 | [diff] [blame] | 560 | std::vector<SymbolData> Symbols; |
Igor Kudrin | f1d6029 | 2015-10-28 07:05:56 +0000 | [diff] [blame] | 561 | |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 562 | unsigned MaskWords; |
| 563 | unsigned NBuckets; |
| 564 | unsigned Shift2; |
| 565 | }; |
| 566 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 567 | template <class ELFT> |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 568 | class DynamicSection final : public OutputSectionBase<ELFT> { |
| 569 | typedef OutputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 570 | typedef typename ELFT::Dyn Elf_Dyn; |
| 571 | typedef typename ELFT::Rel Elf_Rel; |
| 572 | typedef typename ELFT::Rela Elf_Rela; |
| 573 | typedef typename ELFT::Shdr Elf_Shdr; |
| 574 | typedef typename ELFT::Sym Elf_Sym; |
| 575 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 576 | |
Rui Ueyama | 909cc68 | 2016-02-02 03:11:27 +0000 | [diff] [blame] | 577 | // The .dynamic section contains information for the dynamic linker. |
| 578 | // The section consists of fixed size entries, which consist of |
| 579 | // type and value fields. Value are one of plain integers, symbol |
| 580 | // addresses, or section addresses. This struct represents the entry. |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 581 | struct Entry { |
| 582 | int32_t Tag; |
| 583 | union { |
| 584 | OutputSectionBase<ELFT> *OutSec; |
| 585 | uint64_t Val; |
| 586 | const SymbolBody *Sym; |
| 587 | }; |
George Rimar | 03e0560 | 2016-08-19 15:23:39 +0000 | [diff] [blame^] | 588 | enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind; |
| 589 | Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr) |
| 590 | : Tag(Tag), OutSec(OutSec), Kind(Kind) {} |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 591 | Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {} |
| 592 | Entry(int32_t Tag, const SymbolBody *Sym) |
| 593 | : Tag(Tag), Sym(Sym), Kind(SymAddr) {} |
| 594 | }; |
Rui Ueyama | 909cc68 | 2016-02-02 03:11:27 +0000 | [diff] [blame] | 595 | |
| 596 | // finalize() fills this vector with the section contents. finalize() |
| 597 | // cannot directly create final section contents because when the |
| 598 | // function is called, symbol or section addresses are not fixed yet. |
Rafael Espindola | de06936 | 2016-01-25 21:32:04 +0000 | [diff] [blame] | 599 | std::vector<Entry> Entries; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 600 | |
| 601 | public: |
Rui Ueyama | ace4f90 | 2016-05-24 04:25:47 +0000 | [diff] [blame] | 602 | explicit DynamicSection(); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 603 | void finalize() override; |
| 604 | void writeTo(uint8_t *Buf) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 605 | typename Base::Kind getKind() const override { return Base::Dynamic; } |
| 606 | static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; } |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 607 | }; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 608 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 609 | template <class ELFT> |
| 610 | class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> { |
| 611 | typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 612 | typedef OutputSectionBase<ELFT> Base; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 613 | |
| 614 | public: |
| 615 | MipsReginfoOutputSection(); |
| 616 | void writeTo(uint8_t *Buf) override; |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 617 | void addSection(InputSectionBase<ELFT> *S) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 618 | typename Base::Kind getKind() const override { return Base::MipsReginfo; } |
| 619 | static bool classof(const Base *B) { |
| 620 | return B->getKind() == Base::MipsReginfo; |
| 621 | } |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 622 | |
| 623 | private: |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 624 | uint32_t GprMask = 0; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 627 | template <class ELFT> |
| 628 | class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> { |
| 629 | typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options; |
| 630 | typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 631 | typedef OutputSectionBase<ELFT> Base; |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 632 | |
| 633 | public: |
| 634 | MipsOptionsOutputSection(); |
| 635 | void writeTo(uint8_t *Buf) override; |
| 636 | void addSection(InputSectionBase<ELFT> *S) override; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 637 | typename Base::Kind getKind() const override { return Base::MipsOptions; } |
| 638 | static bool classof(const Base *B) { |
| 639 | return B->getKind() == Base::MipsOptions; |
| 640 | } |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 641 | |
| 642 | private: |
| 643 | uint32_t GprMask = 0; |
| 644 | }; |
| 645 | |
Simon Atanasyan | 85c6b44 | 2016-08-12 06:28:49 +0000 | [diff] [blame] | 646 | template <class ELFT> |
| 647 | class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> { |
| 648 | typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags; |
| 649 | typedef OutputSectionBase<ELFT> Base; |
| 650 | |
| 651 | public: |
| 652 | MipsAbiFlagsOutputSection(); |
| 653 | void writeTo(uint8_t *Buf) override; |
| 654 | void addSection(InputSectionBase<ELFT> *S) override; |
| 655 | typename Base::Kind getKind() const override { return Base::MipsAbiFlags; } |
| 656 | static bool classof(const Base *B) { |
| 657 | return B->getKind() == Base::MipsAbiFlags; |
| 658 | } |
| 659 | |
| 660 | private: |
| 661 | Elf_Mips_ABIFlags Flags; |
| 662 | }; |
| 663 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 664 | // --eh-frame-hdr option tells linker to construct a header for all the |
| 665 | // .eh_frame sections. This header is placed to a section named .eh_frame_hdr |
| 666 | // and also to a PT_GNU_EH_FRAME segment. |
| 667 | // At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by |
| 668 | // calling dl_iterate_phdr. |
| 669 | // This section contains a lookup table for quick binary search of FDEs. |
| 670 | // Detailed info about internals can be found in Ian Lance Taylor's blog: |
| 671 | // http://www.airs.com/blog/archives/460 (".eh_frame") |
| 672 | // http://www.airs.com/blog/archives/462 (".eh_frame_hdr") |
| 673 | template <class ELFT> |
| 674 | class EhFrameHeader final : public OutputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 675 | typedef typename ELFT::uint uintX_t; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 676 | typedef OutputSectionBase<ELFT> Base; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 677 | |
| 678 | public: |
| 679 | EhFrameHeader(); |
Rui Ueyama | de9777a | 2016-05-23 16:30:41 +0000 | [diff] [blame] | 680 | void finalize() override; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 681 | void writeTo(uint8_t *Buf) override; |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 682 | void addFde(uint32_t Pc, uint32_t FdeVA); |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 683 | typename Base::Kind getKind() const override { return Base::EHFrameHdr; } |
| 684 | static bool classof(const Base *B) { |
| 685 | return B->getKind() == Base::EHFrameHdr; |
| 686 | } |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 687 | |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 688 | private: |
| 689 | struct FdeData { |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 690 | uint32_t Pc; |
| 691 | uint32_t FdeVA; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 692 | }; |
| 693 | |
Rui Ueyama | e75e933 | 2016-05-23 03:00:33 +0000 | [diff] [blame] | 694 | std::vector<FdeData> Fdes; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 695 | }; |
| 696 | |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 697 | template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> { |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 698 | typedef OutputSectionBase<ELFT> Base; |
| 699 | |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 700 | public: |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 701 | void writeTo(uint8_t *Buf) override; |
Rui Ueyama | dd368fc | 2016-05-02 23:35:59 +0000 | [diff] [blame] | 702 | virtual void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) = 0; |
Eugene Leviant | 9d278b6 | 2016-08-10 18:10:41 +0000 | [diff] [blame] | 703 | typename Base::Kind getKind() const override { return Base::BuildId; } |
| 704 | static bool classof(const Base *B) { return B->getKind() == Base::BuildId; } |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 705 | |
| 706 | protected: |
| 707 | BuildIdSection(size_t HashSize); |
| 708 | size_t HashSize; |
| 709 | uint8_t *HashBuf = nullptr; |
| 710 | }; |
| 711 | |
| 712 | template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> { |
| 713 | public: |
| 714 | BuildIdFnv1() : BuildIdSection<ELFT>(8) {} |
Rui Ueyama | dd368fc | 2016-05-02 23:35:59 +0000 | [diff] [blame] | 715 | void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override; |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 716 | }; |
| 717 | |
| 718 | template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> { |
| 719 | public: |
| 720 | BuildIdMd5() : BuildIdSection<ELFT>(16) {} |
Rui Ueyama | dd368fc | 2016-05-02 23:35:59 +0000 | [diff] [blame] | 721 | void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override; |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 722 | }; |
| 723 | |
Rui Ueyama | d86ec30 | 2016-04-07 23:51:56 +0000 | [diff] [blame] | 724 | template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> { |
| 725 | public: |
| 726 | BuildIdSha1() : BuildIdSection<ELFT>(20) {} |
Rui Ueyama | dd368fc | 2016-05-02 23:35:59 +0000 | [diff] [blame] | 727 | void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override; |
Rui Ueyama | d86ec30 | 2016-04-07 23:51:56 +0000 | [diff] [blame] | 728 | }; |
| 729 | |
Rui Ueyama | 9194db7 | 2016-05-13 21:55:56 +0000 | [diff] [blame] | 730 | template <class ELFT> |
| 731 | class BuildIdHexstring final : public BuildIdSection<ELFT> { |
| 732 | public: |
| 733 | BuildIdHexstring(); |
| 734 | void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override; |
| 735 | }; |
| 736 | |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 737 | // All output sections that are hadnled by the linker specially are |
| 738 | // globally accessible. Writer initializes them, so don't use them |
| 739 | // until Writer is initialized. |
| 740 | template <class ELFT> struct Out { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 741 | typedef typename ELFT::uint uintX_t; |
| 742 | typedef typename ELFT::Phdr Elf_Phdr; |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 743 | static BuildIdSection<ELFT> *BuildId; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 744 | static DynamicSection<ELFT> *Dynamic; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 745 | static EhFrameHeader<ELFT> *EhFrameHdr; |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 746 | static EhOutputSection<ELFT> *EhFrame; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 747 | static GnuHashTableSection<ELFT> *GnuHashTab; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 748 | static GotPltSection<ELFT> *GotPlt; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 749 | static GotSection<ELFT> *Got; |
| 750 | static HashTableSection<ELFT> *HashTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 751 | static InterpSection<ELFT> *Interp; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 752 | static OutputSection<ELFT> *Bss; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 753 | static OutputSection<ELFT> *MipsRldMap; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 754 | static OutputSectionBase<ELFT> *Opd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 755 | static uint8_t *OpdBuf; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 756 | static PltSection<ELFT> *Plt; |
| 757 | static RelocationSection<ELFT> *RelaDyn; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 758 | static RelocationSection<ELFT> *RelaPlt; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 759 | static StringTableSection<ELFT> *DynStrTab; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 760 | static StringTableSection<ELFT> *ShStrTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 761 | static StringTableSection<ELFT> *StrTab; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 762 | static SymbolTableSection<ELFT> *DynSymTab; |
| 763 | static SymbolTableSection<ELFT> *SymTab; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 764 | static VersionDefinitionSection<ELFT> *VerDef; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 765 | static VersionTableSection<ELFT> *VerSym; |
| 766 | static VersionNeedSection<ELFT> *VerNeed; |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 767 | static Elf_Phdr *TlsPhdr; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 768 | static OutputSectionBase<ELFT> *ElfHeader; |
| 769 | static OutputSectionBase<ELFT> *ProgramHeaders; |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 770 | |
| 771 | static OutputSectionBase<ELFT> *PreinitArray; |
| 772 | static OutputSectionBase<ELFT> *InitArray; |
| 773 | static OutputSectionBase<ELFT> *FiniArray; |
Rui Ueyama | bdf836d | 2016-08-12 01:10:17 +0000 | [diff] [blame] | 774 | |
| 775 | // This pool owns dynamically-allocated output sections. |
| 776 | static std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Pool; |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 777 | }; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 778 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 779 | template <bool Is64Bits> struct SectionKey { |
| 780 | typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; |
| 781 | StringRef Name; |
| 782 | uint32_t Type; |
| 783 | uintX_t Flags; |
| 784 | uintX_t Alignment; |
| 785 | }; |
| 786 | |
| 787 | // This class knows how to create an output section for a given |
| 788 | // input section. Output section type is determined by various |
| 789 | // factors, including input section's sh_flags, sh_type and |
| 790 | // linker scripts. |
| 791 | template <class ELFT> class OutputSectionFactory { |
| 792 | typedef typename ELFT::Shdr Elf_Shdr; |
| 793 | typedef typename ELFT::uint uintX_t; |
| 794 | typedef typename elf::SectionKey<ELFT::Is64Bits> Key; |
| 795 | |
| 796 | public: |
| 797 | std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C, |
| 798 | StringRef OutsecName); |
| 799 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 800 | private: |
| 801 | Key createKey(InputSectionBase<ELFT> *C, StringRef OutsecName); |
| 802 | |
| 803 | llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map; |
| 804 | }; |
| 805 | |
Rui Ueyama | 634ddf0 | 2016-03-11 20:51:53 +0000 | [diff] [blame] | 806 | template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 807 | template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic; |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 808 | template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr; |
Rui Ueyama | 3b31e67 | 2016-05-23 16:24:16 +0000 | [diff] [blame] | 809 | template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame; |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 810 | template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 811 | template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 812 | template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got; |
| 813 | template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 814 | template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp; |
Rafael Espindola | d7a267b | 2015-11-03 22:01:20 +0000 | [diff] [blame] | 815 | template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss; |
Igor Kudrin | 304860a | 2015-11-12 04:39:49 +0000 | [diff] [blame] | 816 | template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 817 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 818 | template <class ELFT> uint8_t *Out<ELFT>::OpdBuf; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 819 | template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt; |
| 820 | template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 821 | template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 822 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab; |
George Rimar | 0f5ac9f | 2015-10-20 17:21:35 +0000 | [diff] [blame] | 823 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 824 | template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab; |
Rui Ueyama | d888d10 | 2015-10-09 19:34:55 +0000 | [diff] [blame] | 825 | template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab; |
| 826 | template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab; |
George Rimar | d356630 | 2016-06-20 11:55:12 +0000 | [diff] [blame] | 827 | template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef; |
Peter Collingbourne | 21a12fc | 2016-04-27 20:22:31 +0000 | [diff] [blame] | 828 | template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym; |
| 829 | template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 830 | template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr; |
Rafael Espindola | 4fc6044 | 2016-02-10 22:43:13 +0000 | [diff] [blame] | 831 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader; |
| 832 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders; |
Rui Ueyama | a8f6fea | 2016-08-09 04:25:20 +0000 | [diff] [blame] | 833 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray; |
| 834 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray; |
| 835 | template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray; |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 836 | |
Rui Ueyama | bdf836d | 2016-08-12 01:10:17 +0000 | [diff] [blame] | 837 | template <class ELFT> |
| 838 | std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Out<ELFT>::Pool; |
| 839 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 840 | } // namespace elf |
Eugene Zelenko | 6e43b49 | 2015-11-04 02:11:57 +0000 | [diff] [blame] | 841 | } // namespace lld |
| 842 | |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 843 | namespace llvm { |
| 844 | template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> { |
| 845 | typedef typename lld::elf::SectionKey<Is64Bits> Key; |
| 846 | |
| 847 | static Key getEmptyKey(); |
| 848 | static Key getTombstoneKey(); |
| 849 | static unsigned getHashValue(const Key &Val); |
| 850 | static bool isEqual(const Key &LHS, const Key &RHS); |
| 851 | }; |
| 852 | } |
| 853 | |
| 854 | #endif |