| 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 |  | 
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 13 | #include "Writer.h" | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 14 | #include "lld/Core/LLVM.h" | 
|  | 15 | #include "llvm/ADT/DenseMap.h" | 
|  | 16 | #include "llvm/ADT/MapVector.h" | 
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Allocator.h" | 
|  | 18 | #include "llvm/Support/MemoryBuffer.h" | 
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 19 | #include <functional> | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 20 |  | 
|  | 21 | namespace lld { | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 22 | namespace elf { | 
| Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 23 | class SymbolBody; | 
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 24 | template <class ELFT> class InputSectionBase; | 
|  | 25 | template <class ELFT> class OutputSectionBase; | 
|  | 26 | template <class ELFT> class OutputSectionFactory; | 
| Eugene Leviant | 3e6b027 | 2016-07-28 19:24:13 +0000 | [diff] [blame] | 27 | template <class ELFT> class DefinedCommon; | 
| Rui Ueyama | f34d0e0 | 2016-08-12 01:24:53 +0000 | [diff] [blame] | 28 | template <class ELFT> class LayoutInputSection; | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 29 |  | 
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 30 | typedef std::function<uint64_t(uint64_t)> Expr; | 
|  | 31 |  | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 32 | // Parses a linker script. Calling this function updates | 
|  | 33 | // Config and ScriptConfig. | 
|  | 34 | void readLinkerScript(MemoryBufferRef MB); | 
|  | 35 |  | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 36 | class ScriptParser; | 
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 37 | template <class ELFT> class InputSectionBase; | 
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 38 | template <class ELFT> class OutputSectionBase; | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 39 |  | 
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 40 | // This enum is used to implement linker script SECTIONS command. | 
|  | 41 | // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS | 
|  | 42 | enum SectionsCommandKind { | 
|  | 43 | AssignmentKind, | 
|  | 44 | OutputSectionKind, | 
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 45 | InputSectionKind, | 
|  | 46 | AssertKind | 
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 47 | }; | 
|  | 48 |  | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 49 | struct BaseCommand { | 
|  | 50 | BaseCommand(int K) : Kind(K) {} | 
|  | 51 | virtual ~BaseCommand() {} | 
|  | 52 | int Kind; | 
|  | 53 | }; | 
|  | 54 |  | 
|  | 55 | struct SymbolAssignment : BaseCommand { | 
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 56 | SymbolAssignment(StringRef Name, Expr E) | 
|  | 57 | : BaseCommand(AssignmentKind), Name(Name), Expression(E) {} | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 58 | static bool classof(const BaseCommand *C); | 
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 59 |  | 
|  | 60 | // The LHS of an expression. Name is either a symbol name or ".". | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 61 | StringRef Name; | 
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 62 | SymbolBody *Sym = nullptr; | 
|  | 63 |  | 
|  | 64 | // The RHS of an expression. | 
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 65 | Expr Expression; | 
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 66 |  | 
|  | 67 | // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN. | 
| Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 68 | bool Provide = false; | 
| Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 69 | bool Hidden = false; | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 70 | }; | 
|  | 71 |  | 
| Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 72 | // Linker scripts allow additional constraints to be put on ouput sections. | 
|  | 73 | // An output section will only be created if all of its input sections are | 
|  | 74 | // read-only | 
|  | 75 | // or all of its input sections are read-write by using the keyword ONLY_IF_RO | 
|  | 76 | // and ONLY_IF_RW respectively. | 
| Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 77 | enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite }; | 
| Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 78 |  | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 79 | struct OutputSectionCommand : BaseCommand { | 
|  | 80 | OutputSectionCommand(StringRef Name) | 
|  | 81 | : BaseCommand(OutputSectionKind), Name(Name) {} | 
|  | 82 | static bool classof(const BaseCommand *C); | 
| Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 83 | StringRef Name; | 
| George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 84 | Expr AddrExpr; | 
| George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 85 | Expr AlignExpr; | 
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 86 | std::vector<std::unique_ptr<BaseCommand>> Commands; | 
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 87 | std::vector<StringRef> Phdrs; | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 88 | std::vector<uint8_t> Filler; | 
| Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 89 | ConstraintKind Constraint = ConstraintKind::NoConstraint; | 
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 90 | }; | 
|  | 91 |  | 
| Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 92 | enum SortKind { SortNone, SortByName, SortByAlignment }; | 
| George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 93 |  | 
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 94 | struct InputSectionDescription : BaseCommand { | 
|  | 95 | InputSectionDescription() : BaseCommand(InputSectionKind) {} | 
|  | 96 | static bool classof(const BaseCommand *C); | 
| George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 97 | StringRef FilePattern; | 
| Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 98 | SortKind SortOuter = SortNone; | 
|  | 99 | SortKind SortInner = SortNone; | 
| Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 100 | std::vector<StringRef> ExcludedFiles; | 
| George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 101 | std::vector<StringRef> SectionPatterns; | 
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 102 | }; | 
|  | 103 |  | 
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 104 | struct AssertCommand : BaseCommand { | 
|  | 105 | AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {} | 
|  | 106 | static bool classof(const BaseCommand *C); | 
|  | 107 | Expr Expression; | 
|  | 108 | }; | 
|  | 109 |  | 
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 110 | struct PhdrsCommand { | 
|  | 111 | StringRef Name; | 
|  | 112 | unsigned Type; | 
|  | 113 | bool HasFilehdr; | 
|  | 114 | bool HasPhdrs; | 
| Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 115 | unsigned Flags; | 
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 116 | }; | 
|  | 117 |  | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 118 | // ScriptConfiguration holds linker script parse results. | 
|  | 119 | struct ScriptConfiguration { | 
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 120 | // Used to assign addresses to sections. | 
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 121 | std::vector<std::unique_ptr<BaseCommand>> Commands; | 
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 122 |  | 
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 123 | // Used to assign sections to headers. | 
| George Rimar | 70ce0a9 | 2016-07-20 15:09:10 +0000 | [diff] [blame] | 124 | std::vector<PhdrsCommand> PhdrsCommands; | 
|  | 125 |  | 
| Rui Ueyama | 3de0a33 | 2016-07-29 03:31:09 +0000 | [diff] [blame] | 126 | bool HasContents = false; | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 127 |  | 
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 128 | llvm::BumpPtrAllocator Alloc; | 
| Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 129 |  | 
|  | 130 | // List of section patterns specified with KEEP commands. They will | 
|  | 131 | // be kept even if they are unused and --gc-sections is specified. | 
|  | 132 | std::vector<StringRef> KeptSections; | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 135 | extern ScriptConfiguration *ScriptConfig; | 
|  | 136 |  | 
|  | 137 | // This is a runner of the linker script. | 
|  | 138 | template <class ELFT> class LinkerScript { | 
| Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 139 | typedef typename ELFT::uint uintX_t; | 
|  | 140 |  | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 141 | public: | 
| Rui Ueyama | f34d0e0 | 2016-08-12 01:24:53 +0000 | [diff] [blame] | 142 | LinkerScript(); | 
|  | 143 | ~LinkerScript(); | 
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 144 | void createSections(OutputSectionFactory<ELFT> &Factory); | 
| Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 145 |  | 
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 146 | std::vector<PhdrEntry<ELFT>> createPhdrs(); | 
| Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 147 |  | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 148 | ArrayRef<uint8_t> getFiller(StringRef Name); | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 149 | bool shouldKeep(InputSectionBase<ELFT> *S); | 
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 150 | void assignAddresses(); | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 151 | int compareSections(StringRef A, StringRef B); | 
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 152 | bool hasPhdrsCommands(); | 
| George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 153 | uintX_t getOutputSectionSize(StringRef Name); | 
| George Rimar | e32a359 | 2016-08-10 07:59:34 +0000 | [diff] [blame] | 154 | uintX_t getSizeOfHeaders(); | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 155 |  | 
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 156 | std::vector<OutputSectionBase<ELFT> *> *OutputSections; | 
|  | 157 |  | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 158 | private: | 
| Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 159 | std::vector<InputSectionBase<ELFT> *> | 
| Rui Ueyama | ad10c3d | 2016-07-28 21:05:04 +0000 | [diff] [blame] | 160 | getInputSections(const InputSectionDescription *); | 
| Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 161 |  | 
| Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 162 | void discard(OutputSectionCommand &Cmd); | 
|  | 163 |  | 
| Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame^] | 164 | std::vector<InputSectionBase<ELFT> *> | 
|  | 165 | createInputSectionList(OutputSectionCommand &Cmd); | 
|  | 166 |  | 
| Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 167 | // "ScriptConfig" is a bit too long, so define a short name for it. | 
|  | 168 | ScriptConfiguration &Opt = *ScriptConfig; | 
|  | 169 |  | 
| George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 170 | void filter(); | 
| Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 171 |  | 
| Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 172 | int getSectionIndex(StringRef Name); | 
| Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 173 | std::vector<size_t> getPhdrIndices(StringRef SectionName); | 
| Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 174 | size_t getPhdrIndex(StringRef PhdrName); | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 175 |  | 
| Rui Ueyama | f34d0e0 | 2016-08-12 01:24:53 +0000 | [diff] [blame] | 176 | llvm::SpecificBumpPtrAllocator<LayoutInputSection<ELFT>> LAlloc; | 
|  | 177 |  | 
| Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 178 | uintX_t Dot; | 
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 179 | }; | 
|  | 180 |  | 
|  | 181 | // Variable template is a C++14 feature, so we can't template | 
|  | 182 | // a global variable. Use a struct to workaround. | 
|  | 183 | template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; | 
|  | 184 | template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 185 |  | 
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 186 | } // namespace elf | 
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 187 | } // namespace lld | 
|  | 188 |  | 
|  | 189 | #endif |