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