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