Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/ScriptParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ScriptParser.cpp ---------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file contains a recursive-descendent parser for linker scripts.
11
// Parsed results are stored to Config and Script global objects.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "ScriptParser.h"
16
#include "Config.h"
17
#include "Driver.h"
18
#include "InputSection.h"
19
#include "LinkerScript.h"
20
#include "OutputSections.h"
21
#include "ScriptLexer.h"
22
#include "Symbols.h"
23
#include "Target.h"
24
#include "lld/Common/Memory.h"
25
#include "llvm/ADT/SmallString.h"
26
#include "llvm/ADT/StringRef.h"
27
#include "llvm/ADT/StringSet.h"
28
#include "llvm/ADT/StringSwitch.h"
29
#include "llvm/BinaryFormat/ELF.h"
30
#include "llvm/Support/Casting.h"
31
#include "llvm/Support/ErrorHandling.h"
32
#include "llvm/Support/FileSystem.h"
33
#include "llvm/Support/Path.h"
34
#include <cassert>
35
#include <limits>
36
#include <vector>
37
38
using namespace llvm;
39
using namespace llvm::ELF;
40
using namespace llvm::support::endian;
41
using namespace lld;
42
using namespace lld::elf;
43
44
static bool isUnderSysroot(StringRef Path);
45
46
namespace {
47
class ScriptParser final : ScriptLexer {
48
public:
49
  ScriptParser(MemoryBufferRef MB)
50
      : ScriptLexer(MB),
51
655
        IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
52
53
  void readLinkerScript();
54
  void readVersionScript();
55
  void readDynamicList();
56
  void readDefsym(StringRef Name);
57
58
private:
59
  void addFile(StringRef Path);
60
61
  void readAsNeeded();
62
  void readEntry();
63
  void readExtern();
64
  void readGroup();
65
  void readInclude();
66
  void readInput();
67
  void readMemory();
68
  void readOutput();
69
  void readOutputArch();
70
  void readOutputFormat();
71
  void readPhdrs();
72
  void readRegionAlias();
73
  void readSearchDir();
74
  void readSections();
75
  void readVersion();
76
  void readVersionScriptCommand();
77
78
  SymbolAssignment *readSymbolAssignment(StringRef Name);
79
  ByteCommand *readByteCommand(StringRef Tok);
80
  uint32_t readFill();
81
  uint32_t parseFill(StringRef Tok);
82
  void readSectionAddressType(OutputSection *Cmd);
83
  OutputSection *readOutputSectionDescription(StringRef OutSec);
84
  std::vector<StringRef> readOutputSectionPhdrs();
85
  InputSectionDescription *readInputSectionDescription(StringRef Tok);
86
  StringMatcher readFilePatterns();
87
  std::vector<SectionPattern> readInputSectionsList();
88
  InputSectionDescription *readInputSectionRules(StringRef FilePattern);
89
  unsigned readPhdrType();
90
  SortSectionPolicy readSortKind();
91
  SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
92
  SymbolAssignment *readAssignment(StringRef Tok);
93
  void readSort();
94
  Expr readAssert();
95
  Expr readConstant();
96
  Expr getPageSize();
97
98
  uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
99
  std::pair<uint32_t, uint32_t> readMemoryAttributes();
100
101
  Expr combine(StringRef Op, Expr L, Expr R);
102
  Expr readExpr();
103
  Expr readExpr1(Expr Lhs, int MinPrec);
104
  StringRef readParenLiteral();
105
  Expr readPrimary();
106
  Expr readTernary(Expr Cond);
107
  Expr readParenExpr();
108
109
  // For parsing version script.
110
  std::vector<SymbolVersion> readVersionExtern();
111
  void readAnonymousDeclaration();
112
  void readVersionDeclaration(StringRef VerStr);
113
114
  std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
115
  readSymbols();
116
117
  // True if a script being read is in a subdirectory specified by -sysroot.
118
  bool IsUnderSysroot;
119
120
  // A set to detect an INCLUDE() cycle.
121
  StringSet<> Seen;
122
};
123
} // namespace
124
125
261
static StringRef unquote(StringRef S) {
126
261
  if (S.startswith("\""))
127
73
    return S.substr(1, S.size() - 2);
128
188
  return S;
129
188
}
130
131
655
static bool isUnderSysroot(StringRef Path) {
132
655
  if (Config->Sysroot == "")
133
648
    return false;
134
40
  
for (; 7
!Path.empty();
Path = sys::path::parent_path(Path)33
)
135
34
    if (sys::fs::equivalent(Config->Sysroot, Path))
136
1
      return true;
137
7
  
return false6
;
138
7
}
139
140
// Some operations only support one non absolute value. Move the
141
// absolute one to the right hand side for convenience.
142
170
static void moveAbsRight(ExprValue &A, ExprValue &B) {
143
170
  if (A.Sec == nullptr || 
(80
A.ForceAbsolute80
&&
!B.isAbsolute()9
))
144
92
    std::swap(A, B);
145
170
  if (!B.isAbsolute())
146
1
    error(A.Loc + ": at least one side of the expression must be absolute");
147
170
}
148
149
142
static ExprValue add(ExprValue A, ExprValue B) {
150
142
  moveAbsRight(A, B);
151
142
  return {A.Sec, A.ForceAbsolute, A.getSectionOffset() + B.getValue(), A.Loc};
152
142
}
153
154
34
static ExprValue sub(ExprValue A, ExprValue B) {
155
34
  // The distance between two symbols in sections is absolute.
156
34
  if (!A.isAbsolute() && 
!B.isAbsolute()19
)
157
2
    return A.getValue() - B.getValue();
158
32
  return {A.Sec, false, A.getSectionOffset() - B.getValue(), A.Loc};
159
32
}
160
161
13
static ExprValue bitAnd(ExprValue A, ExprValue B) {
162
13
  moveAbsRight(A, B);
163
13
  return {A.Sec, A.ForceAbsolute,
164
13
          (A.getValue() & B.getValue()) - A.getSecAddr(), A.Loc};
165
13
}
166
167
15
static ExprValue bitOr(ExprValue A, ExprValue B) {
168
15
  moveAbsRight(A, B);
169
15
  return {A.Sec, A.ForceAbsolute,
170
15
          (A.getValue() | B.getValue()) - A.getSecAddr(), A.Loc};
171
15
}
172
173
22
void ScriptParser::readDynamicList() {
174
22
  Config->HasDynamicList = true;
175
22
  expect("{");
176
22
  std::vector<SymbolVersion> Locals;
177
22
  std::vector<SymbolVersion> Globals;
178
22
  std::tie(Locals, Globals) = readSymbols();
179
22
  expect(";");
180
22
181
22
  if (!atEOF()) {
182
0
    setError("EOF expected, but got " + next());
183
0
    return;
184
0
  }
185
22
  if (!Locals.empty()) {
186
0
    setError("\"local:\" scope not supported in --dynamic-list");
187
0
    return;
188
0
  }
189
22
190
22
  for (SymbolVersion V : Globals)
191
26
    Config->DynamicList.push_back(V);
192
22
}
193
194
77
void ScriptParser::readVersionScript() {
195
77
  readVersionScriptCommand();
196
77
  if (!atEOF())
197
1
    setError("EOF expected, but got " + next());
198
77
}
199
200
82
void ScriptParser::readVersionScriptCommand() {
201
82
  if (consume("{")) {
202
22
    readAnonymousDeclaration();
203
22
    return;
204
22
  }
205
60
206
145
  
while (60
!atEOF() &&
!errorCount()91
&&
peek() != "}"91
) {
207
86
    StringRef VerStr = next();
208
86
    if (VerStr == "{") {
209
1
      setError("anonymous version definition is used in "
210
1
               "combination with other version definitions");
211
1
      return;
212
1
    }
213
85
    expect("{");
214
85
    readVersionDeclaration(VerStr);
215
85
  }
216
60
}
217
218
5
void ScriptParser::readVersion() {
219
5
  expect("{");
220
5
  readVersionScriptCommand();
221
5
  expect("}");
222
5
}
223
224
542
void ScriptParser::readLinkerScript() {
225
1.20k
  while (!atEOF()) {
226
659
    StringRef Tok = next();
227
659
    if (Tok == ";")
228
39
      continue;
229
620
230
620
    if (Tok == "ENTRY") {
231
6
      readEntry();
232
614
    } else if (Tok == "EXTERN") {
233
5
      readExtern();
234
609
    } else if (Tok == "GROUP") {
235
22
      readGroup();
236
587
    } else if (Tok == "INCLUDE") {
237
17
      readInclude();
238
570
    } else if (Tok == "INPUT") {
239
4
      readInput();
240
566
    } else if (Tok == "MEMORY") {
241
33
      readMemory();
242
533
    } else if (Tok == "OUTPUT") {
243
7
      readOutput();
244
526
    } else if (Tok == "OUTPUT_ARCH") {
245
1
      readOutputArch();
246
525
    } else if (Tok == "OUTPUT_FORMAT") {
247
4
      readOutputFormat();
248
521
    } else if (Tok == "PHDRS") {
249
27
      readPhdrs();
250
494
    } else if (Tok == "REGION_ALIAS") {
251
8
      readRegionAlias();
252
486
    } else if (Tok == "SEARCH_DIR") {
253
6
      readSearchDir();
254
480
    } else if (Tok == "SECTIONS") {
255
438
      readSections();
256
438
    } else 
if (42
Tok == "VERSION"42
) {
257
5
      readVersion();
258
37
    } else if (SymbolAssignment *Cmd = readAssignment(Tok)) {
259
34
      Script->SectionCommands.push_back(Cmd);
260
34
    } else {
261
3
      setError("unknown directive: " + Tok);
262
3
    }
263
620
  }
264
542
}
265
266
14
void ScriptParser::readDefsym(StringRef Name) {
267
14
  Expr E = readExpr();
268
14
  if (!atEOF())
269
0
    setError("EOF expected, but got " + next());
270
14
  SymbolAssignment *Cmd = make<SymbolAssignment>(Name, E, getCurrentLocation());
271
14
  Script->SectionCommands.push_back(Cmd);
272
14
}
273
274
43
void ScriptParser::addFile(StringRef S) {
275
43
  if (IsUnderSysroot && 
S.startswith("/")2
) {
276
2
    SmallString<128> PathData;
277
2
    StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
278
2
    if (sys::fs::exists(Path)) {
279
1
      Driver->addFile(Saver.save(Path), /*WithLOption=*/false);
280
1
      return;
281
1
    }
282
42
  }
283
42
284
42
  if (S.startswith("/")) {
285
34
    Driver->addFile(S, /*WithLOption=*/false);
286
34
  } else 
if (8
S.startswith("=")8
) {
287
2
    if (Config->Sysroot.empty())
288
1
      Driver->addFile(S.substr(1), /*WithLOption=*/false);
289
1
    else
290
1
      Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)),
