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