| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 1 | //===- SymbolListFile.cpp -------------------------------------------------===// |
| Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 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 the parser/evaluator of the linker script. |
| 11 | // It does not construct an AST but consume linker script directives directly. |
| 12 | // Results are written to Driver or Config object. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 16 | #include "SymbolListFile.h" |
| Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 17 | #include "Config.h" |
| 18 | #include "ScriptParser.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace lld; |
| 23 | using namespace lld::elf; |
| 24 | |
| 25 | // Parse the --dynamic-list argument. A dynamic list is in the form |
| 26 | // |
| 27 | // { symbol1; symbol2; [...]; symbolN }; |
| 28 | // |
| 29 | // Multiple groups can be defined in the same file and they are merged |
| 30 | // in only one definition. |
| 31 | |
| 32 | class DynamicListParser final : public ScriptParserBase { |
| 33 | public: |
| 34 | DynamicListParser(StringRef S) : ScriptParserBase(S) {} |
| 35 | |
| Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 36 | void run(); |
| Adhemerval Zanella | 9df0720 | 2016-04-13 18:51:11 +0000 | [diff] [blame] | 37 | |
| 38 | private: |
| 39 | void readGroup(); |
| 40 | }; |
| 41 | |
| 42 | // Parse the default group definition using C language symbol name. |
| 43 | void DynamicListParser::readGroup() { |
| 44 | expect("{"); |
| 45 | while (!Error) { |
| 46 | Config->DynamicList.push_back(next()); |
| 47 | expect(";"); |
| 48 | if (peek() == "}") { |
| 49 | next(); |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | expect(";"); |
| 54 | } |
| 55 | |
| 56 | void DynamicListParser::run() { |
| 57 | while (!atEOF()) |
| 58 | readGroup(); |
| 59 | } |
| 60 | |
| 61 | void elf::parseDynamicList(MemoryBufferRef MB) { |
| 62 | DynamicListParser(MB.getBuffer()).run(); |
| 63 | } |
| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 64 | |
| 65 | // Parse the --version-script argument. We currently only accept the following |
| 66 | // version script syntax: |
| 67 | // |
| 68 | // { [ global: symbol1; symbol2; [...]; symbolN; ] local: *; }; |
| 69 | // |
| 70 | // No wildcards are supported, other than for the local entry. Symbol versioning |
| 71 | // is also not supported. |
| 72 | |
| 73 | class VersionScriptParser final : public ScriptParserBase { |
| 74 | public: |
| 75 | VersionScriptParser(StringRef S) : ScriptParserBase(S) {} |
| 76 | |
| Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 77 | void run(); |
| George Rimar | 95eeb77 | 2016-06-16 18:47:04 +0000 | [diff] [blame^] | 78 | |
| 79 | private: |
| 80 | void parseVersion(); |
| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| George Rimar | 95eeb77 | 2016-06-16 18:47:04 +0000 | [diff] [blame^] | 83 | void VersionScriptParser::parseVersion() { |
| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 84 | expect("{"); |
| 85 | if (peek() == "global:") { |
| 86 | next(); |
| 87 | while (!Error) { |
| 88 | Config->VersionScriptGlobals.push_back(next()); |
| 89 | expect(";"); |
| 90 | if (peek() == "local:") |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | expect("local:"); |
| 95 | expect("*"); |
| 96 | expect(";"); |
| 97 | expect("}"); |
| 98 | expect(";"); |
| George Rimar | 95eeb77 | 2016-06-16 18:47:04 +0000 | [diff] [blame^] | 99 | } |
| 100 | |
| 101 | void VersionScriptParser::run() { |
| 102 | StringRef Msg = "anonymous version definition is used in " |
| 103 | "combination with other version definitions"; |
| 104 | if (peek() == "{") { |
| 105 | parseVersion(); |
| 106 | if (!atEOF()) |
| 107 | setError(Msg); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | while (!atEOF() && !Error) { |
| 112 | if (next() == "{") { |
| 113 | setError(Msg); |
| 114 | return; |
| 115 | } |
| 116 | parseVersion(); |
| 117 | } |
| Peter Collingbourne | 66ac1d6 | 2016-04-22 20:21:26 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void elf::parseVersionScript(MemoryBufferRef MB) { |
| 121 | VersionScriptParser(MB.getBuffer()).run(); |
| 122 | } |