1 //===- Chunks.h -------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_COFF_CHUNKS_H
11 #define LLD_COFF_CHUNKS_H
14 #include "InputFiles.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/iterator.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Object/COFF.h"
26 using llvm::COFF::ImportDirectoryTableEntry;
27 using llvm::object::COFFSymbolRef;
28 using llvm::object::SectionRef;
29 using llvm::object::coff_relocation;
30 using llvm::object::coff_section;
34 class DefinedImportData;
40 // Mask for section types (code, data, bss, disacardable, etc.)
41 // and permissions (writable, readable or executable).
42 const uint32_t PermMask = 0xFF0000F0;
44 // A Chunk represents a chunk of data that will occupy space in the
45 // output (if the resolver chose that). It may or may not be backed by
46 // a section of an input file. It could be linker-created data, or
47 // doesn't even have actual data (if common or bss).
50 enum Kind { SectionKind, OtherKind };
51 Kind kind() const { return ChunkKind; }
52 virtual ~Chunk() = default;
54 // Returns the size of this chunk (even if this is a common or BSS.)
55 virtual size_t getSize() const = 0;
57 // Write this chunk to a mmap'ed file, assuming Buf is pointing to
58 // beginning of the file. Because this function may use RVA values
59 // of other chunks for relocations, you need to set them properly
60 // before calling this function.
61 virtual void writeTo(uint8_t *Buf) const {}
63 // The writer sets and uses the addresses.
64 uint64_t getRVA() const { return RVA; }
65 void setRVA(uint64_t V) { RVA = V; }
67 // Returns true if this has non-zero data. BSS chunks return
68 // false. If false is returned, the space occupied by this chunk
69 // will be filled with zeros.
70 virtual bool hasData() const { return true; }
72 // Returns readable/writable/executable bits.
73 virtual uint32_t getPermissions() const { return 0; }
75 // Returns the section name if this is a section chunk.
76 // It is illegal to call this function on non-section chunks.
77 virtual StringRef getSectionName() const {
78 llvm_unreachable("unimplemented getSectionName");
81 // An output section has pointers to chunks in the section, and each
82 // chunk has a back pointer to an output section.
83 void setOutputSection(OutputSection *O) { Out = O; }
84 OutputSection *getOutputSection() const { return Out; }
87 // Collect all locations that contain absolute addresses for base relocations.
88 virtual void getBaserels(std::vector<Baserel> *Res) {}
90 // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
91 // bytes, so this is used only for logging or debugging.
92 virtual StringRef getDebugName() { return ""; }
94 // The alignment of this chunk. The writer uses the value.
95 uint32_t Alignment = 1;
98 Chunk(Kind K = OtherKind) : ChunkKind(K) {}
101 // The RVA of this chunk in the output. The writer sets a value.
104 // The output section for this chunk.
105 OutputSection *Out = nullptr;
108 // The offset from beginning of the output section. The writer sets a value.
109 uint64_t OutputSectionOff = 0;
112 // A chunk corresponding a section of an input file.
113 class SectionChunk final : public Chunk {
114 // Identical COMDAT Folding feature accesses section internal data.
118 class symbol_iterator : public llvm::iterator_adaptor_base<
119 symbol_iterator, const coff_relocation *,
120 std::random_access_iterator_tag, Symbol *> {
125 symbol_iterator(ObjFile *File, const coff_relocation *I)
126 : symbol_iterator::iterator_adaptor_base(I), File(File) {}
129 symbol_iterator() = default;
131 Symbol *operator*() const { return File->getSymbol(I->SymbolTableIndex); }
134 SectionChunk(ObjFile *File, const coff_section *Header);
135 static bool classof(const Chunk *C) { return C->kind() == SectionKind; }
136 size_t getSize() const override { return Header->SizeOfRawData; }
137 ArrayRef<uint8_t> getContents() const;
138 void writeTo(uint8_t *Buf) const override;
139 bool hasData() const override;
140 uint32_t getPermissions() const override;
141 StringRef getSectionName() const override { return SectionName; }
142 void getBaserels(std::vector<Baserel> *Res) override;
143 bool isCOMDAT() const;
144 void applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
146 void applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
148 void applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
150 void applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
153 // Called if the garbage collector decides to not include this chunk
154 // in a final output. It's supposed to print out a log message to stdout.
155 void printDiscardedMessage() const;
157 // Adds COMDAT associative sections to this COMDAT section. A chunk
158 // and its children are treated as a group by the garbage collector.
159 void addAssociative(SectionChunk *Child);
161 StringRef getDebugName() override;
163 // Returns true if the chunk was not dropped by GC.
164 bool isLive() { return Live; }
166 // Used by the garbage collector.
168 assert(Config->DoGC && "should only mark things live from GC");
169 assert(!isLive() && "Cannot mark an already live section!");
173 // True if this is a codeview debug info chunk. These will not be laid out in
174 // the image. Instead they will end up in the PDB, if one is requested.
175 bool isCodeView() const {
176 return SectionName == ".debug" || SectionName.startswith(".debug$");
179 // True if this is a DWARF debug info or exception handling chunk.
180 bool isDWARF() const {
181 return SectionName.startswith(".debug_") || SectionName == ".eh_frame";
184 // Allow iteration over the bodies of this chunk's relocated symbols.
185 llvm::iterator_range<symbol_iterator> symbols() const {
186 return llvm::make_range(symbol_iterator(File, Relocs.begin()),
187 symbol_iterator(File, Relocs.end()));
190 // Allow iteration over the associated child chunks for this section.
191 ArrayRef<SectionChunk *> children() const { return AssocChildren; }
193 // A pointer pointing to a replacement for this chunk.
194 // Initially it points to "this" object. If this chunk is merged
195 // with other chunk by ICF, it points to another chunk,
196 // and this chunk is considrered as dead.
199 // The CRC of the contents as described in the COFF spec 4.5.5.
200 // Auxiliary Format 5: Section Definitions. Used for ICF.
201 uint32_t Checksum = 0;
203 const coff_section *Header;
205 // The file that this chunk was created from.
208 // The COMDAT leader symbol if this is a COMDAT chunk.
209 DefinedRegular *Sym = nullptr;
212 StringRef SectionName;
213 std::vector<SectionChunk *> AssocChildren;
214 llvm::iterator_range<const coff_relocation *> Relocs;
217 // Used by the garbage collector.
220 // Used for ICF (Identical COMDAT Folding)
221 void replace(SectionChunk *Other);
222 uint32_t Class[2] = {0, 0};
225 // A chunk for common symbols. Common chunks don't have actual data.
226 class CommonChunk : public Chunk {
228 CommonChunk(const COFFSymbolRef Sym);
229 size_t getSize() const override { return Sym.getValue(); }
230 bool hasData() const override { return false; }
231 uint32_t getPermissions() const override;
232 StringRef getSectionName() const override { return ".bss"; }
235 const COFFSymbolRef Sym;
238 // A chunk for linker-created strings.
239 class StringChunk : public Chunk {
241 explicit StringChunk(StringRef S) : Str(S) {}
242 size_t getSize() const override { return Str.size() + 1; }
243 void writeTo(uint8_t *Buf) const override;
249 static const uint8_t ImportThunkX86[] = {
250 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
253 static const uint8_t ImportThunkARM[] = {
254 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
255 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
256 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
259 static const uint8_t ImportThunkARM64[] = {
260 0x10, 0x00, 0x00, 0x90, // adrp x16, #0
261 0x10, 0x02, 0x40, 0xf9, // ldr x16, [x16]
262 0x00, 0x02, 0x1f, 0xd6, // br x16
266 // A chunk for DLL import jump table entry. In a final output, it's
267 // contents will be a JMP instruction to some __imp_ symbol.
268 class ImportThunkChunkX64 : public Chunk {
270 explicit ImportThunkChunkX64(Defined *S);
271 size_t getSize() const override { return sizeof(ImportThunkX86); }
272 void writeTo(uint8_t *Buf) const override;
278 class ImportThunkChunkX86 : public Chunk {
280 explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
281 size_t getSize() const override { return sizeof(ImportThunkX86); }
282 void getBaserels(std::vector<Baserel> *Res) override;
283 void writeTo(uint8_t *Buf) const override;
289 class ImportThunkChunkARM : public Chunk {
291 explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
292 size_t getSize() const override { return sizeof(ImportThunkARM); }
293 void getBaserels(std::vector<Baserel> *Res) override;
294 void writeTo(uint8_t *Buf) const override;
300 class ImportThunkChunkARM64 : public Chunk {
302 explicit ImportThunkChunkARM64(Defined *S) : ImpSymbol(S) {}
303 size_t getSize() const override { return sizeof(ImportThunkARM64); }
304 void writeTo(uint8_t *Buf) const override;
311 // See comments for DefinedLocalImport class.
312 class LocalImportChunk : public Chunk {
314 explicit LocalImportChunk(Defined *S) : Sym(S) {}
315 size_t getSize() const override;
316 void getBaserels(std::vector<Baserel> *Res) override;
317 void writeTo(uint8_t *Buf) const override;
324 // A chunk for SEH table which contains RVAs of safe exception handler
325 // functions. x86-only.
326 class SEHTableChunk : public Chunk {
328 explicit SEHTableChunk(std::set<Defined *> S) : Syms(std::move(S)) {}
329 size_t getSize() const override { return Syms.size() * 4; }
330 void writeTo(uint8_t *Buf) const override;
333 std::set<Defined *> Syms;
337 // This class represents a block in .reloc section.
338 // See the PE/COFF spec 5.6 for details.
339 class BaserelChunk : public Chunk {
341 BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End);
342 size_t getSize() const override { return Data.size(); }
343 void writeTo(uint8_t *Buf) const override;
346 std::vector<uint8_t> Data;
351 Baserel(uint32_t V, uint8_t Ty) : RVA(V), Type(Ty) {}
352 explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
353 uint8_t getDefaultType();
359 void applyMOV32T(uint8_t *Off, uint32_t V);
360 void applyBranch24T(uint8_t *Off, int32_t V);