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