blob: d03a64cc6b8128a6ffcd338ccfd2c4a199bd96fb [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:541//===- Chunks.h -------------------------------------------------*- C++ -*-===//
Rui Ueyama411c63602015-05-28 19:09:302//
Chandler Carruth2946cd72019-01-19 08:50:563// 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 Ueyama411c63602015-05-28 19:09:306//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_COFF_CHUNKS_H
10#define LLD_COFF_CHUNKS_H
11
Rui Ueyama4dbff202015-09-16 21:40:4712#include "Config.h"
Chandler Carruth59013c32015-06-29 21:12:4913#include "InputFiles.h"
Rui Ueyama3f851702017-10-02 21:00:4114#include "lld/Common/LLVM.h"
Rui Ueyama411c63602015-05-28 19:09:3015#include "llvm/ADT/ArrayRef.h"
Reid Klecknera431dd72019-05-24 20:25:4016#include "llvm/ADT/PointerIntPair.h"
Chandler Carruth59013c32015-06-29 21:12:4917#include "llvm/ADT/iterator.h"
Rui Ueyama42aa00b2015-06-25 00:33:3818#include "llvm/ADT/iterator_range.h"
Peter Collingbournef1a11f82018-03-15 21:14:0219#include "llvm/MC/StringTableBuilder.h"
Rui Ueyama411c63602015-05-28 19:09:3020#include "llvm/Object/COFF.h"
Jacek Caban8f9903d2024-04-04 12:41:5021#include "llvm/Object/WindowsMachineFlag.h"
Benjamin Kramerbd521202016-06-03 16:57:1322#include <utility>
Rui Ueyama411c63602015-05-28 19:09:3023#include <vector>
24
Nico Weber7c266412022-08-08 15:32:2625namespace lld::coff {
Rui Ueyama411c63602015-05-28 19:09:3026
27using llvm::COFF::ImportDirectoryTableEntry;
Jacek Cabancbbb5452023-10-18 11:57:4228using llvm::object::chpe_range_type;
Rui Ueyama411c63602015-05-28 19:09:3029using llvm::object::coff_relocation;
30using llvm::object::coff_section;
Jacek Cabancbbb5452023-10-18 11:57:4231using llvm::object::COFFSymbolRef;
32using llvm::object::SectionRef;
Rui Ueyama411c63602015-05-28 19:09:3033
Rui Ueyama3afd5bf2015-07-25 01:44:3234class Baserel;
Rui Ueyama411c63602015-05-28 19:09:3035class Defined;
36class DefinedImportData;
Rui Ueyama3afd5bf2015-07-25 01:44:3237class DefinedRegular;
Rui Ueyamae1b48e02017-07-26 23:05:2438class ObjFile;
Rui Ueyama411c63602015-05-28 19:09:3039class OutputSection;
Martin Storsjoeac1b052018-08-27 08:43:3140class RuntimePseudoReloc;
Rui Ueyamaf52496e2017-11-03 21:21:4741class Symbol;
Rui Ueyama411c63602015-05-28 19:09:3042
Peter Collingbourne71c7de52018-04-20 21:23:1643// Mask for permissions (discardable, writable, readable, executable, etc).
Rui Ueyama136d27a2019-07-11 05:40:3044const uint32_t permMask = 0xFE000000;
Peter Collingbourne71c7de52018-04-20 21:23:1645
46// Mask for section types (code, data, bss).
Rui Ueyama136d27a2019-07-11 05:40:3047const uint32_t typeMask = 0x000000E0;
Rui Ueyama67fcd1a02015-08-05 19:51:2848
Reid Kleckneree4e0a22019-05-22 20:21:5249// The log base 2 of the largest section alignment, which is log2(8192), or 13.
50enum : unsigned { Log2MaxSectionAlignment = 13 };
51
Rui Ueyama411c63602015-05-28 19:09:3052// 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).
56class Chunk {
57public:
Jacek Cabanfed8e382024-06-18 09:14:0158 enum Kind : uint8_t {
59 SectionKind,
60 SectionECKind,
61 OtherKind,
Jacek Cabanc53e5272025-03-07 17:34:5662 ImportThunkKind,
63 ECExportThunkKind
Jacek Cabanfed8e382024-06-18 09:14:0164 };
Rui Ueyama136d27a2019-07-11 05:40:3065 Kind kind() const { return chunkKind; }
Rui Ueyama411c63602015-05-28 19:09:3066
Rui Ueyama411c63602015-05-28 19:09:3067 // Returns the size of this chunk (even if this is a common or BSS.)
Reid Klecknera431dd72019-05-24 20:25:4068 size_t getSize() const;
Rui Ueyama411c63602015-05-28 19:09:3069
Reid Kleckneree4e0a22019-05-22 20:21:5270 // Returns chunk alignment in power of two form. Value values are powers of
71 // two from 1 to 8192.
Rui Ueyama136d27a2019-07-11 05:40:3072 uint32_t getAlignment() const { return 1U << p2Align; }
Reid Klecknera431dd72019-05-24 20:25:4073
74 // Update the chunk section alignment measured in bytes. Internally alignment
75 // is stored in log2.
Rui Ueyama136d27a2019-07-11 05:40:3076 void setAlignment(uint32_t align) {
Reid Kleckneree4e0a22019-05-22 20:21:5277 // Treat zero byte alignment as 1 byte alignment.
Rui Ueyama136d27a2019-07-11 05:40:3078 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 Kleckneree4e0a22019-05-22 20:21:5282 "impossible requested alignment");
83 }
84
Rui Ueyama743afa02015-06-06 04:07:3985 // 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 Ueyama136d27a2019-07-11 05:40:3089 void writeTo(uint8_t *buf) const;
Rui Ueyamad6fefba42015-05-28 19:45:4390
Reid Kleckner34e9c412019-05-07 20:30:4191 // 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 Ueyama136d27a2019-07-11 05:40:3094 uint32_t getRVA() const { return rva; }
95 void setRVA(uint64_t v) {
Reid Kleckneree23f8b2021-05-14 02:32:4996 // This may truncate. The writer checks for overflow later.
Rui Ueyama136d27a2019-07-11 05:40:3097 rva = (uint32_t)v;
Reid Kleckner34e9c412019-05-07 20:30:4198 }
Rui Ueyama411c63602015-05-28 19:09:3099
Rui Ueyama411c63602015-05-28 19:09:30100 // Returns readable/writable/executable bits.
Reid Klecknera431dd72019-05-24 20:25:40101 uint32_t getOutputCharacteristics() const;
Rui Ueyama411c63602015-05-28 19:09:30102
103 // Returns the section name if this is a section chunk.
104 // It is illegal to call this function on non-section chunks.
Reid Klecknera431dd72019-05-24 20:25:40105 StringRef getSectionName() const;
Rui Ueyama411c63602015-05-28 19:09:30106
Rui Ueyama411c63602015-05-28 19:09:30107 // An output section has pointers to chunks in the section, and each
108 // chunk has a back pointer to an output section.
Rui Ueyama136d27a2019-07-11 05:40:30109 void setOutputSectionIdx(uint16_t o) { osidx = o; }
110 uint16_t getOutputSectionIdx() const { return osidx; }
Rui Ueyama411c63602015-05-28 19:09:30111
Rui Ueyama588e8322015-06-15 01:23:58112 // Windows-specific.
113 // Collect all locations that contain absolute addresses for base relocations.
Rui Ueyama136d27a2019-07-11 05:40:30114 void getBaserels(std::vector<Baserel> *res);
Rui Ueyama588e8322015-06-15 01:23:58115
Rui Ueyama6a60be72015-06-24 00:00:52116 // 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 Klecknera431dd72019-05-24 20:25:40118 StringRef getDebugName() const;
Rui Ueyama6a60be72015-06-24 00:00:52119
Reid Klecknera431dd72019-05-24 20:25:40120 // 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 Ganead307c4c2019-02-23 01:46:18124
Jacek Cabancbbb5452023-10-18 11:57:42125 MachineTypes getMachine() const;
Jacek Caban8f9903d2024-04-04 12:41:50126 llvm::Triple::ArchType getArch() const;
Jacek Cabanc6054312023-11-01 15:47:43127 std::optional<chpe_range_type> getArm64ECRangeType() const;
Jacek Cabancbbb5452023-10-18 11:57:42128
Jacek Cabanfed8e382024-06-18 09:14:01129 // ARM64EC entry thunk associated with the chunk.
130 Defined *getEntryThunk() const;
131 void setEntryThunk(Defined *entryThunk);
132
Rui Ueyama411c63602015-05-28 19:09:30133protected:
Rui Ueyama136d27a2019-07-11 05:40:30134 Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {}
Reid Kleckner14f4ff62019-05-23 20:26:41135
Rui Ueyama136d27a2019-07-11 05:40:30136 const Kind chunkKind;
Rui Ueyamafc510f42015-06-25 19:10:58137
Rui Ueyama7e296ad2019-07-10 09:10:01138public:
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 Klecknera431dd72019-05-24 20:25:40142 // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit.
Rui Ueyama136d27a2019-07-11 05:40:30143 uint8_t hasData : 1;
Reid Klecknera431dd72019-05-24 20:25:40144
Rui Ueyama7e296ad2019-07-10 09:10:01145public:
Reid Kleckneree4e0a22019-05-22 20:21:52146 // The alignment of this chunk, stored in log2 form. The writer uses the
147 // value.
Rui Ueyama136d27a2019-07-11 05:40:30148 uint8_t p2Align : 7;
Reid Kleckneree4e0a22019-05-22 20:21:52149
Reid Kleckner56bee1a2019-05-24 18:25:49150 // The output section index for this chunk. The first valid section number is
151 // one.
Rui Ueyama136d27a2019-07-11 05:40:30152 uint16_t osidx = 0;
Reid Kleckner56bee1a2019-05-24 18:25:49153
Rui Ueyama411c63602015-05-28 19:09:30154 // The RVA of this chunk in the output. The writer sets a value.
Rui Ueyama136d27a2019-07-11 05:40:30155 uint32_t rva = 0;
Rui Ueyama411c63602015-05-28 19:09:30156};
157
Reid Klecknera431dd72019-05-24 20:25:40158class NonSectionChunk : public Chunk {
159public:
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 Ueyama136d27a2019-07-11 05:40:30171 virtual void writeTo(uint8_t *buf) const {}
Reid Klecknera431dd72019-05-24 20:25:40172
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 Ueyama136d27a2019-07-11 05:40:30181 virtual void getBaserels(std::vector<Baserel> *res) {}
Reid Klecknera431dd72019-05-24 20:25:40182
Jacek Cabancbbb5452023-10-18 11:57:42183 virtual MachineTypes getMachine() const { return IMAGE_FILE_MACHINE_UNKNOWN; }
184
Reid Klecknera431dd72019-05-24 20:25:40185 // 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 Cabanf661e6952024-09-26 08:44:40189 // 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 Cabanfed8e382024-06-18 09:14:01196 static bool classof(const Chunk *c) { return c->kind() >= OtherKind; }
Reid Klecknera431dd72019-05-24 20:25:40197
198protected:
Rui Ueyama136d27a2019-07-11 05:40:30199 NonSectionChunk(Kind k = OtherKind) : Chunk(k) {}
Reid Klecknera431dd72019-05-24 20:25:40200};
201
Jacek Caban14043d42023-11-01 12:27:29202class NonSectionCodeChunk : public NonSectionChunk {
203public:
204 virtual uint32_t getOutputCharacteristics() const override {
205 return llvm::COFF::IMAGE_SCN_MEM_READ | llvm::COFF::IMAGE_SCN_MEM_EXECUTE;
206 }
207
208protected:
209 NonSectionCodeChunk(Kind k = OtherKind) : NonSectionChunk(k) {}
210};
211
Alexander Kornienkod5e4a5a2022-09-30 11:04:07212// 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.
215class RuntimePseudoReloc {
216public:
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 Ueyama411c63602015-05-28 19:09:30229// A chunk corresponding a section of an input file.
Jacek Cabanfed8e382024-06-18 09:14:01230class SectionChunk : public Chunk {
Rui Ueyama92298d52015-09-16 14:19:10231 // Identical COMDAT Folding feature accesses section internal data.
232 friend class ICF;
233
Rui Ueyama411c63602015-05-28 19:09:30234public:
Chandler Carruth59013c32015-06-29 21:12:49235 class symbol_iterator : public llvm::iterator_adaptor_base<
236 symbol_iterator, const coff_relocation *,
Rui Ueyamaf52496e2017-11-03 21:21:47237 std::random_access_iterator_tag, Symbol *> {
Chandler Carruth59013c32015-06-29 21:12:49238 friend SectionChunk;
239
Rui Ueyama136d27a2019-07-11 05:40:30240 ObjFile *file;
Chandler Carruth59013c32015-06-29 21:12:49241
Rui Ueyama136d27a2019-07-11 05:40:30242 symbol_iterator(ObjFile *file, const coff_relocation *i)
243 : symbol_iterator::iterator_adaptor_base(i), file(file) {}
Chandler Carruth59013c32015-06-29 21:12:49244
245 public:
246 symbol_iterator() = default;
247
Rui Ueyama136d27a2019-07-11 05:40:30248 Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); }
Chandler Carruth59013c32015-06-29 21:12:49249 };
250
Jacek Cabanfed8e382024-06-18 09:14:01251 SectionChunk(ObjFile *file, const coff_section *header, Kind k = SectionKind);
252 static bool classof(const Chunk *c) { return c->kind() <= SectionECKind; }
Rui Ueyama136d27a2019-07-11 05:40:30253 size_t getSize() const { return header->SizeOfRawData; }
David Majnemer22dff0a2016-03-15 09:48:27254 ArrayRef<uint8_t> getContents() const;
Rui Ueyama136d27a2019-07-11 05:40:30255 void writeTo(uint8_t *buf) const;
Jacek Cabanf8f01b52025-04-11 14:13:46256 MachineTypes getMachine() const;
Jacek Caban47401b62023-10-16 18:50:15257
Reid Klecknerb69db4a2021-03-10 22:51:52258 // 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 Klecknera431dd72019-05-24 20:25:40267 uint32_t getOutputCharacteristics() const {
Rui Ueyama136d27a2019-07-11 05:40:30268 return header->Characteristics & (permMask | typeMask);
Reid Klecknera431dd72019-05-24 20:25:40269 }
270 StringRef getSectionName() const {
Rui Ueyama136d27a2019-07-11 05:40:30271 return StringRef(sectionNameData, sectionNameSize);
Reid Kleckner0a1b1d62019-05-03 20:17:14272 }
Rui Ueyama136d27a2019-07-11 05:40:30273 void getBaserels(std::vector<Baserel> *res);
Rui Ueyamafc510f42015-06-25 19:10:58274 bool isCOMDAT() const;
Reid Klecknerb69db4a2021-03-10 22:51:52275 void applyRelocation(uint8_t *off, const coff_relocation &rel) const;
Rui Ueyama136d27a2019-07-11 05:40:30276 void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
Amy Huang5a58b192023-01-10 04:37:28277 uint64_t p, uint64_t imageBase) const;
Rui Ueyama136d27a2019-07-11 05:40:30278 void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
Amy Huang5a58b192023-01-10 04:37:28279 uint64_t p, uint64_t imageBase) const;
Rui Ueyama136d27a2019-07-11 05:40:30280 void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
Amy Huang5a58b192023-01-10 04:37:28281 uint64_t p, uint64_t imageBase) const;
Rui Ueyama136d27a2019-07-11 05:40:30282 void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
Amy Huang5a58b192023-01-10 04:37:28283 uint64_t p, uint64_t imageBase) const;
Rui Ueyamafc510f42015-06-25 19:10:58284
Rui Ueyama136d27a2019-07-11 05:40:30285 void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res);
Martin Storsjoeac1b052018-08-27 08:43:31286
Rui Ueyamafc510f42015-06-25 19:10:58287 // 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 Ueyama411c63602015-05-28 19:09:30290
Rui Ueyama411c63602015-05-28 19:09:30291 // Adds COMDAT associative sections to this COMDAT section. A chunk
292 // and its children are treated as a group by the garbage collector.
Rui Ueyama136d27a2019-07-11 05:40:30293 void addAssociative(SectionChunk *child);
Rui Ueyama411c63602015-05-28 19:09:30294
Reid Klecknera431dd72019-05-24 20:25:40295 StringRef getDebugName() const;
Rui Ueyama6a60be72015-06-24 00:00:52296
Reid Klecknerf5bb7382017-06-20 17:14:09297 // 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 Song8d85c962023-06-05 21:36:19300 return getSectionName() == ".debug" || getSectionName().starts_with(".debug$");
Reid Klecknerf5bb7382017-06-20 17:14:09301 }
302
Martin Storsjo67dd3412017-10-10 06:05:29303 // True if this is a DWARF debug info or exception handling chunk.
304 bool isDWARF() const {
Fangrui Song8d85c962023-06-05 21:36:19305 return getSectionName().starts_with(".debug_") || getSectionName() == ".eh_frame";
Martin Storsjo67dd3412017-10-10 06:05:29306 }
Shoaib Meenai9a61a792017-07-18 15:11:05307
Chandler Carruth59013c32015-06-29 21:12:49308 // Allow iteration over the bodies of this chunk's relocated symbols.
309 llvm::iterator_range<symbol_iterator> symbols() const {
Rui Ueyama136d27a2019-07-11 05:40:30310 return llvm::make_range(symbol_iterator(file, relocsData),
311 symbol_iterator(file, relocsData + relocsSize));
Reid Kleckner0a1b1d62019-05-03 20:17:14312 }
313
314 ArrayRef<coff_relocation> getRelocs() const {
serge-sans-paille984b8002023-01-09 17:11:07315 return llvm::ArrayRef(relocsData, relocsSize);
Reid Kleckner0a1b1d62019-05-03 20:17:14316 }
317
318 // Reloc setter used by ARM range extension thunk insertion.
Rui Ueyama136d27a2019-07-11 05:40:30319 void setRelocs(ArrayRef<coff_relocation> newRelocs) {
320 relocsData = newRelocs.data();
321 relocsSize = newRelocs.size();
322 assert(relocsSize == newRelocs.size() && "reloc size truncation");
Chandler Carruth59013c32015-06-29 21:12:49323 }
324
Reid Klecknercc525c92019-04-02 22:11:58325 // 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 Ueyama136d27a2019-07-11 05:40:30331 AssociatedIterator(SectionChunk *head) : cur(head) {}
Rui Ueyama136d27a2019-07-11 05:40:30332 bool operator==(const AssociatedIterator &r) const { return cur == r.cur; }
Reid Klecknerfce54572020-05-02 20:28:56333 // FIXME: Wrong const-ness, but it makes filter ranges work.
334 SectionChunk &operator*() const { return *cur; }
Rui Ueyama136d27a2019-07-11 05:40:30335 SectionChunk &operator*() { return *cur; }
Reid Klecknercc525c92019-04-02 22:11:58336 AssociatedIterator &operator++() {
Rui Ueyama136d27a2019-07-11 05:40:30337 cur = cur->assocChildren;
Reid Klecknercc525c92019-04-02 22:11:58338 return *this;
339 }
340
341 private:
Rui Ueyama136d27a2019-07-11 05:40:30342 SectionChunk *cur = nullptr;
Reid Klecknercc525c92019-04-02 22:11:58343 };
344
Chandler Carruth59013c32015-06-29 21:12:49345 // Allow iteration over the associated child chunks for this section.
Reid Klecknercc525c92019-04-02 22:11:58346 llvm::iterator_range<AssociatedIterator> children() const {
Reid Kleckner18a9b182021-04-14 17:39:48347 // 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 Klecknercc525c92019-04-02 22:11:58353 }
Rui Ueyamaddf71fc2015-06-24 04:36:52354
Alexandre Ganea149de8d2018-10-05 12:56:46355 // The section ID this chunk belongs to in its Obj.
356 uint32_t getSectionNumber() const;
357
Alexandre Ganead307c4c2019-02-23 01:46:18358 ArrayRef<uint8_t> consumeDebugMagic();
359
Rui Ueyama136d27a2019-07-11 05:40:30360 static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data,
361 StringRef sectionName);
Alexandre Ganead307c4c2019-02-23 01:46:18362
Rui Ueyama136d27a2019-07-11 05:40:30363 static SectionChunk *findByName(ArrayRef<SectionChunk *> sections,
364 StringRef name);
Alexandre Ganead307c4c2019-02-23 01:46:18365
Reid Klecknercc525c92019-04-02 22:11:58366 // The file that this chunk was created from.
Rui Ueyama136d27a2019-07-11 05:40:30367 ObjFile *file;
Reid Klecknercc525c92019-04-02 22:11:58368
369 // Pointer to the COFF section header in the input file.
Rui Ueyama136d27a2019-07-11 05:40:30370 const coff_section *header;
Reid Klecknercc525c92019-04-02 22:11:58371
372 // The COMDAT leader symbol if this is a COMDAT chunk.
Rui Ueyama136d27a2019-07-11 05:40:30373 DefinedRegular *sym = nullptr;
Reid Klecknercc525c92019-04-02 22:11:58374
Reid Klecknercc525c92019-04-02 22:11:58375 // The CRC of the contents as described in the COFF spec 4.5.5.
376 // Auxiliary Format 5: Section Definitions. Used for ICF.
Rui Ueyama136d27a2019-07-11 05:40:30377 uint32_t checksum = 0;
Reid Klecknercc525c92019-04-02 22:11:58378
379 // Used by the garbage collector.
Rui Ueyama136d27a2019-07-11 05:40:30380 bool live;
Reid Klecknercc525c92019-04-02 22:11:58381
Reid Kleckner14f4ff62019-05-23 20:26:41382 // 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 Ueyama136d27a2019-07-11 05:40:30384 bool keepUnique = false;
Reid Kleckner14f4ff62019-05-23 20:26:41385
Reid Klecknercc525c92019-04-02 22:11:58386 // The COMDAT selection if this is a COMDAT chunk.
Rui Ueyama136d27a2019-07-11 05:40:30387 llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0;
Reid Klecknercc525c92019-04-02 22:11:58388
Rui Ueyamaddf71fc2015-06-24 04:36:52389 // 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 Ganea149de8d2018-10-05 12:56:46392 // and this chunk is considered as dead.
Rui Ueyama136d27a2019-07-11 05:40:30393 SectionChunk *repl;
Rui Ueyamaddf71fc2015-06-24 04:36:52394
Peter Collingbourne6f24fdb2017-01-14 03:14:46395private:
Rui Ueyama136d27a2019-07-11 05:40:30396 SectionChunk *assocChildren = nullptr;
Rui Ueyama6a60be72015-06-24 00:00:52397
Rui Ueyama92298d52015-09-16 14:19:10398 // Used for ICF (Identical COMDAT Folding)
Rui Ueyama136d27a2019-07-11 05:40:30399 void replace(SectionChunk *other);
400 uint32_t eqClass[2] = {0, 0};
Reid Kleckner0a1b1d62019-05-03 20:17:14401
402 // Relocations for this section. Size is stored below.
Rui Ueyama136d27a2019-07-11 05:40:30403 const coff_relocation *relocsData;
Reid Kleckner0a1b1d62019-05-03 20:17:14404
405 // Section name string. Size is stored below.
Rui Ueyama136d27a2019-07-11 05:40:30406 const char *sectionNameData;
Reid Kleckner0a1b1d62019-05-03 20:17:14407
Rui Ueyama136d27a2019-07-11 05:40:30408 uint32_t relocsSize = 0;
409 uint32_t sectionNameSize = 0;
Rui Ueyama411c63602015-05-28 19:09:30410};
411
Jacek Cabanfed8e382024-06-18 09:14:01412// A section chunk corresponding a section of an EC input file.
413class SectionChunkEC final : public SectionChunk {
414public:
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 Klecknera431dd72019-05-24 20:25:40422// Inline methods to implement faux-virtual dispatch for SectionChunk.
423
424inline size_t Chunk::getSize() const {
425 if (isa<SectionChunk>(this))
426 return static_cast<const SectionChunk *>(this)->getSize();
Jacek Cabane24ac112023-10-20 10:38:12427 return static_cast<const NonSectionChunk *>(this)->getSize();
Reid Klecknera431dd72019-05-24 20:25:40428}
429
430inline uint32_t Chunk::getOutputCharacteristics() const {
431 if (isa<SectionChunk>(this))
432 return static_cast<const SectionChunk *>(this)->getOutputCharacteristics();
Jacek Cabane24ac112023-10-20 10:38:12433 return static_cast<const NonSectionChunk *>(this)->getOutputCharacteristics();
Reid Klecknera431dd72019-05-24 20:25:40434}
435
Rui Ueyama136d27a2019-07-11 05:40:30436inline void Chunk::writeTo(uint8_t *buf) const {
Reid Klecknera431dd72019-05-24 20:25:40437 if (isa<SectionChunk>(this))
Rui Ueyama136d27a2019-07-11 05:40:30438 static_cast<const SectionChunk *>(this)->writeTo(buf);
Reid Klecknera431dd72019-05-24 20:25:40439 else
Rui Ueyama136d27a2019-07-11 05:40:30440 static_cast<const NonSectionChunk *>(this)->writeTo(buf);
Reid Klecknera431dd72019-05-24 20:25:40441}
442
Reid Klecknera431dd72019-05-24 20:25:40443inline StringRef Chunk::getSectionName() const {
444 if (isa<SectionChunk>(this))
445 return static_cast<const SectionChunk *>(this)->getSectionName();
Jacek Cabane24ac112023-10-20 10:38:12446 return static_cast<const NonSectionChunk *>(this)->getSectionName();
Reid Klecknera431dd72019-05-24 20:25:40447}
448
Rui Ueyama136d27a2019-07-11 05:40:30449inline void Chunk::getBaserels(std::vector<Baserel> *res) {
Reid Klecknera431dd72019-05-24 20:25:40450 if (isa<SectionChunk>(this))
Rui Ueyama136d27a2019-07-11 05:40:30451 static_cast<SectionChunk *>(this)->getBaserels(res);
Reid Klecknera431dd72019-05-24 20:25:40452 else
Rui Ueyama136d27a2019-07-11 05:40:30453 static_cast<NonSectionChunk *>(this)->getBaserels(res);
Reid Klecknera431dd72019-05-24 20:25:40454}
455
456inline StringRef Chunk::getDebugName() const {
457 if (isa<SectionChunk>(this))
458 return static_cast<const SectionChunk *>(this)->getDebugName();
Jacek Cabane24ac112023-10-20 10:38:12459 return static_cast<const NonSectionChunk *>(this)->getDebugName();
Reid Klecknera431dd72019-05-24 20:25:40460}
461
Jacek Cabancbbb5452023-10-18 11:57:42462inline MachineTypes Chunk::getMachine() const {
463 if (isa<SectionChunk>(this))
464 return static_cast<const SectionChunk *>(this)->getMachine();
Jacek Cabane24ac112023-10-20 10:38:12465 return static_cast<const NonSectionChunk *>(this)->getMachine();
Jacek Cabancbbb5452023-10-18 11:57:42466}
467
Jacek Caban8f9903d2024-04-04 12:41:50468inline llvm::Triple::ArchType Chunk::getArch() const {
469 return llvm::getMachineArchType(getMachine());
470}
471
Jacek Cabanc6054312023-11-01 15:47:43472inline 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 Cabancbbb5452023-10-18 11:57:42477 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 Collingbournef1a11f82018-03-15 21:14:02487// 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 Klecknera431dd72019-05-24 20:25:40496class MergeChunk : public NonSectionChunk {
Peter Collingbournef1a11f82018-03-15 21:14:02497public:
Rui Ueyama136d27a2019-07-11 05:40:30498 MergeChunk(uint32_t alignment);
Amy Huang6f7483b2021-09-16 23:48:26499 static void addSection(COFFLinkerContext &ctx, SectionChunk *c);
Reid Kleckner11c141e2019-05-24 00:02:00500 void finalizeContents();
501 void assignSubsectionRVAs();
Peter Collingbournef1a11f82018-03-15 21:14:02502
Peter Collingbournefa322ab2018-04-19 20:03:24503 uint32_t getOutputCharacteristics() const override;
Peter Collingbournef1a11f82018-03-15 21:14:02504 StringRef getSectionName() const override { return ".rdata"; }
505 size_t getSize() const override;
Rui Ueyama136d27a2019-07-11 05:40:30506 void writeTo(uint8_t *buf) const override;
Peter Collingbournef1a11f82018-03-15 21:14:02507
Rui Ueyama136d27a2019-07-11 05:40:30508 std::vector<SectionChunk *> sections;
Peter Collingbournef1a11f82018-03-15 21:14:02509
510private:
Rui Ueyama136d27a2019-07-11 05:40:30511 llvm::StringTableBuilder builder;
512 bool finalized = false;
Peter Collingbournef1a11f82018-03-15 21:14:02513};
514
Rui Ueyama411c63602015-05-28 19:09:30515// A chunk for common symbols. Common chunks don't have actual data.
Reid Klecknera431dd72019-05-24 20:25:40516class CommonChunk : public NonSectionChunk {
Rui Ueyama411c63602015-05-28 19:09:30517public:
Rui Ueyama136d27a2019-07-11 05:40:30518 CommonChunk(const COFFSymbolRef sym);
519 size_t getSize() const override { return sym.getValue(); }
Peter Collingbournefa322ab2018-04-19 20:03:24520 uint32_t getOutputCharacteristics() const override;
Rui Ueyama411c63602015-05-28 19:09:30521 StringRef getSectionName() const override { return ".bss"; }
522
523private:
Rui Ueyama136d27a2019-07-11 05:40:30524 const COFFSymbolRef sym;
Rui Ueyama411c63602015-05-28 19:09:30525};
526
527// A chunk for linker-created strings.
Reid Klecknera431dd72019-05-24 20:25:40528class StringChunk : public NonSectionChunk {
Rui Ueyama411c63602015-05-28 19:09:30529public:
Rui Ueyama136d27a2019-07-11 05:40:30530 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 Ueyama411c63602015-05-28 19:09:30533
534private:
Rui Ueyama136d27a2019-07-11 05:40:30535 StringRef str;
Rui Ueyama411c63602015-05-28 19:09:30536};
537
Rui Ueyama136d27a2019-07-11 05:40:30538static const uint8_t importThunkX86[] = {
Rui Ueyama411c63602015-05-28 19:09:30539 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
540};
541
Rui Ueyama136d27a2019-07-11 05:40:30542static const uint8_t importThunkARM[] = {
Rui Ueyama3dd93722015-07-25 03:39:29543 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 Ueyama136d27a2019-07-11 05:40:30548static const uint8_t importThunkARM64[] = {
Martin Storsjo27791652017-07-11 07:22:44549 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 Caban7c82b562024-09-12 13:25:12554static 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 Caban99a23542024-09-11 12:46:40560};
561
Rui Ueyama4b22fa72015-06-07 01:15:04562// Windows-specific.
Nico Weber1f3ab982019-01-14 19:05:21563// A chunk for DLL import jump table entry. In a final output, its
Rui Ueyama411c63602015-05-28 19:09:30564// contents will be a JMP instruction to some __imp_ symbol.
Jacek Caban14043d42023-11-01 12:27:29565class ImportThunkChunk : public NonSectionCodeChunk {
Reid Klecknerf612b182019-05-28 17:38:04566public:
Jacek Caban6be9be52024-09-13 13:42:05567 ImportThunkChunk(COFFLinkerContext &ctx, Defined *s);
Rui Ueyama136d27a2019-07-11 05:40:30568 static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; }
Reid Klecknerf612b182019-05-28 17:38:04569
Jacek Caban6be9be52024-09-13 13:42:05570 // We track the usage of the thunk symbol separately from the import file
571 // to avoid generating unnecessary thunks.
572 bool live;
573
Reid Klecknerf612b182019-05-28 17:38:04574protected:
Rui Ueyama136d27a2019-07-11 05:40:30575 Defined *impSymbol;
Amy Huang5a58b192023-01-10 04:37:28576 COFFLinkerContext &ctx;
Reid Klecknerf612b182019-05-28 17:38:04577};
578
579class ImportThunkChunkX64 : public ImportThunkChunk {
Rui Ueyama411c63602015-05-28 19:09:30580public:
Amy Huang5a58b192023-01-10 04:37:28581 explicit ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s);
Rui Ueyama136d27a2019-07-11 05:40:30582 size_t getSize() const override { return sizeof(importThunkX86); }
583 void writeTo(uint8_t *buf) const override;
Jacek Cabancbbb5452023-10-18 11:57:42584 MachineTypes getMachine() const override { return AMD64; }
Rui Ueyama28df0422015-07-25 01:16:06585};
586
Reid Klecknerf612b182019-05-28 17:38:04587class ImportThunkChunkX86 : public ImportThunkChunk {
Rui Ueyama28df0422015-07-25 01:16:06588public:
Amy Huang5a58b192023-01-10 04:37:28589 explicit ImportThunkChunkX86(COFFLinkerContext &ctx, Defined *s)
590 : ImportThunkChunk(ctx, s) {}
Rui Ueyama136d27a2019-07-11 05:40:30591 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 Cabancbbb5452023-10-18 11:57:42594 MachineTypes getMachine() const override { return I386; }
Rui Ueyama411c63602015-05-28 19:09:30595};
596
Reid Klecknerf612b182019-05-28 17:38:04597class ImportThunkChunkARM : public ImportThunkChunk {
Rui Ueyama3dd93722015-07-25 03:39:29598public:
Amy Huang5a58b192023-01-10 04:37:28599 explicit ImportThunkChunkARM(COFFLinkerContext &ctx, Defined *s)
600 : ImportThunkChunk(ctx, s) {
Martin Storsjö12c9e2f2020-04-08 21:42:50601 setAlignment(2);
602 }
Rui Ueyama136d27a2019-07-11 05:40:30603 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 Cabancbbb5452023-10-18 11:57:42606 MachineTypes getMachine() const override { return ARMNT; }
Rui Ueyama3dd93722015-07-25 03:39:29607};
608
Reid Klecknerf612b182019-05-28 17:38:04609class ImportThunkChunkARM64 : public ImportThunkChunk {
Martin Storsjo27791652017-07-11 07:22:44610public:
Jacek Cabanea5d37f2024-09-13 15:05:02611 explicit ImportThunkChunkARM64(COFFLinkerContext &ctx, Defined *s,
612 MachineTypes machine)
613 : ImportThunkChunk(ctx, s), machine(machine) {
Martin Storsjö12c9e2f2020-04-08 21:42:50614 setAlignment(4);
615 }
Rui Ueyama136d27a2019-07-11 05:40:30616 size_t getSize() const override { return sizeof(importThunkARM64); }
617 void writeTo(uint8_t *buf) const override;
Jacek Cabanea5d37f2024-09-13 15:05:02618 MachineTypes getMachine() const override { return machine; }
619
620private:
621 MachineTypes machine;
Martin Storsjo27791652017-07-11 07:22:44622};
623
Jacek Caban99a23542024-09-11 12:46:40624// ARM64EC __impchk_* thunk implementation.
625// Performs an indirect call to an imported function pointer
626// using the __icall_helper_arm64ec helper function.
627class ImportThunkChunkARM64EC : public ImportThunkChunk {
628public:
629 explicit ImportThunkChunkARM64EC(ImportFile *file);
Jacek Cabanf661e6952024-09-26 08:44:40630 size_t getSize() const override;
Jacek Caban99a23542024-09-11 12:46:40631 MachineTypes getMachine() const override { return ARM64EC; }
632 void writeTo(uint8_t *buf) const override;
Jacek Cabanf661e6952024-09-26 08:44:40633 bool verifyRanges() override;
634 uint32_t extendRanges() override;
Jacek Caban99a23542024-09-11 12:46:40635
636 Defined *exitThunk;
Jacek Caban486f7902024-09-19 11:47:22637 Defined *sym = nullptr;
Jacek Cabanf661e6952024-09-26 08:44:40638 bool extended = false;
Jacek Caban99a23542024-09-11 12:46:40639
640private:
641 ImportFile *file;
642};
643
Jacek Caban14043d42023-11-01 12:27:29644class RangeExtensionThunkARM : public NonSectionCodeChunk {
Martin Storsjo57ddec02018-09-25 10:59:29645public:
Amy Huang5a58b192023-01-10 04:37:28646 explicit RangeExtensionThunkARM(COFFLinkerContext &ctx, Defined *t)
647 : target(t), ctx(ctx) {
648 setAlignment(2);
649 }
Martin Storsjoc9f4d25f2019-02-01 22:08:09650 size_t getSize() const override;
Rui Ueyama136d27a2019-07-11 05:40:30651 void writeTo(uint8_t *buf) const override;
Jacek Cabancbbb5452023-10-18 11:57:42652 MachineTypes getMachine() const override { return ARMNT; }
Martin Storsjoc9f4d25f2019-02-01 22:08:09653
Rui Ueyama136d27a2019-07-11 05:40:30654 Defined *target;
Amy Huang5a58b192023-01-10 04:37:28655
656private:
657 COFFLinkerContext &ctx;
Martin Storsjoc9f4d25f2019-02-01 22:08:09658};
659
Jacek Cabanefad5612024-08-29 08:19:32660// A ragnge extension thunk used for both ARM64EC and ARM64 machine types.
Jacek Caban14043d42023-11-01 12:27:29661class RangeExtensionThunkARM64 : public NonSectionCodeChunk {
Martin Storsjoc9f4d25f2019-02-01 22:08:09662public:
Jacek Cabanefad5612024-08-29 08:19:32663 explicit RangeExtensionThunkARM64(MachineTypes machine, Defined *t)
664 : target(t), machine(machine) {
Amy Huang5a58b192023-01-10 04:37:28665 setAlignment(4);
Jacek Cabanefad5612024-08-29 08:19:32666 assert(llvm::COFF::isAnyArm64(machine));
Amy Huang5a58b192023-01-10 04:37:28667 }
Martin Storsjo57ddec02018-09-25 10:59:29668 size_t getSize() const override;
Rui Ueyama136d27a2019-07-11 05:40:30669 void writeTo(uint8_t *buf) const override;
Jacek Cabanefad5612024-08-29 08:19:32670 MachineTypes getMachine() const override { return machine; }
Martin Storsjo57ddec02018-09-25 10:59:29671
Rui Ueyama136d27a2019-07-11 05:40:30672 Defined *target;
Amy Huang5a58b192023-01-10 04:37:28673
674private:
Jacek Cabanefad5612024-08-29 08:19:32675 MachineTypes machine;
Martin Storsjo57ddec02018-09-25 10:59:29676};
677
Rui Ueyama588e8322015-06-15 01:23:58678// Windows-specific.
Rui Ueyama88e0f922015-06-25 03:31:47679// See comments for DefinedLocalImport class.
Reid Klecknera431dd72019-05-24 20:25:40680class LocalImportChunk : public NonSectionChunk {
Rui Ueyama88e0f922015-06-25 03:31:47681public:
Amy Huang5a58b192023-01-10 04:37:28682 explicit LocalImportChunk(COFFLinkerContext &ctx, Defined *s);
Rui Ueyamad4b351f2015-07-09 21:15:58683 size_t getSize() const override;
Rui Ueyama136d27a2019-07-11 05:40:30684 void getBaserels(std::vector<Baserel> *res) override;
685 void writeTo(uint8_t *buf) const override;
Rui Ueyama88e0f922015-06-25 03:31:47686
687private:
Rui Ueyama136d27a2019-07-11 05:40:30688 Defined *sym;
Amy Huang5a58b192023-01-10 04:37:28689 COFFLinkerContext &ctx;
Rui Ueyama88e0f922015-06-25 03:31:47690};
691
Reid Kleckneraf2f7da2018-02-06 01:58:26692// 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.
695struct ChunkAndOffset {
Rui Ueyama136d27a2019-07-11 05:40:30696 Chunk *inputChunk;
697 uint32_t offset;
Reid Kleckneraf2f7da2018-02-06 01:58:26698
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 Ueyama136d27a2019-07-11 05:40:30706 static unsigned getHashValue(const ChunkAndOffset &co) {
Reid Kleckneraf2f7da2018-02-06 01:58:26707 return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue(
Rui Ueyama136d27a2019-07-11 05:40:30708 {co.inputChunk, co.offset});
Reid Kleckneraf2f7da2018-02-06 01:58:26709 }
Rui Ueyama136d27a2019-07-11 05:40:30710 static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) {
711 return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset;
Reid Kleckneraf2f7da2018-02-06 01:58:26712 }
713 };
714};
715
716using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>;
717
718// Table which contains symbol RVAs. Used for /safeseh and /guard:cf.
Reid Klecknera431dd72019-05-24 20:25:40719class RVATableChunk : public NonSectionChunk {
Rui Ueyamacd3f99b2015-07-24 23:51:14720public:
Rui Ueyama136d27a2019-07-11 05:40:30721 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 Ueyamacd3f99b2015-07-24 23:51:14724
725private:
Rui Ueyama136d27a2019-07-11 05:40:30726 SymbolRVASet syms;
Rui Ueyamacd3f99b2015-07-24 23:51:14727};
728
Pengfei Wang184377d2021-04-14 06:21:52729// Table which contains symbol RVAs with flags. Used for /guard:ehcont.
730class RVAFlagTableChunk : public NonSectionChunk {
731public:
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
736private:
737 SymbolRVASet syms;
738};
739
Rui Ueyamacd3f99b2015-07-24 23:51:14740// Windows-specific.
Rui Ueyama588e8322015-06-15 01:23:58741// This class represents a block in .reloc section.
742// See the PE/COFF spec 5.6 for details.
Reid Klecknera431dd72019-05-24 20:25:40743class BaserelChunk : public NonSectionChunk {
Rui Ueyama588e8322015-06-15 01:23:58744public:
Rui Ueyama136d27a2019-07-11 05:40:30745 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 Ueyama588e8322015-06-15 01:23:58748
749private:
Rui Ueyama136d27a2019-07-11 05:40:30750 std::vector<uint8_t> data;
Rui Ueyama588e8322015-06-15 01:23:58751};
752
Rui Ueyama3afd5bf2015-07-25 01:44:32753class Baserel {
754public:
Rui Ueyama136d27a2019-07-11 05:40:30755 Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {}
Amy Huang5a58b192023-01-10 04:37:28756 explicit Baserel(uint32_t v, llvm::COFF::MachineTypes machine)
757 : Baserel(v, getDefaultType(machine)) {}
Jacek Caban233ed512024-09-05 13:57:20758 static uint8_t getDefaultType(llvm::COFF::MachineTypes machine);
Rui Ueyama3afd5bf2015-07-25 01:44:32759
Rui Ueyama136d27a2019-07-11 05:40:30760 uint32_t rva;
761 uint8_t type;
Rui Ueyama3afd5bf2015-07-25 01:44:32762};
763
Martin Storsjoeac1b052018-08-27 08:43:31764// 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 Klecknera431dd72019-05-24 20:25:40768class EmptyChunk : public NonSectionChunk {
Martin Storsjoeac1b052018-08-27 08:43:31769public:
770 EmptyChunk() {}
771 size_t getSize() const override { return 0; }
Rui Ueyama136d27a2019-07-11 05:40:30772 void writeTo(uint8_t *buf) const override {}
Martin Storsjoeac1b052018-08-27 08:43:31773};
774
Jacek Cabanfe2bd122023-11-15 11:35:45775class ECCodeMapEntry {
776public:
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.
786class ECCodeMapChunk : public NonSectionChunk {
787public:
788 ECCodeMapChunk(std::vector<ECCodeMapEntry> &map) : map(map) {}
789 size_t getSize() const override;
790 void writeTo(uint8_t *buf) const override;
791
792private:
793 std::vector<ECCodeMapEntry> &map;
794};
795
Jacek Caban52a71162024-08-23 19:17:38796class CHPECodeRangesChunk : public NonSectionChunk {
797public:
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
803private:
804 std::vector<std::pair<Chunk *, Defined *>> &exportThunks;
805};
806
Jacek Cabancaa844e2024-08-23 18:29:19807class CHPERedirectionChunk : public NonSectionChunk {
808public:
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
814private:
815 std::vector<std::pair<Chunk *, Defined *>> &exportThunks;
816};
817
Jacek Cabana2d87432024-08-22 20:03:05818static 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
828class ECExportThunkChunk : public NonSectionCodeChunk {
829public:
Jacek Cabanc53e5272025-03-07 17:34:56830 explicit ECExportThunkChunk(Defined *targetSym)
831 : NonSectionCodeChunk(ECExportThunkKind), target(targetSym) {}
832 static bool classof(const Chunk *c) { return c->kind() == ECExportThunkKind; }
833
Jacek Cabana2d87432024-08-22 20:03:05834 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 Caban3b0dafff2025-01-10 20:50:07841// ARM64X relocation value, potentially relative to a symbol.
842class Arm64XRelocVal {
843public:
844 Arm64XRelocVal(uint64_t value = 0) : value(value) {}
845 Arm64XRelocVal(Defined *sym, int32_t offset = 0) : sym(sym), value(offset) {}
Jacek Caban659e66e2025-01-21 21:24:00846 Arm64XRelocVal(Chunk *chunk, int32_t offset = 0)
847 : chunk(chunk), value(offset) {}
Jacek Caban3b0dafff2025-01-10 20:50:07848 uint64_t get() const;
849
850private:
851 Defined *sym = nullptr;
Jacek Caban659e66e2025-01-21 21:24:00852 Chunk *chunk = nullptr;
Jacek Caban3b0dafff2025-01-10 20:50:07853 uint64_t value;
854};
855
Jacek Caban71bbafb2024-12-05 12:07:41856// ARM64X entry for dynamic relocations.
857class Arm64XDynamicRelocEntry {
858public:
859 Arm64XDynamicRelocEntry(llvm::COFF::Arm64XFixupType type, uint8_t size,
Jacek Cabana16adaf2025-01-20 10:38:54860 Arm64XRelocVal offset, Arm64XRelocVal value)
Jacek Caban71bbafb2024-12-05 12:07:41861 : offset(offset), value(value), type(type), size(size) {}
862
863 size_t getSize() const;
864 void writeTo(uint8_t *buf) const;
865
Jacek Cabana16adaf2025-01-20 10:38:54866 Arm64XRelocVal offset;
Jacek Caban3b0dafff2025-01-10 20:50:07867 Arm64XRelocVal value;
Jacek Caban71bbafb2024-12-05 12:07:41868
869private:
870 llvm::COFF::Arm64XFixupType type;
871 uint8_t size;
872};
873
874// Dynamic relocation chunk containing ARM64X relocations for the hybrid image.
875class DynamicRelocsChunk : public NonSectionChunk {
876public:
877 DynamicRelocsChunk() {}
878 size_t getSize() const override { return size; }
879 void writeTo(uint8_t *buf) const override;
880 void finalize();
881
Jacek Cabana16adaf2025-01-20 10:38:54882 void add(llvm::COFF::Arm64XFixupType type, uint8_t size,
Jacek Caban659e66e2025-01-21 21:24:00883 Arm64XRelocVal offset, Arm64XRelocVal value = Arm64XRelocVal()) {
Jacek Caban71bbafb2024-12-05 12:07:41884 arm64xRelocs.emplace_back(type, size, offset, value);
885 }
886
Jacek Caban659e66e2025-01-21 21:24:00887 void set(uint32_t rva, Arm64XRelocVal value);
888
Jacek Caban71bbafb2024-12-05 12:07:41889private:
890 std::vector<Arm64XDynamicRelocEntry> arm64xRelocs;
891 size_t size;
892};
893
Martin Storsjoeac1b052018-08-27 08:43:31894// 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 Klecknera431dd72019-05-24 20:25:40900class PseudoRelocTableChunk : public NonSectionChunk {
Martin Storsjoeac1b052018-08-27 08:43:31901public:
Rui Ueyama136d27a2019-07-11 05:40:30902 PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs)
903 : relocs(std::move(relocs)) {
Reid Kleckneree4e0a22019-05-22 20:21:52904 setAlignment(4);
Martin Storsjoeac1b052018-08-27 08:43:31905 }
906 size_t getSize() const override;
Rui Ueyama136d27a2019-07-11 05:40:30907 void writeTo(uint8_t *buf) const override;
Martin Storsjoeac1b052018-08-27 08:43:31908
909private:
Rui Ueyama136d27a2019-07-11 05:40:30910 std::vector<RuntimePseudoReloc> relocs;
Martin Storsjoeac1b052018-08-27 08:43:31911};
912
Martin Storsjo7a416932018-09-14 22:26:59913// MinGW specific. A Chunk that contains one pointer-sized absolute value.
Reid Klecknera431dd72019-05-24 20:25:40914class AbsolutePointerChunk : public NonSectionChunk {
Martin Storsjo7a416932018-09-14 22:26:59915public:
Jacek Cabana92bfaa2025-02-17 20:34:12916 AbsolutePointerChunk(SymbolTable &symtab, uint64_t value)
917 : value(value), symtab(symtab) {
Reid Kleckneree4e0a22019-05-22 20:21:52918 setAlignment(getSize());
Martin Storsjo7a416932018-09-14 22:26:59919 }
920 size_t getSize() const override;
Rui Ueyama136d27a2019-07-11 05:40:30921 void writeTo(uint8_t *buf) const override;
Jacek Cabana92bfaa2025-02-17 20:34:12922 MachineTypes getMachine() const override;
Martin Storsjo7a416932018-09-14 22:26:59923
924private:
Rui Ueyama136d27a2019-07-11 05:40:30925 uint64_t value;
Jacek Cabana92bfaa2025-02-17 20:34:12926 SymbolTable &symtab;
Martin Storsjo7a416932018-09-14 22:26:59927};
928
Reid Klecknerf612b182019-05-28 17:38:04929// 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.
932inline bool Chunk::isHotPatchable() const {
Rui Ueyama136d27a2019-07-11 05:40:30933 if (auto *sc = dyn_cast<SectionChunk>(this))
934 return sc->file->hotPatchable;
Reid Klecknerf612b182019-05-28 17:38:04935 else if (isa<ImportThunkChunk>(this))
936 return true;
937 return false;
938}
939
Jacek Cabanfed8e382024-06-18 09:14:01940inline Defined *Chunk::getEntryThunk() const {
941 if (auto *c = dyn_cast<const SectionChunkEC>(this))
942 return c->entryThunk;
943 return nullptr;
944}
945
946inline void Chunk::setEntryThunk(Defined *entryThunk) {
947 if (auto c = dyn_cast<SectionChunkEC>(this))
948 c->entryThunk = entryThunk;
949}
950
Rui Ueyama136d27a2019-07-11 05:40:30951void applyMOV32T(uint8_t *off, uint32_t v);
952void applyBranch24T(uint8_t *off, int32_t v);
Martin Storsjo82eaf6c2017-07-25 20:00:37953
Rui Ueyama136d27a2019-07-11 05:40:30954void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift);
955void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit);
956void applyArm64Branch26(uint8_t *off, int64_t v);
Martin Storsjo32d21d62018-09-18 07:22:01957
Andrew Ng85a2f292023-01-10 14:03:48958// Convenience class for initializing a coff_section with specific flags.
959class FakeSection {
960public:
961 FakeSection(int c) { section.Characteristics = c; }
962
963 coff_section section;
964};
965
Amy Huang5a58b192023-01-10 04:37:28966// Convenience class for initializing a SectionChunk with specific flags.
967class FakeSectionChunk {
968public:
Andrew Ng85a2f292023-01-10 14:03:48969 FakeSectionChunk(const coff_section *section) : chunk(nullptr, section) {
Amy Huang5a58b192023-01-10 04:37:28970 // 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 Huang5a58b192023-01-10 04:37:28976 SectionChunk chunk;
977};
978
Nico Weber7c266412022-08-08 15:32:26979} // namespace lld::coff
Rui Ueyama411c63602015-05-28 19:09:30980
Reid Kleckneraf2f7da2018-02-06 01:58:26981namespace llvm {
982template <>
983struct DenseMapInfo<lld::coff::ChunkAndOffset>
984 : lld::coff::ChunkAndOffset::DenseMapInfo {};
985}
986
Rui Ueyama411c63602015-05-28 19:09:30987#endif