| 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" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 22 | #include <cstddef> |
| 23 | #include <cstdint> |
| Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 24 | #include <functional> |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <vector> |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 27 | |
| 28 | namespace lld { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 29 | namespace elf { |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 30 | |
| Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 31 | class DefinedCommon; |
| George Rimar | dbb76db | 2016-08-18 13:00:49 +0000 | [diff] [blame] | 32 | class ScriptParser; |
| Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 33 | class SymbolBody; |
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 34 | template <class ELFT> class InputSectionBase; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 35 | template <class ELFT> class InputSection; |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 36 | class OutputSectionBase; |
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 37 | template <class ELFT> class OutputSectionFactory; |
| Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 38 | class InputSectionData; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 39 | |
| Rui Ueyama | b04af13 | 2016-10-13 23:08:33 +0000 | [diff] [blame] | 40 | // This represents an expression in the linker script. |
| 41 | // ScriptParser::readExpr reads an expression and returns an Expr. |
| 42 | // Later, we evaluate the expression by calling the function |
| 43 | // with the value of special context variable ".". |
| Rafael Espindola | f661393 | 2016-10-31 17:43:38 +0000 | [diff] [blame] | 44 | struct Expr { |
| 45 | std::function<uint64_t(uint64_t)> Val; |
| Rafael Espindola | 2f831dc | 2016-10-31 19:56:37 +0000 | [diff] [blame] | 46 | std::function<bool()> IsAbsolute; |
| Eugene Leviant | afaa934 | 2016-11-16 09:49:39 +0000 | [diff] [blame] | 47 | |
| 48 | // If expression is section-relative the function below is used |
| 49 | // to get the output section pointer. |
| 50 | std::function<const OutputSectionBase *()> Section; |
| 51 | |
| Rafael Espindola | f661393 | 2016-10-31 17:43:38 +0000 | [diff] [blame] | 52 | uint64_t operator()(uint64_t Dot) const { return Val(Dot); } |
| 53 | operator bool() const { return (bool)Val; } |
| 54 | |
| Eugene Leviant | afaa934 | 2016-11-16 09:49:39 +0000 | [diff] [blame] | 55 | Expr(std::function<uint64_t(uint64_t)> Val, std::function<bool()> IsAbsolute, |
| 56 | std::function<const OutputSectionBase *()> Section) |
| 57 | : Val(Val), IsAbsolute(IsAbsolute), Section(Section) {} |
| 58 | template <typename T> |
| Rui Ueyama | 009d174 | 2016-11-18 06:49:09 +0000 | [diff] [blame] | 59 | Expr(T V) : Expr(V, [] { return true; }, [] { return nullptr; }) {} |
| Rafael Espindola | f661393 | 2016-10-31 17:43:38 +0000 | [diff] [blame] | 60 | Expr() : Expr(nullptr) {} |
| 61 | }; |
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 62 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 63 | // Parses a linker script. Calling this function updates |
| 64 | // Config and ScriptConfig. |
| 65 | void readLinkerScript(MemoryBufferRef MB); |
| 66 | |
| Rui Ueyama | b04af13 | 2016-10-13 23:08:33 +0000 | [diff] [blame] | 67 | // Parses a version script. |
| George Rimar | 20b6598 | 2016-08-31 09:08:26 +0000 | [diff] [blame] | 68 | void readVersionScript(MemoryBufferRef MB); |
| 69 | |
| Rafael Espindola | d0ebd84 | 2016-12-08 17:54:26 +0000 | [diff] [blame] | 70 | void readDynamicList(MemoryBufferRef MB); |
| 71 | |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 72 | // This enum is used to implement linker script SECTIONS command. |
| 73 | // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS |
| 74 | enum SectionsCommandKind { |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 75 | AssignmentKind, // . = expr or <sym> = expr |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 76 | OutputSectionKind, |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 77 | InputSectionKind, |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 78 | AssertKind, // ASSERT(expr) |
| 79 | BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr) |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 82 | struct BaseCommand { |
| 83 | BaseCommand(int K) : Kind(K) {} |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 84 | |
| 85 | virtual ~BaseCommand() = default; |
| 86 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 87 | int Kind; |
| 88 | }; |
| 89 | |
| Rui Ueyama | b04af13 | 2016-10-13 23:08:33 +0000 | [diff] [blame] | 90 | // This represents ". = <expr>" or "<symbol> = <expr>". |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 91 | struct SymbolAssignment : BaseCommand { |
| Rafael Espindola | f661393 | 2016-10-31 17:43:38 +0000 | [diff] [blame] | 92 | SymbolAssignment(StringRef Name, Expr E) |
| 93 | : BaseCommand(AssignmentKind), Name(Name), Expression(E) {} |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 94 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 95 | static bool classof(const BaseCommand *C); |
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 96 | |
| 97 | // The LHS of an expression. Name is either a symbol name or ".". |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 98 | StringRef Name; |
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 99 | SymbolBody *Sym = nullptr; |
| 100 | |
| 101 | // The RHS of an expression. |
| Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 102 | Expr Expression; |
| Rui Ueyama | 2020424 | 2016-07-29 05:52:33 +0000 | [diff] [blame] | 103 | |
| 104 | // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN. |
| Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 105 | bool Provide = false; |
| Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 106 | bool Hidden = false; |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 109 | // Linker scripts allow additional constraints to be put on ouput sections. |
| Rui Ueyama | b04af13 | 2016-10-13 23:08:33 +0000 | [diff] [blame] | 110 | // If an output section is marked as ONLY_IF_RO, the section is created |
| 111 | // only if its input sections are read-only. Likewise, an output section |
| 112 | // with ONLY_IF_RW is created if all input sections are RW. |
| Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 113 | enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite }; |
| Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 114 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 115 | struct OutputSectionCommand : BaseCommand { |
| 116 | OutputSectionCommand(StringRef Name) |
| 117 | : BaseCommand(OutputSectionKind), Name(Name) {} |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 118 | |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 119 | static bool classof(const BaseCommand *C); |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 120 | |
| Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 121 | StringRef Name; |
| George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 122 | Expr AddrExpr; |
| George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 123 | Expr AlignExpr; |
| Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 124 | Expr LMAExpr; |
| George Rimar | db24d9c | 2016-08-19 15:18:23 +0000 | [diff] [blame] | 125 | Expr SubalignExpr; |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 126 | std::vector<std::unique_ptr<BaseCommand>> Commands; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 127 | std::vector<StringRef> Phdrs; |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 128 | uint32_t Filler = 0; |
| Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 129 | ConstraintKind Constraint = ConstraintKind::NoConstraint; |
| Eugene Leviant | 2a942c4 | 2016-12-05 16:38:32 +0000 | [diff] [blame] | 130 | std::string Location; |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 131 | std::string MemoryRegionName; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| George Rimar | 8034d49 | 2016-09-17 07:31:49 +0000 | [diff] [blame] | 134 | // This struct represents one section match pattern in SECTIONS() command. |
| Rui Ueyama | 4dc07be | 2016-09-17 02:23:40 +0000 | [diff] [blame] | 135 | // It can optionally have negative match pattern for EXCLUDED_FILE command. |
| George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 136 | // Also it may be surrounded with SORT() command, so contains sorting rules. |
| Rui Ueyama | 4dc07be | 2016-09-17 02:23:40 +0000 | [diff] [blame] | 137 | struct SectionPattern { |
| Rui Ueyama | f91282e | 2016-11-03 17:57:38 +0000 | [diff] [blame] | 138 | SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2) |
| 139 | : ExcludedFilePat(Pat1), SectionPat(Pat2) {} |
| Rui Ueyama | d1d7cfc | 2016-09-20 00:02:06 +0000 | [diff] [blame] | 140 | |
| Rui Ueyama | f91282e | 2016-11-03 17:57:38 +0000 | [diff] [blame] | 141 | StringMatcher ExcludedFilePat; |
| 142 | StringMatcher SectionPat; |
| George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 143 | SortSectionPolicy SortOuter; |
| 144 | SortSectionPolicy SortInner; |
| Rui Ueyama | 4dc07be | 2016-09-17 02:23:40 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 147 | struct InputSectionDescription : BaseCommand { |
| George Rimar | c91930a | 2016-09-02 21:17:20 +0000 | [diff] [blame] | 148 | InputSectionDescription(StringRef FilePattern) |
| Vitaly Buka | 0b7de06 | 2016-12-21 02:27:14 +0000 | [diff] [blame] | 149 | : BaseCommand(InputSectionKind), FilePat(FilePattern) {} |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 150 | |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 151 | static bool classof(const BaseCommand *C); |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 152 | |
| Rui Ueyama | f91282e | 2016-11-03 17:57:38 +0000 | [diff] [blame] | 153 | StringMatcher FilePat; |
| Rui Ueyama | 4dc07be | 2016-09-17 02:23:40 +0000 | [diff] [blame] | 154 | |
| George Rimar | 8034d49 | 2016-09-17 07:31:49 +0000 | [diff] [blame] | 155 | // Input sections that matches at least one of SectionPatterns |
| Rui Ueyama | 70efa2f | 2016-09-17 02:34:50 +0000 | [diff] [blame] | 156 | // will be associated with this InputSectionDescription. |
| Rui Ueyama | d1d7cfc | 2016-09-20 00:02:06 +0000 | [diff] [blame] | 157 | std::vector<SectionPattern> SectionPatterns; |
| Rui Ueyama | 4dc07be | 2016-09-17 02:23:40 +0000 | [diff] [blame] | 158 | |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 159 | std::vector<InputSectionData *> Sections; |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| Rui Ueyama | b04af13 | 2016-10-13 23:08:33 +0000 | [diff] [blame] | 162 | // Represents an ASSERT(). |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 163 | struct AssertCommand : BaseCommand { |
| 164 | AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {} |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 165 | |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 166 | static bool classof(const BaseCommand *C); |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 167 | |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 168 | Expr Expression; |
| 169 | }; |
| 170 | |
| Rui Ueyama | b04af13 | 2016-10-13 23:08:33 +0000 | [diff] [blame] | 171 | // Represents BYTE(), SHORT(), LONG(), or QUAD(). |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 172 | struct BytesDataCommand : BaseCommand { |
| Meador Inge | 95c7d8d | 2016-12-08 23:21:30 +0000 | [diff] [blame] | 173 | BytesDataCommand(Expr E, unsigned Size) |
| 174 | : BaseCommand(BytesDataKind), Expression(E), Size(Size) {} |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 175 | |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 176 | static bool classof(const BaseCommand *C); |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 177 | |
| Meador Inge | 95c7d8d | 2016-12-08 23:21:30 +0000 | [diff] [blame] | 178 | Expr Expression; |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 179 | unsigned Offset; |
| 180 | unsigned Size; |
| 181 | }; |
| 182 | |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 183 | struct PhdrsCommand { |
| 184 | StringRef Name; |
| 185 | unsigned Type; |
| 186 | bool HasFilehdr; |
| 187 | bool HasPhdrs; |
| Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 188 | unsigned Flags; |
| Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 189 | Expr LMAExpr; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 192 | // This struct is used to represent the location and size of regions of |
| 193 | // target memory. Instances of the struct are created by parsing the |
| 194 | // MEMORY command. |
| 195 | struct MemoryRegion { |
| 196 | std::string Name; |
| 197 | uint64_t Origin; |
| 198 | uint64_t Length; |
| 199 | uint64_t Offset; |
| 200 | uint32_t Flags; |
| Rui Ueyama | 8a8a953 | 2017-01-26 02:58:59 +0000 | [diff] [blame] | 201 | uint32_t NegFlags; |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 204 | class LinkerScriptBase { |
| Rafael Espindola | 4d1e4d7 | 2016-09-08 14:11:08 +0000 | [diff] [blame] | 205 | protected: |
| 206 | ~LinkerScriptBase() = default; |
| 207 | |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 208 | public: |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 209 | virtual uint64_t getHeaderSize() = 0; |
| Eugene Leviant | f6aeed3 | 2016-12-22 13:13:12 +0000 | [diff] [blame] | 210 | virtual uint64_t getSymbolValue(const Twine &Loc, StringRef S) = 0; |
| George Rimar | f34f45f | 2016-09-23 13:17:23 +0000 | [diff] [blame] | 211 | virtual bool isDefined(StringRef S) = 0; |
| Rafael Espindola | 2f831dc | 2016-10-31 19:56:37 +0000 | [diff] [blame] | 212 | virtual bool isAbsolute(StringRef S) = 0; |
| Eugene Leviant | afaa934 | 2016-11-16 09:49:39 +0000 | [diff] [blame] | 213 | virtual const OutputSectionBase *getSymbolSection(StringRef S) = 0; |
| Eugene Leviant | ed30ce7 | 2016-11-28 09:58:04 +0000 | [diff] [blame] | 214 | virtual const OutputSectionBase *getOutputSection(const Twine &Loc, |
| 215 | StringRef S) = 0; |
| Rui Ueyama | edf75e7 | 2016-11-17 20:27:10 +0000 | [diff] [blame] | 216 | virtual uint64_t getOutputSectionSize(StringRef S) = 0; |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 219 | // ScriptConfiguration holds linker script parse results. |
| 220 | struct ScriptConfiguration { |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 221 | // Used to assign addresses to sections. |
| George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 222 | std::vector<std::unique_ptr<BaseCommand>> Commands; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 223 | |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 224 | // Used to assign sections to headers. |
| George Rimar | 70ce0a9 | 2016-07-20 15:09:10 +0000 | [diff] [blame] | 225 | std::vector<PhdrsCommand> PhdrsCommands; |
| 226 | |
| Eugene Leviant | e05336ff | 2016-09-14 08:32:36 +0000 | [diff] [blame] | 227 | bool HasSections = false; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 228 | |
| Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 229 | // List of section patterns specified with KEEP commands. They will |
| 230 | // be kept even if they are unused and --gc-sections is specified. |
| Eugene Leviant | cf43f17 | 2016-10-05 09:36:59 +0000 | [diff] [blame] | 231 | std::vector<InputSectionDescription *> KeptSections; |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 232 | |
| 233 | // A map from memory region name to a memory region descriptor. |
| 234 | llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 237 | extern ScriptConfiguration *ScriptConfig; |
| 238 | |
| 239 | // This is a runner of the linker script. |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 240 | template <class ELFT> class LinkerScript final : public LinkerScriptBase { |
| Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 241 | typedef typename ELFT::uint uintX_t; |
| 242 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 243 | public: |
| Rui Ueyama | f34d0e0 | 2016-08-12 01:24:53 +0000 | [diff] [blame] | 244 | LinkerScript(); |
| 245 | ~LinkerScript(); |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 246 | |
| Eugene Leviant | 20d0319 | 2016-09-16 15:30:47 +0000 | [diff] [blame] | 247 | void processCommands(OutputSectionFactory<ELFT> &Factory); |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 248 | void addOrphanSections(OutputSectionFactory<ELFT> &Factory); |
| Rafael Espindola | 07fe612 | 2016-11-14 14:23:35 +0000 | [diff] [blame] | 249 | void removeEmptyCommands(); |
| Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 250 | void adjustSectionsBeforeSorting(); |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 251 | void adjustSectionsAfterSorting(); |
| Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 252 | |
| Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 253 | std::vector<PhdrEntry> createPhdrs(); |
| Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 254 | bool ignoreInterpSection(); |
| Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 255 | |
| Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 256 | uint32_t getFiller(StringRef Name); |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 257 | void writeDataBytes(StringRef Name, uint8_t *Buf); |
| Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 258 | bool hasLMA(StringRef Name); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 259 | bool shouldKeep(InputSectionBase<ELFT> *S); |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 260 | void assignOffsets(OutputSectionCommand *Cmd); |
| Rafael Espindola | 337f903 | 2016-11-14 14:13:32 +0000 | [diff] [blame] | 261 | void placeOrphanSections(); |
| Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 262 | void assignAddresses(std::vector<PhdrEntry> &Phdrs); |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 263 | bool hasPhdrsCommands(); |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 264 | uint64_t getHeaderSize() override; |
| Eugene Leviant | f6aeed3 | 2016-12-22 13:13:12 +0000 | [diff] [blame] | 265 | uint64_t getSymbolValue(const Twine &Loc, StringRef S) override; |
| George Rimar | f34f45f | 2016-09-23 13:17:23 +0000 | [diff] [blame] | 266 | bool isDefined(StringRef S) override; |
| Rafael Espindola | 2f831dc | 2016-10-31 19:56:37 +0000 | [diff] [blame] | 267 | bool isAbsolute(StringRef S) override; |
| Eugene Leviant | afaa934 | 2016-11-16 09:49:39 +0000 | [diff] [blame] | 268 | const OutputSectionBase *getSymbolSection(StringRef S) override; |
| Eugene Leviant | ed30ce7 | 2016-11-28 09:58:04 +0000 | [diff] [blame] | 269 | const OutputSectionBase *getOutputSection(const Twine &Loc, |
| 270 | StringRef S) override; |
| Rui Ueyama | edf75e7 | 2016-11-17 20:27:10 +0000 | [diff] [blame] | 271 | uint64_t getOutputSectionSize(StringRef S) override; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 272 | |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 273 | std::vector<OutputSectionBase *> *OutputSections; |
| Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 274 | |
| Rafael Espindola | b6b8f6c | 2016-09-20 22:43:15 +0000 | [diff] [blame] | 275 | int getSectionIndex(StringRef Name); |
| 276 | |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 277 | private: |
| Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 278 | void assignSymbol(SymbolAssignment *Cmd, bool InSec = false); |
| 279 | void addSymbol(SymbolAssignment *Cmd); |
| Rafael Espindola | e71a3f8a | 2016-09-16 20:34:02 +0000 | [diff] [blame] | 280 | void computeInputSections(InputSectionDescription *); |
| Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 281 | void setDot(Expr E, bool InSec = false); |
| Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 282 | |
| Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 283 | void discard(ArrayRef<InputSectionBase<ELFT> *> V); |
| Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 284 | |
| Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 285 | std::vector<InputSectionBase<ELFT> *> |
| 286 | createInputSectionList(OutputSectionCommand &Cmd); |
| 287 | |
| Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 288 | // "ScriptConfig" is a bit too long, so define a short name for it. |
| 289 | ScriptConfiguration &Opt = *ScriptConfig; |
| 290 | |
| Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 291 | std::vector<size_t> getPhdrIndices(StringRef SectionName); |
| Eugene Leviant | 2a942c4 | 2016-12-05 16:38:32 +0000 | [diff] [blame] | 292 | size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName); |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 293 | |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 294 | MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd, |
| 295 | OutputSectionBase *Sec); |
| 296 | |
| Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 297 | uintX_t Dot; |
| Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 298 | uintX_t LMAOffset = 0; |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 299 | OutputSectionBase *CurOutSec = nullptr; |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 300 | MemoryRegion *CurMemRegion = nullptr; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 301 | uintX_t ThreadBssOffset = 0; |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 302 | void switchTo(OutputSectionBase *Sec); |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 303 | void flush(); |
| 304 | void output(InputSection<ELFT> *Sec); |
| 305 | void process(BaseCommand &Base); |
| Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 306 | llvm::DenseSet<OutputSectionBase *> AlreadyOutputOS; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 307 | llvm::DenseSet<InputSectionData *> AlreadyOutputIS; |
| Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | // Variable template is a C++14 feature, so we can't template |
| 311 | // a global variable. Use a struct to workaround. |
| 312 | template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; |
| 313 | template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 314 | |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 315 | extern LinkerScriptBase *ScriptBase; |
| 316 | |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 317 | } // end namespace elf |
| 318 | } // end namespace lld |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 319 | |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 320 | #endif // LLD_ELF_LINKER_SCRIPT_H |