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