291
1
                      /*WithLOption=*/false);
292
6
  } else if (S.startswith("-l")) {
293
2
    Driver->addLibrary(S.substr(2));
294
4
  } else if (sys::fs::exists(S)) {
295
0
    Driver->addFile(S, /*WithLOption=*/false);
296
4
  } else {
297
4
    if (Optional<std::string> Path = findFromSearchPaths(S))
298
2
      Driver->addFile(Saver.save(*Path), /*WithLOption=*/true);
299
2
    else
300
2
      setError("unable to find " + S);
301
4
  }
302
42
}
303
304
2
void ScriptParser::readAsNeeded() {
305
2
  expect("(");
306
2
  bool Orig = Config->AsNeeded;
307
2
  Config->AsNeeded = true;
308
6
  while (!errorCount() && !consume(")"))
309
4
    addFile(unquote(next()));
310
2
  Config->AsNeeded = Orig;
311
2
}
312
313
6
void ScriptParser::readEntry() {
314
6
  // -e <symbol> takes predecence over ENTRY(<symbol>).
315
6
  expect("(");
316
6
  StringRef Tok = next();
317
6
  if (Config->Entry.empty())
318
5
    Config->Entry = Tok;
319
6
  expect(")");
320
6
}
321
322
5
void ScriptParser::readExtern() {
323
5
  expect("(");
324
11
  while (!errorCount() && 
!consume(")")8
)
325
6
    Config->Undefined.push_back(next());
326
5
}
327
328
22
void ScriptParser::readGroup() {
329
22
  bool Orig = InputFile::IsInGroup;
330
22
  InputFile::IsInGroup = true;
331
22
  readInput();
332
22
  InputFile::IsInGroup = Orig;
333
22
  if (!Orig)
334
20
    ++InputFile::NextGroupId;
335
22
}
336
337
17
void ScriptParser::readInclude() {
338
17
  StringRef Tok = unquote(next());
339
17
340
17
  if (!Seen.insert(Tok).second) {
341
2
    setError("there is a cycle in linker script INCLUDEs");
342
2
    return;
343
2
  }
344
15
345
15
  if (Optional<std::string> Path = searchLinkerScript(Tok)) {
346
13
    if (Optional<MemoryBufferRef> MB = readFile(*Path))
347
13
      tokenize(*MB);
348
13
    return;
349
13
  }
350
2
  setError("cannot find linker script " + Tok);
351
2
}
352
353
26
void ScriptParser::readInput() {
354
26
  expect("(");
355
67
  while (!errorCount() && 
!consume(")")61
) {
356
41
    if (consume("AS_NEEDED"))
357
2
      readAsNeeded();
358
39
    else
359
39
      addFile(unquote(next()));
360
41
  }
361
26
}
362
363
7
void ScriptParser::readOutput() {
364
7
  // -o <file> takes predecence over OUTPUT(<file>).
365
7
  expect("(");
366
7
  StringRef Tok = next();
367
7
  if (Config->OutputFile.empty())
368
6
    Config->OutputFile = unquote(Tok);
369
7
  expect(")");
370
7
}
371
372
1
void ScriptParser::readOutputArch() {
373
1
  // OUTPUT_ARCH is ignored for now.
374
1
  expect("(");
375
7
  while (!errorCount() && !consume(")"))
376
6
    skip();
377
1
}
378
379
4
void ScriptParser::readOutputFormat() {
380
4
  // Error checking only for now.
381
4
  expect("(");
382
4
  skip();
383
4
  if (consume(")"))
384
1
    return;
385
3
  expect(",");
386
3
  skip();
387
3
  expect(",");
388
3
  skip();
389
3
  expect(")");
390
3
}
391
392
27
void ScriptParser::readPhdrs() {
393
27
  expect("{");
394
27
395
64
  while (!errorCount() && !consume("}")) {
396
37
    PhdrsCommand Cmd;
397
37
    Cmd.Name = next();
398
37
    Cmd.Type = readPhdrType();
399
37
400
74
    while (!errorCount() && !consume(";")) {
401
37
      if (consume("FILEHDR"))
402
12
        Cmd.HasFilehdr = true;
403
25
      else if (consume("PHDRS"))
404
15
        Cmd.HasPhdrs = true;
405
10
      else if (consume("AT"))
406
1
        Cmd.LMAExpr = readParenExpr();
407
9
      else if (consume("FLAGS"))
408
9
        Cmd.Flags = readParenExpr()().getValue();
409
0
      else
410
0
        setError("unexpected header attribute: " + next());
411
37
    }
412
37
413
37
    Script->PhdrsCommands.push_back(Cmd);
414
37
  }
415
27
}
416
417
8
void ScriptParser::readRegionAlias() {
418
8
  expect("(");
419
8
  StringRef Alias = unquote(next());
420
8
  expect(",");
421
8
  StringRef Name = next();
422
8
  expect(")");
423
8
424
8
  if (Script->MemoryRegions.count(Alias))
425
2
    setError("redefinition of memory region '" + Alias + "'");
426
8
  if (!Script->MemoryRegions.count(Name))
427
1
    setError("memory region '" + Name + "' is not defined");
428
8
  Script->MemoryRegions.insert({Alias, Script->MemoryRegions[Name]});
429
8
}
430
431
6
void ScriptParser::readSearchDir() {
432
6
  expect("(");
433
6
  StringRef Tok = next();
434
6
  if (!Config->Nostdlib)
435
5
    Config->SearchPaths.push_back(unquote(Tok));
436
6
  expect(")");
437
6
}
438
439
438
void ScriptParser::readSections() {
440
438
  Script->HasSectionsCommand = true;
441
438
442
438
  // -no-rosegment is used to avoid placing read only non-executable sections in
443
438
  // their own segment. We do the same if SECTIONS command is present in linker
444
438
  // script. See comment for computeFlags().
445
438
  Config->SingleRoRx = true;
446
438
447
438
  expect("{");
448
438
  std::vector<BaseCommand *> V;
449
1.41k
  while (!errorCount() && 
!consume("}")1.39k
) {
450
976
    StringRef Tok = next();
451
976
    if (BaseCommand *Cmd = readAssignment(Tok))
452
300
      V.push_back(Cmd);
453
676
    else
454
676
      V.push_back(readOutputSectionDescription(Tok));
455
976
  }
456
438
457
438
  if (!atEOF() && 
consume("INSERT")31
) {
458
9
    std::vector<BaseCommand *> *Dest = nullptr;
459
9
    if (consume("AFTER"))
460
4
      Dest = &Script->InsertAfterCommands[next()];
461
5
    else if (consume("BEFORE"))
462
4
      Dest = &Script->InsertBeforeCommands[next()];
463
1
    else
464
1
      setError("expected AFTER/BEFORE, but got '" + next() + "'");
465
9
    if (Dest)
466
8
      Dest->insert(Dest->end(), V.begin(), V.end());
467
9
    return;
468
9
  }
469
429
470
429
  Script->SectionCommands.insert(Script->SectionCommands.end(), V.begin(),
471
429
                                 V.end());
472
429
}
473
474
1.28k
static int precedence(StringRef Op) {
475
1.28k
  return StringSwitch<int>(Op)
476
1.28k
      .Cases("*", "/", "%", 6)
477
1.28k
      .Cases("+", "-", 5)
478
1.28k
      .Cases("<<", ">>", 4)
479
1.28k
      .Cases("<", "<=", ">", ">=", "==", "!=", 3)
480
1.28k
      .Case("&", 2)
481
1.28k
      .Case("|", 1)
482
1.28k
      .Default(-1);
483
1.28k
}
484
485
7
StringMatcher ScriptParser::readFilePatterns() {
486
7
  std::vector<StringRef> V;
487
14
  while (!errorCount() && !consume(")"))
488
7
    V.push_back(next());
489
7
  return StringMatcher(V);
490
7
}
491
492
726
SortSectionPolicy ScriptParser::readSortKind() {
493
726
  if (consume("SORT") || 
consume("SORT_BY_NAME")721
)
494
11
    return SortSectionPolicy::Name;
495
715
  if (consume("SORT_BY_ALIGNMENT"))
496
4
    return SortSectionPolicy::Alignment;
497
711
  if (consume("SORT_BY_INIT_PRIORITY"))
498
1
    return SortSectionPolicy::Priority;
499
710
  if (consume("SORT_NONE"))
500
3
    return SortSectionPolicy::None;
501
707
  return SortSectionPolicy::Default;
502
707
}
503
504
// Reads SECTIONS command contents in the following form:
505
//
506
// <contents> ::= <elem>*
507
// <elem>     ::= <exclude>? <glob-pattern>
508
// <exclude>  ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
509
//
510
// For example,
511
//
512
// *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
513
//
514
// is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
515
// The semantics of that is section .foo in any file, section .bar in
516
// any file but a.o, and section .baz in any file but b.o.
517
710
std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
518
710
  std::vector<SectionPattern> Ret;
