aboutsummaryrefslogtreecommitdiff
path: root/COFF/ModuleDef.cpp
blob: 5e393f45d18412414babceaa2bb9d567ee2955fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//===- COFF/ModuleDef.cpp -------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Windows-specific.
// A parser for the module-definition file (.def file).
// Parsed results are directly written to Config global variable.
//
// The format of module-definition files are described in this document:
// https://msdn.microsoft.com/en-us/library/28d6s79h.aspx
//
//===----------------------------------------------------------------------===//

#include "Config.h"
#include "Error.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>

using namespace llvm;

namespace lld {
namespace coff {
namespace {

enum Kind {
  Unknown,
  Eof,
  Identifier,
  Comma,
  Equal,
  KwBase,
  KwData,
  KwExports,
  KwHeapsize,
  KwLibrary,
  KwName,
  KwNoname,
  KwPrivate,
  KwStacksize,
  KwVersion,
};

struct Token {
  explicit Token(Kind T = Unknown, StringRef S = "") : K(T), Value(S) {}
  Kind K;
  StringRef Value;
};

static bool isDecorated(StringRef Sym) {
  return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?");
}

class Lexer {
public:
  explicit Lexer(StringRef S) : Buf(S) {}

  Token lex() {
    Buf = Buf.trim();
    if (Buf.empty())
      return Token(Eof);

    switch (Buf[0]) {
    case '\0':
      return Token(Eof);
    case ';': {
      size_t End = Buf.find('\n');
      Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
      return lex();
    }
    case '=':
      Buf = Buf.drop_front();
      return Token(Equal, "=");
    case ',':
      Buf = Buf.drop_front();
      return Token(Comma, ",");
    case '"': {
      StringRef S;
      std::tie(S, Buf) = Buf.substr(1).split('"');
      return Token(Identifier, S);
    }
    default: {
      size_t End = Buf.find_first_of("=,\r\n \t\v");
      StringRef Word = Buf.substr(0, End);
      Kind K = llvm::StringSwitch<Kind>(Word)
                   .Case("BASE", KwBase)
                   .Case("DATA", KwData)
                   .Case("EXPORTS", KwExports)
                   .Case("HEAPSIZE", KwHeapsize)
                   .Case("LIBRARY", KwLibrary)
                   .Case("NAME", KwName)
                   .Case("NONAME", KwNoname)
                   .Case("PRIVATE", KwPrivate)
                   .Case("STACKSIZE", KwStacksize)
                   .Case("VERSION", KwVersion)
                   .Default(Identifier);
      Buf = (End == Buf.npos) ? "" : Buf.drop_front(End);
      return Token(K, Word);
    }
    }
  }

private:
  StringRef Buf;
};

class Parser {
public:
  explicit Parser(StringRef S, StringSaver *A) : Lex(S), Alloc(A) {}

  void parse() {
    do {
      parseOne();
    } while (Tok.K != Eof);
  }

private:
  void read() {
    if (Stack.empty()) {
      Tok = Lex.lex();
      return;
    }
    Tok = Stack.back();
    Stack.pop_back();
  }

  void readAsInt(uint64_t *I) {
    read();
    if (Tok.K != Identifier || Tok.Value.getAsInteger(10, *I))
      fatal("integer expected");
  }

  void expect(Kind Expected, StringRef Msg) {
    read();
    if (Tok.K != Expected)
      fatal(Msg);
  }

  void unget() { Stack.push_back(Tok); }

  void parseOne() {
    read();
    switch (Tok.K) {
    case Eof:
      return;
    case KwExports:
      for (;;) {
        read();
        if (Tok.K != Identifier) {
          unget();
          return;
        }
        parseExport();
      }
    case KwHeapsize:
      parseNumbers(&Config->HeapReserve, &Config->HeapCommit);
      return;
    case KwLibrary:
      parseName(&Config->OutputFile, &Config->ImageBase);
      if (!StringRef(Config->OutputFile).endswith_lower(".dll"))
        Config->OutputFile += ".dll";
      return;
    case KwStacksize:
      parseNumbers(&Config->StackReserve, &Config->StackCommit);
      return;
    case KwName:
      parseName(&Config->OutputFile, &Config->ImageBase);
      return;
    case KwVersion:
      parseVersion(&Config->MajorImageVersion, &Config->MinorImageVersion);
      return;
    default:
      fatal("unknown directive: " + Tok.Value);
    }
  }

  void parseExport() {
    Export E;
    E.Name = Tok.Value;
    read();
    if (Tok.K == Equal) {
      read();
      if (Tok.K != Identifier)
        fatal("identifier expected, but got " + Tok.Value);
      E.ExtName = E.Name;
      E.Name = Tok.Value;
    } else {
      unget();
    }

    if (Config->Machine == I386) {
      if (!isDecorated(E.Name))
        E.Name = Alloc->save("_" + E.Name);
      if (!E.ExtName.empty() && !isDecorated(E.ExtName))
        E.ExtName = Alloc->save("_" + E.ExtName);
    }

    for (;;) {
      read();
      if (Tok.K == Identifier && Tok.Value[0] == '@') {
        Tok.Value.drop_front().getAsInteger(10, E.Ordinal);
        read();
        if (Tok.K == KwNoname) {
          E.Noname = true;
        } else {
          unget();
        }
        continue;
      }
      if (Tok.K == KwData) {
        E.Data = true;
        continue;
      }
      if (Tok.K == KwPrivate) {
        E.Private = true;
        continue;
      }
      unget();
      Config->Exports.push_back(E);
      return;
    }
  }

  // HEAPSIZE/STACKSIZE reserve[,commit]
  void parseNumbers(uint64_t *Reserve, uint64_t *Commit) {
    readAsInt(Reserve);
    read();
    if (Tok.K != Comma) {
      unget();
      Commit = nullptr;
      return;
    }
    readAsInt(Commit);
  }

  // NAME outputPath [BASE=address]
  void parseName(std::string *Out, uint64_t *Baseaddr) {
    read();
    if (Tok.K == Identifier) {
      *Out = Tok.Value;
    } else {
      *Out = "";
      unget();
      return;
    }
    read();
    if (Tok.K == KwBase) {
      expect(Equal, "'=' expected");
      readAsInt(Baseaddr);
    } else {
      unget();
      *Baseaddr = 0;
    }
  }

  // VERSION major[.minor]
  void parseVersion(uint32_t *Major, uint32_t *Minor) {
    read();
    if (Tok.K != Identifier)
      fatal("identifier expected, but got " + Tok.Value);
    StringRef V1, V2;
    std::tie(V1, V2) = Tok.Value.split('.');
    if (V1.getAsInteger(10, *Major))
      fatal("integer expected, but got " + Tok.Value);
    if (V2.empty())
      *Minor = 0;
    else if (V2.getAsInteger(10, *Minor))
      fatal("integer expected, but got " + Tok.Value);
  }

  Lexer Lex;
  Token Tok;
  std::vector<Token> Stack;
  StringSaver *Alloc;
};

} // anonymous namespace

void parseModuleDefs(MemoryBufferRef MB, StringSaver *Alloc) {
  Parser(MB.getBuffer(), Alloc).parse();
}

} // namespace coff
} // namespace lld