Path: blob/main/contrib/llvm-project/lld/ELF/LinkerScript.h
34878 views
//===- LinkerScript.h -------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLD_ELF_LINKER_SCRIPT_H9#define LLD_ELF_LINKER_SCRIPT_H1011#include "Config.h"12#include "InputSection.h"13#include "Writer.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/MapVector.h"19#include "llvm/ADT/SmallVector.h"20#include "llvm/ADT/StringRef.h"21#include "llvm/Support/Compiler.h"22#include <cstddef>23#include <cstdint>24#include <functional>25#include <memory>2627namespace lld::elf {2829class Defined;30class InputFile;31class InputSection;32class InputSectionBase;33class OutputSection;34class SectionBase;35class ThunkSection;36struct OutputDesc;3738// This represents an r-value in the linker script.39struct ExprValue {40ExprValue(SectionBase *sec, bool forceAbsolute, uint64_t val,41const Twine &loc)42: sec(sec), val(val), forceAbsolute(forceAbsolute), loc(loc.str()) {}4344ExprValue(uint64_t val) : ExprValue(nullptr, false, val, "") {}4546bool isAbsolute() const { return forceAbsolute || sec == nullptr; }47uint64_t getValue() const;48uint64_t getSecAddr() const;49uint64_t getSectionOffset() const;5051// If a value is relative to a section, it has a non-null Sec.52SectionBase *sec;5354uint64_t val;55uint64_t alignment = 1;5657// The original st_type if the expression represents a symbol. Any operation58// resets type to STT_NOTYPE.59uint8_t type = llvm::ELF::STT_NOTYPE;6061// True if this expression is enclosed in ABSOLUTE().62// This flag affects the return value of getValue().63bool forceAbsolute;6465// Original source location. Used for error messages.66std::string loc;67};6869// This represents an expression in the linker script.70// ScriptParser::readExpr reads an expression and returns an Expr.71// Later, we evaluate the expression by calling the function.72using Expr = std::function<ExprValue()>;7374// This enum is used to implement linker script SECTIONS command.75// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS76enum SectionsCommandKind {77AssignmentKind, // . = expr or <sym> = expr78OutputSectionKind,79InputSectionKind,80ByteKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)81};8283struct SectionCommand {84SectionCommand(int k) : kind(k) {}85int kind;86};8788// This represents ". = <expr>" or "<symbol> = <expr>".89struct SymbolAssignment : SectionCommand {90SymbolAssignment(StringRef name, Expr e, unsigned symOrder, std::string loc)91: SectionCommand(AssignmentKind), name(name), expression(e),92symOrder(symOrder), location(loc) {}9394static bool classof(const SectionCommand *c) {95return c->kind == AssignmentKind;96}9798// The LHS of an expression. Name is either a symbol name or ".".99StringRef name;100Defined *sym = nullptr;101102// The RHS of an expression.103Expr expression;104105// Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.106bool provide = false;107bool hidden = false;108109// This assignment references DATA_SEGMENT_RELRO_END.110bool dataSegmentRelroEnd = false;111112unsigned symOrder;113114// Holds file name and line number for error reporting.115std::string location;116117// A string representation of this command. We use this for -Map.118std::string commandString;119120// Address of this assignment command.121uint64_t addr;122123// Size of this assignment command. This is usually 0, but if124// you move '.' this may be greater than 0.125uint64_t size;126};127128// Linker scripts allow additional constraints to be put on output sections.129// If an output section is marked as ONLY_IF_RO, the section is created130// only if its input sections are read-only. Likewise, an output section131// with ONLY_IF_RW is created if all input sections are RW.132enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };133134// This struct is used to represent the location and size of regions of135// target memory. Instances of the struct are created by parsing the136// MEMORY command.137struct MemoryRegion {138MemoryRegion(StringRef name, Expr origin, Expr length, uint32_t flags,139uint32_t invFlags, uint32_t negFlags, uint32_t negInvFlags)140: name(std::string(name)), origin(origin), length(length), flags(flags),141invFlags(invFlags), negFlags(negFlags), negInvFlags(negInvFlags) {}142143std::string name;144Expr origin;145Expr length;146// A section can be assigned to the region if any of these ELF section flags147// are set...148uint32_t flags;149// ... or any of these flags are not set.150// For example, the memory region attribute "r" maps to SHF_WRITE.151uint32_t invFlags;152// A section cannot be assigned to the region if any of these ELF section153// flags are set...154uint32_t negFlags;155// ... or any of these flags are not set.156// For example, the memory region attribute "!r" maps to SHF_WRITE.157uint32_t negInvFlags;158uint64_t curPos = 0;159160uint64_t getOrigin() const { return origin().getValue(); }161uint64_t getLength() const { return length().getValue(); }162163bool compatibleWith(uint32_t secFlags) const {164if ((secFlags & negFlags) || (~secFlags & negInvFlags))165return false;166return (secFlags & flags) || (~secFlags & invFlags);167}168};169170// This struct represents one section match pattern in SECTIONS() command.171// It can optionally have negative match pattern for EXCLUDED_FILE command.172// Also it may be surrounded with SORT() command, so contains sorting rules.173class SectionPattern {174StringMatcher excludedFilePat;175176// Cache of the most recent input argument and result of excludesFile().177mutable std::optional<std::pair<const InputFile *, bool>> excludesFileCache;178179public:180SectionPattern(StringMatcher &&pat1, StringMatcher &&pat2)181: excludedFilePat(pat1), sectionPat(pat2),182sortOuter(SortSectionPolicy::Default),183sortInner(SortSectionPolicy::Default) {}184185bool excludesFile(const InputFile *file) const;186187StringMatcher sectionPat;188SortSectionPolicy sortOuter;189SortSectionPolicy sortInner;190};191192class InputSectionDescription : public SectionCommand {193SingleStringMatcher filePat;194195// Cache of the most recent input argument and result of matchesFile().196mutable std::optional<std::pair<const InputFile *, bool>> matchesFileCache;197198public:199InputSectionDescription(StringRef filePattern, uint64_t withFlags = 0,200uint64_t withoutFlags = 0)201: SectionCommand(InputSectionKind), filePat(filePattern),202withFlags(withFlags), withoutFlags(withoutFlags) {}203204static bool classof(const SectionCommand *c) {205return c->kind == InputSectionKind;206}207208bool matchesFile(const InputFile *file) const;209210// Input sections that matches at least one of SectionPatterns211// will be associated with this InputSectionDescription.212SmallVector<SectionPattern, 0> sectionPatterns;213214// Includes InputSections and MergeInputSections. Used temporarily during215// assignment of input sections to output sections.216SmallVector<InputSectionBase *, 0> sectionBases;217218// Used after the finalizeInputSections() pass. MergeInputSections have been219// merged into MergeSyntheticSections.220SmallVector<InputSection *, 0> sections;221222// Temporary record of synthetic ThunkSection instances and the pass that223// they were created in. This is used to insert newly created ThunkSections224// into Sections at the end of a createThunks() pass.225SmallVector<std::pair<ThunkSection *, uint32_t>, 0> thunkSections;226227// SectionPatterns can be filtered with the INPUT_SECTION_FLAGS command.228uint64_t withFlags;229uint64_t withoutFlags;230};231232// Represents BYTE(), SHORT(), LONG(), or QUAD().233struct ByteCommand : SectionCommand {234ByteCommand(Expr e, unsigned size, std::string commandString)235: SectionCommand(ByteKind), commandString(commandString), expression(e),236size(size) {}237238static bool classof(const SectionCommand *c) { return c->kind == ByteKind; }239240// Keeps string representing the command. Used for -Map" is perhaps better.241std::string commandString;242243Expr expression;244245// This is just an offset of this assignment command in the output section.246unsigned offset;247248// Size of this data command.249unsigned size;250};251252struct InsertCommand {253SmallVector<StringRef, 0> names;254bool isAfter;255StringRef where;256};257258// A NOCROSSREFS/NOCROSSREFS_TO command that prohibits references between259// certain output sections.260struct NoCrossRefCommand {261SmallVector<StringRef, 0> outputSections;262263// When true, this describes a NOCROSSREFS_TO command that probits references264// to the first output section from any of the other sections.265bool toFirst = false;266};267268struct PhdrsCommand {269StringRef name;270unsigned type = llvm::ELF::PT_NULL;271bool hasFilehdr = false;272bool hasPhdrs = false;273std::optional<unsigned> flags;274Expr lmaExpr = nullptr;275};276277class LinkerScript final {278// Temporary state used in processSectionCommands() and assignAddresses()279// that must be reinitialized for each call to the above functions, and must280// not be used outside of the scope of a call to the above functions.281struct AddressState {282AddressState();283OutputSection *outSec = nullptr;284MemoryRegion *memRegion = nullptr;285MemoryRegion *lmaRegion = nullptr;286uint64_t lmaOffset = 0;287uint64_t tbssAddr = 0;288};289290llvm::DenseMap<llvm::CachedHashStringRef, OutputDesc *> nameToOutputSection;291292void addSymbol(SymbolAssignment *cmd);293void assignSymbol(SymbolAssignment *cmd, bool inSec);294void setDot(Expr e, const Twine &loc, bool inSec);295void expandOutputSection(uint64_t size);296void expandMemoryRegions(uint64_t size);297298SmallVector<InputSectionBase *, 0>299computeInputSections(const InputSectionDescription *,300ArrayRef<InputSectionBase *>,301const OutputSection &outCmd);302303SmallVector<InputSectionBase *, 0> createInputSectionList(OutputSection &cmd);304305void discardSynthetic(OutputSection &);306307SmallVector<size_t, 0> getPhdrIndices(OutputSection *sec);308309std::pair<MemoryRegion *, MemoryRegion *>310findMemoryRegion(OutputSection *sec, MemoryRegion *hint);311312bool assignOffsets(OutputSection *sec);313314// This captures the local AddressState and makes it accessible315// deliberately. This is needed as there are some cases where we cannot just316// thread the current state through to a lambda function created by the317// script parser.318// This should remain a plain pointer as its lifetime is smaller than319// LinkerScript.320AddressState *state = nullptr;321322OutputSection *aether;323324uint64_t dot;325326public:327OutputDesc *createOutputSection(StringRef name, StringRef location);328OutputDesc *getOrCreateOutputSection(StringRef name);329330bool hasPhdrsCommands() { return !phdrsCommands.empty(); }331uint64_t getDot() { return dot; }332void discard(InputSectionBase &s);333334ExprValue getSymbolValue(StringRef name, const Twine &loc);335336void addOrphanSections();337void diagnoseOrphanHandling() const;338void diagnoseMissingSGSectionAddress() const;339void adjustOutputSections();340void adjustSectionsAfterSorting();341342SmallVector<PhdrEntry *, 0> createPhdrs();343bool needsInterpSection();344345bool shouldKeep(InputSectionBase *s);346std::pair<const OutputSection *, const Defined *> assignAddresses();347bool spillSections();348void erasePotentialSpillSections();349void allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs);350void processSectionCommands();351void processSymbolAssignments();352void declareSymbols();353354// Used to handle INSERT AFTER statements.355void processInsertCommands();356357// Describe memory region usage.358void printMemoryUsage(raw_ostream &os);359360// Record a pending error during an assignAddresses invocation.361// assignAddresses is executed more than once. Therefore, lld::error should be362// avoided to not report duplicate errors.363void recordError(const Twine &msg);364365// Check backward location counter assignment and memory region/LMA overflows.366void checkFinalScriptConditions() const;367368// Add symbols that are referenced in the linker script to the symbol table.369// Symbols referenced in a PROVIDE command are only added to the symbol table370// if the PROVIDE command actually provides the symbol.371// It also adds the symbols referenced by the used PROVIDE symbols to the372// linker script referenced symbols list.373void addScriptReferencedSymbolsToSymTable();374375// Returns true if the PROVIDE symbol should be added to the link.376// A PROVIDE symbol is added to the link only if it satisfies an377// undefined reference.378static bool shouldAddProvideSym(StringRef symName);379380// SECTIONS command list.381SmallVector<SectionCommand *, 0> sectionCommands;382383// PHDRS command list.384SmallVector<PhdrsCommand, 0> phdrsCommands;385386bool hasSectionsCommand = false;387bool seenDataAlign = false;388bool seenRelroEnd = false;389bool errorOnMissingSection = false;390SmallVector<SmallString<0>, 0> recordedErrors;391392// List of section patterns specified with KEEP commands. They will393// be kept even if they are unused and --gc-sections is specified.394SmallVector<InputSectionDescription *, 0> keptSections;395396// A map from memory region name to a memory region descriptor.397llvm::MapVector<llvm::StringRef, MemoryRegion *> memoryRegions;398399// A list of symbols referenced by the script.400SmallVector<llvm::StringRef, 0> referencedSymbols;401402// Used to implement INSERT [AFTER|BEFORE]. Contains output sections that need403// to be reordered.404SmallVector<InsertCommand, 0> insertCommands;405406// OutputSections specified by OVERWRITE_SECTIONS.407SmallVector<OutputDesc *, 0> overwriteSections;408409// NOCROSSREFS(_TO) commands.410SmallVector<NoCrossRefCommand, 0> noCrossRefs;411412// Sections that will be warned/errored by --orphan-handling.413SmallVector<const InputSectionBase *, 0> orphanSections;414415// Stores the mapping: PROVIDE symbol -> symbols referred in the PROVIDE416// expression. For example, if the PROVIDE command is:417//418// PROVIDE(v = a + b + c);419//420// then provideMap should contain the mapping: 'v' -> ['a', 'b', 'c']421llvm::MapVector<StringRef, SmallVector<StringRef, 0>> provideMap;422423// List of potential spill locations (PotentialSpillSection) for an input424// section.425struct PotentialSpillList {426// Never nullptr.427PotentialSpillSection *head;428PotentialSpillSection *tail;429};430llvm::DenseMap<InputSectionBase *, PotentialSpillList> potentialSpillLists;431};432433struct ScriptWrapper {434LinkerScript s;435LinkerScript *operator->() { return &s; }436};437438LLVM_LIBRARY_VISIBILITY extern ScriptWrapper script;439440} // end namespace lld::elf441442#endif // LLD_ELF_LINKER_SCRIPT_H443444445