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