blob: 00e4f9ec96ea1c6601f6879361b923ec3989de93 [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;
George Rimardbb76db2016-08-18 13:00:49 +000032class ScriptParser;
Rui Ueyama8d083e62016-07-29 05:48:39 +000033class SymbolBody;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000034class InputSectionBase;
Rafael Espindola774ea7d2017-02-23 16:49:07 +000035class InputSection;
Rafael Espindola24e6f362017-02-24 15:07:30 +000036class OutputSection;
Rui Ueyama02a036f2017-02-27 02:31:48 +000037class OutputSectionFactory;
Rafael Espindolac404d502017-02-23 02:32:18 +000038class InputSectionBase;
Rafael Espindola9bd45662017-03-10 00:47:33 +000039class SectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000040
Rafael Espindola72dc1952017-03-17 13:05:04 +000041struct ExprValue {
42 SectionBase *Sec;
43 uint64_t Val;
44 bool ForceAbsolute;
45
46 ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val)
47 : Sec(Sec), Val(Val), ForceAbsolute(ForceAbsolute) {}
48 ExprValue(SectionBase *Sec, uint64_t Val) : ExprValue(Sec, false, Val) {}
49 ExprValue(uint64_t Val) : ExprValue(nullptr, Val) {}
50 bool isAbsolute() const { return ForceAbsolute || Sec == nullptr; }
51 uint64_t getValue() const;
Rafael Espindola7ba5f472017-03-17 14:55:36 +000052 uint64_t getSecAddr() const;
Rafael Espindola72dc1952017-03-17 13:05:04 +000053};
54
Rui Ueyamab04af132016-10-13 23:08:33 +000055// This represents an expression in the linker script.
56// ScriptParser::readExpr reads an expression and returns an Expr.
Rafael Espindola72dc1952017-03-17 13:05:04 +000057// Later, we evaluate the expression by calling the function.
58typedef std::function<ExprValue()> Expr;
Rui Ueyama708019c2016-07-24 18:19:40 +000059
Rui Ueyama07320e42016-04-20 20:13:41 +000060// Parses a linker script. Calling this function updates
61// Config and ScriptConfig.
62void readLinkerScript(MemoryBufferRef MB);
63
Rui Ueyamab04af132016-10-13 23:08:33 +000064// Parses a version script.
George Rimar20b65982016-08-31 09:08:26 +000065void readVersionScript(MemoryBufferRef MB);
66
Rafael Espindolad0ebd842016-12-08 17:54:26 +000067void readDynamicList(MemoryBufferRef MB);
68
George Rimareea31142016-07-21 14:26:59 +000069// This enum is used to implement linker script SECTIONS command.
70// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
71enum SectionsCommandKind {
George Rimare38cbab2016-09-26 19:22:50 +000072 AssignmentKind, // . = expr or <sym> = expr
George Rimareea31142016-07-21 14:26:59 +000073 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000074 InputSectionKind,
George Rimare38cbab2016-09-26 19:22:50 +000075 AssertKind, // ASSERT(expr)
76 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000077};
78
George Rimar076fe152016-07-21 06:43:01 +000079struct BaseCommand {
80 BaseCommand(int K) : Kind(K) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000081
82 virtual ~BaseCommand() = default;
83
George Rimar076fe152016-07-21 06:43:01 +000084 int Kind;
85};
86
Rui Ueyamab04af132016-10-13 23:08:33 +000087// This represents ". = <expr>" or "<symbol> = <expr>".
George Rimar076fe152016-07-21 06:43:01 +000088struct SymbolAssignment : BaseCommand {
George Rimara8d8dcf2017-02-22 09:13:04 +000089 SymbolAssignment(StringRef Name, Expr E, std::string Loc)
90 : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000091
George Rimar076fe152016-07-21 06:43:01 +000092 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000093
94 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000095 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000096 SymbolBody *Sym = nullptr;
97
98 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000099 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +0000100
101 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +0000102 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +0000103 bool Hidden = false;
George Rimar2ee2d2d2017-02-21 14:50:38 +0000104
George Rimara8d8dcf2017-02-22 09:13:04 +0000105 // Holds file name and line number for error reporting.
George Rimar2ee2d2d2017-02-21 14:50:38 +0000106 std::string Location;
George Rimar076fe152016-07-21 06:43:01 +0000107};
108
Davide Italiano246f6812016-07-22 03:36:24 +0000109// Linker scripts allow additional constraints to be put on ouput sections.
Rui Ueyamab04af132016-10-13 23:08:33 +0000110// If an output section is marked as ONLY_IF_RO, the section is created
111// only if its input sections are read-only. Likewise, an output section
112// with ONLY_IF_RW is created if all input sections are RW.
Rui Ueyamaefc40662016-07-25 22:00:10 +0000113enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +0000114
George Rimar076fe152016-07-21 06:43:01 +0000115struct OutputSectionCommand : BaseCommand {
116 OutputSectionCommand(StringRef Name)
117 : BaseCommand(OutputSectionKind), Name(Name) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000118
George Rimar076fe152016-07-21 06:43:01 +0000119 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000120
Eugene Levianteda81a12016-07-12 06:39:48 +0000121 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +0000122 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +0000123 Expr AlignExpr;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000124 Expr LMAExpr;
George Rimardb24d9c2016-08-19 15:18:23 +0000125 Expr SubalignExpr;
George Rimareea31142016-07-21 14:26:59 +0000126 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000127 std::vector<StringRef> Phdrs;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000128 uint32_t Filler = 0;
Rui Ueyamaefc40662016-07-25 22:00:10 +0000129 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviant2a942c42016-12-05 16:38:32 +0000130 std::string Location;
Meador Ingeb8897442017-01-24 02:34:00 +0000131 std::string MemoryRegionName;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000132};
133
George Rimar8034d492016-09-17 07:31:49 +0000134// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000135// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000136// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000137struct SectionPattern {
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000138 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
139 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000140
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000141 StringMatcher ExcludedFilePat;
142 StringMatcher SectionPat;
George Rimar07171f22016-09-21 15:56:44 +0000143 SortSectionPolicy SortOuter;
144 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000145};
146
George Rimareea31142016-07-21 14:26:59 +0000147struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000148 InputSectionDescription(StringRef FilePattern)
Vitaly Buka0b7de062016-12-21 02:27:14 +0000149 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000150
George Rimareea31142016-07-21 14:26:59 +0000151 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000152
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000153 StringMatcher FilePat;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000154
George Rimar8034d492016-09-17 07:31:49 +0000155 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000156 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000157 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000158
Rafael Espindolac404d502017-02-23 02:32:18 +0000159 std::vector<InputSectionBase *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000160};
161
Rui Ueyamab04af132016-10-13 23:08:33 +0000162// Represents an ASSERT().
George Rimareefa7582016-08-04 09:29:31 +0000163struct AssertCommand : BaseCommand {
164 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000165
George Rimareefa7582016-08-04 09:29:31 +0000166 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000167
George Rimareefa7582016-08-04 09:29:31 +0000168 Expr Expression;
169};
170
Rui Ueyamab04af132016-10-13 23:08:33 +0000171// Represents BYTE(), SHORT(), LONG(), or QUAD().
George Rimare38cbab2016-09-26 19:22:50 +0000172struct BytesDataCommand : BaseCommand {
Meador Inge95c7d8d2016-12-08 23:21:30 +0000173 BytesDataCommand(Expr E, unsigned Size)
174 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000175
George Rimare38cbab2016-09-26 19:22:50 +0000176 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000177
Meador Inge95c7d8d2016-12-08 23:21:30 +0000178 Expr Expression;
George Rimare38cbab2016-09-26 19:22:50 +0000179 unsigned Offset;
180 unsigned Size;
181};
182
Eugene Leviantbbe38602016-07-19 09:25:43 +0000183struct PhdrsCommand {
184 StringRef Name;
185 unsigned Type;
186 bool HasFilehdr;
187 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000188 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000189 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000190};
191
Meador Ingeb8897442017-01-24 02:34:00 +0000192// This struct is used to represent the location and size of regions of
193// target memory. Instances of the struct are created by parsing the
194// MEMORY command.
195struct MemoryRegion {
196 std::string Name;
197 uint64_t Origin;
198 uint64_t Length;
199 uint64_t Offset;
200 uint32_t Flags;
Rui Ueyama8a8a9532017-01-26 02:58:59 +0000201 uint32_t NegFlags;
Meador Ingeb8897442017-01-24 02:34:00 +0000202};
203
Rui Ueyama07320e42016-04-20 20:13:41 +0000204// ScriptConfiguration holds linker script parse results.
205struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000206 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000207 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000208
Eugene Leviantbbe38602016-07-19 09:25:43 +0000209 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000210 std::vector<PhdrsCommand> PhdrsCommands;
211
Eugene Leviante05336ff2016-09-14 08:32:36 +0000212 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000213
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000214 // List of section patterns specified with KEEP commands. They will
215 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000216 std::vector<InputSectionDescription *> KeptSections;
Meador Ingeb8897442017-01-24 02:34:00 +0000217
218 // A map from memory region name to a memory region descriptor.
219 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
Rui Ueyama717677a2016-02-11 21:17:59 +0000220};
221
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000222class LinkerScript {
George Rimar2d262102017-03-14 09:03:53 +0000223protected:
George Rimara2a1ef12017-03-14 12:03:34 +0000224 void assignSymbol(SymbolAssignment *Cmd, bool InSec = false);
225 void computeInputSections(InputSectionDescription *);
226 void setDot(Expr E, const Twine &Loc, bool InSec = false);
227
228 std::vector<InputSectionBase *>
229 createInputSectionList(OutputSectionCommand &Cmd);
230
231 std::vector<size_t> getPhdrIndices(StringRef SectionName);
232 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
233
234 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd, OutputSection *Sec);
235
236 void switchTo(OutputSection *Sec);
237 void flush();
238 void output(InputSection *Sec);
239 void process(BaseCommand &Base);
240
George Rimar2d262102017-03-14 09:03:53 +0000241 OutputSection *Aether;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000242 bool ErrorOnMissingSection = false;
George Rimar2d262102017-03-14 09:03:53 +0000243
Rui Ueyama98e55de2017-03-16 21:50:30 +0000244 uint64_t Dot;
George Rimar0c1c8082017-03-14 10:00:19 +0000245 uint64_t ThreadBssOffset = 0;
246
George Rimara2a1ef12017-03-14 12:03:34 +0000247 std::function<uint64_t()> LMAOffset;
248 OutputSection *CurOutSec = nullptr;
249 MemoryRegion *CurMemRegion = nullptr;
250
251 llvm::DenseSet<OutputSection *> AlreadyOutputOS;
252 llvm::DenseSet<InputSectionBase *> AlreadyOutputIS;
253
George Rimar2d262102017-03-14 09:03:53 +0000254public:
255 bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); }
Rui Ueyama98e55de2017-03-16 21:50:30 +0000256 uint64_t getDot() { return Dot; }
George Rimar851dc1e2017-03-14 10:15:53 +0000257 OutputSection *getOutputSection(const Twine &Loc, StringRef S);
George Rimard83ce1b2017-03-14 10:24:47 +0000258 uint64_t getOutputSectionSize(StringRef S);
George Rimar503206c2017-03-15 15:42:44 +0000259 void discard(ArrayRef<InputSectionBase *> V);
George Rimar2d262102017-03-14 09:03:53 +0000260
George Rimara8dba482017-03-20 10:09:58 +0000261 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
262 bool isDefined(StringRef S);
George Rimar851dc1e2017-03-14 10:15:53 +0000263
Rui Ueyama98e55de2017-03-16 21:50:30 +0000264 std::vector<OutputSection *> *OutputSections;
Rui Ueyama02a036f2017-02-27 02:31:48 +0000265 void addOrphanSections(OutputSectionFactory &Factory);
Rafael Espindola07fe6122016-11-14 14:23:35 +0000266 void removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000267 void adjustSectionsBeforeSorting();
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000268 void adjustSectionsAfterSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000269
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000270 std::vector<PhdrEntry> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000271 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000272
Rui Ueyama16068ae2016-11-19 18:05:56 +0000273 uint32_t getFiller(StringRef Name);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000274 bool hasLMA(StringRef Name);
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000275 bool shouldKeep(InputSectionBase *S);
Rafael Espindolad3190792016-09-16 15:10:23 +0000276 void assignOffsets(OutputSectionCommand *Cmd);
Rafael Espindola337f9032016-11-14 14:13:32 +0000277 void placeOrphanSections();
Petr Hosek02ad5162017-03-15 03:33:23 +0000278 void processNonSectionCommands();
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000279 void assignAddresses(std::vector<PhdrEntry> &Phdrs);
George Rimara2a1ef12017-03-14 12:03:34 +0000280 int getSectionIndex(StringRef Name);
George Rimara2a1ef12017-03-14 12:03:34 +0000281
282 void writeDataBytes(StringRef Name, uint8_t *Buf);
283 void addSymbol(SymbolAssignment *Cmd);
George Rimara2a1ef12017-03-14 12:03:34 +0000284 void processCommands(OutputSectionFactory &Factory);
Rui Ueyamaa34da932017-03-21 23:03:09 +0000285
286 // Parsed linker script configurations are set to this struct.
287 ScriptConfiguration Opt;
Rui Ueyama07320e42016-04-20 20:13:41 +0000288};
289
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000290extern LinkerScript *Script;
George Rimar884e7862016-09-08 08:19:13 +0000291
Eugene Zelenko22886a22016-11-05 01:00:56 +0000292} // end namespace elf
293} // end namespace lld
Rui Ueyama717677a2016-02-11 21:17:59 +0000294
Eugene Zelenko22886a22016-11-05 01:00:56 +0000295#endif // LLD_ELF_LINKER_SCRIPT_H