Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 1 | //===- InputSection.h -------------------------------------------*- C++ -*-===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 10 | #ifndef LLD_ELF_INPUT_SECTION_H |
| 11 | #define LLD_ELF_INPUT_SECTION_H |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 12 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 13 | #include "Config.h" |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 14 | #include "Relocations.h" |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 15 | #include "Thunks.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | #include "lld/Core/LLVM.h" |
Rui Ueyama | 8f687f7 | 2016-12-19 03:14:16 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/CachedHashString.h" |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
Rui Ueyama | c00718f | 2016-02-23 03:34:37 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/TinyPtrVector.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 | #include "llvm/Object/ELF.h" |
Kamil Rytarowski | e739e49 | 2017-05-24 18:31:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Threading.h" |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 22 | #include <mutex> |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
| 24 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 25 | namespace elf { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 27 | class DefinedCommon; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 28 | class SymbolBody; |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 29 | struct SectionPiece; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 30 | |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 31 | class DefinedRegular; |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 32 | class SyntheticSection; |
Rafael Espindola | 66b4e21 | 2017-02-23 22:06:28 +0000 | [diff] [blame] | 33 | template <class ELFT> class EhFrameSection; |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 34 | class MergeSyntheticSection; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 35 | template <class ELFT> class ObjectFile; |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 36 | class OutputSection; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 37 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 38 | // This is the base class of all sections that lld handles. Some are sections in |
| 39 | // input files, some are sections in the produced output file and some exist |
| 40 | // just as a convenience for implementing special ways of combining some |
| 41 | // sections. |
| 42 | class SectionBase { |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 43 | public: |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 44 | enum Kind { Regular, EHFrame, Merge, Synthetic, Output }; |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 45 | |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 46 | Kind kind() const { return (Kind)SectionKind; } |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 47 | |
| 48 | StringRef Name; |
| 49 | |
| 50 | unsigned SectionKind : 3; |
| 51 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 52 | // The next two bit fields are only used by InputSectionBase, but we |
| 53 | // put them here so the struct packs better. |
| 54 | |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 55 | // The garbage collector sets sections' Live bits. |
| 56 | // If GC is disabled, all sections are considered live by default. |
| 57 | unsigned Live : 1; // for garbage collection |
| 58 | unsigned Assigned : 1; // for linker script |
| 59 | |
| 60 | uint32_t Alignment; |
| 61 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 62 | // These corresponds to the fields in Elf_Shdr. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 63 | uint64_t Flags; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 64 | uint64_t Entsize; |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 65 | uint32_t Type; |
| 66 | uint32_t Link; |
| 67 | uint32_t Info; |
| 68 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 69 | OutputSection *getOutputSection(); |
| 70 | const OutputSection *getOutputSection() const { |
| 71 | return const_cast<SectionBase *>(this)->getOutputSection(); |
| 72 | } |
| 73 | |
| 74 | // Translate an offset in the input section to an offset in the output |
| 75 | // section. |
| 76 | uint64_t getOffset(uint64_t Offset) const; |
| 77 | |
| 78 | uint64_t getOffset(const DefinedRegular &Sym) const; |
| 79 | |
| 80 | protected: |
| 81 | SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags, |
| 82 | uint64_t Entsize, uint64_t Alignment, uint32_t Type, |
| 83 | uint32_t Info, uint32_t Link) |
| 84 | : Name(Name), SectionKind(SectionKind), Alignment(Alignment), |
| 85 | Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) { |
| 86 | Live = false; |
| 87 | Assigned = false; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // This corresponds to a section of an input file. |
| 92 | class InputSectionBase : public SectionBase { |
| 93 | public: |
| 94 | static bool classof(const SectionBase *S); |
| 95 | |
| 96 | // The file this section is from. |
| 97 | InputFile *File; |
| 98 | |
| 99 | ArrayRef<uint8_t> Data; |
Rafael Espindola | 35ae65e | 2017-03-08 15:57:17 +0000 | [diff] [blame] | 100 | uint64_t getOffsetInFile() const; |
Rafael Espindola | bdd2e3e | 2017-03-08 14:12:52 +0000 | [diff] [blame] | 101 | |
Rafael Espindola | 2a80e11 | 2017-03-06 22:36:19 +0000 | [diff] [blame] | 102 | static InputSectionBase Discarded; |
| 103 | |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 104 | InputSectionBase() |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 105 | : SectionBase(Regular, "", /*Flags*/ 0, /*Entsize*/ 0, /*Alignment*/ 0, |
| 106 | /*Type*/ 0, |
| 107 | /*Info*/ 0, /*Link*/ 0), |
| 108 | Repl(this) { |
| 109 | Live = false; |
| 110 | Assigned = false; |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 111 | NumRelocations = 0; |
| 112 | AreRelocsRela = false; |
| 113 | } |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 115 | template <class ELFT> |
| 116 | InputSectionBase(ObjectFile<ELFT> *File, const typename ELFT::Shdr *Header, |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 117 | StringRef Name, Kind SectionKind); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 118 | |
| 119 | InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type, |
| 120 | uint64_t Entsize, uint32_t Link, uint32_t Info, |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 121 | uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name, |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 122 | Kind SectionKind); |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame^] | 123 | |
| 124 | // Input sections are part of an output section. Special sections |
| 125 | // like .eh_frame and merge sections are first combined into a |
| 126 | // synthetic section that is then added to an output section. In all |
| 127 | // cases this points one level up. |
| 128 | SectionBase *Parent = nullptr; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 129 | |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 130 | // Relocations that refer to this section. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 131 | const void *FirstRelocation = nullptr; |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 132 | unsigned NumRelocations : 31; |
| 133 | unsigned AreRelocsRela : 1; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 134 | template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const { |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 135 | assert(!AreRelocsRela); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 136 | return llvm::makeArrayRef( |
| 137 | static_cast<const typename ELFT::Rel *>(FirstRelocation), |
| 138 | NumRelocations); |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 139 | } |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 140 | template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const { |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 141 | assert(AreRelocsRela); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 142 | return llvm::makeArrayRef( |
| 143 | static_cast<const typename ELFT::Rela *>(FirstRelocation), |
| 144 | NumRelocations); |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 147 | // This pointer points to the "real" instance of this instance. |
| 148 | // Usually Repl == this. However, if ICF merges two sections, |
| 149 | // Repl pointer of one section points to another section. So, |
| 150 | // if you need to get a pointer to this instance, do not use |
| 151 | // this but instead this->Repl. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 152 | InputSectionBase *Repl; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 153 | |
George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 154 | // InputSections that are dependent on us (reverse dependency for GC) |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 155 | llvm::TinyPtrVector<InputSectionBase *> DependentSections; |
George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 156 | |
Rafael Espindola | 1a54112 | 2016-11-08 14:47:16 +0000 | [diff] [blame] | 157 | // Returns the size of this section (even if this is a common or BSS.) |
Rafael Espindola | 76b6bd3 | 2017-03-08 15:44:30 +0000 | [diff] [blame] | 158 | size_t getSize() const; |
Rafael Espindola | 1a54112 | 2016-11-08 14:47:16 +0000 | [diff] [blame] | 159 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 160 | template <class ELFT> ObjectFile<ELFT> *getFile() const; |
| 161 | |
| 162 | template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const { |
| 163 | return getFile<ELFT>()->getObj(); |
| 164 | } |
| 165 | |
Rafael Espindola | b47c6e5 | 2017-05-31 19:09:52 +0000 | [diff] [blame] | 166 | InputSection *getLinkOrderDep() const; |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 167 | |
George Rimar | 76e562a | 2017-03-21 09:08:58 +0000 | [diff] [blame] | 168 | void uncompress(); |
George Rimar | 602fbee | 2016-06-24 11:18:44 +0000 | [diff] [blame] | 169 | |
Rui Ueyama | da06bfb | 2016-11-25 18:51:53 +0000 | [diff] [blame] | 170 | // Returns a source location string. Used to construct an error message. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 171 | template <class ELFT> std::string getLocation(uint64_t Offset); |
Rui Ueyama | b876020 | 2017-03-30 19:13:47 +0000 | [diff] [blame] | 172 | template <class ELFT> std::string getSrcMsg(uint64_t Offset); |
| 173 | template <class ELFT> std::string getObjMsg(uint64_t Offset); |
Rui Ueyama | da06bfb | 2016-11-25 18:51:53 +0000 | [diff] [blame] | 174 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 175 | template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd); |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 176 | void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd); |
| 177 | template <class ELFT> void relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd); |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 178 | |
| 179 | std::vector<Relocation> Relocations; |
| 180 | |
| 181 | template <typename T> llvm::ArrayRef<T> getDataAs() const { |
| 182 | size_t S = Data.size(); |
| 183 | assert(S % sizeof(T) == 0); |
| 184 | return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T)); |
| 185 | } |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 188 | // SectionPiece represents a piece of splittable section contents. |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 189 | // We allocate a lot of these and binary search on them. This means that they |
| 190 | // have to be as compact as possible, which is why we don't store the size (can |
| 191 | // be found by looking at the next one) and put the hash in a side table. |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 192 | struct SectionPiece { |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 193 | SectionPiece(size_t Off, bool Live = false) |
| 194 | : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {} |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 195 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 196 | size_t InputOff; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 197 | ssize_t OutputOff : 8 * sizeof(ssize_t) - 1; |
Hans Wennborg | 7314c48 | 2016-10-20 15:59:08 +0000 | [diff] [blame] | 198 | size_t Live : 1; |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 199 | }; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 200 | static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t), |
| 201 | "SectionPiece is too big"); |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 202 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 203 | // This corresponds to a SHF_MERGE section of an input file. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 204 | class MergeInputSection : public InputSectionBase { |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 205 | public: |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 206 | template <class ELFT> |
| 207 | MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header, |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 208 | StringRef Name); |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 209 | static bool classof(const SectionBase *S); |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 210 | void splitIntoPieces(); |
| 211 | |
| 212 | // Mark the piece at a given offset live. Used by GC. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 213 | void markLiveAt(uint64_t Offset) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 214 | assert(this->Flags & llvm::ELF::SHF_ALLOC); |
Rafael Espindola | 116d83f | 2016-10-19 23:13:40 +0000 | [diff] [blame] | 215 | LiveOffsets.insert(Offset); |
| 216 | } |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 217 | |
| 218 | // Translate an offset in the input section to an offset |
| 219 | // in the output section. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 220 | uint64_t getOffset(uint64_t Offset) const; |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 221 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 222 | // Splittable sections are handled as a sequence of data |
| 223 | // rather than a single large blob of data. |
| 224 | std::vector<SectionPiece> Pieces; |
Rui Ueyama | c8e6884 | 2016-12-06 02:19:30 +0000 | [diff] [blame] | 225 | |
| 226 | // Returns I'th piece's data. This function is very hot when |
| 227 | // string merging is enabled, so we want to inline. |
| 228 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 229 | llvm::CachedHashStringRef getData(size_t I) const { |
| 230 | size_t Begin = Pieces[I].InputOff; |
| 231 | size_t End; |
| 232 | if (Pieces.size() - 1 == I) |
| 233 | End = this->Data.size(); |
| 234 | else |
| 235 | End = Pieces[I + 1].InputOff; |
| 236 | |
| 237 | StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin}; |
| 238 | return {S, Hashes[I]}; |
| 239 | } |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 240 | |
| 241 | // Returns the SectionPiece at a given input section offset. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 242 | SectionPiece *getSectionPiece(uint64_t Offset); |
| 243 | const SectionPiece *getSectionPiece(uint64_t Offset) const; |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 244 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame^] | 245 | SyntheticSection *getParent() const; |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 246 | |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 247 | private: |
Rui Ueyama | e8a077b | 2016-11-26 15:15:11 +0000 | [diff] [blame] | 248 | void splitStrings(ArrayRef<uint8_t> A, size_t Size); |
| 249 | void splitNonStrings(ArrayRef<uint8_t> A, size_t Size); |
Rui Ueyama | d6bd137 | 2016-08-03 04:39:42 +0000 | [diff] [blame] | 250 | |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 251 | std::vector<uint32_t> Hashes; |
| 252 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 253 | mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap; |
Kamil Rytarowski | e739e49 | 2017-05-24 18:31:48 +0000 | [diff] [blame] | 254 | mutable llvm::once_flag InitOffsetMap; |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 255 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 256 | llvm::DenseSet<uint64_t> LiveOffsets; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 259 | struct EhSectionPiece : public SectionPiece { |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 260 | EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size, |
Eugene Leviant | 531df4f | 2016-11-23 09:45:17 +0000 | [diff] [blame] | 261 | unsigned FirstRelocation) |
| 262 | : SectionPiece(Off, false), ID(ID), Size(Size), |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 263 | FirstRelocation(FirstRelocation) {} |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 264 | InputSectionBase *ID; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 265 | uint32_t Size; |
| 266 | uint32_t size() const { return Size; } |
| 267 | |
Eugene Leviant | 531df4f | 2016-11-23 09:45:17 +0000 | [diff] [blame] | 268 | ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; } |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 269 | unsigned FirstRelocation; |
| 270 | }; |
| 271 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 272 | // This corresponds to a .eh_frame section of an input file. |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 273 | class EhInputSection : public InputSectionBase { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 274 | public: |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 275 | template <class ELFT> |
| 276 | EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header, |
| 277 | StringRef Name); |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 278 | static bool classof(const SectionBase *S); |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 279 | template <class ELFT> void split(); |
| 280 | template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 281 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 282 | // Splittable sections are handled as a sequence of data |
| 283 | // rather than a single large blob of data. |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 284 | std::vector<EhSectionPiece> Pieces; |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame^] | 285 | |
| 286 | SyntheticSection *getParent() const; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
Rafael Espindola | 798ad9a | 2017-02-24 13:06:59 +0000 | [diff] [blame] | 289 | // This is a section that is added directly to an output section |
| 290 | // instead of needing special combination via a synthetic section. This |
| 291 | // includes all input sections with the exceptions of SHF_MERGE and |
| 292 | // .eh_frame. It also includes the synthetic sections themselves. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 293 | class InputSection : public InputSectionBase { |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 294 | public: |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 295 | InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment, |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 296 | ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular); |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 297 | template <class ELFT> |
| 298 | InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header, |
| 299 | StringRef Name); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 300 | |
| 301 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
| 302 | // beginning of the output section. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 303 | template <class ELFT> void writeTo(uint8_t *Buf); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 304 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame^] | 305 | OutputSection *getParent() const; |
| 306 | |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 307 | // The offset from beginning of the output sections this section was assigned |
| 308 | // to. The writer sets a value. |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 309 | uint64_t OutSecOff = 0; |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 310 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 311 | static bool classof(const SectionBase *S); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 312 | |
George Rimar | 1ec03e4 | 2017-03-21 09:13:27 +0000 | [diff] [blame] | 313 | InputSectionBase *getRelocatedSection(); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 314 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 315 | template <class ELFT, class RelTy> |
Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame] | 316 | void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
| 317 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 318 | // Used by ICF. |
Rui Ueyama | fcd3fa8 | 2016-12-05 18:11:35 +0000 | [diff] [blame] | 319 | uint32_t Class[2] = {0, 0}; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 320 | |
| 321 | // Called by ICF to merge two input sections. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 322 | void replace(InputSection *Other); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 323 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 324 | private: |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 325 | template <class ELFT, class RelTy> |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 326 | void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
George Rimar | 3b189d1 | 2017-05-29 08:37:50 +0000 | [diff] [blame] | 327 | |
| 328 | void copyShtGroup(uint8_t *Buf); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 +0000 | [diff] [blame] | 331 | // The list of all input sections. |
| 332 | extern std::vector<InputSectionBase *> InputSections; |
| 333 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 334 | } // namespace elf |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 +0000 | [diff] [blame] | 335 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 336 | std::string toString(const elf::InputSectionBase *); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 337 | } // namespace lld |
| 338 | |
| 339 | #endif |