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