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