519
1.42k
  while (!errorCount() && 
peek() != ")"1.42k
) {
520
713
    StringMatcher ExcludeFilePat;
521
713
    if (consume("EXCLUDE_FILE")) {
522
7
      expect("(");
523
7
      ExcludeFilePat = readFilePatterns();
524
7
    }
525
713
526
713
    std::vector<StringRef> V;
527
1.43k
    while (!errorCount() && peek() != ")" && 
peek() != "EXCLUDE_FILE"725
)
528
721
      V.push_back(next());
529
713
530
713
    if (!V.empty())
531
711
      Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
532
2
    else
533
2
      setError("section pattern is expected");
534
713
  }
535
710
  return Ret;
536
710
}
537
538
// Reads contents of "SECTIONS" directive. That directive contains a
539
// list of glob patterns for input sections. The grammar is as follows.
540
//
541
// <patterns> ::= <section-list>
542
//              | <sort> "(" <section-list> ")"
543
//              | <sort> "(" <sort> "(" <section-list> ")" ")"
544
//
545
// <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
546
//              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
547
//
548
// <section-list> is parsed by readInputSectionsList().
549
InputSectionDescription *
550
708
ScriptParser::readInputSectionRules(StringRef FilePattern) {
551
708
  auto *Cmd = make<InputSectionDescription>(FilePattern);
552
708
  expect("(");
553
708
554
1.41k
  while (!errorCount() && 
!consume(")")1.41k
) {
555
710
    SortSectionPolicy Outer = readSortKind();
556
710
    SortSectionPolicy Inner = SortSectionPolicy::Default;
557
710
    std::vector<SectionPattern> V;
558
710
    if (Outer != SortSectionPolicy::Default) {
559
16
      expect("(");
560
16
      Inner = readSortKind();
561
16
      if (Inner != SortSectionPolicy::Default) {
562
3
        expect("(");
563
3
        V = readInputSectionsList();
564
3
        expect(")");
565
13
      } else {
566
13
        V = readInputSectionsList();
567
13
      }
568
16
      expect(")");
569
694
    } else {
570
694
      V = readInputSectionsList();
571
694
    }
572
710
573
711
    for (SectionPattern &Pat : V) {
574
711
      Pat.SortInner = Inner;
575
711
      Pat.SortOuter = Outer;
576
711
    }
577
710
578
710
    std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
579
710
  }
580
708
  return Cmd;
581
708
}
582
583
InputSectionDescription *
584
708
ScriptParser::readInputSectionDescription(StringRef Tok) {
585
708
  // Input section wildcard can be surrounded by KEEP.
586
708
  // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
587
708
  if (Tok == "KEEP") {
588
14
    expect("(");
589
14
    StringRef FilePattern = next();
590
14
    InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
591
14
    expect(")");
592
14
    Script->KeptSections.push_back(Cmd);
593
14
    return Cmd;
594
14
  }
595
694
  return readInputSectionRules(Tok);
596
694
}
597
598
1
void ScriptParser::readSort() {
599
1
  expect("(");
600
1
  expect("CONSTRUCTORS");
601
1
  expect(")");
602
1
}
603
604
8
Expr ScriptParser::readAssert() {
605
8
  expect("(");
606
8
  Expr E = readExpr();
607
8
  expect(",");
608
8
  StringRef Msg = unquote(next());
609
8
  expect(")");
610
8
611
8
  return [=] {
612
8
    if (!E().getValue())
613
1
      error(Msg);
614
8
    return Script->getDot();
615
8
  };
616
8
}
617
618
// Reads a FILL(expr) command. We handle the FILL command as an
619
// alias for =fillexp section attribute, which is different from
620
// what GNU linkers do.
621
// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
622
4
uint32_t ScriptParser::readFill() {
623
4
  expect("(");
624
4
  uint32_t V = parseFill(next());
625
4
  expect(")");
626
4
  return V;
627
4
}
628
629
// Reads an expression and/or the special directive for an output
630
// section definition. Directive is one of following: "(NOLOAD)",
631
// "(COPY)", "(INFO)" or "(OVERLAY)".
632
//
633
// An output section name can be followed by an address expression
634
// and/or directive. This grammar is not LL(1) because "(" can be
635
// interpreted as either the beginning of some expression or beginning
636
// of directive.
637
//
638
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
639
// https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
640
84
void ScriptParser::readSectionAddressType(OutputSection *Cmd) {
641
84
  if (consume("(")) {
642
8
    if (consume("NOLOAD")) {
643
2
      expect(")");
644
2
      Cmd->Noload = true;
645
2
      return;
646
2
    }
647
6
    if (consume("COPY") || 
consume("INFO")5
||
consume("OVERLAY")4
) {
648
3
      expect(")");
649
3
      Cmd->NonAlloc = true;
650
3
      return;
651
3
    }
652
3
    Cmd->AddrExpr = readExpr();
653
3
    expect(")");
654
76
  } else {
655
76
    Cmd->AddrExpr = readExpr();
656
76
  }
657
84
658
84
  
if (79
consume("(")79
) {
659
1
    expect("NOLOAD");
660
1
    expect(")");
661
1
    Cmd->Noload = true;
662
1
  }
663
79
}
664
665
53
static Expr checkAlignment(Expr E, std::string &Loc) {
666
65
  return [=] {
667
65
    uint64_t Alignment = std::max((uint64_t)1, E().getValue());
668
65
    if (!isPowerOf2_64(Alignment)) {
669
3
      error(Loc + ": alignment must be power of 2");
670
3
      return (uint64_t)1; // Return a dummy value.
671
3
    }
672
62
    return Alignment;
673
62
  };
674
53
}
675
676
676
OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) {
677
676
  OutputSection *Cmd =
678
676
      Script->createOutputSection(OutSec, getCurrentLocation());
679
676
680
676
  size_t SymbolsReferenced = Script->ReferencedSymbols.size();
681
676
682
676
  if (peek() != ":")
683
84
    readSectionAddressType(Cmd);
684
676
  expect(":");
685
676
686
676
  std::string Location = getCurrentLocation();
687
676
  if (consume("AT"))
688
35
    Cmd->LMAExpr = readParenExpr();
689
676
  if (consume("ALIGN"))
690
15
    Cmd->AlignExpr = checkAlignment(readParenExpr(), Location);
691
676
  if (consume("SUBALIGN"))
692
5
    Cmd->SubalignExpr = checkAlignment(readParenExpr(), Location);
693
676
694
676
  // Parse constraints.
695
676
  if (consume("ONLY_IF_RO"))
696
9
    Cmd->Constraint = ConstraintKind::ReadOnly;
697
676
  if (consume("ONLY_IF_RW"))
698
5
    Cmd->Constraint = ConstraintKind::ReadWrite;
699
676
  expect("{");
700
676
701
1.57k
  while (!errorCount() && 
!consume("}")1.56k
) {
702
895
    StringRef Tok = next();
703
895
    if (Tok == ";") {
704
20
      // Empty commands are allowed. Do nothing here.
705
875
    } else if (SymbolAssignment *Assign = readAssignment(Tok)) {
706
120
      Cmd->SectionCommands.push_back(Assign);
707
755
    } else if (ByteCommand *Data = readByteCommand(Tok)) {
708
41
      Cmd->SectionCommands.push_back(Data);
709
714
    } else if (Tok == "CONSTRUCTORS") {
710
1
      // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
711
1
      // by name. This is for very old file formats such as ECOFF/XCOFF.
712
1
      // For ELF, we should ignore.
713
713
    } else if (Tok == "FILL") {
714
4
      Cmd->Filler = readFill();
715
709
    } else if (Tok == "SORT") {
716
1
      readSort();
717
708
    } else if (peek() == "(") {
718
708
      Cmd->SectionCommands.push_back(readInputSectionDescription(Tok));
719
708
    } else {
720
0
      setError("unknown command " + Tok);
721
0
    }
722
895
  }
723
676
724
676
  if (consume(">"))
725
34
    Cmd->MemoryRegionName = next();
726
676
727
676
  if (consume("AT")) {
728
11
    expect(">");
729
11
    Cmd->LMARegionName = next();
730
11
  }
731
676
732
676
  if (Cmd->LMAExpr && 
!Cmd->LMARegionName.empty()35
)
733
1
    error("section can't have both LMA and a load region");
734
676
735
676
  Cmd->Phdrs = readOutputSectionPhdrs();
736
676
737
676
  if (consume("="))
738
1
    Cmd->Filler = parseFill(next());
739
675
  else if (peek().startswith("="))
740
8
    Cmd->Filler = parseFill(next().drop_front());
741
676
742
676
  // Consume optional comma following output section command.
743
676
  consume(",");
744
676
745
676
  if (Script->ReferencedSymbols.size() > SymbolsReferenced)
746
17
    Cmd->ExpressionsUseSymbols = true;
747
676
  return Cmd;
748
676
}
749
750
// Parses a given string as a octal/decimal/hexadecimal number and
751
// returns it as a big-endian number. Used for `=<fillexp>`.
752
// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
753
//
754
// When reading a hexstring, ld.bfd handles it as a blob of arbitrary
755
// size, while ld.gold always handles it as a 32-bit big-endian number.
756
// We are compatible with ld.gold because it's easier to implement.
757
13
uint32_t ScriptParser::parseFill(StringRef Tok) {
758
13
  uint32_t V = 0;
759
13
  if (!to_integer(Tok, V))
760
1
    setError("invalid filler expression: " + Tok);
761
13
762
13
  uint32_t Buf;
763
13
  write32be(&Buf, V);
764
13
  return Buf;
765
13
}
766
767
45
SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
768
45
  expect("(");
