blob: a377af34ed3e6381aadc41779117ea0251ff9619 [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"
Rafael Espindolad3190792016-09-16 15:10:23 +000018#include "llvm/ADT/DenseSet.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000019#include "llvm/ADT/StringRef.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000020#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000021#include <cstddef>
22#include <cstdint>
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +000023#include <functional>
Eugene Zelenko22886a22016-11-05 01:00:56 +000024#include <memory>
25#include <vector>
Rui Ueyama717677a2016-02-11 21:17:59 +000026
27namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000028namespace elf {
Eugene Zelenko22886a22016-11-05 01:00:56 +000029
Rafael Espindolae7553e42016-08-31 13:28:33 +000030class DefinedCommon;
George Rimardbb76db2016-08-18 13:00:49 +000031class ScriptParser;
Rui Ueyama8d083e62016-07-29 05:48:39 +000032class SymbolBody;
Eugene Leviante63d81b2016-07-20 14:43:20 +000033template <class ELFT> class InputSectionBase;
Rafael Espindolad3190792016-09-16 15:10:23 +000034template <class ELFT> class InputSection;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000035class OutputSectionBase;
Eugene Leviante63d81b2016-07-20 14:43:20 +000036template <class ELFT> class OutputSectionFactory;
Eugene Leviant97403d12016-09-01 09:55:57 +000037class InputSectionData;
Rui Ueyama717677a2016-02-11 21:17:59 +000038
Rui Ueyamab04af132016-10-13 23:08:33 +000039// This represents an expression in the linker script.
40// ScriptParser::readExpr reads an expression and returns an Expr.
41// Later, we evaluate the expression by calling the function
42// with the value of special context variable ".".
Rafael Espindolaf6613932016-10-31 17:43:38 +000043struct Expr {
44 std::function<uint64_t(uint64_t)> Val;
Rafael Espindola2f831dc2016-10-31 19:56:37 +000045 std::function<bool()> IsAbsolute;
Eugene Leviantafaa9342016-11-16 09:49:39 +000046
47 // If expression is section-relative the function below is used
48 // to get the output section pointer.
49 std::function<const OutputSectionBase *()> Section;
50
Rafael Espindolaf6613932016-10-31 17:43:38 +000051 uint64_t operator()(uint64_t Dot) const { return Val(Dot); }
52 operator bool() const { return (bool)Val; }
53
Eugene Leviantafaa9342016-11-16 09:49:39 +000054 Expr(std::function<uint64_t(uint64_t)> Val, std::function<bool()> IsAbsolute,
55 std::function<const OutputSectionBase *()> Section)
56 : Val(Val), IsAbsolute(IsAbsolute), Section(Section) {}
57 template <typename T>
Rui Ueyama009d1742016-11-18 06:49:09 +000058 Expr(T V) : Expr(V, [] { return true; }, [] { return nullptr; }) {}
Rafael Espindolaf6613932016-10-31 17:43:38 +000059 Expr() : Expr(nullptr) {}
60};
Rui Ueyama708019c2016-07-24 18:19:40 +000061
Rui Ueyama07320e42016-04-20 20:13:41 +000062// Parses a linker script. Calling this function updates
63// Config and ScriptConfig.
64void readLinkerScript(MemoryBufferRef MB);
65
Rui Ueyamab04af132016-10-13 23:08:33 +000066// Parses a version script.
George Rimar20b65982016-08-31 09:08:26 +000067void readVersionScript(MemoryBufferRef MB);
68
Rafael Espindolad0ebd842016-12-08 17:54:26 +000069void readDynamicList(MemoryBufferRef MB);
70
George Rimareea31142016-07-21 14:26:59 +000071// This enum is used to implement linker script SECTIONS command.
72// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
73enum SectionsCommandKind {
George Rimare38cbab2016-09-26 19:22:50 +000074 AssignmentKind, // . = expr or <sym> = expr
George Rimareea31142016-07-21 14:26:59 +000075 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000076 InputSectionKind,
George Rimare38cbab2016-09-26 19:22:50 +000077 AssertKind, // ASSERT(expr)
78 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000079};
80
George Rimar076fe152016-07-21 06:43:01 +000081struct BaseCommand {
82 BaseCommand(int K) : Kind(K) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000083
84 virtual ~BaseCommand() = default;
85
George Rimar076fe152016-07-21 06:43:01 +000086 int Kind;
87};
88
Rui Ueyamab04af132016-10-13 23:08:33 +000089// This represents ". = <expr>" or "<symbol> = <expr>".
George Rimar076fe152016-07-21 06:43:01 +000090struct SymbolAssignment : BaseCommand {
Rafael Espindolaf6613932016-10-31 17:43:38 +000091 SymbolAssignment(StringRef Name, Expr E)
92 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +000093
George Rimar076fe152016-07-21 06:43:01 +000094 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000095
96 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000097 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000098 SymbolBody *Sym = nullptr;
99
100 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +0000101 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +0000102
103 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +0000104 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +0000105 bool Hidden = false;
George Rimar076fe152016-07-21 06:43:01 +0000106};
107
Davide Italiano246f6812016-07-22 03:36:24 +0000108// Linker scripts allow additional constraints to be put on ouput sections.
Rui Ueyamab04af132016-10-13 23:08:33 +0000109// If an output section is marked as ONLY_IF_RO, the section is created
110// only if its input sections are read-only. Likewise, an output section
111// with ONLY_IF_RW is created if all input sections are RW.
Rui Ueyamaefc40662016-07-25 22:00:10 +0000112enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +0000113
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
Eugene Levianteda81a12016-07-12 06:39:48 +0000120 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +0000121 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +0000122 Expr AlignExpr;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000123 Expr LMAExpr;
George Rimardb24d9c2016-08-19 15:18:23 +0000124 Expr SubalignExpr;
George Rimareea31142016-07-21 14:26:59 +0000125 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000126 std::vector<StringRef> Phdrs;
Rui Ueyama16068ae2016-11-19 18:05:56 +0000127 uint32_t Filler = 0;
Rui Ueyamaefc40662016-07-25 22:00:10 +0000128 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviant2a942c42016-12-05 16:38:32 +0000129 std::string Location;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000130};
131
George Rimar8034d492016-09-17 07:31:49 +0000132// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000133// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000134// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000135struct SectionPattern {
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000136 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
137 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000138
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000139 StringMatcher ExcludedFilePat;
140 StringMatcher SectionPat;
George Rimar07171f22016-09-21 15:56:44 +0000141 SortSectionPolicy SortOuter;
142 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000143};
144
George Rimareea31142016-07-21 14:26:59 +0000145struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000146 InputSectionDescription(StringRef FilePattern)
Vitaly Buka0b7de062016-12-21 02:27:14 +0000147 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000148
George Rimareea31142016-07-21 14:26:59 +0000149 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000150
Rui Ueyamaf91282e2016-11-03 17:57:38 +0000151 StringMatcher FilePat;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000152
George Rimar8034d492016-09-17 07:31:49 +0000153 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000154 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000155 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000156
Rafael Espindolad3190792016-09-16 15:10:23 +0000157 std::vector<InputSectionData *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000158};
159
Rui Ueyamab04af132016-10-13 23:08:33 +0000160// Represents an ASSERT().
George Rimareefa7582016-08-04 09:29:31 +0000161struct AssertCommand : BaseCommand {
162 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000163
George Rimareefa7582016-08-04 09:29:31 +0000164 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000165
George Rimareefa7582016-08-04 09:29:31 +0000166 Expr Expression;
167};
168
Rui Ueyamab04af132016-10-13 23:08:33 +0000169// Represents BYTE(), SHORT(), LONG(), or QUAD().
George Rimare38cbab2016-09-26 19:22:50 +0000170struct BytesDataCommand : BaseCommand {
Meador Inge95c7d8d2016-12-08 23:21:30 +0000171 BytesDataCommand(Expr E, unsigned Size)
172 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
Eugene Zelenko22886a22016-11-05 01:00:56 +0000173
George Rimare38cbab2016-09-26 19:22:50 +0000174 static bool classof(const BaseCommand *C);
Eugene Zelenko22886a22016-11-05 01:00:56 +0000175
Meador Inge95c7d8d2016-12-08 23:21:30 +0000176 Expr Expression;
George Rimare38cbab2016-09-26 19:22:50 +0000177 unsigned Offset;
178 unsigned Size;
179};
180
Eugene Leviantbbe38602016-07-19 09:25:43 +0000181struct PhdrsCommand {
182 StringRef Name;
183 unsigned Type;
184 bool HasFilehdr;
185 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000186 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000187 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000188};
189
George Rimar884e7862016-09-08 08:19:13 +0000190class LinkerScriptBase {
Rafael Espindola4d1e4d72016-09-08 14:11:08 +0000191protected:
192 ~LinkerScriptBase() = default;
193
George Rimar884e7862016-09-08 08:19:13 +0000194public:
George Rimar884e7862016-09-08 08:19:13 +0000195 virtual uint64_t getHeaderSize() = 0;
196 virtual uint64_t getSymbolValue(StringRef S) = 0;
George Rimarf34f45f2016-09-23 13:17:23 +0000197 virtual bool isDefined(StringRef S) = 0;
Rafael Espindola2f831dc2016-10-31 19:56:37 +0000198 virtual bool isAbsolute(StringRef S) = 0;
Eugene Leviantafaa9342016-11-16 09:49:39 +0000199 virtual const OutputSectionBase *getSymbolSection(StringRef S) = 0;
Eugene Levianted30ce72016-11-28 09:58:04 +0000200 virtual const OutputSectionBase *getOutputSection(const Twine &Loc,
201 StringRef S) = 0;
Rui Ueyamaedf75e72016-11-17 20:27:10 +0000202 virtual uint64_t getOutputSectionSize(StringRef S) = 0;
George Rimar884e7862016-09-08 08:19:13 +0000203};
204
Rui Ueyama07320e42016-04-20 20:13:41 +0000205// ScriptConfiguration holds linker script parse results.
206struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000207 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000208 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000209
Eugene Leviantbbe38602016-07-19 09:25:43 +0000210 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000211 std::vector<PhdrsCommand> PhdrsCommands;
212
Eugene Leviante05336ff2016-09-14 08:32:36 +0000213 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000214
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000215 // List of section patterns specified with KEEP commands. They will
216 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000217 std::vector<InputSectionDescription *> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000218};
219
Rui Ueyama07320e42016-04-20 20:13:41 +0000220extern ScriptConfiguration *ScriptConfig;
221
222// This is a runner of the linker script.
George Rimar884e7862016-09-08 08:19:13 +0000223template <class ELFT> class LinkerScript final : public LinkerScriptBase {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000224 typedef typename ELFT::uint uintX_t;
225
Rui Ueyama07320e42016-04-20 20:13:41 +0000226public:
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000227 LinkerScript();
228 ~LinkerScript();
Eugene Zelenko22886a22016-11-05 01:00:56 +0000229
Eugene Leviant20d03192016-09-16 15:30:47 +0000230 void processCommands(OutputSectionFactory<ELFT> &Factory);
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000231 void addOrphanSections(OutputSectionFactory<ELFT> &Factory);
Rafael Espindola07fe6122016-11-14 14:23:35 +0000232 void removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000233 void adjustSectionsBeforeSorting();
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000234 void adjustSectionsAfterSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000235
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000236 std::vector<PhdrEntry> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000237 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000238
Rui Ueyama16068ae2016-11-19 18:05:56 +0000239 uint32_t getFiller(StringRef Name);
George Rimare38cbab2016-09-26 19:22:50 +0000240 void writeDataBytes(StringRef Name, uint8_t *Buf);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000241 bool hasLMA(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000242 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolad3190792016-09-16 15:10:23 +0000243 void assignOffsets(OutputSectionCommand *Cmd);
Rafael Espindola337f9032016-11-14 14:13:32 +0000244 void placeOrphanSections();
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000245 void assignAddresses(std::vector<PhdrEntry> &Phdrs);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000246 bool hasPhdrsCommands();
George Rimar884e7862016-09-08 08:19:13 +0000247 uint64_t getHeaderSize() override;
248 uint64_t getSymbolValue(StringRef S) override;
George Rimarf34f45f2016-09-23 13:17:23 +0000249 bool isDefined(StringRef S) override;
Rafael Espindola2f831dc2016-10-31 19:56:37 +0000250 bool isAbsolute(StringRef S) override;
Eugene Leviantafaa9342016-11-16 09:49:39 +0000251 const OutputSectionBase *getSymbolSection(StringRef S) override;
Eugene Levianted30ce72016-11-28 09:58:04 +0000252 const OutputSectionBase *getOutputSection(const Twine &Loc,
253 StringRef S) override;
Rui Ueyamaedf75e72016-11-17 20:27:10 +0000254 uint64_t getOutputSectionSize(StringRef S) override;
Rui Ueyama07320e42016-04-20 20:13:41 +0000255
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000256 std::vector<OutputSectionBase *> *OutputSections;
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000257
Rafael Espindolab6b8f6c2016-09-20 22:43:15 +0000258 int getSectionIndex(StringRef Name);
259
Rui Ueyama07320e42016-04-20 20:13:41 +0000260private:
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000261 void computeInputSections(InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000262
Eugene Leviant20d03192016-09-16 15:30:47 +0000263 void addSection(OutputSectionFactory<ELFT> &Factory,
264 InputSectionBase<ELFT> *Sec, StringRef Name);
Rafael Espindola7bd37872016-09-12 16:05:16 +0000265 void discard(ArrayRef<InputSectionBase<ELFT> *> V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000266
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000267 std::vector<InputSectionBase<ELFT> *>
268 createInputSectionList(OutputSectionCommand &Cmd);
269
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000270 // "ScriptConfig" is a bit too long, so define a short name for it.
271 ScriptConfiguration &Opt = *ScriptConfig;
272
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000273 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Eugene Leviant2a942c42016-12-05 16:38:32 +0000274 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000275
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000276 uintX_t Dot;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000277 uintX_t LMAOffset = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000278 OutputSectionBase *CurOutSec = nullptr;
Rafael Espindolad3190792016-09-16 15:10:23 +0000279 uintX_t ThreadBssOffset = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000280 void switchTo(OutputSectionBase *Sec);
Rafael Espindolad3190792016-09-16 15:10:23 +0000281 void flush();
282 void output(InputSection<ELFT> *Sec);
283 void process(BaseCommand &Base);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000284 llvm::DenseSet<OutputSectionBase *> AlreadyOutputOS;
Rafael Espindolad3190792016-09-16 15:10:23 +0000285 llvm::DenseSet<InputSectionData *> AlreadyOutputIS;
Rui Ueyama07320e42016-04-20 20:13:41 +0000286};
287
288// Variable template is a C++14 feature, so we can't template
289// a global variable. Use a struct to workaround.
290template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
291template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000292
George Rimar884e7862016-09-08 08:19:13 +0000293extern LinkerScriptBase *ScriptBase;
294
Eugene Zelenko22886a22016-11-05 01:00:56 +0000295} // end namespace elf
296} // end namespace lld
Rui Ueyama717677a2016-02-11 21:17:59 +0000297
Eugene Zelenko22886a22016-11-05 01:00:56 +0000298#endif // LLD_ELF_LINKER_SCRIPT_H