COFFModuleDefinition.cpp 9.9 KB

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