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