blob: f90ce6fa740757de659eebf3563bc4856b44a583 [file] [log] [blame]
Rui Ueyama2ec34542017-04-05 05:07:391//===- ScriptParser.cpp ---------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rui Ueyama2ec34542017-04-05 05:07:396//
7//===----------------------------------------------------------------------===//
Rui Ueyama05f6b852017-04-05 05:50:088//
9// This file contains a recursive-descendent parser for linker scripts.
10// Parsed results are stored to Config and Script global objects.
11//
12//===----------------------------------------------------------------------===//
Rui Ueyama2ec34542017-04-05 05:07:3913
14#include "ScriptParser.h"
15#include "Config.h"
16#include "Driver.h"
Fangrui Songb01430a2022-02-24 03:18:2417#include "InputFiles.h"
Rui Ueyama2ec34542017-04-05 05:07:3918#include "LinkerScript.h"
Rui Ueyama2ec34542017-04-05 05:07:3919#include "OutputSections.h"
20#include "ScriptLexer.h"
Fangrui Song27bb7992022-02-08 05:53:3421#include "SymbolTable.h"
Rui Ueyama2ec34542017-04-05 05:07:3922#include "Symbols.h"
23#include "Target.h"
Alexandre Ganea83d59e02022-01-20 19:53:1824#include "lld/Common/CommonLinkerContext.h"
Rui Ueyama2ec34542017-04-05 05:07:3925#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/StringRef.h"
Rui Ueyama0440be42017-09-06 18:14:0827#include "llvm/ADT/StringSet.h"
Rui Ueyama2ec34542017-04-05 05:07:3928#include "llvm/ADT/StringSwitch.h"
Zachary Turner264b5d92017-06-07 03:48:5629#include "llvm/BinaryFormat/ELF.h"
Rui Ueyama2ec34542017-04-05 05:07:3930#include "llvm/Support/Casting.h"
Rui Ueyama2ec34542017-04-05 05:07:3931#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/FileSystem.h"
Isaac Richterfa1145a2020-07-27 08:49:2433#include "llvm/Support/MathExtras.h"
Rui Ueyama2ec34542017-04-05 05:07:3934#include "llvm/Support/Path.h"
Fangrui Songfe0de252022-06-26 03:25:3435#include "llvm/Support/SaveAndRestore.h"
James Henderson439341b2020-11-03 14:41:0936#include "llvm/Support/TimeProfiler.h"
Rui Ueyama2ec34542017-04-05 05:07:3937#include <cassert>
38#include <limits>
Parth Aroraebb326a2024-03-25 23:11:2139#include <optional>
Rui Ueyama2ec34542017-04-05 05:07:3940#include <vector>
41
42using namespace llvm;
43using namespace llvm::ELF;
Rui Ueyamab58079d42017-04-11 22:45:5744using namespace llvm::support::endian;
Fangrui Song07837b82020-05-15 05:18:5845using namespace lld;
46using namespace lld::elf;
Rui Ueyama2ec34542017-04-05 05:07:3947
Rui Ueyama96b3fe02017-04-05 05:08:0148namespace {
49class ScriptParser final : ScriptLexer {
Rui Ueyama2ec34542017-04-05 05:07:3950public:
Rui Ueyama3837f422019-07-10 05:00:3751 ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
Rui Ueyama11ae59f2019-07-03 06:11:5052 // Initialize IsUnderSysroot
Rui Ueyama3837f422019-07-10 05:00:3753 if (config->sysroot == "")
Rui Ueyama11ae59f2019-07-03 06:11:5054 return;
Rui Ueyama3837f422019-07-10 05:00:3755 StringRef path = mb.getBufferIdentifier();
56 for (; !path.empty(); path = sys::path::parent_path(path)) {
57 if (!sys::fs::equivalent(config->sysroot, path))
Rui Ueyama11ae59f2019-07-03 06:11:5058 continue;
Rui Ueyama3837f422019-07-10 05:00:3759 isUnderSysroot = true;
Rui Ueyama11ae59f2019-07-03 06:11:5060 return;
61 }
62 }
Rui Ueyama2ec34542017-04-05 05:07:3963
64 void readLinkerScript();
65 void readVersionScript();
66 void readDynamicList();
Rui Ueyama3837f422019-07-10 05:00:3767 void readDefsym(StringRef name);
Rui Ueyama2ec34542017-04-05 05:07:3968
69private:
Rui Ueyama3837f422019-07-10 05:00:3770 void addFile(StringRef path);
Rui Ueyama2ec34542017-04-05 05:07:3971
72 void readAsNeeded();
73 void readEntry();
74 void readExtern();
75 void readGroup();
76 void readInclude();
Rui Ueyama1d92aa72018-04-09 23:05:4877 void readInput();
Rui Ueyama2ec34542017-04-05 05:07:3978 void readMemory();
79 void readOutput();
80 void readOutputArch();
81 void readOutputFormat();
Fangrui Song899fdf52021-06-13 19:41:1182 void readOverwriteSections();
Rui Ueyama2ec34542017-04-05 05:07:3983 void readPhdrs();
George Rimar5f375412017-09-08 08:23:1584 void readRegionAlias();
Rui Ueyama2ec34542017-04-05 05:07:3985 void readSearchDir();
86 void readSections();
Rui Ueyamae262bb12018-08-06 21:29:4187 void readTarget();
Rui Ueyama2ec34542017-04-05 05:07:3988 void readVersion();
89 void readVersionScriptCommand();
90
Rui Ueyama3837f422019-07-10 05:00:3791 SymbolAssignment *readSymbolAssignment(StringRef name);
92 ByteCommand *readByteCommand(StringRef tok);
Simon Atanasyanb0486052018-11-14 21:05:2093 std::array<uint8_t, 4> readFill();
Rui Ueyama3837f422019-07-10 05:00:3794 bool readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2);
95 void readSectionAddressType(OutputSection *cmd);
Fangrui Song6c814932022-03-08 19:23:4196 OutputDesc *readOverlaySectionDescription();
97 OutputDesc *readOutputSectionDescription(StringRef outSec);
Fangrui Song64038ef2021-12-27 04:12:5598 SmallVector<SectionCommand *, 0> readOverlay();
Fangrui Songa1c2ee02021-12-26 21:53:4799 SmallVector<StringRef, 0> readOutputSectionPhdrs();
Peter Smithdbd0ad32020-01-15 09:38:00100 std::pair<uint64_t, uint64_t> readInputSectionFlags();
Rui Ueyama3837f422019-07-10 05:00:37101 InputSectionDescription *readInputSectionDescription(StringRef tok);
Rui Ueyama2ec34542017-04-05 05:07:39102 StringMatcher readFilePatterns();
Fangrui Song64038ef2021-12-27 04:12:55103 SmallVector<SectionPattern, 0> readInputSectionsList();
Peter Smithdbd0ad32020-01-15 09:38:00104 InputSectionDescription *readInputSectionRules(StringRef filePattern,
105 uint64_t withFlags,
106 uint64_t withoutFlags);
Rui Ueyama2ec34542017-04-05 05:07:39107 unsigned readPhdrType();
Fangrui Song2a9aed02020-11-12 16:46:53108 SortSectionPolicy peekSortKind();
Rui Ueyama2ec34542017-04-05 05:07:39109 SortSectionPolicy readSortKind();
Rui Ueyama3837f422019-07-10 05:00:37110 SymbolAssignment *readProvideHidden(bool provide, bool hidden);
111 SymbolAssignment *readAssignment(StringRef tok);
Rui Ueyama2ec34542017-04-05 05:07:39112 void readSort();
George Rimard30a78b2018-04-25 11:16:31113 Expr readAssert();
George Rimar5fb17122017-08-03 16:05:08114 Expr readConstant();
115 Expr getPageSize();
Rui Ueyama2ec34542017-04-05 05:07:39116
Fangrui Song92b5b982020-03-06 19:49:58117 Expr readMemoryAssignment(StringRef, StringRef, StringRef);
Igor Kudrin8cdf1c12021-11-24 05:17:03118 void readMemoryAttributes(uint32_t &flags, uint32_t &invFlags,
119 uint32_t &negFlags, uint32_t &negInvFlags);
Rui Ueyama2ec34542017-04-05 05:07:39120
Rui Ueyama3837f422019-07-10 05:00:37121 Expr combine(StringRef op, Expr l, Expr r);
Rui Ueyama2ec34542017-04-05 05:07:39122 Expr readExpr();
Rui Ueyama3837f422019-07-10 05:00:37123 Expr readExpr1(Expr lhs, int minPrec);
Rui Ueyama2ec34542017-04-05 05:07:39124 StringRef readParenLiteral();
125 Expr readPrimary();
Rui Ueyama3837f422019-07-10 05:00:37126 Expr readTernary(Expr cond);
Rui Ueyama2ec34542017-04-05 05:07:39127 Expr readParenExpr();
128
129 // For parsing version script.
Fangrui Song64038ef2021-12-27 04:12:55130 SmallVector<SymbolVersion, 0> readVersionExtern();
Rui Ueyama2ec34542017-04-05 05:07:39131 void readAnonymousDeclaration();
Rui Ueyama3837f422019-07-10 05:00:37132 void readVersionDeclaration(StringRef verStr);
Rui Ueyama2ec34542017-04-05 05:07:39133
Fangrui Song64038ef2021-12-27 04:12:55134 std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
Rui Ueyama2ec34542017-04-05 05:07:39135 readSymbols();
136
Fangrui Songbf6e2592021-10-25 19:52:06137 // True if a script being read is in the --sysroot directory.
Rui Ueyama3837f422019-07-10 05:00:37138 bool isUnderSysroot = false;
Rui Ueyama0440be42017-09-06 18:14:08139
140 // A set to detect an INCLUDE() cycle.
Rui Ueyama3837f422019-07-10 05:00:37141 StringSet<> seen;
Parth Aroraebb326a2024-03-25 23:11:21142
143 // If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command,
144 // then this member is set to the PROVIDE symbol name.
145 std::optional<llvm::StringRef> activeProvideSym;
Rui Ueyama2ec34542017-04-05 05:07:39146};
Rui Ueyama96b3fe02017-04-05 05:08:01147} // namespace
Rui Ueyama2ec34542017-04-05 05:07:39148
Rui Ueyama3837f422019-07-10 05:00:37149static StringRef unquote(StringRef s) {
Fangrui Song8d85c962023-06-05 21:36:19150 if (s.starts_with("\""))
Rui Ueyama3837f422019-07-10 05:00:37151 return s.substr(1, s.size() - 2);
152 return s;
Rui Ueyama1e77ad12017-07-13 20:30:35153}
154
Rui Ueyama2ec34542017-04-05 05:07:39155// Some operations only support one non absolute value. Move the
156// absolute one to the right hand side for convenience.
Rui Ueyama3837f422019-07-10 05:00:37157static void moveAbsRight(ExprValue &a, ExprValue &b) {
158 if (a.sec == nullptr || (a.forceAbsolute && !b.isAbsolute()))
159 std::swap(a, b);
160 if (!b.isAbsolute())
161 error(a.loc + ": at least one side of the expression must be absolute");
Rui Ueyama2ec34542017-04-05 05:07:39162}
163
Rui Ueyama3837f422019-07-10 05:00:37164static ExprValue add(ExprValue a, ExprValue b) {
165 moveAbsRight(a, b);
166 return {a.sec, a.forceAbsolute, a.getSectionOffset() + b.getValue(), a.loc};
Rui Ueyama2ec34542017-04-05 05:07:39167}
168
Rui Ueyama3837f422019-07-10 05:00:37169static ExprValue sub(ExprValue a, ExprValue b) {
Rafael Espindola63a4a982017-12-26 18:11:14170 // The distance between two symbols in sections is absolute.
Rui Ueyama3837f422019-07-10 05:00:37171 if (!a.isAbsolute() && !b.isAbsolute())
172 return a.getValue() - b.getValue();
173 return {a.sec, false, a.getSectionOffset() - b.getValue(), a.loc};
Rui Ueyama2ec34542017-04-05 05:07:39174}
175
Rui Ueyama3837f422019-07-10 05:00:37176static ExprValue bitAnd(ExprValue a, ExprValue b) {
177 moveAbsRight(a, b);
178 return {a.sec, a.forceAbsolute,
179 (a.getValue() & b.getValue()) - a.getSecAddr(), a.loc};
Rui Ueyama2ec34542017-04-05 05:07:39180}
181
Fangrui Songfae96102023-07-15 21:10:40182static ExprValue bitXor(ExprValue a, ExprValue b) {
183 moveAbsRight(a, b);
184 return {a.sec, a.forceAbsolute,
185 (a.getValue() ^ b.getValue()) - a.getSecAddr(), a.loc};
186}
187
Rui Ueyama3837f422019-07-10 05:00:37188static ExprValue bitOr(ExprValue a, ExprValue b) {
189 moveAbsRight(a, b);
190 return {a.sec, a.forceAbsolute,
191 (a.getValue() | b.getValue()) - a.getSecAddr(), a.loc};
Rui Ueyama2ec34542017-04-05 05:07:39192}
193
Rui Ueyama2ec34542017-04-05 05:07:39194void ScriptParser::readDynamicList() {
195 expect("{");
Fangrui Song64038ef2021-12-27 04:12:55196 SmallVector<SymbolVersion, 0> locals;
197 SmallVector<SymbolVersion, 0> globals;
Rui Ueyama3837f422019-07-10 05:00:37198 std::tie(locals, globals) = readSymbols();
Rafael Espindolad72d97b2017-09-08 18:16:59199 expect(";");
200
201 if (!atEOF()) {
Rui Ueyama2ec34542017-04-05 05:07:39202 setError("EOF expected, but got " + next());
Rafael Espindolad72d97b2017-09-08 18:16:59203 return;
204 }
Rui Ueyama3837f422019-07-10 05:00:37205 if (!locals.empty()) {
Rafael Espindolad72d97b2017-09-08 18:16:59206 setError("\"local:\" scope not supported in --dynamic-list");
207 return;
208 }
209
Rui Ueyama3837f422019-07-10 05:00:37210 for (SymbolVersion v : globals)
211 config->dynamicList.push_back(v);
Rui Ueyama2ec34542017-04-05 05:07:39212}
213
214void ScriptParser::readVersionScript() {
215 readVersionScriptCommand();
216 if (!atEOF())
217 setError("EOF expected, but got " + next());
218}
219
220void ScriptParser::readVersionScriptCommand() {
221 if (consume("{")) {
222 readAnonymousDeclaration();
223 return;
224 }
225
Bob Haarmanb8a59c82017-10-25 22:28:38226 while (!atEOF() && !errorCount() && peek() != "}") {
Rui Ueyama3837f422019-07-10 05:00:37227 StringRef verStr = next();
228 if (verStr == "{") {
Rui Ueyama2ec34542017-04-05 05:07:39229 setError("anonymous version definition is used in "
230 "combination with other version definitions");
231 return;
232 }
233 expect("{");
Rui Ueyama3837f422019-07-10 05:00:37234 readVersionDeclaration(verStr);
Rui Ueyama2ec34542017-04-05 05:07:39235 }
236}
237
238void ScriptParser::readVersion() {
239 expect("{");
240 readVersionScriptCommand();
241 expect("}");
242}
243
244void ScriptParser::readLinkerScript() {
245 while (!atEOF()) {
Rui Ueyama3837f422019-07-10 05:00:37246 StringRef tok = next();
247 if (tok == ";")
Rui Ueyama2ec34542017-04-05 05:07:39248 continue;
249
Rui Ueyama3837f422019-07-10 05:00:37250 if (tok == "ENTRY") {
Rui Ueyama2ec34542017-04-05 05:07:39251 readEntry();
Rui Ueyama3837f422019-07-10 05:00:37252 } else if (tok == "EXTERN") {
Rui Ueyama2ec34542017-04-05 05:07:39253 readExtern();
Rui Ueyama3837f422019-07-10 05:00:37254 } else if (tok == "GROUP") {
Rui Ueyama2ec34542017-04-05 05:07:39255 readGroup();
Rui Ueyama3837f422019-07-10 05:00:37256 } else if (tok == "INCLUDE") {
Rui Ueyama2ec34542017-04-05 05:07:39257 readInclude();
Rui Ueyama3837f422019-07-10 05:00:37258 } else if (tok == "INPUT") {
Rui Ueyama1d92aa72018-04-09 23:05:48259 readInput();
Rui Ueyama3837f422019-07-10 05:00:37260 } else if (tok == "MEMORY") {
Rui Ueyama2ec34542017-04-05 05:07:39261 readMemory();
Rui Ueyama3837f422019-07-10 05:00:37262 } else if (tok == "OUTPUT") {
Rui Ueyama2ec34542017-04-05 05:07:39263 readOutput();
Rui Ueyama3837f422019-07-10 05:00:37264 } else if (tok == "OUTPUT_ARCH") {
Rui Ueyama2ec34542017-04-05 05:07:39265 readOutputArch();
Rui Ueyama3837f422019-07-10 05:00:37266 } else if (tok == "OUTPUT_FORMAT") {
Rui Ueyama2ec34542017-04-05 05:07:39267 readOutputFormat();
Fangrui Song899fdf52021-06-13 19:41:11268 } else if (tok == "OVERWRITE_SECTIONS") {
269 readOverwriteSections();
Rui Ueyama3837f422019-07-10 05:00:37270 } else if (tok == "PHDRS") {
Rui Ueyama2ec34542017-04-05 05:07:39271 readPhdrs();
Rui Ueyama3837f422019-07-10 05:00:37272 } else if (tok == "REGION_ALIAS") {
George Rimar5f375412017-09-08 08:23:15273 readRegionAlias();
Rui Ueyama3837f422019-07-10 05:00:37274 } else if (tok == "SEARCH_DIR") {
Rui Ueyama2ec34542017-04-05 05:07:39275 readSearchDir();
Rui Ueyama3837f422019-07-10 05:00:37276 } else if (tok == "SECTIONS") {
Rui Ueyama2ec34542017-04-05 05:07:39277 readSections();
Rui Ueyama3837f422019-07-10 05:00:37278 } else if (tok == "TARGET") {
Rui Ueyamae262bb12018-08-06 21:29:41279 readTarget();
Rui Ueyama3837f422019-07-10 05:00:37280 } else if (tok == "VERSION") {
Rui Ueyama2ec34542017-04-05 05:07:39281 readVersion();
Rui Ueyama3837f422019-07-10 05:00:37282 } else if (SymbolAssignment *cmd = readAssignment(tok)) {
283 script->sectionCommands.push_back(cmd);
Rui Ueyama2ec34542017-04-05 05:07:39284 } else {
Rui Ueyama3837f422019-07-10 05:00:37285 setError("unknown directive: " + tok);
Rui Ueyama2ec34542017-04-05 05:07:39286 }
287 }
288}
289
Rui Ueyama3837f422019-07-10 05:00:37290void ScriptParser::readDefsym(StringRef name) {
George Rimarc1522812018-11-26 12:29:56291 if (errorCount())
292 return;
Rui Ueyama3837f422019-07-10 05:00:37293 Expr e = readExpr();
Petr Hosek8c7e8cc2017-11-04 02:03:58294 if (!atEOF())
295 setError("EOF expected, but got " + next());
Fangrui Song43b13342024-01-22 17:09:46296 auto *cmd = make<SymbolAssignment>(
297 name, e, 0, getCurrentMB().getBufferIdentifier().str());
Rui Ueyama3837f422019-07-10 05:00:37298 script->sectionCommands.push_back(cmd);
Petr Hosek8c7e8cc2017-11-04 02:03:58299}
300
Rui Ueyama3837f422019-07-10 05:00:37301void ScriptParser::addFile(StringRef s) {
Fangrui Song8d85c962023-06-05 21:36:19302 if (isUnderSysroot && s.starts_with("/")) {
Rui Ueyama3837f422019-07-10 05:00:37303 SmallString<128> pathData;
304 StringRef path = (config->sysroot + s).toStringRef(pathData);
Fangrui Song25087332021-06-25 19:52:39305 if (sys::fs::exists(path))
Fangrui Songf596d822022-10-01 22:12:50306 ctx.driver.addFile(saver().save(path), /*withLOption=*/false);
Fangrui Song25087332021-06-25 19:52:39307 else
308 setError("cannot find " + s + " inside " + config->sysroot);
309 return;
Rui Ueyama2ec34542017-04-05 05:07:39310 }
311
Fangrui Song8d85c962023-06-05 21:36:19312 if (s.starts_with("/")) {
Fangrui Songc384ca32020-04-09 04:45:21313 // Case 1: s is an absolute path. Just open it.
Fangrui Songf596d822022-10-01 22:12:50314 ctx.driver.addFile(s, /*withLOption=*/false);
Fangrui Song8d85c962023-06-05 21:36:19315 } else if (s.starts_with("=")) {
Fangrui Songc384ca32020-04-09 04:45:21316 // Case 2: relative to the sysroot.
Rui Ueyama3837f422019-07-10 05:00:37317 if (config->sysroot.empty())
Fangrui Songf596d822022-10-01 22:12:50318 ctx.driver.addFile(s.substr(1), /*withLOption=*/false);
Rui Ueyama2ec34542017-04-05 05:07:39319 else
Fangrui Songf596d822022-10-01 22:12:50320 ctx.driver.addFile(saver().save(config->sysroot + "/" + s.substr(1)),
321 /*withLOption=*/false);
Fangrui Song8d85c962023-06-05 21:36:19322 } else if (s.starts_with("-l")) {
Fangrui Songc384ca32020-04-09 04:45:21323 // Case 3: search in the list of library paths.
Fangrui Songf596d822022-10-01 22:12:50324 ctx.driver.addLibrary(s.substr(2));
Rui Ueyama2ec34542017-04-05 05:07:39325 } else {
Fangrui Songc384ca32020-04-09 04:45:21326 // Case 4: s is a relative path. Search in the directory of the script file.
327 std::string filename = std::string(getCurrentMB().getBufferIdentifier());
328 StringRef directory = sys::path::parent_path(filename);
329 if (!directory.empty()) {
330 SmallString<0> path(directory);
331 sys::path::append(path, s);
332 if (sys::fs::exists(path)) {
Fangrui Songf596d822022-10-01 22:12:50333 ctx.driver.addFile(path, /*withLOption=*/false);
Fangrui Songc384ca32020-04-09 04:45:21334 return;
335 }
336 }
337 // Then search in the current working directory.
338 if (sys::fs::exists(s)) {
Fangrui Songf596d822022-10-01 22:12:50339 ctx.driver.addFile(s, /*withLOption=*/false);
Fangrui Songc384ca32020-04-09 04:45:21340 } else {
341 // Finally, search in the list of library paths.
Fangrui Song4191fda2022-11-27 03:19:15342 if (std::optional<std::string> path = findFromSearchPaths(s))
Fangrui Songf596d822022-10-01 22:12:50343 ctx.driver.addFile(saver().save(*path), /*withLOption=*/true);
Fangrui Songc384ca32020-04-09 04:45:21344 else
345 setError("unable to find " + s);
346 }
Rui Ueyama2ec34542017-04-05 05:07:39347 }
348}
349
350void ScriptParser::readAsNeeded() {
351 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37352 bool orig = config->asNeeded;
353 config->asNeeded = true;
Bob Haarmanb8a59c82017-10-25 22:28:38354 while (!errorCount() && !consume(")"))
Rui Ueyama2ec34542017-04-05 05:07:39355 addFile(unquote(next()));
Rui Ueyama3837f422019-07-10 05:00:37356 config->asNeeded = orig;
Rui Ueyama2ec34542017-04-05 05:07:39357}
358
359void ScriptParser::readEntry() {
360 // -e <symbol> takes predecence over ENTRY(<symbol>).
361 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37362 StringRef tok = next();
363 if (config->entry.empty())
Fangrui Song363b2952022-06-25 19:19:45364 config->entry = unquote(tok);
Rui Ueyama2ec34542017-04-05 05:07:39365 expect(")");
366}
367
368void ScriptParser::readExtern() {
369 expect("(");
Bob Haarmanb8a59c82017-10-25 22:28:38370 while (!errorCount() && !consume(")"))
Rui Ueyama3837f422019-07-10 05:00:37371 config->undefined.push_back(unquote(next()));
Rui Ueyama2ec34542017-04-05 05:07:39372}
373
374void ScriptParser::readGroup() {
Rui Ueyama3837f422019-07-10 05:00:37375 bool orig = InputFile::isInGroup;
376 InputFile::isInGroup = true;
Rui Ueyama1d92aa72018-04-09 23:05:48377 readInput();
Rui Ueyama3837f422019-07-10 05:00:37378 InputFile::isInGroup = orig;
379 if (!orig)
380 ++InputFile::nextGroupId;
Rui Ueyama2ec34542017-04-05 05:07:39381}
382
383void ScriptParser::readInclude() {
Rui Ueyama3837f422019-07-10 05:00:37384 StringRef tok = unquote(next());
Rui Ueyama2ec34542017-04-05 05:07:39385
Rui Ueyama3837f422019-07-10 05:00:37386 if (!seen.insert(tok).second) {
Rui Ueyama0440be42017-09-06 18:14:08387 setError("there is a cycle in linker script INCLUDEs");
388 return;
389 }
390
Fangrui Song4191fda2022-11-27 03:19:15391 if (std::optional<std::string> path = searchScript(tok)) {
392 if (std::optional<MemoryBufferRef> mb = readFile(*path))
Rui Ueyama3837f422019-07-10 05:00:37393 tokenize(*mb);
Rui Ueyama2ec34542017-04-05 05:07:39394 return;
395 }
Rui Ueyama3837f422019-07-10 05:00:37396 setError("cannot find linker script " + tok);
Rui Ueyama2ec34542017-04-05 05:07:39397}
398
Rui Ueyama1d92aa72018-04-09 23:05:48399void ScriptParser::readInput() {
400 expect("(");
401 while (!errorCount() && !consume(")")) {
402 if (consume("AS_NEEDED"))
403 readAsNeeded();
404 else
405 addFile(unquote(next()));
406 }
407}
408
Rui Ueyama2ec34542017-04-05 05:07:39409void ScriptParser::readOutput() {
410 // -o <file> takes predecence over OUTPUT(<file>).
411 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37412 StringRef tok = next();
413 if (config->outputFile.empty())
414 config->outputFile = unquote(tok);
Rui Ueyama2ec34542017-04-05 05:07:39415 expect(")");
416}
417
418void ScriptParser::readOutputArch() {
419 // OUTPUT_ARCH is ignored for now.
420 expect("(");
Bob Haarmanb8a59c82017-10-25 22:28:38421 while (!errorCount() && !consume(")"))
Rui Ueyama2ec34542017-04-05 05:07:39422 skip();
423}
424
Rui Ueyama3837f422019-07-10 05:00:37425static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
426 return StringSwitch<std::pair<ELFKind, uint16_t>>(s)
Rui Ueyama4f8c8222019-01-28 19:11:52427 .Case("elf32-i386", {ELF32LEKind, EM_386})
Ben Shi8527f322022-05-12 13:26:15428 .Case("elf32-avr", {ELF32LEKind, EM_AVR})
Rui Ueyama4f8c8222019-01-28 19:11:52429 .Case("elf32-iamcu", {ELF32LEKind, EM_IAMCU})
430 .Case("elf32-littlearm", {ELF32LEKind, EM_ARM})
Simi Pallipurath2f68ddc2023-02-20 13:31:45431 .Case("elf32-bigarm", {ELF32BEKind, EM_ARM})
Rui Ueyama4f8c8222019-01-28 19:11:52432 .Case("elf32-x86-64", {ELF32LEKind, EM_X86_64})
Dimitry Andric19b134c2019-01-30 06:31:52433 .Case("elf64-aarch64", {ELF64LEKind, EM_AARCH64})
Rui Ueyama4f8c8222019-01-28 19:11:52434 .Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64})
Fangrui Song7605a9a2021-02-08 16:55:28435 .Case("elf64-bigaarch64", {ELF64BEKind, EM_AARCH64})
Rui Ueyama41341432019-02-13 18:51:15436 .Case("elf32-powerpc", {ELF32BEKind, EM_PPC})
Brandon Bergren275eb822021-01-02 18:18:05437 .Case("elf32-powerpcle", {ELF32LEKind, EM_PPC})
Rui Ueyama4f8c8222019-01-28 19:11:52438 .Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
439 .Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
440 .Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
Rui Ueyama41341432019-02-13 18:51:15441 .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS})
Rui Ueyama4f8c8222019-01-28 19:11:52442 .Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS})
443 .Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS})
444 .Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS})
445 .Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS})
446 .Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS})
Fangrui Song44d908d2019-06-10 08:09:55447 .Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV})
448 .Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV})
LemonBoyaff950e2020-04-17 14:58:15449 .Case("elf64-sparc", {ELF64BEKind, EM_SPARCV9})
LemonBoy92c61412020-12-14 17:38:12450 .Case("elf32-msp430", {ELF32LEKind, EM_MSP430})
WANG Xuerui6084ee72023-07-25 09:03:28451 .Case("elf32-loongarch", {ELF32LEKind, EM_LOONGARCH})
452 .Case("elf64-loongarch", {ELF64LEKind, EM_LOONGARCH})
Ulrich Weigandfe3406e2024-02-13 10:29:21453 .Case("elf64-s390", {ELF64BEKind, EM_S390})
Rui Ueyama4f8c8222019-01-28 19:11:52454 .Default({ELFNoneKind, EM_NONE});
Rui Ueyamaea8cd002018-10-22 20:50:01455}
456
Fangrui Songeea34aa2021-02-08 18:34:57457// Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(default, big, little). Choose
458// big if -EB is specified, little if -EL is specified, or default if neither is
459// specified.
Rui Ueyama2ec34542017-04-05 05:07:39460void ScriptParser::readOutputFormat() {
Rui Ueyama2ec34542017-04-05 05:07:39461 expect("(");
Rui Ueyamaea8cd002018-10-22 20:50:01462
Fangrui Songeea34aa2021-02-08 18:34:57463 StringRef s;
Shoaib Meenai28228522020-03-12 22:25:36464 config->bfdname = unquote(next());
Fangrui Songeea34aa2021-02-08 18:34:57465 if (!consume(")")) {
466 expect(",");
467 s = unquote(next());
468 if (config->optEB)
469 config->bfdname = s;
470 expect(",");
471 s = unquote(next());
472 if (config->optEL)
473 config->bfdname = s;
474 consume(")");
475 }
476 s = config->bfdname;
Rui Ueyama3837f422019-07-10 05:00:37477 if (s.consume_back("-freebsd"))
478 config->osabi = ELFOSABI_FREEBSD;
Rui Ueyama4f8c8222019-01-28 19:11:52479
Rui Ueyama3837f422019-07-10 05:00:37480 std::tie(config->ekind, config->emachine) = parseBfdName(s);
481 if (config->emachine == EM_NONE)
Shoaib Meenai28228522020-03-12 22:25:36482 setError("unknown output format name: " + config->bfdname);
Rui Ueyama3837f422019-07-10 05:00:37483 if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
484 config->mipsN32Abi = true;
LemonBoy92c61412020-12-14 17:38:12485 if (config->emachine == EM_MSP430)
486 config->osabi = ELFOSABI_STANDALONE;
Rui Ueyama2ec34542017-04-05 05:07:39487}
488
489void ScriptParser::readPhdrs() {
490 expect("{");
Rui Ueyama2ec34542017-04-05 05:07:39491
Bob Haarmanb8a59c82017-10-25 22:28:38492 while (!errorCount() && !consume("}")) {
Rui Ueyama3837f422019-07-10 05:00:37493 PhdrsCommand cmd;
494 cmd.name = next();
495 cmd.type = readPhdrType();
Rui Ueyamab579c432017-04-05 05:40:21496
Bob Haarmanb8a59c82017-10-25 22:28:38497 while (!errorCount() && !consume(";")) {
Rui Ueyamab579c432017-04-05 05:40:21498 if (consume("FILEHDR"))
Rui Ueyama3837f422019-07-10 05:00:37499 cmd.hasFilehdr = true;
Rui Ueyamab579c432017-04-05 05:40:21500 else if (consume("PHDRS"))
Rui Ueyama3837f422019-07-10 05:00:37501 cmd.hasPhdrs = true;
Rui Ueyamab579c432017-04-05 05:40:21502 else if (consume("AT"))
Rui Ueyama3837f422019-07-10 05:00:37503 cmd.lmaExpr = readParenExpr();
Rui Ueyamab579c432017-04-05 05:40:21504 else if (consume("FLAGS"))
Rui Ueyama3837f422019-07-10 05:00:37505 cmd.flags = readParenExpr()().getValue();
Rui Ueyamab579c432017-04-05 05:40:21506 else
507 setError("unexpected header attribute: " + next());
508 }
Rui Ueyama0ae2c242017-10-08 03:45:49509
Rui Ueyama3837f422019-07-10 05:00:37510 script->phdrsCommands.push_back(cmd);
Rui Ueyama2ec34542017-04-05 05:07:39511 }
512}
513
George Rimar5f375412017-09-08 08:23:15514void ScriptParser::readRegionAlias() {
515 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37516 StringRef alias = unquote(next());
George Rimar5f375412017-09-08 08:23:15517 expect(",");
Rui Ueyama3837f422019-07-10 05:00:37518 StringRef name = next();
George Rimar5f375412017-09-08 08:23:15519 expect(")");
520
Rui Ueyama3837f422019-07-10 05:00:37521 if (script->memoryRegions.count(alias))
522 setError("redefinition of memory region '" + alias + "'");
523 if (!script->memoryRegions.count(name))
524 setError("memory region '" + name + "' is not defined");
525 script->memoryRegions.insert({alias, script->memoryRegions[name]});
George Rimar5f375412017-09-08 08:23:15526}
527
Rui Ueyama2ec34542017-04-05 05:07:39528void ScriptParser::readSearchDir() {
529 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37530 StringRef tok = next();
531 if (!config->nostdlib)
532 config->searchPaths.push_back(unquote(tok));
Rui Ueyama2ec34542017-04-05 05:07:39533 expect(")");
534}
535
George Rimara5824192018-06-27 08:08:12536// This reads an overlay description. Overlays are used to describe output
537// sections that use the same virtual memory range and normally would trigger
538// linker's sections sanity check failures.
539// https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
Fangrui Song64038ef2021-12-27 04:12:55540SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() {
Fangrui Song7c89b202024-01-09 00:12:49541 Expr addrExpr;
542 if (consume(":")) {
543 addrExpr = [] { return script->getDot(); };
544 } else {
545 addrExpr = readExpr();
546 expect(":");
547 }
548 // When AT is omitted, LMA should equal VMA. script->getDot() when evaluating
549 // lmaExpr will ensure this, even if the start address is specified.
550 Expr lmaExpr =
551 consume("AT") ? readParenExpr() : [] { return script->getDot(); };
George Rimara5824192018-06-27 08:08:12552 expect("{");
553
Fangrui Song64038ef2021-12-27 04:12:55554 SmallVector<SectionCommand *, 0> v;
Rui Ueyama3837f422019-07-10 05:00:37555 OutputSection *prev = nullptr;
George Rimara5824192018-06-27 08:08:12556 while (!errorCount() && !consume("}")) {
557 // VA is the same for all sections. The LMAs are consecutive in memory
558 // starting from the base load address specified.
Fangrui Song6c814932022-03-08 19:23:41559 OutputDesc *osd = readOverlaySectionDescription();
560 osd->osec.addrExpr = addrExpr;
Fangrui Song7c89b202024-01-09 00:12:49561 if (prev) {
Fangrui Song6c814932022-03-08 19:23:41562 osd->osec.lmaExpr = [=] { return prev->getLMA() + prev->size; };
Fangrui Song7c89b202024-01-09 00:12:49563 } else {
Fangrui Song6c814932022-03-08 19:23:41564 osd->osec.lmaExpr = lmaExpr;
Fangrui Song7c89b202024-01-09 00:12:49565 // Use first section address for subsequent sections as initial addrExpr
566 // can be DOT. Ensure the first section, even if empty, is not discarded.
567 osd->osec.usedInExpression = true;
568 addrExpr = [=]() -> ExprValue { return {&osd->osec, false, 0, ""}; };
569 }
Fangrui Song6c814932022-03-08 19:23:41570 v.push_back(osd);
571 prev = &osd->osec;
George Rimara5824192018-06-27 08:08:12572 }
573
574 // According to the specification, at the end of the overlay, the location
575 // counter should be equal to the overlay base address plus size of the
576 // largest section seen in the overlay.
577 // Here we want to create the Dot assignment command to achieve that.
Rui Ueyama3837f422019-07-10 05:00:37578 Expr moveDot = [=] {
579 uint64_t max = 0;
Fangrui Song7051aee2021-11-26 04:24:23580 for (SectionCommand *cmd : v)
Fangrui Song6c814932022-03-08 19:23:41581 max = std::max(max, cast<OutputDesc>(cmd)->osec.size);
Rui Ueyama3837f422019-07-10 05:00:37582 return addrExpr().getValue() + max;
George Rimara5824192018-06-27 08:08:12583 };
Fangrui Song65a15a52023-09-09 21:46:51584 v.push_back(make<SymbolAssignment>(".", moveDot, 0, getCurrentLocation()));
Rui Ueyama3837f422019-07-10 05:00:37585 return v;
George Rimara5824192018-06-27 08:08:12586}
587
Fangrui Song899fdf52021-06-13 19:41:11588void ScriptParser::readOverwriteSections() {
589 expect("{");
590 while (!errorCount() && !consume("}"))
591 script->overwriteSections.push_back(readOutputSectionDescription(next()));
592}
593
Rui Ueyama2ec34542017-04-05 05:07:39594void ScriptParser::readSections() {
Rui Ueyama2ec34542017-04-05 05:07:39595 expect("{");
Fangrui Song64038ef2021-12-27 04:12:55596 SmallVector<SectionCommand *, 0> v;
Bob Haarmanb8a59c82017-10-25 22:28:38597 while (!errorCount() && !consume("}")) {
Rui Ueyama3837f422019-07-10 05:00:37598 StringRef tok = next();
599 if (tok == "OVERLAY") {
Fangrui Song7051aee2021-11-26 04:24:23600 for (SectionCommand *cmd : readOverlay())
Rui Ueyama3837f422019-07-10 05:00:37601 v.push_back(cmd);
George Rimara5824192018-06-27 08:08:12602 continue;
Rui Ueyama3837f422019-07-10 05:00:37603 } else if (tok == "INCLUDE") {
Rui Ueyama2e9d40d2018-10-12 17:07:32604 readInclude();
605 continue;
George Rimara5824192018-06-27 08:08:12606 }
607
Fangrui Song7051aee2021-11-26 04:24:23608 if (SectionCommand *cmd = readAssignment(tok))
Rui Ueyama3837f422019-07-10 05:00:37609 v.push_back(cmd);
George Rimard30a78b2018-04-25 11:16:31610 else
Fangrui Song49dfbc62023-07-05 22:08:53611 v.push_back(readOutputSectionDescription(tok));
Rui Ueyama2ec34542017-04-05 05:07:39612 }
Fangrui Song5a449802022-05-04 08:10:45613
614 // If DATA_SEGMENT_RELRO_END is absent, for sections after DATA_SEGMENT_ALIGN,
615 // the relro fields should be cleared.
Fangrui Song5a58e982023-09-14 17:33:11616 if (!script->seenRelroEnd)
Fangrui Song5a449802022-05-04 08:10:45617 for (SectionCommand *cmd : v)
618 if (auto *osd = dyn_cast<OutputDesc>(cmd))
619 osd->osec.relro = false;
620
Fangrui Song7c426fb2020-02-10 23:58:29621 script->sectionCommands.insert(script->sectionCommands.end(), v.begin(),
622 v.end());
George Rimar9e2c8a92018-03-08 14:54:38623
Fangrui Song7c426fb2020-02-10 23:58:29624 if (atEOF() || !consume("INSERT")) {
Fangrui Song7c426fb2020-02-10 23:58:29625 script->hasSectionsCommand = true;
George Rimar9e2c8a92018-03-08 14:54:38626 return;
627 }
628
Fangrui Song7c426fb2020-02-10 23:58:29629 bool isAfter = false;
630 if (consume("AFTER"))
631 isAfter = true;
632 else if (!consume("BEFORE"))
633 setError("expected AFTER/BEFORE, but got '" + next() + "'");
634 StringRef where = next();
Fangrui Songa1c2ee02021-12-26 21:53:47635 SmallVector<StringRef, 0> names;
Fangrui Song7051aee2021-11-26 04:24:23636 for (SectionCommand *cmd : v)
Fangrui Song6c814932022-03-08 19:23:41637 if (auto *os = dyn_cast<OutputDesc>(cmd))
638 names.push_back(os->osec.name);
Fangrui Song03051f72021-06-30 18:35:50639 if (!names.empty())
640 script->insertCommands.push_back({std::move(names), isAfter, where});
Rui Ueyama2ec34542017-04-05 05:07:39641}
642
Rui Ueyamae262bb12018-08-06 21:29:41643void ScriptParser::readTarget() {
644 // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
645 // we accept only a limited set of BFD names (i.e. "elf" or "binary")
646 // for --format. We recognize only /^elf/ and "binary" in the linker
647 // script as well.
648 expect("(");
Fangrui Song4cb05dc2022-06-25 19:31:20649 StringRef tok = unquote(next());
Rui Ueyamae262bb12018-08-06 21:29:41650 expect(")");
651
Fangrui Song8d85c962023-06-05 21:36:19652 if (tok.starts_with("elf"))
Rui Ueyama3837f422019-07-10 05:00:37653 config->formatBinary = false;
654 else if (tok == "binary")
655 config->formatBinary = true;
Rui Ueyamae262bb12018-08-06 21:29:41656 else
Rui Ueyama3837f422019-07-10 05:00:37657 setError("unknown target: " + tok);
Rui Ueyamae262bb12018-08-06 21:29:41658}
659
Rui Ueyama3837f422019-07-10 05:00:37660static int precedence(StringRef op) {
661 return StringSwitch<int>(op)
Fangrui Songfae96102023-07-15 21:10:40662 .Cases("*", "/", "%", 11)
663 .Cases("+", "-", 10)
664 .Cases("<<", ">>", 9)
665 .Cases("<", "<=", ">", ">=", 8)
666 .Cases("==", "!=", 7)
667 .Case("&", 6)
668 .Case("^", 5)
Fangrui Songd479b2e2022-06-25 20:47:32669 .Case("|", 4)
670 .Case("&&", 3)
671 .Case("||", 2)
Fangrui Songb0d6dd32022-06-25 20:48:52672 .Case("?", 1)
Rui Ueyama2ec34542017-04-05 05:07:39673 .Default(-1);
674}
675
676StringMatcher ScriptParser::readFilePatterns() {
Thomas Preud'hommec42fe242020-01-10 16:56:07677 StringMatcher Matcher;
678
Bob Haarmanb8a59c82017-10-25 22:28:38679 while (!errorCount() && !consume(")"))
Thomas Preud'hommec42fe242020-01-10 16:56:07680 Matcher.addPattern(SingleStringMatcher(next()));
681 return Matcher;
Rui Ueyama2ec34542017-04-05 05:07:39682}
683
Fangrui Song2a9aed02020-11-12 16:46:53684SortSectionPolicy ScriptParser::peekSortKind() {
685 return StringSwitch<SortSectionPolicy>(peek())
Justin Cady447aa482023-03-06 14:35:39686 .Case("REVERSE", SortSectionPolicy::Reverse)
Fangrui Song2a9aed02020-11-12 16:46:53687 .Cases("SORT", "SORT_BY_NAME", SortSectionPolicy::Name)
688 .Case("SORT_BY_ALIGNMENT", SortSectionPolicy::Alignment)
689 .Case("SORT_BY_INIT_PRIORITY", SortSectionPolicy::Priority)
690 .Case("SORT_NONE", SortSectionPolicy::None)
691 .Default(SortSectionPolicy::Default);
692}
693
Rui Ueyama2ec34542017-04-05 05:07:39694SortSectionPolicy ScriptParser::readSortKind() {
Fangrui Song2a9aed02020-11-12 16:46:53695 SortSectionPolicy ret = peekSortKind();
696 if (ret != SortSectionPolicy::Default)
697 skip();
698 return ret;
Rui Ueyama2ec34542017-04-05 05:07:39699}
700
Rui Ueyama03fc8d12017-04-05 19:20:54701// Reads SECTIONS command contents in the following form:
702//
703// <contents> ::= <elem>*
704// <elem> ::= <exclude>? <glob-pattern>
705// <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
706//
707// For example,
708//
709// *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
710//
711// is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
712// The semantics of that is section .foo in any file, section .bar in
713// any file but a.o, and section .baz in any file but b.o.
Fangrui Song64038ef2021-12-27 04:12:55714SmallVector<SectionPattern, 0> ScriptParser::readInputSectionsList() {
715 SmallVector<SectionPattern, 0> ret;
Bob Haarmanb8a59c82017-10-25 22:28:38716 while (!errorCount() && peek() != ")") {
Rui Ueyama3837f422019-07-10 05:00:37717 StringMatcher excludeFilePat;
Rui Ueyama2ec34542017-04-05 05:07:39718 if (consume("EXCLUDE_FILE")) {
719 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37720 excludeFilePat = readFilePatterns();
Rui Ueyama2ec34542017-04-05 05:07:39721 }
722
Thomas Preud'hommec42fe242020-01-10 16:56:07723 StringMatcher SectionMatcher;
Fangrui Song2a9aed02020-11-12 16:46:53724 // Break if the next token is ), EXCLUDE_FILE, or SORT*.
Fangrui Song551e20d2024-03-07 01:19:59725 while (!errorCount() && peekSortKind() == SortSectionPolicy::Default) {
726 StringRef s = peek();
727 if (s == ")" || s == "EXCLUDE_FILE")
728 break;
729 // Detect common mistakes when certain non-wildcard meta characters are
730 // used without a closing ')'.
731 if (!s.empty() && strchr("(){}", s[0])) {
732 skip();
733 setError("section pattern is expected");
734 break;
735 }
Thomas Preud'hommec42fe242020-01-10 16:56:07736 SectionMatcher.addPattern(unquote(next()));
Fangrui Song551e20d2024-03-07 01:19:59737 }
Rui Ueyama2ec34542017-04-05 05:07:39738
Thomas Preud'hommec42fe242020-01-10 16:56:07739 if (!SectionMatcher.empty())
740 ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});
Fangrui Song2a9aed02020-11-12 16:46:53741 else if (excludeFilePat.empty())
742 break;
Rui Ueyama2ec34542017-04-05 05:07:39743 else
744 setError("section pattern is expected");
745 }
Rui Ueyama3837f422019-07-10 05:00:37746 return ret;
Rui Ueyama2ec34542017-04-05 05:07:39747}
748
749// Reads contents of "SECTIONS" directive. That directive contains a
750// list of glob patterns for input sections. The grammar is as follows.
751//
752// <patterns> ::= <section-list>
753// | <sort> "(" <section-list> ")"
754// | <sort> "(" <sort> "(" <section-list> ")" ")"
755//
756// <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
757// | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
758//
759// <section-list> is parsed by readInputSectionsList().
760InputSectionDescription *
Peter Smithdbd0ad32020-01-15 09:38:00761ScriptParser::readInputSectionRules(StringRef filePattern, uint64_t withFlags,
762 uint64_t withoutFlags) {
763 auto *cmd =
764 make<InputSectionDescription>(filePattern, withFlags, withoutFlags);
Rui Ueyama2ec34542017-04-05 05:07:39765 expect("(");
766
Bob Haarmanb8a59c82017-10-25 22:28:38767 while (!errorCount() && !consume(")")) {
Rui Ueyama3837f422019-07-10 05:00:37768 SortSectionPolicy outer = readSortKind();
769 SortSectionPolicy inner = SortSectionPolicy::Default;
Fangrui Song64038ef2021-12-27 04:12:55770 SmallVector<SectionPattern, 0> v;
Rui Ueyama3837f422019-07-10 05:00:37771 if (outer != SortSectionPolicy::Default) {
Rui Ueyama2ec34542017-04-05 05:07:39772 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37773 inner = readSortKind();
774 if (inner != SortSectionPolicy::Default) {
Rui Ueyama2ec34542017-04-05 05:07:39775 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37776 v = readInputSectionsList();
Rui Ueyama2ec34542017-04-05 05:07:39777 expect(")");
778 } else {
Rui Ueyama3837f422019-07-10 05:00:37779 v = readInputSectionsList();
Rui Ueyama2ec34542017-04-05 05:07:39780 }
781 expect(")");
782 } else {
Rui Ueyama3837f422019-07-10 05:00:37783 v = readInputSectionsList();
Rui Ueyama2ec34542017-04-05 05:07:39784 }
785
Rui Ueyama3837f422019-07-10 05:00:37786 for (SectionPattern &pat : v) {
787 pat.sortInner = inner;
788 pat.sortOuter = outer;
Rui Ueyama2ec34542017-04-05 05:07:39789 }
790
Rui Ueyama3837f422019-07-10 05:00:37791 std::move(v.begin(), v.end(), std::back_inserter(cmd->sectionPatterns));
Rui Ueyama2ec34542017-04-05 05:07:39792 }
Rui Ueyama3837f422019-07-10 05:00:37793 return cmd;
Rui Ueyama2ec34542017-04-05 05:07:39794}
795
796InputSectionDescription *
Rui Ueyama3837f422019-07-10 05:00:37797ScriptParser::readInputSectionDescription(StringRef tok) {
Rui Ueyama2ec34542017-04-05 05:07:39798 // Input section wildcard can be surrounded by KEEP.
799 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
Peter Smithdbd0ad32020-01-15 09:38:00800 uint64_t withFlags = 0;
801 uint64_t withoutFlags = 0;
Rui Ueyama3837f422019-07-10 05:00:37802 if (tok == "KEEP") {
Rui Ueyama2ec34542017-04-05 05:07:39803 expect("(");
Peter Smithdbd0ad32020-01-15 09:38:00804 if (consume("INPUT_SECTION_FLAGS"))
805 std::tie(withFlags, withoutFlags) = readInputSectionFlags();
806 InputSectionDescription *cmd =
807 readInputSectionRules(next(), withFlags, withoutFlags);
Rui Ueyama2ec34542017-04-05 05:07:39808 expect(")");
Rui Ueyama3837f422019-07-10 05:00:37809 script->keptSections.push_back(cmd);
810 return cmd;
Rui Ueyama2ec34542017-04-05 05:07:39811 }
Peter Smithdbd0ad32020-01-15 09:38:00812 if (tok == "INPUT_SECTION_FLAGS") {
813 std::tie(withFlags, withoutFlags) = readInputSectionFlags();
814 tok = next();
815 }
816 return readInputSectionRules(tok, withFlags, withoutFlags);
Rui Ueyama2ec34542017-04-05 05:07:39817}
818
819void ScriptParser::readSort() {
820 expect("(");
821 expect("CONSTRUCTORS");
822 expect(")");
823}
824
George Rimard30a78b2018-04-25 11:16:31825Expr ScriptParser::readAssert() {
Rui Ueyama2ec34542017-04-05 05:07:39826 expect("(");
Rui Ueyama3837f422019-07-10 05:00:37827 Expr e = readExpr();
Rui Ueyama2ec34542017-04-05 05:07:39828 expect(",");
Rui Ueyama3837f422019-07-10 05:00:37829 StringRef msg = unquote(next());
Rui Ueyama2ec34542017-04-05 05:07:39830 expect(")");
Rui Ueyamab579c432017-04-05 05:40:21831
Rui Ueyama2ec34542017-04-05 05:07:39832 return [=] {
Rui Ueyama3837f422019-07-10 05:00:37833 if (!e().getValue())
Fangrui Song2682bc32019-09-06 16:30:22834 errorOrWarn(msg);
Rui Ueyama3837f422019-07-10 05:00:37835 return script->getDot();
Rui Ueyama2ec34542017-04-05 05:07:39836 };
837}
838
Fangrui Song66f8ac82022-02-17 20:10:58839#define ECase(X) \
840 { #X, X }
841constexpr std::pair<const char *, unsigned> typeMap[] = {
842 ECase(SHT_PROGBITS), ECase(SHT_NOTE), ECase(SHT_NOBITS),
843 ECase(SHT_INIT_ARRAY), ECase(SHT_FINI_ARRAY), ECase(SHT_PREINIT_ARRAY),
844};
845#undef ECase
846
George Rimara46d08e2018-08-28 08:39:21847// Tries to read the special directive for an output section definition which
Fangrui Song66f8ac82022-02-17 20:10:58848// can be one of following: "(NOLOAD)", "(COPY)", "(INFO)", "(OVERLAY)", and
849// "(TYPE=<value>)".
850// Tok1 and Tok2 are next 2 tokens peeked. See comment for
851// readSectionAddressType below.
Rui Ueyama3837f422019-07-10 05:00:37852bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2) {
853 if (tok1 != "(")
George Rimara46d08e2018-08-28 08:39:21854 return false;
Fangrui Song66f8ac82022-02-17 20:10:58855 if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" &&
856 tok2 != "OVERLAY" && tok2 != "TYPE")
George Rimara46d08e2018-08-28 08:39:21857 return false;
858
859 expect("(");
860 if (consume("NOLOAD")) {
Matt Schultefdc41aa2020-03-28 16:54:06861 cmd->type = SHT_NOBITS;
Fangrui Song66f8ac82022-02-17 20:10:58862 cmd->typeIsSet = true;
863 } else if (consume("TYPE")) {
864 expect("=");
865 StringRef value = peek();
866 auto it = llvm::find_if(typeMap, [=](auto e) { return e.first == value; });
867 if (it != std::end(typeMap)) {
868 // The value is a recognized literal SHT_*.
869 cmd->type = it->second;
870 skip();
Fangrui Song8d85c962023-06-05 21:36:19871 } else if (value.starts_with("SHT_")) {
Fangrui Song66f8ac82022-02-17 20:10:58872 setError("unknown section type " + value);
873 } else {
874 // Otherwise, read an expression.
875 cmd->type = readExpr()().getValue();
876 }
877 cmd->typeIsSet = true;
George Rimara46d08e2018-08-28 08:39:21878 } else {
879 skip(); // This is "COPY", "INFO" or "OVERLAY".
Rui Ueyama3837f422019-07-10 05:00:37880 cmd->nonAlloc = true;
George Rimara46d08e2018-08-28 08:39:21881 }
882 expect(")");
883 return true;
884}
885
George Rimar1c08e9f2018-02-16 10:42:58886// Reads an expression and/or the special directive for an output
887// section definition. Directive is one of following: "(NOLOAD)",
888// "(COPY)", "(INFO)" or "(OVERLAY)".
Rui Ueyama3271d372017-06-08 19:47:16889//
890// An output section name can be followed by an address expression
George Rimar1c08e9f2018-02-16 10:42:58891// and/or directive. This grammar is not LL(1) because "(" can be
George Rimar97f4d152018-02-16 10:46:50892// interpreted as either the beginning of some expression or beginning
George Rimar1c08e9f2018-02-16 10:42:58893// of directive.
Rui Ueyama3271d372017-06-08 19:47:16894//
895// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
896// https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
Rui Ueyama3837f422019-07-10 05:00:37897void ScriptParser::readSectionAddressType(OutputSection *cmd) {
Fangrui Song66f8ac82022-02-17 20:10:58898 // Temporarily set inExpr to support TYPE=<value> without spaces.
899 bool saved = std::exchange(inExpr, true);
900 bool isDirective = readSectionDirective(cmd, peek(), peek2());
901 inExpr = saved;
902 if (isDirective)
George Rimara46d08e2018-08-28 08:39:21903 return;
Rui Ueyama3271d372017-06-08 19:47:16904
Rui Ueyama3837f422019-07-10 05:00:37905 cmd->addrExpr = readExpr();
906 if (peek() == "(" && !readSectionDirective(cmd, "(", peek2()))
George Rimara46d08e2018-08-28 08:39:21907 setError("unknown section directive: " + peek2());
Rui Ueyama3271d372017-06-08 19:47:16908}
909
Rui Ueyama3837f422019-07-10 05:00:37910static Expr checkAlignment(Expr e, std::string &loc) {
George Rimarf22ec9d2017-10-25 14:50:51911 return [=] {
Rui Ueyama3837f422019-07-10 05:00:37912 uint64_t alignment = std::max((uint64_t)1, e().getValue());
913 if (!isPowerOf2_64(alignment)) {
914 error(loc + ": alignment must be power of 2");
George Rimarf22ec9d2017-10-25 14:50:51915 return (uint64_t)1; // Return a dummy value.
916 }
Rui Ueyama3837f422019-07-10 05:00:37917 return alignment;
George Rimarf22ec9d2017-10-25 14:50:51918 };
919}
920
Fangrui Song6c814932022-03-08 19:23:41921OutputDesc *ScriptParser::readOverlaySectionDescription() {
922 OutputDesc *osd = script->createOutputSection(next(), getCurrentLocation());
923 osd->osec.inOverlay = true;
George Rimara5824192018-06-27 08:08:12924 expect("{");
Peter Smithdbd0ad32020-01-15 09:38:00925 while (!errorCount() && !consume("}")) {
926 uint64_t withFlags = 0;
927 uint64_t withoutFlags = 0;
928 if (consume("INPUT_SECTION_FLAGS"))
929 std::tie(withFlags, withoutFlags) = readInputSectionFlags();
Fangrui Song6c814932022-03-08 19:23:41930 osd->osec.commands.push_back(
Peter Smithdbd0ad32020-01-15 09:38:00931 readInputSectionRules(next(), withFlags, withoutFlags));
932 }
Peter Smithe16af8a2023-05-11 11:00:19933 osd->osec.phdrs = readOutputSectionPhdrs();
Fangrui Song6c814932022-03-08 19:23:41934 return osd;
George Rimara5824192018-06-27 08:08:12935}
936
Fangrui Song6c814932022-03-08 19:23:41937OutputDesc *ScriptParser::readOutputSectionDescription(StringRef outSec) {
Fangrui Songd60ef932023-02-03 19:03:00938 OutputDesc *cmd =
939 script->createOutputSection(unquote(outSec), getCurrentLocation());
Fangrui Song6c814932022-03-08 19:23:41940 OutputSection *osec = &cmd->osec;
Fangrui Song5a449802022-05-04 08:10:45941 // Maybe relro. Will reset to false if DATA_SEGMENT_RELRO_END is absent.
Fangrui Song5a58e982023-09-14 17:33:11942 osec->relro = script->seenDataAlign && !script->seenRelroEnd;
Rui Ueyama2ec34542017-04-05 05:07:39943
Rui Ueyama3837f422019-07-10 05:00:37944 size_t symbolsReferenced = script->referencedSymbols.size();
George Rimarc4df6702018-03-01 12:27:04945
Rui Ueyama3271d372017-06-08 19:47:16946 if (peek() != ":")
Fangrui Song6c814932022-03-08 19:23:41947 readSectionAddressType(osec);
Rui Ueyama2ec34542017-04-05 05:07:39948 expect(":");
949
Rui Ueyama3837f422019-07-10 05:00:37950 std::string location = getCurrentLocation();
Rui Ueyama2ec34542017-04-05 05:07:39951 if (consume("AT"))
Fangrui Song6c814932022-03-08 19:23:41952 osec->lmaExpr = readParenExpr();
Rui Ueyama2ec34542017-04-05 05:07:39953 if (consume("ALIGN"))
Fangrui Song6c814932022-03-08 19:23:41954 osec->alignExpr = checkAlignment(readParenExpr(), location);
Rui Ueyama2ec34542017-04-05 05:07:39955 if (consume("SUBALIGN"))
Fangrui Song6c814932022-03-08 19:23:41956 osec->subalignExpr = checkAlignment(readParenExpr(), location);
Rui Ueyama2ec34542017-04-05 05:07:39957
958 // Parse constraints.
959 if (consume("ONLY_IF_RO"))
Fangrui Song6c814932022-03-08 19:23:41960 osec->constraint = ConstraintKind::ReadOnly;
Rui Ueyama2ec34542017-04-05 05:07:39961 if (consume("ONLY_IF_RW"))
Fangrui Song6c814932022-03-08 19:23:41962 osec->constraint = ConstraintKind::ReadWrite;
Rui Ueyama2ec34542017-04-05 05:07:39963 expect("{");
964
Bob Haarmanb8a59c82017-10-25 22:28:38965 while (!errorCount() && !consume("}")) {
Rui Ueyama3837f422019-07-10 05:00:37966 StringRef tok = next();
967 if (tok == ";") {
Rui Ueyama2ec34542017-04-05 05:07:39968 // Empty commands are allowed. Do nothing here.
Rui Ueyama3837f422019-07-10 05:00:37969 } else if (SymbolAssignment *assign = readAssignment(tok)) {
Fangrui Song6c814932022-03-08 19:23:41970 osec->commands.push_back(assign);
Rui Ueyama3837f422019-07-10 05:00:37971 } else if (ByteCommand *data = readByteCommand(tok)) {
Fangrui Song6c814932022-03-08 19:23:41972 osec->commands.push_back(data);
Rui Ueyama3837f422019-07-10 05:00:37973 } else if (tok == "CONSTRUCTORS") {
Rui Ueyama2ec34542017-04-05 05:07:39974 // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
975 // by name. This is for very old file formats such as ECOFF/XCOFF.
976 // For ELF, we should ignore.
Rui Ueyama3837f422019-07-10 05:00:37977 } else if (tok == "FILL") {
George Rimar0810f162019-07-04 14:17:31978 // We handle the FILL command as an alias for =fillexp section attribute,
979 // which is different from what GNU linkers do.
980 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
Georgii Rymarbb7d2b12020-02-16 11:17:26981 if (peek() != "(")
982 setError("( expected, but got " + peek());
Fangrui Song6c814932022-03-08 19:23:41983 osec->filler = readFill();
Rui Ueyama3837f422019-07-10 05:00:37984 } else if (tok == "SORT") {
Rui Ueyama2ec34542017-04-05 05:07:39985 readSort();
Rui Ueyama3837f422019-07-10 05:00:37986 } else if (tok == "INCLUDE") {
Rui Ueyama2e9d40d2018-10-12 17:07:32987 readInclude();
Fangrui Song177fd722022-05-13 18:06:01988 } else if (tok == "(" || tok == ")") {
989 setError("expected filename pattern");
Rui Ueyama2ec34542017-04-05 05:07:39990 } else if (peek() == "(") {
Fangrui Song6c814932022-03-08 19:23:41991 osec->commands.push_back(readInputSectionDescription(tok));
Rui Ueyama2ec34542017-04-05 05:07:39992 } else {
George Rimarf49fe212018-12-06 08:34:52993 // We have a file name and no input sections description. It is not a
994 // commonly used syntax, but still acceptable. In that case, all sections
995 // from the file will be included.
Peter Smithdbd0ad32020-01-15 09:38:00996 // FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not
997 // handle this case here as it will already have been matched by the
998 // case above.
Rui Ueyama3837f422019-07-10 05:00:37999 auto *isd = make<InputSectionDescription>(tok);
Thomas Preud'hommec42fe242020-01-10 16:56:071000 isd->sectionPatterns.push_back({{}, StringMatcher("*")});
Fangrui Song6c814932022-03-08 19:23:411001 osec->commands.push_back(isd);
Rui Ueyama2ec34542017-04-05 05:07:391002 }
1003 }
1004
1005 if (consume(">"))
Fangrui Song6c814932022-03-08 19:23:411006 osec->memoryRegionName = std::string(next());
Rui Ueyama2ec34542017-04-05 05:07:391007
George Rimar5d01a8b2018-01-12 09:07:351008 if (consume("AT")) {
1009 expect(">");
Fangrui Song6c814932022-03-08 19:23:411010 osec->lmaRegionName = std::string(next());
George Rimar5d01a8b2018-01-12 09:07:351011 }
1012
Fangrui Song6c814932022-03-08 19:23:411013 if (osec->lmaExpr && !osec->lmaRegionName.empty())
George Rimar5d01a8b2018-01-12 09:07:351014 error("section can't have both LMA and a load region");
1015
Fangrui Song6c814932022-03-08 19:23:411016 osec->phdrs = readOutputSectionPhdrs();
Rui Ueyama2ec34542017-04-05 05:07:391017
Fangrui Song8d85c962023-06-05 21:36:191018 if (peek() == "=" || peek().starts_with("=")) {
Rui Ueyama3837f422019-07-10 05:00:371019 inExpr = true;
George Rimar0810f162019-07-04 14:17:311020 consume("=");
Fangrui Song6c814932022-03-08 19:23:411021 osec->filler = readFill();
Rui Ueyama3837f422019-07-10 05:00:371022 inExpr = false;
George Rimar0810f162019-07-04 14:17:311023 }
Rui Ueyama2ec34542017-04-05 05:07:391024
1025 // Consume optional comma following output section command.
1026 consume(",");
1027
Rui Ueyama3837f422019-07-10 05:00:371028 if (script->referencedSymbols.size() > symbolsReferenced)
Fangrui Song6c814932022-03-08 19:23:411029 osec->expressionsUseSymbols = true;
Rui Ueyama3837f422019-07-10 05:00:371030 return cmd;
Rui Ueyama2ec34542017-04-05 05:07:391031}
1032
George Rimar0810f162019-07-04 14:17:311033// Reads a `=<fillexp>` expression and returns its value as a big-endian number.
Rui Ueyama2ec34542017-04-05 05:07:391034// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
George Rimar0810f162019-07-04 14:17:311035// We do not support using symbols in such expressions.
Rui Ueyama2ec34542017-04-05 05:07:391036//
Rui Ueyama8acbf1c2017-04-13 23:40:191037// When reading a hexstring, ld.bfd handles it as a blob of arbitrary
1038// size, while ld.gold always handles it as a 32-bit big-endian number.
1039// We are compatible with ld.gold because it's easier to implement.
Georgii Rymarbb7d2b12020-02-16 11:17:261040// Also, we require that expressions with operators must be wrapped into
1041// round brackets. We did it to resolve the ambiguity when parsing scripts like:
1042// SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } }
George Rimar0810f162019-07-04 14:17:311043std::array<uint8_t, 4> ScriptParser::readFill() {
Georgii Rymarbb7d2b12020-02-16 11:17:261044 uint64_t value = readPrimary()().val;
Rui Ueyama3837f422019-07-10 05:00:371045 if (value > UINT32_MAX)
George Rimar0810f162019-07-04 14:17:311046 setError("filler expression result does not fit 32-bit: 0x" +
Rui Ueyama3837f422019-07-10 05:00:371047 Twine::utohexstr(value));
Rui Ueyamab58079d42017-04-11 22:45:571048
Rui Ueyama3837f422019-07-10 05:00:371049 std::array<uint8_t, 4> buf;
1050 write32be(buf.data(), (uint32_t)value);
1051 return buf;
Rui Ueyama2ec34542017-04-05 05:07:391052}
1053
Rui Ueyama3837f422019-07-10 05:00:371054SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
Rui Ueyama2ec34542017-04-05 05:07:391055 expect("(");
Fangrui Song21bf6bb2022-06-26 03:26:471056 StringRef name = next(), eq = peek();
1057 if (eq != "=") {
1058 setError("= expected, but got " + next());
1059 while (!atEOF() && next() != ")")
1060 ;
1061 return nullptr;
1062 }
Parth Aroraebb326a2024-03-25 23:11:211063 llvm::SaveAndRestore saveActiveProvideSym(activeProvideSym);
1064 if (provide)
1065 activeProvideSym = name;
Fangrui Song21bf6bb2022-06-26 03:26:471066 SymbolAssignment *cmd = readSymbolAssignment(name);
Rui Ueyama3837f422019-07-10 05:00:371067 cmd->provide = provide;
1068 cmd->hidden = hidden;
Rui Ueyama2ec34542017-04-05 05:07:391069 expect(")");
Rui Ueyama3837f422019-07-10 05:00:371070 return cmd;
Rui Ueyama2ec34542017-04-05 05:07:391071}
1072
Rui Ueyama3837f422019-07-10 05:00:371073SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
George Rimard30a78b2018-04-25 11:16:311074 // Assert expression returns Dot, so this is equal to ".=."
Rui Ueyama3837f422019-07-10 05:00:371075 if (tok == "ASSERT")
Fangrui Song65a15a52023-09-09 21:46:511076 return make<SymbolAssignment>(".", readAssert(), 0, getCurrentLocation());
George Rimard30a78b2018-04-25 11:16:311077
Rui Ueyama3837f422019-07-10 05:00:371078 size_t oldPos = pos;
1079 SymbolAssignment *cmd = nullptr;
Fangrui Song5a58e982023-09-14 17:33:111080 bool savedSeenRelroEnd = script->seenRelroEnd;
Fangrui Song0a0effd2022-06-26 05:22:591081 const StringRef op = peek();
Fangrui Song8d85c962023-06-05 21:36:191082 if (op.starts_with("=")) {
Fangrui Songfe0de252022-06-26 03:25:341083 // Support = followed by an expression without whitespace.
Jan Svobodaabf0c6c2022-12-02 21:36:041084 SaveAndRestore saved(inExpr, true);
Rui Ueyama3837f422019-07-10 05:00:371085 cmd = readSymbolAssignment(tok);
Fangrui Songfae96102023-07-15 21:10:401086 } else if ((op.size() == 2 && op[1] == '=' && strchr("*/+-&^|", op[0])) ||
Fangrui Song0a0effd2022-06-26 05:22:591087 op == "<<=" || op == ">>=") {
Fangrui Songfe0de252022-06-26 03:25:341088 cmd = readSymbolAssignment(tok);
1089 } else if (tok == "PROVIDE") {
Jan Svobodaabf0c6c2022-12-02 21:36:041090 SaveAndRestore saved(inExpr, true);
Rui Ueyama3837f422019-07-10 05:00:371091 cmd = readProvideHidden(true, false);
Fangrui Songfe0de252022-06-26 03:25:341092 } else if (tok == "HIDDEN") {
Jan Svobodaabf0c6c2022-12-02 21:36:041093 SaveAndRestore saved(inExpr, true);
Rui Ueyama3837f422019-07-10 05:00:371094 cmd = readProvideHidden(false, true);
Fangrui Songfe0de252022-06-26 03:25:341095 } else if (tok == "PROVIDE_HIDDEN") {
Jan Svobodaabf0c6c2022-12-02 21:36:041096 SaveAndRestore saved(inExpr, true);
Rui Ueyama3837f422019-07-10 05:00:371097 cmd = readProvideHidden(true, true);
Fangrui Songfe0de252022-06-26 03:25:341098 }
George Rimare88b76a2018-04-05 11:25:581099
Rui Ueyama3837f422019-07-10 05:00:371100 if (cmd) {
Fangrui Song5a58e982023-09-14 17:33:111101 cmd->dataSegmentRelroEnd = !savedSeenRelroEnd && script->seenRelroEnd;
Rui Ueyama3837f422019-07-10 05:00:371102 cmd->commandString =
1103 tok.str() + " " +
1104 llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
George Rimare88b76a2018-04-05 11:25:581105 expect(";");
Rui Ueyama2ec34542017-04-05 05:07:391106 }
Rui Ueyama3837f422019-07-10 05:00:371107 return cmd;
Rui Ueyama2ec34542017-04-05 05:07:391108}
1109
Rui Ueyama3837f422019-07-10 05:00:371110SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
Fangrui Songe7a7ad12021-07-25 23:26:371111 name = unquote(name);
Rui Ueyama3837f422019-07-10 05:00:371112 StringRef op = next();
Fangrui Song0a0effd2022-06-26 05:22:591113 assert(op == "=" || op == "*=" || op == "/=" || op == "+=" || op == "-=" ||
Fangrui Songfae96102023-07-15 21:10:401114 op == "&=" || op == "^=" || op == "|=" || op == "<<=" || op == ">>=");
Fangrui Song1bd5df72023-09-16 00:52:481115 // Note: GNU ld does not support %=.
Rui Ueyama3837f422019-07-10 05:00:371116 Expr e = readExpr();
Fangrui Song0a0effd2022-06-26 05:22:591117 if (op != "=") {
Rui Ueyama3837f422019-07-10 05:00:371118 std::string loc = getCurrentLocation();
Fangrui Song0a0effd2022-06-26 05:22:591119 e = [=, c = op[0]]() -> ExprValue {
1120 ExprValue lhs = script->getSymbolValue(name, loc);
1121 switch (c) {
1122 case '*':
1123 return lhs.getValue() * e().getValue();
1124 case '/':
1125 if (uint64_t rv = e().getValue())
1126 return lhs.getValue() / rv;
1127 error(loc + ": division by zero");
1128 return 0;
1129 case '+':
1130 return add(lhs, e());
1131 case '-':
1132 return sub(lhs, e());
1133 case '<':
Fangrui Songdaba24e2023-06-15 17:34:331134 return lhs.getValue() << e().getValue() % 64;
Fangrui Song0a0effd2022-06-26 05:22:591135 case '>':
Fangrui Songdaba24e2023-06-15 17:34:331136 return lhs.getValue() >> e().getValue() % 64;
Fangrui Song0a0effd2022-06-26 05:22:591137 case '&':
1138 return lhs.getValue() & e().getValue();
Fangrui Songfae96102023-07-15 21:10:401139 case '^':
1140 return lhs.getValue() ^ e().getValue();
Fangrui Song0a0effd2022-06-26 05:22:591141 case '|':
1142 return lhs.getValue() | e().getValue();
1143 default:
1144 llvm_unreachable("");
1145 }
1146 };
Rui Ueyama2ec34542017-04-05 05:07:391147 }
Fangrui Song65a15a52023-09-09 21:46:511148 return make<SymbolAssignment>(name, e, ctx.scriptSymOrderCounter++,
1149 getCurrentLocation());
Rui Ueyama2ec34542017-04-05 05:07:391150}
1151
1152// This is an operator-precedence parser to parse a linker
1153// script expression.
1154Expr ScriptParser::readExpr() {
1155 // Our lexer is context-aware. Set the in-expression bit so that
1156 // they apply different tokenization rules.
Rui Ueyama3837f422019-07-10 05:00:371157 bool orig = inExpr;
1158 inExpr = true;
1159 Expr e = readExpr1(readPrimary(), 0);
1160 inExpr = orig;
1161 return e;
Rui Ueyama2ec34542017-04-05 05:07:391162}
1163
Rui Ueyama3837f422019-07-10 05:00:371164Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
1165 if (op == "+")
1166 return [=] { return add(l(), r()); };
1167 if (op == "-")
1168 return [=] { return sub(l(), r()); };
1169 if (op == "*")
1170 return [=] { return l().getValue() * r().getValue(); };
1171 if (op == "/") {
1172 std::string loc = getCurrentLocation();
George Rimar7b91e212018-03-05 10:02:441173 return [=]() -> uint64_t {
Rui Ueyama3837f422019-07-10 05:00:371174 if (uint64_t rv = r().getValue())
1175 return l().getValue() / rv;
1176 error(loc + ": division by zero");
Rui Ueyama067617f2018-03-05 23:50:451177 return 0;
George Rimar7b91e212018-03-05 10:02:441178 };
1179 }
Rui Ueyama3837f422019-07-10 05:00:371180 if (op == "%") {
1181 std::string loc = getCurrentLocation();
George Rimar7b91e212018-03-05 10:02:441182 return [=]() -> uint64_t {
Rui Ueyama3837f422019-07-10 05:00:371183 if (uint64_t rv = r().getValue())
1184 return l().getValue() % rv;
1185 error(loc + ": modulo by zero");
Rui Ueyama067617f2018-03-05 23:50:451186 return 0;
George Rimar7b91e212018-03-05 10:02:441187 };
1188 }
Rui Ueyama3837f422019-07-10 05:00:371189 if (op == "<<")
Fangrui Songdaba24e2023-06-15 17:34:331190 return [=] { return l().getValue() << r().getValue() % 64; };
Rui Ueyama3837f422019-07-10 05:00:371191 if (op == ">>")
Fangrui Songdaba24e2023-06-15 17:34:331192 return [=] { return l().getValue() >> r().getValue() % 64; };
Rui Ueyama3837f422019-07-10 05:00:371193 if (op == "<")
1194 return [=] { return l().getValue() < r().getValue(); };
1195 if (op == ">")
1196 return [=] { return l().getValue() > r().getValue(); };
1197 if (op == ">=")
1198 return [=] { return l().getValue() >= r().getValue(); };
1199 if (op == "<=")
1200 return [=] { return l().getValue() <= r().getValue(); };
1201 if (op == "==")
1202 return [=] { return l().getValue() == r().getValue(); };
1203 if (op == "!=")
1204 return [=] { return l().getValue() != r().getValue(); };
1205 if (op == "||")
1206 return [=] { return l().getValue() || r().getValue(); };
1207 if (op == "&&")
1208 return [=] { return l().getValue() && r().getValue(); };
1209 if (op == "&")
1210 return [=] { return bitAnd(l(), r()); };
Fangrui Songfae96102023-07-15 21:10:401211 if (op == "^")
1212 return [=] { return bitXor(l(), r()); };
Rui Ueyama3837f422019-07-10 05:00:371213 if (op == "|")
1214 return [=] { return bitOr(l(), r()); };
Rui Ueyama2ec34542017-04-05 05:07:391215 llvm_unreachable("invalid operator");
1216}
1217
1218// This is a part of the operator-precedence parser. This function
1219// assumes that the remaining token stream starts with an operator.
Rui Ueyama3837f422019-07-10 05:00:371220Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
Bob Haarmanb8a59c82017-10-25 22:28:381221 while (!atEOF() && !errorCount()) {
Rui Ueyama2ec34542017-04-05 05:07:391222 // Read an operator and an expression.
Rui Ueyama3837f422019-07-10 05:00:371223 StringRef op1 = peek();
1224 if (precedence(op1) < minPrec)
Rui Ueyama2ec34542017-04-05 05:07:391225 break;
Fangrui Songb0d6dd32022-06-25 20:48:521226 if (consume("?"))
1227 return readTernary(lhs);
Rui Ueyama2ec34542017-04-05 05:07:391228 skip();
Rui Ueyama3837f422019-07-10 05:00:371229 Expr rhs = readPrimary();
Rui Ueyama2ec34542017-04-05 05:07:391230
1231 // Evaluate the remaining part of the expression first if the
1232 // next operator has greater precedence than the previous one.
1233 // For example, if we have read "+" and "3", and if the next
1234 // operator is "*", then we'll evaluate 3 * ... part first.
1235 while (!atEOF()) {
Rui Ueyama3837f422019-07-10 05:00:371236 StringRef op2 = peek();
1237 if (precedence(op2) <= precedence(op1))
Rui Ueyama2ec34542017-04-05 05:07:391238 break;
Rui Ueyama3837f422019-07-10 05:00:371239 rhs = readExpr1(rhs, precedence(op2));
Rui Ueyama2ec34542017-04-05 05:07:391240 }
1241
Rui Ueyama3837f422019-07-10 05:00:371242 lhs = combine(op1, lhs, rhs);
Rui Ueyama2ec34542017-04-05 05:07:391243 }
Rui Ueyama3837f422019-07-10 05:00:371244 return lhs;
Rui Ueyama2ec34542017-04-05 05:07:391245}
1246
George Rimar5fb17122017-08-03 16:05:081247Expr ScriptParser::getPageSize() {
Rui Ueyama3837f422019-07-10 05:00:371248 std::string location = getCurrentLocation();
George Rimar5fb17122017-08-03 16:05:081249 return [=]() -> uint64_t {
Rui Ueyama3837f422019-07-10 05:00:371250 if (target)
1251 return config->commonPageSize;
1252 error(location + ": unable to calculate page size");
George Rimar5fb17122017-08-03 16:05:081253 return 4096; // Return a dummy value.
1254 };
1255}
1256
1257Expr ScriptParser::readConstant() {
Rui Ueyama3837f422019-07-10 05:00:371258 StringRef s = readParenLiteral();
1259 if (s == "COMMONPAGESIZE")
George Rimar5fb17122017-08-03 16:05:081260 return getPageSize();
Rui Ueyama3837f422019-07-10 05:00:371261 if (s == "MAXPAGESIZE")
1262 return [] { return config->maxPageSize; };
1263 setError("unknown constant: " + s);
George Rimarb068b032018-03-01 12:36:011264 return [] { return 0; };
Rui Ueyama2ec34542017-04-05 05:07:391265}
1266
Rui Ueyama5c650882017-04-05 23:22:111267// Parses Tok as an integer. It recognizes hexadecimal (prefixed with
1268// "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
1269// have "K" (Ki) or "M" (Mi) suffixes.
Fangrui Song4191fda2022-11-27 03:19:151270static std::optional<uint64_t> parseInt(StringRef tok) {
Rui Ueyama2ec34542017-04-05 05:07:391271 // Hexadecimal
Rui Ueyama3837f422019-07-10 05:00:371272 uint64_t val;
Kazu Hirataed1539c2023-05-16 17:12:421273 if (tok.starts_with_insensitive("0x")) {
Rui Ueyama3837f422019-07-10 05:00:371274 if (!to_integer(tok.substr(2), val, 16))
Fangrui Song4191fda2022-11-27 03:19:151275 return std::nullopt;
Rui Ueyama3837f422019-07-10 05:00:371276 return val;
Rui Ueyama40920162017-10-11 19:30:391277 }
Kazu Hirataed1539c2023-05-16 17:12:421278 if (tok.ends_with_insensitive("H")) {
Rui Ueyama3837f422019-07-10 05:00:371279 if (!to_integer(tok.drop_back(), val, 16))
Fangrui Song4191fda2022-11-27 03:19:151280 return std::nullopt;
Rui Ueyama3837f422019-07-10 05:00:371281 return val;
Rui Ueyama40920162017-10-11 19:30:391282 }
Rui Ueyama2ec34542017-04-05 05:07:391283
1284 // Decimal
Kazu Hirataed1539c2023-05-16 17:12:421285 if (tok.ends_with_insensitive("K")) {
Rui Ueyama3837f422019-07-10 05:00:371286 if (!to_integer(tok.drop_back(), val, 10))
Fangrui Song4191fda2022-11-27 03:19:151287 return std::nullopt;
Rui Ueyama3837f422019-07-10 05:00:371288 return val * 1024;
Rui Ueyama2ec34542017-04-05 05:07:391289 }
Kazu Hirataed1539c2023-05-16 17:12:421290 if (tok.ends_with_insensitive("M")) {
Rui Ueyama3837f422019-07-10 05:00:371291 if (!to_integer(tok.drop_back(), val, 10))
Fangrui Song4191fda2022-11-27 03:19:151292 return std::nullopt;
Rui Ueyama3837f422019-07-10 05:00:371293 return val * 1024 * 1024;
Rui Ueyama5c650882017-04-05 23:22:111294 }
Rui Ueyama3837f422019-07-10 05:00:371295 if (!to_integer(tok, val, 10))
Fangrui Song4191fda2022-11-27 03:19:151296 return std::nullopt;
Rui Ueyama3837f422019-07-10 05:00:371297 return val;
Rui Ueyama2ec34542017-04-05 05:07:391298}
1299
Rui Ueyama3837f422019-07-10 05:00:371300ByteCommand *ScriptParser::readByteCommand(StringRef tok) {
1301 int size = StringSwitch<int>(tok)
Rui Ueyama2ec34542017-04-05 05:07:391302 .Case("BYTE", 1)
1303 .Case("SHORT", 2)
1304 .Case("LONG", 4)
1305 .Case("QUAD", 8)
1306 .Default(-1);
Rui Ueyama3837f422019-07-10 05:00:371307 if (size == -1)
Rui Ueyama2ec34542017-04-05 05:07:391308 return nullptr;
George Rimar84bcabc2018-03-15 09:16:401309
Rui Ueyama3837f422019-07-10 05:00:371310 size_t oldPos = pos;
1311 Expr e = readParenExpr();
1312 std::string commandString =
1313 tok.str() + " " +
1314 llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
1315 return make<ByteCommand>(e, size, commandString);
Rui Ueyama2ec34542017-04-05 05:07:391316}
1317
Fangrui Song4191fda2022-11-27 03:19:151318static std::optional<uint64_t> parseFlag(StringRef tok) {
1319 if (std::optional<uint64_t> asInt = parseInt(tok))
Peter Smithdbd0ad32020-01-15 09:38:001320 return asInt;
1321#define CASE_ENT(enum) #enum, ELF::enum
Fangrui Song4191fda2022-11-27 03:19:151322 return StringSwitch<std::optional<uint64_t>>(tok)
Peter Smithdbd0ad32020-01-15 09:38:001323 .Case(CASE_ENT(SHF_WRITE))
1324 .Case(CASE_ENT(SHF_ALLOC))
1325 .Case(CASE_ENT(SHF_EXECINSTR))
1326 .Case(CASE_ENT(SHF_MERGE))
1327 .Case(CASE_ENT(SHF_STRINGS))
1328 .Case(CASE_ENT(SHF_INFO_LINK))
1329 .Case(CASE_ENT(SHF_LINK_ORDER))
1330 .Case(CASE_ENT(SHF_OS_NONCONFORMING))
1331 .Case(CASE_ENT(SHF_GROUP))
1332 .Case(CASE_ENT(SHF_TLS))
1333 .Case(CASE_ENT(SHF_COMPRESSED))
1334 .Case(CASE_ENT(SHF_EXCLUDE))
1335 .Case(CASE_ENT(SHF_ARM_PURECODE))
Kazu Hiratac68af422022-12-03 07:12:361336 .Default(std::nullopt);
Peter Smithdbd0ad32020-01-15 09:38:001337#undef CASE_ENT
1338}
1339
1340// Reads the '(' <flags> ')' list of section flags in
1341// INPUT_SECTION_FLAGS '(' <flags> ')' in the
1342// following form:
1343// <flags> ::= <flag>
1344// | <flags> & flag
1345// <flag> ::= Recognized Flag Name, or Integer value of flag.
1346// If the first character of <flag> is a ! then this means without flag,
1347// otherwise with flag.
1348// Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and
1349// without flag SHF_WRITE.
1350std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
1351 uint64_t withFlags = 0;
1352 uint64_t withoutFlags = 0;
1353 expect("(");
1354 while (!errorCount()) {
1355 StringRef tok = unquote(next());
1356 bool without = tok.consume_front("!");
Fangrui Song4191fda2022-11-27 03:19:151357 if (std::optional<uint64_t> flag = parseFlag(tok)) {
Peter Smithdbd0ad32020-01-15 09:38:001358 if (without)
1359 withoutFlags |= *flag;
1360 else
1361 withFlags |= *flag;
1362 } else {
1363 setError("unrecognised flag: " + tok);
1364 }
1365 if (consume(")"))
1366 break;
1367 if (!consume("&")) {
1368 next();
1369 setError("expected & or )");
1370 }
1371 }
1372 return std::make_pair(withFlags, withoutFlags);
1373}
1374
Rui Ueyama2ec34542017-04-05 05:07:391375StringRef ScriptParser::readParenLiteral() {
1376 expect("(");
Rui Ueyama3837f422019-07-10 05:00:371377 bool orig = inExpr;
1378 inExpr = false;
1379 StringRef tok = next();
1380 inExpr = orig;
Rui Ueyama2ec34542017-04-05 05:07:391381 expect(")");
Rui Ueyama3837f422019-07-10 05:00:371382 return tok;
Rui Ueyama2ec34542017-04-05 05:07:391383}
1384
Fangrui Song9e9c86f2022-02-28 19:19:001385static void checkIfExists(const OutputSection &osec, StringRef location) {
1386 if (osec.location.empty() && script->errorOnMissingSection)
1387 error(location + ": undefined section " + osec.name);
Rafael Espindola05c4f672017-06-01 01:16:501388}
1389
Fangrui Songe4f385d2021-03-11 17:34:361390static bool isValidSymbolName(StringRef s) {
1391 auto valid = [](char c) {
1392 return isAlnum(c) || c == '$' || c == '.' || c == '_';
1393 };
1394 return !s.empty() && !isDigit(s[0]) && llvm::all_of(s, valid);
1395}
1396
Rui Ueyama2ec34542017-04-05 05:07:391397Expr ScriptParser::readPrimary() {
1398 if (peek() == "(")
1399 return readParenExpr();
1400
Rui Ueyama5c650882017-04-05 23:22:111401 if (consume("~")) {
Rui Ueyama3837f422019-07-10 05:00:371402 Expr e = readPrimary();
1403 return [=] { return ~e().getValue(); };
Rui Ueyama2ec34542017-04-05 05:07:391404 }
Hafiz Abid Qadeer6f1d9542017-08-10 15:25:471405 if (consume("!")) {
Rui Ueyama3837f422019-07-10 05:00:371406 Expr e = readPrimary();
1407 return [=] { return !e().getValue(); };
Hafiz Abid Qadeer6f1d9542017-08-10 15:25:471408 }
Rui Ueyama5c650882017-04-05 23:22:111409 if (consume("-")) {
Rui Ueyama3837f422019-07-10 05:00:371410 Expr e = readPrimary();
1411 return [=] { return -e().getValue(); };
Rui Ueyama2ec34542017-04-05 05:07:391412 }
1413
Rui Ueyama3837f422019-07-10 05:00:371414 StringRef tok = next();
1415 std::string location = getCurrentLocation();
Rui Ueyama5c650882017-04-05 23:22:111416
Rui Ueyama2ec34542017-04-05 05:07:391417 // Built-in functions are parsed here.
1418 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
Rui Ueyama3837f422019-07-10 05:00:371419 if (tok == "ABSOLUTE") {
1420 Expr inner = readParenExpr();
Rui Ueyama2ec34542017-04-05 05:07:391421 return [=] {
Rui Ueyama3837f422019-07-10 05:00:371422 ExprValue i = inner();
1423 i.forceAbsolute = true;
1424 return i;
Rui Ueyama2ec34542017-04-05 05:07:391425 };
1426 }
Rui Ueyama3837f422019-07-10 05:00:371427 if (tok == "ADDR") {
Roger Pau Monne7cab3852023-07-05 21:56:151428 StringRef name = unquote(readParenLiteral());
Fangrui Song6c814932022-03-08 19:23:411429 OutputSection *osec = &script->getOrCreateOutputSection(name)->osec;
Fangrui Song9e9c86f2022-02-28 19:19:001430 osec->usedInExpression = true;
George Rimar41c7ab42017-06-07 08:54:431431 return [=]() -> ExprValue {
Fangrui Song9e9c86f2022-02-28 19:19:001432 checkIfExists(*osec, location);
1433 return {osec, false, 0, location};
George Rimar41c7ab42017-06-07 08:54:431434 };
Rui Ueyama2ec34542017-04-05 05:07:391435 }
Rui Ueyama3837f422019-07-10 05:00:371436 if (tok == "ALIGN") {
Rui Ueyama2ec34542017-04-05 05:07:391437 expect("(");
Rui Ueyama3837f422019-07-10 05:00:371438 Expr e = readExpr();
George Rimarf22ec9d2017-10-25 14:50:511439 if (consume(")")) {
Rui Ueyama3837f422019-07-10 05:00:371440 e = checkAlignment(e, location);
Fangrui Song85cfd912022-07-24 18:20:491441 return [=] { return alignToPowerOf2(script->getDot(), e().getValue()); };
George Rimarf22ec9d2017-10-25 14:50:511442 }
Rui Ueyamab579c432017-04-05 05:40:211443 expect(",");
Rui Ueyama3837f422019-07-10 05:00:371444 Expr e2 = checkAlignment(readExpr(), location);
Rui Ueyama2ec34542017-04-05 05:07:391445 expect(")");
Petr Hosek3c6de1a2017-05-30 03:18:281446 return [=] {
Rui Ueyama3837f422019-07-10 05:00:371447 ExprValue v = e();
1448 v.alignment = e2().getValue();
1449 return v;
Petr Hosek3c6de1a2017-05-30 03:18:281450 };
Rui Ueyama2ec34542017-04-05 05:07:391451 }
Rui Ueyama3837f422019-07-10 05:00:371452 if (tok == "ALIGNOF") {
Roger Pau Monne7cab3852023-07-05 21:56:151453 StringRef name = unquote(readParenLiteral());
Fangrui Song6c814932022-03-08 19:23:411454 OutputSection *osec = &script->getOrCreateOutputSection(name)->osec;
Rui Ueyama617e2f92017-10-08 02:27:021455 return [=] {
Fangrui Song9e9c86f2022-02-28 19:19:001456 checkIfExists(*osec, location);
Guillaume Chatelet08e2a762022-12-01 16:19:561457 return osec->addralign;
Rui Ueyama617e2f92017-10-08 02:27:021458 };
Rui Ueyama2ec34542017-04-05 05:07:391459 }
Rui Ueyama3837f422019-07-10 05:00:371460 if (tok == "ASSERT")
George Rimard30a78b2018-04-25 11:16:311461 return readAssert();
Rui Ueyama3837f422019-07-10 05:00:371462 if (tok == "CONSTANT")
George Rimar5fb17122017-08-03 16:05:081463 return readConstant();
Rui Ueyama3837f422019-07-10 05:00:371464 if (tok == "DATA_SEGMENT_ALIGN") {
Rui Ueyama2ec34542017-04-05 05:07:391465 expect("(");
Rui Ueyama3837f422019-07-10 05:00:371466 Expr e = readExpr();
Rui Ueyama2ec34542017-04-05 05:07:391467 expect(",");
1468 readExpr();
1469 expect(")");
Fangrui Song5a58e982023-09-14 17:33:111470 script->seenDataAlign = true;
George Rimar60833f62017-07-28 09:27:491471 return [=] {
Fangrui Song85cfd912022-07-24 18:20:491472 uint64_t align = std::max(uint64_t(1), e().getValue());
1473 return (script->getDot() + align - 1) & -align;
George Rimar60833f62017-07-28 09:27:491474 };
Rui Ueyama2ec34542017-04-05 05:07:391475 }
Rui Ueyama3837f422019-07-10 05:00:371476 if (tok == "DATA_SEGMENT_END") {
Rui Ueyama2ec34542017-04-05 05:07:391477 expect("(");
1478 expect(".");
1479 expect(")");
Rui Ueyama3837f422019-07-10 05:00:371480 return [] { return script->getDot(); };
Rui Ueyama2ec34542017-04-05 05:07:391481 }
Rui Ueyama3837f422019-07-10 05:00:371482 if (tok == "DATA_SEGMENT_RELRO_END") {
Rui Ueyama2ec34542017-04-05 05:07:391483 // GNU linkers implements more complicated logic to handle
1484 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1485 // just align to the next page boundary for simplicity.
1486 expect("(");
1487 readExpr();
1488 expect(",");
1489 readExpr();
1490 expect(")");
Fangrui Song5a58e982023-09-14 17:33:111491 script->seenRelroEnd = true;
1492 return [=] { return alignToPowerOf2(script->getDot(), config->maxPageSize); };
Rui Ueyama2ec34542017-04-05 05:07:391493 }
Rui Ueyama3837f422019-07-10 05:00:371494 if (tok == "DEFINED") {
Fangrui Songe7a7ad12021-07-25 23:26:371495 StringRef name = unquote(readParenLiteral());
Fangrui Song65a15a52023-09-09 21:46:511496 // Return 1 if s is defined. If the definition is only found in a linker
1497 // script, it must happen before this DEFINED.
1498 auto order = ctx.scriptSymOrderCounter++;
Hafiz Abid Qadeer1f166ed2020-07-16 20:40:311499 return [=] {
Fangrui Song65a15a52023-09-09 21:46:511500 Symbol *s = symtab.find(name);
1501 return s && s->isDefined() && ctx.scriptSymOrder.lookup(s) < order ? 1
1502 : 0;
Hafiz Abid Qadeer1f166ed2020-07-16 20:40:311503 };
Rui Ueyama2ec34542017-04-05 05:07:391504 }
Rui Ueyama3837f422019-07-10 05:00:371505 if (tok == "LENGTH") {
1506 StringRef name = readParenLiteral();
1507 if (script->memoryRegions.count(name) == 0) {
1508 setError("memory region not defined: " + name);
George Rimarb068b032018-03-01 12:36:011509 return [] { return 0; };
1510 }
Fangrui Song92b5b982020-03-06 19:49:581511 return script->memoryRegions[name]->length;
Rui Ueyama91b95b62017-05-09 18:24:381512 }
Rui Ueyama3837f422019-07-10 05:00:371513 if (tok == "LOADADDR") {
Roger Pau Monne7cab3852023-07-05 21:56:151514 StringRef name = unquote(readParenLiteral());
Fangrui Song6c814932022-03-08 19:23:411515 OutputSection *osec = &script->getOrCreateOutputSection(name)->osec;
Fangrui Song9e9c86f2022-02-28 19:19:001516 osec->usedInExpression = true;
Rui Ueyama617e2f92017-10-08 02:27:021517 return [=] {
Fangrui Song9e9c86f2022-02-28 19:19:001518 checkIfExists(*osec, location);
1519 return osec->getLMA();
Rui Ueyama617e2f92017-10-08 02:27:021520 };
Rui Ueyama2ec34542017-04-05 05:07:391521 }
Isaac Richterfa1145a2020-07-27 08:49:241522 if (tok == "LOG2CEIL") {
1523 expect("(");
1524 Expr a = readExpr();
1525 expect(")");
1526 return [=] {
1527 // LOG2CEIL(0) is defined to be 0.
1528 return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1)));
1529 };
1530 }
Rui Ueyama3837f422019-07-10 05:00:371531 if (tok == "MAX" || tok == "MIN") {
George Rimarfd115602018-03-28 11:33:001532 expect("(");
Rui Ueyama3837f422019-07-10 05:00:371533 Expr a = readExpr();
George Rimarfd115602018-03-28 11:33:001534 expect(",");
Rui Ueyama3837f422019-07-10 05:00:371535 Expr b = readExpr();
George Rimarfd115602018-03-28 11:33:001536 expect(")");
Rui Ueyama3837f422019-07-10 05:00:371537 if (tok == "MIN")
1538 return [=] { return std::min(a().getValue(), b().getValue()); };
1539 return [=] { return std::max(a().getValue(), b().getValue()); };
George Rimarfd115602018-03-28 11:33:001540 }
Rui Ueyama3837f422019-07-10 05:00:371541 if (tok == "ORIGIN") {
1542 StringRef name = readParenLiteral();
1543 if (script->memoryRegions.count(name) == 0) {
1544 setError("memory region not defined: " + name);
George Rimarb068b032018-03-01 12:36:011545 return [] { return 0; };
1546 }
Fangrui Song92b5b982020-03-06 19:49:581547 return script->memoryRegions[name]->origin;
Rui Ueyama91b95b62017-05-09 18:24:381548 }
Rui Ueyama3837f422019-07-10 05:00:371549 if (tok == "SEGMENT_START") {
Rui Ueyama2ec34542017-04-05 05:07:391550 expect("(");
1551 skip();
1552 expect(",");
Rui Ueyama3837f422019-07-10 05:00:371553 Expr e = readExpr();
Rui Ueyama2ec34542017-04-05 05:07:391554 expect(")");
Rui Ueyama3837f422019-07-10 05:00:371555 return [=] { return e(); };
Rui Ueyama2ec34542017-04-05 05:07:391556 }
Rui Ueyama3837f422019-07-10 05:00:371557 if (tok == "SIZEOF") {
Roger Pau Monne7cab3852023-07-05 21:56:151558 StringRef name = unquote(readParenLiteral());
Fangrui Song6c814932022-03-08 19:23:411559 OutputSection *cmd = &script->getOrCreateOutputSection(name)->osec;
Rafael Espindola05c4f672017-06-01 01:16:501560 // Linker script does not create an output section if its content is empty.
1561 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1562 // be empty.
Rui Ueyama3837f422019-07-10 05:00:371563 return [=] { return cmd->size; };
Rui Ueyama2ec34542017-04-05 05:07:391564 }
Rui Ueyama3837f422019-07-10 05:00:371565 if (tok == "SIZEOF_HEADERS")
Fangrui Song07837b82020-05-15 05:18:581566 return [=] { return elf::getHeaderSize(); };
Rui Ueyama2ec34542017-04-05 05:07:391567
Rui Ueyama4eb2ecc2017-04-05 18:02:301568 // Tok is the dot.
Rui Ueyama3837f422019-07-10 05:00:371569 if (tok == ".")
1570 return [=] { return script->getSymbolValue(tok, location); };
Rui Ueyama4eb2ecc2017-04-05 18:02:301571
Rui Ueyama2ec34542017-04-05 05:07:391572 // Tok is a literal number.
Fangrui Song4191fda2022-11-27 03:19:151573 if (std::optional<uint64_t> val = parseInt(tok))
Rui Ueyama3837f422019-07-10 05:00:371574 return [=] { return *val; };
Rui Ueyama2ec34542017-04-05 05:07:391575
1576 // Tok is a symbol name.
Fangrui Song8d85c962023-06-05 21:36:191577 if (tok.starts_with("\""))
Fangrui Song2bf06d92021-09-27 16:50:411578 tok = unquote(tok);
1579 else if (!isValidSymbolName(tok))
Rui Ueyama3837f422019-07-10 05:00:371580 setError("malformed number: " + tok);
Parth Aroraebb326a2024-03-25 23:11:211581 if (activeProvideSym)
1582 script->provideMap[*activeProvideSym].push_back(tok);
1583 else
1584 script->referencedSymbols.push_back(tok);
Rui Ueyama3837f422019-07-10 05:00:371585 return [=] { return script->getSymbolValue(tok, location); };
Rui Ueyama2ec34542017-04-05 05:07:391586}
1587
Rui Ueyama3837f422019-07-10 05:00:371588Expr ScriptParser::readTernary(Expr cond) {
1589 Expr l = readExpr();
Rui Ueyama2ec34542017-04-05 05:07:391590 expect(":");
Rui Ueyama3837f422019-07-10 05:00:371591 Expr r = readExpr();
1592 return [=] { return cond().getValue() ? l() : r(); };
Rui Ueyama2ec34542017-04-05 05:07:391593}
1594
1595Expr ScriptParser::readParenExpr() {
1596 expect("(");
Rui Ueyama3837f422019-07-10 05:00:371597 Expr e = readExpr();
Rui Ueyama2ec34542017-04-05 05:07:391598 expect(")");
Rui Ueyama3837f422019-07-10 05:00:371599 return e;
Rui Ueyama2ec34542017-04-05 05:07:391600}
1601
Fangrui Songa1c2ee02021-12-26 21:53:471602SmallVector<StringRef, 0> ScriptParser::readOutputSectionPhdrs() {
1603 SmallVector<StringRef, 0> phdrs;
Fangrui Song8d85c962023-06-05 21:36:191604 while (!errorCount() && peek().starts_with(":")) {
Rui Ueyama3837f422019-07-10 05:00:371605 StringRef tok = next();
1606 phdrs.push_back((tok.size() == 1) ? next() : tok.substr(1));
Rui Ueyama2ec34542017-04-05 05:07:391607 }
Rui Ueyama3837f422019-07-10 05:00:371608 return phdrs;
Rui Ueyama2ec34542017-04-05 05:07:391609}
1610
1611// Read a program header type name. The next token must be a
1612// name of a program header type or a constant (e.g. "0x3").
1613unsigned ScriptParser::readPhdrType() {
Rui Ueyama3837f422019-07-10 05:00:371614 StringRef tok = next();
Fangrui Song4191fda2022-11-27 03:19:151615 if (std::optional<uint64_t> val = parseInt(tok))
Rui Ueyama3837f422019-07-10 05:00:371616 return *val;
Rui Ueyama2ec34542017-04-05 05:07:391617
Rui Ueyama3837f422019-07-10 05:00:371618 unsigned ret = StringSwitch<unsigned>(tok)
Rui Ueyama2ec34542017-04-05 05:07:391619 .Case("PT_NULL", PT_NULL)
1620 .Case("PT_LOAD", PT_LOAD)
1621 .Case("PT_DYNAMIC", PT_DYNAMIC)
1622 .Case("PT_INTERP", PT_INTERP)
1623 .Case("PT_NOTE", PT_NOTE)
1624 .Case("PT_SHLIB", PT_SHLIB)
1625 .Case("PT_PHDR", PT_PHDR)
1626 .Case("PT_TLS", PT_TLS)
1627 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1628 .Case("PT_GNU_STACK", PT_GNU_STACK)
1629 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1630 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1631 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1632 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1633 .Default(-1);
1634
Rui Ueyama3837f422019-07-10 05:00:371635 if (ret == (unsigned)-1) {
1636 setError("invalid program header type: " + tok);
Rui Ueyama2ec34542017-04-05 05:07:391637 return PT_NULL;
1638 }
Rui Ueyama3837f422019-07-10 05:00:371639 return ret;
Rui Ueyama2ec34542017-04-05 05:07:391640}
1641
1642// Reads an anonymous version declaration.
1643void ScriptParser::readAnonymousDeclaration() {
Fangrui Song64038ef2021-12-27 04:12:551644 SmallVector<SymbolVersion, 0> locals;
1645 SmallVector<SymbolVersion, 0> globals;
Rui Ueyama3837f422019-07-10 05:00:371646 std::tie(locals, globals) = readSymbols();
Fangrui Songe28a70d2019-08-05 14:31:391647 for (const SymbolVersion &pat : locals)
Fangrui Song00809c82021-08-05 06:52:551648 config->versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat);
Fangrui Songe28a70d2019-08-05 14:31:391649 for (const SymbolVersion &pat : globals)
Fangrui Song00809c82021-08-05 06:52:551650 config->versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat);
Rui Ueyama2ec34542017-04-05 05:07:391651
1652 expect(";");
1653}
1654
1655// Reads a non-anonymous version definition,
1656// e.g. "VerStr { global: foo; bar; local: *; };".
Rui Ueyama3837f422019-07-10 05:00:371657void ScriptParser::readVersionDeclaration(StringRef verStr) {
Rui Ueyama2ec34542017-04-05 05:07:391658 // Read a symbol list.
Fangrui Song64038ef2021-12-27 04:12:551659 SmallVector<SymbolVersion, 0> locals;
1660 SmallVector<SymbolVersion, 0> globals;
Rui Ueyama3837f422019-07-10 05:00:371661 std::tie(locals, globals) = readSymbols();
Rui Ueyama2ec34542017-04-05 05:07:391662
1663 // Create a new version definition and add that to the global symbols.
Rui Ueyama3837f422019-07-10 05:00:371664 VersionDefinition ver;
1665 ver.name = verStr;
Fangrui Song00809c82021-08-05 06:52:551666 ver.nonLocalPatterns = std::move(globals);
1667 ver.localPatterns = std::move(locals);
Fangrui Songe28a70d2019-08-05 14:31:391668 ver.id = config->versionDefinitions.size();
Rui Ueyama3837f422019-07-10 05:00:371669 config->versionDefinitions.push_back(ver);
Rui Ueyama2ec34542017-04-05 05:07:391670
1671 // Each version may have a parent version. For example, "Ver2"
1672 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1673 // as a parent. This version hierarchy is, probably against your
1674 // instinct, purely for hint; the runtime doesn't care about it
1675 // at all. In LLD, we simply ignore it.
Fangrui Song5f380402020-02-08 22:10:291676 if (next() != ";")
1677 expect(";");
Rui Ueyama2ec34542017-04-05 05:07:391678}
1679
Fangrui Song49279ca12020-06-18 00:11:381680bool elf::hasWildcard(StringRef s) {
Rui Ueyama3837f422019-07-10 05:00:371681 return s.find_first_of("?*[") != StringRef::npos;
Rui Ueyama1e77ad12017-07-13 20:30:351682}
1683
Rui Ueyama2ec34542017-04-05 05:07:391684// Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
Fangrui Song64038ef2021-12-27 04:12:551685std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
Rui Ueyama2ec34542017-04-05 05:07:391686ScriptParser::readSymbols() {
Fangrui Song64038ef2021-12-27 04:12:551687 SmallVector<SymbolVersion, 0> locals;
1688 SmallVector<SymbolVersion, 0> globals;
1689 SmallVector<SymbolVersion, 0> *v = &globals;
Rui Ueyama2ec34542017-04-05 05:07:391690
Bob Haarmanb8a59c82017-10-25 22:28:381691 while (!errorCount()) {
Rui Ueyama2ec34542017-04-05 05:07:391692 if (consume("}"))
1693 break;
1694 if (consumeLabel("local")) {
Rui Ueyama3837f422019-07-10 05:00:371695 v = &locals;
Rui Ueyama2ec34542017-04-05 05:07:391696 continue;
1697 }
1698 if (consumeLabel("global")) {
Rui Ueyama3837f422019-07-10 05:00:371699 v = &globals;
Rui Ueyama2ec34542017-04-05 05:07:391700 continue;
1701 }
1702
1703 if (consume("extern")) {
Fangrui Song64038ef2021-12-27 04:12:551704 SmallVector<SymbolVersion, 0> ext = readVersionExtern();
Rui Ueyama3837f422019-07-10 05:00:371705 v->insert(v->end(), ext.begin(), ext.end());
Rui Ueyama2ec34542017-04-05 05:07:391706 } else {
Rui Ueyama3837f422019-07-10 05:00:371707 StringRef tok = next();
1708 v->push_back({unquote(tok), false, hasWildcard(tok)});
Rui Ueyama2ec34542017-04-05 05:07:391709 }
1710 expect(";");
1711 }
Rui Ueyama3837f422019-07-10 05:00:371712 return {locals, globals};
Rui Ueyama2ec34542017-04-05 05:07:391713}
1714
1715// Reads an "extern C++" directive, e.g.,
1716// "extern "C++" { ns::*; "f(int, double)"; };"
Rui Ueyama17324d82018-02-01 23:46:171717//
1718// The last semicolon is optional. E.g. this is OK:
1719// "extern "C++" { ns::*; "f(int, double)" };"
Fangrui Song64038ef2021-12-27 04:12:551720SmallVector<SymbolVersion, 0> ScriptParser::readVersionExtern() {
Rui Ueyama3837f422019-07-10 05:00:371721 StringRef tok = next();
1722 bool isCXX = tok == "\"C++\"";
1723 if (!isCXX && tok != "\"C\"")
Rui Ueyama2ec34542017-04-05 05:07:391724 setError("Unknown language");
1725 expect("{");
1726
Fangrui Song64038ef2021-12-27 04:12:551727 SmallVector<SymbolVersion, 0> ret;
Bob Haarmanb8a59c82017-10-25 22:28:381728 while (!errorCount() && peek() != "}") {
Rui Ueyama3837f422019-07-10 05:00:371729 StringRef tok = next();
1730 ret.push_back(
Fangrui Song8d85c962023-06-05 21:36:191731 {unquote(tok), isCXX, !tok.starts_with("\"") && hasWildcard(tok)});
Rui Ueyama17324d82018-02-01 23:46:171732 if (consume("}"))
Rui Ueyama3837f422019-07-10 05:00:371733 return ret;
Rui Ueyama2ec34542017-04-05 05:07:391734 expect(";");
1735 }
1736
1737 expect("}");
Rui Ueyama3837f422019-07-10 05:00:371738 return ret;
Rui Ueyama2ec34542017-04-05 05:07:391739}
1740
Fangrui Song92b5b982020-03-06 19:49:581741Expr ScriptParser::readMemoryAssignment(StringRef s1, StringRef s2,
1742 StringRef s3) {
Rui Ueyama3837f422019-07-10 05:00:371743 if (!consume(s1) && !consume(s2) && !consume(s3)) {
1744 setError("expected one of: " + s1 + ", " + s2 + ", or " + s3);
Fangrui Song92b5b982020-03-06 19:49:581745 return [] { return 0; };
Rui Ueyama2ec34542017-04-05 05:07:391746 }
1747 expect("=");
Fangrui Song92b5b982020-03-06 19:49:581748 return readExpr();
Rui Ueyama2ec34542017-04-05 05:07:391749}
1750
1751// Parse the MEMORY command as specified in:
1752// https://sourceware.org/binutils/docs/ld/MEMORY.html
1753//
1754// MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1755void ScriptParser::readMemory() {
1756 expect("{");
Bob Haarmanb8a59c82017-10-25 22:28:381757 while (!errorCount() && !consume("}")) {
Rui Ueyama3837f422019-07-10 05:00:371758 StringRef tok = next();
1759 if (tok == "INCLUDE") {
Rui Ueyama2e9d40d2018-10-12 17:07:321760 readInclude();
1761 continue;
1762 }
Rui Ueyama2ec34542017-04-05 05:07:391763
Rui Ueyama3837f422019-07-10 05:00:371764 uint32_t flags = 0;
Igor Kudrin8cdf1c12021-11-24 05:17:031765 uint32_t invFlags = 0;
Rui Ueyama3837f422019-07-10 05:00:371766 uint32_t negFlags = 0;
Igor Kudrin8cdf1c12021-11-24 05:17:031767 uint32_t negInvFlags = 0;
Rui Ueyama2ec34542017-04-05 05:07:391768 if (consume("(")) {
Igor Kudrin8cdf1c12021-11-24 05:17:031769 readMemoryAttributes(flags, invFlags, negFlags, negInvFlags);
Rui Ueyama2ec34542017-04-05 05:07:391770 expect(")");
1771 }
1772 expect(":");
1773
Fangrui Song92b5b982020-03-06 19:49:581774 Expr origin = readMemoryAssignment("ORIGIN", "org", "o");
Rui Ueyama2ec34542017-04-05 05:07:391775 expect(",");
Fangrui Song92b5b982020-03-06 19:49:581776 Expr length = readMemoryAssignment("LENGTH", "len", "l");
Rui Ueyama2ec34542017-04-05 05:07:391777
George Rimar5f375412017-09-08 08:23:151778 // Add the memory region to the region map.
Igor Kudrin8cdf1c12021-11-24 05:17:031779 MemoryRegion *mr = make<MemoryRegion>(tok, origin, length, flags, invFlags,
1780 negFlags, negInvFlags);
Rui Ueyama3837f422019-07-10 05:00:371781 if (!script->memoryRegions.insert({tok, mr}).second)
1782 setError("region '" + tok + "' already defined");
Rui Ueyama2ec34542017-04-05 05:07:391783 }
1784}
1785
1786// This function parses the attributes used to match against section
1787// flags when placing output sections in a memory region. These flags
1788// are only used when an explicit memory region name is not used.
Igor Kudrin8cdf1c12021-11-24 05:17:031789void ScriptParser::readMemoryAttributes(uint32_t &flags, uint32_t &invFlags,
1790 uint32_t &negFlags,
1791 uint32_t &negInvFlags) {
Rui Ueyama3837f422019-07-10 05:00:371792 bool invert = false;
Rui Ueyama2ec34542017-04-05 05:07:391793
Rui Ueyama3837f422019-07-10 05:00:371794 for (char c : next().lower()) {
Igor Kudrin8cdf1c12021-11-24 05:17:031795 if (c == '!') {
Rui Ueyama3837f422019-07-10 05:00:371796 invert = !invert;
Igor Kudrin8cdf1c12021-11-24 05:17:031797 std::swap(flags, negFlags);
1798 std::swap(invFlags, negInvFlags);
1799 continue;
1800 }
1801 if (c == 'w')
1802 flags |= SHF_WRITE;
Rui Ueyama3837f422019-07-10 05:00:371803 else if (c == 'x')
Igor Kudrin8cdf1c12021-11-24 05:17:031804 flags |= SHF_EXECINSTR;
Rui Ueyama3837f422019-07-10 05:00:371805 else if (c == 'a')
Igor Kudrin8cdf1c12021-11-24 05:17:031806 flags |= SHF_ALLOC;
1807 else if (c == 'r')
1808 invFlags |= SHF_WRITE;
Rui Ueyama2ec34542017-04-05 05:07:391809 else
Igor Kudrin8cdf1c12021-11-24 05:17:031810 setError("invalid memory region attribute");
Rui Ueyama2ec34542017-04-05 05:07:391811 }
Igor Kudrin8cdf1c12021-11-24 05:17:031812
1813 if (invert) {
1814 std::swap(flags, negFlags);
1815 std::swap(invFlags, negInvFlags);
1816 }
Rui Ueyama2ec34542017-04-05 05:07:391817}
1818
Fangrui Song07837b82020-05-15 05:18:581819void elf::readLinkerScript(MemoryBufferRef mb) {
James Henderson439341b2020-11-03 14:41:091820 llvm::TimeTraceScope timeScope("Read linker script",
1821 mb.getBufferIdentifier());
Rui Ueyama3837f422019-07-10 05:00:371822 ScriptParser(mb).readLinkerScript();
Rui Ueyama2ec34542017-04-05 05:07:391823}
1824
Fangrui Song07837b82020-05-15 05:18:581825void elf::readVersionScript(MemoryBufferRef mb) {
James Henderson439341b2020-11-03 14:41:091826 llvm::TimeTraceScope timeScope("Read version script",
1827 mb.getBufferIdentifier());
Rui Ueyama3837f422019-07-10 05:00:371828 ScriptParser(mb).readVersionScript();
Rui Ueyama2ec34542017-04-05 05:07:391829}
1830
Fangrui Song07837b82020-05-15 05:18:581831void elf::readDynamicList(MemoryBufferRef mb) {
James Henderson439341b2020-11-03 14:41:091832 llvm::TimeTraceScope timeScope("Read dynamic list", mb.getBufferIdentifier());
Fangrui Song07837b82020-05-15 05:18:581833 ScriptParser(mb).readDynamicList();
Petr Hosek8c7e8cc2017-11-04 02:03:581834}
Fangrui Songbd8cfe62019-10-07 08:31:181835
Fangrui Song07837b82020-05-15 05:18:581836void elf::readDefsym(StringRef name, MemoryBufferRef mb) {
James Henderson439341b2020-11-03 14:41:091837 llvm::TimeTraceScope timeScope("Read defsym input", name);
Fangrui Song07837b82020-05-15 05:18:581838 ScriptParser(mb).readDefsym(name);
1839}