28 using namespace llvm::COFF;
67 object_error::parse_failed);
83 size_t End = Buf.find(
'\n');
84 Buf = (End == Buf.npos) ?
"" : Buf.drop_front(End);
88 Buf = Buf.drop_front();
90 if (Buf.startswith(
"="))
91 Buf = Buf.drop_front();
94 Buf = Buf.drop_front();
102 size_t End = Buf.find_first_of(
"=,\r\n \t\v");
117 Buf = (End == Buf.npos) ?
"" : Buf.drop_front(End);
118 return Token(K, Word);
130 : Lex(S),
Machine(M), MingwDef(B) {}
134 if (
Error Err = parseOne())
135 return std::move(Err);
136 }
while (Tok.K !=
Eof);
150 Error readAsInt(uint64_t *
I) {
152 if (Tok.K !=
Identifier || Tok.Value.getAsInteger(10, *I))
159 if (Tok.K != Expected)
164 void unget() {
Stack.push_back(Tok); }
178 if (
Error Err = parseExport())
182 return parseNumbers(&Info.HeapReserve, &Info.HeapCommit);
184 return parseNumbers(&Info.StackReserve, &Info.StackCommit);
189 if (
Error Err = parseName(&Name, &Info.ImageBase))
192 Info.ImportName =
Name;
195 if (Info.OutputFile.empty()) {
196 Info.OutputFile =
Name;
199 Info.OutputFile += IsDll ?
".dll" :
".exe";
205 return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion);
207 return createError(
"unknown directive: " + Tok.Value);
211 Error parseExport() {
215 if (Tok.K ==
Equal) {
218 return createError(
"identifier expected, but got " + Tok.Value);
227 E.Name = (std::string(
"_").append(E.Name));
228 if (!E.ExtName.empty() && !
isDecorated(E.ExtName, MingwDef))
229 E.ExtName = (std::string(
"_").append(E.ExtName));
234 if (Tok.K ==
Identifier && Tok.Value[0] ==
'@') {
235 if (Tok.Value.drop_front().getAsInteger(10, E.Ordinal)) {
239 Info.Exports.push_back(E);
263 Info.Exports.push_back(E);
269 Error parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
270 if (
Error Err = readAsInt(Reserve))
273 if (Tok.K !=
Comma) {
278 if (
Error Err = readAsInt(Commit))
284 Error parseName(std::string *Out, uint64_t *Baseaddr) {
295 if (
Error Err = expect(
Equal,
"'=' expected"))
297 if (
Error Err = readAsInt(Baseaddr))
310 return createError(
"identifier expected, but got " + Tok.Value);
312 std::tie(V1, V2) = Tok.Value.split(
'.');
314 return createError(
"integer expected, but got " + Tok.Value);
318 return createError(
"integer expected, but got " + Tok.Value);
324 std::vector<Token>
Stack;
326 COFFModuleDefinition Info;
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
static bool isDecorated(StringRef Sym, bool MingwDef)
Parser(StringRef S, MachineTypes M, bool B)
Expected< COFFModuleDefinition > parseCOFFModuleDefinition(MemoryBufferRef MB, COFF::MachineTypes Machine, bool MingwDef=false)
std::string str() const
Return the twine contents as a std::string.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
support::ulittle32_t Word
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Tagged union holding either a T or a Error.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
static Error createError(const Twine &Err)
A switch()-like statement whose cases are string literals.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
static const unsigned End
Token(Kind T=Unknown, StringRef S="")
static ErrorSuccess success()
Create a success value.
StringRef getBuffer() const
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Expected< COFFModuleDefinition > parse()
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
LLVM Value Representation.
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.