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