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