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