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