1 //===- InputFiles.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_INPUT_FILES_H 10 #define LLD_COFF_INPUT_FILES_H 11 12 #include "Config.h" 13 #include "lld/Common/LLVM.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/DenseSet.h" 17 #include "llvm/DebugInfo/CodeView/TypeRecord.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" 22 #include <memory> 23 #include <set> 24 #include <vector> 25 26 namespace llvm { 27 namespace pdb { 28 class DbiModuleDescriptorBuilder; 29 } 30 } 31 32 namespace lld { 33 namespace coff { 34 35 std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *File); 36 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; 44 45 class Chunk; 46 class Defined; 47 class DefinedImportData; 48 class DefinedImportThunk; 49 class DefinedRegular; 50 class Lazy; 51 class SectionChunk; 52 class Symbol; 53 class Undefined; 54 55 // The root class of input files. 56 class InputFile { 57 public: 58 enum Kind { ArchiveKind, ObjectKind, ImportKind, BitcodeKind }; 59 Kind kind() const { return FileKind; } 60 virtual ~InputFile() {} 61 62 // Returns the filename. 63 StringRef getName() const { return MB.getBufferIdentifier(); } 64 65 // Reads a file (the constructor doesn't do that). 66 virtual void parse() = 0; 67 68 // Returns the CPU type this file was compiled to. 69 virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; } 70 71 MemoryBufferRef MB; 72 73 // An archive file name if this file is created from an archive. 74 StringRef ParentName; 75 76 // Returns .drectve section contents if exist. 77 StringRef getDirectives() { return StringRef(Directives).trim(); } 78 79 protected: 80 InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} 81 82 std::string Directives; 83 84 private: 85 const Kind FileKind; 86 }; 87 88 // .lib or .a file. 89 class ArchiveFile : public InputFile { 90 public: 91 explicit ArchiveFile(MemoryBufferRef M); 92 static bool classof(const InputFile *F) { return F->kind() == ArchiveKind; } 93 void parse() override; 94 95 // Enqueues an archive member load for the given symbol. If we've already 96 // enqueued a load for the same archive member, this function does nothing, 97 // which ensures that we don't load the same member more than once. 98 void addMember(const Archive::Symbol *Sym); 99 100 private: 101 std::unique_ptr<Archive> File; 102 llvm::DenseSet<uint64_t> Seen; 103 }; 104 105 // .obj or .o file. This may be a member of an archive file. 106 class ObjFile : public InputFile { 107 public: 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; } 118 119 ArrayRef<uint8_t> getDebugSection(StringRef SecName); 120 121 // Returns a Symbol object for the SymbolIndex'th symbol in the 122 // underlying object file. 123 Symbol *getSymbol(uint32_t SymbolIndex) { 124 return Symbols[SymbolIndex]; 125 } 126 127 // Returns the underlying COFF file. 128 COFFObjectFile *getCOFFObj() { return COFFObj.get(); } 129 130 static std::vector<ObjFile *> Instances; 131 132 // Flags in the absolute @feat.00 symbol if it is present. These usually 133 // indicate if an object was compiled with certain security features enabled 134 // like stack guard, safeseh, /guard:cf, or other things. 135 uint32_t Feat00Flags = 0; 136 137 // True if this object file is compatible with SEH. COFF-specific and 138 // x86-only. COFF spec 5.10.1. The .sxdata section. 139 bool hasSafeSEH() { return Feat00Flags & 0x1; } 140 141 // True if this file was compiled with /guard:cf. 142 bool hasGuardCF() { return Feat00Flags & 0x800; } 143 144 // Pointer to the PDB module descriptor builder. Various debug info records 145 // will reference object files by "module index", which is here. Things like 146 // source files and section contributions are also recorded here. Will be null 147 // if we are not producing a PDB. 148 llvm::pdb::DbiModuleDescriptorBuilder *ModuleDBI = nullptr; 149 150 const coff_section *AddrsigSec = nullptr; 151 152 // When using Microsoft precompiled headers, this is the PCH's key. 153 // The same key is used by both the precompiled object, and objects using the 154 // precompiled object. Any difference indicates out-of-date objects. 155 llvm::Optional<uint32_t> PCHSignature; 156 157 // Tells whether this file was compiled with /hotpatch 158 bool HotPatchable = false; 159 160 // Whether the object was already merged into the final PDB or not 161 bool MergedIntoPDB = false; 162 163 private: 164 const coff_section* getSection(uint32_t I); 165 const coff_section *getSection(COFFSymbolRef Sym) { 166 return getSection(Sym.getSectionNumber()); 167 } 168 169 void initializeChunks(); 170 void initializeSymbols(); 171 void initializeFlags(); 172 173 SectionChunk * 174 readSection(uint32_t SectionNumber, 175 const llvm::object::coff_aux_section_definition *Def, 176 StringRef LeaderName); 177 178 void readAssociativeDefinition( 179 COFFSymbolRef COFFSym, 180 const llvm::object::coff_aux_section_definition *Def); 181 182 void readAssociativeDefinition( 183 COFFSymbolRef COFFSym, 184 const llvm::object::coff_aux_section_definition *Def, 185 uint32_t ParentSection); 186 187 void recordPrevailingSymbolForMingw( 188 COFFSymbolRef COFFSym, 189 llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap); 190 191 void maybeAssociateSEHForMingw( 192 COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def, 193 const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap); 194 195 // Given a new symbol Sym with comdat selection Selection, if the new 196 // symbol is not (yet) Prevailing and the existing comdat leader set to 197 // Leader, emits a diagnostic if the new symbol and its selection doesn't 198 // match the existing symbol and its selection. If either old or new 199 // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace 200 // the existing leader. In that case, Prevailing is set to true. 201 void handleComdatSelection(COFFSymbolRef Sym, 202 llvm::COFF::COMDATType &Selection, 203 bool &Prevailing, DefinedRegular *Leader); 204 205 llvm::Optional<Symbol *> 206 createDefined(COFFSymbolRef Sym, 207 std::vector<const llvm::object::coff_aux_section_definition *> 208 &ComdatDefs, 209 bool &PrevailingComdat); 210 Symbol *createRegular(COFFSymbolRef Sym); 211 Symbol *createUndefined(COFFSymbolRef Sym); 212 213 std::unique_ptr<COFFObjectFile> COFFObj; 214 215 // List of all chunks defined by this file. This includes both section 216 // chunks and non-section chunks for common symbols. 217 std::vector<Chunk *> Chunks; 218 219 // CodeView debug info sections. 220 std::vector<SectionChunk *> DebugChunks; 221 222 // Chunks containing symbol table indices of exception handlers. Only used for 223 // 32-bit x86. 224 std::vector<SectionChunk *> SXDataChunks; 225 226 // Chunks containing symbol table indices of address taken symbols and longjmp 227 // targets. These are not linked into the final binary when /guard:cf is set. 228 std::vector<SectionChunk *> GuardFidChunks; 229 std::vector<SectionChunk *> GuardLJmpChunks; 230 231 // This vector contains the same chunks as Chunks, but they are 232 // indexed such that you can get a SectionChunk by section index. 233 // Nonexistent section indices are filled with null pointers. 234 // (Because section number is 1-based, the first slot is always a 235 // null pointer.) 236 std::vector<SectionChunk *> SparseChunks; 237 238 // This vector contains a list of all symbols defined or referenced by this 239 // file. They are indexed such that you can get a Symbol by symbol 240 // index. Nonexistent indices (which are occupied by auxiliary 241 // symbols in the real symbol table) are filled with null pointers. 242 std::vector<Symbol *> Symbols; 243 }; 244 245 // This type represents import library members that contain DLL names 246 // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7 247 // for details about the format. 248 class ImportFile : public InputFile { 249 public: 250 explicit ImportFile(MemoryBufferRef M) : InputFile(ImportKind, M) {} 251 252 static bool classof(const InputFile *F) { return F->kind() == ImportKind; } 253 254 static std::vector<ImportFile *> Instances; 255 256 Symbol *ImpSym = nullptr; 257 Symbol *ThunkSym = nullptr; 258 std::string DLLName; 259 260 private: 261 void parse() override; 262 263 public: 264 StringRef ExternalName; 265 const coff_import_header *Hdr; 266 Chunk *Location = nullptr; 267 268 // We want to eliminate dllimported symbols if no one actually refers them. 269 // These "Live" bits are used to keep track of which import library members 270 // are actually in use. 271 // 272 // If the Live bit is turned off by MarkLive, Writer will ignore dllimported 273 // symbols provided by this import library member. We also track whether the 274 // imported symbol is used separately from whether the thunk is used in order 275 // to avoid creating unnecessary thunks. 276 bool Live = !Config->DoGC; 277 bool ThunkLive = !Config->DoGC; 278 }; 279 280 // Used for LTO. 281 class BitcodeFile : public InputFile { 282 public: 283 explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {} 284 static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; } 285 ArrayRef<Symbol *> getSymbols() { return Symbols; } 286 MachineTypes getMachineType() override; 287 static std::vector<BitcodeFile *> Instances; 288 std::unique_ptr<llvm::lto::InputFile> Obj; 289 290 private: 291 void parse() override; 292 293 std::vector<Symbol *> Symbols; 294 }; 295 } // namespace coff 296 297 std::string toString(const coff::InputFile *File); 298 } // namespace lld 299 300 #endif 301