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