blob: e853d0c0d57f178785307ee9682d8ba69f542c46 [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,
44 InputSectionKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000045};
46
George Rimar076fe152016-07-21 06:43:01 +000047struct BaseCommand {
48 BaseCommand(int K) : Kind(K) {}
49 virtual ~BaseCommand() {}
50 int Kind;
51};
52
53struct SymbolAssignment : BaseCommand {
Rui Ueyama708019c2016-07-24 18:19:40 +000054 SymbolAssignment(StringRef Name, Expr E)
55 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
George Rimar076fe152016-07-21 06:43:01 +000056 static bool classof(const BaseCommand *C);
Rui Ueyama20204242016-07-29 05:52:33 +000057
58 // The LHS of an expression. Name is either a symbol name or ".".
George Rimar076fe152016-07-21 06:43:01 +000059 StringRef Name;
Rui Ueyama20204242016-07-29 05:52:33 +000060 SymbolBody *Sym = nullptr;
61
62 // The RHS of an expression.
Rui Ueyama708019c2016-07-24 18:19:40 +000063 Expr Expression;
Rui Ueyama20204242016-07-29 05:52:33 +000064
65 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
Eugene Levianta31c91b2016-07-22 07:38:40 +000066 bool Provide = false;
Eugene Levianta31c91b2016-07-22 07:38:40 +000067 bool Hidden = false;
George Rimar076fe152016-07-21 06:43:01 +000068};
69
Davide Italiano246f6812016-07-22 03:36:24 +000070// Linker scripts allow additional constraints to be put on ouput sections.
71// An output section will only be created if all of its input sections are
72// read-only
73// or all of its input sections are read-write by using the keyword ONLY_IF_RO
74// and ONLY_IF_RW respectively.
Rui Ueyamaefc40662016-07-25 22:00:10 +000075enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000076
George Rimar076fe152016-07-21 06:43:01 +000077struct OutputSectionCommand : BaseCommand {
78 OutputSectionCommand(StringRef Name)
79 : BaseCommand(OutputSectionKind), Name(Name) {}
80 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000081 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000082 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000083 Expr AlignExpr;
George Rimareea31142016-07-21 14:26:59 +000084 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000085 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000086 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000087 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000088};
89
George Rimar350ece42016-08-03 08:35:59 +000090enum class SortKind { None, Name, Align, NameAlign, AlignName };
91
George Rimareea31142016-07-21 14:26:59 +000092struct InputSectionDescription : BaseCommand {
93 InputSectionDescription() : BaseCommand(InputSectionKind) {}
94 static bool classof(const BaseCommand *C);
George Rimar06598002016-07-28 21:51:30 +000095 StringRef FilePattern;
George Rimar350ece42016-08-03 08:35:59 +000096 SortKind Sort = SortKind::None;
Davide Italianoe7282792016-07-27 01:44:01 +000097 std::vector<StringRef> ExcludedFiles;
George Rimar06598002016-07-28 21:51:30 +000098 std::vector<StringRef> SectionPatterns;
George Rimareea31142016-07-21 14:26:59 +000099};
100
Eugene Leviantbbe38602016-07-19 09:25:43 +0000101struct PhdrsCommand {
102 StringRef Name;
103 unsigned Type;
104 bool HasFilehdr;
105 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000106 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +0000107};
108
Rui Ueyama07320e42016-04-20 20:13:41 +0000109// ScriptConfiguration holds linker script parse results.
110struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000111 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000112 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000113
Eugene Leviantbbe38602016-07-19 09:25:43 +0000114 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000115 std::vector<PhdrsCommand> PhdrsCommands;
116
Rui Ueyama3de0a332016-07-29 03:31:09 +0000117 bool HasContents = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000118
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000119 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000120
121 // List of section patterns specified with KEEP commands. They will
122 // be kept even if they are unused and --gc-sections is specified.
123 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000124};
125
Rui Ueyama07320e42016-04-20 20:13:41 +0000126extern ScriptConfiguration *ScriptConfig;
127
128// This is a runner of the linker script.
129template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000130 typedef typename ELFT::uint uintX_t;
131
Rui Ueyama07320e42016-04-20 20:13:41 +0000132public:
George Rimar9e694502016-07-29 16:18:47 +0000133 void createSections(std::vector<OutputSectionBase<ELFT> *> *Out,
134 OutputSectionFactory<ELFT> &Factory);
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000135
Rui Ueyamaadca2452016-07-23 14:18:48 +0000136 std::vector<PhdrEntry<ELFT>>
137 createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
138
Rui Ueyama07320e42016-04-20 20:13:41 +0000139 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000140 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000141 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000142 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000143 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000144 bool hasPhdrsCommands();
George Rimar9e694502016-07-29 16:18:47 +0000145 uintX_t getOutputSectionSize(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000146
147private:
Davide Italianoe7282792016-07-27 01:44:01 +0000148 std::vector<std::pair<StringRef, const InputSectionDescription *>>
149 getSectionMap();
Rui Ueyama6b274812016-07-25 22:51:07 +0000150
151 std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000152 getInputSections(const InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000153
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000154 // "ScriptConfig" is a bit too long, so define a short name for it.
155 ScriptConfiguration &Opt = *ScriptConfig;
156
George Rimar9e694502016-07-29 16:18:47 +0000157 void filter();
Rui Ueyama3c291e12016-07-25 21:30:00 +0000158
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000159 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000160 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000161 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000162
George Rimar9e694502016-07-29 16:18:47 +0000163 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000164 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000165};
166
167// Variable template is a C++14 feature, so we can't template
168// a global variable. Use a struct to workaround.
169template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
170template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000171
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000172} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000173} // namespace lld
174
175#endif