Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 1 | //===- LinkerScript.cpp ---------------------------------------------------===// |
| 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. |
Rui Ueyama | 629e0aa5 | 2016-07-21 19:45:22 +0000 | [diff] [blame] | 11 | // It parses a linker script and write the result to Config or ScriptConfig |
| 12 | // objects. |
| 13 | // |
| 14 | // If SECTIONS command is used, a ScriptConfig contains an AST |
| 15 | // of the command which will later be consumed by createSections() and |
| 16 | // assignAddresses(). |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 20 | #include "LinkerScript.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 21 | #include "Config.h" |
| 22 | #include "Driver.h" |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 23 | #include "InputSection.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 24 | #include "OutputSections.h" |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 25 | #include "ScriptParser.h" |
Rui Ueyama | 93c9af4 | 2016-06-29 08:01:32 +0000 | [diff] [blame] | 26 | #include "Strings.h" |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 27 | #include "Symbols.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 28 | #include "SymbolTable.h" |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 29 | #include "Target.h" |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 30 | #include "Writer.h" |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringSwitch.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ELF.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 33 | #include "llvm/Support/FileSystem.h" |
| 34 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Path.h" |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 36 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 39 | using namespace llvm::ELF; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 40 | using namespace llvm::object; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 41 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 42 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 43 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 44 | ScriptConfiguration *elf::ScriptConfig; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 45 | |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 46 | template <class ELFT> |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 47 | static void addRegular(SymbolAssignment *Cmd) { |
| 48 | Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT); |
| 49 | Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; |
| 50 | Cmd->Sym = Sym->body(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | template <class ELFT> |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 54 | static void addSynthetic(SymbolAssignment *Cmd, |
| 55 | OutputSectionBase<ELFT> *Section) { |
| 56 | Symbol *Sym = Symtab<ELFT>::X->addSynthetic(Cmd->Name, Section, 0); |
| 57 | Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; |
| 58 | Cmd->Sym = Sym->body(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 61 | // If a symbol was in PROVIDE(), we need to define it only when |
| 62 | // it is an undefined symbol. |
| 63 | template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) { |
| 64 | if (Cmd->Name == ".") |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 65 | return false; |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 66 | if (!Cmd->Provide) |
| 67 | return true; |
| 68 | SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); |
| 69 | return B && B->isUndefined(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 72 | bool SymbolAssignment::classof(const BaseCommand *C) { |
| 73 | return C->Kind == AssignmentKind; |
| 74 | } |
| 75 | |
| 76 | bool OutputSectionCommand::classof(const BaseCommand *C) { |
| 77 | return C->Kind == OutputSectionKind; |
| 78 | } |
| 79 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 80 | bool InputSectionDescription::classof(const BaseCommand *C) { |
| 81 | return C->Kind == InputSectionKind; |
| 82 | } |
| 83 | |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 84 | bool AssertCommand::classof(const BaseCommand *C) { |
| 85 | return C->Kind == AssertKind; |
| 86 | } |
| 87 | |
Rui Ueyama | 36a153c | 2016-07-23 14:09:58 +0000 | [diff] [blame] | 88 | template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 89 | return !S || !S->Live; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 92 | template <class ELFT> |
| 93 | bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 94 | for (StringRef Pat : Opt.KeptSections) |
Rui Ueyama | 722830a | 2016-06-29 05:32:09 +0000 | [diff] [blame] | 95 | if (globMatch(Pat, S->getSectionName())) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 96 | return true; |
| 97 | return false; |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Rui Ueyama | 63dc650 | 2016-07-25 22:41:42 +0000 | [diff] [blame] | 100 | static bool match(ArrayRef<StringRef> Patterns, StringRef S) { |
| 101 | for (StringRef Pat : Patterns) |
| 102 | if (globMatch(Pat, S)) |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 103 | return true; |
| 104 | return false; |
| 105 | } |
| 106 | |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 107 | static bool fileMatches(const InputSectionDescription *Desc, |
| 108 | StringRef Filename) { |
| 109 | if (!globMatch(Desc->FilePattern, Filename)) |
| 110 | return false; |
| 111 | return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename); |
| 112 | } |
| 113 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 114 | // Returns input sections filtered by given glob patterns. |
| 115 | template <class ELFT> |
| 116 | std::vector<InputSectionBase<ELFT> *> |
Rui Ueyama | ad10c3d | 2016-07-28 21:05:04 +0000 | [diff] [blame] | 117 | LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) { |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 118 | ArrayRef<StringRef> Patterns = I->SectionPatterns; |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 119 | std::vector<InputSectionBase<ELFT> *> Ret; |
| 120 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 121 | Symtab<ELFT>::X->getObjectFiles()) { |
| 122 | if (fileMatches(I, sys::path::filename(F->getName()))) |
| 123 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 124 | if (!isDiscarded(S) && !S->OutSec && |
| 125 | match(Patterns, S->getSectionName())) |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 126 | Ret.push_back(S); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 127 | } |
Eugene Leviant | 3e6b027 | 2016-07-28 19:24:13 +0000 | [diff] [blame] | 128 | |
| 129 | if ((llvm::find(Patterns, "COMMON") != Patterns.end())) |
Rui Ueyama | ad10c3d | 2016-07-28 21:05:04 +0000 | [diff] [blame] | 130 | Ret.push_back(CommonInputSection<ELFT>::X); |
Eugene Leviant | 3e6b027 | 2016-07-28 19:24:13 +0000 | [diff] [blame] | 131 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 132 | return Ret; |
| 133 | } |
| 134 | |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 135 | namespace { |
Rui Ueyama | dd81fe3 | 2016-08-11 21:00:02 +0000 | [diff] [blame] | 136 | |
| 137 | // You can define new symbols using linker scripts. For example, |
| 138 | // ".text { abc.o(.text); foo = .; def.o(.text); }" defines symbol |
| 139 | // foo just after abc.o's text section contents. This class is to |
| 140 | // handle such symbol definitions. |
| 141 | // |
| 142 | // In order to handle scripts like the above one, we want to |
| 143 | // keep symbol definitions in output sections. Because output sections |
| 144 | // can contain only input sections, we wrap symbol definitions |
| 145 | // with dummy input sections. This class serves that purpose. |
Rui Ueyama | 2c3f501 | 2016-08-11 22:06:55 +0000 | [diff] [blame] | 146 | template <class ELFT> class LayoutInputSection : public InputSectionBase<ELFT> { |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 147 | public: |
| 148 | LayoutInputSection(SymbolAssignment *Cmd); |
| 149 | static bool classof(const InputSectionBase<ELFT> *S); |
| 150 | SymbolAssignment *Cmd; |
| 151 | |
| 152 | private: |
| 153 | typename ELFT::Shdr Hdr; |
| 154 | }; |
| 155 | |
| 156 | // Helper class, which builds output section list, also |
| 157 | // creating symbol sections, when needed |
| 158 | template <class ELFT> class OutputSectionBuilder { |
| 159 | public: |
| 160 | OutputSectionBuilder(OutputSectionFactory<ELFT> &F, |
| 161 | std::vector<OutputSectionBase<ELFT> *> *Out) |
| 162 | : Factory(F), OutputSections(Out) {} |
| 163 | |
| 164 | void addSection(StringRef OutputName, InputSectionBase<ELFT> *I); |
| 165 | void addSymbol(SymbolAssignment *Cmd) { |
| 166 | PendingSymbols.emplace_back(new LayoutInputSection<ELFT>(Cmd)); |
| 167 | } |
| 168 | void flushSymbols(); |
| 169 | void flushSection(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 170 | |
| 171 | private: |
| 172 | OutputSectionFactory<ELFT> &Factory; |
| 173 | std::vector<OutputSectionBase<ELFT> *> *OutputSections; |
| 174 | OutputSectionBase<ELFT> *Current = nullptr; |
| 175 | std::vector<std::unique_ptr<LayoutInputSection<ELFT>>> PendingSymbols; |
| 176 | static std::vector<std::unique_ptr<LayoutInputSection<ELFT>>> OwningSections; |
| 177 | }; |
| 178 | |
George Rimar | c3cb884 | 2016-07-29 15:12:48 +0000 | [diff] [blame] | 179 | template <class ELFT> |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 180 | std::vector<std::unique_ptr<LayoutInputSection<ELFT>>> |
| 181 | OutputSectionBuilder<ELFT>::OwningSections; |
Rui Ueyama | dd81fe3 | 2016-08-11 21:00:02 +0000 | [diff] [blame] | 182 | |
| 183 | } // anonymous namespace |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 184 | |
| 185 | template <class T> static T *zero(T *Val) { |
| 186 | memset(Val, 0, sizeof(*Val)); |
| 187 | return Val; |
| 188 | } |
| 189 | |
| 190 | template <class ELFT> |
| 191 | LayoutInputSection<ELFT>::LayoutInputSection(SymbolAssignment *Cmd) |
Rui Ueyama | 2c3f501 | 2016-08-11 22:06:55 +0000 | [diff] [blame] | 192 | : InputSectionBase<ELFT>(nullptr, zero(&Hdr), |
| 193 | InputSectionBase<ELFT>::Layout), |
| 194 | Cmd(Cmd) { |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 195 | this->Live = true; |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 196 | Hdr.sh_type = SHT_NOBITS; |
| 197 | } |
| 198 | |
| 199 | template <class ELFT> |
| 200 | bool LayoutInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { |
| 201 | return S->SectionKind == InputSectionBase<ELFT>::Layout; |
| 202 | } |
| 203 | |
| 204 | template <class ELFT> |
| 205 | void OutputSectionBuilder<ELFT>::addSection(StringRef OutputName, |
| 206 | InputSectionBase<ELFT> *C) { |
George Rimar | c3cb884 | 2016-07-29 15:12:48 +0000 | [diff] [blame] | 207 | bool IsNew; |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 208 | std::tie(Current, IsNew) = Factory.create(C, OutputName); |
George Rimar | c3cb884 | 2016-07-29 15:12:48 +0000 | [diff] [blame] | 209 | if (IsNew) |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 210 | OutputSections->push_back(Current); |
| 211 | flushSymbols(); |
| 212 | Current->addSection(C); |
| 213 | } |
| 214 | |
| 215 | template <class ELFT> void OutputSectionBuilder<ELFT>::flushSymbols() { |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 216 | // Only regular output sections are supported. |
| 217 | if (dyn_cast_or_null<OutputSection<ELFT>>(Current)) { |
| 218 | for (std::unique_ptr<LayoutInputSection<ELFT>> &I : PendingSymbols) { |
| 219 | if (I->Cmd->Name == ".") { |
| 220 | Current->addSection(I.get()); |
| 221 | OwningSections.push_back(std::move(I)); |
| 222 | } else if (shouldDefine<ELFT>(I->Cmd)) { |
| 223 | addSynthetic<ELFT>(I->Cmd, Current); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 224 | Current->addSection(I.get()); |
| 225 | OwningSections.push_back(std::move(I)); |
| 226 | } |
| 227 | } |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 228 | } |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 229 | |
| 230 | PendingSymbols.clear(); |
| 231 | } |
| 232 | |
| 233 | template <class ELFT> void OutputSectionBuilder<ELFT>::flushSection() { |
| 234 | flushSymbols(); |
| 235 | Current = nullptr; |
| 236 | } |
| 237 | |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 238 | template <class ELFT> |
| 239 | static bool compareName(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) { |
| 240 | return A->getSectionName() < B->getSectionName(); |
| 241 | } |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 242 | |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 243 | template <class ELFT> |
| 244 | static bool compareAlignment(InputSectionBase<ELFT> *A, |
| 245 | InputSectionBase<ELFT> *B) { |
| 246 | // ">" is not a mistake. Larger alignments are placed before smaller |
| 247 | // alignments in order to reduce the amount of padding necessary. |
| 248 | // This is compatible with GNU. |
| 249 | return A->Alignment > B->Alignment; |
| 250 | } |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 251 | |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 252 | template <class ELFT> |
| 253 | static std::function<bool(InputSectionBase<ELFT> *, InputSectionBase<ELFT> *)> |
| 254 | getComparator(SortKind K) { |
| 255 | if (K == SortByName) |
| 256 | return compareName<ELFT>; |
| 257 | return compareAlignment<ELFT>; |
| 258 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 259 | |
| 260 | template <class ELFT> |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 261 | void LinkerScript<ELFT>::discard(OutputSectionCommand &Cmd) { |
| 262 | for (const std::unique_ptr<BaseCommand> &Base : Cmd.Commands) { |
| 263 | if (auto *Cmd = dyn_cast<InputSectionDescription>(Base.get())) { |
| 264 | for (InputSectionBase<ELFT> *S : getInputSections(Cmd)) { |
| 265 | S->Live = false; |
| 266 | reportDiscarded(S); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | template <class ELFT> |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 273 | void LinkerScript<ELFT>::createSections( |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 274 | OutputSectionFactory<ELFT> &Factory) { |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 275 | OutputSectionBuilder<ELFT> Builder(Factory, OutputSections); |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 276 | |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 277 | for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) { |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 278 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) { |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 279 | if (Cmd->Name == "/DISCARD/") { |
| 280 | discard(*Cmd); |
| 281 | continue; |
| 282 | } |
| 283 | for (const std::unique_ptr<BaseCommand> &Base2 : Cmd->Commands) { |
| 284 | if (auto *Cmd2 = dyn_cast<SymbolAssignment>(Base2.get())) { |
| 285 | Builder.addSymbol(Cmd2); |
| 286 | continue; |
| 287 | } |
| 288 | auto *Cmd2 = cast<InputSectionDescription>(Base2.get()); |
| 289 | std::vector<InputSectionBase<ELFT> *> Sections = getInputSections(Cmd2); |
| 290 | if (Cmd2->SortInner) |
| 291 | std::stable_sort(Sections.begin(), Sections.end(), |
| 292 | getComparator<ELFT>(Cmd2->SortInner)); |
| 293 | if (Cmd2->SortOuter) |
| 294 | std::stable_sort(Sections.begin(), Sections.end(), |
| 295 | getComparator<ELFT>(Cmd2->SortOuter)); |
| 296 | for (InputSectionBase<ELFT> *S : Sections) |
| 297 | Builder.addSection(Cmd->Name, S); |
| 298 | } |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 299 | |
| 300 | Builder.flushSection(); |
| 301 | } else if (auto *Cmd2 = dyn_cast<SymbolAssignment>(Base1.get())) { |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 302 | if (shouldDefine<ELFT>(Cmd2)) |
| 303 | addRegular<ELFT>(Cmd2); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 304 | } |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 305 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 306 | |
| 307 | // Add all other input sections, which are not listed in script. |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 308 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : |
| 309 | Symtab<ELFT>::X->getObjectFiles()) |
| 310 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 311 | if (!isDiscarded(S) && !S->OutSec) |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 312 | Builder.addSection(getOutputSectionName(S), S); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 313 | |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 314 | // Remove from the output all the sections which did not meet |
| 315 | // the optional constraints. |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 316 | filter(); |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Eugene Leviant | c7611fc | 2016-08-04 08:20:23 +0000 | [diff] [blame] | 319 | template <class R, class T> |
| 320 | static inline void removeElementsIf(R &Range, const T &Pred) { |
| 321 | Range.erase(std::remove_if(Range.begin(), Range.end(), Pred), Range.end()); |
| 322 | } |
| 323 | |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 324 | // Process ONLY_IF_RO and ONLY_IF_RW. |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 325 | template <class ELFT> void LinkerScript<ELFT>::filter() { |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 326 | // In this loop, we remove output sections if they don't satisfy |
| 327 | // requested properties. |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 328 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 329 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 330 | if (!Cmd || Cmd->Name == "/DISCARD/") |
| 331 | continue; |
| 332 | |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 333 | if (Cmd->Constraint == ConstraintKind::NoConstraint) |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 334 | continue; |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 335 | |
Rui Ueyama | 808d13e | 2016-08-05 01:05:01 +0000 | [diff] [blame] | 336 | bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly); |
| 337 | bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite); |
| 338 | |
Eugene Leviant | c7611fc | 2016-08-04 08:20:23 +0000 | [diff] [blame] | 339 | removeElementsIf(*OutputSections, [&](OutputSectionBase<ELFT> *S) { |
| 340 | bool Writable = (S->getFlags() & SHF_WRITE); |
Eugene Leviant | c7611fc | 2016-08-04 08:20:23 +0000 | [diff] [blame] | 341 | return S->getName() == Cmd->Name && |
| 342 | ((RO && Writable) || (RW && !Writable)); |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 343 | }); |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 344 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 347 | template <class ELFT> void assignOffsets(OutputSectionBase<ELFT> *Sec) { |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 348 | auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec); |
Rui Ueyama | 2de509c | 2016-08-12 00:55:08 +0000 | [diff] [blame^] | 349 | if (!OutSec) { |
| 350 | Sec->assignOffsets(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 351 | return; |
Rui Ueyama | 2de509c | 2016-08-12 00:55:08 +0000 | [diff] [blame^] | 352 | } |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 353 | |
| 354 | typedef typename ELFT::uint uintX_t; |
| 355 | uintX_t Off = 0; |
| 356 | |
| 357 | for (InputSection<ELFT> *I : OutSec->Sections) { |
| 358 | if (auto *L = dyn_cast<LayoutInputSection<ELFT>>(I)) { |
| 359 | uintX_t Value = L->Cmd->Expression(Sec->getVA() + Off) - Sec->getVA(); |
| 360 | if (L->Cmd->Name == ".") |
| 361 | Off = Value; |
| 362 | else |
| 363 | cast<DefinedSynthetic<ELFT>>(L->Cmd->Sym)->Value = Value; |
| 364 | } else { |
| 365 | Off = alignTo(Off, I->Alignment); |
| 366 | I->OutSecOff = Off; |
| 367 | Off += I->getSize(); |
| 368 | } |
Rui Ueyama | f4a30a5 | 2016-08-11 21:30:42 +0000 | [diff] [blame] | 369 | // Update section size inside for-loop, so that SIZEOF |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 370 | // works correctly in the case below: |
| 371 | // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } |
| 372 | Sec->setSize(Off); |
| 373 | } |
| 374 | } |
| 375 | |
Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 376 | template <class ELFT> void LinkerScript<ELFT>::assignAddresses() { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 377 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 378 | // are not explicitly placed into the output file by the linker script. |
| 379 | // We place orphan sections at end of file. |
| 380 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 381 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | e5cc668 | 2016-08-12 00:36:56 +0000 | [diff] [blame] | 382 | for (OutputSectionBase<ELFT> *Sec : *OutputSections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 383 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 384 | if (getSectionIndex(Name) == INT_MAX) |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 385 | Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 386 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 387 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 388 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
George Rimar | e32a359 | 2016-08-10 07:59:34 +0000 | [diff] [blame] | 389 | Dot = getSizeOfHeaders(); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 390 | uintX_t MinVA = std::numeric_limits<uintX_t>::max(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 391 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 392 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 393 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 394 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { |
Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 395 | if (Cmd->Name == ".") { |
| 396 | Dot = Cmd->Expression(Dot); |
| 397 | } else if (Cmd->Sym) { |
| 398 | cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot); |
| 399 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 403 | if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) { |
| 404 | Cmd->Expression(Dot); |
| 405 | continue; |
| 406 | } |
| 407 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 408 | // Find all the sections with required name. There can be more than |
George Rimar | 6ad330a | 2016-07-19 07:39:07 +0000 | [diff] [blame] | 409 | // one section with such name, if the alignment, flags or type |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 410 | // attribute differs. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 411 | auto *Cmd = cast<OutputSectionCommand>(Base.get()); |
Rui Ueyama | e5cc668 | 2016-08-12 00:36:56 +0000 | [diff] [blame] | 412 | for (OutputSectionBase<ELFT> *Sec : *OutputSections) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 413 | if (Sec->getName() != Cmd->Name) |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 414 | continue; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 415 | |
George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 416 | if (Cmd->AddrExpr) |
| 417 | Dot = Cmd->AddrExpr(Dot); |
| 418 | |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 419 | if (Cmd->AlignExpr) |
| 420 | Sec->updateAlignment(Cmd->AlignExpr(Dot)); |
| 421 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 422 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 423 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 424 | TVA = alignTo(TVA, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 425 | Sec->setVA(TVA); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 426 | assignOffsets(Sec); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 427 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
| 428 | continue; |
| 429 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 430 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 431 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 432 | Dot = alignTo(Dot, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 433 | Sec->setVA(Dot); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 434 | assignOffsets(Sec); |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 435 | MinVA = std::min(MinVA, Dot); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 436 | Dot += Sec->getSize(); |
| 437 | continue; |
| 438 | } |
Rui Ueyama | 2de509c | 2016-08-12 00:55:08 +0000 | [diff] [blame^] | 439 | Sec->assignOffsets(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 442 | |
Rafael Espindola | 64c32d6 | 2016-07-07 14:28:47 +0000 | [diff] [blame] | 443 | // ELF and Program headers need to be right before the first section in |
George Rimar | b91e711 | 2016-07-19 07:42:07 +0000 | [diff] [blame] | 444 | // memory. Set their addresses accordingly. |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 445 | MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - |
| 446 | Out<ELFT>::ProgramHeaders->getSize(), |
| 447 | Target->PageSize); |
| 448 | Out<ELFT>::ElfHeader->setVA(MinVA); |
| 449 | Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 452 | template <class ELFT> |
Rafael Espindola | a4b41dc | 2016-08-04 12:13:05 +0000 | [diff] [blame] | 453 | std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() { |
| 454 | ArrayRef<OutputSectionBase<ELFT> *> Sections = *OutputSections; |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 455 | std::vector<PhdrEntry<ELFT>> Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 456 | |
| 457 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 458 | Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
| 459 | PhdrEntry<ELFT> &Phdr = Ret.back(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 460 | |
| 461 | if (Cmd.HasFilehdr) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 462 | Phdr.add(Out<ELFT>::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 463 | if (Cmd.HasPhdrs) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 464 | Phdr.add(Out<ELFT>::ProgramHeaders); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 465 | |
| 466 | switch (Cmd.Type) { |
| 467 | case PT_INTERP: |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 468 | if (Out<ELFT>::Interp) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 469 | Phdr.add(Out<ELFT>::Interp); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 470 | break; |
| 471 | case PT_DYNAMIC: |
Rui Ueyama | 1034c9e | 2016-08-09 04:42:01 +0000 | [diff] [blame] | 472 | if (Out<ELFT>::DynSymTab) { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 473 | Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags(); |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 474 | Phdr.add(Out<ELFT>::Dynamic); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 475 | } |
| 476 | break; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 477 | case PT_GNU_EH_FRAME: |
| 478 | if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 479 | Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags(); |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 480 | Phdr.add(Out<ELFT>::EhFrameHdr); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 481 | } |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 486 | PhdrEntry<ELFT> *Load = nullptr; |
| 487 | uintX_t Flags = PF_R; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 488 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
| 489 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 490 | break; |
| 491 | |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 492 | std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName()); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 493 | if (!PhdrIds.empty()) { |
| 494 | // Assign headers specified by linker script |
| 495 | for (size_t Id : PhdrIds) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 496 | Ret[Id].add(Sec); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 497 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 498 | Ret[Id].H.p_flags |= Sec->getPhdrFlags(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 499 | } |
| 500 | } else { |
| 501 | // If we have no load segment or flags've changed then we want new load |
| 502 | // segment. |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 503 | uintX_t NewFlags = Sec->getPhdrFlags(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 504 | if (Load == nullptr || Flags != NewFlags) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 505 | Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 506 | Flags = NewFlags; |
| 507 | } |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 508 | Load->add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 509 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 510 | } |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 511 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 515 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 516 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) |
| 517 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 518 | if (Cmd->Name == Name) |
| 519 | return Cmd->Filler; |
| 520 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 523 | // Returns the index of the given section name in linker script |
| 524 | // SECTIONS commands. Sections are laid out as the same order as they |
| 525 | // were in the script. If a given name did not appear in the script, |
| 526 | // it returns INT_MAX, so that it will be laid out at end of file. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 527 | template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
Rui Ueyama | f510fa6 | 2016-07-26 00:21:15 +0000 | [diff] [blame] | 528 | int I = 0; |
| 529 | for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 530 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 531 | if (Cmd->Name == Name) |
| 532 | return I; |
| 533 | ++I; |
| 534 | } |
| 535 | return INT_MAX; |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | // A compartor to sort output sections. Returns -1 or 1 if |
| 539 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 540 | template <class ELFT> |
| 541 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 542 | int I = getSectionIndex(A); |
| 543 | int J = getSectionIndex(B); |
| 544 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 545 | return 0; |
| 546 | return I < J ? -1 : 1; |
| 547 | } |
| 548 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 549 | template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { |
| 550 | return !Opt.PhdrsCommands.empty(); |
| 551 | } |
| 552 | |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 553 | template <class ELFT> |
| 554 | typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) { |
| 555 | for (OutputSectionBase<ELFT> *Sec : *OutputSections) |
| 556 | if (Sec->getName() == Name) |
| 557 | return Sec->getSize(); |
| 558 | error("undefined section " + Name); |
| 559 | return 0; |
| 560 | } |
| 561 | |
George Rimar | e32a359 | 2016-08-10 07:59:34 +0000 | [diff] [blame] | 562 | template <class ELFT> |
| 563 | typename ELFT::uint LinkerScript<ELFT>::getSizeOfHeaders() { |
| 564 | return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
| 565 | } |
| 566 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 567 | // Returns indices of ELF headers containing specific section, identified |
| 568 | // by Name. Each index is a zero based number of ELF header listed within |
| 569 | // PHDRS {} script block. |
| 570 | template <class ELFT> |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 571 | std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 572 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 573 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 574 | if (!Cmd || Cmd->Name != SectionName) |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 575 | continue; |
| 576 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 577 | std::vector<size_t> Ret; |
| 578 | for (StringRef PhdrName : Cmd->Phdrs) |
| 579 | Ret.push_back(getPhdrIndex(PhdrName)); |
| 580 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 581 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 582 | return {}; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 585 | template <class ELFT> |
| 586 | size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) { |
| 587 | size_t I = 0; |
| 588 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
| 589 | if (Cmd.Name == PhdrName) |
| 590 | return I; |
| 591 | ++I; |
| 592 | } |
| 593 | error("section header '" + PhdrName + "' is not listed in PHDRS"); |
| 594 | return 0; |
| 595 | } |
| 596 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 597 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 598 | typedef void (ScriptParser::*Handler)(); |
| 599 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 600 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 601 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 602 | |
Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 603 | void run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 604 | |
| 605 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 606 | void addFile(StringRef Path); |
| 607 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 608 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 609 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 610 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 611 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 612 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 613 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 614 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 615 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 616 | void readOutputFormat(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 617 | void readPhdrs(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 618 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 619 | void readSections(); |
| 620 | |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 621 | SymbolAssignment *readAssignment(StringRef Name); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 622 | OutputSectionCommand *readOutputSectionDescription(StringRef OutSec); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 623 | std::vector<uint8_t> readOutputSectionFiller(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 624 | std::vector<StringRef> readOutputSectionPhdrs(); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 625 | InputSectionDescription *readInputSectionDescription(); |
| 626 | std::vector<StringRef> readInputFilePatterns(); |
| 627 | InputSectionDescription *readInputSectionRules(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 628 | unsigned readPhdrType(); |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 629 | SortKind readSortKind(); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 630 | SymbolAssignment *readProvide(bool Hidden); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 631 | SymbolAssignment *readProvideOrAssignment(StringRef Tok); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 632 | Expr readAlign(); |
George Rimar | 03fc010 | 2016-07-28 07:18:23 +0000 | [diff] [blame] | 633 | void readSort(); |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 634 | Expr readAssert(); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 635 | |
| 636 | Expr readExpr(); |
| 637 | Expr readExpr1(Expr Lhs, int MinPrec); |
| 638 | Expr readPrimary(); |
| 639 | Expr readTernary(Expr Cond); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 640 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 641 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 642 | ScriptConfiguration &Opt = *ScriptConfig; |
| 643 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 644 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 645 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 646 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 647 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 648 | {"ENTRY", &ScriptParser::readEntry}, |
| 649 | {"EXTERN", &ScriptParser::readExtern}, |
| 650 | {"GROUP", &ScriptParser::readGroup}, |
| 651 | {"INCLUDE", &ScriptParser::readInclude}, |
| 652 | {"INPUT", &ScriptParser::readGroup}, |
| 653 | {"OUTPUT", &ScriptParser::readOutput}, |
| 654 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 655 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 656 | {"PHDRS", &ScriptParser::readPhdrs}, |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 657 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 658 | {"SECTIONS", &ScriptParser::readSections}, |
| 659 | {";", &ScriptParser::readNothing}}; |
| 660 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 661 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 662 | while (!atEOF()) { |
| 663 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 664 | if (Handler Fn = Cmd.lookup(Tok)) |
| 665 | (this->*Fn)(); |
| 666 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 667 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 671 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 672 | if (IsUnderSysroot && S.startswith("/")) { |
| 673 | SmallString<128> Path; |
| 674 | (Config->Sysroot + S).toStringRef(Path); |
| 675 | if (sys::fs::exists(Path)) { |
| 676 | Driver->addFile(Saver.save(Path.str())); |
| 677 | return; |
| 678 | } |
| 679 | } |
| 680 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 681 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 682 | Driver->addFile(S); |
| 683 | } else if (S.startswith("=")) { |
| 684 | if (Config->Sysroot.empty()) |
| 685 | Driver->addFile(S.substr(1)); |
| 686 | else |
| 687 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 688 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 689 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 690 | } else if (sys::fs::exists(S)) { |
| 691 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 692 | } else { |
| 693 | std::string Path = findFromSearchPaths(S); |
| 694 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 695 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 696 | else |
| 697 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 701 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 702 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 703 | bool Orig = Config->AsNeeded; |
| 704 | Config->AsNeeded = true; |
Rui Ueyama | a2acc93 | 2016-08-05 01:25:45 +0000 | [diff] [blame] | 705 | while (!Error && !skip(")")) |
| 706 | addFile(next()); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 707 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 710 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 711 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 712 | expect("("); |
| 713 | StringRef Tok = next(); |
| 714 | if (Config->Entry.empty()) |
| 715 | Config->Entry = Tok; |
| 716 | expect(")"); |
| 717 | } |
| 718 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 719 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 720 | expect("("); |
Rui Ueyama | a2acc93 | 2016-08-05 01:25:45 +0000 | [diff] [blame] | 721 | while (!Error && !skip(")")) |
| 722 | Config->Undefined.push_back(next()); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 725 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 726 | expect("("); |
Rui Ueyama | a2acc93 | 2016-08-05 01:25:45 +0000 | [diff] [blame] | 727 | while (!Error && !skip(")")) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 728 | StringRef Tok = next(); |
Rui Ueyama | a2acc93 | 2016-08-05 01:25:45 +0000 | [diff] [blame] | 729 | if (Tok == "AS_NEEDED") |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 730 | readAsNeeded(); |
Rui Ueyama | a2acc93 | 2016-08-05 01:25:45 +0000 | [diff] [blame] | 731 | else |
| 732 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 736 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 737 | StringRef Tok = next(); |
| 738 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 739 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 740 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 741 | return; |
| 742 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 743 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 744 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 745 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 746 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 749 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 750 | // -o <file> takes predecence over OUTPUT(<file>). |
| 751 | expect("("); |
| 752 | StringRef Tok = next(); |
| 753 | if (Config->OutputFile.empty()) |
| 754 | Config->OutputFile = Tok; |
| 755 | expect(")"); |
| 756 | } |
| 757 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 758 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 759 | // Error checking only for now. |
| 760 | expect("("); |
| 761 | next(); |
| 762 | expect(")"); |
| 763 | } |
| 764 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 765 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 766 | // Error checking only for now. |
| 767 | expect("("); |
| 768 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 769 | StringRef Tok = next(); |
| 770 | if (Tok == ")") |
| 771 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 772 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 773 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 774 | return; |
| 775 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 776 | next(); |
| 777 | expect(","); |
| 778 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 779 | expect(")"); |
| 780 | } |
| 781 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 782 | void ScriptParser::readPhdrs() { |
| 783 | expect("{"); |
| 784 | while (!Error && !skip("}")) { |
| 785 | StringRef Tok = next(); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 786 | Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 787 | PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); |
| 788 | |
| 789 | PhdrCmd.Type = readPhdrType(); |
| 790 | do { |
| 791 | Tok = next(); |
| 792 | if (Tok == ";") |
| 793 | break; |
| 794 | if (Tok == "FILEHDR") |
| 795 | PhdrCmd.HasFilehdr = true; |
| 796 | else if (Tok == "PHDRS") |
| 797 | PhdrCmd.HasPhdrs = true; |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 798 | else if (Tok == "FLAGS") { |
| 799 | expect("("); |
Rafael Espindola | eb685cd | 2016-08-02 22:14:57 +0000 | [diff] [blame] | 800 | // Passing 0 for the value of dot is a bit of a hack. It means that |
| 801 | // we accept expressions like ".|1". |
| 802 | PhdrCmd.Flags = readExpr()(0); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 803 | expect(")"); |
| 804 | } else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 805 | setError("unexpected header attribute: " + Tok); |
| 806 | } while (!Error); |
| 807 | } |
| 808 | } |
| 809 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 810 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 811 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 812 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 813 | expect(")"); |
| 814 | } |
| 815 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 816 | void ScriptParser::readSections() { |
Rui Ueyama | 3de0a33 | 2016-07-29 03:31:09 +0000 | [diff] [blame] | 817 | Opt.HasContents = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 818 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 819 | while (!Error && !skip("}")) { |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 820 | StringRef Tok = next(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 821 | BaseCommand *Cmd = readProvideOrAssignment(Tok); |
| 822 | if (!Cmd) { |
| 823 | if (Tok == "ASSERT") |
| 824 | Cmd = new AssertCommand(readAssert()); |
| 825 | else |
| 826 | Cmd = readOutputSectionDescription(Tok); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 827 | } |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 828 | Opt.Commands.emplace_back(Cmd); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 829 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 832 | static int precedence(StringRef Op) { |
| 833 | return StringSwitch<int>(Op) |
| 834 | .Case("*", 4) |
| 835 | .Case("/", 4) |
| 836 | .Case("+", 3) |
| 837 | .Case("-", 3) |
| 838 | .Case("<", 2) |
| 839 | .Case(">", 2) |
| 840 | .Case(">=", 2) |
| 841 | .Case("<=", 2) |
| 842 | .Case("==", 2) |
| 843 | .Case("!=", 2) |
| 844 | .Case("&", 1) |
| 845 | .Default(-1); |
| 846 | } |
| 847 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 848 | std::vector<StringRef> ScriptParser::readInputFilePatterns() { |
| 849 | std::vector<StringRef> V; |
| 850 | while (!Error && !skip(")")) |
| 851 | V.push_back(next()); |
| 852 | return V; |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 855 | SortKind ScriptParser::readSortKind() { |
| 856 | if (skip("SORT") || skip("SORT_BY_NAME")) |
| 857 | return SortByName; |
| 858 | if (skip("SORT_BY_ALIGNMENT")) |
| 859 | return SortByAlignment; |
| 860 | return SortNone; |
| 861 | } |
| 862 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 863 | InputSectionDescription *ScriptParser::readInputSectionRules() { |
| 864 | auto *Cmd = new InputSectionDescription; |
| 865 | Cmd->FilePattern = next(); |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 866 | expect("("); |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 867 | |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 868 | // Read EXCLUDE_FILE(). |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 869 | if (skip("EXCLUDE_FILE")) { |
| 870 | expect("("); |
| 871 | while (!Error && !skip(")")) |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 872 | Cmd->ExcludedFiles.push_back(next()); |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 873 | } |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 874 | |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 875 | // Read SORT(). |
| 876 | if (SortKind K1 = readSortKind()) { |
| 877 | Cmd->SortOuter = K1; |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 878 | expect("("); |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 879 | if (SortKind K2 = readSortKind()) { |
| 880 | Cmd->SortInner = K2; |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 881 | expect("("); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 882 | Cmd->SectionPatterns = readInputFilePatterns(); |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 883 | expect(")"); |
| 884 | } else { |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 885 | Cmd->SectionPatterns = readInputFilePatterns(); |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 886 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 887 | expect(")"); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 888 | return Cmd; |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 889 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 890 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 891 | Cmd->SectionPatterns = readInputFilePatterns(); |
| 892 | return Cmd; |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 895 | InputSectionDescription *ScriptParser::readInputSectionDescription() { |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 896 | // Input section wildcard can be surrounded by KEEP. |
| 897 | // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep |
| 898 | if (skip("KEEP")) { |
| 899 | expect("("); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 900 | InputSectionDescription *Cmd = readInputSectionRules(); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 901 | expect(")"); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 902 | Opt.KeptSections.insert(Opt.KeptSections.end(), |
| 903 | Cmd->SectionPatterns.begin(), |
| 904 | Cmd->SectionPatterns.end()); |
| 905 | return Cmd; |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 906 | } |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 907 | return readInputSectionRules(); |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 910 | Expr ScriptParser::readAlign() { |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 911 | expect("("); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 912 | Expr E = readExpr(); |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 913 | expect(")"); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 914 | return E; |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 915 | } |
| 916 | |
George Rimar | 03fc010 | 2016-07-28 07:18:23 +0000 | [diff] [blame] | 917 | void ScriptParser::readSort() { |
| 918 | expect("("); |
| 919 | expect("CONSTRUCTORS"); |
| 920 | expect(")"); |
| 921 | } |
| 922 | |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 923 | Expr ScriptParser::readAssert() { |
| 924 | expect("("); |
| 925 | Expr E = readExpr(); |
| 926 | expect(","); |
| 927 | StringRef Msg = next(); |
| 928 | expect(")"); |
| 929 | return [=](uint64_t Dot) { |
| 930 | uint64_t V = E(Dot); |
| 931 | if (!V) |
| 932 | error(Msg); |
| 933 | return V; |
| 934 | }; |
| 935 | } |
| 936 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 937 | OutputSectionCommand * |
| 938 | ScriptParser::readOutputSectionDescription(StringRef OutSec) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 939 | OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); |
George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 940 | |
| 941 | // Read an address expression. |
| 942 | // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address |
| 943 | if (peek() != ":") |
| 944 | Cmd->AddrExpr = readExpr(); |
| 945 | |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 946 | expect(":"); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 947 | |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 948 | if (skip("ALIGN")) |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 949 | Cmd->AlignExpr = readAlign(); |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 950 | |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 951 | // Parse constraints. |
| 952 | if (skip("ONLY_IF_RO")) |
Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 953 | Cmd->Constraint = ConstraintKind::ReadOnly; |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 954 | if (skip("ONLY_IF_RW")) |
Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 955 | Cmd->Constraint = ConstraintKind::ReadWrite; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 956 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 957 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 958 | while (!Error && !skip("}")) { |
George Rimar | f586ff7 | 2016-07-28 22:15:44 +0000 | [diff] [blame] | 959 | if (peek().startswith("*") || peek() == "KEEP") { |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 960 | Cmd->Commands.emplace_back(readInputSectionDescription()); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 961 | continue; |
| 962 | } |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 963 | |
| 964 | StringRef Tok = next(); |
| 965 | if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) |
| 966 | Cmd->Commands.emplace_back(Assignment); |
| 967 | else if (Tok == "SORT") |
George Rimar | 03fc010 | 2016-07-28 07:18:23 +0000 | [diff] [blame] | 968 | readSort(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 969 | else |
| 970 | setError("unknown command " + Tok); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 971 | } |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 972 | Cmd->Phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 973 | Cmd->Filler = readOutputSectionFiller(); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 974 | return Cmd; |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 975 | } |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 976 | |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 977 | std::vector<uint8_t> ScriptParser::readOutputSectionFiller() { |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 978 | StringRef Tok = peek(); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 979 | if (!Tok.startswith("=")) |
| 980 | return {}; |
Davide Italiano | 5ac0d7c | 2016-07-29 22:21:28 +0000 | [diff] [blame] | 981 | next(); |
Rui Ueyama | 965827d | 2016-08-03 23:25:15 +0000 | [diff] [blame] | 982 | |
| 983 | // Read a hexstring of arbitrary length. |
Davide Italiano | 5ac0d7c | 2016-07-29 22:21:28 +0000 | [diff] [blame] | 984 | if (Tok.startswith("=0x")) |
| 985 | return parseHex(Tok.substr(3)); |
| 986 | |
Rui Ueyama | 965827d | 2016-08-03 23:25:15 +0000 | [diff] [blame] | 987 | // Read a decimal or octal value as a big-endian 32 bit value. |
| 988 | // Why do this? I don't know, but that's what gold does. |
| 989 | uint32_t V; |
| 990 | if (Tok.substr(1).getAsInteger(0, V)) { |
| 991 | setError("invalid filler expression: " + Tok); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 992 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 993 | } |
Rui Ueyama | 965827d | 2016-08-03 23:25:15 +0000 | [diff] [blame] | 994 | return { uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V) }; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 997 | SymbolAssignment *ScriptParser::readProvide(bool Hidden) { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 998 | expect("("); |
Rui Ueyama | 174e0a1 | 2016-07-29 00:29:25 +0000 | [diff] [blame] | 999 | SymbolAssignment *Cmd = readAssignment(next()); |
| 1000 | Cmd->Provide = true; |
| 1001 | Cmd->Hidden = Hidden; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 1002 | expect(")"); |
| 1003 | expect(";"); |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 1004 | return Cmd; |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 1007 | SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) { |
| 1008 | SymbolAssignment *Cmd = nullptr; |
| 1009 | if (peek() == "=" || peek() == "+=") { |
| 1010 | Cmd = readAssignment(Tok); |
| 1011 | expect(";"); |
| 1012 | } else if (Tok == "PROVIDE") { |
| 1013 | Cmd = readProvide(false); |
| 1014 | } else if (Tok == "PROVIDE_HIDDEN") { |
| 1015 | Cmd = readProvide(true); |
| 1016 | } |
| 1017 | return Cmd; |
| 1018 | } |
| 1019 | |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1020 | static uint64_t getSymbolValue(StringRef S, uint64_t Dot) { |
| 1021 | if (S == ".") |
| 1022 | return Dot; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 1023 | |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1024 | switch (Config->EKind) { |
| 1025 | case ELF32LEKind: |
| 1026 | if (SymbolBody *B = Symtab<ELF32LE>::X->find(S)) |
| 1027 | return B->getVA<ELF32LE>(); |
| 1028 | break; |
| 1029 | case ELF32BEKind: |
| 1030 | if (SymbolBody *B = Symtab<ELF32BE>::X->find(S)) |
| 1031 | return B->getVA<ELF32BE>(); |
| 1032 | break; |
| 1033 | case ELF64LEKind: |
| 1034 | if (SymbolBody *B = Symtab<ELF64LE>::X->find(S)) |
| 1035 | return B->getVA<ELF64LE>(); |
| 1036 | break; |
| 1037 | case ELF64BEKind: |
| 1038 | if (SymbolBody *B = Symtab<ELF64BE>::X->find(S)) |
| 1039 | return B->getVA<ELF64BE>(); |
| 1040 | break; |
George Rimar | 6930a6d | 2016-07-26 18:41:06 +0000 | [diff] [blame] | 1041 | default: |
George Rimar | b567b62 | 2016-07-26 18:46:13 +0000 | [diff] [blame] | 1042 | llvm_unreachable("unsupported target"); |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1043 | } |
| 1044 | error("symbol not found: " + S); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 1048 | static uint64_t getSectionSize(StringRef Name) { |
| 1049 | switch (Config->EKind) { |
| 1050 | case ELF32LEKind: |
| 1051 | return Script<ELF32LE>::X->getOutputSectionSize(Name); |
| 1052 | case ELF32BEKind: |
| 1053 | return Script<ELF32BE>::X->getOutputSectionSize(Name); |
| 1054 | case ELF64LEKind: |
| 1055 | return Script<ELF64LE>::X->getOutputSectionSize(Name); |
| 1056 | case ELF64BEKind: |
| 1057 | return Script<ELF64BE>::X->getOutputSectionSize(Name); |
| 1058 | default: |
| 1059 | llvm_unreachable("unsupported target"); |
| 1060 | } |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
George Rimar | e32a359 | 2016-08-10 07:59:34 +0000 | [diff] [blame] | 1063 | static uint64_t getSizeOfHeaders() { |
| 1064 | switch (Config->EKind) { |
| 1065 | case ELF32LEKind: |
| 1066 | return Script<ELF32LE>::X->getSizeOfHeaders(); |
| 1067 | case ELF32BEKind: |
| 1068 | return Script<ELF32BE>::X->getSizeOfHeaders(); |
| 1069 | case ELF64LEKind: |
| 1070 | return Script<ELF64LE>::X->getSizeOfHeaders(); |
| 1071 | case ELF64BEKind: |
| 1072 | return Script<ELF64BE>::X->getSizeOfHeaders(); |
| 1073 | default: |
| 1074 | llvm_unreachable("unsupported target"); |
| 1075 | } |
| 1076 | } |
| 1077 | |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1078 | SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { |
| 1079 | StringRef Op = next(); |
| 1080 | assert(Op == "=" || Op == "+="); |
| 1081 | Expr E = readExpr(); |
| 1082 | if (Op == "+=") |
| 1083 | E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); }; |
Rui Ueyama | 1041656 | 2016-08-04 02:03:27 +0000 | [diff] [blame] | 1084 | return new SymbolAssignment(Name, E); |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | // This is an operator-precedence parser to parse a linker |
| 1088 | // script expression. |
| 1089 | Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } |
| 1090 | |
Rui Ueyama | 36c1cd2 | 2016-08-05 01:04:59 +0000 | [diff] [blame] | 1091 | static Expr combine(StringRef Op, Expr L, Expr R) { |
| 1092 | if (Op == "*") |
| 1093 | return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; |
| 1094 | if (Op == "/") { |
| 1095 | return [=](uint64_t Dot) -> uint64_t { |
| 1096 | uint64_t RHS = R(Dot); |
| 1097 | if (RHS == 0) { |
| 1098 | error("division by zero"); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | return L(Dot) / RHS; |
| 1102 | }; |
| 1103 | } |
| 1104 | if (Op == "+") |
| 1105 | return [=](uint64_t Dot) { return L(Dot) + R(Dot); }; |
| 1106 | if (Op == "-") |
| 1107 | return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; |
| 1108 | if (Op == "<") |
| 1109 | return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; |
| 1110 | if (Op == ">") |
| 1111 | return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; |
| 1112 | if (Op == ">=") |
| 1113 | return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; |
| 1114 | if (Op == "<=") |
| 1115 | return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; |
| 1116 | if (Op == "==") |
| 1117 | return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; |
| 1118 | if (Op == "!=") |
| 1119 | return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; |
| 1120 | if (Op == "&") |
| 1121 | return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; |
| 1122 | llvm_unreachable("invalid operator"); |
| 1123 | } |
| 1124 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1125 | // This is a part of the operator-precedence parser. This function |
| 1126 | // assumes that the remaining token stream starts with an operator. |
| 1127 | Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { |
| 1128 | while (!atEOF() && !Error) { |
| 1129 | // Read an operator and an expression. |
| 1130 | StringRef Op1 = peek(); |
| 1131 | if (Op1 == "?") |
| 1132 | return readTernary(Lhs); |
| 1133 | if (precedence(Op1) < MinPrec) |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 1134 | break; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1135 | next(); |
| 1136 | Expr Rhs = readPrimary(); |
| 1137 | |
| 1138 | // Evaluate the remaining part of the expression first if the |
| 1139 | // next operator has greater precedence than the previous one. |
| 1140 | // For example, if we have read "+" and "3", and if the next |
| 1141 | // operator is "*", then we'll evaluate 3 * ... part first. |
| 1142 | while (!atEOF()) { |
| 1143 | StringRef Op2 = peek(); |
| 1144 | if (precedence(Op2) <= precedence(Op1)) |
| 1145 | break; |
| 1146 | Rhs = readExpr1(Rhs, precedence(Op2)); |
| 1147 | } |
| 1148 | |
| 1149 | Lhs = combine(Op1, Lhs, Rhs); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 1150 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1151 | return Lhs; |
| 1152 | } |
| 1153 | |
| 1154 | uint64_t static getConstant(StringRef S) { |
| 1155 | if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE") |
| 1156 | return Target->PageSize; |
| 1157 | error("unknown constant: " + S); |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | Expr ScriptParser::readPrimary() { |
| 1162 | StringRef Tok = next(); |
| 1163 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1164 | if (Tok == "(") { |
| 1165 | Expr E = readExpr(); |
| 1166 | expect(")"); |
| 1167 | return E; |
| 1168 | } |
| 1169 | |
| 1170 | // Built-in functions are parsed here. |
| 1171 | // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 1172 | if (Tok == "ASSERT") |
| 1173 | return readAssert(); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1174 | if (Tok == "ALIGN") { |
| 1175 | expect("("); |
| 1176 | Expr E = readExpr(); |
| 1177 | expect(")"); |
| 1178 | return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; |
| 1179 | } |
| 1180 | if (Tok == "CONSTANT") { |
| 1181 | expect("("); |
| 1182 | StringRef Tok = next(); |
| 1183 | expect(")"); |
| 1184 | return [=](uint64_t Dot) { return getConstant(Tok); }; |
| 1185 | } |
Rafael Espindola | 54c145c | 2016-07-28 18:16:24 +0000 | [diff] [blame] | 1186 | if (Tok == "SEGMENT_START") { |
| 1187 | expect("("); |
| 1188 | next(); |
| 1189 | expect(","); |
| 1190 | uint64_t Val; |
| 1191 | next().getAsInteger(0, Val); |
| 1192 | expect(")"); |
| 1193 | return [=](uint64_t Dot) { return Val; }; |
| 1194 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1195 | if (Tok == "DATA_SEGMENT_ALIGN") { |
| 1196 | expect("("); |
| 1197 | Expr E = readExpr(); |
| 1198 | expect(","); |
| 1199 | readExpr(); |
| 1200 | expect(")"); |
Rui Ueyama | f7791bb | 2016-07-26 19:34:10 +0000 | [diff] [blame] | 1201 | return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1202 | } |
| 1203 | if (Tok == "DATA_SEGMENT_END") { |
| 1204 | expect("("); |
| 1205 | expect("."); |
| 1206 | expect(")"); |
| 1207 | return [](uint64_t Dot) { return Dot; }; |
| 1208 | } |
George Rimar | 276b4e6 | 2016-07-26 17:58:44 +0000 | [diff] [blame] | 1209 | // GNU linkers implements more complicated logic to handle |
| 1210 | // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to |
| 1211 | // the next page boundary for simplicity. |
| 1212 | if (Tok == "DATA_SEGMENT_RELRO_END") { |
| 1213 | expect("("); |
| 1214 | next(); |
| 1215 | expect(","); |
| 1216 | readExpr(); |
| 1217 | expect(")"); |
| 1218 | return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; |
| 1219 | } |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 1220 | if (Tok == "SIZEOF") { |
| 1221 | expect("("); |
| 1222 | StringRef Name = next(); |
| 1223 | expect(")"); |
| 1224 | return [=](uint64_t Dot) { return getSectionSize(Name); }; |
| 1225 | } |
George Rimar | e32a359 | 2016-08-10 07:59:34 +0000 | [diff] [blame] | 1226 | if (Tok == "SIZEOF_HEADERS") |
| 1227 | return [=](uint64_t Dot) { return getSizeOfHeaders(); }; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1228 | |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1229 | // Parse a symbol name or a number literal. |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1230 | uint64_t V = 0; |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1231 | if (Tok.getAsInteger(0, V)) { |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1232 | if (Tok != "." && !isValidCIdentifier(Tok)) |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1233 | setError("malformed number: " + Tok); |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1234 | return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); }; |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1235 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1236 | return [=](uint64_t Dot) { return V; }; |
| 1237 | } |
| 1238 | |
| 1239 | Expr ScriptParser::readTernary(Expr Cond) { |
| 1240 | next(); |
| 1241 | Expr L = readExpr(); |
| 1242 | expect(":"); |
| 1243 | Expr R = readExpr(); |
| 1244 | return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; |
| 1245 | } |
| 1246 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1247 | std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { |
| 1248 | std::vector<StringRef> Phdrs; |
| 1249 | while (!Error && peek().startswith(":")) { |
| 1250 | StringRef Tok = next(); |
| 1251 | Tok = (Tok.size() == 1) ? next() : Tok.substr(1); |
| 1252 | if (Tok.empty()) { |
| 1253 | setError("section header name is empty"); |
| 1254 | break; |
| 1255 | } |
Rui Ueyama | 047404f | 2016-07-20 19:36:36 +0000 | [diff] [blame] | 1256 | Phdrs.push_back(Tok); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1257 | } |
| 1258 | return Phdrs; |
| 1259 | } |
| 1260 | |
| 1261 | unsigned ScriptParser::readPhdrType() { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1262 | StringRef Tok = next(); |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 1263 | unsigned Ret = StringSwitch<unsigned>(Tok) |
| 1264 | .Case("PT_NULL", PT_NULL) |
| 1265 | .Case("PT_LOAD", PT_LOAD) |
| 1266 | .Case("PT_DYNAMIC", PT_DYNAMIC) |
| 1267 | .Case("PT_INTERP", PT_INTERP) |
| 1268 | .Case("PT_NOTE", PT_NOTE) |
| 1269 | .Case("PT_SHLIB", PT_SHLIB) |
| 1270 | .Case("PT_PHDR", PT_PHDR) |
| 1271 | .Case("PT_TLS", PT_TLS) |
| 1272 | .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) |
| 1273 | .Case("PT_GNU_STACK", PT_GNU_STACK) |
| 1274 | .Case("PT_GNU_RELRO", PT_GNU_RELRO) |
| 1275 | .Default(-1); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1276 | |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 1277 | if (Ret == (unsigned)-1) { |
| 1278 | setError("invalid program header type: " + Tok); |
| 1279 | return PT_NULL; |
| 1280 | } |
| 1281 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 1284 | static bool isUnderSysroot(StringRef Path) { |
| 1285 | if (Config->Sysroot == "") |
| 1286 | return false; |
| 1287 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 1288 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 1289 | return true; |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1293 | // Entry point. |
| 1294 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 1295 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1296 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 1297 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1298 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1299 | template class elf::LinkerScript<ELF32LE>; |
| 1300 | template class elf::LinkerScript<ELF32BE>; |
| 1301 | template class elf::LinkerScript<ELF64LE>; |
| 1302 | template class elf::LinkerScript<ELF64BE>; |