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