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 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 46 | bool SymbolAssignment::classof(const BaseCommand *C) { |
| 47 | return C->Kind == AssignmentKind; |
| 48 | } |
| 49 | |
| 50 | bool OutputSectionCommand::classof(const BaseCommand *C) { |
| 51 | return C->Kind == OutputSectionKind; |
| 52 | } |
| 53 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 54 | bool InputSectionDescription::classof(const BaseCommand *C) { |
| 55 | return C->Kind == InputSectionKind; |
| 56 | } |
| 57 | |
Rui Ueyama | 36a153c | 2016-07-23 14:09:58 +0000 | [diff] [blame] | 58 | template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 59 | return !S || !S->Live; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 62 | template <class ELFT> |
| 63 | bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 64 | for (StringRef Pat : Opt.KeptSections) |
Rui Ueyama | 722830a | 2016-06-29 05:32:09 +0000 | [diff] [blame] | 65 | if (globMatch(Pat, S->getSectionName())) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 66 | return true; |
| 67 | return false; |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Rui Ueyama | 63dc650 | 2016-07-25 22:41:42 +0000 | [diff] [blame] | 70 | static bool match(ArrayRef<StringRef> Patterns, StringRef S) { |
| 71 | for (StringRef Pat : Patterns) |
| 72 | if (globMatch(Pat, S)) |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 73 | return true; |
| 74 | return false; |
| 75 | } |
| 76 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 77 | // Create a vector of (<output section name>, <input section name patterns>). |
| 78 | // For example, if a returned vector contains (".text" (".foo.*" ".bar.*")), |
| 79 | // input sections start with ".foo." or ".bar." should be added to |
| 80 | // ".text" section. |
| 81 | template <class ELFT> |
| 82 | std::vector<std::pair<StringRef, ArrayRef<StringRef>>> |
| 83 | LinkerScript<ELFT>::getSectionMap() { |
| 84 | std::vector<std::pair<StringRef, ArrayRef<StringRef>>> Ret; |
| 85 | |
| 86 | for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) |
| 87 | if (auto *Cmd1 = dyn_cast<OutputSectionCommand>(Base1.get())) |
| 88 | for (const std::unique_ptr<BaseCommand> &Base2 : Cmd1->Commands) |
| 89 | if (auto *Cmd2 = dyn_cast<InputSectionDescription>(Base2.get())) |
| 90 | Ret.emplace_back(Cmd1->Name, Cmd2->Patterns); |
| 91 | |
| 92 | return Ret; |
| 93 | } |
| 94 | |
| 95 | // Returns input sections filtered by given glob patterns. |
| 96 | template <class ELFT> |
| 97 | std::vector<InputSectionBase<ELFT> *> |
| 98 | LinkerScript<ELFT>::getInputSections(ArrayRef<StringRef> Patterns) { |
| 99 | std::vector<InputSectionBase<ELFT> *> Ret; |
| 100 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : |
| 101 | Symtab<ELFT>::X->getObjectFiles()) |
| 102 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 103 | if (!isDiscarded(S) && !S->OutSec && match(Patterns, S->getSectionName())) |
| 104 | Ret.push_back(S); |
| 105 | return Ret; |
| 106 | } |
| 107 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 108 | template <class ELFT> |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 109 | std::vector<OutputSectionBase<ELFT> *> |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 110 | LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) { |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 111 | std::vector<OutputSectionBase<ELFT> *> Ret; |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 112 | |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 113 | // Add input section to output section. If there is no output section yet, |
| 114 | // then create it and add to output section list. |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 115 | auto Add = [&](InputSectionBase<ELFT> *C, StringRef Name) { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 116 | OutputSectionBase<ELFT> *Sec; |
| 117 | bool IsNew; |
| 118 | std::tie(Sec, IsNew) = Factory.create(C, Name); |
| 119 | if (IsNew) |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 120 | Ret.push_back(Sec); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 121 | Sec->addSection(C); |
| 122 | }; |
| 123 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 124 | for (auto &P : getSectionMap()) { |
| 125 | StringRef OutputName = P.first; |
| 126 | ArrayRef<StringRef> InputPatterns = P.second; |
| 127 | for (InputSectionBase<ELFT> *S : getInputSections(InputPatterns)) { |
| 128 | if (OutputName == "/DISCARD/") { |
| 129 | S->Live = false; |
| 130 | reportDiscarded(S); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 131 | continue; |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 132 | } |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 133 | Add(S, OutputName); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 136 | |
| 137 | // Add all other input sections, which are not listed in script. |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 138 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : |
| 139 | Symtab<ELFT>::X->getObjectFiles()) |
| 140 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 141 | if (!isDiscarded(S) && !S->OutSec) |
| 142 | Add(S, getOutputSectionName(S)); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 143 | |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 144 | // Remove from the output all the sections which did not meet |
| 145 | // the optional constraints. |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 146 | return filter(Ret); |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Process ONLY_IF_RO and ONLY_IF_RW. |
| 150 | template <class ELFT> |
| 151 | std::vector<OutputSectionBase<ELFT> *> |
| 152 | LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) { |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 153 | // In this loop, we remove output sections if they don't satisfy |
| 154 | // requested properties. |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 155 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 156 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 157 | if (!Cmd || Cmd->Name == "/DISCARD/") |
| 158 | continue; |
| 159 | |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 160 | if (Cmd->Constraint == ConstraintKind::NoConstraint) |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 161 | continue; |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 162 | |
| 163 | auto It = llvm::find_if(Sections, [&](OutputSectionBase<ELFT> *S) { |
| 164 | return S->getName() == Cmd->Name; |
| 165 | }); |
| 166 | if (It == Sections.end()) |
| 167 | continue; |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 168 | |
| 169 | OutputSectionBase<ELFT> *Sec = *It; |
| 170 | bool Writable = (Sec->getFlags() & SHF_WRITE); |
| 171 | bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly); |
| 172 | bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite); |
| 173 | |
| 174 | if ((RO && Writable) || (RW && !Writable)) { |
| 175 | Sections.erase(It); |
| 176 | continue; |
| 177 | } |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 178 | } |
| 179 | return Sections; |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | template <class ELFT> |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 183 | void LinkerScript<ELFT>::dispatchAssignment(SymbolAssignment *Cmd) { |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 184 | uint64_t Val = Cmd->Expression(Dot); |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 185 | if (Cmd->Name == ".") { |
| 186 | Dot = Val; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 187 | } else if (!Cmd->Ignore) { |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 188 | auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name)); |
| 189 | D->Value = Val; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 194 | void LinkerScript<ELFT>::assignAddresses( |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 195 | ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 196 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 197 | // are not explicitly placed into the output file by the linker script. |
| 198 | // We place orphan sections at end of file. |
| 199 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 200 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 201 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 202 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 203 | if (getSectionIndex(Name) == INT_MAX) |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 204 | Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 205 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 206 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 207 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 208 | Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 209 | uintX_t MinVA = std::numeric_limits<uintX_t>::max(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 210 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 211 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 212 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 213 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 214 | dispatchAssignment(Cmd); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 215 | continue; |
| 216 | } |
| 217 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 218 | // Find all the sections with required name. There can be more than |
George Rimar | 6ad330a | 2016-07-19 07:39:07 +0000 | [diff] [blame] | 219 | // one section with such name, if the alignment, flags or type |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 220 | // attribute differs. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 221 | auto *Cmd = cast<OutputSectionCommand>(Base.get()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 222 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 223 | if (Sec->getName() != Cmd->Name) |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 224 | continue; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 225 | |
George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 226 | if (Cmd->AddrExpr) |
| 227 | Dot = Cmd->AddrExpr(Dot); |
| 228 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 229 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 230 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 231 | TVA = alignTo(TVA, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 232 | Sec->setVA(TVA); |
| 233 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
| 234 | continue; |
| 235 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 236 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 237 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 238 | Dot = alignTo(Dot, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 239 | Sec->setVA(Dot); |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 240 | MinVA = std::min(MinVA, Dot); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 241 | Dot += Sec->getSize(); |
| 242 | continue; |
| 243 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 246 | |
Rafael Espindola | 64c32d6 | 2016-07-07 14:28:47 +0000 | [diff] [blame] | 247 | // 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] | 248 | // memory. Set their addresses accordingly. |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 249 | MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - |
| 250 | Out<ELFT>::ProgramHeaders->getSize(), |
| 251 | Target->PageSize); |
| 252 | Out<ELFT>::ElfHeader->setVA(MinVA); |
| 253 | Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 256 | template <class ELFT> |
Rafael Espindola | 74df5c7 | 2016-07-19 12:33:46 +0000 | [diff] [blame] | 257 | std::vector<PhdrEntry<ELFT>> |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 258 | LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 259 | std::vector<PhdrEntry<ELFT>> Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 260 | |
| 261 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 262 | Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
| 263 | PhdrEntry<ELFT> &Phdr = Ret.back(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 264 | |
| 265 | if (Cmd.HasFilehdr) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 266 | Phdr.add(Out<ELFT>::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 267 | if (Cmd.HasPhdrs) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 268 | Phdr.add(Out<ELFT>::ProgramHeaders); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 269 | |
| 270 | switch (Cmd.Type) { |
| 271 | case PT_INTERP: |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 272 | if (Out<ELFT>::Interp) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 273 | Phdr.add(Out<ELFT>::Interp); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 274 | break; |
| 275 | case PT_DYNAMIC: |
| 276 | if (isOutputDynamic<ELFT>()) { |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 277 | Phdr.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags()); |
| 278 | Phdr.add(Out<ELFT>::Dynamic); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 279 | } |
| 280 | break; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 281 | case PT_GNU_EH_FRAME: |
| 282 | if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 283 | Phdr.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()); |
| 284 | Phdr.add(Out<ELFT>::EhFrameHdr); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 285 | } |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 290 | PhdrEntry<ELFT> *Load = nullptr; |
| 291 | uintX_t Flags = PF_R; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 292 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
| 293 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 294 | break; |
| 295 | |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 296 | std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName()); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 297 | if (!PhdrIds.empty()) { |
| 298 | // Assign headers specified by linker script |
| 299 | for (size_t Id : PhdrIds) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 300 | Ret[Id].add(Sec); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 301 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 302 | Ret[Id].H.p_flags |= toPhdrFlags(Sec->getFlags()); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 303 | } |
| 304 | } else { |
| 305 | // If we have no load segment or flags've changed then we want new load |
| 306 | // segment. |
| 307 | uintX_t NewFlags = toPhdrFlags(Sec->getFlags()); |
| 308 | if (Load == nullptr || Flags != NewFlags) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 309 | Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 310 | Flags = NewFlags; |
| 311 | } |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 312 | Load->add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 313 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 314 | } |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 315 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 319 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 320 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) |
| 321 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 322 | if (Cmd->Name == Name) |
| 323 | return Cmd->Filler; |
| 324 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 327 | // Returns the index of the given section name in linker script |
| 328 | // SECTIONS commands. Sections are laid out as the same order as they |
| 329 | // were in the script. If a given name did not appear in the script, |
| 330 | // 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] | 331 | template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
Rui Ueyama | f510fa6 | 2016-07-26 00:21:15 +0000 | [diff] [blame] | 332 | int I = 0; |
| 333 | for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 334 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 335 | if (Cmd->Name == Name) |
| 336 | return I; |
| 337 | ++I; |
| 338 | } |
| 339 | return INT_MAX; |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // A compartor to sort output sections. Returns -1 or 1 if |
| 343 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 344 | template <class ELFT> |
| 345 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 346 | int I = getSectionIndex(A); |
| 347 | int J = getSectionIndex(B); |
| 348 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 349 | return 0; |
| 350 | return I < J ? -1 : 1; |
| 351 | } |
| 352 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 353 | template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 354 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 355 | auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()); |
| 356 | if (!Cmd || Cmd->Name == ".") |
| 357 | continue; |
| 358 | |
Davide Italiano | 8ab4108 | 2016-07-23 22:09:04 +0000 | [diff] [blame] | 359 | SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); |
Davide Italiano | 373a533 | 2016-07-25 00:25:18 +0000 | [diff] [blame] | 360 | // The semantic of PROVIDE is that of introducing a symbol only if |
| 361 | // it's not defined and there's at least a reference to it. |
| 362 | if ((!B && !Cmd->Provide) || (B && B->isUndefined())) |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 363 | Symtab<ELFT>::X->addAbsolute(Cmd->Name, |
| 364 | Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT); |
| 365 | else |
| 366 | // Symbol already exists in symbol table. If it is provided |
| 367 | // then we can't override its value. |
| 368 | Cmd->Ignore = Cmd->Provide; |
| 369 | } |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 372 | template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { |
| 373 | return !Opt.PhdrsCommands.empty(); |
| 374 | } |
| 375 | |
| 376 | // Returns indices of ELF headers containing specific section, identified |
| 377 | // by Name. Each index is a zero based number of ELF header listed within |
| 378 | // PHDRS {} script block. |
| 379 | template <class ELFT> |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 380 | std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 381 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 382 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 383 | if (!Cmd || Cmd->Name != SectionName) |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 384 | continue; |
| 385 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 386 | std::vector<size_t> Ret; |
| 387 | for (StringRef PhdrName : Cmd->Phdrs) |
| 388 | Ret.push_back(getPhdrIndex(PhdrName)); |
| 389 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 390 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 391 | return {}; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 394 | template <class ELFT> |
| 395 | size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) { |
| 396 | size_t I = 0; |
| 397 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
| 398 | if (Cmd.Name == PhdrName) |
| 399 | return I; |
| 400 | ++I; |
| 401 | } |
| 402 | error("section header '" + PhdrName + "' is not listed in PHDRS"); |
| 403 | return 0; |
| 404 | } |
| 405 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 406 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 407 | typedef void (ScriptParser::*Handler)(); |
| 408 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 409 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 410 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 411 | |
Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 412 | void run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 413 | |
| 414 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 415 | void addFile(StringRef Path); |
| 416 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 417 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 418 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 419 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 420 | void readGroup(); |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 421 | void readKeep(OutputSectionCommand *Cmd); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 422 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 423 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 424 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 425 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 426 | void readOutputFormat(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 427 | void readPhdrs(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 428 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 429 | void readSections(); |
| 430 | |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 431 | SymbolAssignment *readAssignment(StringRef Name); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 432 | void readOutputSectionDescription(StringRef OutSec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 433 | std::vector<StringRef> readOutputSectionPhdrs(); |
| 434 | unsigned readPhdrType(); |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 435 | void readProvide(bool Hidden); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 436 | |
| 437 | Expr readExpr(); |
| 438 | Expr readExpr1(Expr Lhs, int MinPrec); |
| 439 | Expr readPrimary(); |
| 440 | Expr readTernary(Expr Cond); |
| 441 | Expr combine(StringRef Op, Expr Lhs, Expr Rhs); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 442 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 443 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 444 | ScriptConfiguration &Opt = *ScriptConfig; |
| 445 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 446 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 447 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 449 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 450 | {"ENTRY", &ScriptParser::readEntry}, |
| 451 | {"EXTERN", &ScriptParser::readExtern}, |
| 452 | {"GROUP", &ScriptParser::readGroup}, |
| 453 | {"INCLUDE", &ScriptParser::readInclude}, |
| 454 | {"INPUT", &ScriptParser::readGroup}, |
| 455 | {"OUTPUT", &ScriptParser::readOutput}, |
| 456 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 457 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 458 | {"PHDRS", &ScriptParser::readPhdrs}, |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 459 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 460 | {"SECTIONS", &ScriptParser::readSections}, |
| 461 | {";", &ScriptParser::readNothing}}; |
| 462 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 463 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 464 | while (!atEOF()) { |
| 465 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 466 | if (Handler Fn = Cmd.lookup(Tok)) |
| 467 | (this->*Fn)(); |
| 468 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 469 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 473 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 474 | if (IsUnderSysroot && S.startswith("/")) { |
| 475 | SmallString<128> Path; |
| 476 | (Config->Sysroot + S).toStringRef(Path); |
| 477 | if (sys::fs::exists(Path)) { |
| 478 | Driver->addFile(Saver.save(Path.str())); |
| 479 | return; |
| 480 | } |
| 481 | } |
| 482 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 483 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 484 | Driver->addFile(S); |
| 485 | } else if (S.startswith("=")) { |
| 486 | if (Config->Sysroot.empty()) |
| 487 | Driver->addFile(S.substr(1)); |
| 488 | else |
| 489 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 490 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 491 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 492 | } else if (sys::fs::exists(S)) { |
| 493 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 494 | } else { |
| 495 | std::string Path = findFromSearchPaths(S); |
| 496 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 497 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 498 | else |
| 499 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 503 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 504 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 505 | bool Orig = Config->AsNeeded; |
| 506 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 507 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 508 | StringRef Tok = next(); |
| 509 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 510 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 511 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 512 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 513 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 516 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 517 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 518 | expect("("); |
| 519 | StringRef Tok = next(); |
| 520 | if (Config->Entry.empty()) |
| 521 | Config->Entry = Tok; |
| 522 | expect(")"); |
| 523 | } |
| 524 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 525 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 526 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 527 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 528 | StringRef Tok = next(); |
| 529 | if (Tok == ")") |
| 530 | return; |
| 531 | Config->Undefined.push_back(Tok); |
| 532 | } |
| 533 | } |
| 534 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 535 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 536 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 537 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 538 | StringRef Tok = next(); |
| 539 | if (Tok == ")") |
| 540 | return; |
| 541 | if (Tok == "AS_NEEDED") { |
| 542 | readAsNeeded(); |
| 543 | continue; |
| 544 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 545 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 549 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 550 | StringRef Tok = next(); |
| 551 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 552 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 553 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 556 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 557 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 558 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 559 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 562 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 563 | // -o <file> takes predecence over OUTPUT(<file>). |
| 564 | expect("("); |
| 565 | StringRef Tok = next(); |
| 566 | if (Config->OutputFile.empty()) |
| 567 | Config->OutputFile = Tok; |
| 568 | expect(")"); |
| 569 | } |
| 570 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 571 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 572 | // Error checking only for now. |
| 573 | expect("("); |
| 574 | next(); |
| 575 | expect(")"); |
| 576 | } |
| 577 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 578 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 579 | // Error checking only for now. |
| 580 | expect("("); |
| 581 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 582 | StringRef Tok = next(); |
| 583 | if (Tok == ")") |
| 584 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 585 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 586 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 587 | return; |
| 588 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 589 | next(); |
| 590 | expect(","); |
| 591 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 592 | expect(")"); |
| 593 | } |
| 594 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 595 | void ScriptParser::readPhdrs() { |
| 596 | expect("{"); |
| 597 | while (!Error && !skip("}")) { |
| 598 | StringRef Tok = next(); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 599 | Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 600 | PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); |
| 601 | |
| 602 | PhdrCmd.Type = readPhdrType(); |
| 603 | do { |
| 604 | Tok = next(); |
| 605 | if (Tok == ";") |
| 606 | break; |
| 607 | if (Tok == "FILEHDR") |
| 608 | PhdrCmd.HasFilehdr = true; |
| 609 | else if (Tok == "PHDRS") |
| 610 | PhdrCmd.HasPhdrs = true; |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 611 | else if (Tok == "FLAGS") { |
| 612 | expect("("); |
| 613 | next().getAsInteger(0, PhdrCmd.Flags); |
| 614 | expect(")"); |
| 615 | } else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 616 | setError("unexpected header attribute: " + Tok); |
| 617 | } while (!Error); |
| 618 | } |
| 619 | } |
| 620 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 621 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 622 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 623 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 624 | expect(")"); |
| 625 | } |
| 626 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 627 | void ScriptParser::readSections() { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 628 | Opt.DoLayout = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 629 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 630 | while (!Error && !skip("}")) { |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 631 | StringRef Tok = next(); |
| 632 | if (peek() == "=") { |
| 633 | readAssignment(Tok); |
| 634 | expect(";"); |
| 635 | } else if (Tok == "PROVIDE") { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 636 | readProvide(false); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 637 | } else if (Tok == "PROVIDE_HIDDEN") { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 638 | readProvide(true); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 639 | } else { |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 640 | readOutputSectionDescription(Tok); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 641 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 642 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 645 | static int precedence(StringRef Op) { |
| 646 | return StringSwitch<int>(Op) |
| 647 | .Case("*", 4) |
| 648 | .Case("/", 4) |
| 649 | .Case("+", 3) |
| 650 | .Case("-", 3) |
| 651 | .Case("<", 2) |
| 652 | .Case(">", 2) |
| 653 | .Case(">=", 2) |
| 654 | .Case("<=", 2) |
| 655 | .Case("==", 2) |
| 656 | .Case("!=", 2) |
| 657 | .Case("&", 1) |
| 658 | .Default(-1); |
| 659 | } |
| 660 | |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 661 | void ScriptParser::readKeep(OutputSectionCommand *Cmd) { |
| 662 | expect("("); |
| 663 | expect("*"); |
| 664 | expect("("); |
| 665 | auto *InCmd = new InputSectionDescription(); |
| 666 | Cmd->Commands.emplace_back(InCmd); |
| 667 | while (!Error && !skip(")")) { |
| 668 | Opt.KeptSections.push_back(peek()); |
| 669 | InCmd->Patterns.push_back(next()); |
| 670 | } |
| 671 | expect(")"); |
| 672 | } |
| 673 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 674 | void ScriptParser::readOutputSectionDescription(StringRef OutSec) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 675 | OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); |
| 676 | Opt.Commands.emplace_back(Cmd); |
George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 677 | |
| 678 | // Read an address expression. |
| 679 | // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address |
| 680 | if (peek() != ":") |
| 681 | Cmd->AddrExpr = readExpr(); |
| 682 | |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 683 | expect(":"); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 684 | |
| 685 | // Parse constraints. |
| 686 | if (skip("ONLY_IF_RO")) |
Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 687 | Cmd->Constraint = ConstraintKind::ReadOnly; |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 688 | if (skip("ONLY_IF_RW")) |
Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 689 | Cmd->Constraint = ConstraintKind::ReadWrite; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 690 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 691 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 692 | while (!Error && !skip("}")) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 693 | StringRef Tok = next(); |
| 694 | if (Tok == "*") { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 695 | auto *InCmd = new InputSectionDescription(); |
| 696 | Cmd->Commands.emplace_back(InCmd); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 697 | expect("("); |
| 698 | while (!Error && !skip(")")) |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 699 | InCmd->Patterns.push_back(next()); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 700 | } else if (Tok == "KEEP") { |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 701 | readKeep(Cmd); |
Davide Italiano | 054a679 | 2016-07-24 23:13:48 +0000 | [diff] [blame] | 702 | } else if (Tok == "PROVIDE") { |
| 703 | readProvide(false); |
| 704 | } else if (Tok == "PROVIDE_HIDDEN") { |
| 705 | readProvide(true); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 706 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 707 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 708 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 709 | } |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 710 | Cmd->Phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 711 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 712 | StringRef Tok = peek(); |
| 713 | if (Tok.startswith("=")) { |
| 714 | if (!Tok.startswith("=0x")) { |
Rui Ueyama | 3ed2f06 | 2016-03-13 03:17:44 +0000 | [diff] [blame] | 715 | setError("filler should be a hexadecimal value"); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 718 | Tok = Tok.substr(3); |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 719 | Cmd->Filler = parseHex(Tok); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 720 | next(); |
| 721 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 724 | void ScriptParser::readProvide(bool Hidden) { |
| 725 | expect("("); |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 726 | if (SymbolAssignment *Assignment = readAssignment(next())) { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 727 | Assignment->Provide = true; |
| 728 | Assignment->Hidden = Hidden; |
| 729 | } |
| 730 | expect(")"); |
| 731 | expect(";"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 734 | SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 735 | expect("="); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 736 | Expr E = readExpr(); |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 737 | auto *Cmd = new SymbolAssignment(Name, E); |
| 738 | Opt.Commands.emplace_back(Cmd); |
| 739 | return Cmd; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 742 | // This is an operator-precedence parser to parse a linker |
| 743 | // script expression. |
| 744 | Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 745 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 746 | // This is a part of the operator-precedence parser. This function |
| 747 | // assumes that the remaining token stream starts with an operator. |
| 748 | Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { |
| 749 | while (!atEOF() && !Error) { |
| 750 | // Read an operator and an expression. |
| 751 | StringRef Op1 = peek(); |
| 752 | if (Op1 == "?") |
| 753 | return readTernary(Lhs); |
| 754 | if (precedence(Op1) < MinPrec) |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 755 | break; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 756 | next(); |
| 757 | Expr Rhs = readPrimary(); |
| 758 | |
| 759 | // Evaluate the remaining part of the expression first if the |
| 760 | // next operator has greater precedence than the previous one. |
| 761 | // For example, if we have read "+" and "3", and if the next |
| 762 | // operator is "*", then we'll evaluate 3 * ... part first. |
| 763 | while (!atEOF()) { |
| 764 | StringRef Op2 = peek(); |
| 765 | if (precedence(Op2) <= precedence(Op1)) |
| 766 | break; |
| 767 | Rhs = readExpr1(Rhs, precedence(Op2)); |
| 768 | } |
| 769 | |
| 770 | Lhs = combine(Op1, Lhs, Rhs); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 771 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 772 | return Lhs; |
| 773 | } |
| 774 | |
| 775 | uint64_t static getConstant(StringRef S) { |
| 776 | if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE") |
| 777 | return Target->PageSize; |
| 778 | error("unknown constant: " + S); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | Expr ScriptParser::readPrimary() { |
| 783 | StringRef Tok = next(); |
| 784 | |
| 785 | if (Tok == ".") |
| 786 | return [](uint64_t Dot) { return Dot; }; |
| 787 | |
| 788 | if (Tok == "(") { |
| 789 | Expr E = readExpr(); |
| 790 | expect(")"); |
| 791 | return E; |
| 792 | } |
| 793 | |
| 794 | // Built-in functions are parsed here. |
| 795 | // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. |
| 796 | if (Tok == "ALIGN") { |
| 797 | expect("("); |
| 798 | Expr E = readExpr(); |
| 799 | expect(")"); |
| 800 | return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; |
| 801 | } |
| 802 | if (Tok == "CONSTANT") { |
| 803 | expect("("); |
| 804 | StringRef Tok = next(); |
| 805 | expect(")"); |
| 806 | return [=](uint64_t Dot) { return getConstant(Tok); }; |
| 807 | } |
| 808 | if (Tok == "DATA_SEGMENT_ALIGN") { |
| 809 | expect("("); |
| 810 | Expr E = readExpr(); |
| 811 | expect(","); |
| 812 | readExpr(); |
| 813 | expect(")"); |
George Rimar | 4509a4f | 2016-07-26 17:01:18 +0000 | [diff] [blame] | 814 | return [=](uint64_t Dot) -> uint64_t { return alignTo(Dot, E(Dot)); }; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 815 | } |
| 816 | if (Tok == "DATA_SEGMENT_END") { |
| 817 | expect("("); |
| 818 | expect("."); |
| 819 | expect(")"); |
| 820 | return [](uint64_t Dot) { return Dot; }; |
| 821 | } |
George Rimar | 276b4e6 | 2016-07-26 17:58:44 +0000 | [diff] [blame^] | 822 | // GNU linkers implements more complicated logic to handle |
| 823 | // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to |
| 824 | // the next page boundary for simplicity. |
| 825 | if (Tok == "DATA_SEGMENT_RELRO_END") { |
| 826 | expect("("); |
| 827 | next(); |
| 828 | expect(","); |
| 829 | readExpr(); |
| 830 | expect(")"); |
| 831 | return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; |
| 832 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 833 | |
| 834 | // Parse a number literal |
| 835 | uint64_t V = 0; |
| 836 | if (Tok.getAsInteger(0, V)) |
| 837 | setError("malformed number: " + Tok); |
| 838 | return [=](uint64_t Dot) { return V; }; |
| 839 | } |
| 840 | |
| 841 | Expr ScriptParser::readTernary(Expr Cond) { |
| 842 | next(); |
| 843 | Expr L = readExpr(); |
| 844 | expect(":"); |
| 845 | Expr R = readExpr(); |
| 846 | return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; |
| 847 | } |
| 848 | |
| 849 | Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) { |
| 850 | if (Op == "*") |
| 851 | return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; |
| 852 | if (Op == "/") { |
| 853 | return [=](uint64_t Dot) -> uint64_t { |
| 854 | uint64_t RHS = R(Dot); |
| 855 | if (RHS == 0) { |
| 856 | error("division by zero"); |
| 857 | return 0; |
| 858 | } |
| 859 | return L(Dot) / RHS; |
| 860 | }; |
| 861 | } |
| 862 | if (Op == "+") |
| 863 | return [=](uint64_t Dot) { return L(Dot) + R(Dot); }; |
| 864 | if (Op == "-") |
| 865 | return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; |
| 866 | if (Op == "<") |
| 867 | return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; |
| 868 | if (Op == ">") |
| 869 | return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; |
| 870 | if (Op == ">=") |
| 871 | return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; |
| 872 | if (Op == "<=") |
| 873 | return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; |
| 874 | if (Op == "==") |
| 875 | return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; |
| 876 | if (Op == "!=") |
| 877 | return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; |
| 878 | if (Op == "&") |
| 879 | return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; |
| 880 | llvm_unreachable("invalid operator"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 883 | std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { |
| 884 | std::vector<StringRef> Phdrs; |
| 885 | while (!Error && peek().startswith(":")) { |
| 886 | StringRef Tok = next(); |
| 887 | Tok = (Tok.size() == 1) ? next() : Tok.substr(1); |
| 888 | if (Tok.empty()) { |
| 889 | setError("section header name is empty"); |
| 890 | break; |
| 891 | } |
Rui Ueyama | 047404f | 2016-07-20 19:36:36 +0000 | [diff] [blame] | 892 | Phdrs.push_back(Tok); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 893 | } |
| 894 | return Phdrs; |
| 895 | } |
| 896 | |
| 897 | unsigned ScriptParser::readPhdrType() { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 898 | StringRef Tok = next(); |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 899 | unsigned Ret = StringSwitch<unsigned>(Tok) |
| 900 | .Case("PT_NULL", PT_NULL) |
| 901 | .Case("PT_LOAD", PT_LOAD) |
| 902 | .Case("PT_DYNAMIC", PT_DYNAMIC) |
| 903 | .Case("PT_INTERP", PT_INTERP) |
| 904 | .Case("PT_NOTE", PT_NOTE) |
| 905 | .Case("PT_SHLIB", PT_SHLIB) |
| 906 | .Case("PT_PHDR", PT_PHDR) |
| 907 | .Case("PT_TLS", PT_TLS) |
| 908 | .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) |
| 909 | .Case("PT_GNU_STACK", PT_GNU_STACK) |
| 910 | .Case("PT_GNU_RELRO", PT_GNU_RELRO) |
| 911 | .Default(-1); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 912 | |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 913 | if (Ret == (unsigned)-1) { |
| 914 | setError("invalid program header type: " + Tok); |
| 915 | return PT_NULL; |
| 916 | } |
| 917 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 920 | static bool isUnderSysroot(StringRef Path) { |
| 921 | if (Config->Sysroot == "") |
| 922 | return false; |
| 923 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 924 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 925 | return true; |
| 926 | return false; |
| 927 | } |
| 928 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 929 | // Entry point. |
| 930 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 931 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 932 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 933 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 934 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 935 | template class elf::LinkerScript<ELF32LE>; |
| 936 | template class elf::LinkerScript<ELF32BE>; |
| 937 | template class elf::LinkerScript<ELF64LE>; |
| 938 | template class elf::LinkerScript<ELF64BE>; |