769
45
  SymbolAssignment *Cmd = readSymbolAssignment(next());
770
45
  Cmd->Provide = Provide;
771
45
  Cmd->Hidden = Hidden;
772
45
  expect(")");
773
45
  return Cmd;
774
45
}
775
776
1.88k
SymbolAssignment *ScriptParser::readAssignment(StringRef Tok) {
777
1.88k
  // Assert expression returns Dot, so this is equal to ".=."
778
1.88k
  if (Tok == "ASSERT")
779
6
    return make<SymbolAssignment>(".", readAssert(), getCurrentLocation());
780
1.88k
781
1.88k
  size_t OldPos = Pos;
782
1.88k
  SymbolAssignment *Cmd = nullptr;
783
1.88k
  if (peek() == "=" || 
peek() == "+="1.49k
)
784
403
    Cmd = readSymbolAssignment(Tok);
785
1.47k
  else if (Tok == "PROVIDE")
786
17
    Cmd = readProvideHidden(true, false);
787
1.46k
  else if (Tok == "HIDDEN")
788
3
    Cmd = readProvideHidden(false, true);
789
1.45k
  else if (Tok == "PROVIDE_HIDDEN")
790
25
    Cmd = readProvideHidden(true, true);
791
1.88k
792
1.88k
  if (Cmd) {
793
448
    Cmd->CommandString =
794
448
        Tok.str() + " " +
795
448
        llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " ");
796
448
    expect(";");
797
448
  }
