blob: 25034e590ece9c7ecf063f28cb8f55739595e019 [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
Eugene Leviantbbe38602016-07-19 09:25:43 +000013#include "Writer.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000014#include "lld/Core/LLVM.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/MapVector.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000017#include "llvm/Support/Allocator.h"
18#include "llvm/Support/MemoryBuffer.h"
Rui Ueyama708019c2016-07-24 18:19:40 +000019#include <functional>
Rui Ueyama717677a2016-02-11 21:17:59 +000020
21namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000022namespace elf {
Rui Ueyama8d083e62016-07-29 05:48:39 +000023class SymbolBody;
Eugene Leviante63d81b2016-07-20 14:43:20 +000024template <class ELFT> class InputSectionBase;
25template <class ELFT> class OutputSectionBase;
26template <class ELFT> class OutputSectionFactory;
Eugene Leviant3e6b0272016-07-28 19:24:13 +000027template <class ELFT> class DefinedCommon;
Rui Ueyama717677a2016-02-11 21:17:59 +000028
Rui Ueyama708019c2016-07-24 18:19:40 +000029typedef std::function<uint64_t(uint64_t)> Expr;
30
Rui Ueyama07320e42016-04-20 20:13:41 +000031// Parses a linker script. Calling this function updates
32// Config and ScriptConfig.
33void readLinkerScript(MemoryBufferRef MB);
34
Rui Ueyama717677a2016-02-11 21:17:59 +000035class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000036template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000037template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000038
George Rimareea31142016-07-21 14:26:59 +000039// This enum is used to implement linker script SECTIONS command.
40// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
41enum SectionsCommandKind {
42 AssignmentKind,
43 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000044 InputSectionKind,
45 AssertKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000046};
47
George Rimar076fe152016-07-21 06:43:01 +000048struct BaseCommand {
49 BaseCommand(int K) : Kind(K) {}
50 virtual ~BaseCommand() {}
51 int Kind;
52};
53
54struct SymbolAssignment : BaseCommand {
Rui Ueyama708019c2016-07-24 18:19:40 +000055 SymbolAssignment(StringRef Name, Expr E)
56 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
George Rimar076fe152016-07-21 06:43:01 +000057 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000058
59 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000060 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000061 SymbolBody *Sym = nullptr;
62
63 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000064 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000065
66 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000067 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000068 bool Hidden = false;
George Rimar076fe152016-07-21 06:43:01 +000069};
70
Davide Italiano246f6812016-07-22 03:36:24 +000071// Linker scripts allow additional constraints to be put on ouput sections.
72// An output section will only be created if all of its input sections are
73// read-only
74// or all of its input sections are read-write by using the keyword ONLY_IF_RO
75// and ONLY_IF_RW respectively.
Rui Ueyamaefc40662016-07-25 22:00:10 +000076enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000077
George Rimar076fe152016-07-21 06:43:01 +000078struct OutputSectionCommand : BaseCommand {
79 OutputSectionCommand(StringRef Name)
80 : BaseCommand(OutputSectionKind), Name(Name) {}
81 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000082 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000083 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000084 Expr AlignExpr;
George Rimareea31142016-07-21 14:26:59 +000085 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000086 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000087 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000088 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000089};
90
Rui Ueyama742c3832016-08-04 22:27:00 +000091enum SortKind { SortNone, SortByName, SortByAlignment };
George Rimar350ece42016-08-03 08:35:59 +000092
George Rimareea31142016-07-21 14:26:59 +000093struct InputSectionDescription : BaseCommand {
94 InputSectionDescription() : BaseCommand(InputSectionKind) {}
95 static bool classof(const BaseCommand *C);
George Rimar06598002016-07-28 21:51:30 +000096 StringRef FilePattern;
Rui Ueyama742c3832016-08-04 22:27:00 +000097 SortKind SortOuter = SortNone;
98 SortKind SortInner = SortNone;
Davide Italianoe7282792016-07-27 01:44:01 +000099 std::vector<StringRef> ExcludedFiles;
George Rimar06598002016-07-28 21:51:30 +0000100 std::vector<StringRef> SectionPatterns;
George Rimareea31142016-07-21 14:26:59 +0000101};
102
George Rimareefa7582016-08-04 09:29:31 +0000103struct AssertCommand : BaseCommand {
104 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
105 static bool classof(const BaseCommand *C);
106 Expr Expression;
107};
108
Eugene Leviantbbe38602016-07-19 09:25:43 +0000109struct PhdrsCommand {
110 StringRef Name;
111 unsigned Type;
112 bool HasFilehdr;
113 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000114 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +0000115};
116
Rui Ueyama07320e42016-04-20 20:13:41 +0000117// ScriptConfiguration holds linker script parse results.
118struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000119 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000120 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000121
Eugene Leviantbbe38602016-07-19 09:25:43 +0000122 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000123 std::vector<PhdrsCommand> PhdrsCommands;
124
Rui Ueyama3de0a332016-07-29 03:31:09 +0000125 bool HasContents = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000126
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000127 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000128
129 // List of section patterns specified with KEEP commands. They will
130 // be kept even if they are unused and --gc-sections is specified.
131 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000132};
133
Rui Ueyama07320e42016-04-20 20:13:41 +0000134extern ScriptConfiguration *ScriptConfig;
135
136// This is a runner of the linker script.
137template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000138 typedef typename ELFT::uint uintX_t;
139
Rui Ueyama07320e42016-04-20 20:13:41 +0000140public:
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000141 void createSections(OutputSectionFactory<ELFT> &Factory);
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000142
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000143 std::vector<PhdrEntry<ELFT>> createPhdrs();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000144
Rui Ueyama07320e42016-04-20 20:13:41 +0000145 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000146 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000147 void assignAddresses();
Rui Ueyama07320e42016-04-20 20:13:41 +0000148 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000149 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000150 bool hasPhdrsCommands();
George Rimar9e694502016-07-29 16:18:47 +0000151 uintX_t getOutputSectionSize(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000152
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000153 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
154
Rui Ueyama07320e42016-04-20 20:13:41 +0000155private:
Davide Italianoe7282792016-07-27 01:44:01 +0000156 std::vector<std::pair<StringRef, const InputSectionDescription *>>
157 getSectionMap();
Rui Ueyama6b274812016-07-25 22:51:07 +0000158
159 std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000160 getInputSections(const InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000161
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000162 // "ScriptConfig" is a bit too long, so define a short name for it.
163 ScriptConfiguration &Opt = *ScriptConfig;
164
George Rimar9e694502016-07-29 16:18:47 +0000165 void filter();
Rui Ueyama3c291e12016-07-25 21:30:00 +0000166
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000167 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000168 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000169 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000170
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000171 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000172};
173
174// Variable template is a C++14 feature, so we can't template
175// a global variable. Use a struct to workaround.
176template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
177template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000178
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000179} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000180} // namespace lld
181
182#endif