blob: db9a7792cb85a21b4fb5f0aac1ac724c0d90b52f [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 Ueyama708019c2016-07-24 18:19:40 +000036typedef std::function<uint64_t(uint64_t)> Expr;
37
Rui Ueyama07320e42016-04-20 20:13:41 +000038// Parses a linker script. Calling this function updates
39// Config and ScriptConfig.
40void readLinkerScript(MemoryBufferRef MB);
41
George Rimar20b65982016-08-31 09:08:26 +000042void readVersionScript(MemoryBufferRef MB);
43
George Rimareea31142016-07-21 14:26:59 +000044// This enum is used to implement linker script SECTIONS command.
45// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
46enum SectionsCommandKind {
George Rimare38cbab2016-09-26 19:22:50 +000047 AssignmentKind, // . = expr or <sym> = expr
George Rimareea31142016-07-21 14:26:59 +000048 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000049 InputSectionKind,
George Rimare38cbab2016-09-26 19:22:50 +000050 AssertKind, // ASSERT(expr)
51 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000052};
53
George Rimar076fe152016-07-21 06:43:01 +000054struct BaseCommand {
55 BaseCommand(int K) : Kind(K) {}
56 virtual ~BaseCommand() {}
57 int Kind;
58};
59
60struct SymbolAssignment : BaseCommand {
Eugene Leviantdb741e72016-09-07 07:08:43 +000061 SymbolAssignment(StringRef Name, Expr E, bool IsAbsolute)
62 : BaseCommand(AssignmentKind), Name(Name), Expression(E),
63 IsAbsolute(IsAbsolute) {}
George Rimar076fe152016-07-21 06:43:01 +000064 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000065
66 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000067 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000068 SymbolBody *Sym = nullptr;
69
70 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000071 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000072
73 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000074 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000075 bool Hidden = false;
Eugene Leviantdb741e72016-09-07 07:08:43 +000076 bool IsAbsolute;
George Rimar076fe152016-07-21 06:43:01 +000077};
78
Davide Italiano246f6812016-07-22 03:36:24 +000079// Linker scripts allow additional constraints to be put on ouput sections.
80// An output section will only be created if all of its input sections are
81// read-only
82// or all of its input sections are read-write by using the keyword ONLY_IF_RO
83// and ONLY_IF_RW respectively.
Rui Ueyamaefc40662016-07-25 22:00:10 +000084enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000085
George Rimar076fe152016-07-21 06:43:01 +000086struct OutputSectionCommand : BaseCommand {
87 OutputSectionCommand(StringRef Name)
88 : BaseCommand(OutputSectionKind), Name(Name) {}
89 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000090 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000091 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000092 Expr AlignExpr;
Eugene Leviantb71d6f72016-10-06 09:39:28 +000093 Expr LMAExpr;
George Rimardb24d9c2016-08-19 15:18:23 +000094 Expr SubalignExpr;
George Rimareea31142016-07-21 14:26:59 +000095 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000096 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000097 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000098 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000099};
100
George Rimar8034d492016-09-17 07:31:49 +0000101// This struct represents one section match pattern in SECTIONS() command.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000102// It can optionally have negative match pattern for EXCLUDED_FILE command.
George Rimar07171f22016-09-21 15:56:44 +0000103// Also it may be surrounded with SORT() command, so contains sorting rules.
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000104struct SectionPattern {
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000105 SectionPattern(llvm::Regex &&Re1, llvm::Regex &&Re2)
106 : ExcludedFileRe(std::forward<llvm::Regex>(Re1)),
107 SectionRe(std::forward<llvm::Regex>(Re2)) {}
108
109 SectionPattern(SectionPattern &&Other) {
110 std::swap(ExcludedFileRe, Other.ExcludedFileRe);
111 std::swap(SectionRe, Other.SectionRe);
George Rimar07171f22016-09-21 15:56:44 +0000112 std::swap(SortOuter, Other.SortOuter);
113 std::swap(SortInner, Other.SortInner);
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000114 }
115
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000116 llvm::Regex ExcludedFileRe;
117 llvm::Regex SectionRe;
George Rimar07171f22016-09-21 15:56:44 +0000118 SortSectionPolicy SortOuter;
119 SortSectionPolicy SortInner;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000120};
121
George Rimareea31142016-07-21 14:26:59 +0000122struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000123 InputSectionDescription(StringRef FilePattern)
124 : BaseCommand(InputSectionKind),
125 FileRe(compileGlobPatterns({FilePattern})) {}
George Rimareea31142016-07-21 14:26:59 +0000126 static bool classof(const BaseCommand *C);
George Rimarc91930a2016-09-02 21:17:20 +0000127 llvm::Regex FileRe;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000128
George Rimar8034d492016-09-17 07:31:49 +0000129 // Input sections that matches at least one of SectionPatterns
Rui Ueyama70efa2f2016-09-17 02:34:50 +0000130 // will be associated with this InputSectionDescription.
Rui Ueyamad1d7cfc2016-09-20 00:02:06 +0000131 std::vector<SectionPattern> SectionPatterns;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000132
Rafael Espindolad3190792016-09-16 15:10:23 +0000133 std::vector<InputSectionData *> Sections;
George Rimareea31142016-07-21 14:26:59 +0000134};
135
George Rimareefa7582016-08-04 09:29:31 +0000136struct AssertCommand : BaseCommand {
137 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
138 static bool classof(const BaseCommand *C);
139 Expr Expression;
140};
141
George Rimare38cbab2016-09-26 19:22:50 +0000142struct BytesDataCommand : BaseCommand {
143 BytesDataCommand(uint64_t Data, unsigned Size)
144 : BaseCommand(BytesDataKind), Data(Data), Size(Size) {}
145 static bool classof(const BaseCommand *C);
146 uint64_t Data;
147 unsigned Offset;
148 unsigned Size;
149};
150
Eugene Leviantbbe38602016-07-19 09:25:43 +0000151struct PhdrsCommand {
152 StringRef Name;
153 unsigned Type;
154 bool HasFilehdr;
155 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000156 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000157 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000158};
159
George Rimar884e7862016-09-08 08:19:13 +0000160class LinkerScriptBase {
Rafael Espindola4d1e4d72016-09-08 14:11:08 +0000161protected:
162 ~LinkerScriptBase() = default;
163
George Rimar884e7862016-09-08 08:19:13 +0000164public:
165 virtual uint64_t getOutputSectionAddress(StringRef Name) = 0;
166 virtual uint64_t getOutputSectionSize(StringRef Name) = 0;
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000167 virtual uint64_t getOutputSectionAlign(StringRef Name) = 0;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000168 virtual uint64_t getOutputSectionLMA(StringRef Name) = 0;
George Rimar884e7862016-09-08 08:19:13 +0000169 virtual uint64_t getHeaderSize() = 0;
170 virtual uint64_t getSymbolValue(StringRef S) = 0;
George Rimarf34f45f2016-09-23 13:17:23 +0000171 virtual bool isDefined(StringRef S) = 0;
George Rimar884e7862016-09-08 08:19:13 +0000172};
173
Rui Ueyama07320e42016-04-20 20:13:41 +0000174// ScriptConfiguration holds linker script parse results.
175struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000176 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000177 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000178
Eugene Leviantbbe38602016-07-19 09:25:43 +0000179 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000180 std::vector<PhdrsCommand> PhdrsCommands;
181
Eugene Leviante05336ff2016-09-14 08:32:36 +0000182 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000183
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000184 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000185
186 // List of section patterns specified with KEEP commands. They will
187 // be kept even if they are unused and --gc-sections is specified.
Eugene Leviantcf43f172016-10-05 09:36:59 +0000188 std::vector<InputSectionDescription *> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000189};
190
Rui Ueyama07320e42016-04-20 20:13:41 +0000191extern ScriptConfiguration *ScriptConfig;
192
193// This is a runner of the linker script.
George Rimar884e7862016-09-08 08:19:13 +0000194template <class ELFT> class LinkerScript final : public LinkerScriptBase {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000195 typedef typename ELFT::uint uintX_t;
196
Rui Ueyama07320e42016-04-20 20:13:41 +0000197public:
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000198 LinkerScript();
199 ~LinkerScript();
Eugene Leviant20d03192016-09-16 15:30:47 +0000200 void processCommands(OutputSectionFactory<ELFT> &Factory);
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000201 void createSections(OutputSectionFactory<ELFT> &Factory);
Rafael Espindola9546fff2016-09-22 14:40:50 +0000202 void adjustSectionsBeforeSorting();
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000203
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000204 std::vector<PhdrEntry<ELFT>> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000205 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000206
Rui Ueyama07320e42016-04-20 20:13:41 +0000207 ArrayRef<uint8_t> getFiller(StringRef Name);
George Rimare38cbab2016-09-26 19:22:50 +0000208 void writeDataBytes(StringRef Name, uint8_t *Buf);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000209 bool hasLMA(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000210 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolad3190792016-09-16 15:10:23 +0000211 void assignOffsets(OutputSectionCommand *Cmd);
Rafael Espindola6d91fce2016-09-29 18:50:34 +0000212 void assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000213 bool hasPhdrsCommands();
George Rimar884e7862016-09-08 08:19:13 +0000214 uint64_t getOutputSectionAddress(StringRef Name) override;
215 uint64_t getOutputSectionSize(StringRef Name) override;
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000216 uint64_t getOutputSectionAlign(StringRef Name) override;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000217 uint64_t getOutputSectionLMA(StringRef Name) override;
George Rimar884e7862016-09-08 08:19:13 +0000218 uint64_t getHeaderSize() override;
219 uint64_t getSymbolValue(StringRef S) override;
George Rimarf34f45f2016-09-23 13:17:23 +0000220 bool isDefined(StringRef S) override;
Rui Ueyama07320e42016-04-20 20:13:41 +0000221
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000222 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
223
Rafael Espindolab6b8f6c2016-09-20 22:43:15 +0000224 int getSectionIndex(StringRef Name);
225
Rui Ueyama07320e42016-04-20 20:13:41 +0000226private:
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000227 void computeInputSections(InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000228
Eugene Leviant20d03192016-09-16 15:30:47 +0000229 void addSection(OutputSectionFactory<ELFT> &Factory,
230 InputSectionBase<ELFT> *Sec, StringRef Name);
Rafael Espindola7bd37872016-09-12 16:05:16 +0000231 void discard(ArrayRef<InputSectionBase<ELFT> *> V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000232
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000233 std::vector<InputSectionBase<ELFT> *>
234 createInputSectionList(OutputSectionCommand &Cmd);
235
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000236 // "ScriptConfig" is a bit too long, so define a short name for it.
237 ScriptConfiguration &Opt = *ScriptConfig;
238
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000239 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000240 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000241
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000242 uintX_t Dot;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000243 uintX_t LMAOffset = 0;
Rafael Espindolad3190792016-09-16 15:10:23 +0000244 OutputSectionBase<ELFT> *CurOutSec = nullptr;
245 uintX_t ThreadBssOffset = 0;
246 void switchTo(OutputSectionBase<ELFT> *Sec);
247 void flush();
248 void output(InputSection<ELFT> *Sec);
249 void process(BaseCommand &Base);
250 llvm::DenseSet<OutputSectionBase<ELFT> *> AlreadyOutputOS;
251 llvm::DenseSet<InputSectionData *> AlreadyOutputIS;
Rui Ueyama07320e42016-04-20 20:13:41 +0000252};
253
254// Variable template is a C++14 feature, so we can't template
255// a global variable. Use a struct to workaround.
256template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
257template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000258
George Rimar884e7862016-09-08 08:19:13 +0000259extern LinkerScriptBase *ScriptBase;
260
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000261} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000262} // namespace lld
263
264#endif