blob: 8029c7422b34a6f9dfb20e8fd4e383d12dd2c21d [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 Ueyamaf34d0e02016-08-12 01:24:53 +000028template <class ELFT> class LayoutInputSection;
Rui Ueyama717677a2016-02-11 21:17:59 +000029
Rui Ueyama708019c2016-07-24 18:19:40 +000030typedef std::function<uint64_t(uint64_t)> Expr;
31
Rui Ueyama07320e42016-04-20 20:13:41 +000032// Parses a linker script. Calling this function updates
33// Config and ScriptConfig.
34void readLinkerScript(MemoryBufferRef MB);
35
Rui Ueyama717677a2016-02-11 21:17:59 +000036class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000037template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000038template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000039
George Rimareea31142016-07-21 14:26:59 +000040// This enum is used to implement linker script SECTIONS command.
41// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
42enum SectionsCommandKind {
43 AssignmentKind,
44 OutputSectionKind,
George Rimareefa7582016-08-04 09:29:31 +000045 InputSectionKind,
46 AssertKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000047};
48
George Rimar076fe152016-07-21 06:43:01 +000049struct BaseCommand {
50 BaseCommand(int K) : Kind(K) {}
51 virtual ~BaseCommand() {}
52 int Kind;
53};
54
55struct SymbolAssignment : BaseCommand {
Rui Ueyama708019c2016-07-24 18:19:40 +000056 SymbolAssignment(StringRef Name, Expr E)
57 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
George Rimar076fe152016-07-21 06:43:01 +000058 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000059
60 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000061 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000062 SymbolBody *Sym = nullptr;
63
64 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000065 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000066
67 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000068 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000069 bool Hidden = false;
George Rimar076fe152016-07-21 06:43:01 +000070};
71
Davide Italiano246f6812016-07-22 03:36:24 +000072// Linker scripts allow additional constraints to be put on ouput sections.
73// An output section will only be created if all of its input sections are
74// read-only
75// or all of its input sections are read-write by using the keyword ONLY_IF_RO
76// and ONLY_IF_RW respectively.
Rui Ueyamaefc40662016-07-25 22:00:10 +000077enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000078
George Rimar076fe152016-07-21 06:43:01 +000079struct OutputSectionCommand : BaseCommand {
80 OutputSectionCommand(StringRef Name)
81 : BaseCommand(OutputSectionKind), Name(Name) {}
82 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000083 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000084 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000085 Expr AlignExpr;
George Rimareea31142016-07-21 14:26:59 +000086 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000087 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000088 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000089 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000090};
91
Rui Ueyama742c3832016-08-04 22:27:00 +000092enum SortKind { SortNone, SortByName, SortByAlignment };
George Rimar350ece42016-08-03 08:35:59 +000093
George Rimareea31142016-07-21 14:26:59 +000094struct InputSectionDescription : BaseCommand {
95 InputSectionDescription() : BaseCommand(InputSectionKind) {}
96 static bool classof(const BaseCommand *C);
George Rimar06598002016-07-28 21:51:30 +000097 StringRef FilePattern;
Rui Ueyama742c3832016-08-04 22:27:00 +000098 SortKind SortOuter = SortNone;
99 SortKind SortInner = SortNone;
Davide Italianoe7282792016-07-27 01:44:01 +0000100 std::vector<StringRef> ExcludedFiles;
George Rimar06598002016-07-28 21:51:30 +0000101 std::vector<StringRef> SectionPatterns;
George Rimareea31142016-07-21 14:26:59 +0000102};
103
George Rimareefa7582016-08-04 09:29:31 +0000104struct AssertCommand : BaseCommand {
105 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
106 static bool classof(const BaseCommand *C);
107 Expr Expression;
108};
109
Eugene Leviantbbe38602016-07-19 09:25:43 +0000110struct PhdrsCommand {
111 StringRef Name;
112 unsigned Type;
113 bool HasFilehdr;
114 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000115 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +0000116};
117
Rui Ueyama07320e42016-04-20 20:13:41 +0000118// ScriptConfiguration holds linker script parse results.
119struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000120 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000121 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000122
Eugene Leviantbbe38602016-07-19 09:25:43 +0000123 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000124 std::vector<PhdrsCommand> PhdrsCommands;
125
Rui Ueyama3de0a332016-07-29 03:31:09 +0000126 bool HasContents = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000127
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000128 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000129
130 // List of section patterns specified with KEEP commands. They will
131 // be kept even if they are unused and --gc-sections is specified.
132 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000133};
134
Rui Ueyama07320e42016-04-20 20:13:41 +0000135extern ScriptConfiguration *ScriptConfig;
136
137// This is a runner of the linker script.
138template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000139 typedef typename ELFT::uint uintX_t;
140
Rui Ueyama07320e42016-04-20 20:13:41 +0000141public:
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000142 LinkerScript();
143 ~LinkerScript();
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000144 void createSections(OutputSectionFactory<ELFT> &Factory);
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000145
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000146 std::vector<PhdrEntry<ELFT>> createPhdrs();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000147
Rui Ueyama07320e42016-04-20 20:13:41 +0000148 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000149 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000150 void assignAddresses();
Rui Ueyama07320e42016-04-20 20:13:41 +0000151 int compareSections(StringRef A, StringRef B);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000152 bool hasPhdrsCommands();
George Rimar9e694502016-07-29 16:18:47 +0000153 uintX_t getOutputSectionSize(StringRef Name);
George Rimare32a3592016-08-10 07:59:34 +0000154 uintX_t getSizeOfHeaders();
Rui Ueyama07320e42016-04-20 20:13:41 +0000155
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000156 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
157
Rui Ueyama07320e42016-04-20 20:13:41 +0000158private:
Rui Ueyama6b274812016-07-25 22:51:07 +0000159 std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000160 getInputSections(const InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000161
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000162 void discard(OutputSectionCommand &Cmd);
163
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000164 std::vector<InputSectionBase<ELFT> *>
165 createInputSectionList(OutputSectionCommand &Cmd);
166
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000167 // "ScriptConfig" is a bit too long, so define a short name for it.
168 ScriptConfiguration &Opt = *ScriptConfig;
169
George Rimar9e694502016-07-29 16:18:47 +0000170 void filter();
Rui Ueyama3c291e12016-07-25 21:30:00 +0000171
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000172 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000173 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000174 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000175
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000176 llvm::SpecificBumpPtrAllocator<LayoutInputSection<ELFT>> LAlloc;
177
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000178 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000179};
180
181// Variable template is a C++14 feature, so we can't template
182// a global variable. Use a struct to workaround.
183template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
184template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000185
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000186} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000187} // namespace lld
188
189#endif