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