798
1.88k
  return Cmd;
799
1.88k
}
800
801
448
SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef Name) {
802
448
  StringRef Op = next();
803
448
  assert(Op == "=" || Op == "+=");
804
448
  Expr E = readExpr();
805
448
  if (Op == "+=") {
806
20
    std::string Loc = getCurrentLocation();
807
22
    E = [=] { return add(Script->getSymbolValue(Name, Loc), E()); };
808
20
  }
809
448
  return make<SymbolAssignment>(Name, E, getCurrentLocation());
810
448
}
811
812
// This is an operator-precedence parser to parse a linker
813
// script expression.
814
884
Expr ScriptParser::readExpr() {
815
884
  // Our lexer is context-aware. Set the in-expression bit so that
816
884
  // they apply different tokenization rules.
817
884
  bool Orig = InExpr;
818
884
  InExpr = true;
819
884
  Expr E = readExpr1(readPrimary(), 0);
820
884
  InExpr = Orig;
821
884
  return E;
822
884
}
823
824
142
Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
825
142
  if (Op == "+")
826
120
    
return [=] 69
{ return add(L(), R()); };
827
73
  if (Op == "-")
828
34
    
return [=] 19
{ return sub(L(), R()); };
829
54
  if (Op == "*")
830
20
    
return [=] 15
{ return L().getValue() * R().getValue(); };
831
39
  if (Op == "/") {
832
3
    std::string Loc = getCurrentLocation();
833
5
    return [=]() -> uint64_t {
834
5
      if (uint64_t RV = R().getValue())
835
4
        return L().getValue() / RV;
836
1
      error(Loc + ": division by zero");
837
1
      return 0;
838
1
    };
839
3
  }
840
36
  if (Op == "%") {
841
2
    std::string Loc = getCurrentLocation();
842
3
    return [=]() -> uint64_t {
843
3
      if (uint64_t RV = R().getValue())
844
2
        return L().getValue() % RV;
845
1
      error(Loc + ": modulo by zero");
846
1
      return 0;
847
1
    };
848
2
  }
849
34
  if (Op == "<<")
850
6
    
return [=] 3
{ return L().getValue() << R().getValue(); };
851
31
  if (Op == ">>")
852
2
    
return [=] 1
{ return L().getValue() >> R().getValue(); };
853
30
  if (Op == "<")
854
4
    
return [=] 3
{ return L().getValue() < R().getValue(); };
855
27
  if (Op == ">")
856
2
    
return [=] 1
{ return L().getValue() > R().getValue(); };
857
26
  if (Op == ">=")
858
2
    
return [=] 1
{ return L().getValue() >= R().getValue(); };
859
25
  if (Op == "<=")
860
2
    
return [=] 1
{ return L().getValue() <= R().getValue(); };
861
24
  if (Op == "==")
862
5
    
return [=] 4
{ return L().getValue() == R().getValue(); };
863
20
  if (Op == "!=")
864
4
    
return [=] 2
{ return L().getValue() != R().getValue(); };
865
18
  if (Op == "&")
866
13
    
return [=] 7
{ return bitAnd(L(), R()); };
867
11
  if (Op == "|")
868
15
    
return [=] 11
{ return bitOr(L(), R()); };
869
0
  llvm_unreachable("invalid operator");
870
0
}
871
872
// This is a part of the operator-precedence parser. This function
873
// assumes that the remaining token stream starts with an operator.
874
892
Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
875
1.03k
  while (!atEOF() && 
!errorCount()1.00k
) {
876
1.00k
    // Read an operator and an expression.
877
1.00k
    if (consume("?"))
878
14
      return readTernary(Lhs);
879
988
    StringRef Op1 = peek();
880
988
    if (precedence(Op1) < MinPrec)
881
846
      break;
882
142
    skip();
883
142
    Expr Rhs = readPrimary();
884
142
885
142
    // Evaluate the remaining part of the expression first if the
886
142
    // next operator has greater precedence than the previous one.
887
142
    // For example, if we have read "+" and "3", and if the next
888
142
    // operator is "*", then we'll evaluate 3 * ... part first.
889
150
    while (!atEOF()) {
890
146
      StringRef Op2 = peek();
891
146
      if (precedence(Op2) <= precedence(Op1))
892
138
        break;
893
8
      Rhs = readExpr1(Rhs, precedence(Op2));
894
8
    }
895
142
896
142
    Lhs = combine(Op1, Lhs, Rhs);
897
142
  }
