1 //===- LinkerScript.h -------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLD_ELF_LINKER_SCRIPT_H 11 #define LLD_ELF_LINKER_SCRIPT_H 12 13 #include "Config.h" 14 #include "Strings.h" 15 #include "Writer.h" 16 #include "lld/Core/LLVM.h" 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/DenseSet.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include <cstddef> 23 #include <cstdint> 24 #include <functional> 25 #include <memory> 26 #include <vector> 27 28 namespace lld { 29 namespace elf { 30 31 class DefinedCommon; 32 class SymbolBody; 33 class InputSectionBase; 34 class InputSection; 35 class OutputSection; 36 class OutputSectionFactory; 37 class InputSectionBase; 38 class SectionBase; 39 40 struct ExprValue { 41 SectionBase *Sec; 42 uint64_t Val; 43 bool ForceAbsolute; 44 45 ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val) 46 : Sec(Sec), Val(Val), ForceAbsolute(ForceAbsolute) {} 47 ExprValue(SectionBase *Sec, uint64_t Val) : ExprValue(Sec, false, Val) {} 48 ExprValue(uint64_t Val) : ExprValue(nullptr, Val) {} 49 bool isAbsolute() const { return ForceAbsolute || Sec == nullptr; } 50 uint64_t getValue() const; 51 uint64_t getSecAddr() const; 52 }; 53 54 // This represents an expression in the linker script. 55 // ScriptParser::readExpr reads an expression and returns an Expr. 56 // Later, we evaluate the expression by calling the function. 57 typedef std::function<ExprValue()> Expr; 58 59 // This enum is used to implement linker script SECTIONS command. 60 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS 61 enum SectionsCommandKind { 62 AssignmentKind, // . = expr or <sym> = expr 63 OutputSectionKind, 64 InputSectionKind, 65 AssertKind, // ASSERT(expr) 66 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr) 67 }; 68 69 struct BaseCommand { 70 BaseCommand(int K) : Kind(K) {} 71 int Kind; 72 }; 73 74 // This represents ". = <expr>" or "<symbol> = <expr>". 75 struct SymbolAssignment : BaseCommand { 76 SymbolAssignment(StringRef Name, Expr E, std::string Loc) 77 : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {} 78 79 static bool classof(const BaseCommand *C); 80 81 // The LHS of an expression. Name is either a symbol name or ".". 82 StringRef Name; 83 SymbolBody *Sym = nullptr; 84 85 // The RHS of an expression. 86 Expr Expression; 87 88 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN. 89 bool Provide = false; 90 bool Hidden = false; 91 92 // Holds file name and line number for error reporting. 93 std::string Location; 94 }; 95 96 // Linker scripts allow additional constraints to be put on ouput sections. 97 // If an output section is marked as ONLY_IF_RO, the section is created 98 // only if its input sections are read-only. Likewise, an output section 99 // with ONLY_IF_RW is created if all input sections are RW. 100 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite }; 101 102 // This struct is used to represent the location and size of regions of 103 // target memory. Instances of the struct are created by parsing the 104 // MEMORY command. 105 struct MemoryRegion { 106 std::string Name; 107 uint64_t Origin; 108 uint64_t Length; 109 uint64_t Offset; 110 uint32_t Flags; 111 uint32_t NegFlags; 112 }; 113 114 struct OutputSectionCommand : BaseCommand { 115 OutputSectionCommand(StringRef Name) 116 : BaseCommand(OutputSectionKind), Name(Name) {} 117 118 static bool classof(const BaseCommand *C); 119 120 OutputSection *Sec = nullptr; 121 MemoryRegion *MemRegion = nullptr; 122 StringRef Name; 123 Expr AddrExpr; 124 Expr AlignExpr; 125 Expr LMAExpr; 126 Expr SubalignExpr; 127 std::vector<BaseCommand *> Commands; 128 std::vector<StringRef> Phdrs; 129 llvm::Optional<uint32_t> Filler; 130 ConstraintKind Constraint = ConstraintKind::NoConstraint; 131 std::string Location; 132 std::string MemoryRegionName; 133 134 template <class ELFT> void writeTo(uint8_t *Buf); 135 uint32_t getFiller(); 136 }; 137 138 // This struct represents one section match pattern in SECTIONS() command. 139 // It can optionally have negative match pattern for EXCLUDED_FILE command. 140 // Also it may be surrounded with SORT() command, so contains sorting rules. 141 struct SectionPattern { 142 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2) 143 : ExcludedFilePat(Pat1), SectionPat(Pat2) {} 144 145 StringMatcher ExcludedFilePat; 146 StringMatcher SectionPat; 147 SortSectionPolicy SortOuter; 148 SortSectionPolicy SortInner; 149 }; 150 151 struct InputSectionDescription : BaseCommand { 152 InputSectionDescription(StringRef FilePattern) 153 : BaseCommand(InputSectionKind), FilePat(FilePattern) {} 154 155 static bool classof(const BaseCommand *C); 156 157 StringMatcher FilePat; 158 159 // Input sections that matches at least one of SectionPatterns 160 // will be associated with this InputSectionDescription. 161 std::vector<SectionPattern> SectionPatterns; 162 163 std::vector<InputSection *> Sections; 164 }; 165 166 // Represents an ASSERT(). 167 struct AssertCommand : BaseCommand { 168 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {} 169 170 static bool classof(const BaseCommand *C); 171 172 Expr Expression; 173 }; 174 175 // Represents BYTE(), SHORT(), LONG(), or QUAD(). 176 struct BytesDataCommand : BaseCommand { 177 BytesDataCommand(Expr E, unsigned Size) 178 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {} 179 180 static bool classof(const BaseCommand *C); 181 182 Expr Expression; 183 unsigned Offset; 184 unsigned Size; 185 }; 186 187 struct PhdrsCommand { 188 StringRef Name; 189 unsigned Type; 190 bool HasFilehdr; 191 bool HasPhdrs; 192 unsigned Flags; 193 Expr LMAExpr; 194 }; 195 196 // ScriptConfiguration holds linker script parse results. 197 struct ScriptConfiguration { 198 // Used to assign addresses to sections. 199 std::vector<BaseCommand *> Commands; 200 201 // Used to assign sections to headers. 202 std::vector<PhdrsCommand> PhdrsCommands; 203 204 bool HasSections = false; 205 206 // List of section patterns specified with KEEP commands. They will 207 // be kept even if they are unused and --gc-sections is specified. 208 std::vector<InputSectionDescription *> KeptSections; 209 210 // A map from memory region name to a memory region descriptor. 211 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions; 212 213 // A list of symbols referenced by the script. 214 std::vector<llvm::StringRef> ReferencedSymbols; 215 }; 216 217 class LinkerScript final { 218 llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand; 219 void assignSymbol(SymbolAssignment *Cmd, bool InSec); 220 void setDot(Expr E, const Twine &Loc, bool InSec); 221 222 std::vector<InputSection *> 223 computeInputSections(const InputSectionDescription *); 224 225 std::vector<InputSectionBase *> 226 createInputSectionList(OutputSectionCommand &Cmd); 227 228 std::vector<size_t> getPhdrIndices(OutputSection *Sec); 229 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName); 230 231 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd); 232 233 void switchTo(OutputSection *Sec); 234 uint64_t advance(uint64_t Size, unsigned Align); 235 void output(InputSection *Sec); 236 void process(BaseCommand &Base); 237 238 OutputSection *Aether; 239 bool ErrorOnMissingSection = false; 240 241 uint64_t Dot; 242 uint64_t ThreadBssOffset = 0; 243 244 std::function<uint64_t()> LMAOffset; 245 OutputSection *CurOutSec = nullptr; 246 MemoryRegion *CurMemRegion = nullptr; 247 248 public: 249 OutputSectionCommand *getCmd(OutputSection *Sec) const; 250 bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); } 251 uint64_t getDot() { return Dot; } 252 OutputSection *getOutputSection(const Twine &Loc, StringRef S); 253 uint64_t getOutputSectionSize(StringRef S); 254 void discard(ArrayRef<InputSectionBase *> V); 255 256 ExprValue getSymbolValue(const Twine &Loc, StringRef S); 257 bool isDefined(StringRef S); 258 259 std::vector<OutputSection *> *OutputSections; 260 void fabricateDefaultCommands(); 261 void addOrphanSections(OutputSectionFactory &Factory); 262 void removeEmptyCommands(); 263 void adjustSectionsBeforeSorting(); 264 void adjustSectionsAfterSorting(); 265 266 std::vector<PhdrEntry> createPhdrs(); 267 bool ignoreInterpSection(); 268 269 bool hasLMA(OutputSection *Sec); 270 bool shouldKeep(InputSectionBase *S); 271 void assignOffsets(OutputSectionCommand *Cmd); 272 void placeOrphanSections(); 273 void processNonSectionCommands(); 274 void synchronize(); 275 void assignAddresses(std::vector<PhdrEntry> &Phdrs); 276 277 void addSymbol(SymbolAssignment *Cmd); 278 void processCommands(OutputSectionFactory &Factory); 279 280 // Parsed linker script configurations are set to this struct. 281 ScriptConfiguration Opt; 282 }; 283 284 extern LinkerScript *Script; 285 286 } // end namespace elf 287 } // end namespace lld 288 289 #endif // LLD_ELF_LINKER_SCRIPT_H 290