InputFiles.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #ifndef LLD_COFF_INPUT_FILES_H
  9. #define LLD_COFF_INPUT_FILES_H
  10. #include "Config.h"
  11. #include "lld/Common/DWARF.h"
  12. #include "lld/Common/LLVM.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/BinaryFormat/Magic.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. namespace llvm {
  26. struct DILineInfo;
  27. namespace pdb {
  28. class DbiModuleDescriptorBuilder;
  29. }
  30. }
  31. namespace lld {
  32. namespace coff {
  33. std::vector<MemoryBufferRef> getArchiveMembers(llvm::object::Archive *file);
  34. using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
  35. using llvm::COFF::MachineTypes;
  36. using llvm::object::Archive;
  37. using llvm::object::COFFObjectFile;
  38. using llvm::object::COFFSymbolRef;
  39. using llvm::object::coff_import_header;
  40. using llvm::object::coff_section;
  41. class Chunk;
  42. class Defined;
  43. class DefinedImportData;
  44. class DefinedImportThunk;
  45. class DefinedRegular;
  46. class SectionChunk;
  47. class Symbol;
  48. class Undefined;
  49. class TpiSource;
  50. // The root class of input files.
  51. class InputFile {
  52. public:
  53. enum Kind {
  54. ArchiveKind,
  55. ObjectKind,
  56. LazyObjectKind,
  57. ImportKind,
  58. BitcodeKind
  59. };
  60. Kind kind() const { return fileKind; }
  61. virtual ~InputFile() {}
  62. // Returns the filename.
  63. StringRef getName() const { return mb.getBufferIdentifier(); }
  64. // Reads a file (the constructor doesn't do that).
  65. virtual void parse() = 0;
  66. // Returns the CPU type this file was compiled to.
  67. virtual MachineTypes getMachineType() { return IMAGE_FILE_MACHINE_UNKNOWN; }
  68. MemoryBufferRef mb;
  69. // An archive file name if this file is created from an archive.
  70. StringRef parentName;
  71. // Returns .drectve section contents if exist.
  72. StringRef getDirectives() { return directives; }
  73. protected:
  74. InputFile(Kind k, MemoryBufferRef m) : mb(m), fileKind(k) {}
  75. StringRef directives;
  76. private:
  77. const Kind fileKind;
  78. };
  79. // .lib or .a file.
  80. class ArchiveFile : public InputFile {
  81. public:
  82. explicit ArchiveFile(MemoryBufferRef m);
  83. static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
  84. void parse() override;
  85. // Enqueues an archive member load for the given symbol. If we've already
  86. // enqueued a load for the same archive member, this function does nothing,
  87. // which ensures that we don't load the same member more than once.
  88. void addMember(const Archive::Symbol &sym);
  89. private:
  90. std::unique_ptr<Archive> file;
  91. llvm::DenseSet<uint64_t> seen;
  92. };
  93. // .obj or .o file between -start-lib and -end-lib.
  94. class LazyObjFile : public InputFile {
  95. public:
  96. explicit LazyObjFile(MemoryBufferRef m) : InputFile(LazyObjectKind, m) {}
  97. static bool classof(const InputFile *f) {
  98. return f->kind() == LazyObjectKind;
  99. }
  100. // Makes this object file part of the link.
  101. void fetch();
  102. // Adds the symbols in this file to the symbol table as LazyObject symbols.
  103. void parse() override;
  104. private:
  105. std::vector<Symbol *> symbols;
  106. };
  107. // .obj or .o file. This may be a member of an archive file.
  108. class ObjFile : public InputFile {
  109. public:
  110. explicit ObjFile(MemoryBufferRef m) : InputFile(ObjectKind, m) {}
  111. explicit ObjFile(MemoryBufferRef m, std::vector<Symbol *> &&symbols)
  112. : InputFile(ObjectKind, m), symbols(std::move(symbols)) {}
  113. static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
  114. void parse() override;
  115. MachineTypes getMachineType() override;
  116. ArrayRef<Chunk *> getChunks() { return chunks; }
  117. ArrayRef<SectionChunk *> getDebugChunks() { return debugChunks; }
  118. ArrayRef<SectionChunk *> getSXDataChunks() { return sXDataChunks; }
  119. ArrayRef<SectionChunk *> getGuardFidChunks() { return guardFidChunks; }
  120. ArrayRef<SectionChunk *> getGuardLJmpChunks() { return guardLJmpChunks; }
  121. ArrayRef<Symbol *> getSymbols() { return symbols; }
  122. ArrayRef<uint8_t> getDebugSection(StringRef secName);
  123. // Returns a Symbol object for the symbolIndex'th symbol in the
  124. // underlying object file.
  125. Symbol *getSymbol(uint32_t symbolIndex) {
  126. return symbols[symbolIndex];
  127. }
  128. // Returns the underlying COFF file.
  129. COFFObjectFile *getCOFFObj() { return coffObj.get(); }
  130. // Add a symbol for a range extension thunk. Return the new symbol table
  131. // index. This index can be used to modify a relocation.
  132. uint32_t addRangeThunkSymbol(Symbol *thunk) {
  133. symbols.push_back(thunk);
  134. return symbols.size() - 1;
  135. }
  136. void includeResourceChunks();
  137. bool isResourceObjFile() const { return !resourceChunks.empty(); }
  138. static std::vector<ObjFile *> instances;
  139. // Flags in the absolute @feat.00 symbol if it is present. These usually
  140. // indicate if an object was compiled with certain security features enabled
  141. // like stack guard, safeseh, /guard:cf, or other things.
  142. uint32_t feat00Flags = 0;
  143. // True if this object file is compatible with SEH. COFF-specific and
  144. // x86-only. COFF spec 5.10.1. The .sxdata section.
  145. bool hasSafeSEH() { return feat00Flags & 0x1; }
  146. // True if this file was compiled with /guard:cf.
  147. bool hasGuardCF() { return feat00Flags & 0x800; }
  148. // Pointer to the PDB module descriptor builder. Various debug info records
  149. // will reference object files by "module index", which is here. Things like
  150. // source files and section contributions are also recorded here. Will be null
  151. // if we are not producing a PDB.
  152. llvm::pdb::DbiModuleDescriptorBuilder *moduleDBI = nullptr;
  153. const coff_section *addrsigSec = nullptr;
  154. // When using Microsoft precompiled headers, this is the PCH's key.
  155. // The same key is used by both the precompiled object, and objects using the
  156. // precompiled object. Any difference indicates out-of-date objects.
  157. llvm::Optional<uint32_t> pchSignature;
  158. // Whether this file was compiled with /hotpatch.
  159. bool hotPatchable = false;
  160. // Whether the object was already merged into the final PDB.
  161. bool mergedIntoPDB = false;
  162. // If the OBJ has a .debug$T stream, this tells how it will be handled.
  163. TpiSource *debugTypesObj = nullptr;
  164. // The .debug$T stream if there's one.
  165. llvm::Optional<llvm::codeview::CVTypeArray> debugTypes;
  166. llvm::Optional<std::pair<StringRef, uint32_t>>
  167. getVariableLocation(StringRef var);
  168. llvm::Optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
  169. uint32_t sectionIndex);
  170. private:
  171. const coff_section* getSection(uint32_t i);
  172. const coff_section *getSection(COFFSymbolRef sym) {
  173. return getSection(sym.getSectionNumber());
  174. }
  175. void initializeChunks();
  176. void initializeSymbols();
  177. void initializeFlags();
  178. void initializeDependencies();
  179. SectionChunk *
  180. readSection(uint32_t sectionNumber,
  181. const llvm::object::coff_aux_section_definition *def,
  182. StringRef leaderName);
  183. void readAssociativeDefinition(
  184. COFFSymbolRef coffSym,
  185. const llvm::object::coff_aux_section_definition *def);
  186. void readAssociativeDefinition(
  187. COFFSymbolRef coffSym,
  188. const llvm::object::coff_aux_section_definition *def,
  189. uint32_t parentSection);
  190. void recordPrevailingSymbolForMingw(
  191. COFFSymbolRef coffSym,
  192. llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
  193. void maybeAssociateSEHForMingw(
  194. COFFSymbolRef sym, const llvm::object::coff_aux_section_definition *def,
  195. const llvm::DenseMap<StringRef, uint32_t> &prevailingSectionMap);
  196. // Given a new symbol Sym with comdat selection Selection, if the new
  197. // symbol is not (yet) Prevailing and the existing comdat leader set to
  198. // Leader, emits a diagnostic if the new symbol and its selection doesn't
  199. // match the existing symbol and its selection. If either old or new
  200. // symbol have selection IMAGE_COMDAT_SELECT_LARGEST, Sym might replace
  201. // the existing leader. In that case, Prevailing is set to true.
  202. void handleComdatSelection(COFFSymbolRef sym,
  203. llvm::COFF::COMDATType &selection,
  204. bool &prevailing, DefinedRegular *leader);
  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. std::unique_ptr<COFFObjectFile> coffObj;
  213. // List of all chunks defined by this file. This includes both section
  214. // chunks and non-section chunks for common symbols.
  215. std::vector<Chunk *> chunks;
  216. std::vector<SectionChunk *> resourceChunks;
  217. // CodeView debug info sections.
  218. std::vector<SectionChunk *> debugChunks;
  219. // Chunks containing symbol table indices of exception handlers. Only used for
  220. // 32-bit x86.
  221. std::vector<SectionChunk *> sXDataChunks;
  222. // Chunks containing symbol table indices of address taken symbols and longjmp
  223. // targets. These are not linked into the final binary when /guard:cf is set.
  224. std::vector<SectionChunk *> guardFidChunks;
  225. std::vector<SectionChunk *> guardLJmpChunks;
  226. // This vector contains the same chunks as Chunks, but they are
  227. // indexed such that you can get a SectionChunk by section index.
  228. // Nonexistent section indices are filled with null pointers.
  229. // (Because section number is 1-based, the first slot is always a
  230. // null pointer.)
  231. std::vector<SectionChunk *> sparseChunks;
  232. // This vector contains a list of all symbols defined or referenced by this
  233. // file. They are indexed such that you can get a Symbol by symbol
  234. // index. Nonexistent indices (which are occupied by auxiliary
  235. // symbols in the real symbol table) are filled with null pointers.
  236. std::vector<Symbol *> symbols;
  237. DWARFCache *dwarf = nullptr;
  238. };
  239. // This type represents import library members that contain DLL names
  240. // and symbols exported from the DLLs. See Microsoft PE/COFF spec. 7
  241. // for details about the format.
  242. class ImportFile : public InputFile {
  243. public:
  244. explicit ImportFile(MemoryBufferRef m) : InputFile(ImportKind, m) {}
  245. static bool classof(const InputFile *f) { return f->kind() == ImportKind; }
  246. static std::vector<ImportFile *> instances;
  247. Symbol *impSym = nullptr;
  248. Symbol *thunkSym = nullptr;
  249. std::string dllName;
  250. private:
  251. void parse() override;
  252. public:
  253. StringRef externalName;
  254. const coff_import_header *hdr;
  255. Chunk *location = nullptr;
  256. // We want to eliminate dllimported symbols if no one actually refers them.
  257. // These "Live" bits are used to keep track of which import library members
  258. // are actually in use.
  259. //
  260. // If the Live bit is turned off by MarkLive, Writer will ignore dllimported
  261. // symbols provided by this import library member. We also track whether the
  262. // imported symbol is used separately from whether the thunk is used in order
  263. // to avoid creating unnecessary thunks.
  264. bool live = !config->doGC;
  265. bool thunkLive = !config->doGC;
  266. };
  267. // Used for LTO.
  268. class BitcodeFile : public InputFile {
  269. public:
  270. BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
  271. uint64_t offsetInArchive)
  272. : BitcodeFile(mb, archiveName, offsetInArchive, {}) {}
  273. explicit BitcodeFile(MemoryBufferRef m, StringRef archiveName,
  274. uint64_t offsetInArchive,
  275. std::vector<Symbol *> &&symbols);
  276. static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
  277. ArrayRef<Symbol *> getSymbols() { return symbols; }
  278. MachineTypes getMachineType() override;
  279. static std::vector<BitcodeFile *> instances;
  280. std::unique_ptr<llvm::lto::InputFile> obj;
  281. private:
  282. void parse() override;
  283. std::vector<Symbol *> symbols;
  284. };
  285. inline bool isBitcode(MemoryBufferRef mb) {
  286. return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
  287. }
  288. std::string replaceThinLTOSuffix(StringRef path);
  289. } // namespace coff
  290. std::string toString(const coff::InputFile *file);
  291. } // namespace lld
  292. #endif