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