annotate lib/Object/COFFModuleDefinition.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents
children 3a76565eade5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===--- COFFModuleDefinition.cpp - Simple DEF parser ---------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // Windows-specific.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 // A parser for the module-definition file (.def file).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 // The format of module-definition files are described in this document:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 // https://msdn.microsoft.com/en-us/library/28d6s79h.aspx
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/Object/COFFModuleDefinition.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/ADT/StringRef.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/ADT/StringSwitch.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/Object/COFF.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/Object/COFFImportFile.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include "llvm/Object/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include "llvm/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include "llvm/Support/Path.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #include "llvm/Support/raw_ostream.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 using namespace llvm::COFF;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 namespace object {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 enum Kind {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 Unknown,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 Eof,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 Identifier,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 Comma,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 Equal,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 KwBase,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 KwConstant,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 KwData,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 KwExports,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 KwHeapsize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 KwLibrary,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 KwName,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 KwNoname,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 KwPrivate,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 KwStacksize,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 KwVersion,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 struct Token {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 explicit Token(Kind T = Unknown, StringRef S = "") : K(T), Value(S) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 Kind K;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 StringRef Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 static bool isDecorated(StringRef Sym, bool MingwDef) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 // In def files, the symbols can either be listed decorated or undecorated.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 // - For cdecl symbols, only the undecorated form is allowed.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 // - For fastcall and vectorcall symbols, both fully decorated or
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 // undecorated forms can be present.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 // - For stdcall symbols in non-MinGW environments, the decorated form is
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 // fully decorated with leading underscore and trailing stack argument
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 // size - like "_Func@0".
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 // - In MinGW def files, a decorated stdcall symbol does not include the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 // leading underscore though, like "Func@0".
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 // This function controls whether a leading underscore should be added to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 // the given symbol name or not. For MinGW, treat a stdcall symbol name such
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 // as "Func@0" as undecorated, i.e. a leading underscore must be added.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 // For non-MinGW, look for '@' in the whole string and consider "_Func@0"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 // as decorated, i.e. don't add any more leading underscores.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 // We can't check for a leading underscore here, since function names
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 // themselves can start with an underscore, while a second one still needs
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 // to be added.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 return Sym.startswith("@") || Sym.contains("@@") || Sym.startswith("?") ||
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 (!MingwDef && Sym.contains('@'));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 static Error createError(const Twine &Err) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 return make_error<StringError>(StringRef(Err.str()),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 object_error::parse_failed);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 class Lexer {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 Lexer(StringRef S) : Buf(S) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 Token lex() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 Buf = Buf.trim();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 if (Buf.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 return Token(Eof);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 switch (Buf[0]) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 case '\0':
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 return Token(Eof);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 case ';': {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 size_t End = Buf.find('\n');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 return lex();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 case '=':
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 Buf = Buf.drop_front();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 // GNU dlltool accepts both = and ==.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 if (Buf.startswith("="))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 Buf = Buf.drop_front();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 return Token(Equal, "=");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 case ',':
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 Buf = Buf.drop_front();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 return Token(Comma, ",");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 case '"': {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 StringRef S;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 std::tie(S, Buf) = Buf.substr(1).split('"');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 return Token(Identifier, S);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 default: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 size_t End = Buf.find_first_of("=,\r\n \t\v");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 StringRef Word = Buf.substr(0, End);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 Kind K = llvm::StringSwitch<Kind>(Word)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 .Case("BASE", KwBase)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 .Case("CONSTANT", KwConstant)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 .Case("DATA", KwData)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 .Case("EXPORTS", KwExports)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 .Case("HEAPSIZE", KwHeapsize)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 .Case("LIBRARY", KwLibrary)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 .Case("NAME", KwName)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 .Case("NONAME", KwNoname)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 .Case("PRIVATE", KwPrivate)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 .Case("STACKSIZE", KwStacksize)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 .Case("VERSION", KwVersion)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 .Default(Identifier);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 return Token(K, Word);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 StringRef Buf;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 class Parser {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 explicit Parser(StringRef S, MachineTypes M, bool B)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 : Lex(S), Machine(M), MingwDef(B) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 Expected<COFFModuleDefinition> parse() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 do {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 if (Error Err = parseOne())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 return std::move(Err);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 } while (Tok.K != Eof);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 return Info;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 void read() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 if (Stack.empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 Tok = Lex.lex();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 Tok = Stack.back();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 Stack.pop_back();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 Error readAsInt(uint64_t *I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 if (Tok.K != Identifier || Tok.Value.getAsInteger(10, *I))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 return createError("integer expected");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 Error expect(Kind Expected, StringRef Msg) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 if (Tok.K != Expected)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 return createError(Msg);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 void unget() { Stack.push_back(Tok); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 Error parseOne() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 switch (Tok.K) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 case Eof:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 case KwExports:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 for (;;) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 if (Tok.K != Identifier) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 if (Error Err = parseExport())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 return Err;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 case KwHeapsize:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 return parseNumbers(&Info.HeapReserve, &Info.HeapCommit);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 case KwStacksize:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 return parseNumbers(&Info.StackReserve, &Info.StackCommit);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 case KwLibrary:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 case KwName: {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 bool IsDll = Tok.K == KwLibrary; // Check before parseName.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 std::string Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 if (Error Err = parseName(&Name, &Info.ImageBase))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 return Err;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 Info.ImportName = Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 // Set the output file, but don't override /out if it was already passed.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 if (Info.OutputFile.empty()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 Info.OutputFile = Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 // Append the appropriate file extension if not already present.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 if (!sys::path::has_extension(Name))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 Info.OutputFile += IsDll ? ".dll" : ".exe";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 case KwVersion:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 return createError("unknown directive: " + Tok.Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 Error parseExport() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 COFFShortExport E;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 E.Name = Tok.Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 if (Tok.K == Equal) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 if (Tok.K != Identifier)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 return createError("identifier expected, but got " + Tok.Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 E.ExtName = E.Name;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 E.Name = Tok.Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 if (Machine == IMAGE_FILE_MACHINE_I386) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 if (!isDecorated(E.Name, MingwDef))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 E.Name = (std::string("_").append(E.Name));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 if (!E.ExtName.empty() && !isDecorated(E.ExtName, MingwDef))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 E.ExtName = (std::string("_").append(E.ExtName));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 for (;;) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 if (Tok.K == Identifier && Tok.Value[0] == '@') {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 if (Tok.Value == "@") {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 // "foo @ 10"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256 Tok.Value.getAsInteger(10, E.Ordinal);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 } else if (Tok.Value.drop_front().getAsInteger(10, E.Ordinal)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 // "foo \n @bar" - Not an ordinal modifier at all, but the next
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 // export (fastcall decorated) - complete the current one.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 Info.Exports.push_back(E);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 // "foo @10"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 if (Tok.K == KwNoname) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 E.Noname = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273 if (Tok.K == KwData) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 E.Data = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 if (Tok.K == KwConstant) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278 E.Constant = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 if (Tok.K == KwPrivate) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 E.Private = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286 Info.Exports.push_back(E);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 // HEAPSIZE/STACKSIZE reserve[,commit]
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 Error parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 if (Error Err = readAsInt(Reserve))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 return Err;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 if (Tok.K != Comma) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 Commit = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 if (Error Err = readAsInt(Commit))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 return Err;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 // NAME outputPath [BASE=address]
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 Error parseName(std::string *Out, uint64_t *Baseaddr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 if (Tok.K == Identifier) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 *Out = Tok.Value;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 *Out = "";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317 if (Tok.K == KwBase) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 if (Error Err = expect(Equal, "'=' expected"))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 return Err;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320 if (Error Err = readAsInt(Baseaddr))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 return Err;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 unget();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 *Baseaddr = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 // VERSION major[.minor]
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 Error parseVersion(uint32_t *Major, uint32_t *Minor) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 read();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 if (Tok.K != Identifier)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 return createError("identifier expected, but got " + Tok.Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 StringRef V1, V2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 std::tie(V1, V2) = Tok.Value.split('.');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 if (V1.getAsInteger(10, *Major))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 return createError("integer expected, but got " + Tok.Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338 if (V2.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339 *Minor = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 else if (V2.getAsInteger(10, *Minor))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 return createError("integer expected, but got " + Tok.Value);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 Lexer Lex;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 Token Tok;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 std::vector<Token> Stack;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348 MachineTypes Machine;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 COFFModuleDefinition Info;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 bool MingwDef;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 Expected<COFFModuleDefinition> parseCOFFModuleDefinition(MemoryBufferRef MB,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 MachineTypes Machine,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355 bool MingwDef) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 return Parser(MB.getBuffer(), Machine, MingwDef).parse();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359 } // namespace object
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
360 } // namespace llvm