Bug Summary

File:tools/lld/ELF/ScriptParser.cpp
Warning:line 776, column 3
Undefined or garbage value returned to caller

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ScriptParser.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lld/ELF -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lld/ELF -I /build/llvm-toolchain-snapshot-7~svn329677/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lld/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/lld/ELF -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/lld/ELF/ScriptParser.cpp

/build/llvm-toolchain-snapshot-7~svn329677/tools/lld/ELF/ScriptParser.cpp

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

/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/Endian.h

1//===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
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 declares generic functions to read and write endian specific data.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_ENDIAN_H
15#define LLVM_SUPPORT_ENDIAN_H
16
17#include "llvm/Support/AlignOf.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/Host.h"
20#include "llvm/Support/SwapByteOrder.h"
21#include <cassert>
22#include <cstddef>
23#include <cstdint>
24#include <cstring>
25#include <type_traits>
26
27namespace llvm {
28namespace support {
29
30enum endianness {big, little, native};
31
32// These are named values for common alignments.
33enum {aligned = 0, unaligned = 1};
34
35namespace detail {
36
37/// \brief ::value is either alignment, or alignof(T) if alignment is 0.
38template<class T, int alignment>
39struct PickAlignment {
40 enum { value = alignment == 0 ? alignof(T) : alignment };
41};
42
43} // end namespace detail
44
45namespace endian {
46
47constexpr endianness system_endianness() {
48 return sys::IsBigEndianHost ? big : little;
49}
50
51template <typename value_type>
52inline value_type byte_swap(value_type value, endianness endian) {
53 if ((endian != native) && (endian != system_endianness()))
54 sys::swapByteOrder(value);
55 return value;
56}
57
58/// Swap the bytes of value to match the given endianness.
59template<typename value_type, endianness endian>
60inline value_type byte_swap(value_type value) {
61 return byte_swap(value, endian);
62}
63
64/// Read a value of a particular endianness from memory.
65template <typename value_type, std::size_t alignment>
66inline value_type read(const void *memory, endianness endian) {
67 value_type ret;
68
69 memcpy(&ret,
70 LLVM_ASSUME_ALIGNED(__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
71 memory, (detail::PickAlignment<value_type, alignment>::value))__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
,
72 sizeof(value_type));
73 return byte_swap<value_type>(ret, endian);
74}
75
76template<typename value_type,
77 endianness endian,
78 std::size_t alignment>
79inline value_type read(const void *memory) {
80 return read<value_type, alignment>(memory, endian);
81}
82
83/// Read a value of a particular endianness from a buffer, and increment the
84/// buffer past that value.
85template <typename value_type, std::size_t alignment, typename CharT>
86inline value_type readNext(const CharT *&memory, endianness endian) {
87 value_type ret = read<value_type, alignment>(memory, endian);
88 memory += sizeof(value_type);
89 return ret;
90}
91
92template<typename value_type, endianness endian, std::size_t alignment,
93 typename CharT>
94inline value_type readNext(const CharT *&memory) {
95 return readNext<value_type, alignment, CharT>(memory, endian);
96}
97
98/// Write a value to memory with a particular endianness.
99template <typename value_type, std::size_t alignment>
100inline void write(void *memory, value_type value, endianness endian) {
101 value = byte_swap<value_type>(value, endian);
102 memcpy(LLVM_ASSUME_ALIGNED(__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
103 memory, (detail::PickAlignment<value_type, alignment>::value))__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
,
104 &value, sizeof(value_type));
105}
106
107template<typename value_type,
108 endianness endian,
109 std::size_t alignment>
110inline void write(void *memory, value_type value) {
111 write<value_type, alignment>(memory, value, endian);
112}
113
114template <typename value_type>
115using make_unsigned_t = typename std::make_unsigned<value_type>::type;
116
117/// Read a value of a particular endianness from memory, for a location
118/// that starts at the given bit offset within the first byte.
119template <typename value_type, endianness endian, std::size_t alignment>
120inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
121 assert(startBit < 8)(static_cast <bool> (startBit < 8) ? void (0) : __assert_fail
("startBit < 8", "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/Endian.h"
, 121, __extension__ __PRETTY_FUNCTION__))
;
122 if (startBit == 0)
123 return read<value_type, endian, alignment>(memory);
124 else {
125 // Read two values and compose the result from them.
126 value_type val[2];
127 memcpy(&val[0],
128 LLVM_ASSUME_ALIGNED(__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
129 memory, (detail::PickAlignment<value_type, alignment>::value))__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
,
130 sizeof(value_type) * 2);
131 val[0] = byte_swap<value_type, endian>(val[0]);
132 val[1] = byte_swap<value_type, endian>(val[1]);
133
134 // Shift bits from the lower value into place.
135 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
136 // Mask off upper bits after right shift in case of signed type.
137 make_unsigned_t<value_type> numBitsFirstVal =
138 (sizeof(value_type) * 8) - startBit;
139 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
140
141 // Get the bits from the upper value.
142 make_unsigned_t<value_type> upperVal =
143 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
144 // Shift them in to place.
145 upperVal <<= numBitsFirstVal;
146
147 return lowerVal | upperVal;
148 }
149}
150
151/// Write a value to memory with a particular endianness, for a location
152/// that starts at the given bit offset within the first byte.
153template <typename value_type, endianness endian, std::size_t alignment>
154inline void writeAtBitAlignment(void *memory, value_type value,
155 uint64_t startBit) {
156 assert(startBit < 8)(static_cast <bool> (startBit < 8) ? void (0) : __assert_fail
("startBit < 8", "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/Endian.h"
, 156, __extension__ __PRETTY_FUNCTION__))
;
157 if (startBit == 0)
158 write<value_type, endian, alignment>(memory, value);
159 else {
160 // Read two values and shift the result into them.
161 value_type val[2];
162 memcpy(&val[0],
163 LLVM_ASSUME_ALIGNED(__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
164 memory, (detail::PickAlignment<value_type, alignment>::value))__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
,
165 sizeof(value_type) * 2);
166 val[0] = byte_swap<value_type, endian>(val[0]);
167 val[1] = byte_swap<value_type, endian>(val[1]);
168
169 // Mask off any existing bits in the upper part of the lower value that
170 // we want to replace.
171 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
172 make_unsigned_t<value_type> numBitsFirstVal =
173 (sizeof(value_type) * 8) - startBit;
174 make_unsigned_t<value_type> lowerVal = value;
175 if (startBit > 0) {
176 // Mask off the upper bits in the new value that are not going to go into
177 // the lower value. This avoids a left shift of a negative value, which
178 // is undefined behavior.
179 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
180 // Now shift the new bits into place
181 lowerVal <<= startBit;
182 }
183 val[0] |= lowerVal;
184
185 // Mask off any existing bits in the lower part of the upper value that
186 // we want to replace.
187 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
188 // Next shift the bits that go into the upper value into position.
189 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
190 // Mask off upper bits after right shift in case of signed type.
191 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
192 val[1] |= upperVal;
193
194 // Finally, rewrite values.
195 val[0] = byte_swap<value_type, endian>(val[0]);
196 val[1] = byte_swap<value_type, endian>(val[1]);
197 memcpy(LLVM_ASSUME_ALIGNED(__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
198 memory, (detail::PickAlignment<value_type, alignment>::value))__builtin_assume_aligned(memory, (detail::PickAlignment<value_type
, alignment>::value))
,
199 &val[0], sizeof(value_type) * 2);
200 }
201}
202
203} // end namespace endian
204
205namespace detail {
206
207template<typename value_type,
208 endianness endian,
209 std::size_t alignment>
210struct packed_endian_specific_integral {
211 packed_endian_specific_integral() = default;
212
213 explicit packed_endian_specific_integral(value_type val) { *this = val; }
214
215 operator value_type() const {
216 return endian::read<value_type, endian, alignment>(
217 (const void*)Value.buffer);
218 }
219
220 void operator=(value_type newValue) {
221 endian::write<value_type, endian, alignment>(
222 (void*)Value.buffer, newValue);
223 }
224
225 packed_endian_specific_integral &operator+=(value_type newValue) {
226 *this = *this + newValue;
227 return *this;
228 }
229
230 packed_endian_specific_integral &operator-=(value_type newValue) {
231 *this = *this - newValue;
232 return *this;
233 }
234
235 packed_endian_specific_integral &operator|=(value_type newValue) {
236 *this = *this | newValue;
237 return *this;
238 }
239
240 packed_endian_specific_integral &operator&=(value_type newValue) {
241 *this = *this & newValue;
242 return *this;
243 }
244
245private:
246 AlignedCharArray<PickAlignment<value_type, alignment>::value,
247 sizeof(value_type)> Value;
248
249public:
250 struct ref {
251 explicit ref(void *Ptr) : Ptr(Ptr) {}
252
253 operator value_type() const {
254 return endian::read<value_type, endian, alignment>(Ptr);
255 }
256
257 void operator=(value_type NewValue) {
258 endian::write<value_type, endian, alignment>(Ptr, NewValue);
259 }
260
261 private:
262 void *Ptr;
263 };
264};
265
266} // end namespace detail
267
268using ulittle16_t =
269 detail::packed_endian_specific_integral<uint16_t, little, unaligned>;
270using ulittle32_t =
271 detail::packed_endian_specific_integral<uint32_t, little, unaligned>;
272using ulittle64_t =
273 detail::packed_endian_specific_integral<uint64_t, little, unaligned>;
274
275using little16_t =
276 detail::packed_endian_specific_integral<int16_t, little, unaligned>;
277using little32_t =
278 detail::packed_endian_specific_integral<int32_t, little, unaligned>;
279using little64_t =
280 detail::packed_endian_specific_integral<int64_t, little, unaligned>;
281
282using aligned_ulittle16_t =
283 detail::packed_endian_specific_integral<uint16_t, little, aligned>;
284using aligned_ulittle32_t =
285 detail::packed_endian_specific_integral<uint32_t, little, aligned>;
286using aligned_ulittle64_t =
287 detail::packed_endian_specific_integral<uint64_t, little, aligned>;
288
289using aligned_little16_t =
290 detail::packed_endian_specific_integral<int16_t, little, aligned>;
291using aligned_little32_t =
292 detail::packed_endian_specific_integral<int32_t, little, aligned>;
293using aligned_little64_t =
294 detail::packed_endian_specific_integral<int64_t, little, aligned>;
295
296using ubig16_t =
297 detail::packed_endian_specific_integral<uint16_t, big, unaligned>;
298using ubig32_t =
299 detail::packed_endian_specific_integral<uint32_t, big, unaligned>;
300using ubig64_t =
301 detail::packed_endian_specific_integral<uint64_t, big, unaligned>;
302
303using big16_t =
304 detail::packed_endian_specific_integral<int16_t, big, unaligned>;
305using big32_t =
306 detail::packed_endian_specific_integral<int32_t, big, unaligned>;
307using big64_t =
308 detail::packed_endian_specific_integral<int64_t, big, unaligned>;
309
310using aligned_ubig16_t =
311 detail::packed_endian_specific_integral<uint16_t, big, aligned>;
312using aligned_ubig32_t =
313 detail::packed_endian_specific_integral<uint32_t, big, aligned>;
314using aligned_ubig64_t =
315 detail::packed_endian_specific_integral<uint64_t, big, aligned>;
316
317using aligned_big16_t =
318 detail::packed_endian_specific_integral<int16_t, big, aligned>;
319using aligned_big32_t =
320 detail::packed_endian_specific_integral<int32_t, big, aligned>;
321using aligned_big64_t =
322 detail::packed_endian_specific_integral<int64_t, big, aligned>;
323
324using unaligned_uint16_t =
325 detail::packed_endian_specific_integral<uint16_t, native, unaligned>;
326using unaligned_uint32_t =
327 detail::packed_endian_specific_integral<uint32_t, native, unaligned>;
328using unaligned_uint64_t =
329 detail::packed_endian_specific_integral<uint64_t, native, unaligned>;
330
331using unaligned_int16_t =
332 detail::packed_endian_specific_integral<int16_t, native, unaligned>;
333using unaligned_int32_t =
334 detail::packed_endian_specific_integral<int32_t, native, unaligned>;
335using unaligned_int64_t =
336 detail::packed_endian_specific_integral<int64_t, native, unaligned>;
337
338namespace endian {
339
340template <typename T> inline T read(const void *P, endianness E) {
341 return read<T, unaligned>(P, E);
342}
343
344template <typename T, endianness E> inline T read(const void *P) {
345 return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
346}
347
348inline uint16_t read16(const void *P, endianness E) {
349 return read<uint16_t>(P, E);
350}
351inline uint32_t read32(const void *P, endianness E) {
352 return read<uint32_t>(P, E);
353}
354inline uint64_t read64(const void *P, endianness E) {
355 return read<uint64_t>(P, E);
356}
357
358template <endianness E> inline uint16_t read16(const void *P) {
359 return read<uint16_t, E>(P);
360}
361template <endianness E> inline uint32_t read32(const void *P) {
362 return read<uint32_t, E>(P);
363}
364template <endianness E> inline uint64_t read64(const void *P) {
365 return read<uint64_t, E>(P);
366}
367
368inline uint16_t read16le(const void *P) { return read16<little>(P); }
369inline uint32_t read32le(const void *P) { return read32<little>(P); }
370inline uint64_t read64le(const void *P) { return read64<little>(P); }
371inline uint16_t read16be(const void *P) { return read16<big>(P); }
372inline uint32_t read32be(const void *P) { return read32<big>(P); }
373inline uint64_t read64be(const void *P) { return read64<big>(P); }
374
375template <typename T> inline void write(void *P, T V, endianness E) {
376 write<T, unaligned>(P, V, E);
377}
378
379template <typename T, endianness E> inline void write(void *P, T V) {
380 *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
381}
7
Returning without writing to '*P'
382
383inline void write16(void *P, uint16_t V, endianness E) {
384 write<uint16_t>(P, V, E);
385}
386inline void write32(void *P, uint32_t V, endianness E) {
387 write<uint32_t>(P, V, E);
388}
389inline void write64(void *P, uint64_t V, endianness E) {
390 write<uint64_t>(P, V, E);
391}
392
393template <endianness E> inline void write16(void *P, uint16_t V) {
394 write<uint16_t, E>(P, V);
395}
396template <endianness E> inline void write32(void *P, uint32_t V) {
397 write<uint32_t, E>(P, V);
6
Calling 'write'
8
Returning from 'write'
398}
9
Returning without writing to '*P'
399template <endianness E> inline void write64(void *P, uint64_t V) {
400 write<uint64_t, E>(P, V);
401}
402
403inline void write16le(void *P, uint16_t V) { write16<little>(P, V); }
404inline void write32le(void *P, uint32_t V) { write32<little>(P, V); }
405inline void write64le(void *P, uint64_t V) { write64<little>(P, V); }
406inline void write16be(void *P, uint16_t V) { write16<big>(P, V); }
407inline void write32be(void *P, uint32_t V) { write32<big>(P, V); }
5
Calling 'write32'
10
Returning from 'write32'
11
Returning without writing to '*P'
408inline void write64be(void *P, uint64_t V) { write64<big>(P, V); }
409
410} // end namespace endian
411
412} // end namespace support
413} // end namespace llvm
414
415#endif // LLVM_SUPPORT_ENDIAN_H