xref: /freebsd/contrib/llvm-project/lld/ELF/LinkerScript.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- LinkerScript.h -------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLD_ELF_LINKER_SCRIPT_H
100b57cec5SDimitry Andric #define LLD_ELF_LINKER_SCRIPT_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "Config.h"
130b57cec5SDimitry Andric #include "Writer.h"
140b57cec5SDimitry Andric #include "lld/Common/LLVM.h"
150b57cec5SDimitry Andric #include "lld/Common/Strings.h"
160b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
170b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
180b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
190b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
210b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
220b57cec5SDimitry Andric #include <cstddef>
230b57cec5SDimitry Andric #include <cstdint>
240b57cec5SDimitry Andric #include <functional>
250b57cec5SDimitry Andric #include <memory>
260b57cec5SDimitry Andric #include <vector>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric namespace lld {
290b57cec5SDimitry Andric namespace elf {
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric class Defined;
320b57cec5SDimitry Andric class InputSection;
330b57cec5SDimitry Andric class InputSectionBase;
340b57cec5SDimitry Andric class OutputSection;
350b57cec5SDimitry Andric class SectionBase;
360b57cec5SDimitry Andric class Symbol;
370b57cec5SDimitry Andric class ThunkSection;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric // This represents an r-value in the linker script.
400b57cec5SDimitry Andric struct ExprValue {
410b57cec5SDimitry Andric   ExprValue(SectionBase *sec, bool forceAbsolute, uint64_t val,
420b57cec5SDimitry Andric             const Twine &loc)
430b57cec5SDimitry Andric       : sec(sec), forceAbsolute(forceAbsolute), val(val), loc(loc.str()) {}
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   ExprValue(uint64_t val) : ExprValue(nullptr, false, val, "") {}
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   bool isAbsolute() const { return forceAbsolute || sec == nullptr; }
480b57cec5SDimitry Andric   uint64_t getValue() const;
490b57cec5SDimitry Andric   uint64_t getSecAddr() const;
500b57cec5SDimitry Andric   uint64_t getSectionOffset() const;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   // If a value is relative to a section, it has a non-null Sec.
530b57cec5SDimitry Andric   SectionBase *sec;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // True if this expression is enclosed in ABSOLUTE().
560b57cec5SDimitry Andric   // This flag affects the return value of getValue().
570b57cec5SDimitry Andric   bool forceAbsolute;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   uint64_t val;
600b57cec5SDimitry Andric   uint64_t alignment = 1;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   // Original source location. Used for error messages.
630b57cec5SDimitry Andric   std::string loc;
640b57cec5SDimitry Andric };
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric // This represents an expression in the linker script.
670b57cec5SDimitry Andric // ScriptParser::readExpr reads an expression and returns an Expr.
680b57cec5SDimitry Andric // Later, we evaluate the expression by calling the function.
690b57cec5SDimitry Andric using Expr = std::function<ExprValue()>;
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric // This enum is used to implement linker script SECTIONS command.
720b57cec5SDimitry Andric // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
730b57cec5SDimitry Andric enum SectionsCommandKind {
740b57cec5SDimitry Andric   AssignmentKind, // . = expr or <sym> = expr
750b57cec5SDimitry Andric   OutputSectionKind,
760b57cec5SDimitry Andric   InputSectionKind,
770b57cec5SDimitry Andric   ByteKind    // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
780b57cec5SDimitry Andric };
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric struct BaseCommand {
810b57cec5SDimitry Andric   BaseCommand(int k) : kind(k) {}
820b57cec5SDimitry Andric   int kind;
830b57cec5SDimitry Andric };
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric // This represents ". = <expr>" or "<symbol> = <expr>".
860b57cec5SDimitry Andric struct SymbolAssignment : BaseCommand {
870b57cec5SDimitry Andric   SymbolAssignment(StringRef name, Expr e, std::string loc)
880b57cec5SDimitry Andric       : BaseCommand(AssignmentKind), name(name), expression(e), location(loc) {}
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   static bool classof(const BaseCommand *c) {
910b57cec5SDimitry Andric     return c->kind == AssignmentKind;
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   // The LHS of an expression. Name is either a symbol name or ".".
950b57cec5SDimitry Andric   StringRef name;
960b57cec5SDimitry Andric   Defined *sym = nullptr;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   // The RHS of an expression.
990b57cec5SDimitry Andric   Expr expression;
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
1020b57cec5SDimitry Andric   bool provide = false;
1030b57cec5SDimitry Andric   bool hidden = false;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   // Holds file name and line number for error reporting.
1060b57cec5SDimitry Andric   std::string location;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // A string representation of this command. We use this for -Map.
1090b57cec5SDimitry Andric   std::string commandString;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   // Address of this assignment command.
112*5ffd83dbSDimitry Andric   uint64_t addr;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   // Size of this assignment command. This is usually 0, but if
1150b57cec5SDimitry Andric   // you move '.' this may be greater than 0.
116*5ffd83dbSDimitry Andric   uint64_t size;
1170b57cec5SDimitry Andric };
1180b57cec5SDimitry Andric 
119480093f4SDimitry Andric // Linker scripts allow additional constraints to be put on output sections.
1200b57cec5SDimitry Andric // If an output section is marked as ONLY_IF_RO, the section is created
1210b57cec5SDimitry Andric // only if its input sections are read-only. Likewise, an output section
1220b57cec5SDimitry Andric // with ONLY_IF_RW is created if all input sections are RW.
1230b57cec5SDimitry Andric enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric // This struct is used to represent the location and size of regions of
1260b57cec5SDimitry Andric // target memory. Instances of the struct are created by parsing the
1270b57cec5SDimitry Andric // MEMORY command.
1280b57cec5SDimitry Andric struct MemoryRegion {
129*5ffd83dbSDimitry Andric   MemoryRegion(StringRef name, Expr origin, Expr length, uint32_t flags,
1300b57cec5SDimitry Andric                uint32_t negFlags)
131*5ffd83dbSDimitry Andric       : name(std::string(name)), origin(origin), length(length), flags(flags),
1320b57cec5SDimitry Andric         negFlags(negFlags) {}
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   std::string name;
135*5ffd83dbSDimitry Andric   Expr origin;
136*5ffd83dbSDimitry Andric   Expr length;
1370b57cec5SDimitry Andric   uint32_t flags;
1380b57cec5SDimitry Andric   uint32_t negFlags;
1390b57cec5SDimitry Andric   uint64_t curPos = 0;
1400b57cec5SDimitry Andric };
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric // This struct represents one section match pattern in SECTIONS() command.
1430b57cec5SDimitry Andric // It can optionally have negative match pattern for EXCLUDED_FILE command.
1440b57cec5SDimitry Andric // Also it may be surrounded with SORT() command, so contains sorting rules.
1450b57cec5SDimitry Andric struct SectionPattern {
1460b57cec5SDimitry Andric   SectionPattern(StringMatcher &&pat1, StringMatcher &&pat2)
1470b57cec5SDimitry Andric       : excludedFilePat(pat1), sectionPat(pat2),
1480b57cec5SDimitry Andric         sortOuter(SortSectionPolicy::Default),
1490b57cec5SDimitry Andric         sortInner(SortSectionPolicy::Default) {}
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   StringMatcher excludedFilePat;
1520b57cec5SDimitry Andric   StringMatcher sectionPat;
1530b57cec5SDimitry Andric   SortSectionPolicy sortOuter;
1540b57cec5SDimitry Andric   SortSectionPolicy sortInner;
1550b57cec5SDimitry Andric };
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric struct InputSectionDescription : BaseCommand {
158*5ffd83dbSDimitry Andric   InputSectionDescription(StringRef filePattern, uint64_t withFlags = 0,
159*5ffd83dbSDimitry Andric                           uint64_t withoutFlags = 0)
160*5ffd83dbSDimitry Andric       : BaseCommand(InputSectionKind), filePat(filePattern),
161*5ffd83dbSDimitry Andric         withFlags(withFlags), withoutFlags(withoutFlags) {}
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   static bool classof(const BaseCommand *c) {
1640b57cec5SDimitry Andric     return c->kind == InputSectionKind;
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric 
167*5ffd83dbSDimitry Andric   SingleStringMatcher filePat;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // Input sections that matches at least one of SectionPatterns
1700b57cec5SDimitry Andric   // will be associated with this InputSectionDescription.
1710b57cec5SDimitry Andric   std::vector<SectionPattern> sectionPatterns;
1720b57cec5SDimitry Andric 
17385868e8aSDimitry Andric   // Includes InputSections and MergeInputSections. Used temporarily during
17485868e8aSDimitry Andric   // assignment of input sections to output sections.
17585868e8aSDimitry Andric   std::vector<InputSectionBase *> sectionBases;
17685868e8aSDimitry Andric 
17785868e8aSDimitry Andric   // Used after the finalizeInputSections() pass. MergeInputSections have been
17885868e8aSDimitry Andric   // merged into MergeSyntheticSections.
1790b57cec5SDimitry Andric   std::vector<InputSection *> sections;
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // Temporary record of synthetic ThunkSection instances and the pass that
1820b57cec5SDimitry Andric   // they were created in. This is used to insert newly created ThunkSections
1830b57cec5SDimitry Andric   // into Sections at the end of a createThunks() pass.
1840b57cec5SDimitry Andric   std::vector<std::pair<ThunkSection *, uint32_t>> thunkSections;
185*5ffd83dbSDimitry Andric 
186*5ffd83dbSDimitry Andric   // SectionPatterns can be filtered with the INPUT_SECTION_FLAGS command.
187*5ffd83dbSDimitry Andric   uint64_t withFlags;
188*5ffd83dbSDimitry Andric   uint64_t withoutFlags;
1890b57cec5SDimitry Andric };
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric // Represents BYTE(), SHORT(), LONG(), or QUAD().
1920b57cec5SDimitry Andric struct ByteCommand : BaseCommand {
1930b57cec5SDimitry Andric   ByteCommand(Expr e, unsigned size, std::string commandString)
1940b57cec5SDimitry Andric       : BaseCommand(ByteKind), commandString(commandString), expression(e),
1950b57cec5SDimitry Andric         size(size) {}
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   static bool classof(const BaseCommand *c) { return c->kind == ByteKind; }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   // Keeps string representing the command. Used for -Map" is perhaps better.
2000b57cec5SDimitry Andric   std::string commandString;
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   Expr expression;
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   // This is just an offset of this assignment command in the output section.
2050b57cec5SDimitry Andric   unsigned offset;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   // Size of this data command.
2080b57cec5SDimitry Andric   unsigned size;
2090b57cec5SDimitry Andric };
2100b57cec5SDimitry Andric 
211*5ffd83dbSDimitry Andric struct InsertCommand {
212*5ffd83dbSDimitry Andric   OutputSection *os;
213*5ffd83dbSDimitry Andric   bool isAfter;
214*5ffd83dbSDimitry Andric   StringRef where;
215*5ffd83dbSDimitry Andric };
216*5ffd83dbSDimitry Andric 
2170b57cec5SDimitry Andric struct PhdrsCommand {
2180b57cec5SDimitry Andric   StringRef name;
2190b57cec5SDimitry Andric   unsigned type = llvm::ELF::PT_NULL;
2200b57cec5SDimitry Andric   bool hasFilehdr = false;
2210b57cec5SDimitry Andric   bool hasPhdrs = false;
2220b57cec5SDimitry Andric   llvm::Optional<unsigned> flags;
2230b57cec5SDimitry Andric   Expr lmaExpr = nullptr;
2240b57cec5SDimitry Andric };
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric class LinkerScript final {
2270b57cec5SDimitry Andric   // Temporary state used in processSectionCommands() and assignAddresses()
2280b57cec5SDimitry Andric   // that must be reinitialized for each call to the above functions, and must
2290b57cec5SDimitry Andric   // not be used outside of the scope of a call to the above functions.
2300b57cec5SDimitry Andric   struct AddressState {
2310b57cec5SDimitry Andric     AddressState();
2320b57cec5SDimitry Andric     uint64_t threadBssOffset = 0;
2330b57cec5SDimitry Andric     OutputSection *outSec = nullptr;
2340b57cec5SDimitry Andric     MemoryRegion *memRegion = nullptr;
2350b57cec5SDimitry Andric     MemoryRegion *lmaRegion = nullptr;
2360b57cec5SDimitry Andric     uint64_t lmaOffset = 0;
2370b57cec5SDimitry Andric   };
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   llvm::DenseMap<StringRef, OutputSection *> nameToOutputSection;
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric   void addSymbol(SymbolAssignment *cmd);
2420b57cec5SDimitry Andric   void assignSymbol(SymbolAssignment *cmd, bool inSec);
2430b57cec5SDimitry Andric   void setDot(Expr e, const Twine &loc, bool inSec);
2440b57cec5SDimitry Andric   void expandOutputSection(uint64_t size);
2450b57cec5SDimitry Andric   void expandMemoryRegions(uint64_t size);
2460b57cec5SDimitry Andric 
24785868e8aSDimitry Andric   std::vector<InputSectionBase *>
248*5ffd83dbSDimitry Andric   computeInputSections(const InputSectionDescription *,
249*5ffd83dbSDimitry Andric                        ArrayRef<InputSectionBase *>);
2500b57cec5SDimitry Andric 
25185868e8aSDimitry Andric   std::vector<InputSectionBase *> createInputSectionList(OutputSection &cmd);
2520b57cec5SDimitry Andric 
253*5ffd83dbSDimitry Andric   void discardSynthetic(OutputSection &);
254*5ffd83dbSDimitry Andric 
2550b57cec5SDimitry Andric   std::vector<size_t> getPhdrIndices(OutputSection *sec);
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric   MemoryRegion *findMemoryRegion(OutputSection *sec);
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   void switchTo(OutputSection *sec);
2600b57cec5SDimitry Andric   uint64_t advance(uint64_t size, unsigned align);
2610b57cec5SDimitry Andric   void output(InputSection *sec);
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   void assignOffsets(OutputSection *sec);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   // Ctx captures the local AddressState and makes it accessible
2660b57cec5SDimitry Andric   // deliberately. This is needed as there are some cases where we cannot just
2670b57cec5SDimitry Andric   // thread the current state through to a lambda function created by the
2680b57cec5SDimitry Andric   // script parser.
2690b57cec5SDimitry Andric   // This should remain a plain pointer as its lifetime is smaller than
2700b57cec5SDimitry Andric   // LinkerScript.
2710b57cec5SDimitry Andric   AddressState *ctx = nullptr;
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   OutputSection *aether;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   uint64_t dot;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric public:
2780b57cec5SDimitry Andric   OutputSection *createOutputSection(StringRef name, StringRef location);
2790b57cec5SDimitry Andric   OutputSection *getOrCreateOutputSection(StringRef name);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   bool hasPhdrsCommands() { return !phdrsCommands.empty(); }
2820b57cec5SDimitry Andric   uint64_t getDot() { return dot; }
28385868e8aSDimitry Andric   void discard(InputSectionBase *s);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   ExprValue getSymbolValue(StringRef name, const Twine &loc);
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   void addOrphanSections();
288*5ffd83dbSDimitry Andric   void diagnoseOrphanHandling() const;
2890b57cec5SDimitry Andric   void adjustSectionsBeforeSorting();
2900b57cec5SDimitry Andric   void adjustSectionsAfterSorting();
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   std::vector<PhdrEntry *> createPhdrs();
2930b57cec5SDimitry Andric   bool needsInterpSection();
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   bool shouldKeep(InputSectionBase *s);
29685868e8aSDimitry Andric   const Defined *assignAddresses();
2970b57cec5SDimitry Andric   void allocateHeaders(std::vector<PhdrEntry *> &phdrs);
2980b57cec5SDimitry Andric   void processSectionCommands();
29985868e8aSDimitry Andric   void processSymbolAssignments();
3000b57cec5SDimitry Andric   void declareSymbols();
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   // Used to handle INSERT AFTER statements.
3030b57cec5SDimitry Andric   void processInsertCommands();
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   // SECTIONS command list.
3060b57cec5SDimitry Andric   std::vector<BaseCommand *> sectionCommands;
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // PHDRS command list.
3090b57cec5SDimitry Andric   std::vector<PhdrsCommand> phdrsCommands;
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   bool hasSectionsCommand = false;
3120b57cec5SDimitry Andric   bool errorOnMissingSection = false;
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   // List of section patterns specified with KEEP commands. They will
3150b57cec5SDimitry Andric   // be kept even if they are unused and --gc-sections is specified.
3160b57cec5SDimitry Andric   std::vector<InputSectionDescription *> keptSections;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // A map from memory region name to a memory region descriptor.
3190b57cec5SDimitry Andric   llvm::MapVector<llvm::StringRef, MemoryRegion *> memoryRegions;
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   // A list of symbols referenced by the script.
3220b57cec5SDimitry Andric   std::vector<llvm::StringRef> referencedSymbols;
3230b57cec5SDimitry Andric 
324*5ffd83dbSDimitry Andric   // Used to implement INSERT [AFTER|BEFORE]. Contains output sections that need
325*5ffd83dbSDimitry Andric   // to be reordered.
326*5ffd83dbSDimitry Andric   std::vector<InsertCommand> insertCommands;
327*5ffd83dbSDimitry Andric 
328*5ffd83dbSDimitry Andric   // Sections that will be warned/errored by --orphan-handling.
329*5ffd83dbSDimitry Andric   std::vector<const InputSectionBase *> orphanSections;
3300b57cec5SDimitry Andric };
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric extern LinkerScript *script;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric } // end namespace elf
3350b57cec5SDimitry Andric } // end namespace lld
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric #endif // LLD_ELF_LINKER_SCRIPT_H
338