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