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