diff options
| -rw-r--r-- | lld/COFF/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | lld/COFF/Driver.cpp | 88 | ||||
| -rw-r--r-- | lld/COFF/Driver.h | 3 | ||||
| -rw-r--r-- | llvm/include/llvm/Object/COFFImportFile.h | 34 | ||||
| -rw-r--r-- | llvm/include/llvm/Object/COFFModuleDefinition.h | 49 | ||||
| -rw-r--r-- | llvm/lib/Object/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | llvm/lib/Object/COFFImportFile.cpp (renamed from lld/COFF/Librarian.cpp) | 154 | ||||
| -rw-r--r-- | llvm/lib/Object/COFFModuleDefinition.cpp (renamed from lld/COFF/ModuleDef.cpp) | 137 |
8 files changed, 329 insertions, 140 deletions
diff --git a/lld/COFF/CMakeLists.txt b/lld/COFF/CMakeLists.txt index 8f24e36c0ec..14f553e540d 100644 --- a/lld/COFF/CMakeLists.txt +++ b/lld/COFF/CMakeLists.txt @@ -14,11 +14,9 @@ add_lld_library(lldCOFF Error.cpp ICF.cpp InputFiles.cpp - Librarian.cpp LTO.cpp MapFile.cpp MarkLive.cpp - ModuleDef.cpp PDB.cpp Strings.cpp SymbolTable.cpp diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index da028242886..96c328b3051 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -19,6 +19,8 @@ #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Object/ArchiveWriter.h" +#include "llvm/Object/COFFImportFile.h" +#include "llvm/Object/COFFModuleDefinition.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" @@ -35,6 +37,7 @@ #include <future> using namespace llvm; +using namespace llvm::object; using namespace llvm::COFF; using llvm::sys::Process; using llvm::sys::fs::file_magic; @@ -419,6 +422,85 @@ static std::string getMapFile(const opt::InputArgList &Args) { return (OutFile.substr(0, OutFile.rfind('.')) + ".map").str(); } +static std::string getImplibPath() { + if (!Config->Implib.empty()) + return Config->Implib; + SmallString<128> Out = StringRef(Config->OutputFile); + sys::path::replace_extension(Out, ".lib"); + return Out.str(); +} + +std::vector<COFFShortExport> createCOFFShortExportFromConfig() { + std::vector<COFFShortExport> Exports; + for (Export &E1 : Config->Exports) { + COFFShortExport E2; + // Use SymbolName, which will have any stdcall or fastcall qualifiers. + E2.Name = E1.SymbolName; + E2.ExtName = E1.ExtName; + E2.Ordinal = E1.Ordinal; + E2.Noname = E1.Noname; + E2.Data = E1.Data; + E2.Private = E1.Private; + E2.Constant = E1.Constant; + Exports.push_back(E2); + } + return Exports; +} + +static void createImportLibrary() { + std::vector<COFFShortExport> Exports = createCOFFShortExportFromConfig(); + std::string DLLName = sys::path::filename(Config->OutputFile); + std::string Path = getImplibPath(); + writeImportLibrary(DLLName, Path, Exports, Config->Machine); +} + +static void parseModuleDefs(StringRef Path) { + std::unique_ptr<MemoryBuffer> MB = check( + MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); + MemoryBufferRef MBRef = MB->getMemBufferRef(); + + Expected<COFFModuleDefinition> Def = + parseCOFFModuleDefinition(MBRef, Config->Machine); + if (!Def) + fatal(errorToErrorCode(Def.takeError()).message()); + + COFFModuleDefinition &M = *Def; + if (Config->OutputFile.empty()) + Config->OutputFile = Saver.save(M.OutputFile); + + if (M.ImageBase) + Config->ImageBase = M.ImageBase; + if (M.StackReserve) + Config->StackReserve = M.StackReserve; + if (M.StackCommit) + Config->StackCommit = M.StackCommit; + if (M.HeapReserve) + Config->HeapReserve = M.HeapReserve; + if (M.HeapCommit) + Config->HeapCommit = M.HeapCommit; + if (M.MajorImageVersion) + Config->MajorImageVersion = M.MajorImageVersion; + if (M.MinorImageVersion) + Config->MinorImageVersion = M.MinorImageVersion; + if (M.MajorOSVersion) + Config->MajorOSVersion = M.MajorOSVersion; + if (M.MinorOSVersion) + Config->MinorOSVersion = M.MinorOSVersion; + + for (COFFShortExport E1 : M.Exports) { + Export E2; + E2.Name = Saver.save(E1.Name); + if (E1.isWeak()) + E2.ExtName = Saver.save(E1.ExtName); + E2.Ordinal = E1.Ordinal; + E2.Noname = E1.Noname; + E2.Data = E1.Data; + E2.Private = E1.Private; + E2.Constant = E1.Constant; + Config->Exports.push_back(E2); + } +} + std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) { std::vector<MemoryBufferRef> V; Error Err = Error::success(); @@ -912,9 +994,7 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { // Handle /def if (auto *Arg = Args.getLastArg(OPT_deffile)) { // parseModuleDefs mutates Config object. - parseModuleDefs( - takeBuffer(check(MemoryBuffer::getFile(Arg->getValue()), - Twine("could not open ") + Arg->getValue()))); + parseModuleDefs(Arg->getValue()); } // Handle /delayload @@ -1034,7 +1114,7 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { // need to create a .lib file. if (!Config->Exports.empty() || Config->DLL) { fixupExports(); - writeImportLibrary(); + createImportLibrary(); assignExportOrdinals(); } diff --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h index ed174d9f350..3eb950cca25 100644 --- a/lld/COFF/Driver.h +++ b/lld/COFF/Driver.h @@ -124,9 +124,6 @@ private: std::vector<MemoryBufferRef> Resources; }; -void parseModuleDefs(MemoryBufferRef MB); -void writeImportLibrary(); - // Functions below this line are defined in DriverUtils.cpp. void printHelp(const char *Argv0); diff --git a/llvm/include/llvm/Object/COFFImportFile.h b/llvm/include/llvm/Object/COFFImportFile.h index 78d9d679acd..78044a2832f 100644 --- a/llvm/include/llvm/Object/COFFImportFile.h +++ b/llvm/include/llvm/Object/COFFImportFile.h @@ -9,13 +9,15 @@ // // COFF short import file is a special kind of file which contains // only symbol names for DLL-exported symbols. This class implements -// SymbolicFile interface for the file. +// exporting of Symbols to create libraries and a SymbolicFile +// interface for the file type. // //===----------------------------------------------------------------------===// #ifndef LLVM_OBJECT_COFF_IMPORT_FILE_H #define LLVM_OBJECT_COFF_IMPORT_FILE_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/Object/COFF.h" #include "llvm/Object/IRObjectFile.h" #include "llvm/Object/ObjectFile.h" @@ -68,6 +70,36 @@ private: } }; +struct COFFShortExport { + std::string Name; + std::string ExtName; + + uint16_t Ordinal = 0; + bool Noname = false; + bool Data = false; + bool Private = false; + bool Constant = false; + + bool isWeak() { + return ExtName.size() && ExtName != Name; + } + + friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) { + return L.Name == R.Name && L.ExtName == R.ExtName && + L.Ordinal == R.Ordinal && L.Noname == R.Noname && + L.Data == R.Data && L.Private == R.Private; + } + + friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) { + return !(L == R); + } +}; + +std::error_code writeImportLibrary(StringRef DLLName, + StringRef Path, + ArrayRef<COFFShortExport> Exports, + COFF::MachineTypes Machine); + } // namespace object } // namespace llvm diff --git a/llvm/include/llvm/Object/COFFModuleDefinition.h b/llvm/include/llvm/Object/COFFModuleDefinition.h new file mode 100644 index 00000000000..0428283fdc8 --- /dev/null +++ b/llvm/include/llvm/Object/COFFModuleDefinition.h @@ -0,0 +1,49 @@ +//===--- COFFModuleDefinition.h ---------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Windows-specific. +// A parser for the module-definition file (.def file). +// Parsed results are directly written to Config global variable. +// +// The format of module-definition files are described in this document: +// https://msdn.microsoft.com/en-us/library/28d6s79h.aspx +// +//===----------------------------------------------------------------------===// + + +#ifndef LLVM_OBJECT_COFF_MODULE_DEFINITION_H +#define LLVM_OBJECT_COFF_MODULE_DEFINITION_H + +#include "llvm/Object/COFFImportFile.h" +#include "llvm/Object/COFF.h" + +namespace llvm { +namespace object { + +struct COFFModuleDefinition { + std::vector<COFFShortExport> Exports; + std::string OutputFile; + uint64_t ImageBase = 0; + uint64_t StackReserve = 0; + uint64_t StackCommit = 0; + uint64_t HeapReserve = 0; + uint64_t HeapCommit = 0; + uint32_t MajorImageVersion = 0; + uint32_t MinorImageVersion = 0; + uint32_t MajorOSVersion = 0; + uint32_t MinorOSVersion = 0; +}; + +Expected<COFFModuleDefinition> +parseCOFFModuleDefinition(MemoryBufferRef MB, COFF::MachineTypes Machine); + +} // End namespace object. +} // End namespace llvm. + +#endif diff --git a/llvm/lib/Object/CMakeLists.txt b/llvm/lib/Object/CMakeLists.txt index fa033589028..1d08a9efd8b 100644 --- a/llvm/lib/Object/CMakeLists.txt +++ b/llvm/lib/Object/CMakeLists.txt @@ -2,6 +2,8 @@ add_llvm_library(LLVMObject Archive.cpp ArchiveWriter.cpp Binary.cpp + COFFImportFile.cpp + COFFModuleDefinition.cpp COFFObjectFile.cpp Decompressor.cpp ELF.cpp diff --git a/lld/COFF/Librarian.cpp b/llvm/lib/Object/COFFImportFile.cpp index 91316ee6b0c..37962d84d85 100644 --- a/lld/COFF/Librarian.cpp +++ b/llvm/lib/Object/COFFImportFile.cpp @@ -1,36 +1,39 @@ -//===- Librarian.cpp ------------------------------------------------------===// +//===- COFFImportFile.cpp - COFF short import file implementation ---------===// // -// The LLVM Linker +// The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // -// This file contains functions for the Librarian. The librarian creates and -// manages libraries of the Common Object File Format (COFF) object files. It -// primarily is used for creating static libraries and import libraries. +// This file defines the writeImportLibrary function. // //===----------------------------------------------------------------------===// -#include "Config.h" -#include "Driver.h" -#include "Error.h" -#include "Symbols.h" +#include "llvm/Object/COFFImportFile.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/Object/Archive.h" #include "llvm/Object/ArchiveWriter.h" #include "llvm/Object/COFF.h" +#include "llvm/Support/Error.h" #include "llvm/Support/Path.h" +#include <cstdint> +#include <map> +#include <set> +#include <string> #include <vector> -using namespace lld::coff; using namespace llvm::COFF; using namespace llvm::object; using namespace llvm; -static bool is32bit() { - switch (Config->Machine) { +namespace llvm { +namespace object { + +static bool is32bit(MachineTypes Machine) { + switch (Machine) { default: llvm_unreachable("unsupported machine"); case IMAGE_FILE_MACHINE_AMD64: @@ -41,8 +44,8 @@ static bool is32bit() { } } -static uint16_t getImgRelRelocation() { - switch (Config->Machine) { +static uint16_t getImgRelRelocation(MachineTypes Machine) { + switch (Machine) { default: llvm_unreachable("unsupported machine"); case IMAGE_FILE_MACHINE_AMD64: @@ -68,8 +71,8 @@ static void writeStringTable(std::vector<uint8_t> &B, // strings. The termination is important as they are referenced to by offset // by the symbol entity in the file format. - std::vector<uint8_t>::size_type Pos = B.size(); - std::vector<uint8_t>::size_type Offset = B.size(); + size_t Pos = B.size(); + size_t Offset = B.size(); // Skip over the length field, we will fill it in later as we will have // computed the length while emitting the string content itself. @@ -83,26 +86,20 @@ static void writeStringTable(std::vector<uint8_t> &B, // Backfill the length of the table now that it has been computed. support::ulittle32_t Length(B.size() - Offset); - memcpy(&B[Offset], &Length, sizeof(Length)); -} - -static std::string getImplibPath() { - if (!Config->Implib.empty()) - return Config->Implib; - SmallString<128> Out = StringRef(Config->OutputFile); - sys::path::replace_extension(Out, ".lib"); - return Out.str(); + support::endian::write32le(&B[Offset], Length); } -static ImportNameType getNameType(StringRef Sym, StringRef ExtName) { +static ImportNameType getNameType(StringRef Sym, StringRef ExtName, + MachineTypes Machine) { if (Sym != ExtName) return IMPORT_NAME_UNDECORATE; - if (Config->Machine == I386 && Sym.startswith("_")) + if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.startswith("_")) return IMPORT_NAME_NOPREFIX; return IMPORT_NAME; } -static std::string replace(StringRef S, StringRef From, StringRef To) { +static Expected<std::string> replace(StringRef S, StringRef From, + StringRef To) { size_t Pos = S.find(From); // From and To may be mangled, but substrings in S may not. @@ -113,9 +110,11 @@ static std::string replace(StringRef S, StringRef From, StringRef To) { } if (Pos == StringRef::npos) { - error(S + ": replacing '" + From + "' with '" + To + "' failed"); - return ""; + return make_error<StringError>( + StringRef(Twine(S + ": replacing '" + From + + "' with '" + To + "' failed").str()), object_error::parse_failed); } + return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str(); } @@ -130,7 +129,7 @@ namespace { class ObjectFactory { using u16 = support::ulittle16_t; using u32 = support::ulittle32_t; - + MachineTypes Machine; BumpPtrAllocator Alloc; StringRef DLLName; StringRef Library; @@ -138,8 +137,8 @@ class ObjectFactory { std::string NullThunkSymbolName; public: - ObjectFactory(StringRef S) - : DLLName(S), Library(S.drop_back(4)), + ObjectFactory(StringRef S, MachineTypes M) + : Machine(M), DLLName(S), Library(S.drop_back(4)), ImportDescriptorSymbolName(("__IMPORT_DESCRIPTOR_" + Library).str()), NullThunkSymbolName(("\x7f" + Library + "_NULL_THUNK_DATA").str()) {} @@ -164,7 +163,7 @@ public: NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal, ImportType Type, ImportNameType NameType); }; -} +} // namespace NewArchiveMember ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) { @@ -174,15 +173,18 @@ ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) { // COFF Header coff_file_header Header{ - u16(Config->Machine), u16(NumberOfSections), u32(0), + u16(Machine), + u16(NumberOfSections), + u32(0), u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) + // .idata$2 sizeof(coff_import_directory_table_entry) + NumberOfRelocations * sizeof(coff_relocation) + // .idata$4 (DLLName.size() + 1)), - u32(NumberOfSymbols), u16(0), - u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0), + u32(NumberOfSymbols), + u16(0), + u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0), }; append(Buffer, Header); @@ -224,11 +226,11 @@ ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) { static const coff_relocation RelocationTable[NumberOfRelocations] = { {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2), - u16(getImgRelRelocation())}, + u16(getImgRelRelocation(Machine))}, {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)), - u32(3), u16(getImgRelRelocation())}, + u32(3), u16(getImgRelRelocation(Machine))}, {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)), - u32(4), u16(getImgRelRelocation())}, + u32(4), u16(getImgRelRelocation(Machine))}, }; append(Buffer, RelocationTable); @@ -308,12 +310,15 @@ ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) { // COFF Header coff_file_header Header{ - u16(Config->Machine), u16(NumberOfSections), u32(0), + u16(Machine), + u16(NumberOfSections), + u32(0), u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) + // .idata$3 sizeof(coff_import_directory_table_entry)), - u32(NumberOfSymbols), u16(0), - u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0), + u32(NumberOfSymbols), + u16(0), + u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0), }; append(Buffer, Header); @@ -363,18 +368,21 @@ ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) { NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { static const uint32_t NumberOfSections = 2; static const uint32_t NumberOfSymbols = 1; - uint32_t VASize = is32bit() ? 4 : 8; + uint32_t VASize = is32bit(Machine) ? 4 : 8; // COFF Header coff_file_header Header{ - u16(Config->Machine), u16(NumberOfSections), u32(0), + u16(Machine), + u16(NumberOfSections), + u32(0), u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) + // .idata$5 VASize + // .idata$4 VASize), - u32(NumberOfSymbols), u16(0), - u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0), + u32(NumberOfSymbols), + u16(0), + u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0), }; append(Buffer, Header); @@ -389,7 +397,8 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { u32(0), u16(0), u16(0), - u32((is32bit() ? IMAGE_SCN_ALIGN_4BYTES : IMAGE_SCN_ALIGN_8BYTES) | + u32((is32bit(Machine) ? IMAGE_SCN_ALIGN_4BYTES + : IMAGE_SCN_ALIGN_8BYTES) | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)}, {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}, @@ -402,7 +411,8 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { u32(0), u16(0), u16(0), - u32((is32bit() ? IMAGE_SCN_ALIGN_4BYTES : IMAGE_SCN_ALIGN_8BYTES) | + u32((is32bit(Machine) ? IMAGE_SCN_ALIGN_4BYTES + : IMAGE_SCN_ALIGN_8BYTES) | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)}, }; @@ -410,12 +420,12 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) { // .idata$5, ILT append(Buffer, u32(0)); - if (!is32bit()) + if (!is32bit(Machine)) append(Buffer, u32(0)); // .idata$4, IAT append(Buffer, u32(0)); - if (!is32bit()) + if (!is32bit(Machine)) append(Buffer, u32(0)); // Symbol Table @@ -452,7 +462,7 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym, auto *Imp = reinterpret_cast<coff_import_header *>(P); P += sizeof(*Imp); Imp->Sig2 = 0xFFFF; - Imp->Machine = Config->Machine; + Imp->Machine = Machine; Imp->SizeOfData = ImpSize; if (Ordinal > 0) Imp->OrdinalHint = Ordinal; @@ -466,15 +476,12 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym, return {MemoryBufferRef(StringRef(Buf, Size), DLLName)}; } -// Creates an import library for a DLL. In this function, we first -// create an empty import library using lib.exe and then adds short -// import files to that file. -void lld::coff::writeImportLibrary() { - std::vector<NewArchiveMember> Members; +std::error_code writeImportLibrary(StringRef DLLName, StringRef Path, + ArrayRef<COFFShortExport> Exports, + MachineTypes Machine) { - std::string Path = getImplibPath(); - std::string DLLName = sys::path::filename(Config->OutputFile); - ObjectFactory OF(DLLName); + std::vector<NewArchiveMember> Members; + ObjectFactory OF(llvm::sys::path::filename(DLLName), Machine); std::vector<uint8_t> ImportDescriptor; Members.push_back(OF.createImportDescriptor(ImportDescriptor)); @@ -485,7 +492,7 @@ void lld::coff::writeImportLibrary() { std::vector<uint8_t> NullThunk; Members.push_back(OF.createNullThunk(NullThunk)); - for (Export &E : Config->Exports) { + for (COFFShortExport E : Exports) { if (E.Private) continue; @@ -495,17 +502,26 @@ void lld::coff::writeImportLibrary() { if (E.Constant) ImportType = IMPORT_CONST; - ImportNameType NameType = getNameType(E.SymbolName, E.Name); - std::string Name = E.ExtName.empty() - ? std::string(E.SymbolName) - : replace(E.SymbolName, E.Name, E.ExtName); - Members.push_back(OF.createShortImport(Name, E.Ordinal, ImportType, - NameType)); + StringRef SymbolName = E.isWeak() ? E.ExtName : E.Name; + ImportNameType NameType = getNameType(SymbolName, E.Name, Machine); + Expected<std::string> Name = E.ExtName.empty() + ? SymbolName + : replace(SymbolName, E.Name, E.ExtName); + + if (!Name) { + return errorToErrorCode(Name.takeError()); + } + + Members.push_back( + OF.createShortImport(*Name, E.Ordinal, ImportType, NameType)); } std::pair<StringRef, std::error_code> Result = writeArchive(Path, Members, /*WriteSymtab*/ true, object::Archive::K_GNU, /*Deterministic*/ true, /*Thin*/ false); - if (auto EC = Result.second) - fatal(EC, "failed to write " + Path); + + return Result.second; } + +} // namespace object +} // namespace llvm diff --git a/lld/COFF/ModuleDef.cpp b/llvm/lib/Object/COFFModuleDefinition.cpp index 740ce867a7c..0d69cb6b709 100644 --- a/lld/COFF/ModuleDef.cpp +++ b/llvm/lib/Object/COFFModuleDefinition.cpp @@ -1,6 +1,6 @@ -//===- COFF/ModuleDef.cpp -------------------------------------------------===// +//===--- COFFModuleDefinition.cpp - Simple DEF parser ---------------------===// // -// The LLVM Linker +// The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. @@ -9,27 +9,26 @@ // // Windows-specific. // A parser for the module-definition file (.def file). -// Parsed results are directly written to Config global variable. // // The format of module-definition files are described in this document: // https://msdn.microsoft.com/en-us/library/28d6s79h.aspx // //===----------------------------------------------------------------------===// -#include "Config.h" -#include "Error.h" -#include "Memory.h" +#include "llvm/Object/COFFModuleDefinition.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSwitch.h" -#include "llvm/Support/StringSaver.h" +#include "llvm/Object/COFF.h" +#include "llvm/Object/COFFImportFile.h" +#include "llvm/Object/Error.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" -#include <system_error> +using namespace llvm::COFF; using namespace llvm; -namespace lld { -namespace coff { -namespace { +namespace llvm { +namespace object { enum Kind { Unknown, @@ -60,9 +59,14 @@ static bool isDecorated(StringRef Sym) { return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?"); } +static Error createError(const Twine &Err) { + return make_error<StringError>(StringRef(Err.str()), + object_error::parse_failed); +} + class Lexer { public: - explicit Lexer(StringRef S) : Buf(S) {} + Lexer(StringRef S) : Buf(S) {} Token lex() { Buf = Buf.trim(); @@ -116,12 +120,14 @@ private: class Parser { public: - explicit Parser(StringRef S) : Lex(S) {} + explicit Parser(StringRef S, MachineTypes M) : Lex(S), Machine(M) {} - void parse() { + Expected<COFFModuleDefinition> parse() { do { - parseOne(); + if (Error Err = parseOne()) + return std::move(Err); } while (Tok.K != Eof); + return Info; } private: @@ -134,83 +140,83 @@ private: Stack.pop_back(); } - void readAsInt(uint64_t *I) { + Error readAsInt(uint64_t *I) { read(); if (Tok.K != Identifier || Tok.Value.getAsInteger(10, *I)) - fatal("integer expected"); + return createError("integer expected"); + return Error::success(); } - void expect(Kind Expected, StringRef Msg) { + Error expect(Kind Expected, StringRef Msg) { read(); if (Tok.K != Expected) - fatal(Msg); + return createError(Msg); + return Error::success(); } void unget() { Stack.push_back(Tok); } - void parseOne() { + Error parseOne() { read(); switch (Tok.K) { case Eof: - return; + return Error::success(); case KwExports: for (;;) { read(); if (Tok.K != Identifier) { unget(); - return; + return Error::success(); } - parseExport(); + if (Error Err = parseExport()) + return Err; } case KwHeapsize: - parseNumbers(&Config->HeapReserve, &Config->HeapCommit); - return; + return parseNumbers(&Info.HeapReserve, &Info.HeapCommit); case KwStacksize: - parseNumbers(&Config->StackReserve, &Config->StackCommit); - return; + return parseNumbers(&Info.StackReserve, &Info.StackCommit); case KwLibrary: case KwName: { bool IsDll = Tok.K == KwLibrary; // Check before parseName. std::string Name; - parseName(&Name, &Config->ImageBase); - + if (Error Err = parseName(&Name, &Info.ImageBase)) + return Err; // Append the appropriate file extension if not already present. StringRef Ext = IsDll ? ".dll" : ".exe"; if (!StringRef(Name).endswith_lower(Ext)) Name += Ext; // Set the output file, but don't override /out if it was already passed. - if (Config->OutputFile.empty()) - Config->OutputFile = Name; - return; + if (Info.OutputFile.empty()) + Info.OutputFile = Name; + return Error::success(); } case KwVersion: - parseVersion(&Config->MajorImageVersion, &Config->MinorImageVersion); - return; + return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion); default: - fatal("unknown directive: " + Tok.Value); + return createError("unknown directive: " + Tok.Value); } } - void parseExport() { - Export E; + Error parseExport() { + COFFShortExport E; E.Name = Tok.Value; read(); if (Tok.K == Equal) { read(); if (Tok.K != Identifier) - fatal("identifier expected, but got " + Tok.Value); + return createError("identifier expected, but got " + Tok.Value); E.ExtName = E.Name; E.Name = Tok.Value; } else { unget(); } - if (Config->Machine == I386) { + if (Machine == IMAGE_FILE_MACHINE_I386) { if (!isDecorated(E.Name)) - E.Name = Saver.save("_" + E.Name); + E.Name = (std::string("_").append(E.Name)); if (!E.ExtName.empty() && !isDecorated(E.ExtName)) - E.ExtName = Saver.save("_" + E.ExtName); + E.ExtName = (std::string("_").append(E.ExtName)); } for (;;) { @@ -230,7 +236,6 @@ private: continue; } if (Tok.K == KwConstant) { - warn("CONSTANT keyword is obsolete; use DATA"); E.Constant = true; continue; } @@ -239,66 +244,76 @@ private: continue; } unget(); - Config->Exports.push_back(E); - return; + Info.Exports.push_back(E); + return Error::success(); } } // HEAPSIZE/STACKSIZE reserve[,commit] - void parseNumbers(uint64_t *Reserve, uint64_t *Commit) { - readAsInt(Reserve); + Error parseNumbers(uint64_t *Reserve, uint64_t *Commit) { + if (Error Err = readAsInt(Reserve)) + return Err; read(); if (Tok.K != Comma) { unget(); Commit = nullptr; - return; + return Error::success(); } - readAsInt(Commit); + if (Error Err = readAsInt(Commit)) + return Err; + return Error::success(); } // NAME outputPath [BASE=address] - void parseName(std::string *Out, uint64_t *Baseaddr) { + Error parseName(std::string *Out, uint64_t *Baseaddr) { read(); if (Tok.K == Identifier) { *Out = Tok.Value; } else { *Out = ""; unget(); - return; + return Error::success(); } read(); if (Tok.K == KwBase) { - expect(Equal, "'=' expected"); - readAsInt(Baseaddr); + if (Error Err = expect(Equal, "'=' expected")) + return Err; + if (Error Err = readAsInt(Baseaddr)) + return Err; } else { unget(); *Baseaddr = 0; } + return Error::success(); } // VERSION major[.minor] - void parseVersion(uint32_t *Major, uint32_t *Minor) { + Error parseVersion(uint32_t *Major, uint32_t *Minor) { read(); if (Tok.K != Identifier) - fatal("identifier expected, but got " + Tok.Value); + return createError("identifier expected, but got " + Tok.Value); StringRef V1, V2; std::tie(V1, V2) = Tok.Value.split('.'); if (V1.getAsInteger(10, *Major)) - fatal("integer expected, but got " + Tok.Value); + return createError("integer expected, but got " + Tok.Value); if (V2.empty()) *Minor = 0; else if (V2.getAsInteger(10, *Minor)) - fatal("integer expected, but got " + Tok.Value); + return createError("integer expected, but got " + Tok.Value); + return Error::success(); } Lexer Lex; Token Tok; std::vector<Token> Stack; + MachineTypes Machine; + COFFModuleDefinition Info; }; -} // anonymous namespace - -void parseModuleDefs(MemoryBufferRef MB) { Parser(MB.getBuffer()).parse(); } +Expected<COFFModuleDefinition> parseCOFFModuleDefinition(MemoryBufferRef MB, + MachineTypes Machine) { + return Parser(MB.getBuffer(), Machine).parse(); +} -} // namespace coff -} // namespace lld +} // namespace object +} // namespace llvm |

