blob: b4ca6535891a80e4315079297aac7c3be70ad8bb [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) {}
George Rimar076fe152016-07-21 06:43:01 +000071 int Kind;
72};
73
Rui Ueyamab04af132016-10-13 23:08:33 +000074// This represents ". = <expr>" or "<symbol> = <expr>".
George Rimar076fe152016-07-21 06:43:01 +000075struct SymbolAssignment : BaseCommand {
George Rimara8d8dcf2017-02-22 09:13:04 +000076 SymbolAssignment(StringRef Name, Expr E, std::string Loc)
77 : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000078
George Rimar076fe152016-07-21 06:43:01 +000079 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000080
81 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000082 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000083 SymbolBody *Sym = nullptr;
84
85 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000086 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000087
88 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000089 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000090 bool Hidden = false;
George Rimar2ee2d2d2017-02-21 14:50:38 +000091
George Rimara8d8dcf2017-02-22 09:13:04 +000092 // Holds file name and line number for error reporting.
George Rimar2ee2d2d2017-02-21 14:50:38 +000093 std::string Location;
George Rimar076fe152016-07-21 06:43:01 +000094};
95
Davide Italiano246f6812016-07-22 03:36:24 +000096// Linker scripts allow additional constraints to be put on ouput sections.
Rui Ueyamab04af132016-10-13 23:08:33 +000097// 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.
Rui Ueyamaefc40662016-07-25 22:00:10 +0000100enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +0000101
Rafael Espindolafeed7502017-04-06 21:31:24 +0000102// 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.
105struct 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
George Rimar076fe152016-07-21 06:43:01 +0000114struct OutputSectionCommand : BaseCommand {
115 OutputSectionCommand(StringRef Name)
116 : BaseCommand(OutputSectionKind), Name(Name) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000117
George Rimar076fe152016-07-21 06:43:01 +0000118 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000119
Rafael Espindola9b980092017-04-06 21:05:39 +0000120 OutputSection *Sec = nullptr;
Rafael Espindolafeed7502017-04-06 21:31:24 +0000121 MemoryRegion *MemRegion = nullptr;
Eugene Levianteda81a12016-07-12 06:39:48 +0000122 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +0000123 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +0000124 Expr AlignExpr;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000125 Expr LMAExpr;
George Rimardb24d9c2016-08-19 15:18:23 +0000126 Expr SubalignExpr;
Rui Ueyama8f99f732017-04-05 03:20:42 +0000127 std::vector<BaseCommand *> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000128 std::vector<StringRef> Phdrs;
James Henderson9d9a6632017-04-07 10:36:42 +0000129 llvm::Optional<uint32_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +0000130 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviant2a942c42016-12-05 16:38:32 +0000131 std::string Location;
Meador Ingeb8897442017-01-24 02:34:00 +0000132 std::string MemoryRegionName;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000133};
134
George Rimar8034d492016-09-17 07:31:49 +0000135// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000136// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000137// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000138struct SectionPattern {
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000139 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
140 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000141
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000142 StringMatcher ExcludedFilePat;
143 StringMatcher SectionPat;
George Rimar07171f22016-09-21 15:56:44 +0000144 SortSectionPolicy SortOuter;
145 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000146};
147
George Rimareea31142016-07-21 14:26:59 +0000148struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000149 InputSectionDescription(StringRef FilePattern)
Vitaly Buka0b7de062016-12-21 02:27:14 +0000150 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000151
George Rimareea31142016-07-21 14:26:59 +0000152 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000153
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000154 StringMatcher FilePat;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000155
George Rimar8034d492016-09-17 07:31:49 +0000156 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000157 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000158 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000159
Rafael Espindolac404d502017-02-23 02:32:18 +0000160 std::vector<InputSectionBase *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000161};
162
Rui Ueyamab04af132016-10-13 23:08:33 +0000163// Represents an ASSERT().
George Rimareefa7582016-08-04 09:29:31 +0000164struct AssertCommand : BaseCommand {
165 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000166
George Rimareefa7582016-08-04 09:29:31 +0000167 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000168
George Rimareefa7582016-08-04 09:29:31 +0000169 Expr Expression;
170};
171
Rui Ueyamab04af132016-10-13 23:08:33 +0000172// Represents BYTE(), SHORT(), LONG(), or QUAD().
George Rimare38cbab2016-09-26 19:22:50 +0000173struct BytesDataCommand : BaseCommand {
Meador Inge95c7d8d2016-12-08 23:21:30 +0000174 BytesDataCommand(Expr E, unsigned Size)
175 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000176
George Rimare38cbab2016-09-26 19:22:50 +0000177 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000178
Meador Inge95c7d8d2016-12-08 23:21:30 +0000179 Expr Expression;
George Rimare38cbab2016-09-26 19:22:50 +0000180 unsigned Offset;
181 unsigned Size;
182};
183
Eugene Leviantbbe38602016-07-19 09:25:43 +0000184struct PhdrsCommand {
185 StringRef Name;
186 unsigned Type;
187 bool HasFilehdr;
188 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000189 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000190 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000191};
192
Rui Ueyama07320e42016-04-20 20:13:41 +0000193// ScriptConfiguration holds linker script parse results.
194struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000195 // Used to assign addresses to sections.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000196 std::vector<BaseCommand *> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000197
Eugene Leviantbbe38602016-07-19 09:25:43 +0000198 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000199 std::vector<PhdrsCommand> PhdrsCommands;
200
Eugene Leviante05336ff2016-09-14 08:32:36 +0000201 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000202
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000203 // List of section patterns specified with KEEP commands. They will
204 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000205 std::vector<InputSectionDescription *> KeptSections;
Meador Ingeb8897442017-01-24 02:34:00 +0000206
207 // A map from memory region name to a memory region descriptor.
208 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
Petr Hosek30f16b22017-03-23 03:52:34 +0000209
Rui Ueyama03fc8d12017-04-05 19:20:54 +0000210 // A list of symbols referenced by the script.
Rui Ueyama4eb2ecc2017-04-05 18:02:30 +0000211 std::vector<llvm::StringRef> ReferencedSymbols;
Rui Ueyama717677a2016-02-11 21:17:59 +0000212};
213
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000214class LinkerScript {
George Rimar2d262102017-03-14 09:03:53 +0000215protected:
Rui Ueyamad379f732017-04-05 03:20:22 +0000216 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
217 void setDot(Expr E, const Twine &Loc, bool InSec);
George Rimara2a1ef12017-03-14 12:03:34 +0000218
219 std::vector<InputSectionBase *>
Rui Ueyama72e107f2017-04-05 02:05:48 +0000220 computeInputSections(const InputSectionDescription *);
221
222 std::vector<InputSectionBase *>
George Rimara2a1ef12017-03-14 12:03:34 +0000223 createInputSectionList(OutputSectionCommand &Cmd);
224
225 std::vector<size_t> getPhdrIndices(StringRef SectionName);
226 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
227
Rafael Espindola1902b332017-04-06 21:26:03 +0000228 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
George Rimara2a1ef12017-03-14 12:03:34 +0000229
230 void switchTo(OutputSection *Sec);
Rafael Espindola7c4eafa2017-05-04 03:00:27 +0000231 uint64_t advance(uint64_t Size, unsigned Align);
George Rimara2a1ef12017-03-14 12:03:34 +0000232 void output(InputSection *Sec);
233 void process(BaseCommand &Base);
234
George Rimar2d262102017-03-14 09:03:53 +0000235 OutputSection *Aether;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000236 bool ErrorOnMissingSection = false;
George Rimar2d262102017-03-14 09:03:53 +0000237
Rui Ueyama98e55de2017-03-16 21:50:30 +0000238 uint64_t Dot;
George Rimar0c1c8082017-03-14 10:00:19 +0000239 uint64_t ThreadBssOffset = 0;
240
George Rimara2a1ef12017-03-14 12:03:34 +0000241 std::function<uint64_t()> LMAOffset;
242 OutputSection *CurOutSec = nullptr;
243 MemoryRegion *CurMemRegion = nullptr;
244
George Rimar2d262102017-03-14 09:03:53 +0000245public:
246 bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); }
Rui Ueyama98e55de2017-03-16 21:50:30 +0000247 uint64_t getDot() { return Dot; }
George Rimar851dc1e2017-03-14 10:15:53 +0000248 OutputSection *getOutputSection(const Twine &Loc, StringRef S);
George Rimard83ce1b2017-03-14 10:24:47 +0000249 uint64_t getOutputSectionSize(StringRef S);
George Rimar503206c2017-03-15 15:42:44 +0000250 void discard(ArrayRef<InputSectionBase *> V);
George Rimar2d262102017-03-14 09:03:53 +0000251
George Rimara8dba482017-03-20 10:09:58 +0000252 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
253 bool isDefined(StringRef S);
George Rimar851dc1e2017-03-14 10:15:53 +0000254
Rui Ueyama98e55de2017-03-16 21:50:30 +0000255 std::vector<OutputSection *> *OutputSections;
Peter Smithcbfe9e92017-04-19 12:46:32 +0000256 void fabricateDefaultCommands(bool AllocateHeader);
Rui Ueyama02a036f2017-02-27 02:31:48 +0000257 void addOrphanSections(OutputSectionFactory &Factory);
Rafael Espindola07fe6122016-11-14 14:23:35 +0000258 void removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000259 void adjustSectionsBeforeSorting();
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000260 void adjustSectionsAfterSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000261
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000262 std::vector<PhdrEntry> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000263 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000264
James Henderson9d9a6632017-04-07 10:36:42 +0000265 llvm::Optional<uint32_t> getFiller(StringRef Name);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000266 bool hasLMA(StringRef Name);
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000267 bool shouldKeep(InputSectionBase *S);
Rafael Espindolad3190792016-09-16 15:10:23 +0000268 void assignOffsets(OutputSectionCommand *Cmd);
Rafael Espindola337f9032016-11-14 14:13:32 +0000269 void placeOrphanSections();
Petr Hosek02ad5162017-03-15 03:33:23 +0000270 void processNonSectionCommands();
Rafael Espindolade8d9892017-04-29 15:44:03 +0000271 void synchronize();
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000272 void assignAddresses(std::vector<PhdrEntry> &Phdrs);
George Rimara2a1ef12017-03-14 12:03:34 +0000273 int getSectionIndex(StringRef Name);
George Rimara2a1ef12017-03-14 12:03:34 +0000274
275 void writeDataBytes(StringRef Name, uint8_t *Buf);
276 void addSymbol(SymbolAssignment *Cmd);
George Rimara2a1ef12017-03-14 12:03:34 +0000277 void processCommands(OutputSectionFactory &Factory);
Rui Ueyamaa34da932017-03-21 23:03:09 +0000278
279 // Parsed linker script configurations are set to this struct.
280 ScriptConfiguration Opt;
Rui Ueyama07320e42016-04-20 20:13:41 +0000281};
282
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000283extern LinkerScript *Script;
George Rimar884e7862016-09-08 08:19:13 +0000284
Eugene Zelenko22886a22016-11-05 01:00:56 +0000285} // end namespace elf
286} // end namespace lld
Rui Ueyama717677a2016-02-11 21:17:59 +0000287
Eugene Zelenko22886a22016-11-05 01:00:56 +0000288#endif // LLD_ELF_LINKER_SCRIPT_H