898
892
  
return Lhs878
;
899
892
}
900
901
7
Expr ScriptParser::getPageSize() {
902
7
  std::string Location = getCurrentLocation();
903
7
  return [=]() -> uint64_t {
904
6
    if (Target)
905
4
      return Target->PageSize;
906
2
    error(Location + ": unable to calculate page size");
907
2
    return 4096; // Return a dummy value.
908
2
  };
909
7
}
910
911
20
Expr ScriptParser::readConstant() {
912
20
  StringRef S = readParenLiteral();
913
20
  if (S == "COMMONPAGESIZE")
914
4
    return getPageSize();
915
16
  if (S == "MAXPAGESIZE")
916
21
    
return [] 15
{ return Config->MaxPageSize; };
917
1
  setError("unknown constant: " + S);
918
1
  return [] { return 0; };
919
1
}
920
921
// Parses Tok as an integer. It recognizes hexadecimal (prefixed with
922
// "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
923
// have "K" (Ki) or "M" (Mi) suffixes.
924
735
static Optional<uint64_t> parseInt(StringRef Tok) {
925
735
  // Hexadecimal
926
735
  uint64_t Val;
927
735
  if (Tok.startswith_lower("0x")) {
928
364
    if (!to_integer(Tok.substr(2), Val, 16))
929
4
      return None;
930
360
    return Val;
931
360
  }
932
371
  if (Tok.endswith_lower("H")) {
933
2
    if (!to_integer(Tok.drop_back(), Val, 16))
934
0
      return None;
935
2
    return Val;
936
2
  }
937
369
938
369
  // Decimal
939
369
  if (Tok.endswith_lower("K")) {
940
15
    if (!to_integer(Tok.drop_back(), Val, 10))
941
0
      return None;
942
15
    return Val * 1024;
943
15
  }
944
354
  if (Tok.endswith_lower("M")) {
945
8
    if (!to_integer(Tok.drop_back(), Val, 10))
946
0
      return None;
947
8
    return Val * 1024 * 1024;
948
8
  }
949
346
  if (!to_integer(Tok, Val, 10))
950
117
    return None;
951
229
  return Val;
952
229
}
953
954
755
ByteCommand *ScriptParser::readByteCommand(StringRef Tok) {
955
755
  int Size = StringSwitch<int>(Tok)
956
755
                 .Case("BYTE", 1)
957
755
                 .Case("SHORT", 2)
958
755
                 .Case("LONG", 4)
959
755
                 .Case("QUAD", 8)
960
755
                 .Default(-1);
961
755
  if (Size == -1)
962
714
    return nullptr;
963
41
964
41
  size_t OldPos = Pos;
965
41
  Expr E = readParenExpr();
966
41
  std::string CommandString =
967
41
      Tok.str() + " " +
968
41
      llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " ");
969
41
  return make<ByteCommand>(E, Size, CommandString);
970
41
}
971
972
78
StringRef ScriptParser::readParenLiteral() {
973
78
  expect("(");
974
78
  bool Orig = InExpr;
975
78
  InExpr = false;
976
78
  StringRef Tok = next();
977
78
  InExpr = Orig;
978
78
  expect(")");
979
78
  return Tok;
980
78
}
981
982
57
static void checkIfExists(OutputSection *Cmd, StringRef Location) {
983
57
  if (Cmd->Location.empty() && 
Script->ErrorOnMissingSection10
)
984
2
    error(Location + ": undefined section " + Cmd->Name);
985
57
}
986
987
1.03k
Expr ScriptParser::readPrimary() {
988
1.03k
  if (peek() == "(")
989
16
    return readParenExpr();
990
1.02k
991
1.02k
  if (consume("~")) {
992
5
    Expr E = readPrimary();
993
10
    return [=] { return ~E().getValue(); };
994
5
  }
995
1.01k
  if (consume("!")) {
996
2
    Expr E = readPrimary();
997
4
    return [=] { return !E().getValue(); };
998
2
  }
999
1.01k
  if (consume("-")) {
1000
4
    Expr E = readPrimary();
1001
8
    return [=] { return -E().getValue(); };
1002
4
  }
1003
1.01k
1004
1.01k
  StringRef Tok = next();
1005
1.01k
  std::string Location = getCurrentLocation();
1006
1.01k
1007
1.01k
  // Built-in functions are parsed here.
1008
1.01k
  // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1009
1.01k
  if (Tok == "ABSOLUTE") {
1010
15
    Expr Inner = readParenExpr();
1011
32
    return [=] {
1012
32
      ExprValue I = Inner();
1013
32
      I.ForceAbsolute = true;
1014
32
      return I;
1015
32
    };
1016
15
  }
1017
995
  if (Tok == "ADDR") {
1018
22
    StringRef Name = readParenLiteral();
1019
22
    OutputSection *Sec = Script->getOrCreateOutputSection(Name);
1020
35
    return [=]() -> ExprValue {
1021
35
      checkIfExists(Sec, Location);
1022
35
      return {Sec, false, 0, Location};
1023
35
    };
1024
22
  }
1025
973
  if (Tok == "ALIGN") {
1026
33
    expect("(");
1027
33
    Expr E = readExpr();
1028
33
    if (consume(")")) {
1029
17
      E = checkAlignment(E, Location);
1030
19
      return [=] { return alignTo(Script->getDot(), E().getValue()); };
1031
17
    }
1032
16
    expect(",");
1033
16
    Expr E2 = checkAlignment(readExpr(), Location);
1034
16
    expect(")");
1035
26
    return [=] {
1036
26
      ExprValue V = E();
1037
26
      V.Alignment = E2().getValue();
1038
26
      return V;
1039
26
    };
1040
16
  }
1041
940
  if (Tok == "ALIGNOF") {
1042
5
    StringRef Name = readParenLiteral();
1043
5
    OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
1044
10
    return [=] {
1045
10
      checkIfExists(Cmd, Location);
1046
10
      return Cmd->Alignment;
1047
10
    };
1048
5
  }
1049
935
  if (Tok == "ASSERT")
1050
2
    return readAssert();
1051
933
  if (Tok == "CONSTANT")
1052
20
    return readConstant();
1053
913
  if (Tok == "DATA_SEGMENT_ALIGN") {
1054
4
    expect("(");
1055
4
    Expr E = readExpr();
1056
4
    expect(",");
1057
4
    readExpr();
1058
4
    expect(")");
1059
6
    return [=] {
1060
6
      return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue()));
1061
6
    };
1062
4
  }
1063
909
  if (Tok == "DATA_SEGMENT_END") {
1064
2
    expect("(");
1065
2
    expect(".");
1066
2
    expect(")");
1067
2
    return [] { return Script->getDot(); };
1068
2
  }
