1 //===- InputFiles.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_INPUT_FILES_H
11 #define LLD_COFF_INPUT_FILES_H
14 #include "lld/Common/LLVM.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/LTO/LTO.h"
19 #include "llvm/Object/Archive.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Support/StringSaver.h"
28 class DbiModuleDescriptorBuilder;
35 std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File);
37 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
38 using llvm::COFF::MachineTypes;
39 using llvm::object::Archive;
40 using llvm::object::COFFObjectFile;
41 using llvm::object::COFFSymbolRef;
42 using llvm::object::coff_import_header;
43 using llvm::object::coff_section;
47 class DefinedImportData;
48 class DefinedImportThunk;
54 // The root class of input files.
57 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind };
58 Kind kind() const { return FileKind; }
59 virtual ~InputFile() {}
61 // Returns the filename.
62 StringRef getName() const { return MB.getBufferIdentifier(); }
64 // Reads a file (the constructor doesn't do that).
65 virtual void parse() = 0;
67 // Returns the CPU type this file was compiled to.
68 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
72 // An archive file name if this file is created from an archive.
75 // Returns .drectve section contents if exist.
76 StringRef getDirectives() { return StringRef(Directives).trim(); }
79 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
81 std::string Directives;
88 class ArchiveFile : public InputFile {
90 explicit ArchiveFile(MemoryBufferRef M);
91 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; }
92 void parse() override;
94 // Enqueues an archive member load for the given symbol. If we've already
95 // enqueued a load for the same archive member, this function does nothing,
96 // which ensures that we don't load the same member more than once.
97 void addMember(const Archive::Symbol *Sym);
100 std::unique_ptr<Archive> File;
101 std::string Filename;
102 llvm::DenseSet<uint64_t> Seen;
105 // .obj or .o file. This may be a member of an archive file.
106 class ObjFile : public InputFile {
108 explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
109 static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
110 void parse() override;
111 MachineTypes getMachineType() override;
112 ArrayRef<Chunk *> getChunks() { return Chunks; }
113 ArrayRef<SectionChunk *> getDebugChunks() { return DebugChunks; }
114 ArrayRef<SectionChunk *> getSXDataChunks() { return SXDataChunks; }
115 ArrayRef<SectionChunk *> getGuardFidChunks() { return GuardFidChunks; }
116 ArrayRef<SectionChunk *> getGuardLJmpChunks() { return GuardLJmpChunks; }
117 ArrayRef<Symbol *> getSymbols() { return Symbols; }
119 // Returns a Symbol object for the SymbolIndex'th symbol in the
120 // underlying object file.
121 Symbol *getSymbol(uint32_t SymbolIndex) {
122 return Symbols[SymbolIndex];
125 // Returns the underying COFF file.
126 COFFObjectFile *getCOFFObj() { return COFFObj.get(); }
128 static std::vector<ObjFile *> Instances;
130 // Flags in the absolute @feat.00 symbol if it is present. These usually
131 // indicate if an object was compiled with certain security features enabled
132 // like stack guard, safeseh, /guard:cf, or other things.
133 uint32_t Feat00Flags = 0;
135 // True if this object file is compatible with SEH. COFF-specific and
136 // x86-only. COFF spec 5.10.1. The .sxdata section.
137 bool hasSafeSEH() { return Feat00Flags & 0x1; }
139 // True if this file was compiled with /guard:cf.
140 bool hasGuardCF() { return Feat00Flags & 0x800; }
142 // Pointer to the PDB module descriptor builder. Various debug info records
143 // will reference object files by "module index", which is here. Things like
144 // source files and section contributions are also recorded here. Will be null
145 // if we are not producing a PDB.
146 llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr;
149 void initializeChunks();
150 void initializeSymbols();
153 readSection(uint32_t SectionNumber,
154 const llvm::object::coff_aux_section_definition *Def,
155 StringRef LeaderName);
157 void readAssociativeDefinition(
158 COFFSymbolRef COFFSym,
159 const llvm::object::coff_aux_section_definition *Def);
161 void readAssociativeDefinition(
162 COFFSymbolRef COFFSym,
163 const llvm::object::coff_aux_section_definition *Def,
164 uint32_t ParentSection);
166 void recordPrevailingSymbolForMingw(
167 COFFSymbolRef COFFSym,
168 llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
170 void maybeAssociateSEHForMingw(
171 COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def,
172 const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
174 llvm::Optional<Symbol *>
175 createDefined(COFFSymbolRef Sym,
176 std::vector<const llvm::object::coff_aux_section_definition *>
178 bool &PrevailingComdat);
179 Symbol *createRegular(COFFSymbolRef Sym);
180 Symbol *createUndefined(COFFSymbolRef Sym);
182 std::unique_ptr<COFFObjectFile> COFFObj;
184 // List of all chunks defined by this file. This includes both section
185 // chunks and non-section chunks for common symbols.
186 std::vector<Chunk *> Chunks;
188 // CodeView debug info sections.
189 std::vector<SectionChunk *> DebugChunks;
191 // Chunks containing symbol table indices of exception handlers. Only used for
193 std::vector<SectionChunk *> SXDataChunks;
195 // Chunks containing symbol table indices of address taken symbols and longjmp
196 // targets. These are not linked into the final binary when /guard:cf is set.
197 std::vector<SectionChunk *> GuardFidChunks;
198 std::vector<SectionChunk *> GuardLJmpChunks;
200 // This vector contains the same chunks as Chunks, but they are
201 // indexed such that you can get a SectionChunk by section index.
202 // Nonexistent section indices are filled with null pointers.
203 // (Because section number is 1-based, the first slot is always a
205 std::vector<SectionChunk *> SparseChunks;
207 // This vector contains a list of all symbols defined or referenced by this
208 // file. They are indexed such that you can get a Symbol by symbol
209 // index. Nonexistent indices (which are occupied by auxiliary
210 // symbols in the real symbol table) are filled with null pointers.
211 std::vector<Symbol *> Symbols;
214 // This type represents import library members that contain DLL names
215 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
216 // for details about the format.
217 class ImportFile : public InputFile {
219 explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {}
221 static bool classof(const InputFile *F) { return F->kind() == ImportKind; }
223 static std::vector<ImportFile *> Instances;
225 Symbol *ImpSym = nullptr;
226 Symbol *ThunkSym = nullptr;
230 void parse() override;
233 StringRef ExternalName;
234 const coff_import_header *Hdr;
235 Chunk *Location = nullptr;
237 // We want to eliminate dllimported symbols if no one actually refers them.
238 // These "Live" bits are used to keep track of which import library members
239 // are actually in use.
241 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
242 // symbols provided by this import library member. We also track whether the
243 // imported symbol is used separately from whether the thunk is used in order
244 // to avoid creating unnecessary thunks.
245 bool Live = !Config->DoGC;
246 bool ThunkLive = !Config->DoGC;
250 class BitcodeFile : public InputFile {
252 explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
253 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
254 ArrayRef<Symbol *> getSymbols() { return Symbols; }
255 MachineTypes getMachineType() override;
256 static std::vector<BitcodeFile *> Instances;
257 std::unique_ptr<llvm::lto::InputFile> Obj;
260 void parse() override;
262 std::vector<Symbol *> Symbols;
266 std::string toString(const coff::InputFile *File);