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