blob: f428d8599e216b0e40731348e9d38ae98fcb99b6 [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 Rimarc91930a2016-09-02 21:17:20 +000013#include "Strings.h"
Eugene Leviantbbe38602016-07-19 09:25:43 +000014#include "Writer.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000015#include "lld/Core/LLVM.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/MapVector.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000018#include "llvm/Support/Allocator.h"
19#include "llvm/Support/MemoryBuffer.h"
George Rimarc91930a2016-09-02 21:17:20 +000020#include "llvm/Support/Regex.h"
Rui Ueyama708019c2016-07-24 18:19:40 +000021#include <functional>
Rui Ueyama717677a2016-02-11 21:17:59 +000022
23namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000024namespace elf {
Rafael Espindolae7553e42016-08-31 13:28:33 +000025class DefinedCommon;
George Rimardbb76db2016-08-18 13:00:49 +000026class ScriptParser;
Rui Ueyama8d083e62016-07-29 05:48:39 +000027class SymbolBody;
Eugene Leviante63d81b2016-07-20 14:43:20 +000028template <class ELFT> class InputSectionBase;
29template <class ELFT> class OutputSectionBase;
30template <class ELFT> class OutputSectionFactory;
Eugene Leviant97403d12016-09-01 09:55:57 +000031class InputSectionData;
Rui Ueyama717677a2016-02-11 21:17:59 +000032
Rui Ueyama708019c2016-07-24 18:19:40 +000033typedef std::function<uint64_t(uint64_t)> Expr;
34
Rui Ueyama07320e42016-04-20 20:13:41 +000035// Parses a linker script. Calling this function updates
36// Config and ScriptConfig.
37void readLinkerScript(MemoryBufferRef MB);
38
George Rimar20b65982016-08-31 09:08:26 +000039void readVersionScript(MemoryBufferRef MB);
40
George Rimareea31142016-07-21 14:26:59 +000041// This enum is used to implement linker script SECTIONS command.
42// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
43enum SectionsCommandKind {
44 AssignmentKind,
45 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000046 InputSectionKind,
47 AssertKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000048};
49
George Rimar076fe152016-07-21 06:43:01 +000050struct BaseCommand {
51 BaseCommand(int K) : Kind(K) {}
52 virtual ~BaseCommand() {}
53 int Kind;
54};
55
56struct SymbolAssignment : BaseCommand {
Eugene Leviantdb741e72016-09-07 07:08:43 +000057 SymbolAssignment(StringRef Name, Expr E, bool IsAbsolute)
58 : BaseCommand(AssignmentKind), Name(Name), Expression(E),
59 IsAbsolute(IsAbsolute) {}
George Rimar076fe152016-07-21 06:43:01 +000060 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000061
62 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000063 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000064 SymbolBody *Sym = nullptr;
65
66 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000067 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000068
69 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000070 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000071 bool Hidden = false;
Eugene Leviantdb741e72016-09-07 07:08:43 +000072 bool IsAbsolute;
Eugene Leviant97403d12016-09-01 09:55:57 +000073 InputSectionData *GoesAfter = nullptr;
George Rimar076fe152016-07-21 06:43:01 +000074};
75
Davide Italiano246f6812016-07-22 03:36:24 +000076// Linker scripts allow additional constraints to be put on ouput sections.
77// An output section will only be created if all of its input sections are
78// read-only
79// or all of its input sections are read-write by using the keyword ONLY_IF_RO
80// and ONLY_IF_RW respectively.
Rui Ueyamaefc40662016-07-25 22:00:10 +000081enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000082
George Rimar076fe152016-07-21 06:43:01 +000083struct OutputSectionCommand : BaseCommand {
84 OutputSectionCommand(StringRef Name)
85 : BaseCommand(OutputSectionKind), Name(Name) {}
86 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000087 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000088 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000089 Expr AlignExpr;
George Rimar8ceadb32016-08-17 07:44:19 +000090 Expr LmaExpr;
George Rimardb24d9c2016-08-19 15:18:23 +000091 Expr SubalignExpr;
George Rimareea31142016-07-21 14:26:59 +000092 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000093 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000094 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000095 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000096};
97
Rui Ueyama742c3832016-08-04 22:27:00 +000098enum SortKind { SortNone, SortByName, SortByAlignment };
George Rimar350ece42016-08-03 08:35:59 +000099
George Rimareea31142016-07-21 14:26:59 +0000100struct InputSectionDescription : BaseCommand {
George Rimarc91930a2016-09-02 21:17:20 +0000101 InputSectionDescription(StringRef FilePattern)
102 : BaseCommand(InputSectionKind),
103 FileRe(compileGlobPatterns({FilePattern})) {}
George Rimareea31142016-07-21 14:26:59 +0000104 static bool classof(const BaseCommand *C);
George Rimarc91930a2016-09-02 21:17:20 +0000105 llvm::Regex FileRe;
Rui Ueyama742c3832016-08-04 22:27:00 +0000106 SortKind SortOuter = SortNone;
107 SortKind SortInner = SortNone;
George Rimarc91930a2016-09-02 21:17:20 +0000108 llvm::Regex ExcludedFileRe;
109 llvm::Regex SectionRe;
George Rimareea31142016-07-21 14:26:59 +0000110};
111
George Rimareefa7582016-08-04 09:29:31 +0000112struct AssertCommand : BaseCommand {
113 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
114 static bool classof(const BaseCommand *C);
115 Expr Expression;
116};
117
Eugene Leviantbbe38602016-07-19 09:25:43 +0000118struct PhdrsCommand {
119 StringRef Name;
120 unsigned Type;
121 bool HasFilehdr;
122 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000123 unsigned Flags;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000124 Expr LMAExpr;
George Rimar652852c2016-04-16 10:10:32 +0000125};
126
George Rimar884e7862016-09-08 08:19:13 +0000127class LinkerScriptBase {
Rafael Espindola4d1e4d72016-09-08 14:11:08 +0000128protected:
129 ~LinkerScriptBase() = default;
130
George Rimar884e7862016-09-08 08:19:13 +0000131public:
132 virtual uint64_t getOutputSectionAddress(StringRef Name) = 0;
133 virtual uint64_t getOutputSectionSize(StringRef Name) = 0;
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000134 virtual uint64_t getOutputSectionAlign(StringRef Name) = 0;
George Rimar884e7862016-09-08 08:19:13 +0000135 virtual uint64_t getHeaderSize() = 0;
136 virtual uint64_t getSymbolValue(StringRef S) = 0;
137};
138
Rui Ueyama07320e42016-04-20 20:13:41 +0000139// ScriptConfiguration holds linker script parse results.
140struct ScriptConfiguration {
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000141 // Used to create symbol assignments outside SECTIONS command.
142 std::vector<std::unique_ptr<SymbolAssignment>> Assignments;
George Rimar652852c2016-04-16 10:10:32 +0000143 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000144 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000145
Eugene Leviantbbe38602016-07-19 09:25:43 +0000146 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000147 std::vector<PhdrsCommand> PhdrsCommands;
148
Eugene Leviante05336ff2016-09-14 08:32:36 +0000149 bool HasSections = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000150
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000151 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000152
153 // List of section patterns specified with KEEP commands. They will
154 // be kept even if they are unused and --gc-sections is specified.
George Rimarc91930a2016-09-02 21:17:20 +0000155 std::vector<llvm::Regex *> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000156};
157
Rui Ueyama07320e42016-04-20 20:13:41 +0000158extern ScriptConfiguration *ScriptConfig;
159
160// This is a runner of the linker script.
George Rimar884e7862016-09-08 08:19:13 +0000161template <class ELFT> class LinkerScript final : public LinkerScriptBase {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000162 typedef typename ELFT::uint uintX_t;
163
Rui Ueyama07320e42016-04-20 20:13:41 +0000164public:
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000165 LinkerScript();
166 ~LinkerScript();
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000167 void createAssignments();
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000168 void createSections(OutputSectionFactory<ELFT> &Factory);
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000169
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000170 std::vector<PhdrEntry<ELFT>> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000171 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000172
Rui Ueyama07320e42016-04-20 20:13:41 +0000173 ArrayRef<uint8_t> getFiller(StringRef Name);
George Rimar8ceadb32016-08-17 07:44:19 +0000174 Expr getLma(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000175 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000176 void assignAddresses();
Rui Ueyama07320e42016-04-20 20:13:41 +0000177 int compareSections(StringRef A, StringRef B);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000178 bool hasPhdrsCommands();
George Rimar884e7862016-09-08 08:19:13 +0000179 uint64_t getOutputSectionAddress(StringRef Name) override;
180 uint64_t getOutputSectionSize(StringRef Name) override;
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000181 uint64_t getOutputSectionAlign(StringRef Name) override;
George Rimar884e7862016-09-08 08:19:13 +0000182 uint64_t getHeaderSize() override;
183 uint64_t getSymbolValue(StringRef S) override;
Rui Ueyama07320e42016-04-20 20:13:41 +0000184
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000185 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
186
Rui Ueyama07320e42016-04-20 20:13:41 +0000187private:
Rui Ueyama6b274812016-07-25 22:51:07 +0000188 std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000189 getInputSections(const InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000190
Rafael Espindola7bd37872016-09-12 16:05:16 +0000191 void discard(ArrayRef<InputSectionBase<ELFT> *> V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000192
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000193 std::vector<InputSectionBase<ELFT> *>
194 createInputSectionList(OutputSectionCommand &Cmd);
195
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000196 // "ScriptConfig" is a bit too long, so define a short name for it.
197 ScriptConfiguration &Opt = *ScriptConfig;
198
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000199 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000200 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000201 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000202
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000203 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000204};
205
206// Variable template is a C++14 feature, so we can't template
207// a global variable. Use a struct to workaround.
208template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
209template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000210
George Rimar884e7862016-09-08 08:19:13 +0000211extern LinkerScriptBase *ScriptBase;
212
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000213} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000214} // namespace lld
215
216#endif