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