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