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