Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1 | //===- ScriptParser.cpp ---------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // 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 Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Rui Ueyama | 05f6b85 | 2017-04-05 05:50:08 | [diff] [blame] | 8 | // |
| 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 Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 13 | |
| 14 | #include "ScriptParser.h" |
| 15 | #include "Config.h" |
| 16 | #include "Driver.h" |
Fangrui Song | b01430a | 2022-02-24 03:18:24 | [diff] [blame] | 17 | #include "InputFiles.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 18 | #include "LinkerScript.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 19 | #include "OutputSections.h" |
| 20 | #include "ScriptLexer.h" |
Fangrui Song | 27bb799 | 2022-02-08 05:53:34 | [diff] [blame] | 21 | #include "SymbolTable.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 22 | #include "Symbols.h" |
| 23 | #include "Target.h" |
Alexandre Ganea | 83d59e0 | 2022-01-20 19:53:18 | [diff] [blame] | 24 | #include "lld/Common/CommonLinkerContext.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
| 26 | #include "llvm/ADT/StringRef.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 27 | #include "llvm/ADT/StringSwitch.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 | [diff] [blame] | 28 | #include "llvm/BinaryFormat/ELF.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 29 | #include "llvm/Support/Casting.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/FileSystem.h" |
Isaac Richter | fa1145a | 2020-07-27 08:49:24 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 33 | #include "llvm/Support/Path.h" |
Fangrui Song | fe0de25 | 2022-06-26 03:25:34 | [diff] [blame] | 34 | #include "llvm/Support/SaveAndRestore.h" |
James Henderson | 439341b | 2020-11-03 14:41:09 | [diff] [blame] | 35 | #include "llvm/Support/TimeProfiler.h" |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 36 | #include <cassert> |
| 37 | #include <limits> |
Parth Arora | ebb326a | 2024-03-25 23:11:21 | [diff] [blame] | 38 | #include <optional> |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 39 | #include <vector> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | using namespace llvm::ELF; |
Rui Ueyama | b58079d4 | 2017-04-11 22:45:57 | [diff] [blame] | 43 | using namespace llvm::support::endian; |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 44 | using namespace lld; |
| 45 | using namespace lld::elf; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 46 | |
Rui Ueyama | 96b3fe0 | 2017-04-05 05:08:01 | [diff] [blame] | 47 | namespace { |
| 48 | class ScriptParser final : ScriptLexer { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 49 | public: |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 50 | ScriptParser(Ctx &ctx, MemoryBufferRef mb) : ScriptLexer(ctx, mb), ctx(ctx) {} |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 51 | |
| 52 | void readLinkerScript(); |
| 53 | void readVersionScript(); |
| 54 | void readDynamicList(); |
Fangrui Song | ff7f97a | 2024-07-28 19:38:10 | [diff] [blame] | 55 | void readDefsym(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 56 | |
| 57 | private: |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 58 | void addFile(StringRef path); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 59 | |
| 60 | void readAsNeeded(); |
| 61 | void readEntry(); |
| 62 | void readExtern(); |
| 63 | void readGroup(); |
| 64 | void readInclude(); |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 65 | void readInput(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 66 | void readMemory(); |
| 67 | void readOutput(); |
| 68 | void readOutputArch(); |
| 69 | void readOutputFormat(); |
Fangrui Song | 899fdf5 | 2021-06-13 19:41:11 | [diff] [blame] | 70 | void readOverwriteSections(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 71 | void readPhdrs(); |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 72 | void readRegionAlias(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 73 | void readSearchDir(); |
| 74 | void readSections(); |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 75 | void readTarget(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 76 | void readVersion(); |
| 77 | void readVersionScriptCommand(); |
Fangrui Song | 0778f5c | 2024-07-17 17:45:59 | [diff] [blame] | 78 | void readNoCrossRefs(bool to); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 79 | |
Fangrui Song | 30ec2bf | 2024-07-27 23:19:57 | [diff] [blame] | 80 | StringRef readName(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 81 | SymbolAssignment *readSymbolAssignment(StringRef name); |
| 82 | ByteCommand *readByteCommand(StringRef tok); |
Simon Atanasyan | b048605 | 2018-11-14 21:05:20 | [diff] [blame] | 83 | std::array<uint8_t, 4> readFill(); |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 84 | bool readSectionDirective(OutputSection *cmd, StringRef tok); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 85 | void readSectionAddressType(OutputSection *cmd); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 86 | OutputDesc *readOverlaySectionDescription(); |
| 87 | OutputDesc *readOutputSectionDescription(StringRef outSec); |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 88 | SmallVector<SectionCommand *, 0> readOverlay(); |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 89 | SectionClassDesc *readSectionClassDescription(); |
| 90 | StringRef readSectionClassName(); |
Fangrui Song | a1c2ee0 | 2021-12-26 21:53:47 | [diff] [blame] | 91 | SmallVector<StringRef, 0> readOutputSectionPhdrs(); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 92 | std::pair<uint64_t, uint64_t> readInputSectionFlags(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 93 | InputSectionDescription *readInputSectionDescription(StringRef tok); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 94 | StringMatcher readFilePatterns(); |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 95 | SmallVector<SectionPattern, 0> readInputSectionsList(); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 96 | InputSectionDescription *readInputSectionRules(StringRef filePattern, |
| 97 | uint64_t withFlags, |
| 98 | uint64_t withoutFlags); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 99 | unsigned readPhdrType(); |
Fangrui Song | 2a9aed0 | 2020-11-12 16:46:53 | [diff] [blame] | 100 | SortSectionPolicy peekSortKind(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 101 | SortSectionPolicy readSortKind(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 102 | SymbolAssignment *readProvideHidden(bool provide, bool hidden); |
| 103 | SymbolAssignment *readAssignment(StringRef tok); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 104 | void readSort(); |
George Rimar | d30a78b | 2018-04-25 11:16:31 | [diff] [blame] | 105 | Expr readAssert(); |
George Rimar | 5fb1712 | 2017-08-03 16:05:08 | [diff] [blame] | 106 | Expr readConstant(); |
| 107 | Expr getPageSize(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 108 | |
Fangrui Song | 92b5b98 | 2020-03-06 19:49:58 | [diff] [blame] | 109 | Expr readMemoryAssignment(StringRef, StringRef, StringRef); |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 110 | void readMemoryAttributes(uint32_t &flags, uint32_t &invFlags, |
| 111 | uint32_t &negFlags, uint32_t &negInvFlags); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 112 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 113 | Expr combine(StringRef op, Expr l, Expr r); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 114 | Expr readExpr(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 115 | Expr readExpr1(Expr lhs, int minPrec); |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 116 | StringRef readParenName(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 117 | Expr readPrimary(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 118 | Expr readTernary(Expr cond); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 119 | Expr readParenExpr(); |
| 120 | |
| 121 | // For parsing version script. |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 122 | SmallVector<SymbolVersion, 0> readVersionExtern(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 123 | void readAnonymousDeclaration(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 124 | void readVersionDeclaration(StringRef verStr); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 125 | |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 126 | std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>> |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 127 | readSymbols(); |
| 128 | |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 129 | Ctx &ctx; |
| 130 | |
Parth Arora | ebb326a | 2024-03-25 23:11:21 | [diff] [blame] | 131 | // If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command, |
| 132 | // then this member is set to the PROVIDE symbol name. |
| 133 | std::optional<llvm::StringRef> activeProvideSym; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 134 | }; |
Rui Ueyama | 96b3fe0 | 2017-04-05 05:08:01 | [diff] [blame] | 135 | } // namespace |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 136 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 137 | static StringRef unquote(StringRef s) { |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 138 | if (s.starts_with("\"")) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 139 | return s.substr(1, s.size() - 2); |
| 140 | return s; |
Rui Ueyama | 1e77ad1 | 2017-07-13 20:30:35 | [diff] [blame] | 141 | } |
| 142 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 143 | // Some operations only support one non absolute value. Move the |
| 144 | // absolute one to the right hand side for convenience. |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 145 | static void moveAbsRight(LinkerScript &s, ExprValue &a, ExprValue &b) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 146 | if (a.sec == nullptr || (a.forceAbsolute && !b.isAbsolute())) |
| 147 | std::swap(a, b); |
| 148 | if (!b.isAbsolute()) |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 149 | s.recordError(a.loc + |
| 150 | ": at least one side of the expression must be absolute"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 151 | } |
| 152 | |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 153 | static ExprValue add(LinkerScript &s, ExprValue a, ExprValue b) { |
| 154 | moveAbsRight(s, a, b); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 155 | return {a.sec, a.forceAbsolute, a.getSectionOffset() + b.getValue(), a.loc}; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 156 | } |
| 157 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 158 | static ExprValue sub(ExprValue a, ExprValue b) { |
Rafael Espindola | 63a4a98 | 2017-12-26 18:11:14 | [diff] [blame] | 159 | // The distance between two symbols in sections is absolute. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 160 | if (!a.isAbsolute() && !b.isAbsolute()) |
| 161 | return a.getValue() - b.getValue(); |
| 162 | return {a.sec, false, a.getSectionOffset() - b.getValue(), a.loc}; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 163 | } |
| 164 | |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 165 | static ExprValue bitAnd(LinkerScript &s, ExprValue a, ExprValue b) { |
| 166 | moveAbsRight(s, a, b); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 167 | return {a.sec, a.forceAbsolute, |
| 168 | (a.getValue() & b.getValue()) - a.getSecAddr(), a.loc}; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 169 | } |
| 170 | |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 171 | static ExprValue bitXor(LinkerScript &s, ExprValue a, ExprValue b) { |
| 172 | moveAbsRight(s, a, b); |
Fangrui Song | fae9610 | 2023-07-15 21:10:40 | [diff] [blame] | 173 | return {a.sec, a.forceAbsolute, |
| 174 | (a.getValue() ^ b.getValue()) - a.getSecAddr(), a.loc}; |
| 175 | } |
| 176 | |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 177 | static ExprValue bitOr(LinkerScript &s, ExprValue a, ExprValue b) { |
| 178 | moveAbsRight(s, a, b); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 179 | return {a.sec, a.forceAbsolute, |
| 180 | (a.getValue() | b.getValue()) - a.getSecAddr(), a.loc}; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 181 | } |
| 182 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 183 | void ScriptParser::readDynamicList() { |
| 184 | expect("{"); |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 185 | SmallVector<SymbolVersion, 0> locals; |
| 186 | SmallVector<SymbolVersion, 0> globals; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 187 | std::tie(locals, globals) = readSymbols(); |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 | [diff] [blame] | 188 | expect(";"); |
| 189 | |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 190 | StringRef tok = peek(); |
| 191 | if (tok.size()) { |
| 192 | setError("EOF expected, but got " + tok); |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 | [diff] [blame] | 193 | return; |
| 194 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 195 | if (!locals.empty()) { |
Rafael Espindola | d72d97b | 2017-09-08 18:16:59 | [diff] [blame] | 196 | setError("\"local:\" scope not supported in --dynamic-list"); |
| 197 | return; |
| 198 | } |
| 199 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 200 | for (SymbolVersion v : globals) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 201 | ctx.arg.dynamicList.push_back(v); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void ScriptParser::readVersionScript() { |
| 205 | readVersionScriptCommand(); |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 206 | StringRef tok = peek(); |
| 207 | if (tok.size()) |
| 208 | setError("EOF expected, but got " + tok); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void ScriptParser::readVersionScriptCommand() { |
| 212 | if (consume("{")) { |
| 213 | readAnonymousDeclaration(); |
| 214 | return; |
| 215 | } |
| 216 | |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 217 | if (atEOF()) |
| 218 | setError("unexpected EOF"); |
| 219 | while (peek() != "}" && !atEOF()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 220 | StringRef verStr = next(); |
| 221 | if (verStr == "{") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 222 | setError("anonymous version definition is used in " |
| 223 | "combination with other version definitions"); |
| 224 | return; |
| 225 | } |
| 226 | expect("{"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 227 | readVersionDeclaration(verStr); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | void ScriptParser::readVersion() { |
| 232 | expect("{"); |
| 233 | readVersionScriptCommand(); |
| 234 | expect("}"); |
| 235 | } |
| 236 | |
| 237 | void ScriptParser::readLinkerScript() { |
| 238 | while (!atEOF()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 239 | StringRef tok = next(); |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 240 | if (atEOF()) |
| 241 | break; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 242 | if (tok == ";") |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 243 | continue; |
| 244 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 245 | if (tok == "ENTRY") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 246 | readEntry(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 247 | } else if (tok == "EXTERN") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 248 | readExtern(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 249 | } else if (tok == "GROUP") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 250 | readGroup(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 251 | } else if (tok == "INCLUDE") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 252 | readInclude(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 253 | } else if (tok == "INPUT") { |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 254 | readInput(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 255 | } else if (tok == "MEMORY") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 256 | readMemory(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 257 | } else if (tok == "OUTPUT") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 258 | readOutput(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 259 | } else if (tok == "OUTPUT_ARCH") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 260 | readOutputArch(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 261 | } else if (tok == "OUTPUT_FORMAT") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 262 | readOutputFormat(); |
Fangrui Song | 899fdf5 | 2021-06-13 19:41:11 | [diff] [blame] | 263 | } else if (tok == "OVERWRITE_SECTIONS") { |
| 264 | readOverwriteSections(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 265 | } else if (tok == "PHDRS") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 266 | readPhdrs(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 267 | } else if (tok == "REGION_ALIAS") { |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 268 | readRegionAlias(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 269 | } else if (tok == "SEARCH_DIR") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 270 | readSearchDir(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 271 | } else if (tok == "SECTIONS") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 272 | readSections(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 273 | } else if (tok == "TARGET") { |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 274 | readTarget(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 275 | } else if (tok == "VERSION") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 276 | readVersion(); |
Fangrui Song | 0778f5c | 2024-07-17 17:45:59 | [diff] [blame] | 277 | } else if (tok == "NOCROSSREFS") { |
| 278 | readNoCrossRefs(/*to=*/false); |
| 279 | } else if (tok == "NOCROSSREFS_TO") { |
| 280 | readNoCrossRefs(/*to=*/true); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 281 | } else if (SymbolAssignment *cmd = readAssignment(tok)) { |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 282 | ctx.script->sectionCommands.push_back(cmd); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 283 | } else { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 284 | setError("unknown directive: " + tok); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Fangrui Song | ff7f97a | 2024-07-28 19:38:10 | [diff] [blame] | 289 | void ScriptParser::readDefsym() { |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 290 | if (errCount(ctx)) |
George Rimar | c152281 | 2018-11-26 12:29:56 | [diff] [blame] | 291 | return; |
Fangrui Song | ff7f97a | 2024-07-28 19:38:10 | [diff] [blame] | 292 | inExpr = true; |
| 293 | StringRef name = readName(); |
| 294 | expect("="); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 295 | Expr e = readExpr(); |
Petr Hosek | 8c7e8cc | 2017-11-04 02:03:58 | [diff] [blame] | 296 | if (!atEOF()) |
| 297 | setError("EOF expected, but got " + next()); |
Fangrui Song | 43b1334 | 2024-01-22 17:09:46 | [diff] [blame] | 298 | auto *cmd = make<SymbolAssignment>( |
| 299 | name, e, 0, getCurrentMB().getBufferIdentifier().str()); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 300 | ctx.script->sectionCommands.push_back(cmd); |
Petr Hosek | 8c7e8cc | 2017-11-04 02:03:58 | [diff] [blame] | 301 | } |
| 302 | |
Fangrui Song | 0778f5c | 2024-07-17 17:45:59 | [diff] [blame] | 303 | void ScriptParser::readNoCrossRefs(bool to) { |
| 304 | expect("("); |
| 305 | NoCrossRefCommand cmd{{}, to}; |
Fangrui Song | b32c38a | 2024-07-27 00:25:23 | [diff] [blame] | 306 | while (auto tok = till(")")) |
| 307 | cmd.outputSections.push_back(unquote(tok)); |
Fangrui Song | 0778f5c | 2024-07-17 17:45:59 | [diff] [blame] | 308 | if (cmd.outputSections.size() < 2) |
Fangrui Song | f8bae3a | 2024-11-07 06:19:31 | [diff] [blame] | 309 | Warn(ctx) << getCurrentLocation() |
| 310 | << ": ignored with fewer than 2 output sections"; |
Fangrui Song | 0778f5c | 2024-07-17 17:45:59 | [diff] [blame] | 311 | else |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 312 | ctx.script->noCrossRefs.push_back(std::move(cmd)); |
Fangrui Song | 0778f5c | 2024-07-17 17:45:59 | [diff] [blame] | 313 | } |
| 314 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 315 | void ScriptParser::addFile(StringRef s) { |
Fangrui Song | a7e8bdd | 2024-07-28 18:43:27 | [diff] [blame] | 316 | if (curBuf.isUnderSysroot && s.starts_with("/")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 317 | SmallString<128> pathData; |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 318 | StringRef path = (ctx.arg.sysroot + s).toStringRef(pathData); |
Fangrui Song | 2508733 | 2021-06-25 19:52:39 | [diff] [blame] | 319 | if (sys::fs::exists(path)) |
Fangrui Song | 2991a4e | 2024-11-17 06:34:12 | [diff] [blame] | 320 | ctx.driver.addFile(ctx.saver.save(path), /*withLOption=*/false); |
Fangrui Song | 2508733 | 2021-06-25 19:52:39 | [diff] [blame] | 321 | else |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 322 | setError("cannot find " + s + " inside " + ctx.arg.sysroot); |
Fangrui Song | 2508733 | 2021-06-25 19:52:39 | [diff] [blame] | 323 | return; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 324 | } |
| 325 | |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 326 | if (s.starts_with("/")) { |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 327 | // Case 1: s is an absolute path. Just open it. |
Fangrui Song | f596d82 | 2022-10-01 22:12:50 | [diff] [blame] | 328 | ctx.driver.addFile(s, /*withLOption=*/false); |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 329 | } else if (s.starts_with("=")) { |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 330 | // Case 2: relative to the sysroot. |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 331 | if (ctx.arg.sysroot.empty()) |
Fangrui Song | f596d82 | 2022-10-01 22:12:50 | [diff] [blame] | 332 | ctx.driver.addFile(s.substr(1), /*withLOption=*/false); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 333 | else |
Fangrui Song | 2991a4e | 2024-11-17 06:34:12 | [diff] [blame] | 334 | ctx.driver.addFile(ctx.saver.save(ctx.arg.sysroot + "/" + s.substr(1)), |
Fangrui Song | f596d82 | 2022-10-01 22:12:50 | [diff] [blame] | 335 | /*withLOption=*/false); |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 336 | } else if (s.starts_with("-l")) { |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 337 | // Case 3: search in the list of library paths. |
Fangrui Song | f596d82 | 2022-10-01 22:12:50 | [diff] [blame] | 338 | ctx.driver.addLibrary(s.substr(2)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 339 | } else { |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 340 | // Case 4: s is a relative path. Search in the directory of the script file. |
| 341 | std::string filename = std::string(getCurrentMB().getBufferIdentifier()); |
| 342 | StringRef directory = sys::path::parent_path(filename); |
| 343 | if (!directory.empty()) { |
| 344 | SmallString<0> path(directory); |
| 345 | sys::path::append(path, s); |
| 346 | if (sys::fs::exists(path)) { |
Fangrui Song | f596d82 | 2022-10-01 22:12:50 | [diff] [blame] | 347 | ctx.driver.addFile(path, /*withLOption=*/false); |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | } |
| 351 | // Then search in the current working directory. |
| 352 | if (sys::fs::exists(s)) { |
Fangrui Song | f596d82 | 2022-10-01 22:12:50 | [diff] [blame] | 353 | ctx.driver.addFile(s, /*withLOption=*/false); |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 354 | } else { |
| 355 | // Finally, search in the list of library paths. |
Fangrui Song | 4986510 | 2024-10-06 18:27:24 | [diff] [blame] | 356 | if (std::optional<std::string> path = findFromSearchPaths(ctx, s)) |
Fangrui Song | 2991a4e | 2024-11-17 06:34:12 | [diff] [blame] | 357 | ctx.driver.addFile(ctx.saver.save(*path), /*withLOption=*/true); |
Fangrui Song | c384ca3 | 2020-04-09 04:45:21 | [diff] [blame] | 358 | else |
| 359 | setError("unable to find " + s); |
| 360 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | void ScriptParser::readAsNeeded() { |
| 365 | expect("("); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 366 | bool orig = ctx.arg.asNeeded; |
| 367 | ctx.arg.asNeeded = true; |
Fangrui Song | 10bb296 | 2024-07-27 00:19:04 | [diff] [blame] | 368 | while (auto tok = till(")")) |
| 369 | addFile(unquote(tok)); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 370 | ctx.arg.asNeeded = orig; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void ScriptParser::readEntry() { |
| 374 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 375 | expect("("); |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 376 | StringRef name = readName(); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 377 | if (ctx.arg.entry.empty()) |
| 378 | ctx.arg.entry = name; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 379 | expect(")"); |
| 380 | } |
| 381 | |
| 382 | void ScriptParser::readExtern() { |
| 383 | expect("("); |
Fangrui Song | 10bb296 | 2024-07-27 00:19:04 | [diff] [blame] | 384 | while (auto tok = till(")")) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 385 | ctx.arg.undefined.push_back(unquote(tok)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void ScriptParser::readGroup() { |
Fangrui Song | f2b0133 | 2024-10-07 00:38:35 | [diff] [blame] | 389 | SaveAndRestore saved(ctx.driver.isInGroup, true); |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 390 | readInput(); |
Fangrui Song | f2b0133 | 2024-10-07 00:38:35 | [diff] [blame] | 391 | if (!saved.get()) |
| 392 | ++ctx.driver.nextGroupId; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void ScriptParser::readInclude() { |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 396 | StringRef name = readName(); |
Fangrui Song | 8f72b0c | 2024-07-28 00:25:13 | [diff] [blame] | 397 | if (!activeFilenames.insert(name).second) { |
Rui Ueyama | 0440be4 | 2017-09-06 18:14:08 | [diff] [blame] | 398 | setError("there is a cycle in linker script INCLUDEs"); |
| 399 | return; |
| 400 | } |
| 401 | |
Fangrui Song | 4986510 | 2024-10-06 18:27:24 | [diff] [blame] | 402 | if (std::optional<std::string> path = searchScript(ctx, name)) { |
| 403 | if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) { |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 404 | buffers.push_back(curBuf); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 405 | curBuf = Buffer(ctx, *mb); |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 406 | mbs.push_back(*mb); |
| 407 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 408 | return; |
| 409 | } |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 410 | setError("cannot find linker script " + name); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 411 | } |
| 412 | |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 413 | void ScriptParser::readInput() { |
| 414 | expect("("); |
Fangrui Song | 10bb296 | 2024-07-27 00:19:04 | [diff] [blame] | 415 | while (auto tok = till(")")) { |
| 416 | if (tok == "AS_NEEDED") |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 417 | readAsNeeded(); |
| 418 | else |
Fangrui Song | 10bb296 | 2024-07-27 00:19:04 | [diff] [blame] | 419 | addFile(unquote(tok)); |
Rui Ueyama | 1d92aa7 | 2018-04-09 23:05:48 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 423 | void ScriptParser::readOutput() { |
| 424 | // -o <file> takes predecence over OUTPUT(<file>). |
| 425 | expect("("); |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 426 | StringRef name = readName(); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 427 | if (ctx.arg.outputFile.empty()) |
| 428 | ctx.arg.outputFile = name; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 429 | expect(")"); |
| 430 | } |
| 431 | |
| 432 | void ScriptParser::readOutputArch() { |
| 433 | // OUTPUT_ARCH is ignored for now. |
| 434 | expect("("); |
Fangrui Song | dbd65a0 | 2024-07-27 23:52:47 | [diff] [blame] | 435 | while (till(")")) |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 436 | ; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 437 | } |
| 438 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 439 | static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) { |
| 440 | return StringSwitch<std::pair<ELFKind, uint16_t>>(s) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 441 | .Case("elf32-i386", {ELF32LEKind, EM_386}) |
Ben Shi | 8527f32 | 2022-05-12 13:26:15 | [diff] [blame] | 442 | .Case("elf32-avr", {ELF32LEKind, EM_AVR}) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 443 | .Case("elf32-iamcu", {ELF32LEKind, EM_IAMCU}) |
| 444 | .Case("elf32-littlearm", {ELF32LEKind, EM_ARM}) |
Simi Pallipurath | 2f68ddc | 2023-02-20 13:31:45 | [diff] [blame] | 445 | .Case("elf32-bigarm", {ELF32BEKind, EM_ARM}) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 446 | .Case("elf32-x86-64", {ELF32LEKind, EM_X86_64}) |
Dimitry Andric | 19b134c | 2019-01-30 06:31:52 | [diff] [blame] | 447 | .Case("elf64-aarch64", {ELF64LEKind, EM_AARCH64}) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 448 | .Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64}) |
Fangrui Song | 7605a9a | 2021-02-08 16:55:28 | [diff] [blame] | 449 | .Case("elf64-bigaarch64", {ELF64BEKind, EM_AARCH64}) |
Rui Ueyama | 4134143 | 2019-02-13 18:51:15 | [diff] [blame] | 450 | .Case("elf32-powerpc", {ELF32BEKind, EM_PPC}) |
Brandon Bergren | 275eb82 | 2021-01-02 18:18:05 | [diff] [blame] | 451 | .Case("elf32-powerpcle", {ELF32LEKind, EM_PPC}) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 452 | .Case("elf64-powerpc", {ELF64BEKind, EM_PPC64}) |
| 453 | .Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64}) |
| 454 | .Case("elf64-x86-64", {ELF64LEKind, EM_X86_64}) |
Rui Ueyama | 4134143 | 2019-02-13 18:51:15 | [diff] [blame] | 455 | .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS}) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 456 | .Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS}) |
| 457 | .Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS}) |
| 458 | .Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS}) |
| 459 | .Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS}) |
| 460 | .Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS}) |
Fangrui Song | 44d908d | 2019-06-10 08:09:55 | [diff] [blame] | 461 | .Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV}) |
| 462 | .Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV}) |
LemonBoy | aff950e | 2020-04-17 14:58:15 | [diff] [blame] | 463 | .Case("elf64-sparc", {ELF64BEKind, EM_SPARCV9}) |
LemonBoy | 92c6141 | 2020-12-14 17:38:12 | [diff] [blame] | 464 | .Case("elf32-msp430", {ELF32LEKind, EM_MSP430}) |
WANG Xuerui | 6084ee7 | 2023-07-25 09:03:28 | [diff] [blame] | 465 | .Case("elf32-loongarch", {ELF32LEKind, EM_LOONGARCH}) |
| 466 | .Case("elf64-loongarch", {ELF64LEKind, EM_LOONGARCH}) |
Ulrich Weigand | fe3406e | 2024-02-13 10:29:21 | [diff] [blame] | 467 | .Case("elf64-s390", {ELF64BEKind, EM_S390}) |
Brian Cain | 9078036 | 2024-07-16 20:01:27 | [diff] [blame] | 468 | .Cases("elf32-hexagon", "elf32-littlehexagon", {ELF32LEKind, EM_HEXAGON}) |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 469 | .Default({ELFNoneKind, EM_NONE}); |
Rui Ueyama | ea8cd00 | 2018-10-22 20:50:01 | [diff] [blame] | 470 | } |
| 471 | |
Fangrui Song | eea34aa | 2021-02-08 18:34:57 | [diff] [blame] | 472 | // Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(default, big, little). Choose |
| 473 | // big if -EB is specified, little if -EL is specified, or default if neither is |
| 474 | // specified. |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 475 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 476 | expect("("); |
Rui Ueyama | ea8cd00 | 2018-10-22 20:50:01 | [diff] [blame] | 477 | |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 478 | StringRef s = readName(); |
Fangrui Song | eea34aa | 2021-02-08 18:34:57 | [diff] [blame] | 479 | if (!consume(")")) { |
| 480 | expect(","); |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 481 | StringRef tmp = readName(); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 482 | if (ctx.arg.optEB) |
Fangrui Song | 6464dd2 | 2024-07-16 17:28:09 | [diff] [blame] | 483 | s = tmp; |
Fangrui Song | eea34aa | 2021-02-08 18:34:57 | [diff] [blame] | 484 | expect(","); |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 485 | tmp = readName(); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 486 | if (ctx.arg.optEL) |
Fangrui Song | 6464dd2 | 2024-07-16 17:28:09 | [diff] [blame] | 487 | s = tmp; |
Fangrui Song | eea34aa | 2021-02-08 18:34:57 | [diff] [blame] | 488 | consume(")"); |
| 489 | } |
Fangrui Song | 6464dd2 | 2024-07-16 17:28:09 | [diff] [blame] | 490 | // If more than one OUTPUT_FORMAT is specified, only the first is checked. |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 491 | if (!ctx.arg.bfdname.empty()) |
Fangrui Song | 6464dd2 | 2024-07-16 17:28:09 | [diff] [blame] | 492 | return; |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 493 | ctx.arg.bfdname = s; |
Fangrui Song | 6464dd2 | 2024-07-16 17:28:09 | [diff] [blame] | 494 | |
| 495 | if (s == "binary") { |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 496 | ctx.arg.oFormatBinary = true; |
Fangrui Song | 6464dd2 | 2024-07-16 17:28:09 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 500 | if (s.consume_back("-freebsd")) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 501 | ctx.arg.osabi = ELFOSABI_FREEBSD; |
Rui Ueyama | 4f8c822 | 2019-01-28 19:11:52 | [diff] [blame] | 502 | |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 503 | std::tie(ctx.arg.ekind, ctx.arg.emachine) = parseBfdName(s); |
| 504 | if (ctx.arg.emachine == EM_NONE) |
| 505 | setError("unknown output format name: " + ctx.arg.bfdname); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 506 | if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips") |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 507 | ctx.arg.mipsN32Abi = true; |
| 508 | if (ctx.arg.emachine == EM_MSP430) |
| 509 | ctx.arg.osabi = ELFOSABI_STANDALONE; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void ScriptParser::readPhdrs() { |
| 513 | expect("{"); |
Fangrui Song | 2a89356 | 2024-07-27 00:13:37 | [diff] [blame] | 514 | while (auto tok = till("}")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 515 | PhdrsCommand cmd; |
Fangrui Song | 2a89356 | 2024-07-27 00:13:37 | [diff] [blame] | 516 | cmd.name = tok; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 517 | cmd.type = readPhdrType(); |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 518 | |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 519 | while (!errCount(ctx) && !consume(";")) { |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 520 | if (consume("FILEHDR")) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 521 | cmd.hasFilehdr = true; |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 522 | else if (consume("PHDRS")) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 523 | cmd.hasPhdrs = true; |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 524 | else if (consume("AT")) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 525 | cmd.lmaExpr = readParenExpr(); |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 526 | else if (consume("FLAGS")) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 527 | cmd.flags = readParenExpr()().getValue(); |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 528 | else |
| 529 | setError("unexpected header attribute: " + next()); |
| 530 | } |
Rui Ueyama | 0ae2c24 | 2017-10-08 03:45:49 | [diff] [blame] | 531 | |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 532 | ctx.script->phdrsCommands.push_back(cmd); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 536 | void ScriptParser::readRegionAlias() { |
| 537 | expect("("); |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 538 | StringRef alias = readName(); |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 539 | expect(","); |
Fangrui Song | 74ef53a | 2024-07-27 23:29:43 | [diff] [blame] | 540 | StringRef name = readName(); |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 541 | expect(")"); |
| 542 | |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 543 | if (ctx.script->memoryRegions.count(alias)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 544 | setError("redefinition of memory region '" + alias + "'"); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 545 | if (!ctx.script->memoryRegions.count(name)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 546 | setError("memory region '" + name + "' is not defined"); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 547 | ctx.script->memoryRegions.insert({alias, ctx.script->memoryRegions[name]}); |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 548 | } |
| 549 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 550 | void ScriptParser::readSearchDir() { |
| 551 | expect("("); |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 552 | StringRef name = readName(); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 553 | if (!ctx.arg.nostdlib) |
| 554 | ctx.arg.searchPaths.push_back(name); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 555 | expect(")"); |
| 556 | } |
| 557 | |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 558 | // This reads an overlay description. Overlays are used to describe output |
| 559 | // sections that use the same virtual memory range and normally would trigger |
| 560 | // linker's sections sanity check failures. |
| 561 | // https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 562 | SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() { |
Fangrui Song | 7c89b20 | 2024-01-09 00:12:49 | [diff] [blame] | 563 | Expr addrExpr; |
| 564 | if (consume(":")) { |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 565 | addrExpr = [s = ctx.script] { return s->getDot(); }; |
Fangrui Song | 7c89b20 | 2024-01-09 00:12:49 | [diff] [blame] | 566 | } else { |
| 567 | addrExpr = readExpr(); |
| 568 | expect(":"); |
| 569 | } |
| 570 | // When AT is omitted, LMA should equal VMA. script->getDot() when evaluating |
| 571 | // lmaExpr will ensure this, even if the start address is specified. |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 572 | Expr lmaExpr = consume("AT") ? readParenExpr() |
| 573 | : [s = ctx.script] { return s->getDot(); }; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 574 | expect("{"); |
| 575 | |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 576 | SmallVector<SectionCommand *, 0> v; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 577 | OutputSection *prev = nullptr; |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 578 | while (!errCount(ctx) && !consume("}")) { |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 579 | // VA is the same for all sections. The LMAs are consecutive in memory |
| 580 | // starting from the base load address specified. |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 581 | OutputDesc *osd = readOverlaySectionDescription(); |
| 582 | osd->osec.addrExpr = addrExpr; |
Fangrui Song | 7c89b20 | 2024-01-09 00:12:49 | [diff] [blame] | 583 | if (prev) { |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 584 | osd->osec.lmaExpr = [=] { return prev->getLMA() + prev->size; }; |
Fangrui Song | 7c89b20 | 2024-01-09 00:12:49 | [diff] [blame] | 585 | } else { |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 586 | osd->osec.lmaExpr = lmaExpr; |
Fangrui Song | 7c89b20 | 2024-01-09 00:12:49 | [diff] [blame] | 587 | // Use first section address for subsequent sections as initial addrExpr |
| 588 | // can be DOT. Ensure the first section, even if empty, is not discarded. |
| 589 | osd->osec.usedInExpression = true; |
| 590 | addrExpr = [=]() -> ExprValue { return {&osd->osec, false, 0, ""}; }; |
| 591 | } |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 592 | v.push_back(osd); |
| 593 | prev = &osd->osec; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // According to the specification, at the end of the overlay, the location |
| 597 | // counter should be equal to the overlay base address plus size of the |
| 598 | // largest section seen in the overlay. |
| 599 | // Here we want to create the Dot assignment command to achieve that. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 600 | Expr moveDot = [=] { |
| 601 | uint64_t max = 0; |
Fangrui Song | 7051aee | 2021-11-26 04:24:23 | [diff] [blame] | 602 | for (SectionCommand *cmd : v) |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 603 | max = std::max(max, cast<OutputDesc>(cmd)->osec.size); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 604 | return addrExpr().getValue() + max; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 605 | }; |
Fangrui Song | 65a15a5 | 2023-09-09 21:46:51 | [diff] [blame] | 606 | v.push_back(make<SymbolAssignment>(".", moveDot, 0, getCurrentLocation())); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 607 | return v; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 608 | } |
| 609 | |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 610 | SectionClassDesc *ScriptParser::readSectionClassDescription() { |
| 611 | StringRef name = readSectionClassName(); |
| 612 | SectionClassDesc *desc = make<SectionClassDesc>(name); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 613 | if (!ctx.script->sectionClasses.insert({CachedHashStringRef(name), desc}) |
| 614 | .second) |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 615 | setError("section class '" + name + "' already defined"); |
| 616 | expect("{"); |
| 617 | while (auto tok = till("}")) { |
| 618 | if (tok == "(" || tok == ")") { |
| 619 | setError("expected filename pattern"); |
| 620 | } else if (peek() == "(") { |
| 621 | InputSectionDescription *isd = readInputSectionDescription(tok); |
| 622 | if (!isd->classRef.empty()) |
| 623 | setError("section class '" + name + "' references class '" + |
| 624 | isd->classRef + "'"); |
| 625 | desc->sc.commands.push_back(isd); |
| 626 | } |
| 627 | } |
| 628 | return desc; |
| 629 | } |
| 630 | |
| 631 | StringRef ScriptParser::readSectionClassName() { |
| 632 | expect("("); |
| 633 | StringRef name = unquote(next()); |
| 634 | expect(")"); |
| 635 | return name; |
| 636 | } |
| 637 | |
Fangrui Song | 899fdf5 | 2021-06-13 19:41:11 | [diff] [blame] | 638 | void ScriptParser::readOverwriteSections() { |
| 639 | expect("{"); |
Fangrui Song | 2a89356 | 2024-07-27 00:13:37 | [diff] [blame] | 640 | while (auto tok = till("}")) |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 641 | ctx.script->overwriteSections.push_back(readOutputSectionDescription(tok)); |
Fangrui Song | 899fdf5 | 2021-06-13 19:41:11 | [diff] [blame] | 642 | } |
| 643 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 644 | void ScriptParser::readSections() { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 645 | expect("{"); |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 646 | SmallVector<SectionCommand *, 0> v; |
Fangrui Song | 2a89356 | 2024-07-27 00:13:37 | [diff] [blame] | 647 | while (auto tok = till("}")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 648 | if (tok == "OVERLAY") { |
Fangrui Song | 7051aee | 2021-11-26 04:24:23 | [diff] [blame] | 649 | for (SectionCommand *cmd : readOverlay()) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 650 | v.push_back(cmd); |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 651 | continue; |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 652 | } |
| 653 | if (tok == "CLASS") { |
| 654 | v.push_back(readSectionClassDescription()); |
| 655 | continue; |
| 656 | } |
| 657 | if (tok == "INCLUDE") { |
Rui Ueyama | 2e9d40d | 2018-10-12 17:07:32 | [diff] [blame] | 658 | readInclude(); |
| 659 | continue; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 660 | } |
| 661 | |
Fangrui Song | 7051aee | 2021-11-26 04:24:23 | [diff] [blame] | 662 | if (SectionCommand *cmd = readAssignment(tok)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 663 | v.push_back(cmd); |
George Rimar | d30a78b | 2018-04-25 11:16:31 | [diff] [blame] | 664 | else |
Fangrui Song | 49dfbc6 | 2023-07-05 22:08:53 | [diff] [blame] | 665 | v.push_back(readOutputSectionDescription(tok)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 666 | } |
Fangrui Song | 5a44980 | 2022-05-04 08:10:45 | [diff] [blame] | 667 | |
| 668 | // If DATA_SEGMENT_RELRO_END is absent, for sections after DATA_SEGMENT_ALIGN, |
| 669 | // the relro fields should be cleared. |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 670 | if (!ctx.script->seenRelroEnd) |
Fangrui Song | 5a44980 | 2022-05-04 08:10:45 | [diff] [blame] | 671 | for (SectionCommand *cmd : v) |
| 672 | if (auto *osd = dyn_cast<OutputDesc>(cmd)) |
| 673 | osd->osec.relro = false; |
| 674 | |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 675 | ctx.script->sectionCommands.insert(ctx.script->sectionCommands.end(), |
| 676 | v.begin(), v.end()); |
George Rimar | 9e2c8a9 | 2018-03-08 14:54:38 | [diff] [blame] | 677 | |
Fangrui Song | 7c426fb | 2020-02-10 23:58:29 | [diff] [blame] | 678 | if (atEOF() || !consume("INSERT")) { |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 679 | ctx.script->hasSectionsCommand = true; |
George Rimar | 9e2c8a9 | 2018-03-08 14:54:38 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
Fangrui Song | 7c426fb | 2020-02-10 23:58:29 | [diff] [blame] | 683 | bool isAfter = false; |
| 684 | if (consume("AFTER")) |
| 685 | isAfter = true; |
| 686 | else if (!consume("BEFORE")) |
| 687 | setError("expected AFTER/BEFORE, but got '" + next() + "'"); |
Fangrui Song | 9c16a4a | 2024-07-28 00:34:37 | [diff] [blame] | 688 | StringRef where = readName(); |
Fangrui Song | a1c2ee0 | 2021-12-26 21:53:47 | [diff] [blame] | 689 | SmallVector<StringRef, 0> names; |
Fangrui Song | 7051aee | 2021-11-26 04:24:23 | [diff] [blame] | 690 | for (SectionCommand *cmd : v) |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 691 | if (auto *os = dyn_cast<OutputDesc>(cmd)) |
| 692 | names.push_back(os->osec.name); |
Fangrui Song | 03051f7 | 2021-06-30 18:35:50 | [diff] [blame] | 693 | if (!names.empty()) |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 694 | ctx.script->insertCommands.push_back({std::move(names), isAfter, where}); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 695 | } |
| 696 | |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 697 | void ScriptParser::readTarget() { |
| 698 | // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers, |
| 699 | // we accept only a limited set of BFD names (i.e. "elf" or "binary") |
| 700 | // for --format. We recognize only /^elf/ and "binary" in the linker |
| 701 | // script as well. |
| 702 | expect("("); |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 703 | StringRef tok = readName(); |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 704 | expect(")"); |
| 705 | |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 706 | if (tok.starts_with("elf")) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 707 | ctx.arg.formatBinary = false; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 708 | else if (tok == "binary") |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 709 | ctx.arg.formatBinary = true; |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 710 | else |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 711 | setError("unknown target: " + tok); |
Rui Ueyama | e262bb1 | 2018-08-06 21:29:41 | [diff] [blame] | 712 | } |
| 713 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 714 | static int precedence(StringRef op) { |
| 715 | return StringSwitch<int>(op) |
Fangrui Song | fae9610 | 2023-07-15 21:10:40 | [diff] [blame] | 716 | .Cases("*", "/", "%", 11) |
| 717 | .Cases("+", "-", 10) |
| 718 | .Cases("<<", ">>", 9) |
| 719 | .Cases("<", "<=", ">", ">=", 8) |
| 720 | .Cases("==", "!=", 7) |
| 721 | .Case("&", 6) |
| 722 | .Case("^", 5) |
Fangrui Song | d479b2e | 2022-06-25 20:47:32 | [diff] [blame] | 723 | .Case("|", 4) |
| 724 | .Case("&&", 3) |
| 725 | .Case("||", 2) |
Fangrui Song | b0d6dd3 | 2022-06-25 20:48:52 | [diff] [blame] | 726 | .Case("?", 1) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 727 | .Default(-1); |
| 728 | } |
| 729 | |
| 730 | StringMatcher ScriptParser::readFilePatterns() { |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 | [diff] [blame] | 731 | StringMatcher Matcher; |
Fangrui Song | 10bb296 | 2024-07-27 00:19:04 | [diff] [blame] | 732 | while (auto tok = till(")")) |
| 733 | Matcher.addPattern(SingleStringMatcher(tok)); |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 | [diff] [blame] | 734 | return Matcher; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 735 | } |
| 736 | |
Fangrui Song | 2a9aed0 | 2020-11-12 16:46:53 | [diff] [blame] | 737 | SortSectionPolicy ScriptParser::peekSortKind() { |
| 738 | return StringSwitch<SortSectionPolicy>(peek()) |
Justin Cady | 447aa48 | 2023-03-06 14:35:39 | [diff] [blame] | 739 | .Case("REVERSE", SortSectionPolicy::Reverse) |
Fangrui Song | 2a9aed0 | 2020-11-12 16:46:53 | [diff] [blame] | 740 | .Cases("SORT", "SORT_BY_NAME", SortSectionPolicy::Name) |
| 741 | .Case("SORT_BY_ALIGNMENT", SortSectionPolicy::Alignment) |
| 742 | .Case("SORT_BY_INIT_PRIORITY", SortSectionPolicy::Priority) |
| 743 | .Case("SORT_NONE", SortSectionPolicy::None) |
| 744 | .Default(SortSectionPolicy::Default); |
| 745 | } |
| 746 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 747 | SortSectionPolicy ScriptParser::readSortKind() { |
Fangrui Song | 2a9aed0 | 2020-11-12 16:46:53 | [diff] [blame] | 748 | SortSectionPolicy ret = peekSortKind(); |
| 749 | if (ret != SortSectionPolicy::Default) |
| 750 | skip(); |
| 751 | return ret; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 752 | } |
| 753 | |
Rui Ueyama | 03fc8d1 | 2017-04-05 19:20:54 | [diff] [blame] | 754 | // Reads SECTIONS command contents in the following form: |
| 755 | // |
| 756 | // <contents> ::= <elem>* |
| 757 | // <elem> ::= <exclude>? <glob-pattern> |
| 758 | // <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")" |
| 759 | // |
| 760 | // For example, |
| 761 | // |
| 762 | // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz) |
| 763 | // |
| 764 | // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o". |
| 765 | // The semantics of that is section .foo in any file, section .bar in |
| 766 | // any file but a.o, and section .baz in any file but b.o. |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 767 | SmallVector<SectionPattern, 0> ScriptParser::readInputSectionsList() { |
| 768 | SmallVector<SectionPattern, 0> ret; |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 769 | while (!errCount(ctx) && peek() != ")") { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 770 | StringMatcher excludeFilePat; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 771 | if (consume("EXCLUDE_FILE")) { |
| 772 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 773 | excludeFilePat = readFilePatterns(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 774 | } |
| 775 | |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 | [diff] [blame] | 776 | StringMatcher SectionMatcher; |
Fangrui Song | 2a9aed0 | 2020-11-12 16:46:53 | [diff] [blame] | 777 | // Break if the next token is ), EXCLUDE_FILE, or SORT*. |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 778 | while (!errCount(ctx) && peekSortKind() == SortSectionPolicy::Default) { |
Fangrui Song | 551e20d | 2024-03-07 01:19:59 | [diff] [blame] | 779 | StringRef s = peek(); |
| 780 | if (s == ")" || s == "EXCLUDE_FILE") |
| 781 | break; |
| 782 | // Detect common mistakes when certain non-wildcard meta characters are |
| 783 | // used without a closing ')'. |
| 784 | if (!s.empty() && strchr("(){}", s[0])) { |
| 785 | skip(); |
| 786 | setError("section pattern is expected"); |
| 787 | break; |
| 788 | } |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 789 | SectionMatcher.addPattern(readName()); |
Fangrui Song | 551e20d | 2024-03-07 01:19:59 | [diff] [blame] | 790 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 791 | |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 | [diff] [blame] | 792 | if (!SectionMatcher.empty()) |
| 793 | ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)}); |
Fangrui Song | 2a9aed0 | 2020-11-12 16:46:53 | [diff] [blame] | 794 | else if (excludeFilePat.empty()) |
| 795 | break; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 796 | else |
| 797 | setError("section pattern is expected"); |
| 798 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 799 | return ret; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | // Reads contents of "SECTIONS" directive. That directive contains a |
| 803 | // list of glob patterns for input sections. The grammar is as follows. |
| 804 | // |
| 805 | // <patterns> ::= <section-list> |
| 806 | // | <sort> "(" <section-list> ")" |
| 807 | // | <sort> "(" <sort> "(" <section-list> ")" ")" |
| 808 | // |
| 809 | // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT" |
| 810 | // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE" |
| 811 | // |
| 812 | // <section-list> is parsed by readInputSectionsList(). |
| 813 | InputSectionDescription * |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 814 | ScriptParser::readInputSectionRules(StringRef filePattern, uint64_t withFlags, |
| 815 | uint64_t withoutFlags) { |
| 816 | auto *cmd = |
| 817 | make<InputSectionDescription>(filePattern, withFlags, withoutFlags); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 818 | expect("("); |
| 819 | |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 820 | while (peek() != ")" && !atEOF()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 821 | SortSectionPolicy outer = readSortKind(); |
| 822 | SortSectionPolicy inner = SortSectionPolicy::Default; |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 823 | SmallVector<SectionPattern, 0> v; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 824 | if (outer != SortSectionPolicy::Default) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 825 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 826 | inner = readSortKind(); |
| 827 | if (inner != SortSectionPolicy::Default) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 828 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 829 | v = readInputSectionsList(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 830 | expect(")"); |
| 831 | } else { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 832 | v = readInputSectionsList(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 833 | } |
| 834 | expect(")"); |
| 835 | } else { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 836 | v = readInputSectionsList(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 837 | } |
| 838 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 839 | for (SectionPattern &pat : v) { |
| 840 | pat.sortInner = inner; |
| 841 | pat.sortOuter = outer; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 842 | } |
| 843 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 844 | std::move(v.begin(), v.end(), std::back_inserter(cmd->sectionPatterns)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 845 | } |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 846 | expect(")"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 847 | return cmd; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | InputSectionDescription * |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 851 | ScriptParser::readInputSectionDescription(StringRef tok) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 852 | // Input section wildcard can be surrounded by KEEP. |
| 853 | // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 854 | uint64_t withFlags = 0; |
| 855 | uint64_t withoutFlags = 0; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 856 | if (tok == "KEEP") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 857 | expect("("); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 858 | if (consume("INPUT_SECTION_FLAGS")) |
| 859 | std::tie(withFlags, withoutFlags) = readInputSectionFlags(); |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 860 | |
| 861 | tok = next(); |
| 862 | InputSectionDescription *cmd; |
| 863 | if (tok == "CLASS") |
| 864 | cmd = make<InputSectionDescription>(StringRef{}, withFlags, withoutFlags, |
| 865 | readSectionClassName()); |
| 866 | else |
| 867 | cmd = readInputSectionRules(tok, withFlags, withoutFlags); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 868 | expect(")"); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 869 | ctx.script->keptSections.push_back(cmd); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 870 | return cmd; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 871 | } |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 872 | if (tok == "INPUT_SECTION_FLAGS") { |
| 873 | std::tie(withFlags, withoutFlags) = readInputSectionFlags(); |
| 874 | tok = next(); |
| 875 | } |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 876 | if (tok == "CLASS") |
| 877 | return make<InputSectionDescription>(StringRef{}, withFlags, withoutFlags, |
| 878 | readSectionClassName()); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 879 | return readInputSectionRules(tok, withFlags, withoutFlags); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void ScriptParser::readSort() { |
| 883 | expect("("); |
| 884 | expect("CONSTRUCTORS"); |
| 885 | expect(")"); |
| 886 | } |
| 887 | |
George Rimar | d30a78b | 2018-04-25 11:16:31 | [diff] [blame] | 888 | Expr ScriptParser::readAssert() { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 889 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 890 | Expr e = readExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 891 | expect(","); |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 892 | StringRef msg = readName(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 893 | expect(")"); |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 894 | |
Fangrui Song | 9b058bb | 2024-11-07 06:33:51 | [diff] [blame] | 895 | return [=, s = ctx.script, &ctx = ctx]() -> ExprValue { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 896 | if (!e().getValue()) |
Fangrui Song | 9b058bb | 2024-11-07 06:33:51 | [diff] [blame] | 897 | Err(ctx) << msg; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 898 | return s->getDot(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 899 | }; |
| 900 | } |
| 901 | |
Fangrui Song | 66f8ac8 | 2022-02-17 20:10:58 | [diff] [blame] | 902 | #define ECase(X) \ |
| 903 | { #X, X } |
| 904 | constexpr std::pair<const char *, unsigned> typeMap[] = { |
| 905 | ECase(SHT_PROGBITS), ECase(SHT_NOTE), ECase(SHT_NOBITS), |
| 906 | ECase(SHT_INIT_ARRAY), ECase(SHT_FINI_ARRAY), ECase(SHT_PREINIT_ARRAY), |
| 907 | }; |
| 908 | #undef ECase |
| 909 | |
George Rimar | a46d08e | 2018-08-28 08:39:21 | [diff] [blame] | 910 | // Tries to read the special directive for an output section definition which |
Fangrui Song | 66f8ac8 | 2022-02-17 20:10:58 | [diff] [blame] | 911 | // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)", "(OVERLAY)", and |
| 912 | // "(TYPE=<value>)". |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 913 | bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok) { |
| 914 | if (tok != "NOLOAD" && tok != "COPY" && tok != "INFO" && tok != "OVERLAY" && |
| 915 | tok != "TYPE") |
George Rimar | a46d08e | 2018-08-28 08:39:21 | [diff] [blame] | 916 | return false; |
| 917 | |
George Rimar | a46d08e | 2018-08-28 08:39:21 | [diff] [blame] | 918 | if (consume("NOLOAD")) { |
Matt Schulte | fdc41aa | 2020-03-28 16:54:06 | [diff] [blame] | 919 | cmd->type = SHT_NOBITS; |
Fangrui Song | 66f8ac8 | 2022-02-17 20:10:58 | [diff] [blame] | 920 | cmd->typeIsSet = true; |
| 921 | } else if (consume("TYPE")) { |
| 922 | expect("="); |
| 923 | StringRef value = peek(); |
| 924 | auto it = llvm::find_if(typeMap, [=](auto e) { return e.first == value; }); |
| 925 | if (it != std::end(typeMap)) { |
| 926 | // The value is a recognized literal SHT_*. |
| 927 | cmd->type = it->second; |
| 928 | skip(); |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 929 | } else if (value.starts_with("SHT_")) { |
Fangrui Song | 66f8ac8 | 2022-02-17 20:10:58 | [diff] [blame] | 930 | setError("unknown section type " + value); |
| 931 | } else { |
| 932 | // Otherwise, read an expression. |
| 933 | cmd->type = readExpr()().getValue(); |
| 934 | } |
| 935 | cmd->typeIsSet = true; |
George Rimar | a46d08e | 2018-08-28 08:39:21 | [diff] [blame] | 936 | } else { |
| 937 | skip(); // This is "COPY", "INFO" or "OVERLAY". |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 938 | cmd->nonAlloc = true; |
George Rimar | a46d08e | 2018-08-28 08:39:21 | [diff] [blame] | 939 | } |
| 940 | expect(")"); |
| 941 | return true; |
| 942 | } |
| 943 | |
George Rimar | 1c08e9f | 2018-02-16 10:42:58 | [diff] [blame] | 944 | // Reads an expression and/or the special directive for an output |
| 945 | // section definition. Directive is one of following: "(NOLOAD)", |
| 946 | // "(COPY)", "(INFO)" or "(OVERLAY)". |
Rui Ueyama | 3271d37 | 2017-06-08 19:47:16 | [diff] [blame] | 947 | // |
| 948 | // An output section name can be followed by an address expression |
George Rimar | 1c08e9f | 2018-02-16 10:42:58 | [diff] [blame] | 949 | // and/or directive. This grammar is not LL(1) because "(" can be |
George Rimar | 97f4d15 | 2018-02-16 10:46:50 | [diff] [blame] | 950 | // interpreted as either the beginning of some expression or beginning |
George Rimar | 1c08e9f | 2018-02-16 10:42:58 | [diff] [blame] | 951 | // of directive. |
Rui Ueyama | 3271d37 | 2017-06-08 19:47:16 | [diff] [blame] | 952 | // |
| 953 | // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html |
| 954 | // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 955 | void ScriptParser::readSectionAddressType(OutputSection *cmd) { |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 956 | if (consume("(")) { |
Fangrui Song | 28045ce | 2024-07-20 21:13:02 | [diff] [blame] | 957 | // Temporarily set inExpr to support TYPE=<value> without spaces. |
| 958 | SaveAndRestore saved(inExpr, true); |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 959 | if (readSectionDirective(cmd, peek())) |
Fangrui Song | 28045ce | 2024-07-20 21:13:02 | [diff] [blame] | 960 | return; |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 961 | cmd->addrExpr = readExpr(); |
| 962 | expect(")"); |
| 963 | } else { |
| 964 | cmd->addrExpr = readExpr(); |
Fangrui Song | 28045ce | 2024-07-20 21:13:02 | [diff] [blame] | 965 | } |
Fangrui Song | 28045ce | 2024-07-20 21:13:02 | [diff] [blame] | 966 | |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 967 | if (consume("(")) { |
Fangrui Song | 28045ce | 2024-07-20 21:13:02 | [diff] [blame] | 968 | SaveAndRestore saved(inExpr, true); |
Hongyu Chen | b828c13 | 2024-07-20 23:35:38 | [diff] [blame] | 969 | StringRef tok = peek(); |
| 970 | if (!readSectionDirective(cmd, tok)) |
| 971 | setError("unknown section directive: " + tok); |
Fangrui Song | 28045ce | 2024-07-20 21:13:02 | [diff] [blame] | 972 | } |
Rui Ueyama | 3271d37 | 2017-06-08 19:47:16 | [diff] [blame] | 973 | } |
| 974 | |
Fangrui Song | e24457a | 2024-11-15 06:17:10 | [diff] [blame] | 975 | static Expr checkAlignment(Ctx &ctx, Expr e, std::string &loc) { |
| 976 | return [=, &ctx] { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 977 | uint64_t alignment = std::max((uint64_t)1, e().getValue()); |
| 978 | if (!isPowerOf2_64(alignment)) { |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 979 | ErrAlways(ctx) << loc << ": alignment must be power of 2"; |
George Rimar | f22ec9d | 2017-10-25 14:50:51 | [diff] [blame] | 980 | return (uint64_t)1; // Return a dummy value. |
| 981 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 982 | return alignment; |
George Rimar | f22ec9d | 2017-10-25 14:50:51 | [diff] [blame] | 983 | }; |
| 984 | } |
| 985 | |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 986 | OutputDesc *ScriptParser::readOverlaySectionDescription() { |
Fangrui Song | e689515 | 2024-07-27 23:33:18 | [diff] [blame] | 987 | OutputDesc *osd = |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 988 | ctx.script->createOutputSection(readName(), getCurrentLocation()); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 989 | osd->osec.inOverlay = true; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 990 | expect("{"); |
Hongyu Chen | f1a7d14 | 2024-07-27 21:16:12 | [diff] [blame] | 991 | while (auto tok = till("}")) { |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 992 | uint64_t withFlags = 0; |
| 993 | uint64_t withoutFlags = 0; |
Hongyu Chen | f1a7d14 | 2024-07-27 21:16:12 | [diff] [blame] | 994 | if (tok == "INPUT_SECTION_FLAGS") { |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 995 | std::tie(withFlags, withoutFlags) = readInputSectionFlags(); |
Hongyu Chen | f1a7d14 | 2024-07-27 21:16:12 | [diff] [blame] | 996 | tok = till(""); |
| 997 | } |
Daniel Thornburgh | 7e8a902 | 2024-08-05 20:06:45 | [diff] [blame] | 998 | if (tok == "CLASS") |
| 999 | osd->osec.commands.push_back(make<InputSectionDescription>( |
| 1000 | StringRef{}, withFlags, withoutFlags, readSectionClassName())); |
| 1001 | else |
| 1002 | osd->osec.commands.push_back( |
| 1003 | readInputSectionRules(tok, withFlags, withoutFlags)); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1004 | } |
Peter Smith | e16af8a | 2023-05-11 11:00:19 | [diff] [blame] | 1005 | osd->osec.phdrs = readOutputSectionPhdrs(); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1006 | return osd; |
George Rimar | a582419 | 2018-06-27 08:08:12 | [diff] [blame] | 1007 | } |
| 1008 | |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1009 | OutputDesc *ScriptParser::readOutputSectionDescription(StringRef outSec) { |
Fangrui Song | d60ef93 | 2023-02-03 19:03:00 | [diff] [blame] | 1010 | OutputDesc *cmd = |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1011 | ctx.script->createOutputSection(unquote(outSec), getCurrentLocation()); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1012 | OutputSection *osec = &cmd->osec; |
Fangrui Song | 5a44980 | 2022-05-04 08:10:45 | [diff] [blame] | 1013 | // Maybe relro. Will reset to false if DATA_SEGMENT_RELRO_END is absent. |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1014 | osec->relro = ctx.script->seenDataAlign && !ctx.script->seenRelroEnd; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1015 | |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1016 | size_t symbolsReferenced = ctx.script->referencedSymbols.size(); |
George Rimar | c4df670 | 2018-03-01 12:27:04 | [diff] [blame] | 1017 | |
Rui Ueyama | 3271d37 | 2017-06-08 19:47:16 | [diff] [blame] | 1018 | if (peek() != ":") |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1019 | readSectionAddressType(osec); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1020 | expect(":"); |
| 1021 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1022 | std::string location = getCurrentLocation(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1023 | if (consume("AT")) |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1024 | osec->lmaExpr = readParenExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1025 | if (consume("ALIGN")) |
Fangrui Song | e24457a | 2024-11-15 06:17:10 | [diff] [blame] | 1026 | osec->alignExpr = checkAlignment(ctx, readParenExpr(), location); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1027 | if (consume("SUBALIGN")) |
Fangrui Song | e24457a | 2024-11-15 06:17:10 | [diff] [blame] | 1028 | osec->subalignExpr = checkAlignment(ctx, readParenExpr(), location); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1029 | |
| 1030 | // Parse constraints. |
| 1031 | if (consume("ONLY_IF_RO")) |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1032 | osec->constraint = ConstraintKind::ReadOnly; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1033 | if (consume("ONLY_IF_RW")) |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1034 | osec->constraint = ConstraintKind::ReadWrite; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1035 | expect("{"); |
| 1036 | |
Fangrui Song | 2a89356 | 2024-07-27 00:13:37 | [diff] [blame] | 1037 | while (auto tok = till("}")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1038 | if (tok == ";") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1039 | // Empty commands are allowed. Do nothing here. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1040 | } else if (SymbolAssignment *assign = readAssignment(tok)) { |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1041 | osec->commands.push_back(assign); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1042 | } else if (ByteCommand *data = readByteCommand(tok)) { |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1043 | osec->commands.push_back(data); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1044 | } else if (tok == "CONSTRUCTORS") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1045 | // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors |
| 1046 | // by name. This is for very old file formats such as ECOFF/XCOFF. |
| 1047 | // For ELF, we should ignore. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1048 | } else if (tok == "FILL") { |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1049 | // We handle the FILL command as an alias for =fillexp section attribute, |
| 1050 | // which is different from what GNU linkers do. |
| 1051 | // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html |
Georgii Rymar | bb7d2b1 | 2020-02-16 11:17:26 | [diff] [blame] | 1052 | if (peek() != "(") |
| 1053 | setError("( expected, but got " + peek()); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1054 | osec->filler = readFill(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1055 | } else if (tok == "SORT") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1056 | readSort(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1057 | } else if (tok == "INCLUDE") { |
Rui Ueyama | 2e9d40d | 2018-10-12 17:07:32 | [diff] [blame] | 1058 | readInclude(); |
Fangrui Song | 177fd72 | 2022-05-13 18:06:01 | [diff] [blame] | 1059 | } else if (tok == "(" || tok == ")") { |
| 1060 | setError("expected filename pattern"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1061 | } else if (peek() == "(") { |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1062 | osec->commands.push_back(readInputSectionDescription(tok)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1063 | } else { |
George Rimar | f49fe21 | 2018-12-06 08:34:52 | [diff] [blame] | 1064 | // We have a file name and no input sections description. It is not a |
| 1065 | // commonly used syntax, but still acceptable. In that case, all sections |
| 1066 | // from the file will be included. |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1067 | // FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not |
| 1068 | // handle this case here as it will already have been matched by the |
| 1069 | // case above. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1070 | auto *isd = make<InputSectionDescription>(tok); |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 | [diff] [blame] | 1071 | isd->sectionPatterns.push_back({{}, StringMatcher("*")}); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1072 | osec->commands.push_back(isd); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | if (consume(">")) |
Fangrui Song | 0d8bc10 | 2024-07-27 23:39:14 | [diff] [blame] | 1077 | osec->memoryRegionName = std::string(readName()); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1078 | |
George Rimar | 5d01a8b | 2018-01-12 09:07:35 | [diff] [blame] | 1079 | if (consume("AT")) { |
| 1080 | expect(">"); |
Fangrui Song | 0d8bc10 | 2024-07-27 23:39:14 | [diff] [blame] | 1081 | osec->lmaRegionName = std::string(readName()); |
George Rimar | 5d01a8b | 2018-01-12 09:07:35 | [diff] [blame] | 1082 | } |
| 1083 | |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1084 | if (osec->lmaExpr && !osec->lmaRegionName.empty()) |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1085 | ErrAlways(ctx) << "section can't have both LMA and a load region"; |
George Rimar | 5d01a8b | 2018-01-12 09:07:35 | [diff] [blame] | 1086 | |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1087 | osec->phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1088 | |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 1089 | if (peek() == "=" || peek().starts_with("=")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1090 | inExpr = true; |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1091 | consume("="); |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1092 | osec->filler = readFill(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1093 | inExpr = false; |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1094 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1095 | |
| 1096 | // Consume optional comma following output section command. |
| 1097 | consume(","); |
| 1098 | |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1099 | if (ctx.script->referencedSymbols.size() > symbolsReferenced) |
Fangrui Song | 6c81493 | 2022-03-08 19:23:41 | [diff] [blame] | 1100 | osec->expressionsUseSymbols = true; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1101 | return cmd; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1102 | } |
| 1103 | |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1104 | // Reads a `=<fillexp>` expression and returns its value as a big-endian number. |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1105 | // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1106 | // We do not support using symbols in such expressions. |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1107 | // |
Rui Ueyama | 8acbf1c | 2017-04-13 23:40:19 | [diff] [blame] | 1108 | // When reading a hexstring, ld.bfd handles it as a blob of arbitrary |
| 1109 | // size, while ld.gold always handles it as a 32-bit big-endian number. |
| 1110 | // We are compatible with ld.gold because it's easier to implement. |
Georgii Rymar | bb7d2b1 | 2020-02-16 11:17:26 | [diff] [blame] | 1111 | // Also, we require that expressions with operators must be wrapped into |
| 1112 | // round brackets. We did it to resolve the ambiguity when parsing scripts like: |
| 1113 | // SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } } |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1114 | std::array<uint8_t, 4> ScriptParser::readFill() { |
Georgii Rymar | bb7d2b1 | 2020-02-16 11:17:26 | [diff] [blame] | 1115 | uint64_t value = readPrimary()().val; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1116 | if (value > UINT32_MAX) |
George Rimar | 0810f16 | 2019-07-04 14:17:31 | [diff] [blame] | 1117 | setError("filler expression result does not fit 32-bit: 0x" + |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1118 | Twine::utohexstr(value)); |
Rui Ueyama | b58079d4 | 2017-04-11 22:45:57 | [diff] [blame] | 1119 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1120 | std::array<uint8_t, 4> buf; |
| 1121 | write32be(buf.data(), (uint32_t)value); |
| 1122 | return buf; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1123 | } |
| 1124 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1125 | SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1126 | expect("("); |
Fangrui Song | 30ec2bf | 2024-07-27 23:19:57 | [diff] [blame] | 1127 | StringRef name = readName(), eq = peek(); |
Fangrui Song | 21bf6bb | 2022-06-26 03:26:47 | [diff] [blame] | 1128 | if (eq != "=") { |
| 1129 | setError("= expected, but got " + next()); |
Hongyu Chen | f1a7d14 | 2024-07-27 21:16:12 | [diff] [blame] | 1130 | while (till(")")) |
Fangrui Song | 21bf6bb | 2022-06-26 03:26:47 | [diff] [blame] | 1131 | ; |
| 1132 | return nullptr; |
| 1133 | } |
Parth Arora | ebb326a | 2024-03-25 23:11:21 | [diff] [blame] | 1134 | llvm::SaveAndRestore saveActiveProvideSym(activeProvideSym); |
| 1135 | if (provide) |
| 1136 | activeProvideSym = name; |
Fangrui Song | 21bf6bb | 2022-06-26 03:26:47 | [diff] [blame] | 1137 | SymbolAssignment *cmd = readSymbolAssignment(name); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1138 | cmd->provide = provide; |
| 1139 | cmd->hidden = hidden; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1140 | expect(")"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1141 | return cmd; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1142 | } |
| 1143 | |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 1144 | // Replace whitespace sequence (including \n) with one single space. The output |
| 1145 | // is used by -Map. |
| 1146 | static void squeezeSpaces(std::string &str) { |
| 1147 | char prev = '\0'; |
| 1148 | auto it = str.begin(); |
| 1149 | for (char c : str) |
| 1150 | if (!isSpace(c) || (c = ' ') != prev) |
| 1151 | *it++ = prev = c; |
| 1152 | str.erase(it, str.end()); |
| 1153 | } |
| 1154 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1155 | SymbolAssignment *ScriptParser::readAssignment(StringRef tok) { |
George Rimar | d30a78b | 2018-04-25 11:16:31 | [diff] [blame] | 1156 | // Assert expression returns Dot, so this is equal to ".=." |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1157 | if (tok == "ASSERT") |
Fangrui Song | 65a15a5 | 2023-09-09 21:46:51 | [diff] [blame] | 1158 | return make<SymbolAssignment>(".", readAssert(), 0, getCurrentLocation()); |
George Rimar | d30a78b | 2018-04-25 11:16:31 | [diff] [blame] | 1159 | |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 1160 | const char *oldS = prevTok.data(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1161 | SymbolAssignment *cmd = nullptr; |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1162 | bool savedSeenRelroEnd = ctx.script->seenRelroEnd; |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1163 | const StringRef op = peek(); |
Fangrui Song | edcc60e | 2024-07-27 23:04:38 | [diff] [blame] | 1164 | { |
Jan Svoboda | abf0c6c | 2022-12-02 21:36:04 | [diff] [blame] | 1165 | SaveAndRestore saved(inExpr, true); |
Fangrui Song | edcc60e | 2024-07-27 23:04:38 | [diff] [blame] | 1166 | if (op.starts_with("=")) { |
| 1167 | // Support = followed by an expression without whitespace. |
Fangrui Song | 30ec2bf | 2024-07-27 23:19:57 | [diff] [blame] | 1168 | cmd = readSymbolAssignment(unquote(tok)); |
Fangrui Song | edcc60e | 2024-07-27 23:04:38 | [diff] [blame] | 1169 | } else if ((op.size() == 2 && op[1] == '=' && strchr("+-*/&^|", op[0])) || |
| 1170 | op == "<<=" || op == ">>=") { |
Fangrui Song | 30ec2bf | 2024-07-27 23:19:57 | [diff] [blame] | 1171 | cmd = readSymbolAssignment(unquote(tok)); |
Fangrui Song | edcc60e | 2024-07-27 23:04:38 | [diff] [blame] | 1172 | } else if (tok == "PROVIDE") { |
| 1173 | cmd = readProvideHidden(true, false); |
| 1174 | } else if (tok == "HIDDEN") { |
| 1175 | cmd = readProvideHidden(false, true); |
| 1176 | } else if (tok == "PROVIDE_HIDDEN") { |
| 1177 | cmd = readProvideHidden(true, true); |
| 1178 | } |
Fangrui Song | fe0de25 | 2022-06-26 03:25:34 | [diff] [blame] | 1179 | } |
George Rimar | e88b76a | 2018-04-05 11:25:58 | [diff] [blame] | 1180 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1181 | if (cmd) { |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1182 | cmd->dataSegmentRelroEnd = !savedSeenRelroEnd && ctx.script->seenRelroEnd; |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 1183 | cmd->commandString = StringRef(oldS, curTok.data() - oldS).str(); |
| 1184 | squeezeSpaces(cmd->commandString); |
George Rimar | e88b76a | 2018-04-05 11:25:58 | [diff] [blame] | 1185 | expect(";"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1186 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1187 | return cmd; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1188 | } |
| 1189 | |
Fangrui Song | 30ec2bf | 2024-07-27 23:19:57 | [diff] [blame] | 1190 | StringRef ScriptParser::readName() { return unquote(next()); } |
| 1191 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1192 | SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1193 | StringRef op = next(); |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1194 | assert(op == "=" || op == "*=" || op == "/=" || op == "+=" || op == "-=" || |
Fangrui Song | fae9610 | 2023-07-15 21:10:40 | [diff] [blame] | 1195 | op == "&=" || op == "^=" || op == "|=" || op == "<<=" || op == ">>="); |
Fangrui Song | 1bd5df7 | 2023-09-16 00:52:48 | [diff] [blame] | 1196 | // Note: GNU ld does not support %=. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1197 | Expr e = readExpr(); |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1198 | if (op != "=") { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1199 | std::string loc = getCurrentLocation(); |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1200 | e = [=, s = ctx.script, c = op[0], &ctx = ctx]() -> ExprValue { |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1201 | ExprValue lhs = s->getSymbolValue(name, loc); |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1202 | switch (c) { |
| 1203 | case '*': |
| 1204 | return lhs.getValue() * e().getValue(); |
| 1205 | case '/': |
| 1206 | if (uint64_t rv = e().getValue()) |
| 1207 | return lhs.getValue() / rv; |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1208 | ErrAlways(ctx) << loc << ": division by zero"; |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1209 | return 0; |
| 1210 | case '+': |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1211 | return add(*s, lhs, e()); |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1212 | case '-': |
| 1213 | return sub(lhs, e()); |
| 1214 | case '<': |
Fangrui Song | daba24e | 2023-06-15 17:34:33 | [diff] [blame] | 1215 | return lhs.getValue() << e().getValue() % 64; |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1216 | case '>': |
Fangrui Song | daba24e | 2023-06-15 17:34:33 | [diff] [blame] | 1217 | return lhs.getValue() >> e().getValue() % 64; |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1218 | case '&': |
| 1219 | return lhs.getValue() & e().getValue(); |
Fangrui Song | fae9610 | 2023-07-15 21:10:40 | [diff] [blame] | 1220 | case '^': |
| 1221 | return lhs.getValue() ^ e().getValue(); |
Fangrui Song | 0a0effd | 2022-06-26 05:22:59 | [diff] [blame] | 1222 | case '|': |
| 1223 | return lhs.getValue() | e().getValue(); |
| 1224 | default: |
| 1225 | llvm_unreachable(""); |
| 1226 | } |
| 1227 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1228 | } |
Fangrui Song | 65a15a5 | 2023-09-09 21:46:51 | [diff] [blame] | 1229 | return make<SymbolAssignment>(name, e, ctx.scriptSymOrderCounter++, |
| 1230 | getCurrentLocation()); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | // This is an operator-precedence parser to parse a linker |
| 1234 | // script expression. |
| 1235 | Expr ScriptParser::readExpr() { |
| 1236 | // Our lexer is context-aware. Set the in-expression bit so that |
| 1237 | // they apply different tokenization rules. |
Fangrui Song | efa833d | 2024-07-20 21:36:55 | [diff] [blame] | 1238 | SaveAndRestore saved(inExpr, true); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1239 | Expr e = readExpr1(readPrimary(), 0); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1240 | return e; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1241 | } |
| 1242 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1243 | Expr ScriptParser::combine(StringRef op, Expr l, Expr r) { |
| 1244 | if (op == "+") |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1245 | return [=, s = ctx.script] { return add(*s, l(), r()); }; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1246 | if (op == "-") |
| 1247 | return [=] { return sub(l(), r()); }; |
| 1248 | if (op == "*") |
| 1249 | return [=] { return l().getValue() * r().getValue(); }; |
| 1250 | if (op == "/") { |
| 1251 | std::string loc = getCurrentLocation(); |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1252 | return [=, &ctx = ctx]() -> uint64_t { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1253 | if (uint64_t rv = r().getValue()) |
| 1254 | return l().getValue() / rv; |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1255 | ErrAlways(ctx) << loc << ": division by zero"; |
Rui Ueyama | 067617f | 2018-03-05 23:50:45 | [diff] [blame] | 1256 | return 0; |
George Rimar | 7b91e21 | 2018-03-05 10:02:44 | [diff] [blame] | 1257 | }; |
| 1258 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1259 | if (op == "%") { |
| 1260 | std::string loc = getCurrentLocation(); |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1261 | return [=, &ctx = ctx]() -> uint64_t { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1262 | if (uint64_t rv = r().getValue()) |
| 1263 | return l().getValue() % rv; |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1264 | ErrAlways(ctx) << loc << ": modulo by zero"; |
Rui Ueyama | 067617f | 2018-03-05 23:50:45 | [diff] [blame] | 1265 | return 0; |
George Rimar | 7b91e21 | 2018-03-05 10:02:44 | [diff] [blame] | 1266 | }; |
| 1267 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1268 | if (op == "<<") |
Fangrui Song | daba24e | 2023-06-15 17:34:33 | [diff] [blame] | 1269 | return [=] { return l().getValue() << r().getValue() % 64; }; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1270 | if (op == ">>") |
Fangrui Song | daba24e | 2023-06-15 17:34:33 | [diff] [blame] | 1271 | return [=] { return l().getValue() >> r().getValue() % 64; }; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1272 | if (op == "<") |
| 1273 | return [=] { return l().getValue() < r().getValue(); }; |
| 1274 | if (op == ">") |
| 1275 | return [=] { return l().getValue() > r().getValue(); }; |
| 1276 | if (op == ">=") |
| 1277 | return [=] { return l().getValue() >= r().getValue(); }; |
| 1278 | if (op == "<=") |
| 1279 | return [=] { return l().getValue() <= r().getValue(); }; |
| 1280 | if (op == "==") |
| 1281 | return [=] { return l().getValue() == r().getValue(); }; |
| 1282 | if (op == "!=") |
| 1283 | return [=] { return l().getValue() != r().getValue(); }; |
| 1284 | if (op == "||") |
| 1285 | return [=] { return l().getValue() || r().getValue(); }; |
| 1286 | if (op == "&&") |
| 1287 | return [=] { return l().getValue() && r().getValue(); }; |
| 1288 | if (op == "&") |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1289 | return [=, s = ctx.script] { return bitAnd(*s, l(), r()); }; |
Fangrui Song | fae9610 | 2023-07-15 21:10:40 | [diff] [blame] | 1290 | if (op == "^") |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1291 | return [=, s = ctx.script] { return bitXor(*s, l(), r()); }; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1292 | if (op == "|") |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1293 | return [=, s = ctx.script] { return bitOr(*s, l(), r()); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1294 | llvm_unreachable("invalid operator"); |
| 1295 | } |
| 1296 | |
| 1297 | // This is a part of the operator-precedence parser. This function |
| 1298 | // assumes that the remaining token stream starts with an operator. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1299 | Expr ScriptParser::readExpr1(Expr lhs, int minPrec) { |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 1300 | while (!atEOF() && !errCount(ctx)) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1301 | // Read an operator and an expression. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1302 | StringRef op1 = peek(); |
| 1303 | if (precedence(op1) < minPrec) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1304 | break; |
| 1305 | skip(); |
Fangrui Song | efa833d | 2024-07-20 21:36:55 | [diff] [blame] | 1306 | if (op1 == "?") |
| 1307 | return readTernary(lhs); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1308 | Expr rhs = readPrimary(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1309 | |
| 1310 | // Evaluate the remaining part of the expression first if the |
| 1311 | // next operator has greater precedence than the previous one. |
| 1312 | // For example, if we have read "+" and "3", and if the next |
| 1313 | // operator is "*", then we'll evaluate 3 * ... part first. |
| 1314 | while (!atEOF()) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1315 | StringRef op2 = peek(); |
| 1316 | if (precedence(op2) <= precedence(op1)) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1317 | break; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1318 | rhs = readExpr1(rhs, precedence(op2)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1319 | } |
| 1320 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1321 | lhs = combine(op1, lhs, rhs); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1322 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1323 | return lhs; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1324 | } |
| 1325 | |
George Rimar | 5fb1712 | 2017-08-03 16:05:08 | [diff] [blame] | 1326 | Expr ScriptParser::getPageSize() { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1327 | std::string location = getCurrentLocation(); |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1328 | return [=, &ctx = this->ctx]() -> uint64_t { |
Fangrui Song | b4feb26 | 2024-08-22 06:53:36 | [diff] [blame] | 1329 | if (ctx.target) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1330 | return ctx.arg.commonPageSize; |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 1331 | ErrAlways(ctx) << location << ": unable to calculate page size"; |
George Rimar | 5fb1712 | 2017-08-03 16:05:08 | [diff] [blame] | 1332 | return 4096; // Return a dummy value. |
| 1333 | }; |
| 1334 | } |
| 1335 | |
| 1336 | Expr ScriptParser::readConstant() { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1337 | StringRef s = readParenName(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1338 | if (s == "COMMONPAGESIZE") |
George Rimar | 5fb1712 | 2017-08-03 16:05:08 | [diff] [blame] | 1339 | return getPageSize(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1340 | if (s == "MAXPAGESIZE") |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1341 | return [&ctx = this->ctx] { return ctx.arg.maxPageSize; }; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1342 | setError("unknown constant: " + s); |
George Rimar | b068b03 | 2018-03-01 12:36:01 | [diff] [blame] | 1343 | return [] { return 0; }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1344 | } |
| 1345 | |
Rui Ueyama | 5c65088 | 2017-04-05 23:22:11 | [diff] [blame] | 1346 | // Parses Tok as an integer. It recognizes hexadecimal (prefixed with |
| 1347 | // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may |
| 1348 | // have "K" (Ki) or "M" (Mi) suffixes. |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1349 | static std::optional<uint64_t> parseInt(StringRef tok) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1350 | // Hexadecimal |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1351 | uint64_t val; |
Kazu Hirata | ed1539c | 2023-05-16 17:12:42 | [diff] [blame] | 1352 | if (tok.starts_with_insensitive("0x")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1353 | if (!to_integer(tok.substr(2), val, 16)) |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1354 | return std::nullopt; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1355 | return val; |
Rui Ueyama | 4092016 | 2017-10-11 19:30:39 | [diff] [blame] | 1356 | } |
Kazu Hirata | ed1539c | 2023-05-16 17:12:42 | [diff] [blame] | 1357 | if (tok.ends_with_insensitive("H")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1358 | if (!to_integer(tok.drop_back(), val, 16)) |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1359 | return std::nullopt; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1360 | return val; |
Rui Ueyama | 4092016 | 2017-10-11 19:30:39 | [diff] [blame] | 1361 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1362 | |
| 1363 | // Decimal |
Kazu Hirata | ed1539c | 2023-05-16 17:12:42 | [diff] [blame] | 1364 | if (tok.ends_with_insensitive("K")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1365 | if (!to_integer(tok.drop_back(), val, 10)) |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1366 | return std::nullopt; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1367 | return val * 1024; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1368 | } |
Kazu Hirata | ed1539c | 2023-05-16 17:12:42 | [diff] [blame] | 1369 | if (tok.ends_with_insensitive("M")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1370 | if (!to_integer(tok.drop_back(), val, 10)) |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1371 | return std::nullopt; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1372 | return val * 1024 * 1024; |
Rui Ueyama | 5c65088 | 2017-04-05 23:22:11 | [diff] [blame] | 1373 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1374 | if (!to_integer(tok, val, 10)) |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1375 | return std::nullopt; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1376 | return val; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1377 | } |
| 1378 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1379 | ByteCommand *ScriptParser::readByteCommand(StringRef tok) { |
| 1380 | int size = StringSwitch<int>(tok) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1381 | .Case("BYTE", 1) |
| 1382 | .Case("SHORT", 2) |
| 1383 | .Case("LONG", 4) |
| 1384 | .Case("QUAD", 8) |
| 1385 | .Default(-1); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1386 | if (size == -1) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1387 | return nullptr; |
George Rimar | 84bcabc | 2018-03-15 09:16:40 | [diff] [blame] | 1388 | |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 1389 | const char *oldS = prevTok.data(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1390 | Expr e = readParenExpr(); |
Fangrui Song | 1978c21 | 2024-07-26 21:26:38 | [diff] [blame] | 1391 | std::string commandString = StringRef(oldS, curBuf.s.data() - oldS).str(); |
| 1392 | squeezeSpaces(commandString); |
| 1393 | return make<ByteCommand>(e, size, std::move(commandString)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1394 | } |
| 1395 | |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1396 | static std::optional<uint64_t> parseFlag(StringRef tok) { |
| 1397 | if (std::optional<uint64_t> asInt = parseInt(tok)) |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1398 | return asInt; |
| 1399 | #define CASE_ENT(enum) #enum, ELF::enum |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1400 | return StringSwitch<std::optional<uint64_t>>(tok) |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1401 | .Case(CASE_ENT(SHF_WRITE)) |
| 1402 | .Case(CASE_ENT(SHF_ALLOC)) |
| 1403 | .Case(CASE_ENT(SHF_EXECINSTR)) |
| 1404 | .Case(CASE_ENT(SHF_MERGE)) |
| 1405 | .Case(CASE_ENT(SHF_STRINGS)) |
| 1406 | .Case(CASE_ENT(SHF_INFO_LINK)) |
| 1407 | .Case(CASE_ENT(SHF_LINK_ORDER)) |
| 1408 | .Case(CASE_ENT(SHF_OS_NONCONFORMING)) |
| 1409 | .Case(CASE_ENT(SHF_GROUP)) |
| 1410 | .Case(CASE_ENT(SHF_TLS)) |
| 1411 | .Case(CASE_ENT(SHF_COMPRESSED)) |
| 1412 | .Case(CASE_ENT(SHF_EXCLUDE)) |
| 1413 | .Case(CASE_ENT(SHF_ARM_PURECODE)) |
Kazu Hirata | c68af42 | 2022-12-03 07:12:36 | [diff] [blame] | 1414 | .Default(std::nullopt); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1415 | #undef CASE_ENT |
| 1416 | } |
| 1417 | |
| 1418 | // Reads the '(' <flags> ')' list of section flags in |
| 1419 | // INPUT_SECTION_FLAGS '(' <flags> ')' in the |
| 1420 | // following form: |
| 1421 | // <flags> ::= <flag> |
| 1422 | // | <flags> & flag |
| 1423 | // <flag> ::= Recognized Flag Name, or Integer value of flag. |
| 1424 | // If the first character of <flag> is a ! then this means without flag, |
| 1425 | // otherwise with flag. |
| 1426 | // Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and |
| 1427 | // without flag SHF_WRITE. |
| 1428 | std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() { |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 1429 | uint64_t withFlags = 0; |
| 1430 | uint64_t withoutFlags = 0; |
| 1431 | expect("("); |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 1432 | while (!errCount(ctx)) { |
Fangrui Song | c89566f | 2024-07-27 23:27:05 | [diff] [blame] | 1433 | StringRef tok = readName(); |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1434 | bool without = tok.consume_front("!"); |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1435 | if (std::optional<uint64_t> flag = parseFlag(tok)) { |
Peter Smith | dbd0ad3 | 2020-01-15 09:38:00 | [diff] [blame] | 1436 | if (without) |
| 1437 | withoutFlags |= *flag; |
| 1438 | else |
| 1439 | withFlags |= *flag; |
| 1440 | } else { |
| 1441 | setError("unrecognised flag: " + tok); |
| 1442 | } |
| 1443 | if (consume(")")) |
| 1444 | break; |
| 1445 | if (!consume("&")) { |
| 1446 | next(); |
| 1447 | setError("expected & or )"); |
| 1448 | } |
| 1449 | } |
| 1450 | return std::make_pair(withFlags, withoutFlags); |
| 1451 | } |
| 1452 | |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1453 | StringRef ScriptParser::readParenName() { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1454 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1455 | bool orig = inExpr; |
| 1456 | inExpr = false; |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1457 | StringRef tok = readName(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1458 | inExpr = orig; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1459 | expect(")"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1460 | return tok; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1461 | } |
| 1462 | |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1463 | static void checkIfExists(LinkerScript &script, const OutputSection &osec, |
| 1464 | StringRef location) { |
| 1465 | if (osec.location.empty() && script.errorOnMissingSection) |
| 1466 | script.recordError(location + ": undefined section " + osec.name); |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 | [diff] [blame] | 1467 | } |
| 1468 | |
Fangrui Song | e4f385d | 2021-03-11 17:34:36 | [diff] [blame] | 1469 | static bool isValidSymbolName(StringRef s) { |
| 1470 | auto valid = [](char c) { |
| 1471 | return isAlnum(c) || c == '$' || c == '.' || c == '_'; |
| 1472 | }; |
| 1473 | return !s.empty() && !isDigit(s[0]) && llvm::all_of(s, valid); |
| 1474 | } |
| 1475 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1476 | Expr ScriptParser::readPrimary() { |
| 1477 | if (peek() == "(") |
| 1478 | return readParenExpr(); |
| 1479 | |
Rui Ueyama | 5c65088 | 2017-04-05 23:22:11 | [diff] [blame] | 1480 | if (consume("~")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1481 | Expr e = readPrimary(); |
| 1482 | return [=] { return ~e().getValue(); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1483 | } |
Hafiz Abid Qadeer | 6f1d954 | 2017-08-10 15:25:47 | [diff] [blame] | 1484 | if (consume("!")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1485 | Expr e = readPrimary(); |
| 1486 | return [=] { return !e().getValue(); }; |
Hafiz Abid Qadeer | 6f1d954 | 2017-08-10 15:25:47 | [diff] [blame] | 1487 | } |
Rui Ueyama | 5c65088 | 2017-04-05 23:22:11 | [diff] [blame] | 1488 | if (consume("-")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1489 | Expr e = readPrimary(); |
| 1490 | return [=] { return -e().getValue(); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1491 | } |
| 1492 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1493 | StringRef tok = next(); |
| 1494 | std::string location = getCurrentLocation(); |
Rui Ueyama | 5c65088 | 2017-04-05 23:22:11 | [diff] [blame] | 1495 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1496 | // Built-in functions are parsed here. |
| 1497 | // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1498 | if (tok == "ABSOLUTE") { |
| 1499 | Expr inner = readParenExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1500 | return [=] { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1501 | ExprValue i = inner(); |
| 1502 | i.forceAbsolute = true; |
| 1503 | return i; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1504 | }; |
| 1505 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1506 | if (tok == "ADDR") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1507 | StringRef name = readParenName(); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1508 | OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec; |
Fangrui Song | 9e9c86f | 2022-02-28 19:19:00 | [diff] [blame] | 1509 | osec->usedInExpression = true; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1510 | return [=, s = ctx.script]() -> ExprValue { |
| 1511 | checkIfExists(*s, *osec, location); |
Fangrui Song | 9e9c86f | 2022-02-28 19:19:00 | [diff] [blame] | 1512 | return {osec, false, 0, location}; |
George Rimar | 41c7ab4 | 2017-06-07 08:54:43 | [diff] [blame] | 1513 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1514 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1515 | if (tok == "ALIGN") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1516 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1517 | Expr e = readExpr(); |
George Rimar | f22ec9d | 2017-10-25 14:50:51 | [diff] [blame] | 1518 | if (consume(")")) { |
Fangrui Song | e24457a | 2024-11-15 06:17:10 | [diff] [blame] | 1519 | e = checkAlignment(ctx, e, location); |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1520 | return [=, s = ctx.script] { |
| 1521 | return alignToPowerOf2(s->getDot(), e().getValue()); |
| 1522 | }; |
George Rimar | f22ec9d | 2017-10-25 14:50:51 | [diff] [blame] | 1523 | } |
Rui Ueyama | b579c43 | 2017-04-05 05:40:21 | [diff] [blame] | 1524 | expect(","); |
Fangrui Song | e24457a | 2024-11-15 06:17:10 | [diff] [blame] | 1525 | Expr e2 = checkAlignment(ctx, readExpr(), location); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1526 | expect(")"); |
Petr Hosek | 3c6de1a | 2017-05-30 03:18:28 | [diff] [blame] | 1527 | return [=] { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1528 | ExprValue v = e(); |
| 1529 | v.alignment = e2().getValue(); |
| 1530 | return v; |
Petr Hosek | 3c6de1a | 2017-05-30 03:18:28 | [diff] [blame] | 1531 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1532 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1533 | if (tok == "ALIGNOF") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1534 | StringRef name = readParenName(); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1535 | OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1536 | return [=, s = ctx.script] { |
| 1537 | checkIfExists(*s, *osec, location); |
Guillaume Chatelet | 08e2a76 | 2022-12-01 16:19:56 | [diff] [blame] | 1538 | return osec->addralign; |
Rui Ueyama | 617e2f9 | 2017-10-08 02:27:02 | [diff] [blame] | 1539 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1540 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1541 | if (tok == "ASSERT") |
George Rimar | d30a78b | 2018-04-25 11:16:31 | [diff] [blame] | 1542 | return readAssert(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1543 | if (tok == "CONSTANT") |
George Rimar | 5fb1712 | 2017-08-03 16:05:08 | [diff] [blame] | 1544 | return readConstant(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1545 | if (tok == "DATA_SEGMENT_ALIGN") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1546 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1547 | Expr e = readExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1548 | expect(","); |
| 1549 | readExpr(); |
| 1550 | expect(")"); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1551 | ctx.script->seenDataAlign = true; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1552 | return [=, s = ctx.script] { |
Fangrui Song | 85cfd91 | 2022-07-24 18:20:49 | [diff] [blame] | 1553 | uint64_t align = std::max(uint64_t(1), e().getValue()); |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1554 | return (s->getDot() + align - 1) & -align; |
George Rimar | 60833f6 | 2017-07-28 09:27:49 | [diff] [blame] | 1555 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1556 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1557 | if (tok == "DATA_SEGMENT_END") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1558 | expect("("); |
| 1559 | expect("."); |
| 1560 | expect(")"); |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1561 | return [s = ctx.script] { return s->getDot(); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1562 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1563 | if (tok == "DATA_SEGMENT_RELRO_END") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1564 | // GNU linkers implements more complicated logic to handle |
| 1565 | // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and |
| 1566 | // just align to the next page boundary for simplicity. |
| 1567 | expect("("); |
| 1568 | readExpr(); |
| 1569 | expect(","); |
| 1570 | readExpr(); |
| 1571 | expect(")"); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1572 | ctx.script->seenRelroEnd = true; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1573 | return [&ctx = this->ctx] { |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1574 | return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1575 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1576 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1577 | if (tok == "DEFINED") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1578 | StringRef name = readParenName(); |
Fangrui Song | 65a15a5 | 2023-09-09 21:46:51 | [diff] [blame] | 1579 | // Return 1 if s is defined. If the definition is only found in a linker |
| 1580 | // script, it must happen before this DEFINED. |
| 1581 | auto order = ctx.scriptSymOrderCounter++; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1582 | return [=, &ctx = this->ctx] { |
Fangrui Song | df0864e | 2024-09-23 17:33:43 | [diff] [blame] | 1583 | Symbol *s = ctx.symtab->find(name); |
Fangrui Song | 65a15a5 | 2023-09-09 21:46:51 | [diff] [blame] | 1584 | return s && s->isDefined() && ctx.scriptSymOrder.lookup(s) < order ? 1 |
| 1585 | : 0; |
Hafiz Abid Qadeer | 1f166ed | 2020-07-16 20:40:31 | [diff] [blame] | 1586 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1587 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1588 | if (tok == "LENGTH") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1589 | StringRef name = readParenName(); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1590 | if (ctx.script->memoryRegions.count(name) == 0) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1591 | setError("memory region not defined: " + name); |
George Rimar | b068b03 | 2018-03-01 12:36:01 | [diff] [blame] | 1592 | return [] { return 0; }; |
| 1593 | } |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1594 | return ctx.script->memoryRegions[name]->length; |
Rui Ueyama | 91b95b6 | 2017-05-09 18:24:38 | [diff] [blame] | 1595 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1596 | if (tok == "LOADADDR") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1597 | StringRef name = readParenName(); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1598 | OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec; |
Fangrui Song | 9e9c86f | 2022-02-28 19:19:00 | [diff] [blame] | 1599 | osec->usedInExpression = true; |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1600 | return [=, s = ctx.script] { |
| 1601 | checkIfExists(*s, *osec, location); |
Fangrui Song | 9e9c86f | 2022-02-28 19:19:00 | [diff] [blame] | 1602 | return osec->getLMA(); |
Rui Ueyama | 617e2f9 | 2017-10-08 02:27:02 | [diff] [blame] | 1603 | }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1604 | } |
Isaac Richter | fa1145a | 2020-07-27 08:49:24 | [diff] [blame] | 1605 | if (tok == "LOG2CEIL") { |
| 1606 | expect("("); |
| 1607 | Expr a = readExpr(); |
| 1608 | expect(")"); |
| 1609 | return [=] { |
| 1610 | // LOG2CEIL(0) is defined to be 0. |
| 1611 | return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1))); |
| 1612 | }; |
| 1613 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1614 | if (tok == "MAX" || tok == "MIN") { |
George Rimar | fd11560 | 2018-03-28 11:33:00 | [diff] [blame] | 1615 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1616 | Expr a = readExpr(); |
George Rimar | fd11560 | 2018-03-28 11:33:00 | [diff] [blame] | 1617 | expect(","); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1618 | Expr b = readExpr(); |
George Rimar | fd11560 | 2018-03-28 11:33:00 | [diff] [blame] | 1619 | expect(")"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1620 | if (tok == "MIN") |
| 1621 | return [=] { return std::min(a().getValue(), b().getValue()); }; |
| 1622 | return [=] { return std::max(a().getValue(), b().getValue()); }; |
George Rimar | fd11560 | 2018-03-28 11:33:00 | [diff] [blame] | 1623 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1624 | if (tok == "ORIGIN") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1625 | StringRef name = readParenName(); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1626 | if (ctx.script->memoryRegions.count(name) == 0) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1627 | setError("memory region not defined: " + name); |
George Rimar | b068b03 | 2018-03-01 12:36:01 | [diff] [blame] | 1628 | return [] { return 0; }; |
| 1629 | } |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1630 | return ctx.script->memoryRegions[name]->origin; |
Rui Ueyama | 91b95b6 | 2017-05-09 18:24:38 | [diff] [blame] | 1631 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1632 | if (tok == "SEGMENT_START") { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1633 | expect("("); |
| 1634 | skip(); |
| 1635 | expect(","); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1636 | Expr e = readExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1637 | expect(")"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1638 | return [=] { return e(); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1639 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1640 | if (tok == "SIZEOF") { |
Fangrui Song | 74f843d | 2024-07-27 23:47:17 | [diff] [blame] | 1641 | StringRef name = readParenName(); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1642 | OutputSection *cmd = &ctx.script->getOrCreateOutputSection(name)->osec; |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 | [diff] [blame] | 1643 | // Linker script does not create an output section if its content is empty. |
| 1644 | // We want to allow SIZEOF(.foo) where .foo is a section which happened to |
| 1645 | // be empty. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1646 | return [=] { return cmd->size; }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1647 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1648 | if (tok == "SIZEOF_HEADERS") |
Fangrui Song | 3590068 | 2024-10-04 03:06:58 | [diff] [blame] | 1649 | return [=, &ctx = ctx] { return elf::getHeaderSize(ctx); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1650 | |
Rui Ueyama | 4eb2ecc | 2017-04-05 18:02:30 | [diff] [blame] | 1651 | // Tok is the dot. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1652 | if (tok == ".") |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1653 | return [=, s = ctx.script] { return s->getSymbolValue(tok, location); }; |
Rui Ueyama | 4eb2ecc | 2017-04-05 18:02:30 | [diff] [blame] | 1654 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1655 | // Tok is a literal number. |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1656 | if (std::optional<uint64_t> val = parseInt(tok)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1657 | return [=] { return *val; }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1658 | |
| 1659 | // Tok is a symbol name. |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 1660 | if (tok.starts_with("\"")) |
Fangrui Song | 2bf06d9 | 2021-09-27 16:50:41 | [diff] [blame] | 1661 | tok = unquote(tok); |
| 1662 | else if (!isValidSymbolName(tok)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1663 | setError("malformed number: " + tok); |
Parth Arora | ebb326a | 2024-03-25 23:11:21 | [diff] [blame] | 1664 | if (activeProvideSym) |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1665 | ctx.script->provideMap[*activeProvideSym].push_back(tok); |
Parth Arora | ebb326a | 2024-03-25 23:11:21 | [diff] [blame] | 1666 | else |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1667 | ctx.script->referencedSymbols.push_back(tok); |
Fangrui Song | 3320400 | 2024-09-21 18:51:02 | [diff] [blame] | 1668 | return [=, s = ctx.script] { return s->getSymbolValue(tok, location); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1669 | } |
| 1670 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1671 | Expr ScriptParser::readTernary(Expr cond) { |
| 1672 | Expr l = readExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1673 | expect(":"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1674 | Expr r = readExpr(); |
| 1675 | return [=] { return cond().getValue() ? l() : r(); }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | Expr ScriptParser::readParenExpr() { |
| 1679 | expect("("); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1680 | Expr e = readExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1681 | expect(")"); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1682 | return e; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1683 | } |
| 1684 | |
Fangrui Song | a1c2ee0 | 2021-12-26 21:53:47 | [diff] [blame] | 1685 | SmallVector<StringRef, 0> ScriptParser::readOutputSectionPhdrs() { |
| 1686 | SmallVector<StringRef, 0> phdrs; |
Fangrui Song | ed6c106 | 2024-11-07 17:06:01 | [diff] [blame] | 1687 | while (!errCount(ctx) && peek().starts_with(":")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1688 | StringRef tok = next(); |
Fangrui Song | a4921f1 | 2024-07-28 00:40:51 | [diff] [blame] | 1689 | phdrs.push_back((tok.size() == 1) ? readName() : tok.substr(1)); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1690 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1691 | return phdrs; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | // Read a program header type name. The next token must be a |
| 1695 | // name of a program header type or a constant (e.g. "0x3"). |
| 1696 | unsigned ScriptParser::readPhdrType() { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1697 | StringRef tok = next(); |
Fangrui Song | 4191fda | 2022-11-27 03:19:15 | [diff] [blame] | 1698 | if (std::optional<uint64_t> val = parseInt(tok)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1699 | return *val; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1700 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1701 | unsigned ret = StringSwitch<unsigned>(tok) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1702 | .Case("PT_NULL", PT_NULL) |
| 1703 | .Case("PT_LOAD", PT_LOAD) |
| 1704 | .Case("PT_DYNAMIC", PT_DYNAMIC) |
| 1705 | .Case("PT_INTERP", PT_INTERP) |
| 1706 | .Case("PT_NOTE", PT_NOTE) |
| 1707 | .Case("PT_SHLIB", PT_SHLIB) |
| 1708 | .Case("PT_PHDR", PT_PHDR) |
| 1709 | .Case("PT_TLS", PT_TLS) |
| 1710 | .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) |
| 1711 | .Case("PT_GNU_STACK", PT_GNU_STACK) |
| 1712 | .Case("PT_GNU_RELRO", PT_GNU_RELRO) |
John Ericson | d7fd8b1 | 2024-07-12 18:34:17 | [diff] [blame] | 1713 | .Case("PT_OPENBSD_MUTABLE", PT_OPENBSD_MUTABLE) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1714 | .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE) |
John Ericson | d7fd8b1 | 2024-07-12 18:34:17 | [diff] [blame] | 1715 | .Case("PT_OPENBSD_SYSCALLS", PT_OPENBSD_SYSCALLS) |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1716 | .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED) |
| 1717 | .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA) |
| 1718 | .Default(-1); |
| 1719 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1720 | if (ret == (unsigned)-1) { |
| 1721 | setError("invalid program header type: " + tok); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1722 | return PT_NULL; |
| 1723 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1724 | return ret; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | // Reads an anonymous version declaration. |
| 1728 | void ScriptParser::readAnonymousDeclaration() { |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1729 | SmallVector<SymbolVersion, 0> locals; |
| 1730 | SmallVector<SymbolVersion, 0> globals; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1731 | std::tie(locals, globals) = readSymbols(); |
Fangrui Song | e28a70d | 2019-08-05 14:31:39 | [diff] [blame] | 1732 | for (const SymbolVersion &pat : locals) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1733 | ctx.arg.versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat); |
Fangrui Song | e28a70d | 2019-08-05 14:31:39 | [diff] [blame] | 1734 | for (const SymbolVersion &pat : globals) |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1735 | ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1736 | |
| 1737 | expect(";"); |
| 1738 | } |
| 1739 | |
| 1740 | // Reads a non-anonymous version definition, |
| 1741 | // e.g. "VerStr { global: foo; bar; local: *; };". |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1742 | void ScriptParser::readVersionDeclaration(StringRef verStr) { |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1743 | // Read a symbol list. |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1744 | SmallVector<SymbolVersion, 0> locals; |
| 1745 | SmallVector<SymbolVersion, 0> globals; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1746 | std::tie(locals, globals) = readSymbols(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1747 | |
| 1748 | // Create a new version definition and add that to the global symbols. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1749 | VersionDefinition ver; |
| 1750 | ver.name = verStr; |
Fangrui Song | 00809c8 | 2021-08-05 06:52:55 | [diff] [blame] | 1751 | ver.nonLocalPatterns = std::move(globals); |
| 1752 | ver.localPatterns = std::move(locals); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1753 | ver.id = ctx.arg.versionDefinitions.size(); |
| 1754 | ctx.arg.versionDefinitions.push_back(ver); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1755 | |
| 1756 | // Each version may have a parent version. For example, "Ver2" |
| 1757 | // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" |
| 1758 | // as a parent. This version hierarchy is, probably against your |
| 1759 | // instinct, purely for hint; the runtime doesn't care about it |
| 1760 | // at all. In LLD, we simply ignore it. |
Fangrui Song | 5f38040 | 2020-02-08 22:10:29 | [diff] [blame] | 1761 | if (next() != ";") |
| 1762 | expect(";"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1763 | } |
| 1764 | |
Fangrui Song | 49279ca1 | 2020-06-18 00:11:38 | [diff] [blame] | 1765 | bool elf::hasWildcard(StringRef s) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1766 | return s.find_first_of("?*[") != StringRef::npos; |
Rui Ueyama | 1e77ad1 | 2017-07-13 20:30:35 | [diff] [blame] | 1767 | } |
| 1768 | |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1769 | // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };". |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1770 | std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>> |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1771 | ScriptParser::readSymbols() { |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1772 | SmallVector<SymbolVersion, 0> locals; |
| 1773 | SmallVector<SymbolVersion, 0> globals; |
| 1774 | SmallVector<SymbolVersion, 0> *v = &globals; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1775 | |
Hongyu Chen | f1a7d14 | 2024-07-27 21:16:12 | [diff] [blame] | 1776 | while (auto tok = till("}")) { |
| 1777 | if (tok == "extern") { |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1778 | SmallVector<SymbolVersion, 0> ext = readVersionExtern(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1779 | v->insert(v->end(), ext.begin(), ext.end()); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1780 | } else { |
Hongyu Chen | 2ae862b | 2024-07-24 05:03:46 | [diff] [blame] | 1781 | if (tok == "local:" || (tok == "local" && consume(":"))) { |
| 1782 | v = &locals; |
| 1783 | continue; |
| 1784 | } |
| 1785 | if (tok == "global:" || (tok == "global" && consume(":"))) { |
| 1786 | v = &globals; |
| 1787 | continue; |
| 1788 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1789 | v->push_back({unquote(tok), false, hasWildcard(tok)}); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1790 | } |
| 1791 | expect(";"); |
| 1792 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1793 | return {locals, globals}; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // Reads an "extern C++" directive, e.g., |
| 1797 | // "extern "C++" { ns::*; "f(int, double)"; };" |
Rui Ueyama | 17324d8 | 2018-02-01 23:46:17 | [diff] [blame] | 1798 | // |
| 1799 | // The last semicolon is optional. E.g. this is OK: |
| 1800 | // "extern "C++" { ns::*; "f(int, double)" };" |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1801 | SmallVector<SymbolVersion, 0> ScriptParser::readVersionExtern() { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1802 | StringRef tok = next(); |
| 1803 | bool isCXX = tok == "\"C++\""; |
| 1804 | if (!isCXX && tok != "\"C\"") |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1805 | setError("Unknown language"); |
| 1806 | expect("{"); |
| 1807 | |
Fangrui Song | 64038ef | 2021-12-27 04:12:55 | [diff] [blame] | 1808 | SmallVector<SymbolVersion, 0> ret; |
Fangrui Song | b32c38a | 2024-07-27 00:25:23 | [diff] [blame] | 1809 | while (auto tok = till("}")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1810 | ret.push_back( |
Fangrui Song | b32c38a | 2024-07-27 00:25:23 | [diff] [blame] | 1811 | {unquote(tok), isCXX, !tok.str.starts_with("\"") && hasWildcard(tok)}); |
Rui Ueyama | 17324d8 | 2018-02-01 23:46:17 | [diff] [blame] | 1812 | if (consume("}")) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1813 | return ret; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1814 | expect(";"); |
| 1815 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1816 | return ret; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1817 | } |
| 1818 | |
Fangrui Song | 92b5b98 | 2020-03-06 19:49:58 | [diff] [blame] | 1819 | Expr ScriptParser::readMemoryAssignment(StringRef s1, StringRef s2, |
| 1820 | StringRef s3) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1821 | if (!consume(s1) && !consume(s2) && !consume(s3)) { |
| 1822 | setError("expected one of: " + s1 + ", " + s2 + ", or " + s3); |
Fangrui Song | 92b5b98 | 2020-03-06 19:49:58 | [diff] [blame] | 1823 | return [] { return 0; }; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1824 | } |
| 1825 | expect("="); |
Fangrui Song | 92b5b98 | 2020-03-06 19:49:58 | [diff] [blame] | 1826 | return readExpr(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | // Parse the MEMORY command as specified in: |
| 1830 | // https://sourceware.org/binutils/docs/ld/MEMORY.html |
| 1831 | // |
| 1832 | // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... } |
| 1833 | void ScriptParser::readMemory() { |
| 1834 | expect("{"); |
Fangrui Song | 2a89356 | 2024-07-27 00:13:37 | [diff] [blame] | 1835 | while (auto tok = till("}")) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1836 | if (tok == "INCLUDE") { |
Rui Ueyama | 2e9d40d | 2018-10-12 17:07:32 | [diff] [blame] | 1837 | readInclude(); |
| 1838 | continue; |
| 1839 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1840 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1841 | uint32_t flags = 0; |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1842 | uint32_t invFlags = 0; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1843 | uint32_t negFlags = 0; |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1844 | uint32_t negInvFlags = 0; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1845 | if (consume("(")) { |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1846 | readMemoryAttributes(flags, invFlags, negFlags, negInvFlags); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1847 | expect(")"); |
| 1848 | } |
| 1849 | expect(":"); |
| 1850 | |
Fangrui Song | 92b5b98 | 2020-03-06 19:49:58 | [diff] [blame] | 1851 | Expr origin = readMemoryAssignment("ORIGIN", "org", "o"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1852 | expect(","); |
Fangrui Song | 92b5b98 | 2020-03-06 19:49:58 | [diff] [blame] | 1853 | Expr length = readMemoryAssignment("LENGTH", "len", "l"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1854 | |
George Rimar | 5f37541 | 2017-09-08 08:23:15 | [diff] [blame] | 1855 | // Add the memory region to the region map. |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1856 | MemoryRegion *mr = make<MemoryRegion>(tok, origin, length, flags, invFlags, |
| 1857 | negFlags, negInvFlags); |
Fangrui Song | 4629aa1 | 2024-08-22 04:23:28 | [diff] [blame] | 1858 | if (!ctx.script->memoryRegions.insert({tok, mr}).second) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1859 | setError("region '" + tok + "' already defined"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | // This function parses the attributes used to match against section |
| 1864 | // flags when placing output sections in a memory region. These flags |
| 1865 | // are only used when an explicit memory region name is not used. |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1866 | void ScriptParser::readMemoryAttributes(uint32_t &flags, uint32_t &invFlags, |
| 1867 | uint32_t &negFlags, |
| 1868 | uint32_t &negInvFlags) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1869 | bool invert = false; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1870 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1871 | for (char c : next().lower()) { |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1872 | if (c == '!') { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1873 | invert = !invert; |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1874 | std::swap(flags, negFlags); |
| 1875 | std::swap(invFlags, negInvFlags); |
| 1876 | continue; |
| 1877 | } |
| 1878 | if (c == 'w') |
| 1879 | flags |= SHF_WRITE; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1880 | else if (c == 'x') |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1881 | flags |= SHF_EXECINSTR; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 1882 | else if (c == 'a') |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1883 | flags |= SHF_ALLOC; |
| 1884 | else if (c == 'r') |
| 1885 | invFlags |= SHF_WRITE; |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1886 | else |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1887 | setError("invalid memory region attribute"); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1888 | } |
Igor Kudrin | 8cdf1c1 | 2021-11-24 05:17:03 | [diff] [blame] | 1889 | |
| 1890 | if (invert) { |
| 1891 | std::swap(flags, negFlags); |
| 1892 | std::swap(invFlags, negInvFlags); |
| 1893 | } |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1894 | } |
| 1895 | |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1896 | void elf::readLinkerScript(Ctx &ctx, MemoryBufferRef mb) { |
James Henderson | 439341b | 2020-11-03 14:41:09 | [diff] [blame] | 1897 | llvm::TimeTraceScope timeScope("Read linker script", |
| 1898 | mb.getBufferIdentifier()); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1899 | ScriptParser(ctx, mb).readLinkerScript(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1900 | } |
| 1901 | |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1902 | void elf::readVersionScript(Ctx &ctx, MemoryBufferRef mb) { |
James Henderson | 439341b | 2020-11-03 14:41:09 | [diff] [blame] | 1903 | llvm::TimeTraceScope timeScope("Read version script", |
| 1904 | mb.getBufferIdentifier()); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1905 | ScriptParser(ctx, mb).readVersionScript(); |
Rui Ueyama | 2ec3454 | 2017-04-05 05:07:39 | [diff] [blame] | 1906 | } |
| 1907 | |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1908 | void elf::readDynamicList(Ctx &ctx, MemoryBufferRef mb) { |
James Henderson | 439341b | 2020-11-03 14:41:09 | [diff] [blame] | 1909 | llvm::TimeTraceScope timeScope("Read dynamic list", mb.getBufferIdentifier()); |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1910 | ScriptParser(ctx, mb).readDynamicList(); |
Petr Hosek | 8c7e8cc | 2017-11-04 02:03:58 | [diff] [blame] | 1911 | } |
Fangrui Song | bd8cfe6 | 2019-10-07 08:31:18 | [diff] [blame] | 1912 | |
Fangrui Song | cf57a67 | 2024-09-21 18:06:06 | [diff] [blame] | 1913 | void elf::readDefsym(Ctx &ctx, MemoryBufferRef mb) { |
| 1914 | ScriptParser(ctx, mb).readDefsym(); |
| 1915 | } |