| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 1 | //===- LinkerScript.h -------------------------------------------*- C++ -*-===// |
| 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 | #ifndef LLD_ELF_LINKER_SCRIPT_H |
| 11 | #define LLD_ELF_LINKER_SCRIPT_H |
| 12 | |
| 13 | #include "lld/Core/LLVM.h" |
| 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "llvm/ADT/MapVector.h" |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Allocator.h" |
| 17 | #include "llvm/Support/MemoryBuffer.h" |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 18 | |
| 19 | namespace lld { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 20 | namespace elf { |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 21 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 22 | // Parses a linker script. Calling this function updates |
| 23 | // Config and ScriptConfig. |
| 24 | void readLinkerScript(MemoryBufferRef MB); |
| 25 | |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 26 | class ScriptParser; |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 27 | template <class ELFT> class InputSectionBase; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 28 | template <class ELFT> class OutputSectionBase; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 29 | |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 30 | // This class represents each rule in SECTIONS command. |
| Rui Ueyama | c9f402e | 2016-04-22 00:23:52 +0000 | [diff] [blame] | 31 | struct SectionRule { |
| Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 32 | SectionRule(StringRef D, StringRef S) |
| 33 | : Dest(D), SectionPattern(S) {} |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 34 | |
| 35 | // Returns true if S should be in Dest section. |
| 36 | template <class ELFT> bool match(InputSectionBase<ELFT> *S); |
| 37 | |
| 38 | StringRef Dest; |
| 39 | |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 40 | StringRef SectionPattern; |
| 41 | }; |
| 42 | |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 43 | // This enum represents what we can observe in SECTIONS tag of script: |
| George Rimar | a3ab109 | 2016-04-19 12:09:25 +0000 | [diff] [blame] | 44 | // ExprKind is a location counter change, like ". = . + 0x1000" |
| 45 | // SectionKind is a description of output section, like ".data :..." |
| Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 46 | enum SectionsCommandKind { ExprKind, SectionKind }; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 47 | |
| Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 48 | struct SectionsCommand { |
| 49 | SectionsCommandKind Kind; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 50 | std::vector<StringRef> Expr; |
| 51 | StringRef SectionName; |
| 52 | }; |
| 53 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 54 | // ScriptConfiguration holds linker script parse results. |
| 55 | struct ScriptConfiguration { |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 56 | // SECTIONS commands. |
| 57 | std::vector<SectionRule> Sections; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 58 | |
| Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 59 | // Section fill attribute for each section. |
| 60 | llvm::StringMap<std::vector<uint8_t>> Filler; |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 61 | |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 62 | // Used to assign addresses to sections. |
| Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 63 | std::vector<SectionsCommand> Commands; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 64 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 65 | bool DoLayout = false; |
| 66 | |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 67 | llvm::BumpPtrAllocator Alloc; |
| Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 68 | |
| 69 | // List of section patterns specified with KEEP commands. They will |
| 70 | // be kept even if they are unused and --gc-sections is specified. |
| 71 | std::vector<StringRef> KeptSections; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 74 | extern ScriptConfiguration *ScriptConfig; |
| 75 | |
| 76 | // This is a runner of the linker script. |
| 77 | template <class ELFT> class LinkerScript { |
| 78 | public: |
| 79 | StringRef getOutputSection(InputSectionBase<ELFT> *S); |
| 80 | ArrayRef<uint8_t> getFiller(StringRef Name); |
| 81 | bool isDiscarded(InputSectionBase<ELFT> *S); |
| 82 | bool shouldKeep(InputSectionBase<ELFT> *S); |
| George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 83 | void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 84 | int compareSections(StringRef A, StringRef B); |
| 85 | |
| 86 | private: |
| Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 87 | // "ScriptConfig" is a bit too long, so define a short name for it. |
| 88 | ScriptConfiguration &Opt = *ScriptConfig; |
| 89 | |
| Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 90 | int getSectionIndex(StringRef Name); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 91 | |
| Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 92 | uint64_t evaluate(ArrayRef<StringRef> Tokens); |
| 93 | uint64_t parseExpr(ArrayRef<StringRef> &Tokens); |
| 94 | uint64_t parsePrimary(ArrayRef<StringRef> &Tokens); |
| 95 | uint64_t parseExpr1(ArrayRef<StringRef> &Tokens, uint64_t Lhs, int MinPrec); |
| George Rimar | fba45c4 | 2016-04-22 11:28:54 +0000 | [diff] [blame^] | 96 | uint64_t parseTernary(ArrayRef<StringRef> &Tokens, uint64_t Cond); |
| Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 97 | typename ELFT::uint Dot; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | // Variable template is a C++14 feature, so we can't template |
| 101 | // a global variable. Use a struct to workaround. |
| 102 | template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; |
| 103 | template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 104 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 105 | } // namespace elf |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 106 | } // namespace lld |
| 107 | |
| 108 | #endif |