| 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 | |
| George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame^] | 13 | #include "Config.h" |
| George Rimar | c91930a | 2016-09-02 21:17:20 +0000 | [diff] [blame] | 14 | #include "Strings.h" |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 15 | #include "Writer.h" |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 16 | #include "lld/Core/LLVM.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/MapVector.h" |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Allocator.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| George Rimar | c91930a | 2016-09-02 21:17:20 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Regex.h" |
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 23 | #include <functional> |
| George Rimar | 395281c | 2016-09-16 17:42:10 +0000 | [diff] [blame] | 24 | #include <list> |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 25 | |
| 26 | namespace lld { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 27 | namespace elf { |
| Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 28 | class DefinedCommon; |
| George Rimar | dbb76db | 2016-08-18 13:00:49 +0000 | [diff] [blame] | 29 | class ScriptParser; |
| Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 30 | class SymbolBody; |
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 31 | template <class ELFT> class InputSectionBase; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 32 | template <class ELFT> class InputSection; |
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 33 | template <class ELFT> class OutputSectionBase; |
| 34 | template <class ELFT> class OutputSectionFactory; |
| Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 35 | class InputSectionData; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 36 | |
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 37 | typedef std::function<uint64_t(uint64_t)> Expr; |
| 38 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 39 | // Parses a linker script. Calling this function updates |
| 40 | // Config and ScriptConfig. |
| 41 | void readLinkerScript(MemoryBufferRef MB); |
| 42 | |
| George Rimar | 20b6598 | 2016-08-31 09:08:26 +0000 | [diff] [blame] | 43 | void readVersionScript(MemoryBufferRef MB); |
| 44 | |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 45 | // This enum is used to implement linker script SECTIONS command. |
| 46 | // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS |
| 47 | enum SectionsCommandKind { |
| 48 | AssignmentKind, |
| 49 | OutputSectionKind, |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 50 | InputSectionKind, |
| 51 | AssertKind |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 54 | struct BaseCommand { |
| 55 | BaseCommand(int K) : Kind(K) {} |
| 56 | virtual ~BaseCommand() {} |
| 57 | int Kind; |
| 58 | }; |
| 59 | |
| 60 | struct SymbolAssignment : BaseCommand { |
| Eugene Leviant | db741e7 | 2016-09-07 07:08:43 +0000 | [diff] [blame] | 61 | SymbolAssignment(StringRef Name, Expr E, bool IsAbsolute) |
| 62 | : BaseCommand(AssignmentKind), Name(Name), Expression(E), |
| 63 | IsAbsolute(IsAbsolute) {} |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 64 | static bool classof(const BaseCommand *C); |
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 65 | |
| 66 | // The LHS of an expression. Name is either a symbol name or ".". |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 67 | StringRef Name; |
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 68 | SymbolBody *Sym = nullptr; |
| 69 | |
| 70 | // The RHS of an expression. |
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 71 | Expr Expression; |
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 72 | |
| 73 | // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN. |
| Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 74 | bool Provide = false; |
| Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 75 | bool Hidden = false; |
| Eugene Leviant | db741e7 | 2016-09-07 07:08:43 +0000 | [diff] [blame] | 76 | bool IsAbsolute; |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 79 | // Linker scripts allow additional constraints to be put on ouput sections. |
| 80 | // An output section will only be created if all of its input sections are |
| 81 | // read-only |
| 82 | // or all of its input sections are read-write by using the keyword ONLY_IF_RO |
| 83 | // and ONLY_IF_RW respectively. |
| Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 84 | enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite }; |
| Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 85 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 86 | struct OutputSectionCommand : BaseCommand { |
| 87 | OutputSectionCommand(StringRef Name) |
| 88 | : BaseCommand(OutputSectionKind), Name(Name) {} |
| 89 | static bool classof(const BaseCommand *C); |
| Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 90 | StringRef Name; |
| George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 91 | Expr AddrExpr; |
| George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 92 | Expr AlignExpr; |
| George Rimar | 8ceadb3 | 2016-08-17 07:44:19 +0000 | [diff] [blame] | 93 | Expr LmaExpr; |
| George Rimar | db24d9c | 2016-08-19 15:18:23 +0000 | [diff] [blame] | 94 | Expr SubalignExpr; |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 95 | std::vector<std::unique_ptr<BaseCommand>> Commands; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 96 | std::vector<StringRef> Phdrs; |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 97 | std::vector<uint8_t> Filler; |
| Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 98 | ConstraintKind Constraint = ConstraintKind::NoConstraint; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 101 | struct InputSectionDescription : BaseCommand { |
| George Rimar | c91930a | 2016-09-02 21:17:20 +0000 | [diff] [blame] | 102 | InputSectionDescription(StringRef FilePattern) |
| 103 | : BaseCommand(InputSectionKind), |
| 104 | FileRe(compileGlobPatterns({FilePattern})) {} |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 105 | static bool classof(const BaseCommand *C); |
| George Rimar | c91930a | 2016-09-02 21:17:20 +0000 | [diff] [blame] | 106 | llvm::Regex FileRe; |
| George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame^] | 107 | SortSectionPolicy SortOuter = SortSectionPolicy::None; |
| 108 | SortSectionPolicy SortInner = SortSectionPolicy::None; |
| George Rimar | 395281c | 2016-09-16 17:42:10 +0000 | [diff] [blame] | 109 | // Pairs of section regex and files excluded. |
| 110 | std::list<std::pair<llvm::Regex, llvm::Regex>> SectionsVec; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 111 | std::vector<InputSectionData *> Sections; |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 114 | struct AssertCommand : BaseCommand { |
| 115 | AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {} |
| 116 | static bool classof(const BaseCommand *C); |
| 117 | Expr Expression; |
| 118 | }; |
| 119 | |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 120 | struct PhdrsCommand { |
| 121 | StringRef Name; |
| 122 | unsigned Type; |
| 123 | bool HasFilehdr; |
| 124 | bool HasPhdrs; |
| Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 125 | unsigned Flags; |
| Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 126 | Expr LMAExpr; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 129 | class LinkerScriptBase { |
| Rafael Espindola | 4d1e4d7 | 2016-09-08 14:11:08 +0000 | [diff] [blame] | 130 | protected: |
| 131 | ~LinkerScriptBase() = default; |
| 132 | |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 133 | public: |
| 134 | virtual uint64_t getOutputSectionAddress(StringRef Name) = 0; |
| 135 | virtual uint64_t getOutputSectionSize(StringRef Name) = 0; |
| Eugene Leviant | 36fac7f | 2016-09-08 09:08:30 +0000 | [diff] [blame] | 136 | virtual uint64_t getOutputSectionAlign(StringRef Name) = 0; |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 137 | virtual uint64_t getHeaderSize() = 0; |
| 138 | virtual uint64_t getSymbolValue(StringRef S) = 0; |
| 139 | }; |
| 140 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 141 | // ScriptConfiguration holds linker script parse results. |
| 142 | struct ScriptConfiguration { |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 143 | // Used to assign addresses to sections. |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 144 | std::vector<std::unique_ptr<BaseCommand>> Commands; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 145 | |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 146 | // Used to assign sections to headers. |
| George Rimar | 70ce0a9 | 2016-07-20 15:09:10 +0000 | [diff] [blame] | 147 | std::vector<PhdrsCommand> PhdrsCommands; |
| 148 | |
| Eugene Leviant | e05336ff | 2016-09-14 08:32:36 +0000 | [diff] [blame] | 149 | bool HasSections = false; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 150 | |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 151 | llvm::BumpPtrAllocator Alloc; |
| Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 152 | |
| 153 | // List of section patterns specified with KEEP commands. They will |
| 154 | // be kept even if they are unused and --gc-sections is specified. |
| George Rimar | c91930a | 2016-09-02 21:17:20 +0000 | [diff] [blame] | 155 | std::vector<llvm::Regex *> KeptSections; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 158 | extern ScriptConfiguration *ScriptConfig; |
| 159 | |
| 160 | // This is a runner of the linker script. |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 161 | template <class ELFT> class LinkerScript final : public LinkerScriptBase { |
| Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 162 | typedef typename ELFT::uint uintX_t; |
| 163 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 164 | public: |
| Rui Ueyama | f34d0e0 | 2016-08-12 01:24:53 +0000 | [diff] [blame] | 165 | LinkerScript(); |
| 166 | ~LinkerScript(); |
| Eugene Leviant | 20d0319 | 2016-09-16 15:30:47 +0000 | [diff] [blame] | 167 | void processCommands(OutputSectionFactory<ELFT> &Factory); |
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 168 | void createSections(OutputSectionFactory<ELFT> &Factory); |
| Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 169 | |
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 170 | std::vector<PhdrEntry<ELFT>> createPhdrs(); |
| Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 171 | bool ignoreInterpSection(); |
| Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 172 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 173 | ArrayRef<uint8_t> getFiller(StringRef Name); |
| George Rimar | 8ceadb3 | 2016-08-17 07:44:19 +0000 | [diff] [blame] | 174 | Expr getLma(StringRef Name); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 175 | bool shouldKeep(InputSectionBase<ELFT> *S); |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 176 | void assignOffsets(OutputSectionCommand *Cmd); |
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 177 | void assignAddresses(); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 178 | int compareSections(StringRef A, StringRef B); |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 179 | bool hasPhdrsCommands(); |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 180 | uint64_t getOutputSectionAddress(StringRef Name) override; |
| 181 | uint64_t getOutputSectionSize(StringRef Name) override; |
| Eugene Leviant | 36fac7f | 2016-09-08 09:08:30 +0000 | [diff] [blame] | 182 | uint64_t getOutputSectionAlign(StringRef Name) override; |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 183 | uint64_t getHeaderSize() override; |
| 184 | uint64_t getSymbolValue(StringRef S) override; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 185 | |
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 186 | std::vector<OutputSectionBase<ELFT> *> *OutputSections; |
| 187 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 188 | private: |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 189 | void computeInputSections(InputSectionDescription *, |
| 190 | ConstraintKind Constraint); |
| Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 191 | |
| Eugene Leviant | 20d0319 | 2016-09-16 15:30:47 +0000 | [diff] [blame] | 192 | void addSection(OutputSectionFactory<ELFT> &Factory, |
| 193 | InputSectionBase<ELFT> *Sec, StringRef Name); |
| Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 194 | void discard(ArrayRef<InputSectionBase<ELFT> *> V); |
| Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 195 | |
| Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 196 | std::vector<InputSectionBase<ELFT> *> |
| 197 | createInputSectionList(OutputSectionCommand &Cmd); |
| 198 | |
| Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 199 | // "ScriptConfig" is a bit too long, so define a short name for it. |
| 200 | ScriptConfiguration &Opt = *ScriptConfig; |
| 201 | |
| Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 202 | int getSectionIndex(StringRef Name); |
| Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 203 | std::vector<size_t> getPhdrIndices(StringRef SectionName); |
| Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 204 | size_t getPhdrIndex(StringRef PhdrName); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 205 | |
| Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 206 | uintX_t Dot; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 207 | OutputSectionBase<ELFT> *CurOutSec = nullptr; |
| 208 | uintX_t ThreadBssOffset = 0; |
| 209 | void switchTo(OutputSectionBase<ELFT> *Sec); |
| 210 | void flush(); |
| 211 | void output(InputSection<ELFT> *Sec); |
| 212 | void process(BaseCommand &Base); |
| 213 | llvm::DenseSet<OutputSectionBase<ELFT> *> AlreadyOutputOS; |
| 214 | llvm::DenseSet<InputSectionData *> AlreadyOutputIS; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | // Variable template is a C++14 feature, so we can't template |
| 218 | // a global variable. Use a struct to workaround. |
| 219 | template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; |
| 220 | template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 221 | |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 222 | extern LinkerScriptBase *ScriptBase; |
| 223 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 224 | } // namespace elf |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 225 | } // namespace lld |
| 226 | |
| 227 | #endif |