xref: /freebsd/contrib/llvm-project/lld/COFF/Writer.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- Writer.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 
9 #ifndef LLD_COFF_WRITER_H
10 #define LLD_COFF_WRITER_H
11 
12 #include "Chunks.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Object/COFF.h"
15 #include <chrono>
16 #include <cstdint>
17 #include <vector>
18 
19 namespace lld::coff {
20 static const int pageSize = 4096;
21 class COFFLinkerContext;
22 
23 void writeResult(COFFLinkerContext &ctx);
24 
25 class PartialSection {
26 public:
PartialSection(StringRef n,uint32_t chars)27   PartialSection(StringRef n, uint32_t chars)
28       : name(n), characteristics(chars) {}
29   StringRef name;
30   unsigned characteristics;
31   std::vector<Chunk *> chunks;
32 };
33 
34 // OutputSection represents a section in an output file. It's a
35 // container of chunks. OutputSection and Chunk are 1:N relationship.
36 // Chunks cannot belong to more than one OutputSections. The writer
37 // creates multiple OutputSections and assign them unique,
38 // non-overlapping file offsets and RVAs.
39 class OutputSection {
40 public:
OutputSection(llvm::StringRef n,uint32_t chars)41   OutputSection(llvm::StringRef n, uint32_t chars) : name(n) {
42     header.Characteristics = chars;
43   }
44   void addChunk(Chunk *c);
45   void insertChunkAtStart(Chunk *c);
46   void merge(OutputSection *other);
47   void setPermissions(uint32_t c);
getRVA()48   uint64_t getRVA() const { return header.VirtualAddress; }
getFileOff()49   uint64_t getFileOff() const { return header.PointerToRawData; }
50   void writeHeaderTo(uint8_t *buf, bool isDebug);
51   void addContributingPartialSection(PartialSection *sec);
52 
53   // Sort chunks to split native and EC sections on hybrid targets.
54   void splitECChunks();
55 
56   // Returns the size of this section in an executable memory image.
57   // This may be smaller than the raw size (the raw size is multiple
58   // of disk sector size, so there may be padding at end), or may be
59   // larger (if that's the case, the loader reserves spaces after end
60   // of raw data).
getVirtualSize()61   uint64_t getVirtualSize() { return header.VirtualSize; }
62 
63   // Returns the size of the section in the output file.
getRawSize()64   uint64_t getRawSize() { return header.SizeOfRawData; }
65 
66   // Set offset into the string table storing this section name.
67   // Used only when the name is longer than 8 bytes.
setStringTableOff(uint32_t v)68   void setStringTableOff(uint32_t v) { stringTableOff = v; }
69 
isCodeSection()70   bool isCodeSection() const {
71     return (header.Characteristics & llvm::COFF::IMAGE_SCN_CNT_CODE) &&
72            (header.Characteristics & llvm::COFF::IMAGE_SCN_MEM_READ) &&
73            (header.Characteristics & llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
74   }
75 
76   // N.B. The section index is one based.
77   uint32_t sectionIndex = 0;
78 
79   llvm::StringRef name;
80   llvm::object::coff_section header = {};
81 
82   std::vector<Chunk *> chunks;
83   std::vector<Chunk *> origChunks;
84 
85   std::vector<PartialSection *> contribSections;
86 
87 private:
88   uint32_t stringTableOff = 0;
89 };
90 
91 } // namespace lld::coff
92 
93 #endif
94