Revert "COFF: migrate def parser from LLD to LLVM"

This reverts commits r303490, r303491, r303493, and r303494.

This caused http://crbug.com/728726. Essentially, exporting stdcall
functions doesn't appear to work after this change. Reduced test case
soon.

llvm-svn: 304561
This commit is contained in:
Reid Kleckner 2017-06-02 16:26:24 +00:00
parent 84a9f168f1
commit d249e4a188
8 changed files with 140 additions and 328 deletions

View File

@ -14,9 +14,11 @@ 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

View File

@ -19,8 +19,6 @@
#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"
@ -37,7 +35,6 @@
#include <future>
using namespace llvm;
using namespace llvm::object;
using namespace llvm::COFF;
using llvm::sys::Process;
using llvm::sys::fs::file_magic;
@ -422,84 +419,6 @@ 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;
E2.Name = E1.Name;
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();
@ -993,7 +912,9 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
// Handle /def
if (auto *Arg = Args.getLastArg(OPT_deffile)) {
// parseModuleDefs mutates Config object.
parseModuleDefs(Arg->getValue());
parseModuleDefs(
takeBuffer(check(MemoryBuffer::getFile(Arg->getValue()),
Twine("could not open ") + Arg->getValue())));
}
// Handle /delayload
@ -1113,7 +1034,7 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
// need to create a .lib file.
if (!Config->Exports.empty() || Config->DLL) {
fixupExports();
createImportLibrary();
writeImportLibrary();
assignExportOrdinals();
}

View File

@ -124,6 +124,9 @@ 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);

View File

