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