1069
907
  if (Tok == "DATA_SEGMENT_RELRO_END") {
1070
3
    // GNU linkers implements more complicated logic to handle
1071
3
    // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1072
3
    // just align to the next page boundary for simplicity.
1073
3
    expect("(");
1074
3
    readExpr();
1075
3
    expect(",");
1076
3
    readExpr();
1077
3
    expect(")");
1078
3
    Expr E = getPageSize();
1079
3
    return [=] { return alignTo(Script->getDot(), E().getValue()); };
1080
3
  }
1081
904
  if (Tok == "DEFINED") {
1082
2
    StringRef Name = readParenLiteral();
1083
2
    return [=] { return Symtab->find(Name) ? 
11
:
01
; };
1084
2
  }
1085
902
  if (Tok == "LENGTH") {
1086
3
    StringRef Name = readParenLiteral();
1087
3
    if (Script->MemoryRegions.count(Name) == 0) {
1088
1
      setError("memory region not defined: " + Name);
1089
1
      return [] { return 0; };
1090
1
    }
1091
4
    
return [=] 2
{ return Script->MemoryRegions[Name]->Length; };
1092
2
  }
1093
899
  if (Tok == "LOADADDR") {
1094
6
    StringRef Name = readParenLiteral();
1095
6
    OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
1096
12
    return [=] {
1097
12
      checkIfExists(Cmd, Location);
1098
12
      return Cmd->getLMA();
1099
12
    };
1100
6
  }
1101
893
  if (Tok == "MAX" || 
Tok == "MIN"892
) {
1102
2
    expect("(");
1103
2
    Expr A = readExpr();
1104
2
    expect(",");
1105
2
    Expr B = readExpr();
1106
2
    expect(")");
1107
2
    if (Tok == "MIN")
1108
2
      
return [=] 1
{ return std::min(A().getValue(), B().getValue()); };
1109
2
    
return [=] 1
{ return std::max(A().getValue(), B().getValue()); };
1110
1
  }
1111
891
  if (Tok == "ORIGIN") {
1112
4
    StringRef Name = readParenLiteral();
1113
4
    if (Script->MemoryRegions.count(Name) == 0) {
1114
2
      setError("memory region not defined: " + Name);
1115
2
      return [] 
{ return 0; }1
;
1116
2
    }
1117
4
    
return [=] 2
{ return Script->MemoryRegions[Name]->Origin; };
1118
2
  }
1119
887
  if (Tok == "SEGMENT_START") {
1120
5
    expect("(");
1121
5
    skip();
1122
5
    expect(",");
1123
5
    Expr E = readExpr();
1124
5
    expect(")");
1125
9
    return [=] { return E(); };
1126
5
  }
1127
882
  if (Tok == "SIZEOF") {
1128
16
    StringRef Name = readParenLiteral();
1129
16
    OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
1130
16
    // Linker script does not create an output section if its content is empty.
1131
16
    // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1132
16
    // be empty.
1133
29
    return [=] { return Cmd->Size; };
1134
16
  }
1135
866
  if (Tok == "SIZEOF_HEADERS")
1136
63
    
return [=] 48
{ return elf::getHeaderSize(); };
1137
818
1138
818
  // Tok is the dot.
1139
818
  if (Tok == ".")
1140
223
    
return [=] 120
{ return Script->getSymbolValue(Tok, Location); };
1141
698
1142
698
  // Tok is a literal number.
1143
698
  if (Optional<uint64_t> Val = parseInt(Tok))
1144
816
    
return [=] 612
{ return *Val; };
1145
86
1146
86
  // Tok is a symbol name.
1147
86
  if (!isValidCIdentifier(Tok))
1148
12
    setError("malformed number: " + Tok);
1149
86
  Script->ReferencedSymbols.push_back(Tok);
1150
121
  return [=] { return Script->getSymbolValue(Tok, Location); };
1151
86
}
1152
1153
14
Expr ScriptParser::readTernary(Expr Cond) {
1154
14
  Expr L = readExpr();
1155
14
  expect(":");
1156
14
  Expr R = readExpr();
1157
19
  return [=] { return Cond().getValue() ? 
L()10
:
R()9
; };
1158
14
}
1159
1160
137
Expr ScriptParser::readParenExpr() {
1161
137
  expect("(");
1162
137
  Expr E = readExpr();
1163
137
  expect(")");
1164
137
  return E;
1165
137
}
1166
1167
676
std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1168
676
  std::vector<StringRef> Phdrs;
1169
715
  while (!errorCount() && 
peek().startswith(":")708
) {
1170
39
    StringRef Tok = next();
1171
39
    Phdrs.push_back((Tok.size() == 1) ? 
next()16
:
Tok.substr(1)23
);
1172
39
  }
1173
676
  return Phdrs;
1174
676
}
1175
1176
// Read a program header type name. The next token must be a
1177
// name of a program header type or a constant (e.g. "0x3").
1178
37
unsigned ScriptParser::readPhdrType() {
1179
37
  StringRef Tok = next();
1180
37
  if (Optional<uint64_t> Val = parseInt(Tok))
1181
2
    return *Val;
1182
35
1183
35
  unsigned Ret = StringSwitch<unsigned>(Tok)
1184
35
                     .Case("PT_NULL", PT_NULL)
1185
35
                     .Case("PT_LOAD", PT_LOAD)
1186
35
                     .Case("PT_DYNAMIC", PT_DYNAMIC)
1187
35
                     .Case("PT_INTERP", PT_INTERP)
1188
35
                     .Case("PT_NOTE", PT_NOTE)
1189
35
                     .Case("PT_SHLIB", PT_SHLIB)
1190
35
                     .Case("PT_PHDR", PT_PHDR)
1191
35
                     .Case("PT_TLS", PT_TLS)
1192
35
                     .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1193
35
                     .Case("PT_GNU_STACK", PT_GNU_STACK)
1194
35
                     .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1195
35
                     .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1196
35
                     .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1197
35
                     .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1198
35
                     .Default(-1);
1199
35
1200
35
  if (Ret == (unsigned)-1) {
1201
0
    setError("invalid program header type: " + Tok);
1202
0
    return PT_NULL;
1203
0
  }
1204
35
  return Ret;
1205
35
}
1206
1207
// Reads an anonymous version declaration.
1208
22
void ScriptParser::readAnonymousDeclaration() {
1209
22
  std::vector<SymbolVersion> Locals;
1210
22
  std::vector<SymbolVersion> Globals;
1211
22
  std::tie(Locals, Globals) = readSymbols();
1212
22
1213
22
  for (SymbolVersion V : Locals) {
1214
19
    if (V.Name == "*")
1215
18
      Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1216
1
    else
1217
1
      Config->VersionScriptLocals.push_back(V);
1218
19
  }
1219
22
1220
22
  for (SymbolVersion V : Globals)
1221
22
    Config->VersionScriptGlobals.push_back(V);
1222
22
1223
22
  expect(";");
1224
22
}
1225
1226
// Reads a non-anonymous version definition,
1227
// e.g. "VerStr { global: foo; bar; local: *; };".
1228
85
void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1229
85
  // Read a symbol list.
