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" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 16 | #include "lld/Common/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 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 27 | class Symbol; |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 28 | struct SectionPiece; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 29 | |
Peter Collingbourne | e9a9e0a | 2017-11-06 04:35:31 +0000 | [diff] [blame] | 30 | class Defined; |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 31 | class SyntheticSection; |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 32 | class MergeSyntheticSection; |
Rui Ueyama | 709fb2bb1 | 2017-07-26 22:13:32 +0000 | [diff] [blame] | 33 | template <class ELFT> class ObjFile; |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 34 | class OutputSection; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 36 | // This is the base class of all sections that lld handles. Some are sections in |
| 37 | // input files, some are sections in the produced output file and some exist |
| 38 | // just as a convenience for implementing special ways of combining some |
| 39 | // sections. |
| 40 | class SectionBase { |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 41 | public: |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 42 | enum Kind { Regular, EHFrame, Merge, Synthetic, Output }; |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 43 | |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 44 | Kind kind() const { return (Kind)SectionKind; } |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 45 | |
| 46 | StringRef Name; |
| 47 | |
Rafael Espindola | f4fb5fd | 2017-12-13 22:59:23 +0000 | [diff] [blame] | 48 | // This pointer points to the "real" instance of this instance. |
| 49 | // Usually Repl == this. However, if ICF merges two sections, |
| 50 | // Repl pointer of one section points to another section. So, |
| 51 | // if you need to get a pointer to this instance, do not use |
| 52 | // this but instead this->Repl. |
| 53 | SectionBase *Repl; |
| 54 | |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 55 | unsigned SectionKind : 3; |
| 56 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 57 | // The next two bit fields are only used by InputSectionBase, but we |
| 58 | // put them here so the struct packs better. |
| 59 | |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 60 | // The garbage collector sets sections' Live bits. |
| 61 | // If GC is disabled, all sections are considered live by default. |
Rui Ueyama | e8936bc | 2017-10-08 01:55:29 +0000 | [diff] [blame] | 62 | unsigned Live : 1; |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 63 | |
Peter Collingbourne | 6c55a70 | 2017-11-06 04:33:58 +0000 | [diff] [blame] | 64 | unsigned Bss : 1; |
| 65 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 66 | // These corresponds to the fields in Elf_Shdr. |
Rui Ueyama | e8936bc | 2017-10-08 01:55:29 +0000 | [diff] [blame] | 67 | uint32_t Alignment; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 68 | uint64_t Flags; |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 69 | uint64_t Entsize; |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 70 | uint32_t Type; |
| 71 | uint32_t Link; |
| 72 | uint32_t Info; |
| 73 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 74 | OutputSection *getOutputSection(); |
| 75 | const OutputSection *getOutputSection() const { |
| 76 | return const_cast<SectionBase *>(this)->getOutputSection(); |
| 77 | } |
| 78 | |
| 79 | // Translate an offset in the input section to an offset in the output |
| 80 | // section. |
| 81 | uint64_t getOffset(uint64_t Offset) const; |
| 82 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 83 | protected: |
| 84 | SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags, |
| 85 | uint64_t Entsize, uint64_t Alignment, uint32_t Type, |
| 86 | uint32_t Info, uint32_t Link) |
Rafael Espindola | f4fb5fd | 2017-12-13 22:59:23 +0000 | [diff] [blame] | 87 | : Name(Name), Repl(this), SectionKind(SectionKind), Live(false), |
| 88 | Bss(false), Alignment(Alignment), Flags(Flags), Entsize(Entsize), |
| 89 | Type(Type), Link(Link), Info(Info) {} |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | // This corresponds to a section of an input file. |
| 93 | class InputSectionBase : public SectionBase { |
| 94 | public: |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 95 | template <class ELFT> |
Rafael Espindola | 6040327 | 2017-12-21 02:03:39 +0000 | [diff] [blame] | 96 | InputSectionBase(ObjFile<ELFT> &File, const typename ELFT::Shdr &Header, |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 97 | StringRef Name, Kind SectionKind); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 98 | |
| 99 | InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type, |
| 100 | uint64_t Entsize, uint32_t Link, uint32_t Info, |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 101 | uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name, |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 102 | Kind SectionKind); |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 103 | |
Rui Ueyama | 124bedb | 2017-10-10 03:22:29 +0000 | [diff] [blame] | 104 | static bool classof(const SectionBase *S) { return S->kind() != Output; } |
| 105 | |
| 106 | // The file which contains this section. It's dynamic type is always |
| 107 | // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as |
| 108 | // its static type. |
| 109 | InputFile *File; |
| 110 | |
| 111 | template <class ELFT> ObjFile<ELFT> *getFile() const { |
| 112 | return cast_or_null<ObjFile<ELFT>>(File); |
| 113 | } |
| 114 | |
| 115 | ArrayRef<uint8_t> Data; |
| 116 | uint64_t getOffsetInFile() const; |
| 117 | |
Rui Ueyama | ea1398e | 2017-10-29 23:32:23 +0000 | [diff] [blame] | 118 | // True if this section has already been placed to a linker script |
| 119 | // output section. This is needed because, in a linker script, you |
| 120 | // can refer to the same section more than once. For example, in |
| 121 | // the following linker script, |
| 122 | // |
| 123 | // .foo : { *(.text) } |
| 124 | // .bar : { *(.text) } |
| 125 | // |
| 126 | // .foo takes all .text sections, and .bar becomes empty. To achieve |
| 127 | // this, we need to memorize whether a section has been placed or |
| 128 | // not for each input section. |
| 129 | bool Assigned = false; |
| 130 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 131 | // Input sections are part of an output section. Special sections |
| 132 | // like .eh_frame and merge sections are first combined into a |
| 133 | // synthetic section that is then added to an output section. In all |
| 134 | // cases this points one level up. |
| 135 | SectionBase *Parent = nullptr; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 136 | |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 137 | // Relocations that refer to this section. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 138 | const void *FirstRelocation = nullptr; |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 139 | unsigned NumRelocations : 31; |
| 140 | unsigned AreRelocsRela : 1; |
Rui Ueyama | 124bedb | 2017-10-10 03:22:29 +0000 | [diff] [blame] | 141 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 142 | template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const { |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 143 | assert(!AreRelocsRela); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 144 | return llvm::makeArrayRef( |
| 145 | static_cast<const typename ELFT::Rel *>(FirstRelocation), |
| 146 | NumRelocations); |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 147 | } |
Rui Ueyama | 124bedb | 2017-10-10 03:22:29 +0000 | [diff] [blame] | 148 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 149 | template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const { |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 150 | assert(AreRelocsRela); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 151 | return llvm::makeArrayRef( |
| 152 | static_cast<const typename ELFT::Rela *>(FirstRelocation), |
| 153 | NumRelocations); |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 156 | // InputSections that are dependent on us (reverse dependency for GC) |
Rui Ueyama | 0543343 | 2017-10-11 04:01:13 +0000 | [diff] [blame] | 157 | llvm::TinyPtrVector<InputSection *> DependentSections; |
George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 158 | |
Rafael Espindola | 1a54112 | 2016-11-08 14:47:16 +0000 | [diff] [blame] | 159 | // 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] | 160 | size_t getSize() const; |
Rafael Espindola | 1a54112 | 2016-11-08 14:47:16 +0000 | [diff] [blame] | 161 | |
Rafael Espindola | b47c6e5 | 2017-05-31 19:09:52 +0000 | [diff] [blame] | 162 | InputSection *getLinkOrderDep() const; |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 163 | |
Rui Ueyama | f987fe7 | 2017-10-10 03:40:57 +0000 | [diff] [blame] | 164 | // Compilers emit zlib-compressed debug sections if the -gz option |
| 165 | // is given. This function checks if this section is compressed, and |
| 166 | // if so, decompress in memory. |
Rui Ueyama | ac114d2 | 2018-02-12 21:56:14 +0000 | [diff] [blame] | 167 | void maybeDecompress(); |
George Rimar | 602fbee | 2016-06-24 11:18:44 +0000 | [diff] [blame] | 168 | |
Rui Ueyama | da06bfb | 2016-11-25 18:51:53 +0000 | [diff] [blame] | 169 | // Returns a source location string. Used to construct an error message. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 170 | template <class ELFT> std::string getLocation(uint64_t Offset); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 171 | std::string getSrcMsg(const Symbol &Sym, uint64_t Offset); |
Rui Ueyama | d6b7a39 | 2017-10-27 03:13:54 +0000 | [diff] [blame] | 172 | std::string getObjMsg(uint64_t Offset); |
Rui Ueyama | da06bfb | 2016-11-25 18:51:53 +0000 | [diff] [blame] | 173 | |
Rui Ueyama | f987fe7 | 2017-10-10 03:40:57 +0000 | [diff] [blame] | 174 | // Each section knows how to relocate itself. These functions apply |
| 175 | // relocations, assuming that Buf points to this section's copy in |
| 176 | // the mmap'ed output buffer. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 177 | template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd); |
Rafael Espindola | a6465bb | 2017-05-18 16:45:36 +0000 | [diff] [blame] | 178 | void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd); |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 179 | |
Rui Ueyama | f987fe7 | 2017-10-10 03:40:57 +0000 | [diff] [blame] | 180 | // The native ELF reloc data type is not very convenient to handle. |
| 181 | // So we convert ELF reloc records to our own records in Relocations.cpp. |
| 182 | // This vector contains such "cooked" relocations. |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 183 | std::vector<Relocation> Relocations; |
| 184 | |
| 185 | template <typename T> llvm::ArrayRef<T> getDataAs() const { |
| 186 | size_t S = Data.size(); |
| 187 | assert(S % sizeof(T) == 0); |
| 188 | return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T)); |
| 189 | } |
Rui Ueyama | 314a005 | 2017-08-17 00:27:55 +0000 | [diff] [blame] | 190 | |
| 191 | private: |
Rui Ueyama | ac114d2 | 2018-02-12 21:56:14 +0000 | [diff] [blame] | 192 | // A pointer that owns decompressed data if a section is compressed by zlib. |
Rui Ueyama | 314a005 | 2017-08-17 00:27:55 +0000 | [diff] [blame] | 193 | // Since the feature is not used often, this is usually a nullptr. |
Rui Ueyama | ac114d2 | 2018-02-12 21:56:14 +0000 | [diff] [blame] | 194 | std::unique_ptr<char[]> DecompressBuf; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 197 | // SectionPiece represents a piece of splittable section contents. |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 198 | // We allocate a lot of these and binary search on them. This means that they |
| 199 | // have to be as compact as possible, which is why we don't store the size (can |
Rui Ueyama | d5135b7 | 2017-10-24 21:44:43 +0000 | [diff] [blame] | 200 | // be found by looking at the next one). |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 201 | struct SectionPiece { |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 202 | SectionPiece(size_t Off, uint32_t Hash, bool Live) |
| 203 | : InputOff(Off), Hash(Hash), OutputOff(-1), |
| 204 | Live(Live || !Config->GcSections) {} |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 205 | |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 206 | uint32_t InputOff; |
| 207 | uint32_t Hash; |
Konstantin Zhuravlyov | 70abb6e | 2017-10-23 19:31:31 +0000 | [diff] [blame] | 208 | int64_t OutputOff : 63; |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 209 | uint64_t Live : 1; |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 210 | }; |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 211 | |
| 212 | static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big"); |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 214 | // This corresponds to a SHF_MERGE section of an input file. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 215 | class MergeInputSection : public InputSectionBase { |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 216 | public: |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 217 | template <class ELFT> |
Rafael Espindola | 6040327 | 2017-12-21 02:03:39 +0000 | [diff] [blame] | 218 | MergeInputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header, |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 219 | StringRef Name); |
Rafael Espindola | 5c73c49 | 2017-12-21 01:21:59 +0000 | [diff] [blame] | 220 | MergeInputSection(uint64_t Flags, uint32_t Type, uint64_t Entsize, |
| 221 | ArrayRef<uint8_t> Data, StringRef Name); |
| 222 | |
Rui Ueyama | 43ca716 | 2017-10-01 23:46:31 +0000 | [diff] [blame] | 223 | static bool classof(const SectionBase *S) { return S->kind() == Merge; } |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 224 | void splitIntoPieces(); |
| 225 | |
| 226 | // Mark the piece at a given offset live. Used by GC. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 227 | void markLiveAt(uint64_t Offset) { |
George Rimar | f4ca4a6 | 2017-10-24 08:26:32 +0000 | [diff] [blame] | 228 | if (this->Flags & llvm::ELF::SHF_ALLOC) |
| 229 | LiveOffsets.insert(Offset); |
Rafael Espindola | 116d83f | 2016-10-19 23:13:40 +0000 | [diff] [blame] | 230 | } |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 231 | |
| 232 | // Translate an offset in the input section to an offset |
| 233 | // in the output section. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 234 | uint64_t getOffset(uint64_t Offset) const; |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 235 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 236 | // Splittable sections are handled as a sequence of data |
| 237 | // rather than a single large blob of data. |
| 238 | std::vector<SectionPiece> Pieces; |
Rui Ueyama | c8e6884 | 2016-12-06 02:19:30 +0000 | [diff] [blame] | 239 | |
| 240 | // Returns I'th piece's data. This function is very hot when |
| 241 | // string merging is enabled, so we want to inline. |
| 242 | LLVM_ATTRIBUTE_ALWAYS_INLINE |
| 243 | llvm::CachedHashStringRef getData(size_t I) const { |
| 244 | size_t Begin = Pieces[I].InputOff; |
Rui Ueyama | 95bf509 | 2017-10-21 23:20:13 +0000 | [diff] [blame] | 245 | size_t End = |
| 246 | (Pieces.size() - 1 == I) ? Data.size() : Pieces[I + 1].InputOff; |
| 247 | return {toStringRef(Data.slice(Begin, End - Begin)), Pieces[I].Hash}; |
Rui Ueyama | c8e6884 | 2016-12-06 02:19:30 +0000 | [diff] [blame] | 248 | } |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 249 | |
| 250 | // Returns the SectionPiece at a given input section offset. |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 251 | SectionPiece *getSectionPiece(uint64_t Offset); |
| 252 | const SectionPiece *getSectionPiece(uint64_t Offset) const; |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 253 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 254 | SyntheticSection *getParent() const; |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 255 | |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 256 | private: |
Rui Ueyama | e8a077b | 2016-11-26 15:15:11 +0000 | [diff] [blame] | 257 | void splitStrings(ArrayRef<uint8_t> A, size_t Size); |
| 258 | void splitNonStrings(ArrayRef<uint8_t> A, size_t Size); |
Rui Ueyama | d6bd137 | 2016-08-03 04:39:42 +0000 | [diff] [blame] | 259 | |
Rui Ueyama | 95c142e | 2017-10-31 19:14:06 +0000 | [diff] [blame] | 260 | mutable llvm::DenseMap<uint32_t, uint32_t> OffsetMap; |
| 261 | mutable llvm::once_flag InitOffsetMap; |
| 262 | |
Rafael Espindola | 6119b86 | 2017-03-06 20:23:56 +0000 | [diff] [blame] | 263 | llvm::DenseSet<uint64_t> LiveOffsets; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
Rui Ueyama | e084aac | 2017-09-18 23:07:21 +0000 | [diff] [blame] | 266 | struct EhSectionPiece { |
NAKAMURA Takumi | 169dbde | 2017-09-20 08:03:18 +0000 | [diff] [blame] | 267 | EhSectionPiece(size_t Off, InputSectionBase *Sec, uint32_t Size, |
| 268 | unsigned FirstRelocation) |
| 269 | : InputOff(Off), Sec(Sec), Size(Size), FirstRelocation(FirstRelocation) {} |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 270 | |
NAKAMURA Takumi | 169dbde | 2017-09-20 08:03:18 +0000 | [diff] [blame] | 271 | ArrayRef<uint8_t> data() { return {Sec->Data.data() + this->InputOff, Size}; } |
| 272 | |
| 273 | size_t InputOff; |
| 274 | ssize_t OutputOff = -1; |
| 275 | InputSectionBase *Sec; |
Rui Ueyama | e084aac | 2017-09-18 23:07:21 +0000 | [diff] [blame] | 276 | uint32_t Size; |
NAKAMURA Takumi | 169dbde | 2017-09-20 08:03:18 +0000 | [diff] [blame] | 277 | unsigned FirstRelocation; |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 280 | // This corresponds to a .eh_frame section of an input file. |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 281 | class EhInputSection : public InputSectionBase { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 282 | public: |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 283 | template <class ELFT> |
Rafael Espindola | 6040327 | 2017-12-21 02:03:39 +0000 | [diff] [blame] | 284 | EhInputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header, |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 285 | StringRef Name); |
Rui Ueyama | 43ca716 | 2017-10-01 23:46:31 +0000 | [diff] [blame] | 286 | static bool classof(const SectionBase *S) { return S->kind() == EHFrame; } |
Rafael Espindola | 5c02b74 | 2017-03-06 21:17:18 +0000 | [diff] [blame] | 287 | template <class ELFT> void split(); |
| 288 | template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 289 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 290 | // Splittable sections are handled as a sequence of data |
| 291 | // rather than a single large blob of data. |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 292 | std::vector<EhSectionPiece> Pieces; |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 293 | |
| 294 | SyntheticSection *getParent() const; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
Rafael Espindola | 798ad9a | 2017-02-24 13:06:59 +0000 | [diff] [blame] | 297 | // This is a section that is added directly to an output section |
| 298 | // instead of needing special combination via a synthetic section. This |
| 299 | // includes all input sections with the exceptions of SHF_MERGE and |
| 300 | // .eh_frame. It also includes the synthetic sections themselves. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 301 | class InputSection : public InputSectionBase { |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 302 | public: |
Rafael Espindola | ce3b52c | 2017-12-21 02:11:51 +0000 | [diff] [blame] | 303 | InputSection(InputFile *F, uint64_t Flags, uint32_t Type, uint32_t Alignment, |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 304 | ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular); |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 305 | template <class ELFT> |
Rafael Espindola | 6040327 | 2017-12-21 02:03:39 +0000 | [diff] [blame] | 306 | InputSection(ObjFile<ELFT> &F, const typename ELFT::Shdr &Header, |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 307 | StringRef Name); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 308 | |
| 309 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
| 310 | // beginning of the output section. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 311 | template <class ELFT> void writeTo(uint8_t *Buf); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 312 | |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 313 | OutputSection *getParent() const; |
| 314 | |
James Henderson | 8d0efdd | 2017-12-12 11:51:13 +0000 | [diff] [blame] | 315 | // This variable has two usages. Initially, it represents an index in the |
| 316 | // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER |
| 317 | // sections. After assignAddresses is called, it represents the offset from |
| 318 | // the beginning of the output section this section was assigned to. |
Rafael Espindola | 10bcc1c | 2017-12-12 17:37:01 +0000 | [diff] [blame] | 319 | uint64_t OutSecOff = 0; |
| 320 | |
| 321 | static bool classof(const SectionBase *S); |
| 322 | |
| 323 | InputSectionBase *getRelocatedSection(); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 324 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 325 | template <class ELFT, class RelTy> |
Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame] | 326 | void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
| 327 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 328 | // Used by ICF. |
Rui Ueyama | fcd3fa8 | 2016-12-05 18:11:35 +0000 | [diff] [blame] | 329 | uint32_t Class[2] = {0, 0}; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 330 | |
| 331 | // Called by ICF to merge two input sections. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 332 | void replace(InputSection *Other); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 333 | |
Rafael Espindola | b01cd86 | 2017-12-13 01:39:35 +0000 | [diff] [blame] | 334 | static InputSection Discarded; |
| 335 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 336 | private: |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 337 | template <class ELFT, class RelTy> |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 338 | void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
George Rimar | 3b189d1 | 2017-05-29 08:37:50 +0000 | [diff] [blame] | 339 | |
Rui Ueyama | f08b38c | 2017-06-09 03:19:08 +0000 | [diff] [blame] | 340 | template <class ELFT> void copyShtGroup(uint8_t *Buf); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
Rui Ueyama | 536a267 | 2017-02-27 02:32:08 +0000 | [diff] [blame] | 343 | // The list of all input sections. |
| 344 | extern std::vector<InputSectionBase *> InputSections; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 345 | } // namespace elf |
Rui Ueyama | ce03926 | 2017-01-06 10:04:08 +0000 | [diff] [blame] | 346 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 347 | std::string toString(const elf::InputSectionBase *); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 348 | } // namespace lld |
| 349 | |
| 350 | #endif |