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