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