Chunks.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. //===- Chunks.h -------------------------------------------------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLD_COFF_CHUNKS_H
  9. #define LLD_COFF_CHUNKS_H
  10. #include "Config.h"
  11. #include "InputFiles.h"
  12. #include "lld/Common/LLVM.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/ADT/PointerIntPair.h"
  15. #include "llvm/ADT/iterator.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/MC/StringTableBuilder.h"
  18. #include "llvm/Object/COFF.h"
  19. #include <utility>
  20. #include <vector>
  21. namespace lld {
  22. namespace coff {
  23. using llvm::COFF::ImportDirectoryTableEntry;
  24. using llvm::object::COFFSymbolRef;
  25. using llvm::object::SectionRef;
  26. using llvm::object::coff_relocation;
  27. using llvm::object::coff_section;
  28. class Baserel;
  29. class Defined;
  30. class DefinedImportData;
  31. class DefinedRegular;
  32. class ObjFile;
  33. class OutputSection;
  34. class RuntimePseudoReloc;
  35. class Symbol;
  36. // Mask for permissions (discardable, writable, readable, executable, etc).
  37. const uint32_t permMask = 0xFE000000;
  38. // Mask for section types (code, data, bss).
  39. const uint32_t typeMask = 0x000000E0;
  40. // The log base 2 of the largest section alignment, which is log2(8192), or 13.
  41. enum : unsigned { Log2MaxSectionAlignment = 13 };
  42. // A Chunk represents a chunk of data that will occupy space in the
  43. // output (if the resolver chose that). It may or may not be backed by
  44. // a section of an input file. It could be linker-created data, or
  45. // doesn't even have actual data (if common or bss).
  46. class Chunk {
  47. public:
  48. enum Kind : uint8_t { SectionKind, OtherKind, ImportThunkKind };
  49. Kind kind() const { return chunkKind; }
  50. // Returns the size of this chunk (even if this is a common or BSS.)
  51. size_t getSize() const;
  52. // Returns chunk alignment in power of two form. Value values are powers of
  53. // two from 1 to 8192.
  54. uint32_t getAlignment() const { return 1U << p2Align; }
  55. // Update the chunk section alignment measured in bytes. Internally alignment
  56. // is stored in log2.
  57. void setAlignment(uint32_t align) {
  58. // Treat zero byte alignment as 1 byte alignment.
  59. align = align ? align : 1;
  60. assert(llvm::isPowerOf2_32(align) && "alignment is not a power of 2");
  61. p2Align = llvm::Log2_32(align);
  62. assert(p2Align <= Log2MaxSectionAlignment &&
  63. "impossible requested alignment");
  64. }
  65. // Write this chunk to a mmap'ed file, assuming Buf is pointing to
  66. // beginning of the file. Because this function may use RVA values
  67. // of other chunks for relocations, you need to set them properly
  68. // before calling this function.
  69. void writeTo(uint8_t *buf) const;
  70. // The writer sets and uses the addresses. In practice, PE images cannot be
  71. // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs
  72. // can be stored with 32 bits.
  73. uint32_t getRVA() const { return rva; }
  74. void setRVA(uint64_t v) {
  75. rva = (uint32_t)v;
  76. assert(rva == v && "RVA truncated");
  77. }
  78. // Returns readable/writable/executable bits.
  79. uint32_t getOutputCharacteristics() const;
  80. // Returns the section name if this is a section chunk.
  81. // It is illegal to call this function on non-section chunks.
  82. StringRef getSectionName() const;
  83. // An output section has pointers to chunks in the section, and each
  84. // chunk has a back pointer to an output section.
  85. void setOutputSectionIdx(uint16_t o) { osidx = o; }
  86. uint16_t getOutputSectionIdx() const { return osidx; }
  87. OutputSection *getOutputSection() const;
  88. // Windows-specific.
  89. // Collect all locations that contain absolute addresses for base relocations.
  90. void getBaserels(std::vector<Baserel> *res);
  91. // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
  92. // bytes, so this is used only for logging or debugging.
  93. StringRef getDebugName() const;
  94. // Return true if this file has the hotpatch flag set to true in the
  95. // S_COMPILE3 record in codeview debug info. Also returns true for some thunks
  96. // synthesized by the linker.
  97. bool isHotPatchable() const;
  98. protected:
  99. Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {}
  100. const Kind chunkKind;
  101. public:
  102. // Returns true if this has non-zero data. BSS chunks return
  103. // false. If false is returned, the space occupied by this chunk
  104. // will be filled with zeros. Corresponds to the
  105. // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit.
  106. uint8_t hasData : 1;
  107. public:
  108. // The alignment of this chunk, stored in log2 form. The writer uses the
  109. // value.
  110. uint8_t p2Align : 7;
  111. // The output section index for this chunk. The first valid section number is
  112. // one.
  113. uint16_t osidx = 0;
  114. // The RVA of this chunk in the output. The writer sets a value.
  115. uint32_t rva = 0;
  116. };
  117. class NonSectionChunk : public Chunk {
  118. public:
  119. virtual ~NonSectionChunk() = default;
  120. // Returns the size of this chunk (even if this is a common or BSS.)
  121. virtual size_t getSize() const = 0;
  122. virtual uint32_t getOutputCharacteristics() const { return 0; }
  123. // Write this chunk to a mmap'ed file, assuming Buf is pointing to
  124. // beginning of the file. Because this function may use RVA values
  125. // of other chunks for relocations, you need to set them properly
  126. // before calling this function.
  127. virtual void writeTo(uint8_t *buf) const {}
  128. // Returns the section name if this is a section chunk.
  129. // It is illegal to call this function on non-section chunks.
  130. virtual StringRef getSectionName() const {
  131. llvm_unreachable("unimplemented getSectionName");
  132. }
  133. // Windows-specific.
  134. // Collect all locations that contain absolute addresses for base relocations.
  135. virtual void getBaserels(std::vector<Baserel> *res) {}
  136. // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
  137. // bytes, so this is used only for logging or debugging.
  138. virtual StringRef getDebugName() const { return ""; }
  139. static bool classof(const Chunk *c) { return c->kind() != SectionKind; }
  140. protected:
  141. NonSectionChunk(Kind k = OtherKind) : Chunk(k) {}
  142. };
  143. // A chunk corresponding a section of an input file.
  144. class SectionChunk final : public Chunk {
  145. // Identical COMDAT Folding feature accesses section internal data.
  146. friend class ICF;
  147. public:
  148. class symbol_iterator : public llvm::iterator_adaptor_base<
  149. symbol_iterator, const coff_relocation *,
  150. std::random_access_iterator_tag, Symbol *> {
  151. friend SectionChunk;
  152. ObjFile *file;
  153. symbol_iterator(ObjFile *file, const coff_relocation *i)
  154. : symbol_iterator::iterator_adaptor_base(i), file(file) {}
  155. public:
  156. symbol_iterator() = default;
  157. Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); }
  158. };
  159. SectionChunk(ObjFile *file, const coff_section *header);
  160. static bool classof(const Chunk *c) { return c->kind() == SectionKind; }
  161. size_t getSize() const { return header->SizeOfRawData; }
  162. ArrayRef<uint8_t> getContents() const;
  163. void writeTo(uint8_t *buf) const;
  164. uint32_t getOutputCharacteristics() const {
  165. return header->Characteristics & (permMask | typeMask);
  166. }
  167. StringRef getSectionName() const {
  168. return StringRef(sectionNameData, sectionNameSize);
  169. }
  170. void getBaserels(std::vector<Baserel> *res);
  171. bool isCOMDAT() const;
  172. void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
  173. uint64_t p) const;
  174. void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
  175. uint64_t p) const;
  176. void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
  177. uint64_t p) const;
  178. void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
  179. uint64_t p) const;
  180. void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res);
  181. // Called if the garbage collector decides to not include this chunk
  182. // in a final output. It's supposed to print out a log message to stdout.
  183. void printDiscardedMessage() const;
  184. // Adds COMDAT associative sections to this COMDAT section. A chunk
  185. // and its children are treated as a group by the garbage collector.
  186. void addAssociative(SectionChunk *child);
  187. StringRef getDebugName() const;
  188. // True if this is a codeview debug info chunk. These will not be laid out in
  189. // the image. Instead they will end up in the PDB, if one is requested.
  190. bool isCodeView() const {
  191. return getSectionName() == ".debug" || getSectionName().startswith(".debug$");
  192. }
  193. // True if this is a DWARF debug info or exception handling chunk.
  194. bool isDWARF() const {
  195. return getSectionName().startswith(".debug_") || getSectionName() == ".eh_frame";
  196. }
  197. // Allow iteration over the bodies of this chunk's relocated symbols.
  198. llvm::iterator_range<symbol_iterator> symbols() const {
  199. return llvm::make_range(symbol_iterator(file, relocsData),
  200. symbol_iterator(file, relocsData + relocsSize));
  201. }
  202. ArrayRef<coff_relocation> getRelocs() const {
  203. return llvm::makeArrayRef(relocsData, relocsSize);
  204. }
  205. // Reloc setter used by ARM range extension thunk insertion.
  206. void setRelocs(ArrayRef<coff_relocation> newRelocs) {
  207. relocsData = newRelocs.data();
  208. relocsSize = newRelocs.size();
  209. assert(relocsSize == newRelocs.size() && "reloc size truncation");
  210. }
  211. // Single linked list iterator for associated comdat children.
  212. class AssociatedIterator
  213. : public llvm::iterator_facade_base<
  214. AssociatedIterator, std::forward_iterator_tag, SectionChunk> {
  215. public:
  216. AssociatedIterator() = default;
  217. AssociatedIterator(SectionChunk *head) : cur(head) {}
  218. AssociatedIterator &operator=(const AssociatedIterator &r) {
  219. cur = r.cur;
  220. return *this;
  221. }
  222. bool operator==(const AssociatedIterator &r) const { return cur == r.cur; }
  223. const SectionChunk &operator*() const { return *cur; }
  224. SectionChunk &operator*() { return *cur; }
  225. AssociatedIterator &operator++() {
  226. cur = cur->assocChildren;
  227. return *this;
  228. }
  229. private:
  230. SectionChunk *cur = nullptr;
  231. };
  232. // Allow iteration over the associated child chunks for this section.
  233. llvm::iterator_range<AssociatedIterator> children() const {
  234. return llvm::make_range(AssociatedIterator(assocChildren),
  235. AssociatedIterator(nullptr));
  236. }
  237. // The section ID this chunk belongs to in its Obj.
  238. uint32_t getSectionNumber() const;
  239. ArrayRef<uint8_t> consumeDebugMagic();
  240. static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data,
  241. StringRef sectionName);
  242. static SectionChunk *findByName(ArrayRef<SectionChunk *> sections,
  243. StringRef name);
  244. // The file that this chunk was created from.
  245. ObjFile *file;
  246. // Pointer to the COFF section header in the input file.
  247. const coff_section *header;
  248. // The COMDAT leader symbol if this is a COMDAT chunk.
  249. DefinedRegular *sym = nullptr;
  250. // The CRC of the contents as described in the COFF spec 4.5.5.
  251. // Auxiliary Format 5: Section Definitions. Used for ICF.
  252. uint32_t checksum = 0;
  253. // Used by the garbage collector.
  254. bool live;
  255. // Whether this section needs to be kept distinct from other sections during
  256. // ICF. This is set by the driver using address-significance tables.
  257. bool keepUnique = false;
  258. // The COMDAT selection if this is a COMDAT chunk.
  259. llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0;
  260. // A pointer pointing to a replacement for this chunk.
  261. // Initially it points to "this" object. If this chunk is merged
  262. // with other chunk by ICF, it points to another chunk,
  263. // and this chunk is considered as dead.
  264. SectionChunk *repl;
  265. private:
  266. SectionChunk *assocChildren = nullptr;
  267. // Used for ICF (Identical COMDAT Folding)
  268. void replace(SectionChunk *other);
  269. uint32_t eqClass[2] = {0, 0};
  270. // Relocations for this section. Size is stored below.
  271. const coff_relocation *relocsData;
  272. // Section name string. Size is stored below.
  273. const char *sectionNameData;
  274. uint32_t relocsSize = 0;
  275. uint32_t sectionNameSize = 0;
  276. };
  277. // Inline methods to implement faux-virtual dispatch for SectionChunk.
  278. inline size_t Chunk::getSize() const {
  279. if (isa<SectionChunk>(this))
  280. return static_cast<const SectionChunk *>(this)->getSize();
  281. else
  282. return static_cast<const NonSectionChunk *>(this)->getSize();
  283. }
  284. inline uint32_t Chunk::getOutputCharacteristics() const {
  285. if (isa<SectionChunk>(this))
  286. return static_cast<const SectionChunk *>(this)->getOutputCharacteristics();
  287. else
  288. return static_cast<const NonSectionChunk *>(this)
  289. ->getOutputCharacteristics();
  290. }
  291. inline void Chunk::writeTo(uint8_t *buf) const {
  292. if (isa<SectionChunk>(this))
  293. static_cast<const SectionChunk *>(this)->writeTo(buf);
  294. else
  295. static_cast<const NonSectionChunk *>(this)->writeTo(buf);
  296. }
  297. inline StringRef Chunk::getSectionName() const {
  298. if (isa<SectionChunk>(this))
  299. return static_cast<const SectionChunk *>(this)->getSectionName();
  300. else
  301. return static_cast<const NonSectionChunk *>(this)->getSectionName();
  302. }
  303. inline void Chunk::getBaserels(std::vector<Baserel> *res) {
  304. if (isa<SectionChunk>(this))
  305. static_cast<SectionChunk *>(this)->getBaserels(res);
  306. else
  307. static_cast<NonSectionChunk *>(this)->getBaserels(res);
  308. }
  309. inline StringRef Chunk::getDebugName() const {
  310. if (isa<SectionChunk>(this))
  311. return static_cast<const SectionChunk *>(this)->getDebugName();
  312. else
  313. return static_cast<const NonSectionChunk *>(this)->getDebugName();
  314. }
  315. // This class is used to implement an lld-specific feature (not implemented in
  316. // MSVC) that minimizes the output size by finding string literals sharing tail
  317. // parts and merging them.
  318. //
  319. // If string tail merging is enabled and a section is identified as containing a
  320. // string literal, it is added to a MergeChunk with an appropriate alignment.
  321. // The MergeChunk then tail merges the strings using the StringTableBuilder
  322. // class and assigns RVAs and section offsets to each of the member chunks based
  323. // on the offsets assigned by the StringTableBuilder.
  324. class MergeChunk : public NonSectionChunk {
  325. public:
  326. MergeChunk(uint32_t alignment);
  327. static void addSection(SectionChunk *c);
  328. void finalizeContents();
  329. void assignSubsectionRVAs();
  330. uint32_t getOutputCharacteristics() const override;
  331. StringRef getSectionName() const override { return ".rdata"; }
  332. size_t getSize() const override;
  333. void writeTo(uint8_t *buf) const override;
  334. static MergeChunk *instances[Log2MaxSectionAlignment + 1];
  335. std::vector<SectionChunk *> sections;
  336. private:
  337. llvm::StringTableBuilder builder;
  338. bool finalized = false;
  339. };
  340. // A chunk for common symbols. Common chunks don't have actual data.
  341. class CommonChunk : public NonSectionChunk {
  342. public:
  343. CommonChunk(const COFFSymbolRef sym);
  344. size_t getSize() const override { return sym.getValue(); }
  345. uint32_t getOutputCharacteristics() const override;
  346. StringRef getSectionName() const override { return ".bss"; }
  347. private:
  348. const COFFSymbolRef sym;
  349. };
  350. // A chunk for linker-created strings.
  351. class StringChunk : public NonSectionChunk {
  352. public:
  353. explicit StringChunk(StringRef s) : str(s) {}
  354. size_t getSize() const override { return str.size() + 1; }
  355. void writeTo(uint8_t *buf) const override;
  356. private:
  357. StringRef str;
  358. };
  359. static const uint8_t importThunkX86[] = {
  360. 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
  361. };
  362. static const uint8_t importThunkARM[] = {
  363. 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
  364. 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
  365. 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
  366. };
  367. static const uint8_t importThunkARM64[] = {
  368. 0x10, 0x00, 0x00, 0x90, // adrp x16, #0
  369. 0x10, 0x02, 0x40, 0xf9, // ldr x16, [x16]
  370. 0x00, 0x02, 0x1f, 0xd6, // br x16
  371. };
  372. // Windows-specific.
  373. // A chunk for DLL import jump table entry. In a final output, its
  374. // contents will be a JMP instruction to some __imp_ symbol.
  375. class ImportThunkChunk : public NonSectionChunk {
  376. public:
  377. ImportThunkChunk(Defined *s)
  378. : NonSectionChunk(ImportThunkKind), impSymbol(s) {}
  379. static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; }
  380. protected:
  381. Defined *impSymbol;
  382. };
  383. class ImportThunkChunkX64 : public ImportThunkChunk {
  384. public:
  385. explicit ImportThunkChunkX64(Defined *s);
  386. size_t getSize() const override { return sizeof(importThunkX86); }
  387. void writeTo(uint8_t *buf) const override;
  388. };
  389. class ImportThunkChunkX86 : public ImportThunkChunk {
  390. public:
  391. explicit ImportThunkChunkX86(Defined *s) : ImportThunkChunk(s) {}
  392. size_t getSize() const override { return sizeof(importThunkX86); }
  393. void getBaserels(std::vector<Baserel> *res) override;
  394. void writeTo(uint8_t *buf) const override;
  395. };
  396. class ImportThunkChunkARM : public ImportThunkChunk {
  397. public:
  398. explicit ImportThunkChunkARM(Defined *s) : ImportThunkChunk(s) {}
  399. size_t getSize() const override { return sizeof(importThunkARM); }
  400. void getBaserels(std::vector<Baserel> *res) override;
  401. void writeTo(uint8_t *buf) const override;
  402. };
  403. class ImportThunkChunkARM64 : public ImportThunkChunk {
  404. public:
  405. explicit ImportThunkChunkARM64(Defined *s) : ImportThunkChunk(s) {}
  406. size_t getSize() const override { return sizeof(importThunkARM64); }
  407. void writeTo(uint8_t *buf) const override;
  408. };
  409. class RangeExtensionThunkARM : public NonSectionChunk {
  410. public:
  411. explicit RangeExtensionThunkARM(Defined *t) : target(t) {}
  412. size_t getSize() const override;
  413. void writeTo(uint8_t *buf) const override;
  414. Defined *target;
  415. };
  416. class RangeExtensionThunkARM64 : public NonSectionChunk {
  417. public:
  418. explicit RangeExtensionThunkARM64(Defined *t) : target(t) {}
  419. size_t getSize() const override;
  420. void writeTo(uint8_t *buf) const override;
  421. Defined *target;
  422. };
  423. // Windows-specific.
  424. // See comments for DefinedLocalImport class.
  425. class LocalImportChunk : public NonSectionChunk {
  426. public:
  427. explicit LocalImportChunk(Defined *s) : sym(s) {
  428. setAlignment(config->wordsize);
  429. }
  430. size_t getSize() const override;
  431. void getBaserels(std::vector<Baserel> *res) override;
  432. void writeTo(uint8_t *buf) const override;
  433. private:
  434. Defined *sym;
  435. };
  436. // Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and
  437. // offset into the chunk. Order does not matter as the RVA table will be sorted
  438. // later.
  439. struct ChunkAndOffset {
  440. Chunk *inputChunk;
  441. uint32_t offset;
  442. struct DenseMapInfo {
  443. static ChunkAndOffset getEmptyKey() {
  444. return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0};
  445. }
  446. static ChunkAndOffset getTombstoneKey() {
  447. return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0};
  448. }
  449. static unsigned getHashValue(const ChunkAndOffset &co) {
  450. return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue(
  451. {co.inputChunk, co.offset});
  452. }
  453. static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) {
  454. return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset;
  455. }
  456. };
  457. };
  458. using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>;
  459. // Table which contains symbol RVAs. Used for /safeseh and /guard:cf.
  460. class RVATableChunk : public NonSectionChunk {
  461. public:
  462. explicit RVATableChunk(SymbolRVASet s) : syms(std::move(s)) {}
  463. size_t getSize() const override { return syms.size() * 4; }
  464. void writeTo(uint8_t *buf) const override;
  465. private:
  466. SymbolRVASet syms;
  467. };
  468. // Windows-specific.
  469. // This class represents a block in .reloc section.
  470. // See the PE/COFF spec 5.6 for details.
  471. class BaserelChunk : public NonSectionChunk {
  472. public:
  473. BaserelChunk(uint32_t page, Baserel *begin, Baserel *end);
  474. size_t getSize() const override { return data.size(); }
  475. void writeTo(uint8_t *buf) const override;
  476. private:
  477. std::vector<uint8_t> data;
  478. };
  479. class Baserel {
  480. public:
  481. Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {}
  482. explicit Baserel(uint32_t v) : Baserel(v, getDefaultType()) {}
  483. uint8_t getDefaultType();
  484. uint32_t rva;
  485. uint8_t type;
  486. };
  487. // This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a
  488. // specific place in a section, without any data. This is used for the MinGW
  489. // specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept
  490. // of an empty chunk isn't MinGW specific.
  491. class EmptyChunk : public NonSectionChunk {
  492. public:
  493. EmptyChunk() {}
  494. size_t getSize() const override { return 0; }
  495. void writeTo(uint8_t *buf) const override {}
  496. };
  497. // MinGW specific, for the "automatic import of variables from DLLs" feature.
  498. // This provides the table of runtime pseudo relocations, for variable
  499. // references that turned out to need to be imported from a DLL even though
  500. // the reference didn't use the dllimport attribute. The MinGW runtime will
  501. // process this table after loading, before handling control over to user
  502. // code.
  503. class PseudoRelocTableChunk : public NonSectionChunk {
  504. public:
  505. PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs)
  506. : relocs(std::move(relocs)) {
  507. setAlignment(4);
  508. }
  509. size_t getSize() const override;
  510. void writeTo(uint8_t *buf) const override;
  511. private:
  512. std::vector<RuntimePseudoReloc> relocs;
  513. };
  514. // MinGW specific; information about one individual location in the image
  515. // that needs to be fixed up at runtime after loading. This represents
  516. // one individual element in the PseudoRelocTableChunk table.
  517. class RuntimePseudoReloc {
  518. public:
  519. RuntimePseudoReloc(Defined *sym, SectionChunk *target, uint32_t targetOffset,
  520. int flags)
  521. : sym(sym), target(target), targetOffset(targetOffset), flags(flags) {}
  522. Defined *sym;
  523. SectionChunk *target;
  524. uint32_t targetOffset;
  525. // The Flags field contains the size of the relocation, in bits. No other
  526. // flags are currently defined.
  527. int flags;
  528. };
  529. // MinGW specific. A Chunk that contains one pointer-sized absolute value.
  530. class AbsolutePointerChunk : public NonSectionChunk {
  531. public:
  532. AbsolutePointerChunk(uint64_t value) : value(value) {
  533. setAlignment(getSize());
  534. }
  535. size_t getSize() const override;
  536. void writeTo(uint8_t *buf) const override;
  537. private:
  538. uint64_t value;
  539. };
  540. // Return true if this file has the hotpatch flag set to true in the S_COMPILE3
  541. // record in codeview debug info. Also returns true for some thunks synthesized
  542. // by the linker.
  543. inline bool Chunk::isHotPatchable() const {
  544. if (auto *sc = dyn_cast<SectionChunk>(this))
  545. return sc->file->hotPatchable;
  546. else if (isa<ImportThunkChunk>(this))
  547. return true;
  548. return false;
  549. }
  550. void applyMOV32T(uint8_t *off, uint32_t v);
  551. void applyBranch24T(uint8_t *off, int32_t v);
  552. void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift);
  553. void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit);
  554. void applyArm64Branch26(uint8_t *off, int64_t v);
  555. } // namespace coff
  556. } // namespace lld
  557. namespace llvm {
  558. template <>
  559. struct DenseMapInfo<lld::coff::ChunkAndOffset>
  560. : lld::coff::ChunkAndOffset::DenseMapInfo {};
  561. }
  562. #endif