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/Core/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"
27 using llvm::COFF::ImportDirectoryTableEntry;
28 using llvm::object::COFFSymbolRef;
29 using llvm::object::SectionRef;
30 using llvm::object::coff_relocation;
31 using llvm::object::coff_section;
32 using llvm::sys::fs::file_magic;
36 class DefinedImportData;
42 // Mask for section types (code, data, bss, disacardable, etc.)
43 // and permissions (writable, readable or executable).
44 const uint32_t PermMask = 0xFF0000F0;
46 // A Chunk represents a chunk of data that will occupy space in the
47 // output (if the resolver chose that). It may or may not be backed by
48 // a section of an input file. It could be linker-created data, or
49 // doesn't even have actual data (if common or bss).
52 enum Kind { SectionKind, OtherKind };
53 Kind kind() const { return ChunkKind; }
54 virtual ~Chunk() = default;
56 // Returns the size of this chunk (even if this is a common or BSS.)
57 virtual size_t getSize() const = 0;
59 // Write this chunk to a mmap'ed file, assuming Buf is pointing to
60 // beginning of the file. Because this function may use RVA values
61 // of other chunks for relocations, you need to set them properly
62 // before calling this function.
63 virtual void writeTo(uint8_t *Buf) const {}
65 // The writer sets and uses the addresses.
66 uint64_t getRVA() const { return RVA; }
67 uint32_t getAlign() const { return Align; }
68 void setRVA(uint64_t V) { RVA = V; }
69 void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; }
71 // Returns true if this has non-zero data. BSS chunks return
72 // false. If false is returned, the space occupied by this chunk
73 // will be filled with zeros.
74 virtual bool hasData() const { return true; }
76 // Returns readable/writable/executable bits.
77 virtual uint32_t getPermissions() const { return 0; }
79 // Returns the section name if this is a section chunk.
80 // It is illegal to call this function on non-section chunks.
81 virtual StringRef getSectionName() const {
82 llvm_unreachable("unimplemented getSectionName");
85 // An output section has pointers to chunks in the section, and each
86 // chunk has a back pointer to an output section.
87 void setOutputSection(OutputSection *O) { Out = O; }
88 OutputSection *getOutputSection() { return Out; }
91 // Collect all locations that contain absolute addresses for base relocations.
92 virtual void getBaserels(std::vector<Baserel> *Res) {}
94 // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
95 // bytes, so this is used only for logging or debugging.
96 virtual StringRef getDebugName() { return ""; }
99 Chunk(Kind K = OtherKind) : ChunkKind(K) {}
100 const Kind ChunkKind;
102 // The RVA of this chunk in the output. The writer sets a value.
105 // The offset from beginning of the output section. The writer sets a value.
106 uint64_t OutputSectionOff = 0;
108 // The output section for this chunk.
109 OutputSection *Out = nullptr;
111 // The alignment of this chunk. The writer uses the value.
115 // A chunk corresponding a section of an input file.
116 class SectionChunk : public Chunk {
117 // Identical COMDAT Folding feature accesses section internal data.
121 class symbol_iterator : public llvm::iterator_adaptor_base<
122 symbol_iterator, const coff_relocation *,
123 std::random_access_iterator_tag, SymbolBody *> {
128 symbol_iterator(ObjectFile *File, const coff_relocation *I)
129 : symbol_iterator::iterator_adaptor_base(I), File(File) {}
132 symbol_iterator() = default;
134 SymbolBody *operator*() const {
135 return File->getSymbolBody(I->SymbolTableIndex);
139 SectionChunk(ObjectFile *File, const coff_section *Header);
140 static bool classof(const Chunk *C) { return C->kind() == SectionKind; }
141 size_t getSize() const override { return Header->SizeOfRawData; }
142 ArrayRef<uint8_t> getContents() const;
143 void writeTo(uint8_t *Buf) const override;
144 bool hasData() const override;
145 uint32_t getPermissions() const override;
146 StringRef getSectionName() const override { return SectionName; }
147 void getBaserels(std::vector<Baserel> *Res) override;
148 bool isCOMDAT() const;
149 void applyRelX64(uint8_t *Off, uint16_t Type, Defined *Sym, uint64_t P) const;
150 void applyRelX86(uint8_t *Off, uint16_t Type, Defined *Sym, uint64_t P) const;
151 void applyRelARM(uint8_t *Off, uint16_t Type, Defined *Sym, uint64_t P) const;
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;
162 void setSymbol(DefinedRegular *S) { if (!Sym) Sym = S; }
164 // Used by the garbage collector.
165 bool isLive() { return !Config->DoGC || Live; }
167 assert(!isLive() && "Cannot mark an already live section!");
171 // Allow iteration over the bodies of this chunk's relocated symbols.
172 llvm::iterator_range<symbol_iterator> symbols() const {
173 return llvm::make_range(symbol_iterator(File, Relocs.begin()),
174 symbol_iterator(File, Relocs.end()));
177 // Allow iteration over the associated child chunks for this section.
178 ArrayRef<SectionChunk *> children() const { return AssocChildren; }
180 // A pointer pointing to a replacement for this chunk.
181 // Initially it points to "this" object. If this chunk is merged
182 // with other chunk by ICF, it points to another chunk,
183 // and this chunk is considrered as dead.
186 // The CRC of the contents as described in the COFF spec 4.5.5.
187 // Auxiliary Format 5: Section Definitions. Used for ICF.
188 uint32_t Checksum = 0;
191 // A file this chunk was created from.
194 const coff_section *Header;
195 StringRef SectionName;
196 std::vector<SectionChunk *> AssocChildren;
197 llvm::iterator_range<const coff_relocation *> Relocs;
200 // Used by the garbage collector.
203 // Used for ICF (Identical COMDAT Folding)
204 void replace(SectionChunk *Other);
205 std::atomic<uint64_t> GroupID = { 0 };
207 // Sym points to a section symbol if this is a COMDAT chunk.
208 DefinedRegular *Sym = nullptr;
211 // A chunk for common symbols. Common chunks don't have actual data.
212 class CommonChunk : public Chunk {
214 CommonChunk(const COFFSymbolRef Sym);
215 size_t getSize() const override { return Sym.getValue(); }
216 bool hasData() const override { return false; }
217 uint32_t getPermissions() const override;
218 StringRef getSectionName() const override { return ".bss"; }
221 const COFFSymbolRef Sym;
224 // A chunk for linker-created strings.
225 class StringChunk : public Chunk {
227 explicit StringChunk(StringRef S) : Str(S) {}
228 size_t getSize() const override { return Str.size() + 1; }
229 void writeTo(uint8_t *Buf) const override;
235 static const uint8_t ImportThunkX86[] = {
236 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
239 static const uint8_t ImportThunkARM[] = {
240 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
241 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
242 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
246 // A chunk for DLL import jump table entry. In a final output, it's
247 // contents will be a JMP instruction to some __imp_ symbol.
248 class ImportThunkChunkX64 : public Chunk {
250 explicit ImportThunkChunkX64(Defined *S);
251 size_t getSize() const override { return sizeof(ImportThunkX86); }
252 void writeTo(uint8_t *Buf) const override;
258 class ImportThunkChunkX86 : public Chunk {
260 explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
261 size_t getSize() const override { return sizeof(ImportThunkX86); }
262 void getBaserels(std::vector<Baserel> *Res) override;
263 void writeTo(uint8_t *Buf) const override;
269 class ImportThunkChunkARM : public Chunk {
271 explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
272 size_t getSize() const override { return sizeof(ImportThunkARM); }
273 void getBaserels(std::vector<Baserel> *Res) override;
274 void writeTo(uint8_t *Buf) const override;
281 // See comments for DefinedLocalImport class.
282 class LocalImportChunk : public Chunk {
284 explicit LocalImportChunk(Defined *S) : Sym(S) {}
285 size_t getSize() const override;
286 void getBaserels(std::vector<Baserel> *Res) override;
287 void writeTo(uint8_t *Buf) const override;
294 // A chunk for SEH table which contains RVAs of safe exception handler
295 // functions. x86-only.
296 class SEHTableChunk : public Chunk {
298 explicit SEHTableChunk(std::set<Defined *> S) : Syms(std::move(S)) {}
299 size_t getSize() const override { return Syms.size() * 4; }
300 void writeTo(uint8_t *Buf) const override;
303 std::set<Defined *> Syms;
307 // This class represents a block in .reloc section.
308 // See the PE/COFF spec 5.6 for details.
309 class BaserelChunk : public Chunk {
311 BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End);
312 size_t getSize() const override { return Data.size(); }
313 void writeTo(uint8_t *Buf) const override;
316 std::vector<uint8_t> Data;
321 Baserel(uint32_t V, uint8_t Ty) : RVA(V), Type(Ty) {}
322 explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
323 uint8_t getDefaultType();