1 //===- LinkerScript.h -------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_LINKER_SCRIPT_H
10 #define LLD_ELF_LINKER_SCRIPT_H
14 #include "lld/Common/LLVM.h"
15 #include "lld/Common/Strings.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/MemoryBuffer.h"
33 class InputSectionBase;
39 // This represents an r-value in the linker script.
41 ExprValue(SectionBase *sec, bool forceAbsolute, uint64_t val,
43 : sec(sec), forceAbsolute(forceAbsolute), val(val), loc(loc.str()) {}
45 ExprValue(uint64_t val) : ExprValue(nullptr, false, val, "") {}
47 bool isAbsolute() const { return forceAbsolute || sec == nullptr; }
48 uint64_t getValue() const;
49 uint64_t getSecAddr() const;
50 uint64_t getSectionOffset() const;
52 // If a value is relative to a section, it has a non-null Sec.
55 // True if this expression is enclosed in ABSOLUTE().
56 // This flag affects the return value of getValue().
60 uint64_t alignment = 1;
62 // Original source location. Used for error messages.
66 // This represents an expression in the linker script.
67 // ScriptParser::readExpr reads an expression and returns an Expr.
68 // Later, we evaluate the expression by calling the function.
69 using Expr = std::function<ExprValue()>;
71 // This enum is used to implement linker script SECTIONS command.
72 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
73 enum SectionsCommandKind {
74 AssignmentKind, // . = expr or <sym> = expr
77 ByteKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
81 BaseCommand(int k) : kind(k) {}
85 // This represents ". = <expr>" or "<symbol> = <expr>".
86 struct SymbolAssignment : BaseCommand {
87 SymbolAssignment(StringRef name, Expr e, std::string loc)
88 : BaseCommand(AssignmentKind), name(name), expression(e), location(loc) {}
90 static bool classof(const BaseCommand *c) {
91 return c->kind == AssignmentKind;
94 // The LHS of an expression. Name is either a symbol name or ".".
96 Defined *sym = nullptr;
98 // The RHS of an expression.
101 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
102 bool provide = false;
105 // Holds file name and line number for error reporting.
106 std::string location;
108 // A string representation of this command. We use this for -Map.
109 std::string commandString;
111 // Address of this assignment command.
114 // Size of this assignment command. This is usually 0, but if
115 // you move '.' this may be greater than 0.
119 // Linker scripts allow additional constraints to be put on ouput sections.
120 // If an output section is marked as ONLY_IF_RO, the section is created
121 // only if its input sections are read-only. Likewise, an output section
122 // with ONLY_IF_RW is created if all input sections are RW.
123 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
125 // This struct is used to represent the location and size of regions of
126 // target memory. Instances of the struct are created by parsing the
128 struct MemoryRegion {
129 MemoryRegion(StringRef name, uint64_t origin, uint64_t length, uint32_t flags,
131 : name(name), origin(origin), length(length), flags(flags),
132 negFlags(negFlags) {}
142 // This struct represents one section match pattern in SECTIONS() command.
143 // It can optionally have negative match pattern for EXCLUDED_FILE command.
144 // Also it may be surrounded with SORT() command, so contains sorting rules.
145 struct SectionPattern {
146 SectionPattern(StringMatcher &&pat1, StringMatcher &&pat2)
147 : excludedFilePat(pat1), sectionPat(pat2),
148 sortOuter(SortSectionPolicy::Default),
149 sortInner(SortSectionPolicy::Default) {}
151 StringMatcher excludedFilePat;
152 StringMatcher sectionPat;
153 SortSectionPolicy sortOuter;
154 SortSectionPolicy sortInner;
157 struct InputSectionDescription : BaseCommand {
158 InputSectionDescription(StringRef filePattern)
159 : BaseCommand(InputSectionKind), filePat(filePattern) {}
161 static bool classof(const BaseCommand *c) {
162 return c->kind == InputSectionKind;
165 StringMatcher filePat;
167 // Input sections that matches at least one of SectionPatterns
168 // will be associated with this InputSectionDescription.
169 std::vector<SectionPattern> sectionPatterns;
171 std::vector<InputSection *> sections;
173 // Temporary record of synthetic ThunkSection instances and the pass that
174 // they were created in. This is used to insert newly created ThunkSections
175 // into Sections at the end of a createThunks() pass.
176 std::vector<std::pair<ThunkSection *, uint32_t>> thunkSections;
179 // Represents BYTE(), SHORT(), LONG(), or QUAD().
180 struct ByteCommand : BaseCommand {
181 ByteCommand(Expr e, unsigned size, std::string commandString)
182 : BaseCommand(ByteKind), commandString(commandString), expression(e),
185 static bool classof(const BaseCommand *c) { return c->kind == ByteKind; }
187 // Keeps string representing the command. Used for -Map" is perhaps better.
188 std::string commandString;
192 // This is just an offset of this assignment command in the output section.
195 // Size of this data command.
199 struct PhdrsCommand {
201 unsigned type = llvm::ELF::PT_NULL;
202 bool hasFilehdr = false;
203 bool hasPhdrs = false;
204 llvm::Optional<unsigned> flags;
205 Expr lmaExpr = nullptr;
208 class LinkerScript final {
209 // Temporary state used in processSectionCommands() and assignAddresses()
210 // that must be reinitialized for each call to the above functions, and must
211 // not be used outside of the scope of a call to the above functions.
212 struct AddressState {
214 uint64_t threadBssOffset = 0;
215 OutputSection *outSec = nullptr;
216 MemoryRegion *memRegion = nullptr;
217 MemoryRegion *lmaRegion = nullptr;
218 uint64_t lmaOffset = 0;
221 llvm::DenseMap<StringRef, OutputSection *> nameToOutputSection;
223 void addSymbol(SymbolAssignment *cmd);
224 void assignSymbol(SymbolAssignment *cmd, bool inSec);
225 void setDot(Expr e, const Twine &loc, bool inSec);
226 void expandOutputSection(uint64_t size);
227 void expandMemoryRegions(uint64_t size);
229 std::vector<InputSection *>
230 computeInputSections(const InputSectionDescription *);
232 std::vector<InputSection *> createInputSectionList(OutputSection &cmd);
234 std::vector<size_t> getPhdrIndices(OutputSection *sec);
236 MemoryRegion *findMemoryRegion(OutputSection *sec);
238 void switchTo(OutputSection *sec);
239 uint64_t advance(uint64_t size, unsigned align);
240 void output(InputSection *sec);
242 void assignOffsets(OutputSection *sec);
244 // Ctx captures the local AddressState and makes it accessible
245 // deliberately. This is needed as there are some cases where we cannot just
246 // thread the current state through to a lambda function created by the
248 // This should remain a plain pointer as its lifetime is smaller than
250 AddressState *ctx = nullptr;
252 OutputSection *aether;
257 OutputSection *createOutputSection(StringRef name, StringRef location);
258 OutputSection *getOrCreateOutputSection(StringRef name);
260 bool hasPhdrsCommands() { return !phdrsCommands.empty(); }
261 uint64_t getDot() { return dot; }
262 void discard(ArrayRef<InputSection *> v);
264 ExprValue getSymbolValue(StringRef name, const Twine &loc);
266 void addOrphanSections();
267 void adjustSectionsBeforeSorting();
268 void adjustSectionsAfterSorting();
270 std::vector<PhdrEntry *> createPhdrs();
271 bool needsInterpSection();
273 bool shouldKeep(InputSectionBase *s);
274 void assignAddresses();
275 void allocateHeaders(std::vector<PhdrEntry *> &phdrs);
276 void processSectionCommands();
277 void declareSymbols();
279 // Used to handle INSERT AFTER statements.
280 void processInsertCommands();
282 // SECTIONS command list.
283 std::vector<BaseCommand *> sectionCommands;
285 // PHDRS command list.
286 std::vector<PhdrsCommand> phdrsCommands;
288 bool hasSectionsCommand = false;
289 bool errorOnMissingSection = false;
291 // List of section patterns specified with KEEP commands. They will
292 // be kept even if they are unused and --gc-sections is specified.
293 std::vector<InputSectionDescription *> keptSections;
295 // A map from memory region name to a memory region descriptor.
296 llvm::MapVector<llvm::StringRef, MemoryRegion *> memoryRegions;
298 // A list of symbols referenced by the script.
299 std::vector<llvm::StringRef> referencedSymbols;
301 // Used to implement INSERT [AFTER|BEFORE]. Contains commands that need
302 // to be inserted into SECTIONS commands list.
303 llvm::DenseMap<StringRef, std::vector<BaseCommand *>> insertAfterCommands;
304 llvm::DenseMap<StringRef, std::vector<BaseCommand *>> insertBeforeCommands;
307 extern LinkerScript *script;
309 } // end namespace elf
310 } // end namespace lld
312 #endif // LLD_ELF_LINKER_SCRIPT_H