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; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 41 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 42 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 43 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 44 | ScriptConfiguration *elf::ScriptConfig; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 45 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 46 | bool SymbolAssignment::classof(const BaseCommand *C) { |
| 47 | return C->Kind == AssignmentKind; |
| 48 | } |
| 49 | |
| 50 | bool OutputSectionCommand::classof(const BaseCommand *C) { |
| 51 | return C->Kind == OutputSectionKind; |
| 52 | } |
| 53 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 54 | bool InputSectionDescription::classof(const BaseCommand *C) { |
| 55 | return C->Kind == InputSectionKind; |
| 56 | } |
| 57 | |
Rui Ueyama | 36a153c | 2016-07-23 14:09:58 +0000 | [diff] [blame] | 58 | template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 59 | return !S || !S->Live; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 62 | template <class ELFT> |
| 63 | bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 64 | for (StringRef Pat : Opt.KeptSections) |
Rui Ueyama | 722830a | 2016-06-29 05:32:09 +0000 | [diff] [blame] | 65 | if (globMatch(Pat, S->getSectionName())) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 66 | return true; |
| 67 | return false; |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Rui Ueyama | 63dc650 | 2016-07-25 22:41:42 +0000 | [diff] [blame] | 70 | static bool match(ArrayRef<StringRef> Patterns, StringRef S) { |
| 71 | for (StringRef Pat : Patterns) |
| 72 | if (globMatch(Pat, S)) |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 73 | return true; |
| 74 | return false; |
| 75 | } |
| 76 | |
George Rimar | eaee2af | 2016-07-29 15:07:11 +0000 | [diff] [blame] | 77 | // Create a vector of (<output section name>, <input section description>). |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 78 | template <class ELFT> |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 79 | std::vector<std::pair<StringRef, const InputSectionDescription *>> |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 80 | LinkerScript<ELFT>::getSectionMap() { |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 81 | std::vector<std::pair<StringRef, const InputSectionDescription *>> Ret; |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 82 | |
| 83 | for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) |
| 84 | if (auto *Cmd1 = dyn_cast<OutputSectionCommand>(Base1.get())) |
| 85 | for (const std::unique_ptr<BaseCommand> &Base2 : Cmd1->Commands) |
| 86 | if (auto *Cmd2 = dyn_cast<InputSectionDescription>(Base2.get())) |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 87 | Ret.emplace_back(Cmd1->Name, Cmd2); |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 88 | |
| 89 | return Ret; |
| 90 | } |
| 91 | |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 92 | static bool fileMatches(const InputSectionDescription *Desc, |
| 93 | StringRef Filename) { |
| 94 | if (!globMatch(Desc->FilePattern, Filename)) |
| 95 | return false; |
| 96 | return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename); |
| 97 | } |
| 98 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 99 | // Returns input sections filtered by given glob patterns. |
| 100 | template <class ELFT> |
| 101 | std::vector<InputSectionBase<ELFT> *> |
Rui Ueyama | ad10c3d | 2016-07-28 21:05:04 +0000 | [diff] [blame] | 102 | LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) { |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 103 | ArrayRef<StringRef> Patterns = I->SectionPatterns; |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 104 | std::vector<InputSectionBase<ELFT> *> Ret; |
| 105 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 106 | Symtab<ELFT>::X->getObjectFiles()) { |
| 107 | if (fileMatches(I, sys::path::filename(F->getName()))) |
| 108 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 109 | if (!isDiscarded(S) && !S->OutSec && |
| 110 | match(Patterns, S->getSectionName())) |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 111 | Ret.push_back(S); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 112 | } |
Eugene Leviant | 3e6b027 | 2016-07-28 19:24:13 +0000 | [diff] [blame] | 113 | |
| 114 | if ((llvm::find(Patterns, "COMMON") != Patterns.end())) |
Rui Ueyama | ad10c3d | 2016-07-28 21:05:04 +0000 | [diff] [blame] | 115 | Ret.push_back(CommonInputSection<ELFT>::X); |
Eugene Leviant | 3e6b027 | 2016-07-28 19:24:13 +0000 | [diff] [blame] | 116 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 117 | return Ret; |
| 118 | } |
| 119 | |
George Rimar | c3cb884 | 2016-07-29 15:12:48 +0000 | [diff] [blame] | 120 | // Add input section to output section. If there is no output section yet, |
| 121 | // then create it and add to output section list. |
| 122 | template <class ELFT> |
| 123 | static void addSection(OutputSectionFactory<ELFT> &Factory, |
| 124 | std::vector<OutputSectionBase<ELFT> *> &Out, |
| 125 | InputSectionBase<ELFT> *C, StringRef Name) { |
| 126 | OutputSectionBase<ELFT> *Sec; |
| 127 | bool IsNew; |
| 128 | std::tie(Sec, IsNew) = Factory.create(C, Name); |
| 129 | if (IsNew) |
| 130 | Out.push_back(Sec); |
| 131 | Sec->addSection(C); |
| 132 | } |
| 133 | |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 134 | template <class ELFT> struct SectionsSorter { |
| 135 | SectionsSorter(SortKind Kind) : Kind(Kind) {} |
| 136 | bool operator()(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) { |
| 137 | int AlignmentCmp = A->Alignment - B->Alignment; |
| 138 | if (Kind == SortKind::Align || (Kind == SortKind::AlignName && AlignmentCmp != 0)) |
| 139 | return AlignmentCmp < 0; |
| 140 | |
| 141 | int NameCmp = A->getSectionName().compare(B->getSectionName()); |
| 142 | if (Kind == SortKind::Name || (Kind == SortKind::NameAlign && NameCmp != 0)) |
| 143 | return NameCmp < 0; |
| 144 | |
| 145 | if (Kind == SortKind::NameAlign) |
| 146 | return AlignmentCmp < 0; |
| 147 | if (Kind == SortKind::AlignName) |
| 148 | return NameCmp < 0; |
| 149 | |
| 150 | llvm_unreachable("unknown section sort kind in predicate"); |
| 151 | return false; |
| 152 | } |
| 153 | SortKind Kind; |
| 154 | }; |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 155 | |
| 156 | template <class ELFT> |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 157 | void LinkerScript<ELFT>::createSections( |
| 158 | std::vector<OutputSectionBase<ELFT> *> *Out, |
| 159 | OutputSectionFactory<ELFT> &Factory) { |
| 160 | OutputSections = Out; |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 161 | |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 162 | for (auto &P : getSectionMap()) { |
| 163 | StringRef OutputName = P.first; |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 164 | const InputSectionDescription *Cmd = P.second; |
| 165 | std::vector<InputSectionBase<ELFT> *> Sections = getInputSections(Cmd); |
| 166 | |
| 167 | if (OutputName == "/DISCARD/") { |
| 168 | for (InputSectionBase<ELFT> *S : Sections) { |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 169 | S->Live = false; |
| 170 | reportDiscarded(S); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 171 | } |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 172 | continue; |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 173 | } |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 174 | |
| 175 | if (Cmd->Sort != SortKind::None) |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 176 | std::stable_sort(Sections.begin(), Sections.end(), |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 177 | SectionsSorter<ELFT>(Cmd->Sort)); |
| 178 | |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 179 | for (InputSectionBase<ELFT> *S : Sections) |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 180 | addSection(Factory, *Out, S, OutputName); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 181 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 182 | |
| 183 | // Add all other input sections, which are not listed in script. |
Rui Ueyama | 6b27481 | 2016-07-25 22:51:07 +0000 | [diff] [blame] | 184 | for (const std::unique_ptr<ObjectFile<ELFT>> &F : |
| 185 | Symtab<ELFT>::X->getObjectFiles()) |
| 186 | for (InputSectionBase<ELFT> *S : F->getSections()) |
| 187 | if (!isDiscarded(S) && !S->OutSec) |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 188 | addSection(Factory, *Out, S, getOutputSectionName(S)); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 189 | |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 190 | // Remove from the output all the sections which did not meet |
| 191 | // the optional constraints. |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 192 | filter(); |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Process ONLY_IF_RO and ONLY_IF_RW. |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 196 | template <class ELFT> void LinkerScript<ELFT>::filter() { |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 197 | // In this loop, we remove output sections if they don't satisfy |
| 198 | // requested properties. |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 199 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 200 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 201 | if (!Cmd || Cmd->Name == "/DISCARD/") |
| 202 | continue; |
| 203 | |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 204 | if (Cmd->Constraint == ConstraintKind::NoConstraint) |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 205 | continue; |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 206 | |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 207 | auto It = llvm::find_if(*OutputSections, [&](OutputSectionBase<ELFT> *S) { |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 208 | return S->getName() == Cmd->Name; |
| 209 | }); |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 210 | if (It == OutputSections->end()) |
George Rimar | bfc4a4b | 2016-07-26 10:47:09 +0000 | [diff] [blame] | 211 | continue; |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 212 | |
| 213 | OutputSectionBase<ELFT> *Sec = *It; |
| 214 | bool Writable = (Sec->getFlags() & SHF_WRITE); |
| 215 | bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly); |
| 216 | bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite); |
| 217 | |
Rui Ueyama | ed94271 | 2016-07-29 06:21:06 +0000 | [diff] [blame] | 218 | if ((RO && Writable) || (RW && !Writable)) |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 219 | OutputSections->erase(It); |
Rui Ueyama | 3c291e1 | 2016-07-25 21:30:00 +0000 | [diff] [blame] | 220 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Nico Weber | 2e36772 | 2016-08-03 14:37:57 +0000 | [diff] [blame] | 223 | template <class ELFT> |
| 224 | void LinkerScript<ELFT>::assignAddresses( |
| 225 | ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 226 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 227 | // are not explicitly placed into the output file by the linker script. |
| 228 | // We place orphan sections at end of file. |
| 229 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 230 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 231 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 232 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 233 | if (getSectionIndex(Name) == INT_MAX) |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 234 | Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 235 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 236 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 237 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 238 | Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 239 | uintX_t MinVA = std::numeric_limits<uintX_t>::max(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 240 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 241 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 242 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 243 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { |
Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 244 | if (Cmd->Name == ".") { |
| 245 | Dot = Cmd->Expression(Dot); |
| 246 | } else if (Cmd->Sym) { |
| 247 | cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot); |
| 248 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 252 | // Find all the sections with required name. There can be more than |
George Rimar | 6ad330a | 2016-07-19 07:39:07 +0000 | [diff] [blame] | 253 | // one section with such name, if the alignment, flags or type |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 254 | // attribute differs. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 255 | auto *Cmd = cast<OutputSectionCommand>(Base.get()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 256 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 257 | if (Sec->getName() != Cmd->Name) |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 258 | continue; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 259 | |
George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 260 | if (Cmd->AddrExpr) |
| 261 | Dot = Cmd->AddrExpr(Dot); |
| 262 | |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 263 | if (Cmd->AlignExpr) |
| 264 | Sec->updateAlignment(Cmd->AlignExpr(Dot)); |
| 265 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 266 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 267 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 268 | TVA = alignTo(TVA, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 269 | Sec->setVA(TVA); |
| 270 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
| 271 | continue; |
| 272 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 273 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 274 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 275 | Dot = alignTo(Dot, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 276 | Sec->setVA(Dot); |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 277 | MinVA = std::min(MinVA, Dot); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 278 | Dot += Sec->getSize(); |
| 279 | continue; |
| 280 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 283 | |
Rafael Espindola | 64c32d6 | 2016-07-07 14:28:47 +0000 | [diff] [blame] | 284 | // ELF and Program headers need to be right before the first section in |
George Rimar | b91e711 | 2016-07-19 07:42:07 +0000 | [diff] [blame] | 285 | // memory. Set their addresses accordingly. |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 286 | MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - |
| 287 | Out<ELFT>::ProgramHeaders->getSize(), |
| 288 | Target->PageSize); |
| 289 | Out<ELFT>::ElfHeader->setVA(MinVA); |
| 290 | Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 293 | template <class ELFT> |
Nico Weber | 2e36772 | 2016-08-03 14:37:57 +0000 | [diff] [blame] | 294 | std::vector<PhdrEntry<ELFT>> |
| 295 | LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 296 | std::vector<PhdrEntry<ELFT>> Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 297 | |
| 298 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 299 | Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
| 300 | PhdrEntry<ELFT> &Phdr = Ret.back(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 301 | |
| 302 | if (Cmd.HasFilehdr) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 303 | Phdr.add(Out<ELFT>::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 304 | if (Cmd.HasPhdrs) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 305 | Phdr.add(Out<ELFT>::ProgramHeaders); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 306 | |
| 307 | switch (Cmd.Type) { |
| 308 | case PT_INTERP: |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 309 | if (Out<ELFT>::Interp) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 310 | Phdr.add(Out<ELFT>::Interp); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 311 | break; |
| 312 | case PT_DYNAMIC: |
| 313 | if (isOutputDynamic<ELFT>()) { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 314 | Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags(); |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 315 | Phdr.add(Out<ELFT>::Dynamic); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 316 | } |
| 317 | break; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 318 | case PT_GNU_EH_FRAME: |
| 319 | if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 320 | Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags(); |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 321 | Phdr.add(Out<ELFT>::EhFrameHdr); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 322 | } |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 327 | PhdrEntry<ELFT> *Load = nullptr; |
| 328 | uintX_t Flags = PF_R; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 329 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
| 330 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 331 | break; |
| 332 | |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 333 | std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName()); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 334 | if (!PhdrIds.empty()) { |
| 335 | // Assign headers specified by linker script |
| 336 | for (size_t Id : PhdrIds) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 337 | Ret[Id].add(Sec); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 338 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 339 | Ret[Id].H.p_flags |= Sec->getPhdrFlags(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 340 | } |
| 341 | } else { |
| 342 | // If we have no load segment or flags've changed then we want new load |
| 343 | // segment. |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 344 | uintX_t NewFlags = Sec->getPhdrFlags(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 345 | if (Load == nullptr || Flags != NewFlags) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 346 | Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 347 | Flags = NewFlags; |
| 348 | } |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 349 | Load->add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 350 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 351 | } |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 352 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 356 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 357 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) |
| 358 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 359 | if (Cmd->Name == Name) |
| 360 | return Cmd->Filler; |
| 361 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 364 | // Returns the index of the given section name in linker script |
| 365 | // SECTIONS commands. Sections are laid out as the same order as they |
| 366 | // were in the script. If a given name did not appear in the script, |
| 367 | // 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] | 368 | template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
Rui Ueyama | f510fa6 | 2016-07-26 00:21:15 +0000 | [diff] [blame] | 369 | int I = 0; |
| 370 | for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 371 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 372 | if (Cmd->Name == Name) |
| 373 | return I; |
| 374 | ++I; |
| 375 | } |
| 376 | return INT_MAX; |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // A compartor to sort output sections. Returns -1 or 1 if |
| 380 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 381 | template <class ELFT> |
| 382 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 383 | int I = getSectionIndex(A); |
| 384 | int J = getSectionIndex(B); |
| 385 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 386 | return 0; |
| 387 | return I < J ? -1 : 1; |
| 388 | } |
| 389 | |
Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 390 | // Add symbols defined by linker scripts. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 391 | template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 392 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 393 | auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()); |
| 394 | if (!Cmd || Cmd->Name == ".") |
| 395 | continue; |
| 396 | |
Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 397 | // If a symbol was in PROVIDE(), define it only when it is an |
| 398 | // undefined symbol. |
Davide Italiano | 8ab4108 | 2016-07-23 22:09:04 +0000 | [diff] [blame] | 399 | SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); |
Rui Ueyama | 8d083e6 | 2016-07-29 05:48:39 +0000 | [diff] [blame] | 400 | if (Cmd->Provide && !(B && B->isUndefined())) |
| 401 | continue; |
| 402 | |
| 403 | // Define an absolute symbol. The symbol value will be assigned later. |
| 404 | // (At this point, we don't know the final address yet.) |
| 405 | Symbol *Sym = Symtab<ELFT>::X->addUndefined(Cmd->Name); |
| 406 | replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, STV_DEFAULT); |
| 407 | Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; |
| 408 | Cmd->Sym = Sym->body(); |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 409 | } |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 412 | template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { |
| 413 | return !Opt.PhdrsCommands.empty(); |
| 414 | } |
| 415 | |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 416 | template <class ELFT> |
| 417 | typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) { |
| 418 | for (OutputSectionBase<ELFT> *Sec : *OutputSections) |
| 419 | if (Sec->getName() == Name) |
| 420 | return Sec->getSize(); |
| 421 | error("undefined section " + Name); |
| 422 | return 0; |
| 423 | } |
| 424 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 425 | // Returns indices of ELF headers containing specific section, identified |
| 426 | // by Name. Each index is a zero based number of ELF header listed within |
| 427 | // PHDRS {} script block. |
| 428 | template <class ELFT> |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 429 | std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 430 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 431 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 432 | if (!Cmd || Cmd->Name != SectionName) |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 433 | continue; |
| 434 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 435 | std::vector<size_t> Ret; |
| 436 | for (StringRef PhdrName : Cmd->Phdrs) |
| 437 | Ret.push_back(getPhdrIndex(PhdrName)); |
| 438 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 439 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 440 | return {}; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 443 | template <class ELFT> |
| 444 | size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) { |
| 445 | size_t I = 0; |
| 446 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
| 447 | if (Cmd.Name == PhdrName) |
| 448 | return I; |
| 449 | ++I; |
| 450 | } |
| 451 | error("section header '" + PhdrName + "' is not listed in PHDRS"); |
| 452 | return 0; |
| 453 | } |
| 454 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 455 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 456 | typedef void (ScriptParser::*Handler)(); |
| 457 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 458 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 459 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 460 | |
Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 461 | void run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 462 | |
| 463 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 464 | void addFile(StringRef Path); |
| 465 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 466 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 467 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 468 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 469 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 470 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 471 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 472 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 473 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 474 | void readOutputFormat(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 475 | void readPhdrs(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 476 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 477 | void readSections(); |
| 478 | |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 479 | SymbolAssignment *readAssignment(StringRef Name); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 480 | void readOutputSectionDescription(StringRef OutSec); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 481 | std::vector<uint8_t> readOutputSectionFiller(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 482 | std::vector<StringRef> readOutputSectionPhdrs(); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 483 | std::unique_ptr<InputSectionDescription> readInputSectionDescription(); |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 484 | void readInputFilePattern(InputSectionDescription *InCmd, bool Keep); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 485 | void readInputSectionRules(InputSectionDescription *InCmd, bool Keep); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 486 | unsigned readPhdrType(); |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 487 | void readProvide(bool Hidden); |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 488 | void readAlign(OutputSectionCommand *Cmd); |
George Rimar | 03fc010 | 2016-07-28 07:18:23 +0000 | [diff] [blame] | 489 | void readSort(); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 490 | |
| 491 | Expr readExpr(); |
| 492 | Expr readExpr1(Expr Lhs, int MinPrec); |
| 493 | Expr readPrimary(); |
| 494 | Expr readTernary(Expr Cond); |
| 495 | Expr combine(StringRef Op, Expr Lhs, Expr Rhs); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 496 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 497 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 498 | ScriptConfiguration &Opt = *ScriptConfig; |
| 499 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 500 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 501 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 502 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 503 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 504 | {"ENTRY", &ScriptParser::readEntry}, |
| 505 | {"EXTERN", &ScriptParser::readExtern}, |
| 506 | {"GROUP", &ScriptParser::readGroup}, |
| 507 | {"INCLUDE", &ScriptParser::readInclude}, |
| 508 | {"INPUT", &ScriptParser::readGroup}, |
| 509 | {"OUTPUT", &ScriptParser::readOutput}, |
| 510 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 511 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 512 | {"PHDRS", &ScriptParser::readPhdrs}, |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 513 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 514 | {"SECTIONS", &ScriptParser::readSections}, |
| 515 | {";", &ScriptParser::readNothing}}; |
| 516 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 517 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 518 | while (!atEOF()) { |
| 519 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 520 | if (Handler Fn = Cmd.lookup(Tok)) |
| 521 | (this->*Fn)(); |
| 522 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 523 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 527 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 528 | if (IsUnderSysroot && S.startswith("/")) { |
| 529 | SmallString<128> Path; |
| 530 | (Config->Sysroot + S).toStringRef(Path); |
| 531 | if (sys::fs::exists(Path)) { |
| 532 | Driver->addFile(Saver.save(Path.str())); |
| 533 | return; |
| 534 | } |
| 535 | } |
| 536 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 537 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 538 | Driver->addFile(S); |
| 539 | } else if (S.startswith("=")) { |
| 540 | if (Config->Sysroot.empty()) |
| 541 | Driver->addFile(S.substr(1)); |
| 542 | else |
| 543 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 544 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 545 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 546 | } else if (sys::fs::exists(S)) { |
| 547 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 548 | } else { |
| 549 | std::string Path = findFromSearchPaths(S); |
| 550 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 551 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 552 | else |
| 553 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 557 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 558 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 559 | bool Orig = Config->AsNeeded; |
| 560 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 561 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 562 | StringRef Tok = next(); |
| 563 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 564 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 565 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 566 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 567 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 570 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 571 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 572 | expect("("); |
| 573 | StringRef Tok = next(); |
| 574 | if (Config->Entry.empty()) |
| 575 | Config->Entry = Tok; |
| 576 | expect(")"); |
| 577 | } |
| 578 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 579 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 580 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 581 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 582 | StringRef Tok = next(); |
| 583 | if (Tok == ")") |
| 584 | return; |
| 585 | Config->Undefined.push_back(Tok); |
| 586 | } |
| 587 | } |
| 588 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 589 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 590 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 591 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 592 | StringRef Tok = next(); |
| 593 | if (Tok == ")") |
| 594 | return; |
| 595 | if (Tok == "AS_NEEDED") { |
| 596 | readAsNeeded(); |
| 597 | continue; |
| 598 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 599 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 603 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 604 | StringRef Tok = next(); |
| 605 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 606 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 607 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 608 | return; |
| 609 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 610 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 611 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 612 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 613 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 616 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 617 | // -o <file> takes predecence over OUTPUT(<file>). |
| 618 | expect("("); |
| 619 | StringRef Tok = next(); |
| 620 | if (Config->OutputFile.empty()) |
| 621 | Config->OutputFile = Tok; |
| 622 | expect(")"); |
| 623 | } |
| 624 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 625 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 626 | // Error checking only for now. |
| 627 | expect("("); |
| 628 | next(); |
| 629 | expect(")"); |
| 630 | } |
| 631 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 632 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 633 | // Error checking only for now. |
| 634 | expect("("); |
| 635 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 636 | StringRef Tok = next(); |
| 637 | if (Tok == ")") |
| 638 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 639 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 640 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 641 | return; |
| 642 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 643 | next(); |
| 644 | expect(","); |
| 645 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 646 | expect(")"); |
| 647 | } |
| 648 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 649 | void ScriptParser::readPhdrs() { |
| 650 | expect("{"); |
| 651 | while (!Error && !skip("}")) { |
| 652 | StringRef Tok = next(); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 653 | Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 654 | PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); |
| 655 | |
| 656 | PhdrCmd.Type = readPhdrType(); |
| 657 | do { |
| 658 | Tok = next(); |
| 659 | if (Tok == ";") |
| 660 | break; |
| 661 | if (Tok == "FILEHDR") |
| 662 | PhdrCmd.HasFilehdr = true; |
| 663 | else if (Tok == "PHDRS") |
| 664 | PhdrCmd.HasPhdrs = true; |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 665 | else if (Tok == "FLAGS") { |
| 666 | expect("("); |
Rafael Espindola | eb685cd | 2016-08-02 22:14:57 +0000 | [diff] [blame] | 667 | // Passing 0 for the value of dot is a bit of a hack. It means that |
| 668 | // we accept expressions like ".|1". |
| 669 | PhdrCmd.Flags = readExpr()(0); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 670 | expect(")"); |
| 671 | } else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 672 | setError("unexpected header attribute: " + Tok); |
| 673 | } while (!Error); |
| 674 | } |
| 675 | } |
| 676 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 677 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 678 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 679 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 680 | expect(")"); |
| 681 | } |
| 682 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 683 | void ScriptParser::readSections() { |
Rui Ueyama | 3de0a33 | 2016-07-29 03:31:09 +0000 | [diff] [blame] | 684 | Opt.HasContents = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 685 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 686 | while (!Error && !skip("}")) { |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 687 | StringRef Tok = next(); |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 688 | if (peek() == "=" || peek() == "+=") { |
Rui Ueyama | 113cdec | 2016-07-24 23:05:57 +0000 | [diff] [blame] | 689 | readAssignment(Tok); |
| 690 | expect(";"); |
| 691 | } else if (Tok == "PROVIDE") { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 692 | readProvide(false); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 693 | } else if (Tok == "PROVIDE_HIDDEN") { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 694 | readProvide(true); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 695 | } else { |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 696 | readOutputSectionDescription(Tok); |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 697 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 698 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 701 | static int precedence(StringRef Op) { |
| 702 | return StringSwitch<int>(Op) |
| 703 | .Case("*", 4) |
| 704 | .Case("/", 4) |
| 705 | .Case("+", 3) |
| 706 | .Case("-", 3) |
| 707 | .Case("<", 2) |
| 708 | .Case(">", 2) |
| 709 | .Case(">=", 2) |
| 710 | .Case("<=", 2) |
| 711 | .Case("==", 2) |
| 712 | .Case("!=", 2) |
| 713 | .Case("&", 1) |
| 714 | .Default(-1); |
| 715 | } |
| 716 | |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 717 | void ScriptParser::readInputFilePattern(InputSectionDescription *InCmd, |
| 718 | bool Keep) { |
| 719 | while (!Error && !skip(")")) { |
| 720 | if (Keep) |
| 721 | Opt.KeptSections.push_back(peek()); |
| 722 | InCmd->SectionPatterns.push_back(next()); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | void ScriptParser::readInputSectionRules(InputSectionDescription *InCmd, |
| 727 | bool Keep) { |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 728 | InCmd->FilePattern = next(); |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 729 | expect("("); |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 730 | |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 731 | if (skip("EXCLUDE_FILE")) { |
| 732 | expect("("); |
| 733 | while (!Error && !skip(")")) |
| 734 | InCmd->ExcludedFiles.push_back(next()); |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 735 | } |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 736 | |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 737 | if (skip("SORT") || skip("SORT_BY_NAME")) { |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 738 | expect("("); |
George Rimar | 350ece4 | 2016-08-03 08:35:59 +0000 | [diff] [blame] | 739 | if (skip("SORT_BY_ALIGNMENT")) { |
| 740 | InCmd->Sort = SortKind::NameAlign; |
| 741 | expect("("); |
| 742 | readInputFilePattern(InCmd, Keep); |
| 743 | expect(")"); |
| 744 | } else { |
| 745 | InCmd->Sort = SortKind::Name; |
| 746 | readInputFilePattern(InCmd, Keep); |
| 747 | } |
| 748 | expect(")"); |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | if (skip("SORT_BY_ALIGNMENT")) { |
| 753 | expect("("); |
| 754 | if (skip("SORT") || skip("SORT_BY_NAME")) { |
| 755 | InCmd->Sort = SortKind::AlignName; |
| 756 | expect("("); |
| 757 | readInputFilePattern(InCmd, Keep); |
| 758 | expect(")"); |
| 759 | } else { |
| 760 | InCmd->Sort = SortKind::Align; |
| 761 | readInputFilePattern(InCmd, Keep); |
| 762 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 763 | expect(")"); |
| 764 | return; |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 765 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 766 | |
| 767 | readInputFilePattern(InCmd, Keep); |
Davide Italiano | e728279 | 2016-07-27 01:44:01 +0000 | [diff] [blame] | 768 | } |
| 769 | |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 770 | std::unique_ptr<InputSectionDescription> |
| 771 | ScriptParser::readInputSectionDescription() { |
George Rimar | 352eac3 | 2016-07-28 22:10:50 +0000 | [diff] [blame] | 772 | auto InCmd = llvm::make_unique<InputSectionDescription>(); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 773 | |
| 774 | // Input section wildcard can be surrounded by KEEP. |
| 775 | // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep |
| 776 | if (skip("KEEP")) { |
| 777 | expect("("); |
| 778 | readInputSectionRules(InCmd.get(), true); |
| 779 | expect(")"); |
| 780 | } else { |
| 781 | readInputSectionRules(InCmd.get(), false); |
| 782 | } |
| 783 | |
| 784 | return InCmd; |
Davide Italiano | 0ed42b0 | 2016-07-25 21:47:13 +0000 | [diff] [blame] | 785 | } |
| 786 | |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 787 | void ScriptParser::readAlign(OutputSectionCommand *Cmd) { |
| 788 | expect("("); |
| 789 | Cmd->AlignExpr = readExpr(); |
| 790 | expect(")"); |
| 791 | } |
| 792 | |
George Rimar | 03fc010 | 2016-07-28 07:18:23 +0000 | [diff] [blame] | 793 | void ScriptParser::readSort() { |
| 794 | expect("("); |
| 795 | expect("CONSTRUCTORS"); |
| 796 | expect(")"); |
| 797 | } |
| 798 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 799 | void ScriptParser::readOutputSectionDescription(StringRef OutSec) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 800 | OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); |
| 801 | Opt.Commands.emplace_back(Cmd); |
George Rimar | 58e5c4d | 2016-07-25 08:29:46 +0000 | [diff] [blame] | 802 | |
| 803 | // Read an address expression. |
| 804 | // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address |
| 805 | if (peek() != ":") |
| 806 | Cmd->AddrExpr = readExpr(); |
| 807 | |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 808 | expect(":"); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 809 | |
George Rimar | 630c617 | 2016-07-26 18:06:29 +0000 | [diff] [blame] | 810 | if (skip("ALIGN")) |
| 811 | readAlign(Cmd); |
| 812 | |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 813 | // Parse constraints. |
| 814 | if (skip("ONLY_IF_RO")) |
Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 815 | Cmd->Constraint = ConstraintKind::ReadOnly; |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 816 | if (skip("ONLY_IF_RW")) |
Rui Ueyama | efc4066 | 2016-07-25 22:00:10 +0000 | [diff] [blame] | 817 | Cmd->Constraint = ConstraintKind::ReadWrite; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 818 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 819 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 820 | while (!Error && !skip("}")) { |
George Rimar | f586ff7 | 2016-07-28 22:15:44 +0000 | [diff] [blame] | 821 | if (peek().startswith("*") || peek() == "KEEP") { |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 822 | Cmd->Commands.push_back(readInputSectionDescription()); |
| 823 | continue; |
| 824 | } |
| 825 | |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 826 | StringRef Tok = next(); |
George Rimar | 0659800 | 2016-07-28 21:51:30 +0000 | [diff] [blame] | 827 | if (Tok == "PROVIDE") { |
Davide Italiano | 054a679 | 2016-07-24 23:13:48 +0000 | [diff] [blame] | 828 | readProvide(false); |
| 829 | } else if (Tok == "PROVIDE_HIDDEN") { |
| 830 | readProvide(true); |
George Rimar | 03fc010 | 2016-07-28 07:18:23 +0000 | [diff] [blame] | 831 | } else if (Tok == "SORT") { |
| 832 | readSort(); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 833 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 834 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 835 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 836 | } |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 837 | Cmd->Phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 838 | Cmd->Filler = readOutputSectionFiller(); |
| 839 | } |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 840 | |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 841 | std::vector<uint8_t> ScriptParser::readOutputSectionFiller() { |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 842 | StringRef Tok = peek(); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 843 | if (!Tok.startswith("=")) |
| 844 | return {}; |
Davide Italiano | 5ac0d7c | 2016-07-29 22:21:28 +0000 | [diff] [blame] | 845 | next(); |
Rui Ueyama | 965827d | 2016-08-03 23:25:15 +0000 | [diff] [blame^] | 846 | |
| 847 | // Read a hexstring of arbitrary length. |
Davide Italiano | 5ac0d7c | 2016-07-29 22:21:28 +0000 | [diff] [blame] | 848 | if (Tok.startswith("=0x")) |
| 849 | return parseHex(Tok.substr(3)); |
| 850 | |
Rui Ueyama | 965827d | 2016-08-03 23:25:15 +0000 | [diff] [blame^] | 851 | // Read a decimal or octal value as a big-endian 32 bit value. |
| 852 | // Why do this? I don't know, but that's what gold does. |
| 853 | uint32_t V; |
| 854 | if (Tok.substr(1).getAsInteger(0, V)) { |
| 855 | setError("invalid filler expression: " + Tok); |
Rui Ueyama | f71caa2 | 2016-07-29 06:14:07 +0000 | [diff] [blame] | 856 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 857 | } |
Rui Ueyama | 965827d | 2016-08-03 23:25:15 +0000 | [diff] [blame^] | 858 | 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] | 859 | } |
| 860 | |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 861 | void ScriptParser::readProvide(bool Hidden) { |
| 862 | expect("("); |
Rui Ueyama | 174e0a1 | 2016-07-29 00:29:25 +0000 | [diff] [blame] | 863 | SymbolAssignment *Cmd = readAssignment(next()); |
| 864 | Cmd->Provide = true; |
| 865 | Cmd->Hidden = Hidden; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 866 | expect(")"); |
| 867 | expect(";"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 868 | } |
| 869 | |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 870 | static uint64_t getSymbolValue(StringRef S, uint64_t Dot) { |
| 871 | if (S == ".") |
| 872 | return Dot; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 873 | |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 874 | switch (Config->EKind) { |
| 875 | case ELF32LEKind: |
| 876 | if (SymbolBody *B = Symtab<ELF32LE>::X->find(S)) |
| 877 | return B->getVA<ELF32LE>(); |
| 878 | break; |
| 879 | case ELF32BEKind: |
| 880 | if (SymbolBody *B = Symtab<ELF32BE>::X->find(S)) |
| 881 | return B->getVA<ELF32BE>(); |
| 882 | break; |
| 883 | case ELF64LEKind: |
| 884 | if (SymbolBody *B = Symtab<ELF64LE>::X->find(S)) |
| 885 | return B->getVA<ELF64LE>(); |
| 886 | break; |
| 887 | case ELF64BEKind: |
| 888 | if (SymbolBody *B = Symtab<ELF64BE>::X->find(S)) |
| 889 | return B->getVA<ELF64BE>(); |
| 890 | break; |
George Rimar | 6930a6d | 2016-07-26 18:41:06 +0000 | [diff] [blame] | 891 | default: |
George Rimar | b567b62 | 2016-07-26 18:46:13 +0000 | [diff] [blame] | 892 | llvm_unreachable("unsupported target"); |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 893 | } |
| 894 | error("symbol not found: " + S); |
| 895 | return 0; |
| 896 | } |
| 897 | |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 898 | static uint64_t getSectionSize(StringRef Name) { |
| 899 | switch (Config->EKind) { |
| 900 | case ELF32LEKind: |
| 901 | return Script<ELF32LE>::X->getOutputSectionSize(Name); |
| 902 | case ELF32BEKind: |
| 903 | return Script<ELF32BE>::X->getOutputSectionSize(Name); |
| 904 | case ELF64LEKind: |
| 905 | return Script<ELF64LE>::X->getOutputSectionSize(Name); |
| 906 | case ELF64BEKind: |
| 907 | return Script<ELF64BE>::X->getOutputSectionSize(Name); |
| 908 | default: |
| 909 | llvm_unreachable("unsupported target"); |
| 910 | } |
| 911 | return 0; |
| 912 | } |
| 913 | |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 914 | SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { |
| 915 | StringRef Op = next(); |
| 916 | assert(Op == "=" || Op == "+="); |
| 917 | Expr E = readExpr(); |
| 918 | if (Op == "+=") |
| 919 | E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); }; |
| 920 | auto *Cmd = new SymbolAssignment(Name, E); |
| 921 | Opt.Commands.emplace_back(Cmd); |
| 922 | return Cmd; |
| 923 | } |
| 924 | |
| 925 | // This is an operator-precedence parser to parse a linker |
| 926 | // script expression. |
| 927 | Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } |
| 928 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 929 | // This is a part of the operator-precedence parser. This function |
| 930 | // assumes that the remaining token stream starts with an operator. |
| 931 | Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { |
| 932 | while (!atEOF() && !Error) { |
| 933 | // Read an operator and an expression. |
| 934 | StringRef Op1 = peek(); |
| 935 | if (Op1 == "?") |
| 936 | return readTernary(Lhs); |
| 937 | if (precedence(Op1) < MinPrec) |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 938 | break; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 939 | next(); |
| 940 | Expr Rhs = readPrimary(); |
| 941 | |
| 942 | // Evaluate the remaining part of the expression first if the |
| 943 | // next operator has greater precedence than the previous one. |
| 944 | // For example, if we have read "+" and "3", and if the next |
| 945 | // operator is "*", then we'll evaluate 3 * ... part first. |
| 946 | while (!atEOF()) { |
| 947 | StringRef Op2 = peek(); |
| 948 | if (precedence(Op2) <= precedence(Op1)) |
| 949 | break; |
| 950 | Rhs = readExpr1(Rhs, precedence(Op2)); |
| 951 | } |
| 952 | |
| 953 | Lhs = combine(Op1, Lhs, Rhs); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 954 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 955 | return Lhs; |
| 956 | } |
| 957 | |
| 958 | uint64_t static getConstant(StringRef S) { |
| 959 | if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE") |
| 960 | return Target->PageSize; |
| 961 | error("unknown constant: " + S); |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | Expr ScriptParser::readPrimary() { |
| 966 | StringRef Tok = next(); |
| 967 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 968 | if (Tok == "(") { |
| 969 | Expr E = readExpr(); |
| 970 | expect(")"); |
| 971 | return E; |
| 972 | } |
| 973 | |
| 974 | // Built-in functions are parsed here. |
| 975 | // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. |
| 976 | if (Tok == "ALIGN") { |
| 977 | expect("("); |
| 978 | Expr E = readExpr(); |
| 979 | expect(")"); |
| 980 | return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; |
| 981 | } |
| 982 | if (Tok == "CONSTANT") { |
| 983 | expect("("); |
| 984 | StringRef Tok = next(); |
| 985 | expect(")"); |
| 986 | return [=](uint64_t Dot) { return getConstant(Tok); }; |
| 987 | } |
Rafael Espindola | 54c145c | 2016-07-28 18:16:24 +0000 | [diff] [blame] | 988 | if (Tok == "SEGMENT_START") { |
| 989 | expect("("); |
| 990 | next(); |
| 991 | expect(","); |
| 992 | uint64_t Val; |
| 993 | next().getAsInteger(0, Val); |
| 994 | expect(")"); |
| 995 | return [=](uint64_t Dot) { return Val; }; |
| 996 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 997 | if (Tok == "DATA_SEGMENT_ALIGN") { |
| 998 | expect("("); |
| 999 | Expr E = readExpr(); |
| 1000 | expect(","); |
| 1001 | readExpr(); |
| 1002 | expect(")"); |
Rui Ueyama | f7791bb | 2016-07-26 19:34:10 +0000 | [diff] [blame] | 1003 | return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1004 | } |
| 1005 | if (Tok == "DATA_SEGMENT_END") { |
| 1006 | expect("("); |
| 1007 | expect("."); |
| 1008 | expect(")"); |
| 1009 | return [](uint64_t Dot) { return Dot; }; |
| 1010 | } |
George Rimar | 276b4e6 | 2016-07-26 17:58:44 +0000 | [diff] [blame] | 1011 | // GNU linkers implements more complicated logic to handle |
| 1012 | // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to |
| 1013 | // the next page boundary for simplicity. |
| 1014 | if (Tok == "DATA_SEGMENT_RELRO_END") { |
| 1015 | expect("("); |
| 1016 | next(); |
| 1017 | expect(","); |
| 1018 | readExpr(); |
| 1019 | expect(")"); |
| 1020 | return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; |
| 1021 | } |
George Rimar | 9e69450 | 2016-07-29 16:18:47 +0000 | [diff] [blame] | 1022 | if (Tok == "SIZEOF") { |
| 1023 | expect("("); |
| 1024 | StringRef Name = next(); |
| 1025 | expect(")"); |
| 1026 | return [=](uint64_t Dot) { return getSectionSize(Name); }; |
| 1027 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1028 | |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1029 | // Parse a symbol name or a number literal. |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1030 | uint64_t V = 0; |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1031 | if (Tok.getAsInteger(0, V)) { |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1032 | if (Tok != "." && !isValidCIdentifier(Tok)) |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1033 | setError("malformed number: " + Tok); |
George Rimar | 30835ea | 2016-07-28 21:08:56 +0000 | [diff] [blame] | 1034 | return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); }; |
George Rimar | a9c5a52 | 2016-07-26 18:18:58 +0000 | [diff] [blame] | 1035 | } |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 1036 | return [=](uint64_t Dot) { return V; }; |
| 1037 | } |
| 1038 | |
| 1039 | Expr ScriptParser::readTernary(Expr Cond) { |
| 1040 | next(); |
| 1041 | Expr L = readExpr(); |
| 1042 | expect(":"); |
| 1043 | Expr R = readExpr(); |
| 1044 | return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; |
| 1045 | } |
| 1046 | |
| 1047 | Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) { |
| 1048 | if (Op == "*") |
| 1049 | return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; |
| 1050 | if (Op == "/") { |
| 1051 | return [=](uint64_t Dot) -> uint64_t { |
| 1052 | uint64_t RHS = R(Dot); |
| 1053 | if (RHS == 0) { |
| 1054 | error("division by zero"); |
| 1055 | return 0; |
| 1056 | } |
| 1057 | return L(Dot) / RHS; |
| 1058 | }; |
| 1059 | } |
| 1060 | if (Op == "+") |
| 1061 | return [=](uint64_t Dot) { return L(Dot) + R(Dot); }; |
| 1062 | if (Op == "-") |
| 1063 | return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; |
| 1064 | if (Op == "<") |
| 1065 | return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; |
| 1066 | if (Op == ">") |
| 1067 | return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; |
| 1068 | if (Op == ">=") |
| 1069 | return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; |
| 1070 | if (Op == "<=") |
| 1071 | return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; |
| 1072 | if (Op == "==") |
| 1073 | return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; |
| 1074 | if (Op == "!=") |
| 1075 | return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; |
| 1076 | if (Op == "&") |
| 1077 | return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; |
| 1078 | llvm_unreachable("invalid operator"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1081 | std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { |
| 1082 | std::vector<StringRef> Phdrs; |
| 1083 | while (!Error && peek().startswith(":")) { |
| 1084 | StringRef Tok = next(); |
| 1085 | Tok = (Tok.size() == 1) ? next() : Tok.substr(1); |
| 1086 | if (Tok.empty()) { |
| 1087 | setError("section header name is empty"); |
| 1088 | break; |
| 1089 | } |
Rui Ueyama | 047404f | 2016-07-20 19:36:36 +0000 | [diff] [blame] | 1090 | Phdrs.push_back(Tok); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1091 | } |
| 1092 | return Phdrs; |
| 1093 | } |
| 1094 | |
| 1095 | unsigned ScriptParser::readPhdrType() { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1096 | StringRef Tok = next(); |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 1097 | unsigned Ret = StringSwitch<unsigned>(Tok) |
| 1098 | .Case("PT_NULL", PT_NULL) |
| 1099 | .Case("PT_LOAD", PT_LOAD) |
| 1100 | .Case("PT_DYNAMIC", PT_DYNAMIC) |
| 1101 | .Case("PT_INTERP", PT_INTERP) |
| 1102 | .Case("PT_NOTE", PT_NOTE) |
| 1103 | .Case("PT_SHLIB", PT_SHLIB) |
| 1104 | .Case("PT_PHDR", PT_PHDR) |
| 1105 | .Case("PT_TLS", PT_TLS) |
| 1106 | .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) |
| 1107 | .Case("PT_GNU_STACK", PT_GNU_STACK) |
| 1108 | .Case("PT_GNU_RELRO", PT_GNU_RELRO) |
| 1109 | .Default(-1); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1110 | |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 1111 | if (Ret == (unsigned)-1) { |
| 1112 | setError("invalid program header type: " + Tok); |
| 1113 | return PT_NULL; |
| 1114 | } |
| 1115 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 1118 | static bool isUnderSysroot(StringRef Path) { |
| 1119 | if (Config->Sysroot == "") |
| 1120 | return false; |
| 1121 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 1122 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 1123 | return true; |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1127 | // Entry point. |
| 1128 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 1129 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1130 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 1131 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 1132 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 1133 | template class elf::LinkerScript<ELF32LE>; |
| 1134 | template class elf::LinkerScript<ELF32BE>; |
| 1135 | template class elf::LinkerScript<ELF64LE>; |
| 1136 | template class elf::LinkerScript<ELF64BE>; |