COFFModuleDefinition.cpp 9.9 KB

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