@ -1,39 +1,36 @@
//===- COFFImportFile.cpp - COFF short import file implementation ---------===//
//===- Librarian.cpp ------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the writeImportLibrary function.
// 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.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/COFFImportFile.h"
#include "llvm/ADT/ArrayRef.h"
#include "Config.h"
#include "Driver.h"
#include "Error.h"
#include "Symbols.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;
namespace llvm {
namespace object {
static bool is32bit(MachineTypes Machine) {
switch (Machine) {
static bool is32bit() {
switch (Config->Machine) {
default:
llvm_unreachable("unsupported machine");
case IMAGE_FILE_MACHINE_AMD64:
@ -44,8 +41,8 @@ static bool is32bit(MachineTypes Machine) {
}
}
static uint16_t getImgRelRelocation(MachineTypes Machine) {
switch (Machine) {
static uint16_t getImgRelRelocation() {
switch (Config->Machine) {
default:
llvm_unreachable("unsupported machine");
case IMAGE_FILE_MACHINE_AMD64:
@ -71,8 +68,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.
size_t Pos = B.size();
size_t Offset = B.size();
std::vector<uint8_t>::size_type Pos = B.size();
std::vector<uint8_t>::size_type 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.
@ -86,20 +83,26 @@ 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);
support::endian::write32le(&B[Offset], Length);
memcpy(&B[Offset], &Length, sizeof(Length));
}
static ImportNameType getNameType(StringRef Sym, StringRef ExtName,
MachineTypes Machine) {
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();
}
static ImportNameType getNameType(StringRef Sym, StringRef ExtName) {
if (Sym != ExtName)
return IMPORT_NAME_UNDECORATE;
if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.startswith("_"))
if (Config->Machine == I386 && Sym.startswith("_"))
return IMPORT_NAME_NOPREFIX;
return IMPORT_NAME;
}
static Expected<std::string> replace(StringRef S, StringRef From,
StringRef To) {
static 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.
@ -110,11 +113,9 @@ static Expected<std::string> replace(StringRef S, StringRef From,
}
if (Pos == StringRef::npos) {
return make_error<StringError>(
StringRef(Twine(S + ": replacing '" + From +
"' with '" + To + "' failed").str()), object_error::parse_failed);
error(S + ": replacing '" + From + "' with '" + To + "' failed");
return "";
}
return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str();
}
@ -129,7 +130,7 @@ namespace {
class ObjectFactory {
using u16 = support::ulittle16_t;
using u32 = support::ulittle32_t;
MachineTypes Machine;
BumpPtrAllocator Alloc;
StringRef DLLName;
StringRef Library;
@ -137,8 +138,8 @@ class ObjectFactory {
std::string NullThunkSymbolName;
public:
ObjectFactory(StringRef S, MachineTypes M)
: Machine(M), DLLName(S), Library(S.drop_back(4)),
ObjectFactory(StringRef S)
: DLLName(S), Library(S.drop_back(4)),
ImportDescriptorSymbolName(("__IMPORT_DESCRIPTOR_" + Library).str()),
NullThunkSymbolName(("\x7f" + Library + "_NULL_THUNK_DATA").str()) {}
@ -163,7 +164,7 @@ public:
NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal,
ImportType Type, ImportNameType NameType);
};
} // namespace
}
NewArchiveMember
ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
@ -173,18 +174,15 @@ ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
// COFF Header
coff_file_header Header{
u16(Machine),
u16(NumberOfSections),
u32(0),
u16(Config->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(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0),
u32(NumberOfSymbols), u16(0),
u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0),
};
append(Buffer, Header);
@ -226,11 +224,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(Machine))},
u16(getImgRelRelocation())},
{u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)),
u32(3), u16(getImgRelRelocation(Machine))},
u32(3), u16(getImgRelRelocation())},
{u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)),
u32(4), u16(getImgRelRelocation(Machine))},
u32(4), u16(getImgRelRelocation())},
};
append(Buffer, RelocationTable);
@ -310,15 +308,12 @@ ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
// COFF Header
coff_file_header Header{
u16(Machine),
u16(NumberOfSections),
u32(0),
u16(Config->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(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0),
u32(NumberOfSymbols), u16(0),
u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0),
};
append(Buffer, Header);
@ -368,21 +363,18 @@ 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(Machine) ? 4 : 8;
uint32_t VASize = is32bit() ? 4 : 8;
// COFF Header
coff_file_header Header{
u16(Machine),
u16(NumberOfSections),
u32(0),
u16(Config->Machine), u16(NumberOfSections), u32(0),
u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
// .idata$5
VASize +
// .idata$4
VASize),
u32(NumberOfSymbols),
u16(0),
u16(is32bit(Machine) ? IMAGE_FILE_32BIT_MACHINE : 0),
u32(NumberOfSymbols), u16(0),
u16(is32bit() ? IMAGE_FILE_32BIT_MACHINE : 0),
};
append(Buffer, Header);
@ -397,8 +389,7 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
u32(0),
u16(0),
u16(0),
u32((is32bit(Machine) ? IMAGE_SCN_ALIGN_4BYTES
: IMAGE_SCN_ALIGN_8BYTES) |
u32((is32bit() ? 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'},
@ -411,8 +402,7 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
u32(0),
u16(0),
u16(0),
u32((is32bit(Machine) ? IMAGE_SCN_ALIGN_4BYTES
: IMAGE_SCN_ALIGN_8BYTES) |
u32((is32bit() ? IMAGE_SCN_ALIGN_4BYTES : IMAGE_SCN_ALIGN_8BYTES) |
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_WRITE)},
};
@ -420,12 +410,12 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
// .idata$5, ILT
append(Buffer, u32(0));
if (!is32bit(Machine))
if (!is32bit())
append(Buffer, u32(0));
// .idata$4, IAT
append(Buffer, u32(0));
if (!is32bit(Machine))
if (!is32bit())
append(Buffer, u32(0));
// Symbol Table
@ -462,7 +452,7 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym,
auto *Imp = reinterpret_cast<coff_import_header *>(P);
P += sizeof(*Imp);
Imp->Sig2 = 0xFFFF;
Imp->Machine = Machine;
Imp->Machine = Config->Machine;
Imp->SizeOfData = ImpSize;
if (Ordinal > 0)
Imp->OrdinalHint = Ordinal;
@ -476,12 +466,15 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym,
return {MemoryBufferRef(StringRef(Buf, Size), DLLName)};
}
std::error_code writeImportLibrary(StringRef DLLName, StringRef Path,
ArrayRef<COFFShortExport> Exports,
MachineTypes Machine) {
// 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;
ObjectFactory OF(llvm::sys::path::filename(DLLName), Machine);
std::string Path = getImplibPath();
std::string DLLName = sys::path::filename(Config->OutputFile);
ObjectFactory OF(DLLName);
std::vector<uint8_t> ImportDescriptor;
Members.push_back(OF.createImportDescriptor(ImportDescriptor));
@ -492,7 +485,7 @@ std::error_code writeImportLibrary(StringRef DLLName, StringRef Path,
std::vector<uint8_t> NullThunk;
Members.push_back(OF.createNullThunk(NullThunk));
for (COFFShortExport E : Exports) {
for (Export &E : Config->Exports) {
if (E.Private)
continue;
@ -502,26 +495,17 @@ std::error_code writeImportLibrary(StringRef DLLName, StringRef Path,
if (E.Constant)
ImportType = IMPORT_CONST;
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));
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));
}
std::pair<StringRef, std::error_code> Result =
writeArchive(Path, Members, /*WriteSymtab*/ true, object::Archive::K_GNU,
/*Deterministic*/ true, /*Thin*/ false);
return Result.second;
if (auto EC = Result.second)
fatal(EC, "failed to write " + Path);
}
} // namespace object
} // namespace llvm

