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