LLVM: lib/Object/COFFModuleDefinition.cpp Source File (original) (raw)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
79 (!MingwDef && Sym.contains('@'));
80}
83public:
85
87 Buf = Buf.trim();
88 if (Buf.empty())
90
91 switch (Buf[0]) {
92 case '\0':
94 case ';': {
95 size_t End = Buf.find('\n');
96 Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
97 return lex();
98 }
99 case '=':
100 Buf = Buf.drop_front();
101 if (Buf.consume_front("="))
104 case ',':
105 Buf = Buf.drop_front();
107 case '"': {
109 std::tie(S, Buf) = Buf.substr(1).split('"');
111 }
112 default: {
113 size_t End = Buf.find_first_of("=,;\r\n \t\v");
114 StringRef Word = Buf.substr(0, End);
129 Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
130 return Token(K, Word);
131 }
132 }
133 }
134
135private:
137};
140public:
142 : Lex(S), Machine(M), MingwDef(B), AddUnderscores(AU) {
144 AddUnderscores = false;
145 }
146
148 do {
149 if (Error Err = parseOne())
150 return std::move(Err);
151 } while (Tok.K != Eof);
152 return Info;
153 }
154
155private:
156 void read() {
157 if (Stack.empty()) {
158 Tok = Lex.lex();
159 return;
160 }
161 Tok = Stack.back();
162 Stack.pop_back();
163 }
164
166 read();
170 }
171
172 Error expect(Kind Expected, StringRef Msg) {
173 read();
174 if (Tok.K != Expected)
177 }
178
179 void unget() { Stack.push_back(Tok); }
180
181 Error parseOne() {
182 read();
183 switch (Tok.K) {
184 case Eof:
187 for (;;) {
188 read();
190 unget();
192 }
193 if (Error Err = parseExport())
194 return Err;
195 }
197 return parseNumbers(&Info.HeapReserve, &Info.HeapCommit);
199 return parseNumbers(&Info.StackReserve, &Info.StackCommit);
202 bool IsDll = Tok.K == KwLibrary;
203 std::string Name;
204 if (Error Err = parseName(&Name, &Info.ImageBase))
205 return Err;
206
207 Info.ImportName = Name;
208
209
210 if (Info.OutputFile.empty()) {
211 Info.OutputFile = Name;
212
214 Info.OutputFile += IsDll ? ".dll" : ".exe";
215 }
216
218 }
220 return parseVersion(&Info.MajorImageVersion, &Info.MinorImageVersion);
221 default:
222 return createError("unknown directive: " + Tok.Value);
223 }
224 }
225
226 Error parseExport() {
227 COFFShortExport E;
228 E.Name = std::string(Tok.Value);
229 read();
230 if (Tok.K == Equal) {
231 read();
233 return createError("identifier expected, but got " + Tok.Value);
235 E.Name = std::string(Tok.Value);
236 } else {
237 unget();
238 }
239
240 if (AddUnderscores) {
241
242
244 (E.ExtName.empty() || !StringRef(E.Name).contains(".")))
245 E.Name = (std::string("_").append(E.Name));
246 if (.ExtName.empty() &&
(E.ExtName, MingwDef))
247 E.ExtName = (std::string("_").append(E.ExtName));
248 }
249
250 for (;;) {
251 read();
252 if (Tok.K == Identifier && Tok.Value[0] == '@') {
253 if (Tok.Value == "@") {
254
255 read();
256 Tok.Value.getAsInteger(10, E.Ordinal);
257 } else if (Tok.Value.drop_front().getAsInteger(10, E.Ordinal)) {
258
259
260 unget();
261 Info.Exports.push_back(E);
263 }
264
265 read();
267 E.Noname = true;
268 } else {
269 unget();
270 }
271 continue;
272 }
273 if (Tok.K == KwData) {
274 E.Data = true;
275 continue;
276 }
278 E.Constant = true;
279 continue;
280 }
282 E.Private = true;
283 continue;
284 }
286 read();
287 E.ImportName = std::string(Tok.Value);
288 continue;
289 }
290
292 read();
293 if (Tok.K == Eof)
295 "unexpected end of file, EXPORTAS identifier expected");
296 E.ExportAs = std::string(Tok.Value);
297 } else {
298 unget();
299 }
300 Info.Exports.push_back(E);
302 }
303 }
304
305
306 Error parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
307 if (Error Err = readAsInt(Reserve))
308 return Err;
309 read();
310 if (Tok.K != Comma) {
311 unget();
312 Commit = nullptr;
314 }
315 if (Error Err = readAsInt(Commit))
316 return Err;
318 }
319
320
321 Error parseName(std::string *Out, uint64_t *Baseaddr) {
322 read();
324 *Out = std::string(Tok.Value);
325 } else {
326 *Out = "";
327 unget();
329 }
330 read();
331 if (Tok.K == KwBase) {
332 if (Error Err = expect(Equal, "'=' expected"))
333 return Err;
334 if (Error Err = readAsInt(Baseaddr))
335 return Err;
336 } else {
337 unget();
338 *Baseaddr = 0;
339 }
341 }
342
343
344 Error parseVersion(uint32_t *Major, uint32_t *Minor) {
345 read();
347 return createError("identifier expected, but got " + Tok.Value);
348 StringRef V1, V2;
349 std::tie(V1, V2) = Tok.Value.split('.');
351 return createError("integer expected, but got " + Tok.Value);
353 *Minor = 0;
355 return createError("integer expected, but got " + Tok.Value);
357 }
358
359 Lexer Lex;
360 Token Tok;
361 std::vector Stack;
363 COFFModuleDefinition Info;
364 bool MingwDef;
365 bool AddUnderscores;
366};
370 bool MingwDef,
371 bool AddUnderscores) {
373}