View File

@ -1,6 +1,6 @@
//===--- COFFModuleDefinition.cpp - Simple DEF parser ---------------------===//
//===- COFF/ModuleDef.cpp -------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
@ -9,26 +9,27 @@
//
// 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 "llvm/Object/COFFModuleDefinition.h"
#include "Config.h"
#include "Error.h"
#include "Memory.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/COFFImportFile.h"
#include "llvm/Object/Error.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
using namespace llvm::COFF;
using namespace llvm;
namespace llvm {
namespace object {
namespace lld {
namespace coff {
namespace {
enum Kind {
Unknown,
@ -59,14 +60,9 @@ 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:
Lexer(StringRef S) : Buf(S) {}
explicit Lexer(StringRef S) : Buf(S) {}
Token lex() {
Buf = Buf.trim();
@ -120,14 +116,12 @@ private:
class Parser {
public:
explicit Parser(StringRef S, MachineTypes M) : Lex(S), Machine(M) {}
explicit Parser(StringRef S) : Lex(S) {}
Expected<COFFModuleDefinition> parse() {
void parse() {
do {
if (Error Err = parseOne())
return std::move(Err);
parseOne();
} while (Tok.K != Eof);
return Info;
}
private:
@ -140,83 +134,83 @@ private:
Stack.pop_back();
}
Error readAsInt(uint64_t *I) {
void readAsInt(uint64_t *I) {
read();
if (Tok.K != Identifier || Tok.Value.getAsInteger(10, *I))
return createError("integer expected");
return Error::success();
fatal("integer expected");
}
Error expect(Kind Expected, StringRef Msg) {
void expect(Kind Expected, StringRef Msg) {
read();
if (Tok.K != Expected)
return createError(Msg);
return Error::success();
fatal(Msg);
}
void unget() { Stack.push_back(Tok); }
Error parseOne() {
void parseOne() {
read();
switch (Tok.K) {
case Eof:
return Error::success();
return;
case KwExports:
for (;;) {
read();
if (Tok.K != Identifier) {
unget();
return Error::success();
return;
}
if (Error Err = parseExport())
return Err;
parseExport();
}
case KwHeapsize:
return parseNumbers(&Info.HeapReserve, &Info.HeapCommit);
parseNumbers(&Config->HeapReserve, &Config->HeapCommit);
return;
case KwStacksize:
return parseNumbers(&Info.StackReserve, &Info.StackCommit);
parseNumbers(&Config->StackReserve, &Config->StackCommit);
return;
case KwLibrary:
case KwName: {
bool IsDll = Tok.K == KwLibrary; // Check before parseName.
std::string Name;
if (Error Err = parseName(&Name, &Info.ImageBase))
return Err;
parseName(&Name, &Config->ImageBase);
// 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 (Info.OutputFile.empty())
Info.OutputFile = Name;
return Error::success();
if (Config->OutputFile.empty())
Config->OutputFile = Name;
return;
}
case KwVersion:
return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion);
parseVersion(&Config->MajorImageVersion, &Config->MinorImageVersion);
return;
default:
return createError("unknown directive: " + Tok.Value);
fatal("unknown directive: " + Tok.Value);
}
}
Error parseExport() {
COFFShortExport E;
void parseExport() {
Export E;
E.Name = Tok.Value;
read();
if (Tok.K == Equal) {
read();
if (Tok.K != Identifier)
return createError("identifier expected, but got " + Tok.Value);
fatal("identifier expected, but got " + Tok.Value);
E.ExtName = E.Name;
E.Name = Tok.Value;
} else {
unget();
}
if (Machine == IMAGE_FILE_MACHINE_I386) {
if (Config->Machine == I386) {
if (!isDecorated(E.Name))
E.Name = (std::string("_").append(E.Name));
E.Name = Saver.save("_" + E.Name);
if (!E.ExtName.empty() && !isDecorated(E.ExtName))
E.ExtName = (std::string("_").append(E.ExtName));
E.ExtName = Saver.save("_" + E.ExtName);
}
for (;;) {
@ -236,6 +230,7 @@ private:
continue;
}
if (Tok.K == KwConstant) {
warn("CONSTANT keyword is obsolete; use DATA");
E.Constant = true;
continue;
}
@ -244,76 +239,66 @@ private:
continue;
}
unget();
Info.Exports.push_back(E);
return Error::success();
Config->Exports.push_back(E);
return;
}
}
// HEAPSIZE/STACKSIZE reserve[,commit]
Error parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
if (Error Err = readAsInt(Reserve))
return Err;
void parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
readAsInt(Reserve);
read();
if (Tok.K != Comma) {
unget();
Commit = nullptr;
return Error::success();
return;
}
if (Error Err = readAsInt(Commit))
return Err;
return Error::success();
readAsInt(Commit);
}
// NAME outputPath [BASE=address]
Error parseName(std::string *Out, uint64_t *Baseaddr) {
void parseName(std::string *Out, uint64_t *Baseaddr) {
read();
if (Tok.K == Identifier) {
*Out = Tok.Value;
} else {
*Out = "";
unget();
return Error::success();
return;
}
read();
if (Tok.K == KwBase) {
if (Error Err = expect(Equal, "'=' expected"))
return Err;
if (Error Err = readAsInt(Baseaddr))
return Err;
expect(Equal, "'=' expected");
readAsInt(Baseaddr);
} else {
unget();
*Baseaddr = 0;
}
return Error::success();
}
// VERSION major[.minor]
Error parseVersion(uint32_t *Major, uint32_t *Minor) {
void parseVersion(uint32_t *Major, uint32_t *Minor) {
read();
if (Tok.K != Identifier)
return createError("identifier expected, but got " + Tok.Value);
fatal("identifier expected, but got " + Tok.Value);
StringRef V1, V2;
std::tie(V1, V2) = Tok.Value.split('.');
if (V1.getAsInteger(10, *Major))
return createError("integer expected, but got " + Tok.Value);
fatal("integer expected, but got " + Tok.Value);
if (V2.empty())
*Minor = 0;
else if (V2.getAsInteger(10, *Minor))
return createError("integer expected, but got " + Tok.Value);
return Error::success();
fatal("integer expected, but got " + Tok.Value);
}
Lexer Lex;
Token Tok;
std::vector<Token> Stack;
MachineTypes Machine;
COFFModuleDefinition Info;
};
Expected<COFFModuleDefinition> parseCOFFModuleDefinition(MemoryBufferRef MB,
MachineTypes Machine) {
return Parser(MB.getBuffer(), Machine).parse();
}
} // anonymous namespace
} // namespace object
} // namespace llvm
void parseModuleDefs(MemoryBufferRef MB) { Parser(MB.getBuffer()).parse(); }
} // namespace coff
} // namespace lld

View File

@ -9,15 +9,13 @@
//
// COFF short import file is a special kind of file which contains
// only symbol names for DLL-exported symbols. This class implements
// exporting of Symbols to create libraries and a SymbolicFile
// interface for the file type.
// SymbolicFile interface for the file.
//
//===----------------------------------------------------------------------===//
#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"
@ -70,36 +68,6 @@ 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

View File

@ -1,49 +0,0 @@
//===--- 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

View File

@ -2,8 +2,6 @@ add_llvm_library(LLVMObject
Archive.cpp
ArchiveWriter.cpp
Binary.cpp
COFFImportFile.cpp
COFFModuleDefinition.cpp
COFFObjectFile.cpp
Decompressor.cpp
ELF.cpp