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