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