1230
85
  std::vector<SymbolVersion> Locals;
1231
85
  std::vector<SymbolVersion> Globals;
1232
85
  std::tie(Locals, Globals) = readSymbols();
1233
85
1234
85
  for (SymbolVersion V : Locals) {
1235
40
    if (V.Name == "*")
1236
33
      Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1237
7
    else
1238
7
      Config->VersionScriptLocals.push_back(V);
1239
40
  }
1240
85
1241
85
  // Create a new version definition and add that to the global symbols.
1242
85
  VersionDefinition Ver;
1243
85
  Ver.Name = VerStr;
1244
85
  Ver.Globals = Globals;
1245
85
1246
85
  // User-defined version number starts from 2 because 0 and 1 are
1247
85
  // reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively.
1248
85
  Ver.Id = Config->VersionDefinitions.size() + 2;
1249
85
  Config->VersionDefinitions.push_back(Ver);
1250
85
1251
85
  // Each version may have a parent version. For example, "Ver2"
1252
85
  // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1253
85
  // as a parent. This version hierarchy is, probably against your
1254
85
  // instinct, purely for hint; the runtime doesn't care about it
1255
85
  // at all. In LLD, we simply ignore it.
1256
85
  if (peek() != ";")
1257
3
    skip();
1258
85
  expect(";");
1259
85
}
1260
1261
167
static bool hasWildcard(StringRef S) {
1262
167
  return S.find_first_of("?*[") != StringRef::npos;
1263
167
}
1264
1265
// Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1266
std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1267
129
ScriptParser::readSymbols() {
1268
129
  std::vector<SymbolVersion> Locals;
1269
129
  std::vector<SymbolVersion> Globals;
1270
129
  std::vector<SymbolVersion> *V = &Globals;
1271
129
1272
427
  while (!errorCount()) {
1273
422
    if (consume("}"))
1274
124
      break;
1275
298
    if (consumeLabel("local")) {
1276
59
      V = &Locals;
1277
59
      continue;
1278
59
    }
1279
239
    if (consumeLabel("global")) {
1280
70
      V = &Globals;
1281
70
      continue;
1282
70
    }
1283
169
1284
169
    if (consume("extern")) {
1285
25
      std::vector<SymbolVersion> Ext = readVersionExtern();
1286
25
      V->insert(V->end(), Ext.begin(), Ext.end());
1287
144
    } else {
1288
144
      StringRef Tok = next();
1289
144
      V->push_back({unquote(Tok), false, hasWildcard(Tok)});
1290
144
    }
1291
169
    expect(";");
1292
169
  }
1293
129
  return {Locals, Globals};
1294
129
}
1295
1296
// Reads an "extern C++" directive, e.g.,
1297
// "extern "C++" { ns::*; "f(int, double)"; };"
1298
//
1299
// The last semicolon is optional. E.g. this is OK:
1300
// "extern "C++" { ns::*; "f(int, double)" };"
1301
25
std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1302
25
  StringRef Tok = next();
1303
25
  bool IsCXX = Tok == "\"C++\"";
1304
25
  if (!IsCXX && 
Tok != "\"C\""4
)
1305
1
    setError("Unknown language");
1306
25
  expect("{");
1307
25
1308
25
  std::vector<SymbolVersion> Ret;
1309
53
  while (!errorCount() && 
peek() != "}"52
) {
1310
30
    StringRef Tok = next();
1311
30
    bool HasWildcard = !Tok.startswith("\"") && 
hasWildcard(Tok)23
;
1312
30
    Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
1313
30
    if (consume("}"))
1314
2
      return Ret;
1315
28
    expect(";");
1316
28
  }
1317
25
1318
25
  expect("}");
1319
23
  return Ret;
1320
25
}
1321
1322
uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2,
1323
108
                                            StringRef S3) {
1324
108
  if (!consume(S1) && 
!consume(S2)32
&&
!consume(S3)16
) {
1325
10
    setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
1326
10
    return 0;
1327
10
  }
1328
98
  expect("=");
1329
98
  return readExpr()().getValue();
1330
98
}
1331
1332
// Parse the MEMORY command as specified in:
1333
// https://sourceware.org/binutils/docs/ld/MEMORY.html
1334
//
1335
// MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1336
33
void ScriptParser::readMemory() {
1337
33
  expect("{");
1338
87
  while (!errorCount() && 
!consume("}")77
) {
1339
54
    StringRef Name = next();
1340
54
1341
54
    uint32_t Flags = 0;
1342
54
    uint32_t NegFlags = 0;
1343
54
    if (consume("(")) {
1344
51
      std::tie(Flags, NegFlags) = readMemoryAttributes();
1345
51
      expect(")");
1346
51
    }
1347
54
    expect(":");
1348
54
1349
54
    uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
1350
54
    expect(",");
1351
54
    uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
1352
54
1353
54
    // Add the memory region to the region map.
1354
54
    MemoryRegion *MR =
1355
54
        make<MemoryRegion>(Name, Origin, Length, Flags, NegFlags);
1356
54
    if (!Script->MemoryRegions.insert({Name, MR}).second)
1357
1
      setError("region '" + Name + "' already defined");
1358
54
  }
1359
33
}
1360
1361
// This function parses the attributes used to match against section
1362
// flags when placing output sections in a memory region. These flags
1363
// are only used when an explicit memory region name is not used.
1364
51
std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
1365
51
  uint32_t Flags = 0;
1366
51
  uint32_t NegFlags = 0;
1367
51
  bool Invert = false;
1368
51
1369
132
  for (char C : next().lower()) {
1370
132
    uint32_t Flag = 0;
1371
132
    if (C == '!')
1372
2
      Invert = !Invert;
1373
130
    else if (C == 'w')
1374
30
      Flag = SHF_WRITE;
1375
100
    else if (C == 'x')
1376
47
      Flag = SHF_EXECINSTR;
1377
53
    else if (C == 'a')
1378
15
      Flag = SHF_ALLOC;
1379
38
    else if (C != 'r')
1380
2
      setError("invalid memory region attribute");
1381
132
1382
132
    if (Invert)
1383
6
      NegFlags |= Flag;
1384
126
    else
1385
126
      Flags |= Flag;
1386
132
  }
1387
51
  return {Flags, NegFlags};
1388
51
}
1389
1390
542
void elf::readLinkerScript(MemoryBufferRef MB) {
1391
542
  ScriptParser(MB).readLinkerScript();
1392
542
}
1393
1394
77
void elf::readVersionScript(MemoryBufferRef MB) {
1395
77
  ScriptParser(MB).readVersionScript();
1396
77
}
1397
1398
22
void elf::readDynamicList(MemoryBufferRef MB) {
1399
22
  ScriptParser(MB).readDynamicList();
1400
22
}
1401
1402
14
void elf::readDefsym(StringRef Name, MemoryBufferRef MB) {
1403
14
  ScriptParser(MB).readDefsym(Name);
1404
14
}