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