2017-06-03 01:53:06 +08:00
|
|
|
//===--- COFFModuleDefinition.cpp - Simple DEF parser ---------------------===//
|
2017-05-21 03:56:29 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-21 03:56:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Windows-specific.
|
|
|
|
// A parser for the module-definition file (.def file).
|
|
|
|
//
|
|
|
|
// The format of module-definition files are described in this document:
|
|
|
|
// https://msdn.microsoft.com/en-us/library/28d6s79h.aspx
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
#include "llvm/Object/COFFModuleDefinition.h"
|
2017-05-21 03:56:29 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-06-03 01:53:06 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Object/COFFImportFile.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2017-07-19 06:11:00 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-05-21 03:56:29 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
using namespace llvm::COFF;
|
2017-05-21 03:56:29 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
2017-05-21 03:56:29 +08:00
|
|
|
|
|
|
|
enum Kind {
|
|
|
|
Unknown,
|
|
|
|
Eof,
|
|
|
|
Identifier,
|
|
|
|
Comma,
|
|
|
|
Equal,
|
2018-05-09 17:21:53 +08:00
|
|
|
EqualEqual,
|
2017-05-21 03:56:29 +08:00
|
|
|
KwBase,
|
|
|
|
KwConstant,
|
|
|
|
KwData,
|
|
|
|
KwExports,
|
|
|
|
KwHeapsize,
|
|
|
|
KwLibrary,
|
|
|
|
KwName,
|
|
|
|
KwNoname,
|
|
|
|
KwPrivate,
|
|
|
|
KwStacksize,
|
|
|
|
KwVersion,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Token {
|
|
|
|
explicit Token(Kind T = Unknown, StringRef S = "") : K(T), Value(S) {}
|
|
|
|
Kind K;
|
|
|
|
StringRef Value;
|
|
|
|
};
|
|
|
|
|
2017-07-19 05:26:38 +08:00
|
|
|
static bool isDecorated(StringRef Sym, bool MingwDef) {
|
2017-10-23 17:08:13 +08:00
|
|
|
// In def files, the symbols can either be listed decorated or undecorated.
|
|
|
|
//
|
|
|
|
// - For cdecl symbols, only the undecorated form is allowed.
|
|
|
|
// - For fastcall and vectorcall symbols, both fully decorated or
|
|
|
|
// undecorated forms can be present.
|
|
|
|
// - For stdcall symbols in non-MinGW environments, the decorated form is
|
|
|
|
// fully decorated with leading underscore and trailing stack argument
|
|
|
|
// size - like "_Func@0".
|
|
|
|
// - In MinGW def files, a decorated stdcall symbol does not include the
|
|
|
|
// leading underscore though, like "Func@0".
|
|
|
|
|
|
|
|
// This function controls whether a leading underscore should be added to
|
|
|
|
// the given symbol name or not. For MinGW, treat a stdcall symbol name such
|
|
|
|
// as "Func@0" as undecorated, i.e. a leading underscore must be added.
|
|
|
|
// For non-MinGW, look for '@' in the whole string and consider "_Func@0"
|
|
|
|
// as decorated, i.e. don't add any more leading underscores.
|
|
|
|
// We can't check for a leading underscore here, since function names
|
|
|
|
// themselves can start with an underscore, while a second one still needs
|
|
|
|
// to be added.
|
|
|
|
return Sym.startswith("@") || Sym.contains("@@") || Sym.startswith("?") ||
|
|
|
|
(!MingwDef && Sym.contains('@'));
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
static Error createError(const Twine &Err) {
|
|
|
|
return make_error<StringError>(StringRef(Err.str()),
|
|
|
|
object_error::parse_failed);
|
|
|
|
}
|
|
|
|
|
2017-05-21 03:56:29 +08:00
|
|
|
class Lexer {
|
|
|
|
public:
|
2017-06-03 01:53:06 +08:00
|
|
|
Lexer(StringRef S) : Buf(S) {}
|
2017-05-21 03:56:29 +08:00
|
|
|
|
|
|
|
Token lex() {
|
|
|
|
Buf = Buf.trim();
|
|
|
|
if (Buf.empty())
|
|
|
|
return Token(Eof);
|
|
|
|
|
|
|
|
switch (Buf[0]) {
|
|
|
|
case '\0':
|
|
|
|
return Token(Eof);
|
|
|
|
case ';': {
|
|
|
|
size_t End = Buf.find('\n');
|
|
|
|
Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
|
|
|
|
return lex();
|
|
|
|
}
|
|
|
|
case '=':
|
|
|
|
Buf = Buf.drop_front();
|
2018-05-09 17:21:53 +08:00
|
|
|
if (Buf.startswith("=")) {
|
2017-07-19 05:26:38 +08:00
|
|
|
Buf = Buf.drop_front();
|
2018-05-09 17:21:53 +08:00
|
|
|
return Token(EqualEqual, "==");
|
|
|
|
}
|
2017-05-21 03:56:29 +08:00
|
|
|
return Token(Equal, "=");
|
|
|
|
case ',':
|
|
|
|
Buf = Buf.drop_front();
|
|
|
|
return Token(Comma, ",");
|
|
|
|
case '"': {
|
|
|
|
StringRef S;
|
|
|
|
std::tie(S, Buf) = Buf.substr(1).split('"');
|
|
|
|
return Token(Identifier, S);
|
|
|
|
}
|
|
|
|
default: {
|
2017-12-07 03:18:24 +08:00
|
|
|
size_t End = Buf.find_first_of("=,;\r\n \t\v");
|
2017-05-21 03:56:29 +08:00
|
|
|
StringRef Word = Buf.substr(0, End);
|
|
|
|
Kind K = llvm::StringSwitch<Kind>(Word)
|
|
|
|
.Case("BASE", KwBase)
|
|
|
|
.Case("CONSTANT", KwConstant)
|
|
|
|
.Case("DATA", KwData)
|
|
|
|
.Case("EXPORTS", KwExports)
|
|
|
|
.Case("HEAPSIZE", KwHeapsize)
|
|
|
|
.Case("LIBRARY", KwLibrary)
|
|
|
|
.Case("NAME", KwName)
|
|
|
|
.Case("NONAME", KwNoname)
|
|
|
|
.Case("PRIVATE", KwPrivate)
|
|
|
|
.Case("STACKSIZE", KwStacksize)
|
|
|
|
.Case("VERSION", KwVersion)
|
|
|
|
.Default(Identifier);
|
|
|
|
Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
|
|
|
|
return Token(K, Word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef Buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Parser {
|
|
|
|
public:
|
2017-07-19 05:26:38 +08:00
|
|
|
explicit Parser(StringRef S, MachineTypes M, bool B)
|
|
|
|
: Lex(S), Machine(M), MingwDef(B) {}
|
2017-05-21 03:56:29 +08:00
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
Expected<COFFModuleDefinition> parse() {
|
2017-05-21 03:56:29 +08:00
|
|
|
do {
|
2017-06-03 01:53:06 +08:00
|
|
|
if (Error Err = parseOne())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
2017-05-21 03:56:29 +08:00
|
|
|
} while (Tok.K != Eof);
|
2017-06-03 01:53:06 +08:00
|
|
|
return Info;
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void read() {
|
|
|
|
if (Stack.empty()) {
|
|
|
|
Tok = Lex.lex();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Tok = Stack.back();
|
|
|
|
Stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
Error readAsInt(uint64_t *I) {
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K != Identifier || Tok.Value.getAsInteger(10, *I))
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError("integer expected");
|
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
Error expect(Kind Expected, StringRef Msg) {
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K != Expected)
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError(Msg);
|
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void unget() { Stack.push_back(Tok); }
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
Error parseOne() {
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
switch (Tok.K) {
|
|
|
|
case Eof:
|
2017-06-03 01:53:06 +08:00
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
case KwExports:
|
|
|
|
for (;;) {
|
|
|
|
read();
|
|
|
|
if (Tok.K != Identifier) {
|
|
|
|
unget();
|
2017-06-03 01:53:06 +08:00
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
2017-06-03 01:53:06 +08:00
|
|
|
if (Error Err = parseExport())
|
|
|
|
return Err;
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
case KwHeapsize:
|
2017-06-03 01:53:06 +08:00
|
|
|
return parseNumbers(&Info.HeapReserve, &Info.HeapCommit);
|
2017-05-21 03:56:29 +08:00
|
|
|
case KwStacksize:
|
2017-06-03 01:53:06 +08:00
|
|
|
return parseNumbers(&Info.StackReserve, &Info.StackCommit);
|
2017-05-21 03:56:29 +08:00
|
|
|
case KwLibrary:
|
|
|
|
case KwName: {
|
|
|
|
bool IsDll = Tok.K == KwLibrary; // Check before parseName.
|
|
|
|
std::string Name;
|
2017-06-03 01:53:06 +08:00
|
|
|
if (Error Err = parseName(&Name, &Info.ImageBase))
|
|
|
|
return Err;
|
2017-07-19 10:01:22 +08:00
|
|
|
|
|
|
|
Info.ImportName = Name;
|
2017-05-21 03:56:29 +08:00
|
|
|
|
|
|
|
// Set the output file, but don't override /out if it was already passed.
|
2017-07-19 10:01:22 +08:00
|
|
|
if (Info.OutputFile.empty()) {
|
2017-06-03 01:53:06 +08:00
|
|
|
Info.OutputFile = Name;
|
2017-07-19 10:01:22 +08:00
|
|
|
// Append the appropriate file extension if not already present.
|
|
|
|
if (!sys::path::has_extension(Name))
|
|
|
|
Info.OutputFile += IsDll ? ".dll" : ".exe";
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
case KwVersion:
|
2017-06-03 01:53:06 +08:00
|
|
|
return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion);
|
2017-05-21 03:56:29 +08:00
|
|
|
default:
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError("unknown directive: " + Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
Error parseExport() {
|
|
|
|
COFFShortExport E;
|
2020-01-29 03:23:46 +08:00
|
|
|
E.Name = std::string(Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K == Equal) {
|
|
|
|
read();
|
|
|
|
if (Tok.K != Identifier)
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError("identifier expected, but got " + Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
E.ExtName = E.Name;
|
2020-01-29 03:23:46 +08:00
|
|
|
E.Name = std::string(Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
} else {
|
|
|
|
unget();
|
|
|
|
}
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
if (Machine == IMAGE_FILE_MACHINE_I386) {
|
2017-07-19 05:26:38 +08:00
|
|
|
if (!isDecorated(E.Name, MingwDef))
|
2017-06-03 01:53:06 +08:00
|
|
|
E.Name = (std::string("_").append(E.Name));
|
2017-07-19 05:26:38 +08:00
|
|
|
if (!E.ExtName.empty() && !isDecorated(E.ExtName, MingwDef))
|
2017-06-03 01:53:06 +08:00
|
|
|
E.ExtName = (std::string("_").append(E.ExtName));
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
read();
|
|
|
|
if (Tok.K == Identifier && Tok.Value[0] == '@') {
|
2017-10-27 04:11:32 +08:00
|
|
|
if (Tok.Value == "@") {
|
|
|
|
// "foo @ 10"
|
|
|
|
read();
|
|
|
|
Tok.Value.getAsInteger(10, E.Ordinal);
|
|
|
|
} else if (Tok.Value.drop_front().getAsInteger(10, E.Ordinal)) {
|
|
|
|
// "foo \n @bar" - Not an ordinal modifier at all, but the next
|
|
|
|
// export (fastcall decorated) - complete the current one.
|
2017-08-16 13:18:36 +08:00
|
|
|
unget();
|
|
|
|
Info.Exports.push_back(E);
|
|
|
|
return Error::success();
|
|
|
|
}
|
2017-10-27 04:11:32 +08:00
|
|
|
// "foo @10"
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K == KwNoname) {
|
|
|
|
E.Noname = true;
|
|
|
|
} else {
|
|
|
|
unget();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tok.K == KwData) {
|
|
|
|
E.Data = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tok.K == KwConstant) {
|
|
|
|
E.Constant = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Tok.K == KwPrivate) {
|
|
|
|
E.Private = true;
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-09 17:21:53 +08:00
|
|
|
if (Tok.K == EqualEqual) {
|
|
|
|
read();
|
2020-01-29 03:23:46 +08:00
|
|
|
E.AliasTarget = std::string(Tok.Value);
|
2018-05-09 17:21:53 +08:00
|
|
|
if (Machine == IMAGE_FILE_MACHINE_I386 && !isDecorated(E.AliasTarget, MingwDef))
|
|
|
|
E.AliasTarget = std::string("_").append(E.AliasTarget);
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-21 03:56:29 +08:00
|
|
|
unget();
|
2017-06-03 01:53:06 +08:00
|
|
|
Info.Exports.push_back(E);
|
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEAPSIZE/STACKSIZE reserve[,commit]
|
2017-06-03 01:53:06 +08:00
|
|
|
Error parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
|
|
|
|
if (Error Err = readAsInt(Reserve))
|
|
|
|
return Err;
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K != Comma) {
|
|
|
|
unget();
|
|
|
|
Commit = nullptr;
|
2017-06-03 01:53:06 +08:00
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
2017-06-03 01:53:06 +08:00
|
|
|
if (Error Err = readAsInt(Commit))
|
|
|
|
return Err;
|
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NAME outputPath [BASE=address]
|
2017-06-03 01:53:06 +08:00
|
|
|
Error parseName(std::string *Out, uint64_t *Baseaddr) {
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K == Identifier) {
|
2020-01-29 03:23:46 +08:00
|
|
|
*Out = std::string(Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
} else {
|
|
|
|
*Out = "";
|
|
|
|
unget();
|
2017-06-03 01:53:06 +08:00
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
read();
|
|
|
|
if (Tok.K == KwBase) {
|
2017-06-03 01:53:06 +08:00
|
|
|
if (Error Err = expect(Equal, "'=' expected"))
|
|
|
|
return Err;
|
|
|
|
if (Error Err = readAsInt(Baseaddr))
|
|
|
|
return Err;
|
2017-05-21 03:56:29 +08:00
|
|
|
} else {
|
|
|
|
unget();
|
|
|
|
*Baseaddr = 0;
|
|
|
|
}
|
2017-06-03 01:53:06 +08:00
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// VERSION major[.minor]
|
2017-06-03 01:53:06 +08:00
|
|
|
Error parseVersion(uint32_t *Major, uint32_t *Minor) {
|
2017-05-21 03:56:29 +08:00
|
|
|
read();
|
|
|
|
if (Tok.K != Identifier)
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError("identifier expected, but got " + Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
StringRef V1, V2;
|
|
|
|
std::tie(V1, V2) = Tok.Value.split('.');
|
|
|
|
if (V1.getAsInteger(10, *Major))
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError("integer expected, but got " + Tok.Value);
|
2017-05-21 03:56:29 +08:00
|
|
|
if (V2.empty())
|
|
|
|
*Minor = 0;
|
|
|
|
else if (V2.getAsInteger(10, *Minor))
|
2017-06-03 01:53:06 +08:00
|
|
|
return createError("integer expected, but got " + Tok.Value);
|
|
|
|
return Error::success();
|
2017-05-21 03:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Lexer Lex;
|
|
|
|
Token Tok;
|
|
|
|
std::vector<Token> Stack;
|
2017-06-03 01:53:06 +08:00
|
|
|
MachineTypes Machine;
|
|
|
|
COFFModuleDefinition Info;
|
2017-07-19 05:26:38 +08:00
|
|
|
bool MingwDef;
|
2017-05-21 03:56:29 +08:00
|
|
|
};
|
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
Expected<COFFModuleDefinition> parseCOFFModuleDefinition(MemoryBufferRef MB,
|
2017-07-19 05:26:38 +08:00
|
|
|
MachineTypes Machine,
|
|
|
|
bool MingwDef) {
|
|
|
|
return Parser(MB.getBuffer(), Machine, MingwDef).parse();
|
2017-06-03 01:53:06 +08:00
|
|
|
}
|
2017-05-21 03:56:29 +08:00
|
|
|
|
2017-06-03 01:53:06 +08:00
|
|
|
} // namespace object
|
|
|
|
} // namespace llvm
|