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. |
| 11 | // It does not construct an AST but consume linker script directives directly. |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 12 | // Results are written to Driver or Config object. |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 16 | #include "LinkerScript.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 17 | #include "Config.h" |
| 18 | #include "Driver.h" |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 19 | #include "InputSection.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 20 | #include "OutputSections.h" |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 21 | #include "ScriptParser.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 22 | #include "SymbolTable.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 23 | #include "llvm/Support/ELF.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 27 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 30 | using namespace llvm::ELF; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 31 | using namespace llvm::object; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 32 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 33 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 34 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 35 | LinkerScript *elf::Script; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 36 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 37 | static uint64_t getInteger(StringRef S) { |
| 38 | uint64_t V; |
| 39 | if (S.getAsInteger(0, V)) { |
| 40 | error("malformed number: " + S); |
| 41 | return 0; |
| 42 | } |
| 43 | return V; |
| 44 | } |
| 45 | |
| 46 | // Evaluates the expression given by list of tokens. |
| 47 | uint64_t LinkerScript::evaluate(std::vector<StringRef> &Tokens, |
| 48 | uint64_t LocCounter) { |
| 49 | uint64_t Result = 0; |
| 50 | for (size_t I = 0, E = Tokens.size(); I < E; ++I) { |
| 51 | // Each second token should be '+' as this is the |
| 52 | // only operator we support now. |
| 53 | if (I % 2 == 1) { |
| 54 | if (Tokens[I] == "+") |
| 55 | continue; |
| 56 | error("error in location counter expression"); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | StringRef Tok = Tokens[I]; |
| 61 | if (Tok == ".") |
| 62 | Result += LocCounter; |
| 63 | else |
| 64 | Result += getInteger(Tok); |
| 65 | } |
| 66 | return Result; |
| 67 | } |
| 68 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 69 | template <class ELFT> |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 70 | SectionRule *LinkerScript::find(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 71 | for (SectionRule &R : Sections) |
| 72 | if (R.match(S)) |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 73 | return &R; |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | template <class ELFT> |
| 78 | StringRef LinkerScript::getOutputSection(InputSectionBase<ELFT> *S) { |
| 79 | SectionRule *R = find(S); |
| 80 | return R ? R->Dest : ""; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 83 | template <class ELFT> |
| 84 | bool LinkerScript::isDiscarded(InputSectionBase<ELFT> *S) { |
| 85 | return getOutputSection(S) == "/DISCARD/"; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 86 | } |
| 87 | |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 88 | template <class ELFT> bool LinkerScript::shouldKeep(InputSectionBase<ELFT> *S) { |
| 89 | SectionRule *R = find(S); |
| 90 | return R && R->Keep; |
| 91 | } |
| 92 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 93 | // This method finalizes the Locations list. Adds neccesary locations for |
| 94 | // orphan sections, what prepares it for futher use without |
| 95 | // changes in LinkerScript::assignAddresses(). |
| 96 | template <class ELFT> |
| 97 | void LinkerScript::fixupLocations(std::vector<OutputSectionBase<ELFT> *> &S) { |
| 98 | // Orphan sections are sections present in the input files which |
| 99 | // are not explicitly placed into the output file by the linker |
| 100 | // script. We place orphan sections at end of file. Other linkers places |
| 101 | // them using some heuristics as described in |
| 102 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
| 103 | for (OutputSectionBase<ELFT> *Sec : S) { |
| 104 | StringRef Name = Sec->getName(); |
| 105 | auto I = std::find(SectionOrder.begin(), SectionOrder.end(), Name); |
| 106 | if (I == SectionOrder.end()) |
| 107 | Locations.push_back({Command::Section, {}, {Name}}); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | template <class ELFT> |
| 112 | void LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S) { |
| 113 | typedef typename ELFT::uint uintX_t; |
| 114 | |
| 115 | Script->fixupLocations(S); |
| 116 | |
| 117 | uintX_t ThreadBssOffset = 0; |
| 118 | uintX_t VA = |
| 119 | Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
| 120 | |
| 121 | for (LocationNode &Node : Locations) { |
| 122 | if (Node.Type == Command::Expr) { |
| 123 | VA = evaluate(Node.Expr, VA); |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | auto I = |
| 128 | std::find_if(S.begin(), S.end(), [&](OutputSectionBase<ELFT> *Sec) { |
| 129 | return Sec->getName() == Node.SectionName; |
| 130 | }); |
| 131 | if (I == S.end()) |
| 132 | continue; |
| 133 | |
| 134 | OutputSectionBase<ELFT> *Sec = *I; |
| 135 | uintX_t Align = Sec->getAlign(); |
| 136 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 137 | uintX_t TVA = VA + ThreadBssOffset; |
| 138 | TVA = alignTo(TVA, Align); |
| 139 | Sec->setVA(TVA); |
| 140 | ThreadBssOffset = TVA - VA + Sec->getSize(); |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | if (Sec->getFlags() & SHF_ALLOC) { |
| 145 | VA = alignTo(VA, Align); |
| 146 | Sec->setVA(VA); |
| 147 | VA += Sec->getSize(); |
| 148 | continue; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 153 | ArrayRef<uint8_t> LinkerScript::getFiller(StringRef Name) { |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 154 | auto I = Filler.find(Name); |
| 155 | if (I == Filler.end()) |
| 156 | return {}; |
| 157 | return I->second; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Rui Ueyama | e9c5806 | 2016-02-12 20:41:43 +0000 | [diff] [blame] | 160 | // A compartor to sort output sections. Returns -1 or 1 if both |
| 161 | // A and B are mentioned in linker scripts. Otherwise, returns 0 |
| 162 | // to use the default rule which is implemented in Writer.cpp. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 163 | int LinkerScript::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 164 | auto E = SectionOrder.end(); |
| 165 | auto I = std::find(SectionOrder.begin(), E, A); |
| 166 | auto J = std::find(SectionOrder.begin(), E, B); |
| 167 | if (I == E || J == E) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 168 | return 0; |
| 169 | return I < J ? -1 : 1; |
| 170 | } |
| 171 | |
George Rimar | cb2aeb6 | 2016-02-24 08:49:50 +0000 | [diff] [blame] | 172 | // Returns true if S matches T. S can contain glob meta-characters. |
| 173 | // The asterisk ('*') matches zero or more characacters, and the question |
| 174 | // mark ('?') matches one character. |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 175 | static bool matchStr(StringRef S, StringRef T) { |
| 176 | for (;;) { |
| 177 | if (S.empty()) |
| 178 | return T.empty(); |
| 179 | if (S[0] == '*') { |
| 180 | S = S.substr(1); |
| 181 | if (S.empty()) |
| 182 | // Fast path. If a pattern is '*', it matches anything. |
| 183 | return true; |
| 184 | for (size_t I = 0, E = T.size(); I < E; ++I) |
| 185 | if (matchStr(S, T.substr(I))) |
| 186 | return true; |
| 187 | return false; |
| 188 | } |
George Rimar | cb2aeb6 | 2016-02-24 08:49:50 +0000 | [diff] [blame] | 189 | if (T.empty() || (S[0] != T[0] && S[0] != '?')) |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 190 | return false; |
| 191 | S = S.substr(1); |
| 192 | T = T.substr(1); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) { |
| 197 | return matchStr(SectionPattern, S->getSectionName()); |
| 198 | } |
| 199 | |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 200 | class elf::ScriptParser final : public elf::ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 201 | typedef void (ScriptParser::*Handler)(); |
| 202 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 203 | public: |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 204 | ScriptParser(BumpPtrAllocator *A, StringRef S, bool B) |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 205 | : ScriptParserBase(S), Saver(*A), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 206 | |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 207 | void run() override; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 208 | |
| 209 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 210 | void addFile(StringRef Path); |
| 211 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 212 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 213 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 214 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 215 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 216 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 217 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 218 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 219 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 220 | void readOutputFormat(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 221 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 222 | void readSections(); |
| 223 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 224 | void readLocationCounterValue(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 225 | void readOutputSectionDescription(); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 226 | void readSectionPatterns(StringRef OutSec, bool Keep); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 227 | |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 228 | StringSaver Saver; |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 229 | const static StringMap<Handler> Cmd; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 230 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 231 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 232 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 233 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 234 | {"ENTRY", &ScriptParser::readEntry}, |
| 235 | {"EXTERN", &ScriptParser::readExtern}, |
| 236 | {"GROUP", &ScriptParser::readGroup}, |
| 237 | {"INCLUDE", &ScriptParser::readInclude}, |
| 238 | {"INPUT", &ScriptParser::readGroup}, |
| 239 | {"OUTPUT", &ScriptParser::readOutput}, |
| 240 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 241 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
| 242 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 243 | {"SECTIONS", &ScriptParser::readSections}, |
| 244 | {";", &ScriptParser::readNothing}}; |
| 245 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 246 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 247 | while (!atEOF()) { |
| 248 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 249 | if (Handler Fn = Cmd.lookup(Tok)) |
| 250 | (this->*Fn)(); |
| 251 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 252 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 256 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 257 | if (IsUnderSysroot && S.startswith("/")) { |
| 258 | SmallString<128> Path; |
| 259 | (Config->Sysroot + S).toStringRef(Path); |
| 260 | if (sys::fs::exists(Path)) { |
| 261 | Driver->addFile(Saver.save(Path.str())); |
| 262 | return; |
| 263 | } |
| 264 | } |
| 265 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 266 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 267 | Driver->addFile(S); |
| 268 | } else if (S.startswith("=")) { |
| 269 | if (Config->Sysroot.empty()) |
| 270 | Driver->addFile(S.substr(1)); |
| 271 | else |
| 272 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 273 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 274 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 275 | } else if (sys::fs::exists(S)) { |
| 276 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 277 | } else { |
| 278 | std::string Path = findFromSearchPaths(S); |
| 279 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 280 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 281 | else |
| 282 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 286 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 287 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 288 | bool Orig = Config->AsNeeded; |
| 289 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 290 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 291 | StringRef Tok = next(); |
| 292 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 293 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 294 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 295 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 296 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 299 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 300 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 301 | expect("("); |
| 302 | StringRef Tok = next(); |
| 303 | if (Config->Entry.empty()) |
| 304 | Config->Entry = Tok; |
| 305 | expect(")"); |
| 306 | } |
| 307 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 308 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 309 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 310 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 311 | StringRef Tok = next(); |
| 312 | if (Tok == ")") |
| 313 | return; |
| 314 | Config->Undefined.push_back(Tok); |
| 315 | } |
| 316 | } |
| 317 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 318 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 319 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 320 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 321 | StringRef Tok = next(); |
| 322 | if (Tok == ")") |
| 323 | return; |
| 324 | if (Tok == "AS_NEEDED") { |
| 325 | readAsNeeded(); |
| 326 | continue; |
| 327 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 328 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 332 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 333 | StringRef Tok = next(); |
| 334 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 335 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 336 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 337 | return; |
| 338 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 339 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 340 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 341 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 342 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 345 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 346 | // -o <file> takes predecence over OUTPUT(<file>). |
| 347 | expect("("); |
| 348 | StringRef Tok = next(); |
| 349 | if (Config->OutputFile.empty()) |
| 350 | Config->OutputFile = Tok; |
| 351 | expect(")"); |
| 352 | } |
| 353 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 354 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 355 | // Error checking only for now. |
| 356 | expect("("); |
| 357 | next(); |
| 358 | expect(")"); |
| 359 | } |
| 360 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 361 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 362 | // Error checking only for now. |
| 363 | expect("("); |
| 364 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 365 | StringRef Tok = next(); |
| 366 | if (Tok == ")") |
| 367 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 368 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 369 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 370 | return; |
| 371 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 372 | next(); |
| 373 | expect(","); |
| 374 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 375 | expect(")"); |
| 376 | } |
| 377 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 378 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 379 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 380 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 381 | expect(")"); |
| 382 | } |
| 383 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 384 | void ScriptParser::readSections() { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 385 | Script->DoLayout = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 386 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 387 | while (!Error && !skip("}")) { |
| 388 | StringRef Tok = peek(); |
| 389 | if (Tok == ".") |
| 390 | readLocationCounterValue(); |
| 391 | else |
| 392 | readOutputSectionDescription(); |
| 393 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 394 | } |
| 395 | |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 396 | void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) { |
| 397 | expect("("); |
| 398 | while (!Error && !skip(")")) |
| 399 | Script->Sections.emplace_back(OutSec, next(), Keep); |
| 400 | } |
| 401 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 402 | void ScriptParser::readLocationCounterValue() { |
| 403 | expect("."); |
| 404 | expect("="); |
| 405 | Script->Locations.push_back({Command::Expr, {}, {}}); |
| 406 | LocationNode &Node = Script->Locations.back(); |
| 407 | while (!Error) { |
| 408 | StringRef Tok = next(); |
| 409 | if (Tok == ";") |
| 410 | break; |
| 411 | Node.Expr.push_back(Tok); |
| 412 | } |
| 413 | if (Node.Expr.empty()) |
| 414 | error("error in location counter expression"); |
| 415 | } |
| 416 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 417 | void ScriptParser::readOutputSectionDescription() { |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 418 | StringRef OutSec = next(); |
| 419 | Script->SectionOrder.push_back(OutSec); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 420 | Script->Locations.push_back({Command::Section, {}, {OutSec}}); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 421 | expect(":"); |
| 422 | expect("{"); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 423 | while (!Error && !skip("}")) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 424 | StringRef Tok = next(); |
| 425 | if (Tok == "*") { |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 426 | readSectionPatterns(OutSec, false); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 427 | } else if (Tok == "KEEP") { |
| 428 | expect("("); |
| 429 | next(); // Skip * |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 430 | readSectionPatterns(OutSec, true); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 431 | expect(")"); |
| 432 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 433 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 434 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 435 | } |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 436 | StringRef Tok = peek(); |
| 437 | if (Tok.startswith("=")) { |
| 438 | if (!Tok.startswith("=0x")) { |
Rui Ueyama | 3ed2f06 | 2016-03-13 03:17:44 +0000 | [diff] [blame] | 439 | setError("filler should be a hexadecimal value"); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 440 | return; |
| 441 | } |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 442 | Tok = Tok.substr(3); |
| 443 | Script->Filler[OutSec] = parseHex(Tok); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 444 | next(); |
| 445 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 448 | static bool isUnderSysroot(StringRef Path) { |
| 449 | if (Config->Sysroot == "") |
| 450 | return false; |
| 451 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 452 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 453 | return true; |
| 454 | return false; |
| 455 | } |
| 456 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 457 | // Entry point. The other functions or classes are private to this file. |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 458 | void LinkerScript::read(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 459 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 460 | ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 461 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 462 | |
| 463 | template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF32LE> *); |
| 464 | template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF32BE> *); |
| 465 | template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF64LE> *); |
| 466 | template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF64BE> *); |
| 467 | |
| 468 | template bool LinkerScript::isDiscarded(InputSectionBase<ELF32LE> *); |
| 469 | template bool LinkerScript::isDiscarded(InputSectionBase<ELF32BE> *); |
| 470 | template bool LinkerScript::isDiscarded(InputSectionBase<ELF64LE> *); |
| 471 | template bool LinkerScript::isDiscarded(InputSectionBase<ELF64BE> *); |
| 472 | |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 473 | template bool LinkerScript::shouldKeep(InputSectionBase<ELF32LE> *); |
| 474 | template bool LinkerScript::shouldKeep(InputSectionBase<ELF32BE> *); |
| 475 | template bool LinkerScript::shouldKeep(InputSectionBase<ELF64LE> *); |
| 476 | template bool LinkerScript::shouldKeep(InputSectionBase<ELF64BE> *); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 477 | |
| 478 | template void |
| 479 | LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF32LE> *> &); |
| 480 | template void |
| 481 | LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF32BE> *> &); |
| 482 | template void |
| 483 | LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF64LE> *> &); |
| 484 | template void |
| 485 | LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF64BE> *> &); |