blob: 2c8be8df3eb542be7e404898dd8900485a498721 [file] [log] [blame]
Rui Ueyama717677a2016-02-11 21:17:59 +00001//===- 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
George Rimarbe394db2016-09-16 20:21:55 +000013#include "Config.h"
George Rimarc91930a2016-09-02 21:17:20 +000014#include "Strings.h"
Eugene Leviantbbe38602016-07-19 09:25:43 +000015#include "Writer.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000016#include "lld/Core/LLVM.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000017#include "llvm/ADT/ArrayRef.h"
Meador Ingeb8897442017-01-24 02:34:00 +000018#include "llvm/ADT/DenseMap.h"
Rafael Espindolad3190792016-09-16 15:10:23 +000019#include "llvm/ADT/DenseSet.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000020#include "llvm/ADT/StringRef.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000021#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000022#include <cstddef>
23#include <cstdint>
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +000024#include <functional>
Eugene Zelenko22886a22016-11-05 01:00:56 +000025#include <memory>
26#include <vector>
Rui Ueyama717677a2016-02-11 21:17:59 +000027
28namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029namespace elf {
Eugene Zelenko22886a22016-11-05 01:00:56 +000030
Rafael Espindolae7553e42016-08-31 13:28:33 +000031class DefinedCommon;
Rui Ueyama8d083e62016-07-29 05:48:39 +000032class SymbolBody;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000033class InputSectionBase;
Rafael Espindola774ea7d2017-02-23 16:49:07 +000034class InputSection;
Rafael Espindola24e6f362017-02-24 15:07:30 +000035class OutputSection;
Rui Ueyama02a036f2017-02-27 02:31:48 +000036class OutputSectionFactory;
Rafael Espindolac404d502017-02-23 02:32:18 +000037class InputSectionBase;
Rafael Espindola9bd45662017-03-10 00:47:33 +000038class SectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000039
Rafael Espindola72dc1952017-03-17 13:05:04 +000040struct 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;
Rafael Espindola7ba5f472017-03-17 14:55:36 +000051 uint64_t getSecAddr() const;
Rafael Espindola72dc1952017-03-17 13:05:04 +000052};
53
Rui Ueyamab04af132016-10-13 23:08:33 +000054// This represents an expression in the linker script.
55// ScriptParser::readExpr reads an expression and returns an Expr.
Rafael Espindola72dc1952017-03-17 13:05:04 +000056// Later, we evaluate the expression by calling the function.
57typedef std::function<ExprValue()> Expr;
Rui Ueyama708019c2016-07-24 18:19:40 +000058
George Rimareea31142016-07-21 14:26:59 +000059// This enum is used to implement linker script SECTIONS command.
60// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
61enum SectionsCommandKind {
George Rimare38cbab2016-09-26 19:22:50 +000062 AssignmentKind, // . = expr or <sym> = expr
George Rimareea31142016-07-21 14:26:59 +000063 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000064 InputSectionKind,
George Rimare38cbab2016-09-26 19:22:50 +000065 AssertKind, // ASSERT(expr)
66 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000067};
68
George Rimar076fe152016-07-21 06:43:01 +000069struct BaseCommand {
70 BaseCommand(int K) : Kind(K) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000071
72 virtual ~BaseCommand() = default;
73
George Rimar076fe152016-07-21 06:43:01 +000074 int Kind;
75};
76
Rui Ueyamab04af132016-10-13 23:08:33 +000077// This represents ". = <expr>" or "<symbol> = <expr>".
George Rimar076fe152016-07-21 06:43:01 +000078struct SymbolAssignment : BaseCommand {
George Rimara8d8dcf2017-02-22 09:13:04 +000079 SymbolAssignment(StringRef Name, Expr E, std::string Loc)
80 : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000081
George Rimar076fe152016-07-21 06:43:01 +000082 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000083
84 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000085 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000086 SymbolBody *Sym = nullptr;
87
88 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000089 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000090
91 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000092 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000093 bool Hidden = false;
George Rimar2ee2d2d2017-02-21 14:50:38 +000094
George Rimara8d8dcf2017-02-22 09:13:04 +000095 // Holds file name and line number for error reporting.
George Rimar2ee2d2d2017-02-21 14:50:38 +000096 std::string Location;
George Rimar076fe152016-07-21 06:43:01 +000097};
98
Davide Italiano246f6812016-07-22 03:36:24 +000099// Linker scripts allow additional constraints to be put on ouput sections.
Rui Ueyamab04af132016-10-13 23:08:33 +0000100// If an output section is marked as ONLY_IF_RO, the section is created
101// only if its input sections are read-only. Likewise, an output section
102// with ONLY_IF_RW is created if all input sections are RW.
Rui Ueyamaefc40662016-07-25 22:00:10 +0000103enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +0000104
George Rimar076fe152016-07-21 06:43:01 +0000105struct OutputSectionCommand : BaseCommand {
106 OutputSectionCommand(StringRef Name)
107 : BaseCommand(OutputSectionKind), Name(Name) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000108
George Rimar076fe152016-07-21 06:43:01 +0000109 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000110
Eugene Levianteda81a12016-07-12 06:39:48 +0000111 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +0000112 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +0000113 Expr AlignExpr;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000114 Expr LMAExpr;
George Rimardb24d9c2016-08-19 15:18:23 +0000115 Expr SubalignExpr;
Rui Ueyama8f99f732017-04-05 03:20:42 +0000116 std::vector<BaseCommand *> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000117 std::vector<StringRef> Phdrs;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000118 uint32_t Filler = 0;
Rui Ueyamaefc40662016-07-25 22:00:10 +0000119 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviant2a942c42016-12-05 16:38:32 +0000120 std::string Location;
Meador Ingeb8897442017-01-24 02:34:00 +0000121 std::string MemoryRegionName;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000122};
123
George Rimar8034d492016-09-17 07:31:49 +0000124// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000125// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000126// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000127struct SectionPattern {
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000128 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
129 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000130
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000131 StringMatcher ExcludedFilePat;
132 StringMatcher SectionPat;
George Rimar07171f22016-09-21 15:56:44 +0000133 SortSectionPolicy SortOuter;
134 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000135};
136
George Rimareea31142016-07-21 14:26:59 +0000137struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000138 InputSectionDescription(StringRef FilePattern)
Vitaly Buka0b7de062016-12-21 02:27:14 +0000139 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000140
George Rimareea31142016-07-21 14:26:59 +0000141 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000142
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000143 StringMatcher FilePat;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000144
George Rimar8034d492016-09-17 07:31:49 +0000145 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000146 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000147 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000148
Rafael Espindolac404d502017-02-23 02:32:18 +0000149 std::vector<InputSectionBase *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000150};
151
Rui Ueyamab04af132016-10-13 23:08:33 +0000152// Represents an ASSERT().
George Rimareefa7582016-08-04 09:29:31 +0000153struct AssertCommand : BaseCommand {
154 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000155
George Rimareefa7582016-08-04 09:29:31 +0000156 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000157
George Rimareefa7582016-08-04 09:29:31 +0000158 Expr Expression;
159};
160
Rui Ueyamab04af132016-10-13 23:08:33 +0000161// Represents BYTE(), SHORT(), LONG(), or QUAD().
George Rimare38cbab2016-09-26 19:22:50 +0000162struct BytesDataCommand : BaseCommand {
Meador Inge95c7d8d2016-12-08 23:21:30 +0000163 BytesDataCommand(Expr E, unsigned Size)
164 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000165
George Rimare38cbab2016-09-26 19:22:50 +0000166 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000167
Meador Inge95c7d8d2016-12-08 23:21:30 +0000168 Expr Expression;
George Rimare38cbab2016-09-26 19:22:50 +0000169 unsigned Offset;
170 unsigned Size;
171};
172
Eugene Leviantbbe38602016-07-19 09:25:43 +0000173struct PhdrsCommand {
174 StringRef Name;
175 unsigned Type;
176 bool HasFilehdr;
177 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000178 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000179 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000180};
181
Meador Ingeb8897442017-01-24 02:34:00 +0000182// This struct is used to represent the location and size of regions of
183// target memory. Instances of the struct are created by parsing the
184// MEMORY command.
185struct MemoryRegion {
186 std::string Name;
187 uint64_t Origin;
188 uint64_t Length;
189 uint64_t Offset;
190 uint32_t Flags;
Rui Ueyama8a8a9532017-01-26 02:58:59 +0000191 uint32_t NegFlags;
Meador Ingeb8897442017-01-24 02:34:00 +0000192};
193
Rui Ueyama07320e42016-04-20 20:13:41 +0000194// ScriptConfiguration holds linker script parse results.
195struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000196 // Used to assign addresses to sections.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000197 std::vector<BaseCommand *> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000198
Eugene Leviantbbe38602016-07-19 09:25:43 +0000199 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000200 std::vector<PhdrsCommand> PhdrsCommands;
201
Eugene Leviante05336ff2016-09-14 08:32:36 +0000202 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000203
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000204 // List of section patterns specified with KEEP commands. They will
205 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000206 std::vector<InputSectionDescription *> KeptSections;
Meador Ingeb8897442017-01-24 02:34:00 +0000207
208 // A map from memory region name to a memory region descriptor.
209 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
Petr Hosek30f16b22017-03-23 03:52:34 +0000210
211 // A list of undefined symbols referenced by the script.
Rui Ueyama4eb2ecc2017-04-05 18:02:30 +0000212 std::vector<llvm::StringRef> ReferencedSymbols;
Rui Ueyama717677a2016-02-11 21:17:59 +0000213};
214
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000215class LinkerScript {
George Rimar2d262102017-03-14 09:03:53 +0000216protected:
Rui Ueyamad379f732017-04-05 03:20:22 +0000217 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
218 void setDot(Expr E, const Twine &Loc, bool InSec);
George Rimara2a1ef12017-03-14 12:03:34 +0000219
220 std::vector<InputSectionBase *>
Rui Ueyama72e107f2017-04-05 02:05:48 +0000221 computeInputSections(const InputSectionDescription *);
222
223 std::vector<InputSectionBase *>
George Rimara2a1ef12017-03-14 12:03:34 +0000224 createInputSectionList(OutputSectionCommand &Cmd);
225
226 std::vector<size_t> getPhdrIndices(StringRef SectionName);
227 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
228
229 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd, OutputSection *Sec);
230
231 void switchTo(OutputSection *Sec);
232 void flush();
233 void output(InputSection *Sec);
234 void process(BaseCommand &Base);
235
George Rimar2d262102017-03-14 09:03:53 +0000236 OutputSection *Aether;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000237 bool ErrorOnMissingSection = false;
George Rimar2d262102017-03-14 09:03:53 +0000238
Rui Ueyama98e55de2017-03-16 21:50:30 +0000239 uint64_t Dot;
George Rimar0c1c8082017-03-14 10:00:19 +0000240 uint64_t ThreadBssOffset = 0;
241
George Rimara2a1ef12017-03-14 12:03:34 +0000242 std::function<uint64_t()> LMAOffset;
243 OutputSection *CurOutSec = nullptr;
244 MemoryRegion *CurMemRegion = nullptr;
245
246 llvm::DenseSet<OutputSection *> AlreadyOutputOS;
247 llvm::DenseSet<InputSectionBase *> AlreadyOutputIS;
248
George Rimar2d262102017-03-14 09:03:53 +0000249public:
250 bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); }
Rui Ueyama98e55de2017-03-16 21:50:30 +0000251 uint64_t getDot() { return Dot; }
George Rimar851dc1e2017-03-14 10:15:53 +0000252 OutputSection *getOutputSection(const Twine &Loc, StringRef S);
George Rimard83ce1b2017-03-14 10:24:47 +0000253 uint64_t getOutputSectionSize(StringRef S);
George Rimar503206c2017-03-15 15:42:44 +0000254 void discard(ArrayRef<InputSectionBase *> V);
George Rimar2d262102017-03-14 09:03:53 +0000255
George Rimara8dba482017-03-20 10:09:58 +0000256 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
257 bool isDefined(StringRef S);
George Rimar851dc1e2017-03-14 10:15:53 +0000258
Rui Ueyama98e55de2017-03-16 21:50:30 +0000259 std::vector<OutputSection *> *OutputSections;
Rui Ueyama02a036f2017-02-27 02:31:48 +0000260 void addOrphanSections(OutputSectionFactory &Factory);
Rafael Espindola07fe6122016-11-14 14:23:35 +0000261 void removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000262 void adjustSectionsBeforeSorting();
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000263 void adjustSectionsAfterSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000264
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000265 std::vector<PhdrEntry> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000266 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000267
Rui Ueyama16068ae2016-11-19 18:05:56 +0000268 uint32_t getFiller(StringRef Name);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000269 bool hasLMA(StringRef Name);
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000270 bool shouldKeep(InputSectionBase *S);
Rafael Espindolad3190792016-09-16 15:10:23 +0000271 void assignOffsets(OutputSectionCommand *Cmd);
Rafael Espindola337f9032016-11-14 14:13:32 +0000272 void placeOrphanSections();
Petr Hosek02ad5162017-03-15 03:33:23 +0000273 void processNonSectionCommands();
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000274 void assignAddresses(std::vector<PhdrEntry> &Phdrs);
George Rimara2a1ef12017-03-14 12:03:34 +0000275 int getSectionIndex(StringRef Name);
George Rimara2a1ef12017-03-14 12:03:34 +0000276
277 void writeDataBytes(StringRef Name, uint8_t *Buf);
278 void addSymbol(SymbolAssignment *Cmd);
George Rimara2a1ef12017-03-14 12:03:34 +0000279 void processCommands(OutputSectionFactory &Factory);
Rui Ueyamaa34da932017-03-21 23:03:09 +0000280
281 // Parsed linker script configurations are set to this struct.
282 ScriptConfiguration Opt;
Rui Ueyama07320e42016-04-20 20:13:41 +0000283};
284
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000285extern LinkerScript *Script;
George Rimar884e7862016-09-08 08:19:13 +0000286
Eugene Zelenko22886a22016-11-05 01:00:56 +0000287} // end namespace elf
288} // end namespace lld
Rui Ueyama717677a2016-02-11 21:17:59 +0000289
Eugene Zelenko22886a22016-11-05 01:00:56 +0000290#endif // LLD_ELF_LINKER_SCRIPT_H