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