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