blob: 6d128b42e5a8317e3ed604db50f3ba2780c34ccd [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"
17#include "llvm/ADT/DenseMap.h"
Rafael Espindolad3190792016-09-16 15:10:23 +000018#include "llvm/ADT/DenseSet.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000019#include "llvm/ADT/MapVector.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/MemoryBuffer.h"
George Rimarc91930a2016-09-02 21:17:20 +000022#include "llvm/Support/Regex.h"
Rui Ueyama708019c2016-07-24 18:19:40 +000023#include <functional>
Rui Ueyama717677a2016-02-11 21:17:59 +000024
25namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000026namespace elf {
Rafael Espindolae7553e42016-08-31 13:28:33 +000027class DefinedCommon;
George Rimardbb76db2016-08-18 13:00:49 +000028class ScriptParser;
Rui Ueyama8d083e62016-07-29 05:48:39 +000029class SymbolBody;
Eugene Leviante63d81b2016-07-20 14:43:20 +000030template <class ELFT> class InputSectionBase;
Rafael Espindolad3190792016-09-16 15:10:23 +000031template <class ELFT> class InputSection;
Eugene Leviante63d81b2016-07-20 14:43:20 +000032template <class ELFT> class OutputSectionBase;
33template <class ELFT> class OutputSectionFactory;
Eugene Leviant97403d12016-09-01 09:55:57 +000034class InputSectionData;
Rui Ueyama717677a2016-02-11 21:17:59 +000035
Rui Ueyamab04af132016-10-13 23:08:33 +000036// This represents an expression in the linker script.
37// ScriptParser::readExpr reads an expression and returns an Expr.
38// Later, we evaluate the expression by calling the function
39// with the value of special context variable ".".
Rui Ueyama708019c2016-07-24 18:19:40 +000040typedef std::function<uint64_t(uint64_t)> Expr;
41
Rui Ueyama07320e42016-04-20 20:13:41 +000042// Parses a linker script. Calling this function updates
43// Config and ScriptConfig.
44void readLinkerScript(MemoryBufferRef MB);
45
Rui Ueyamab04af132016-10-13 23:08:33 +000046// Parses a version script.
George Rimar20b65982016-08-31 09:08:26 +000047void readVersionScript(MemoryBufferRef MB);
48
George Rimareea31142016-07-21 14:26:59 +000049// This enum is used to implement linker script SECTIONS command.
50// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
51enum SectionsCommandKind {
George Rimare38cbab2016-09-26 19:22:50 +000052 AssignmentKind, // . = expr or <sym> = expr
George Rimareea31142016-07-21 14:26:59 +000053 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000054 InputSectionKind,
George Rimare38cbab2016-09-26 19:22:50 +000055 AssertKind, // ASSERT(expr)
56 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000057};
58
George Rimar076fe152016-07-21 06:43:01 +000059struct BaseCommand {
60 BaseCommand(int K) : Kind(K) {}
61 virtual ~BaseCommand() {}
62 int Kind;
63};
64
Rui Ueyamab04af132016-10-13 23:08:33 +000065// This represents ". = <expr>" or "<symbol> = <expr>".
George Rimar076fe152016-07-21 06:43:01 +000066struct SymbolAssignment : BaseCommand {
Eugene Leviantdb741e72016-09-07 07:08:43 +000067 SymbolAssignment(StringRef Name, Expr E, bool IsAbsolute)
68 : BaseCommand(AssignmentKind), Name(Name), Expression(E),
69 IsAbsolute(IsAbsolute) {}
George Rimar076fe152016-07-21 06:43:01 +000070 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000071
72 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000073 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000074 SymbolBody *Sym = nullptr;
75
76 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000077 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000078
79 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000080 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000081 bool Hidden = false;
Eugene Leviantdb741e72016-09-07 07:08:43 +000082 bool IsAbsolute;
George Rimar076fe152016-07-21 06:43:01 +000083};
84
Davide Italiano246f6812016-07-22 03:36:24 +000085// Linker scripts allow additional constraints to be put on ouput sections.
Rui Ueyamab04af132016-10-13 23:08:33 +000086// If an output section is marked as ONLY_IF_RO, the section is created
87// only if its input sections are read-only. Likewise, an output section
88// with ONLY_IF_RW is created if all input sections are RW.
Rui Ueyamaefc40662016-07-25 22:00:10 +000089enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000090
George Rimar076fe152016-07-21 06:43:01 +000091struct OutputSectionCommand : BaseCommand {
92 OutputSectionCommand(StringRef Name)
93 : BaseCommand(OutputSectionKind), Name(Name) {}
94 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000095 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000096 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000097 Expr AlignExpr;
Eugene Leviantb71d6f72016-10-06 09:39:28 +000098 Expr LMAExpr;
George Rimardb24d9c2016-08-19 15:18:23 +000099 Expr SubalignExpr;
George Rimareea31142016-07-21 14:26:59 +0000100 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000101 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +0000102 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +0000103 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000104};
105
George Rimar8034d492016-09-17 07:31:49 +0000106// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000107// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000108// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000109struct SectionPattern {
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000110 SectionPattern(llvm::Regex &&Re1, llvm::Regex &&Re2)
111 : ExcludedFileRe(std::forward<llvm::Regex>(Re1)),
112 SectionRe(std::forward<llvm::Regex>(Re2)) {}
113
114 SectionPattern(SectionPattern &&Other) {
115 std::swap(ExcludedFileRe, Other.ExcludedFileRe);
116 std::swap(SectionRe, Other.SectionRe);
George Rimar07171f22016-09-21 15:56:44 +0000117 std::swap(SortOuter, Other.SortOuter);
118 std::swap(SortInner, Other.SortInner);
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000119 }
120
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000121 llvm::Regex ExcludedFileRe;
122 llvm::Regex SectionRe;
George Rimar07171f22016-09-21 15:56:44 +0000123 SortSectionPolicy SortOuter;
124 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000125};
126
George Rimareea31142016-07-21 14:26:59 +0000127struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000128 InputSectionDescription(StringRef FilePattern)
129 : BaseCommand(InputSectionKind),
130 FileRe(compileGlobPatterns({FilePattern})) {}
George Rimareea31142016-07-21 14:26:59 +0000131 static bool classof(const BaseCommand *C);
George Rimarc91930a2016-09-02 21:17:20 +0000132 llvm::Regex FileRe;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000133
George Rimar8034d492016-09-17 07:31:49 +0000134 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000135 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000136 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000137
Rafael Espindolad3190792016-09-16 15:10:23 +0000138 std::vector<InputSectionData *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000139};
140
Rui Ueyamab04af132016-10-13 23:08:33 +0000141// Represents an ASSERT().
George Rimareefa7582016-08-04 09:29:31 +0000142struct AssertCommand : BaseCommand {
143 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
144 static bool classof(const BaseCommand *C);
145 Expr Expression;
146};
147
Rui Ueyamab04af132016-10-13 23:08:33 +0000148// Represents BYTE(), SHORT(), LONG(), or QUAD().
George Rimare38cbab2016-09-26 19:22:50 +0000149struct BytesDataCommand : BaseCommand {
150 BytesDataCommand(uint64_t Data, unsigned Size)
151 : BaseCommand(BytesDataKind), Data(Data), Size(Size) {}
152 static bool classof(const BaseCommand *C);
153 uint64_t Data;
154 unsigned Offset;
155 unsigned Size;
156};
157
Eugene Leviantbbe38602016-07-19 09:25:43 +0000158struct PhdrsCommand {
159 StringRef Name;
160 unsigned Type;
161 bool HasFilehdr;
162 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000163 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000164 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000165};
166
George Rimar884e7862016-09-08 08:19:13 +0000167class LinkerScriptBase {
Rafael Espindola4d1e4d72016-09-08 14:11:08 +0000168protected:
169 ~LinkerScriptBase() = default;
170
George Rimar884e7862016-09-08 08:19:13 +0000171public:
172 virtual uint64_t getOutputSectionAddress(StringRef Name) = 0;
173 virtual uint64_t getOutputSectionSize(StringRef Name) = 0;
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000174 virtual uint64_t getOutputSectionAlign(StringRef Name) = 0;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000175 virtual uint64_t getOutputSectionLMA(StringRef Name) = 0;
George Rimar884e7862016-09-08 08:19:13 +0000176 virtual uint64_t getHeaderSize() = 0;
177 virtual uint64_t getSymbolValue(StringRef S) = 0;
George Rimarf34f45f2016-09-23 13:17:23 +0000178 virtual bool isDefined(StringRef S) = 0;
George Rimar884e7862016-09-08 08:19:13 +0000179};
180
Rui Ueyama07320e42016-04-20 20:13:41 +0000181// ScriptConfiguration holds linker script parse results.
182struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000183 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000184 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000185
Eugene Leviantbbe38602016-07-19 09:25:43 +0000186 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000187 std::vector<PhdrsCommand> PhdrsCommands;
188
Eugene Leviante05336ff2016-09-14 08:32:36 +0000189 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000190
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000191 // List of section patterns specified with KEEP commands. They will
192 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000193 std::vector<InputSectionDescription *> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000194};
195
Rui Ueyama07320e42016-04-20 20:13:41 +0000196extern ScriptConfiguration *ScriptConfig;
197
198// This is a runner of the linker script.
George Rimar884e7862016-09-08 08:19:13 +0000199template <class ELFT> class LinkerScript final : public LinkerScriptBase {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000200 typedef typename ELFT::uint uintX_t;
201
Rui Ueyama07320e42016-04-20 20:13:41 +0000202public:
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000203 LinkerScript();
204 ~LinkerScript();
Eugene Leviant20d03192016-09-16 15:30:47 +0000205 void processCommands(OutputSectionFactory<ELFT> &Factory);
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000206 void createSections(OutputSectionFactory<ELFT> &Factory);
Rafael Espindola9546fff2016-09-22 14:40:50 +0000207 void adjustSectionsBeforeSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000208
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000209 std::vector<PhdrEntry<ELFT>> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000210 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000211
Rui Ueyama07320e42016-04-20 20:13:41 +0000212 ArrayRef<uint8_t> getFiller(StringRef Name);
George Rimare38cbab2016-09-26 19:22:50 +0000213 void writeDataBytes(StringRef Name, uint8_t *Buf);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000214 bool hasLMA(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000215 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolad3190792016-09-16 15:10:23 +0000216 void assignOffsets(OutputSectionCommand *Cmd);
Rafael Espindola6d91fce2016-09-29 18:50:34 +0000217 void assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000218 bool hasPhdrsCommands();
George Rimar884e7862016-09-08 08:19:13 +0000219 uint64_t getOutputSectionAddress(StringRef Name) override;
220 uint64_t getOutputSectionSize(StringRef Name) override;
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000221 uint64_t getOutputSectionAlign(StringRef Name) override;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000222 uint64_t getOutputSectionLMA(StringRef Name) override;
George Rimar884e7862016-09-08 08:19:13 +0000223 uint64_t getHeaderSize() override;
224 uint64_t getSymbolValue(StringRef S) override;
George Rimarf34f45f2016-09-23 13:17:23 +0000225 bool isDefined(StringRef S) override;
Rui Ueyama07320e42016-04-20 20:13:41 +0000226
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000227 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
228
Rafael Espindolab6b8f6c2016-09-20 22:43:15 +0000229 int getSectionIndex(StringRef Name);
230
Rui Ueyama07320e42016-04-20 20:13:41 +0000231private:
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000232 void computeInputSections(InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000233
Eugene Leviant20d03192016-09-16 15:30:47 +0000234 void addSection(OutputSectionFactory<ELFT> &Factory,
235 InputSectionBase<ELFT> *Sec, StringRef Name);
Rafael Espindola7bd37872016-09-12 16:05:16 +0000236 void discard(ArrayRef<InputSectionBase<ELFT> *> V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000237
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000238 std::vector<InputSectionBase<ELFT> *>
239 createInputSectionList(OutputSectionCommand &Cmd);
240
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000241 // "ScriptConfig" is a bit too long, so define a short name for it.
242 ScriptConfiguration &Opt = *ScriptConfig;
243
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000244 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000245 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000246
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000247 uintX_t Dot;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000248 uintX_t LMAOffset = 0;
Rafael Espindolad3190792016-09-16 15:10:23 +0000249 OutputSectionBase<ELFT> *CurOutSec = nullptr;
250 uintX_t ThreadBssOffset = 0;
251 void switchTo(OutputSectionBase<ELFT> *Sec);
252 void flush();
253 void output(InputSection<ELFT> *Sec);
254 void process(BaseCommand &Base);
255 llvm::DenseSet<OutputSectionBase<ELFT> *> AlreadyOutputOS;
256 llvm::DenseSet<InputSectionData *> AlreadyOutputIS;
Rui Ueyama07320e42016-04-20 20:13:41 +0000257};
258
259// Variable template is a C++14 feature, so we can't template
260// a global variable. Use a struct to workaround.
261template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
262template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000263
George Rimar884e7862016-09-08 08:19:13 +0000264extern LinkerScriptBase *ScriptBase;
265
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000266} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000267} // namespace lld
268
269#endif