Rafael Espindola | beee25e | 2015-08-14 14:12:54 | [diff] [blame] | 1 | //===- Chunks.h -------------------------------------------------*- C++ -*-===// |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLD_COFF_CHUNKS_H |
| 10 | #define LLD_COFF_CHUNKS_H |
| 11 | |
Rui Ueyama | 4dbff20 | 2015-09-16 21:40:47 | [diff] [blame] | 12 | #include "Config.h" |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 13 | #include "InputFiles.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 14 | #include "lld/Common/LLVM.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 16 | #include "llvm/ADT/PointerIntPair.h" |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 17 | #include "llvm/ADT/iterator.h" |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 | [diff] [blame] | 18 | #include "llvm/ADT/iterator_range.h" |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 19 | #include "llvm/MC/StringTableBuilder.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 20 | #include "llvm/Object/COFF.h" |
Jacek Caban | 8f9903d | 2024-04-04 12:41:50 | [diff] [blame] | 21 | #include "llvm/Object/WindowsMachineFlag.h" |
Benjamin Kramer | bd52120 | 2016-06-03 16:57:13 | [diff] [blame] | 22 | #include <utility> |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Nico Weber | 7c26641 | 2022-08-08 15:32:26 | [diff] [blame] | 25 | namespace lld::coff { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 26 | |
| 27 | using llvm::COFF::ImportDirectoryTableEntry; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 28 | using llvm::object::chpe_range_type; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 29 | using llvm::object::coff_relocation; |
| 30 | using llvm::object::coff_section; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 31 | using llvm::object::COFFSymbolRef; |
| 32 | using llvm::object::SectionRef; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 33 | |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 | [diff] [blame] | 34 | class Baserel; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 35 | class Defined; |
| 36 | class DefinedImportData; |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 | [diff] [blame] | 37 | class DefinedRegular; |
Rui Ueyama | e1b48e0 | 2017-07-26 23:05:24 | [diff] [blame] | 38 | class ObjFile; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 39 | class OutputSection; |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 40 | class RuntimePseudoReloc; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 41 | class Symbol; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 42 | |
Peter Collingbourne | 71c7de5 | 2018-04-20 21:23:16 | [diff] [blame] | 43 | // Mask for permissions (discardable, writable, readable, executable, etc). |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 44 | const uint32_t permMask = 0xFE000000; |
Peter Collingbourne | 71c7de5 | 2018-04-20 21:23:16 | [diff] [blame] | 45 | |
| 46 | // Mask for section types (code, data, bss). |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 47 | const uint32_t typeMask = 0x000000E0; |
Rui Ueyama | 67fcd1a0 | 2015-08-05 19:51:28 | [diff] [blame] | 48 | |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 49 | // The log base 2 of the largest section alignment, which is log2(8192), or 13. |
| 50 | enum : unsigned { Log2MaxSectionAlignment = 13 }; |
| 51 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 52 | // A Chunk represents a chunk of data that will occupy space in the |
| 53 | // output (if the resolver chose that). It may or may not be backed by |
| 54 | // a section of an input file. It could be linker-created data, or |
| 55 | // doesn't even have actual data (if common or bss). |
| 56 | class Chunk { |
| 57 | public: |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 58 | enum Kind : uint8_t { |
| 59 | SectionKind, |
| 60 | SectionECKind, |
| 61 | OtherKind, |
Jacek Caban | c53e527 | 2025-03-07 17:34:56 | [diff] [blame] | 62 | ImportThunkKind, |
| 63 | ECExportThunkKind |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 64 | }; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 65 | Kind kind() const { return chunkKind; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 66 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 67 | // Returns the size of this chunk (even if this is a common or BSS.) |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 68 | size_t getSize() const; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 69 | |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 70 | // Returns chunk alignment in power of two form. Value values are powers of |
| 71 | // two from 1 to 8192. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 72 | uint32_t getAlignment() const { return 1U << p2Align; } |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 73 | |
| 74 | // Update the chunk section alignment measured in bytes. Internally alignment |
| 75 | // is stored in log2. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 76 | void setAlignment(uint32_t align) { |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 77 | // Treat zero byte alignment as 1 byte alignment. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 78 | align = align ? align : 1; |
| 79 | assert(llvm::isPowerOf2_32(align) && "alignment is not a power of 2"); |
| 80 | p2Align = llvm::Log2_32(align); |
| 81 | assert(p2Align <= Log2MaxSectionAlignment && |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 82 | "impossible requested alignment"); |
| 83 | } |
| 84 | |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 | [diff] [blame] | 85 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
| 86 | // beginning of the file. Because this function may use RVA values |
| 87 | // of other chunks for relocations, you need to set them properly |
| 88 | // before calling this function. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 89 | void writeTo(uint8_t *buf) const; |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 | [diff] [blame] | 90 | |
Reid Kleckner | 34e9c41 | 2019-05-07 20:30:41 | [diff] [blame] | 91 | // The writer sets and uses the addresses. In practice, PE images cannot be |
| 92 | // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs |
| 93 | // can be stored with 32 bits. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 94 | uint32_t getRVA() const { return rva; } |
| 95 | void setRVA(uint64_t v) { |
Reid Kleckner | ee23f8b | 2021-05-14 02:32:49 | [diff] [blame] | 96 | // This may truncate. The writer checks for overflow later. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 97 | rva = (uint32_t)v; |
Reid Kleckner | 34e9c41 | 2019-05-07 20:30:41 | [diff] [blame] | 98 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 99 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 100 | // Returns readable/writable/executable bits. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 101 | uint32_t getOutputCharacteristics() const; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 102 | |
| 103 | // Returns the section name if this is a section chunk. |
| 104 | // It is illegal to call this function on non-section chunks. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 105 | StringRef getSectionName() const; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 106 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 107 | // An output section has pointers to chunks in the section, and each |
| 108 | // chunk has a back pointer to an output section. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 109 | void setOutputSectionIdx(uint16_t o) { osidx = o; } |
| 110 | uint16_t getOutputSectionIdx() const { return osidx; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 111 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 112 | // Windows-specific. |
| 113 | // Collect all locations that contain absolute addresses for base relocations. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 114 | void getBaserels(std::vector<Baserel> *res); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 115 | |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 | [diff] [blame] | 116 | // Returns a human-readable name of this chunk. Chunks are unnamed chunks of |
| 117 | // bytes, so this is used only for logging or debugging. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 118 | StringRef getDebugName() const; |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 | [diff] [blame] | 119 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 120 | // Return true if this file has the hotpatch flag set to true in the |
| 121 | // S_COMPILE3 record in codeview debug info. Also returns true for some thunks |
| 122 | // synthesized by the linker. |
| 123 | bool isHotPatchable() const; |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 124 | |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 125 | MachineTypes getMachine() const; |
Jacek Caban | 8f9903d | 2024-04-04 12:41:50 | [diff] [blame] | 126 | llvm::Triple::ArchType getArch() const; |
Jacek Caban | c605431 | 2023-11-01 15:47:43 | [diff] [blame] | 127 | std::optional<chpe_range_type> getArm64ECRangeType() const; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 128 | |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 129 | // ARM64EC entry thunk associated with the chunk. |
| 130 | Defined *getEntryThunk() const; |
| 131 | void setEntryThunk(Defined *entryThunk); |
| 132 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 133 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 134 | Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {} |
Reid Kleckner | 14f4ff6 | 2019-05-23 20:26:41 | [diff] [blame] | 135 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 136 | const Kind chunkKind; |
Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 | [diff] [blame] | 137 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 | [diff] [blame] | 138 | public: |
| 139 | // Returns true if this has non-zero data. BSS chunks return |
| 140 | // false. If false is returned, the space occupied by this chunk |
| 141 | // will be filled with zeros. Corresponds to the |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 142 | // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 143 | uint8_t hasData : 1; |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 144 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 | [diff] [blame] | 145 | public: |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 146 | // The alignment of this chunk, stored in log2 form. The writer uses the |
| 147 | // value. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 148 | uint8_t p2Align : 7; |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 149 | |
Reid Kleckner | 56bee1a | 2019-05-24 18:25:49 | [diff] [blame] | 150 | // The output section index for this chunk. The first valid section number is |
| 151 | // one. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 152 | uint16_t osidx = 0; |
Reid Kleckner | 56bee1a | 2019-05-24 18:25:49 | [diff] [blame] | 153 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 154 | // The RVA of this chunk in the output. The writer sets a value. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 155 | uint32_t rva = 0; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 156 | }; |
| 157 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 158 | class NonSectionChunk : public Chunk { |
| 159 | public: |
| 160 | virtual ~NonSectionChunk() = default; |
| 161 | |
| 162 | // Returns the size of this chunk (even if this is a common or BSS.) |
| 163 | virtual size_t getSize() const = 0; |
| 164 | |
| 165 | virtual uint32_t getOutputCharacteristics() const { return 0; } |
| 166 | |
| 167 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
| 168 | // beginning of the file. Because this function may use RVA values |
| 169 | // of other chunks for relocations, you need to set them properly |
| 170 | // before calling this function. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 171 | virtual void writeTo(uint8_t *buf) const {} |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 172 | |
| 173 | // Returns the section name if this is a section chunk. |
| 174 | // It is illegal to call this function on non-section chunks. |
| 175 | virtual StringRef getSectionName() const { |
| 176 | llvm_unreachable("unimplemented getSectionName"); |
| 177 | } |
| 178 | |
| 179 | // Windows-specific. |
| 180 | // Collect all locations that contain absolute addresses for base relocations. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 181 | virtual void getBaserels(std::vector<Baserel> *res) {} |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 182 | |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 183 | virtual MachineTypes getMachine() const { return IMAGE_FILE_MACHINE_UNKNOWN; } |
| 184 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 185 | // Returns a human-readable name of this chunk. Chunks are unnamed chunks of |
| 186 | // bytes, so this is used only for logging or debugging. |
| 187 | virtual StringRef getDebugName() const { return ""; } |
| 188 | |
Jacek Caban | f661e695 | 2024-09-26 08:44:40 | [diff] [blame] | 189 | // Verify that chunk relocations are within their ranges. |
| 190 | virtual bool verifyRanges() { return true; }; |
| 191 | |
| 192 | // If needed, extend the chunk to ensure all relocations are within the |
| 193 | // allowed ranges. Return the additional space required for the extension. |
| 194 | virtual uint32_t extendRanges() { return 0; }; |
| 195 | |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 196 | static bool classof(const Chunk *c) { return c->kind() >= OtherKind; } |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 197 | |
| 198 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 199 | NonSectionChunk(Kind k = OtherKind) : Chunk(k) {} |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 200 | }; |
| 201 | |
Jacek Caban | 14043d4 | 2023-11-01 12:27:29 | [diff] [blame] | 202 | class NonSectionCodeChunk : public NonSectionChunk { |
| 203 | public: |
| 204 | virtual uint32_t getOutputCharacteristics() const override { |
| 205 | return llvm::COFF::IMAGE_SCN_MEM_READ | llvm::COFF::IMAGE_SCN_MEM_EXECUTE; |
| 206 | } |
| 207 | |
| 208 | protected: |
| 209 | NonSectionCodeChunk(Kind k = OtherKind) : NonSectionChunk(k) {} |
| 210 | }; |
| 211 | |
Alexander Kornienko | d5e4a5a | 2022-09-30 11:04:07 | [diff] [blame] | 212 | // MinGW specific; information about one individual location in the image |
| 213 | // that needs to be fixed up at runtime after loading. This represents |
| 214 | // one individual element in the PseudoRelocTableChunk table. |
| 215 | class RuntimePseudoReloc { |
| 216 | public: |
| 217 | RuntimePseudoReloc(Defined *sym, SectionChunk *target, uint32_t targetOffset, |
| 218 | int flags) |
| 219 | : sym(sym), target(target), targetOffset(targetOffset), flags(flags) {} |
| 220 | |
| 221 | Defined *sym; |
| 222 | SectionChunk *target; |
| 223 | uint32_t targetOffset; |
| 224 | // The Flags field contains the size of the relocation, in bits. No other |
| 225 | // flags are currently defined. |
| 226 | int flags; |
| 227 | }; |
| 228 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 229 | // A chunk corresponding a section of an input file. |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 230 | class SectionChunk : public Chunk { |
Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 | [diff] [blame] | 231 | // Identical COMDAT Folding feature accesses section internal data. |
| 232 | friend class ICF; |
| 233 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 234 | public: |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 235 | class symbol_iterator : public llvm::iterator_adaptor_base< |
| 236 | symbol_iterator, const coff_relocation *, |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 237 | std::random_access_iterator_tag, Symbol *> { |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 238 | friend SectionChunk; |
| 239 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 240 | ObjFile *file; |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 241 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 242 | symbol_iterator(ObjFile *file, const coff_relocation *i) |
| 243 | : symbol_iterator::iterator_adaptor_base(i), file(file) {} |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 244 | |
| 245 | public: |
| 246 | symbol_iterator() = default; |
| 247 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 248 | Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); } |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 249 | }; |
| 250 | |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 251 | SectionChunk(ObjFile *file, const coff_section *header, Kind k = SectionKind); |
| 252 | static bool classof(const Chunk *c) { return c->kind() <= SectionECKind; } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 253 | size_t getSize() const { return header->SizeOfRawData; } |
David Majnemer | 22dff0a | 2016-03-15 09:48:27 | [diff] [blame] | 254 | ArrayRef<uint8_t> getContents() const; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 255 | void writeTo(uint8_t *buf) const; |
Jacek Caban | f8f01b5 | 2025-04-11 14:13:46 | [diff] [blame] | 256 | MachineTypes getMachine() const; |
Jacek Caban | 47401b6 | 2023-10-16 18:50:15 | [diff] [blame] | 257 | |
Reid Kleckner | b69db4a | 2021-03-10 22:51:52 | [diff] [blame] | 258 | // Defend against unsorted relocations. This may be overly conservative. |
| 259 | void sortRelocations(); |
| 260 | |
| 261 | // Write and relocate a portion of the section. This is intended to be called |
| 262 | // in a loop. Relocations must be sorted first. |
| 263 | void writeAndRelocateSubsection(ArrayRef<uint8_t> sec, |
| 264 | ArrayRef<uint8_t> subsec, |
| 265 | uint32_t &nextRelocIndex, uint8_t *buf) const; |
| 266 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 267 | uint32_t getOutputCharacteristics() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 268 | return header->Characteristics & (permMask | typeMask); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 269 | } |
| 270 | StringRef getSectionName() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 271 | return StringRef(sectionNameData, sectionNameSize); |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 | [diff] [blame] | 272 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 273 | void getBaserels(std::vector<Baserel> *res); |
Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 | [diff] [blame] | 274 | bool isCOMDAT() const; |
Reid Kleckner | b69db4a | 2021-03-10 22:51:52 | [diff] [blame] | 275 | void applyRelocation(uint8_t *off, const coff_relocation &rel) const; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 276 | void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 277 | uint64_t p, uint64_t imageBase) const; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 278 | void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 279 | uint64_t p, uint64_t imageBase) const; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 280 | void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 281 | uint64_t p, uint64_t imageBase) const; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 282 | void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 283 | uint64_t p, uint64_t imageBase) const; |
Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 | [diff] [blame] | 284 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 285 | void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 286 | |
Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 | [diff] [blame] | 287 | // Called if the garbage collector decides to not include this chunk |
| 288 | // in a final output. It's supposed to print out a log message to stdout. |
| 289 | void printDiscardedMessage() const; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 290 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 291 | // Adds COMDAT associative sections to this COMDAT section. A chunk |
| 292 | // and its children are treated as a group by the garbage collector. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 293 | void addAssociative(SectionChunk *child); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 294 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 295 | StringRef getDebugName() const; |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 | [diff] [blame] | 296 | |
Reid Kleckner | f5bb738 | 2017-06-20 17:14:09 | [diff] [blame] | 297 | // True if this is a codeview debug info chunk. These will not be laid out in |
| 298 | // the image. Instead they will end up in the PDB, if one is requested. |
| 299 | bool isCodeView() const { |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 300 | return getSectionName() == ".debug" || getSectionName().starts_with(".debug$"); |
Reid Kleckner | f5bb738 | 2017-06-20 17:14:09 | [diff] [blame] | 301 | } |
| 302 | |
Martin Storsjo | 67dd341 | 2017-10-10 06:05:29 | [diff] [blame] | 303 | // True if this is a DWARF debug info or exception handling chunk. |
| 304 | bool isDWARF() const { |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 305 | return getSectionName().starts_with(".debug_") || getSectionName() == ".eh_frame"; |
Martin Storsjo | 67dd341 | 2017-10-10 06:05:29 | [diff] [blame] | 306 | } |
Shoaib Meenai | 9a61a79 | 2017-07-18 15:11:05 | [diff] [blame] | 307 | |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 308 | // Allow iteration over the bodies of this chunk's relocated symbols. |
| 309 | llvm::iterator_range<symbol_iterator> symbols() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 310 | return llvm::make_range(symbol_iterator(file, relocsData), |
| 311 | symbol_iterator(file, relocsData + relocsSize)); |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | ArrayRef<coff_relocation> getRelocs() const { |
serge-sans-paille | 984b800 | 2023-01-09 17:11:07 | [diff] [blame] | 315 | return llvm::ArrayRef(relocsData, relocsSize); |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Reloc setter used by ARM range extension thunk insertion. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 319 | void setRelocs(ArrayRef<coff_relocation> newRelocs) { |
| 320 | relocsData = newRelocs.data(); |
| 321 | relocsSize = newRelocs.size(); |
| 322 | assert(relocsSize == newRelocs.size() && "reloc size truncation"); |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 323 | } |
| 324 | |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 325 | // Single linked list iterator for associated comdat children. |
| 326 | class AssociatedIterator |
| 327 | : public llvm::iterator_facade_base< |
| 328 | AssociatedIterator, std::forward_iterator_tag, SectionChunk> { |
| 329 | public: |
| 330 | AssociatedIterator() = default; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 331 | AssociatedIterator(SectionChunk *head) : cur(head) {} |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 332 | bool operator==(const AssociatedIterator &r) const { return cur == r.cur; } |
Reid Kleckner | fce5457 | 2020-05-02 20:28:56 | [diff] [blame] | 333 | // FIXME: Wrong const-ness, but it makes filter ranges work. |
| 334 | SectionChunk &operator*() const { return *cur; } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 335 | SectionChunk &operator*() { return *cur; } |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 336 | AssociatedIterator &operator++() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 337 | cur = cur->assocChildren; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 338 | return *this; |
| 339 | } |
| 340 | |
| 341 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 342 | SectionChunk *cur = nullptr; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 343 | }; |
| 344 | |
Chandler Carruth | 59013c3 | 2015-06-29 21:12:49 | [diff] [blame] | 345 | // Allow iteration over the associated child chunks for this section. |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 346 | llvm::iterator_range<AssociatedIterator> children() const { |
Reid Kleckner | 18a9b18 | 2021-04-14 17:39:48 | [diff] [blame] | 347 | // Associated sections do not have children. The assocChildren field is |
| 348 | // part of the parent's list of children. |
| 349 | bool isAssoc = selection == llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
| 350 | return llvm::make_range( |
| 351 | AssociatedIterator(isAssoc ? nullptr : assocChildren), |
| 352 | AssociatedIterator(nullptr)); |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 353 | } |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 | [diff] [blame] | 354 | |
Alexandre Ganea | 149de8d | 2018-10-05 12:56:46 | [diff] [blame] | 355 | // The section ID this chunk belongs to in its Obj. |
| 356 | uint32_t getSectionNumber() const; |
| 357 | |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 358 | ArrayRef<uint8_t> consumeDebugMagic(); |
| 359 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 360 | static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data, |
| 361 | StringRef sectionName); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 362 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 363 | static SectionChunk *findByName(ArrayRef<SectionChunk *> sections, |
| 364 | StringRef name); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 | [diff] [blame] | 365 | |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 366 | // The file that this chunk was created from. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 367 | ObjFile *file; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 368 | |
| 369 | // Pointer to the COFF section header in the input file. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 370 | const coff_section *header; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 371 | |
| 372 | // The COMDAT leader symbol if this is a COMDAT chunk. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 373 | DefinedRegular *sym = nullptr; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 374 | |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 375 | // The CRC of the contents as described in the COFF spec 4.5.5. |
| 376 | // Auxiliary Format 5: Section Definitions. Used for ICF. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 377 | uint32_t checksum = 0; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 378 | |
| 379 | // Used by the garbage collector. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 380 | bool live; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 381 | |
Reid Kleckner | 14f4ff6 | 2019-05-23 20:26:41 | [diff] [blame] | 382 | // Whether this section needs to be kept distinct from other sections during |
| 383 | // ICF. This is set by the driver using address-significance tables. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 384 | bool keepUnique = false; |
Reid Kleckner | 14f4ff6 | 2019-05-23 20:26:41 | [diff] [blame] | 385 | |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 386 | // The COMDAT selection if this is a COMDAT chunk. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 387 | llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0; |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 | [diff] [blame] | 388 | |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 | [diff] [blame] | 389 | // A pointer pointing to a replacement for this chunk. |
| 390 | // Initially it points to "this" object. If this chunk is merged |
| 391 | // with other chunk by ICF, it points to another chunk, |
Alexandre Ganea | 149de8d | 2018-10-05 12:56:46 | [diff] [blame] | 392 | // and this chunk is considered as dead. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 393 | SectionChunk *repl; |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 | [diff] [blame] | 394 | |
Peter Collingbourne | 6f24fdb | 2017-01-14 03:14:46 | [diff] [blame] | 395 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 396 | SectionChunk *assocChildren = nullptr; |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 | [diff] [blame] | 397 | |
Rui Ueyama | 92298d5 | 2015-09-16 14:19:10 | [diff] [blame] | 398 | // Used for ICF (Identical COMDAT Folding) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 399 | void replace(SectionChunk *other); |
| 400 | uint32_t eqClass[2] = {0, 0}; |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 | [diff] [blame] | 401 | |
| 402 | // Relocations for this section. Size is stored below. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 403 | const coff_relocation *relocsData; |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 | [diff] [blame] | 404 | |
| 405 | // Section name string. Size is stored below. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 406 | const char *sectionNameData; |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 | [diff] [blame] | 407 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 408 | uint32_t relocsSize = 0; |
| 409 | uint32_t sectionNameSize = 0; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 410 | }; |
| 411 | |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 412 | // A section chunk corresponding a section of an EC input file. |
| 413 | class SectionChunkEC final : public SectionChunk { |
| 414 | public: |
| 415 | static bool classof(const Chunk *c) { return c->kind() == SectionECKind; } |
| 416 | |
| 417 | SectionChunkEC(ObjFile *file, const coff_section *header) |
| 418 | : SectionChunk(file, header, SectionECKind) {} |
| 419 | Defined *entryThunk = nullptr; |
| 420 | }; |
| 421 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 422 | // Inline methods to implement faux-virtual dispatch for SectionChunk. |
| 423 | |
| 424 | inline size_t Chunk::getSize() const { |
| 425 | if (isa<SectionChunk>(this)) |
| 426 | return static_cast<const SectionChunk *>(this)->getSize(); |
Jacek Caban | e24ac11 | 2023-10-20 10:38:12 | [diff] [blame] | 427 | return static_cast<const NonSectionChunk *>(this)->getSize(); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | inline uint32_t Chunk::getOutputCharacteristics() const { |
| 431 | if (isa<SectionChunk>(this)) |
| 432 | return static_cast<const SectionChunk *>(this)->getOutputCharacteristics(); |
Jacek Caban | e24ac11 | 2023-10-20 10:38:12 | [diff] [blame] | 433 | return static_cast<const NonSectionChunk *>(this)->getOutputCharacteristics(); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 434 | } |
| 435 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 436 | inline void Chunk::writeTo(uint8_t *buf) const { |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 437 | if (isa<SectionChunk>(this)) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 438 | static_cast<const SectionChunk *>(this)->writeTo(buf); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 439 | else |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 440 | static_cast<const NonSectionChunk *>(this)->writeTo(buf); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 441 | } |
| 442 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 443 | inline StringRef Chunk::getSectionName() const { |
| 444 | if (isa<SectionChunk>(this)) |
| 445 | return static_cast<const SectionChunk *>(this)->getSectionName(); |
Jacek Caban | e24ac11 | 2023-10-20 10:38:12 | [diff] [blame] | 446 | return static_cast<const NonSectionChunk *>(this)->getSectionName(); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 447 | } |
| 448 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 449 | inline void Chunk::getBaserels(std::vector<Baserel> *res) { |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 450 | if (isa<SectionChunk>(this)) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 451 | static_cast<SectionChunk *>(this)->getBaserels(res); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 452 | else |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 453 | static_cast<NonSectionChunk *>(this)->getBaserels(res); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | inline StringRef Chunk::getDebugName() const { |
| 457 | if (isa<SectionChunk>(this)) |
| 458 | return static_cast<const SectionChunk *>(this)->getDebugName(); |
Jacek Caban | e24ac11 | 2023-10-20 10:38:12 | [diff] [blame] | 459 | return static_cast<const NonSectionChunk *>(this)->getDebugName(); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 460 | } |
| 461 | |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 462 | inline MachineTypes Chunk::getMachine() const { |
| 463 | if (isa<SectionChunk>(this)) |
| 464 | return static_cast<const SectionChunk *>(this)->getMachine(); |
Jacek Caban | e24ac11 | 2023-10-20 10:38:12 | [diff] [blame] | 465 | return static_cast<const NonSectionChunk *>(this)->getMachine(); |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 466 | } |
| 467 | |
Jacek Caban | 8f9903d | 2024-04-04 12:41:50 | [diff] [blame] | 468 | inline llvm::Triple::ArchType Chunk::getArch() const { |
| 469 | return llvm::getMachineArchType(getMachine()); |
| 470 | } |
| 471 | |
Jacek Caban | c605431 | 2023-11-01 15:47:43 | [diff] [blame] | 472 | inline std::optional<chpe_range_type> Chunk::getArm64ECRangeType() const { |
| 473 | // Data sections don't need codemap entries. |
| 474 | if (!(getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE)) |
| 475 | return std::nullopt; |
| 476 | |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 477 | switch (getMachine()) { |
| 478 | case AMD64: |
| 479 | return chpe_range_type::Amd64; |
| 480 | case ARM64EC: |
| 481 | return chpe_range_type::Arm64EC; |
| 482 | default: |
| 483 | return chpe_range_type::Arm64; |
| 484 | } |
| 485 | } |
| 486 | |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 487 | // This class is used to implement an lld-specific feature (not implemented in |
| 488 | // MSVC) that minimizes the output size by finding string literals sharing tail |
| 489 | // parts and merging them. |
| 490 | // |
| 491 | // If string tail merging is enabled and a section is identified as containing a |
| 492 | // string literal, it is added to a MergeChunk with an appropriate alignment. |
| 493 | // The MergeChunk then tail merges the strings using the StringTableBuilder |
| 494 | // class and assigns RVAs and section offsets to each of the member chunks based |
| 495 | // on the offsets assigned by the StringTableBuilder. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 496 | class MergeChunk : public NonSectionChunk { |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 497 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 498 | MergeChunk(uint32_t alignment); |
Amy Huang | 6f7483b | 2021-09-16 23:48:26 | [diff] [blame] | 499 | static void addSection(COFFLinkerContext &ctx, SectionChunk *c); |
Reid Kleckner | 11c141e | 2019-05-24 00:02:00 | [diff] [blame] | 500 | void finalizeContents(); |
| 501 | void assignSubsectionRVAs(); |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 502 | |
Peter Collingbourne | fa322ab | 2018-04-19 20:03:24 | [diff] [blame] | 503 | uint32_t getOutputCharacteristics() const override; |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 504 | StringRef getSectionName() const override { return ".rdata"; } |
| 505 | size_t getSize() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 506 | void writeTo(uint8_t *buf) const override; |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 507 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 508 | std::vector<SectionChunk *> sections; |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 509 | |
| 510 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 511 | llvm::StringTableBuilder builder; |
| 512 | bool finalized = false; |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 | [diff] [blame] | 513 | }; |
| 514 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 515 | // A chunk for common symbols. Common chunks don't have actual data. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 516 | class CommonChunk : public NonSectionChunk { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 517 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 518 | CommonChunk(const COFFSymbolRef sym); |
| 519 | size_t getSize() const override { return sym.getValue(); } |
Peter Collingbourne | fa322ab | 2018-04-19 20:03:24 | [diff] [blame] | 520 | uint32_t getOutputCharacteristics() const override; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 521 | StringRef getSectionName() const override { return ".bss"; } |
| 522 | |
| 523 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 524 | const COFFSymbolRef sym; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | // A chunk for linker-created strings. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 528 | class StringChunk : public NonSectionChunk { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 529 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 530 | explicit StringChunk(StringRef s) : str(s) {} |
| 531 | size_t getSize() const override { return str.size() + 1; } |
| 532 | void writeTo(uint8_t *buf) const override; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 533 | |
| 534 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 535 | StringRef str; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 536 | }; |
| 537 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 538 | static const uint8_t importThunkX86[] = { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 539 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0 |
| 540 | }; |
| 541 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 542 | static const uint8_t importThunkARM[] = { |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 | [diff] [blame] | 543 | 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 |
| 544 | 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 |
| 545 | 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip] |
| 546 | }; |
| 547 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 548 | static const uint8_t importThunkARM64[] = { |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 | [diff] [blame] | 549 | 0x10, 0x00, 0x00, 0x90, // adrp x16, #0 |
| 550 | 0x10, 0x02, 0x40, 0xf9, // ldr x16, [x16] |
| 551 | 0x00, 0x02, 0x1f, 0xd6, // br x16 |
| 552 | }; |
| 553 | |
Jacek Caban | 7c82b56 | 2024-09-12 13:25:12 | [diff] [blame] | 554 | static const uint8_t importThunkARM64EC[] = { |
| 555 | 0x0b, 0x00, 0x00, 0x90, // adrp x11, 0x0 |
| 556 | 0x6b, 0x01, 0x40, 0xf9, // ldr x11, [x11] |
| 557 | 0x0a, 0x00, 0x00, 0x90, // adrp x10, 0x0 |
| 558 | 0x4a, 0x01, 0x00, 0x91, // add x10, x10, #0x0 |
| 559 | 0x00, 0x00, 0x00, 0x14 // b 0x0 |
Jacek Caban | 99a2354 | 2024-09-11 12:46:40 | [diff] [blame] | 560 | }; |
| 561 | |
Rui Ueyama | 4b22fa7 | 2015-06-07 01:15:04 | [diff] [blame] | 562 | // Windows-specific. |
Nico Weber | 1f3ab98 | 2019-01-14 19:05:21 | [diff] [blame] | 563 | // A chunk for DLL import jump table entry. In a final output, its |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 564 | // contents will be a JMP instruction to some __imp_ symbol. |
Jacek Caban | 14043d4 | 2023-11-01 12:27:29 | [diff] [blame] | 565 | class ImportThunkChunk : public NonSectionCodeChunk { |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 566 | public: |
Jacek Caban | 6be9be5 | 2024-09-13 13:42:05 | [diff] [blame] | 567 | ImportThunkChunk(COFFLinkerContext &ctx, Defined *s); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 568 | static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; } |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 569 | |
Jacek Caban | 6be9be5 | 2024-09-13 13:42:05 | [diff] [blame] | 570 | // We track the usage of the thunk symbol separately from the import file |
| 571 | // to avoid generating unnecessary thunks. |
| 572 | bool live; |
| 573 | |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 574 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 575 | Defined *impSymbol; |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 576 | COFFLinkerContext &ctx; |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | class ImportThunkChunkX64 : public ImportThunkChunk { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 580 | public: |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 581 | explicit ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 582 | size_t getSize() const override { return sizeof(importThunkX86); } |
| 583 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 584 | MachineTypes getMachine() const override { return AMD64; } |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 | [diff] [blame] | 585 | }; |
| 586 | |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 587 | class ImportThunkChunkX86 : public ImportThunkChunk { |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 | [diff] [blame] | 588 | public: |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 589 | explicit ImportThunkChunkX86(COFFLinkerContext &ctx, Defined *s) |
| 590 | : ImportThunkChunk(ctx, s) {} |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 591 | size_t getSize() const override { return sizeof(importThunkX86); } |
| 592 | void getBaserels(std::vector<Baserel> *res) override; |
| 593 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 594 | MachineTypes getMachine() const override { return I386; } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 595 | }; |
| 596 | |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 597 | class ImportThunkChunkARM : public ImportThunkChunk { |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 | [diff] [blame] | 598 | public: |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 599 | explicit ImportThunkChunkARM(COFFLinkerContext &ctx, Defined *s) |
| 600 | : ImportThunkChunk(ctx, s) { |
Martin Storsjö | 12c9e2f | 2020-04-08 21:42:50 | [diff] [blame] | 601 | setAlignment(2); |
| 602 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 603 | size_t getSize() const override { return sizeof(importThunkARM); } |
| 604 | void getBaserels(std::vector<Baserel> *res) override; |
| 605 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 606 | MachineTypes getMachine() const override { return ARMNT; } |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 | [diff] [blame] | 607 | }; |
| 608 | |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 609 | class ImportThunkChunkARM64 : public ImportThunkChunk { |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 | [diff] [blame] | 610 | public: |
Jacek Caban | ea5d37f | 2024-09-13 15:05:02 | [diff] [blame] | 611 | explicit ImportThunkChunkARM64(COFFLinkerContext &ctx, Defined *s, |
| 612 | MachineTypes machine) |
| 613 | : ImportThunkChunk(ctx, s), machine(machine) { |
Martin Storsjö | 12c9e2f | 2020-04-08 21:42:50 | [diff] [blame] | 614 | setAlignment(4); |
| 615 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 616 | size_t getSize() const override { return sizeof(importThunkARM64); } |
| 617 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | ea5d37f | 2024-09-13 15:05:02 | [diff] [blame] | 618 | MachineTypes getMachine() const override { return machine; } |
| 619 | |
| 620 | private: |
| 621 | MachineTypes machine; |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 | [diff] [blame] | 622 | }; |
| 623 | |
Jacek Caban | 99a2354 | 2024-09-11 12:46:40 | [diff] [blame] | 624 | // ARM64EC __impchk_* thunk implementation. |
| 625 | // Performs an indirect call to an imported function pointer |
| 626 | // using the __icall_helper_arm64ec helper function. |
| 627 | class ImportThunkChunkARM64EC : public ImportThunkChunk { |
| 628 | public: |
| 629 | explicit ImportThunkChunkARM64EC(ImportFile *file); |
Jacek Caban | f661e695 | 2024-09-26 08:44:40 | [diff] [blame] | 630 | size_t getSize() const override; |
Jacek Caban | 99a2354 | 2024-09-11 12:46:40 | [diff] [blame] | 631 | MachineTypes getMachine() const override { return ARM64EC; } |
| 632 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | f661e695 | 2024-09-26 08:44:40 | [diff] [blame] | 633 | bool verifyRanges() override; |
| 634 | uint32_t extendRanges() override; |
Jacek Caban | 99a2354 | 2024-09-11 12:46:40 | [diff] [blame] | 635 | |
| 636 | Defined *exitThunk; |
Jacek Caban | 486f790 | 2024-09-19 11:47:22 | [diff] [blame] | 637 | Defined *sym = nullptr; |
Jacek Caban | f661e695 | 2024-09-26 08:44:40 | [diff] [blame] | 638 | bool extended = false; |
Jacek Caban | 99a2354 | 2024-09-11 12:46:40 | [diff] [blame] | 639 | |
| 640 | private: |
| 641 | ImportFile *file; |
| 642 | }; |
| 643 | |
Jacek Caban | 14043d4 | 2023-11-01 12:27:29 | [diff] [blame] | 644 | class RangeExtensionThunkARM : public NonSectionCodeChunk { |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 | [diff] [blame] | 645 | public: |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 646 | explicit RangeExtensionThunkARM(COFFLinkerContext &ctx, Defined *t) |
| 647 | : target(t), ctx(ctx) { |
| 648 | setAlignment(2); |
| 649 | } |
Martin Storsjo | c9f4d25f | 2019-02-01 22:08:09 | [diff] [blame] | 650 | size_t getSize() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 651 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | cbbb545 | 2023-10-18 11:57:42 | [diff] [blame] | 652 | MachineTypes getMachine() const override { return ARMNT; } |
Martin Storsjo | c9f4d25f | 2019-02-01 22:08:09 | [diff] [blame] | 653 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 654 | Defined *target; |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 655 | |
| 656 | private: |
| 657 | COFFLinkerContext &ctx; |
Martin Storsjo | c9f4d25f | 2019-02-01 22:08:09 | [diff] [blame] | 658 | }; |
| 659 | |
Jacek Caban | efad561 | 2024-08-29 08:19:32 | [diff] [blame] | 660 | // A ragnge extension thunk used for both ARM64EC and ARM64 machine types. |
Jacek Caban | 14043d4 | 2023-11-01 12:27:29 | [diff] [blame] | 661 | class RangeExtensionThunkARM64 : public NonSectionCodeChunk { |
Martin Storsjo | c9f4d25f | 2019-02-01 22:08:09 | [diff] [blame] | 662 | public: |
Jacek Caban | efad561 | 2024-08-29 08:19:32 | [diff] [blame] | 663 | explicit RangeExtensionThunkARM64(MachineTypes machine, Defined *t) |
| 664 | : target(t), machine(machine) { |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 665 | setAlignment(4); |
Jacek Caban | efad561 | 2024-08-29 08:19:32 | [diff] [blame] | 666 | assert(llvm::COFF::isAnyArm64(machine)); |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 667 | } |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 | [diff] [blame] | 668 | size_t getSize() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 669 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | efad561 | 2024-08-29 08:19:32 | [diff] [blame] | 670 | MachineTypes getMachine() const override { return machine; } |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 | [diff] [blame] | 671 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 672 | Defined *target; |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 673 | |
| 674 | private: |
Jacek Caban | efad561 | 2024-08-29 08:19:32 | [diff] [blame] | 675 | MachineTypes machine; |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 | [diff] [blame] | 676 | }; |
| 677 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 678 | // Windows-specific. |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 679 | // See comments for DefinedLocalImport class. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 680 | class LocalImportChunk : public NonSectionChunk { |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 681 | public: |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 682 | explicit LocalImportChunk(COFFLinkerContext &ctx, Defined *s); |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 | [diff] [blame] | 683 | size_t getSize() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 684 | void getBaserels(std::vector<Baserel> *res) override; |
| 685 | void writeTo(uint8_t *buf) const override; |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 686 | |
| 687 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 688 | Defined *sym; |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 689 | COFFLinkerContext &ctx; |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 | [diff] [blame] | 690 | }; |
| 691 | |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 692 | // Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and |
| 693 | // offset into the chunk. Order does not matter as the RVA table will be sorted |
| 694 | // later. |
| 695 | struct ChunkAndOffset { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 696 | Chunk *inputChunk; |
| 697 | uint32_t offset; |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 698 | |
| 699 | struct DenseMapInfo { |
| 700 | static ChunkAndOffset getEmptyKey() { |
| 701 | return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0}; |
| 702 | } |
| 703 | static ChunkAndOffset getTombstoneKey() { |
| 704 | return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0}; |
| 705 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 706 | static unsigned getHashValue(const ChunkAndOffset &co) { |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 707 | return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue( |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 708 | {co.inputChunk, co.offset}); |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 709 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 710 | static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) { |
| 711 | return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset; |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 712 | } |
| 713 | }; |
| 714 | }; |
| 715 | |
| 716 | using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>; |
| 717 | |
| 718 | // Table which contains symbol RVAs. Used for /safeseh and /guard:cf. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 719 | class RVATableChunk : public NonSectionChunk { |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 720 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 721 | explicit RVATableChunk(SymbolRVASet s) : syms(std::move(s)) {} |
| 722 | size_t getSize() const override { return syms.size() * 4; } |
| 723 | void writeTo(uint8_t *buf) const override; |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 724 | |
| 725 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 726 | SymbolRVASet syms; |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 727 | }; |
| 728 | |
Pengfei Wang | 184377d | 2021-04-14 06:21:52 | [diff] [blame] | 729 | // Table which contains symbol RVAs with flags. Used for /guard:ehcont. |
| 730 | class RVAFlagTableChunk : public NonSectionChunk { |
| 731 | public: |
| 732 | explicit RVAFlagTableChunk(SymbolRVASet s) : syms(std::move(s)) {} |
| 733 | size_t getSize() const override { return syms.size() * 5; } |
| 734 | void writeTo(uint8_t *buf) const override; |
| 735 | |
| 736 | private: |
| 737 | SymbolRVASet syms; |
| 738 | }; |
| 739 | |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 | [diff] [blame] | 740 | // Windows-specific. |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 741 | // This class represents a block in .reloc section. |
| 742 | // See the PE/COFF spec 5.6 for details. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 743 | class BaserelChunk : public NonSectionChunk { |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 744 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 745 | BaserelChunk(uint32_t page, Baserel *begin, Baserel *end); |
| 746 | size_t getSize() const override { return data.size(); } |
| 747 | void writeTo(uint8_t *buf) const override; |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 748 | |
| 749 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 750 | std::vector<uint8_t> data; |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 | [diff] [blame] | 751 | }; |
| 752 | |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 | [diff] [blame] | 753 | class Baserel { |
| 754 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 755 | Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {} |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 756 | explicit Baserel(uint32_t v, llvm::COFF::MachineTypes machine) |
| 757 | : Baserel(v, getDefaultType(machine)) {} |
Jacek Caban | 233ed51 | 2024-09-05 13:57:20 | [diff] [blame] | 758 | static uint8_t getDefaultType(llvm::COFF::MachineTypes machine); |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 | [diff] [blame] | 759 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 760 | uint32_t rva; |
| 761 | uint8_t type; |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 | [diff] [blame] | 762 | }; |
| 763 | |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 764 | // This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a |
| 765 | // specific place in a section, without any data. This is used for the MinGW |
| 766 | // specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept |
| 767 | // of an empty chunk isn't MinGW specific. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 768 | class EmptyChunk : public NonSectionChunk { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 769 | public: |
| 770 | EmptyChunk() {} |
| 771 | size_t getSize() const override { return 0; } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 772 | void writeTo(uint8_t *buf) const override {} |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 773 | }; |
| 774 | |
Jacek Caban | fe2bd12 | 2023-11-15 11:35:45 | [diff] [blame] | 775 | class ECCodeMapEntry { |
| 776 | public: |
| 777 | ECCodeMapEntry(Chunk *first, Chunk *last, chpe_range_type type) |
| 778 | : first(first), last(last), type(type) {} |
| 779 | Chunk *first; |
| 780 | Chunk *last; |
| 781 | chpe_range_type type; |
| 782 | }; |
| 783 | |
| 784 | // This is a chunk containing CHPE code map on EC targets. It's a table |
| 785 | // of address ranges and their types. |
| 786 | class ECCodeMapChunk : public NonSectionChunk { |
| 787 | public: |
| 788 | ECCodeMapChunk(std::vector<ECCodeMapEntry> &map) : map(map) {} |
| 789 | size_t getSize() const override; |
| 790 | void writeTo(uint8_t *buf) const override; |
| 791 | |
| 792 | private: |
| 793 | std::vector<ECCodeMapEntry> ↦ |
| 794 | }; |
| 795 | |
Jacek Caban | 52a7116 | 2024-08-23 19:17:38 | [diff] [blame] | 796 | class CHPECodeRangesChunk : public NonSectionChunk { |
| 797 | public: |
| 798 | CHPECodeRangesChunk(std::vector<std::pair<Chunk *, Defined *>> &exportThunks) |
| 799 | : exportThunks(exportThunks) {} |
| 800 | size_t getSize() const override; |
| 801 | void writeTo(uint8_t *buf) const override; |
| 802 | |
| 803 | private: |
| 804 | std::vector<std::pair<Chunk *, Defined *>> &exportThunks; |
| 805 | }; |
| 806 | |
Jacek Caban | caa844e | 2024-08-23 18:29:19 | [diff] [blame] | 807 | class CHPERedirectionChunk : public NonSectionChunk { |
| 808 | public: |
| 809 | CHPERedirectionChunk(std::vector<std::pair<Chunk *, Defined *>> &exportThunks) |
| 810 | : exportThunks(exportThunks) {} |
| 811 | size_t getSize() const override; |
| 812 | void writeTo(uint8_t *buf) const override; |
| 813 | |
| 814 | private: |
| 815 | std::vector<std::pair<Chunk *, Defined *>> &exportThunks; |
| 816 | }; |
| 817 | |
Jacek Caban | a2d8743 | 2024-08-22 20:03:05 | [diff] [blame] | 818 | static const uint8_t ECExportThunkCode[] = { |
| 819 | 0x48, 0x8b, 0xc4, // movq %rsp, %rax |
| 820 | 0x48, 0x89, 0x58, 0x20, // movq %rbx, 0x20(%rax) |
| 821 | 0x55, // pushq %rbp |
| 822 | 0x5d, // popq %rbp |
| 823 | 0xe9, 0, 0, 0, 0, // jmp *0x0 |
| 824 | 0xcc, // int3 |
| 825 | 0xcc // int3 |
| 826 | }; |
| 827 | |
| 828 | class ECExportThunkChunk : public NonSectionCodeChunk { |
| 829 | public: |
Jacek Caban | c53e527 | 2025-03-07 17:34:56 | [diff] [blame] | 830 | explicit ECExportThunkChunk(Defined *targetSym) |
| 831 | : NonSectionCodeChunk(ECExportThunkKind), target(targetSym) {} |
| 832 | static bool classof(const Chunk *c) { return c->kind() == ECExportThunkKind; } |
| 833 | |
Jacek Caban | a2d8743 | 2024-08-22 20:03:05 | [diff] [blame] | 834 | size_t getSize() const override { return sizeof(ECExportThunkCode); }; |
| 835 | void writeTo(uint8_t *buf) const override; |
| 836 | MachineTypes getMachine() const override { return AMD64; } |
| 837 | |
| 838 | Defined *target; |
| 839 | }; |
| 840 | |
Jacek Caban | 3b0dafff | 2025-01-10 20:50:07 | [diff] [blame] | 841 | // ARM64X relocation value, potentially relative to a symbol. |
| 842 | class Arm64XRelocVal { |
| 843 | public: |
| 844 | Arm64XRelocVal(uint64_t value = 0) : value(value) {} |
| 845 | Arm64XRelocVal(Defined *sym, int32_t offset = 0) : sym(sym), value(offset) {} |
Jacek Caban | 659e66e | 2025-01-21 21:24:00 | [diff] [blame] | 846 | Arm64XRelocVal(Chunk *chunk, int32_t offset = 0) |
| 847 | : chunk(chunk), value(offset) {} |
Jacek Caban | 3b0dafff | 2025-01-10 20:50:07 | [diff] [blame] | 848 | uint64_t get() const; |
| 849 | |
| 850 | private: |
| 851 | Defined *sym = nullptr; |
Jacek Caban | 659e66e | 2025-01-21 21:24:00 | [diff] [blame] | 852 | Chunk *chunk = nullptr; |
Jacek Caban | 3b0dafff | 2025-01-10 20:50:07 | [diff] [blame] | 853 | uint64_t value; |
| 854 | }; |
| 855 | |
Jacek Caban | 71bbafb | 2024-12-05 12:07:41 | [diff] [blame] | 856 | // ARM64X entry for dynamic relocations. |
| 857 | class Arm64XDynamicRelocEntry { |
| 858 | public: |
| 859 | Arm64XDynamicRelocEntry(llvm::COFF::Arm64XFixupType type, uint8_t size, |
Jacek Caban | a16adaf | 2025-01-20 10:38:54 | [diff] [blame] | 860 | Arm64XRelocVal offset, Arm64XRelocVal value) |
Jacek Caban | 71bbafb | 2024-12-05 12:07:41 | [diff] [blame] | 861 | : offset(offset), value(value), type(type), size(size) {} |
| 862 | |
| 863 | size_t getSize() const; |
| 864 | void writeTo(uint8_t *buf) const; |
| 865 | |
Jacek Caban | a16adaf | 2025-01-20 10:38:54 | [diff] [blame] | 866 | Arm64XRelocVal offset; |
Jacek Caban | 3b0dafff | 2025-01-10 20:50:07 | [diff] [blame] | 867 | Arm64XRelocVal value; |
Jacek Caban | 71bbafb | 2024-12-05 12:07:41 | [diff] [blame] | 868 | |
| 869 | private: |
| 870 | llvm::COFF::Arm64XFixupType type; |
| 871 | uint8_t size; |
| 872 | }; |
| 873 | |
| 874 | // Dynamic relocation chunk containing ARM64X relocations for the hybrid image. |
| 875 | class DynamicRelocsChunk : public NonSectionChunk { |
| 876 | public: |
| 877 | DynamicRelocsChunk() {} |
| 878 | size_t getSize() const override { return size; } |
| 879 | void writeTo(uint8_t *buf) const override; |
| 880 | void finalize(); |
| 881 | |
Jacek Caban | a16adaf | 2025-01-20 10:38:54 | [diff] [blame] | 882 | void add(llvm::COFF::Arm64XFixupType type, uint8_t size, |
Jacek Caban | 659e66e | 2025-01-21 21:24:00 | [diff] [blame] | 883 | Arm64XRelocVal offset, Arm64XRelocVal value = Arm64XRelocVal()) { |
Jacek Caban | 71bbafb | 2024-12-05 12:07:41 | [diff] [blame] | 884 | arm64xRelocs.emplace_back(type, size, offset, value); |
| 885 | } |
| 886 | |
Jacek Caban | 659e66e | 2025-01-21 21:24:00 | [diff] [blame] | 887 | void set(uint32_t rva, Arm64XRelocVal value); |
| 888 | |
Jacek Caban | 71bbafb | 2024-12-05 12:07:41 | [diff] [blame] | 889 | private: |
| 890 | std::vector<Arm64XDynamicRelocEntry> arm64xRelocs; |
| 891 | size_t size; |
| 892 | }; |
| 893 | |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 894 | // MinGW specific, for the "automatic import of variables from DLLs" feature. |
| 895 | // This provides the table of runtime pseudo relocations, for variable |
| 896 | // references that turned out to need to be imported from a DLL even though |
| 897 | // the reference didn't use the dllimport attribute. The MinGW runtime will |
| 898 | // process this table after loading, before handling control over to user |
| 899 | // code. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 900 | class PseudoRelocTableChunk : public NonSectionChunk { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 901 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 902 | PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs) |
| 903 | : relocs(std::move(relocs)) { |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 904 | setAlignment(4); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 905 | } |
| 906 | size_t getSize() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 907 | void writeTo(uint8_t *buf) const override; |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 908 | |
| 909 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 910 | std::vector<RuntimePseudoReloc> relocs; |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 | [diff] [blame] | 911 | }; |
| 912 | |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 | [diff] [blame] | 913 | // MinGW specific. A Chunk that contains one pointer-sized absolute value. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 | [diff] [blame] | 914 | class AbsolutePointerChunk : public NonSectionChunk { |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 | [diff] [blame] | 915 | public: |
Jacek Caban | a92bfaa | 2025-02-17 20:34:12 | [diff] [blame] | 916 | AbsolutePointerChunk(SymbolTable &symtab, uint64_t value) |
| 917 | : value(value), symtab(symtab) { |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 | [diff] [blame] | 918 | setAlignment(getSize()); |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 | [diff] [blame] | 919 | } |
| 920 | size_t getSize() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 921 | void writeTo(uint8_t *buf) const override; |
Jacek Caban | a92bfaa | 2025-02-17 20:34:12 | [diff] [blame] | 922 | MachineTypes getMachine() const override; |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 | [diff] [blame] | 923 | |
| 924 | private: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 925 | uint64_t value; |
Jacek Caban | a92bfaa | 2025-02-17 20:34:12 | [diff] [blame] | 926 | SymbolTable &symtab; |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 | [diff] [blame] | 927 | }; |
| 928 | |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 929 | // Return true if this file has the hotpatch flag set to true in the S_COMPILE3 |
| 930 | // record in codeview debug info. Also returns true for some thunks synthesized |
| 931 | // by the linker. |
| 932 | inline bool Chunk::isHotPatchable() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 933 | if (auto *sc = dyn_cast<SectionChunk>(this)) |
| 934 | return sc->file->hotPatchable; |
Reid Kleckner | f612b18 | 2019-05-28 17:38:04 | [diff] [blame] | 935 | else if (isa<ImportThunkChunk>(this)) |
| 936 | return true; |
| 937 | return false; |
| 938 | } |
| 939 | |
Jacek Caban | fed8e38 | 2024-06-18 09:14:01 | [diff] [blame] | 940 | inline Defined *Chunk::getEntryThunk() const { |
| 941 | if (auto *c = dyn_cast<const SectionChunkEC>(this)) |
| 942 | return c->entryThunk; |
| 943 | return nullptr; |
| 944 | } |
| 945 | |
| 946 | inline void Chunk::setEntryThunk(Defined *entryThunk) { |
| 947 | if (auto c = dyn_cast<SectionChunkEC>(this)) |
| 948 | c->entryThunk = entryThunk; |
| 949 | } |
| 950 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 951 | void applyMOV32T(uint8_t *off, uint32_t v); |
| 952 | void applyBranch24T(uint8_t *off, int32_t v); |
Martin Storsjo | 82eaf6c | 2017-07-25 20:00:37 | [diff] [blame] | 953 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 954 | void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift); |
| 955 | void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit); |
| 956 | void applyArm64Branch26(uint8_t *off, int64_t v); |
Martin Storsjo | 32d21d6 | 2018-09-18 07:22:01 | [diff] [blame] | 957 | |
Andrew Ng | 85a2f29 | 2023-01-10 14:03:48 | [diff] [blame] | 958 | // Convenience class for initializing a coff_section with specific flags. |
| 959 | class FakeSection { |
| 960 | public: |
| 961 | FakeSection(int c) { section.Characteristics = c; } |
| 962 | |
| 963 | coff_section section; |
| 964 | }; |
| 965 | |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 966 | // Convenience class for initializing a SectionChunk with specific flags. |
| 967 | class FakeSectionChunk { |
| 968 | public: |
Andrew Ng | 85a2f29 | 2023-01-10 14:03:48 | [diff] [blame] | 969 | FakeSectionChunk(const coff_section *section) : chunk(nullptr, section) { |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 970 | // Comdats from LTO files can't be fully treated as regular comdats |
| 971 | // at this point; we don't know what size or contents they are going to |
| 972 | // have, so we can't do proper checking of such aspects of them. |
| 973 | chunk.selection = llvm::COFF::IMAGE_COMDAT_SELECT_ANY; |
| 974 | } |
| 975 | |
Amy Huang | 5a58b19 | 2023-01-10 04:37:28 | [diff] [blame] | 976 | SectionChunk chunk; |
| 977 | }; |
| 978 | |
Nico Weber | 7c26641 | 2022-08-08 15:32:26 | [diff] [blame] | 979 | } // namespace lld::coff |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 980 | |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 | [diff] [blame] | 981 | namespace llvm { |
| 982 | template <> |
| 983 | struct DenseMapInfo<lld::coff::ChunkAndOffset> |
| 984 | : lld::coff::ChunkAndOffset::DenseMapInfo {}; |
| 985 | } |
| 986 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 | [diff] [blame] | 987 | #endif |