blob: e56e569d4e72b286758573f9b9c81193d9d77ec5 [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;
Rafael Espindola55b169b2017-05-24 18:08:04 +0000133
134 template <class ELFT> void writeTo(uint8_t *Buf);
135 uint32_t getFiller();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000136};
137
George Rimar8034d492016-09-17 07:31:49 +0000138// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000139// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000140// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000141struct SectionPattern {
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000142 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
143 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000144
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000145 StringMatcher ExcludedFilePat;
146 StringMatcher SectionPat;
George Rimar07171f22016-09-21 15:56:44 +0000147 SortSectionPolicy SortOuter;
148 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000149};
150
George Rimareea31142016-07-21 14:26:59 +0000151struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000152 InputSectionDescription(StringRef FilePattern)
Vitaly Buka0b7de062016-12-21 02:27:14 +0000153 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000154
George Rimareea31142016-07-21 14:26:59 +0000155 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000156
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000157 StringMatcher FilePat;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000158
George Rimar8034d492016-09-17 07:31:49 +0000159 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000160 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000161 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000162
Rafael Espindola6a1aa8d2017-05-23 22:47:31 +0000163 std::vector<InputSection *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000164};
165
Rui Ueyamab04af132016-10-13 23:08:33 +0000166// Represents an ASSERT().
George Rimareefa7582016-08-04 09:29:31 +0000167struct AssertCommand : BaseCommand {
168 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000169
George Rimareefa7582016-08-04 09:29:31 +0000170 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000171
George Rimareefa7582016-08-04 09:29:31 +0000172 Expr Expression;
173};
174
Rui Ueyamab04af132016-10-13 23:08:33 +0000175// Represents BYTE(), SHORT(), LONG(), or QUAD().
George Rimare38cbab2016-09-26 19:22:50 +0000176struct BytesDataCommand : BaseCommand {
Meador Inge95c7d8d2016-12-08 23:21:30 +0000177 BytesDataCommand(Expr E, unsigned Size)
178 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000179
George Rimare38cbab2016-09-26 19:22:50 +0000180 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000181
Meador Inge95c7d8d2016-12-08 23:21:30 +0000182 Expr Expression;
George Rimare38cbab2016-09-26 19:22:50 +0000183 unsigned Offset;
184 unsigned Size;
185};
186
Eugene Leviantbbe38602016-07-19 09:25:43 +0000187struct PhdrsCommand {
188 StringRef Name;
189 unsigned Type;
190 bool HasFilehdr;
191 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000192 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000193 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000194};
195
Rui Ueyama07320e42016-04-20 20:13:41 +0000196// ScriptConfiguration holds linker script parse results.
197struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000198 // Used to assign addresses to sections.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000199 std::vector<BaseCommand *> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000200
Eugene Leviantbbe38602016-07-19 09:25:43 +0000201 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000202 std::vector<PhdrsCommand> PhdrsCommands;
203
Eugene Leviante05336ff2016-09-14 08:32:36 +0000204 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000205
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000206 // List of section patterns specified with KEEP commands. They will
207 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000208 std::vector<InputSectionDescription *> KeptSections;
Meador Ingeb8897442017-01-24 02:34:00 +0000209
210 // A map from memory region name to a memory region descriptor.
211 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
Petr Hosek30f16b22017-03-23 03:52:34 +0000212
Rui Ueyama03fc8d12017-04-05 19:20:54 +0000213 // A list of symbols referenced by the script.
Rui Ueyama4eb2ecc2017-04-05 18:02:30 +0000214 std::vector<llvm::StringRef> ReferencedSymbols;
Rui Ueyama717677a2016-02-11 21:17:59 +0000215};
216
Rafael Espindolac2dd0bd2017-05-10 14:45:15 +0000217class LinkerScript final {
Rafael Espindolad7dc2252017-05-10 19:13:38 +0000218 llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand;
Rui Ueyamad379f732017-04-05 03:20:22 +0000219 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
220 void setDot(Expr E, const Twine &Loc, bool InSec);
George Rimara2a1ef12017-03-14 12:03:34 +0000221
Rafael Espindola6a1aa8d2017-05-23 22:47:31 +0000222 std::vector<InputSection *>
Rui Ueyama72e107f2017-04-05 02:05:48 +0000223 computeInputSections(const InputSectionDescription *);
224
225 std::vector<InputSectionBase *>
George Rimara2a1ef12017-03-14 12:03:34 +0000226 createInputSectionList(OutputSectionCommand &Cmd);
227
Rafael Espindola2c923c22017-05-10 14:28:31 +0000228 std::vector<size_t> getPhdrIndices(OutputSection *Sec);
George Rimara2a1ef12017-03-14 12:03:34 +0000229 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
230
Rafael Espindola1902b332017-04-06 21:26:03 +0000231 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
George Rimara2a1ef12017-03-14 12:03:34 +0000232
233 void switchTo(OutputSection *Sec);
Rafael Espindola7c4eafa2017-05-04 03:00:27 +0000234 uint64_t advance(uint64_t Size, unsigned Align);
George Rimara2a1ef12017-03-14 12:03:34 +0000235 void output(InputSection *Sec);
236 void process(BaseCommand &Base);
237
George Rimar2d262102017-03-14 09:03:53 +0000238 OutputSection *Aether;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000239 bool ErrorOnMissingSection = false;
George Rimar2d262102017-03-14 09:03:53 +0000240
Rui Ueyama98e55de2017-03-16 21:50:30 +0000241 uint64_t Dot;
George Rimar0c1c8082017-03-14 10:00:19 +0000242 uint64_t ThreadBssOffset = 0;
243
George Rimara2a1ef12017-03-14 12:03:34 +0000244 std::function<uint64_t()> LMAOffset;
245 OutputSection *CurOutSec = nullptr;
246 MemoryRegion *CurMemRegion = nullptr;
247
George Rimar2d262102017-03-14 09:03:53 +0000248public:
Rafael Espindola55b169b2017-05-24 18:08:04 +0000249 OutputSectionCommand *getCmd(OutputSection *Sec) const;
George Rimar2d262102017-03-14 09:03:53 +0000250 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;
Rafael Espindola02ed7572017-05-04 19:34:17 +0000260 void fabricateDefaultCommands();
Rui Ueyama02a036f2017-02-27 02:31:48 +0000261 void addOrphanSections(OutputSectionFactory &Factory);
Rafael Espindola07fe6122016-11-14 14:23:35 +0000262 void removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000263 void adjustSectionsBeforeSorting();
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000264 void adjustSectionsAfterSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000265
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000266 std::vector<PhdrEntry> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000267 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000268
Rafael Espindoladc1ed122017-05-10 14:12:02 +0000269 bool hasLMA(OutputSection *Sec);
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 Espindolade8d9892017-04-29 15:44:03 +0000274 void synchronize();
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000275 void assignAddresses(std::vector<PhdrEntry> &Phdrs);
George Rimara2a1ef12017-03-14 12:03:34 +0000276
George Rimara2a1ef12017-03-14 12:03:34 +0000277 void addSymbol(SymbolAssignment *Cmd);
George Rimara2a1ef12017-03-14 12:03:34 +0000278 void processCommands(OutputSectionFactory &Factory);
Rui Ueyamaa34da932017-03-21 23:03:09 +0000279
280 // Parsed linker script configurations are set to this struct.
281 ScriptConfiguration Opt;
Rui Ueyama07320e42016-04-20 20:13:41 +0000282};
283
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000284extern LinkerScript *Script;
George Rimar884e7862016-09-08 08:19:13 +0000285
Eugene Zelenko22886a22016-11-05 01:00:56 +0000286} // end namespace elf
287} // end namespace lld
Rui Ueyama717677a2016-02-11 21:17:59 +0000288
Eugene Zelenko22886a22016-11-05 01:00:56 +0000289#endif // LLD_ELF_LINKER_SCRIPT_H