blob: 5134888136b91c603eb73899d030132acd34fb0b [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 {
Eugene Leviante63d81b2016-07-20 14:43:20 +000023template <class ELFT> class InputSectionBase;
24template <class ELFT> class OutputSectionBase;
25template <class ELFT> class OutputSectionFactory;
Eugene Leviant3e6b0272016-07-28 19:24:13 +000026template <class ELFT> class DefinedCommon;
Rui Ueyama717677a2016-02-11 21:17:59 +000027
Rui Ueyama708019c2016-07-24 18:19:40 +000028typedef std::function<uint64_t(uint64_t)> Expr;
29
Rui Ueyama07320e42016-04-20 20:13:41 +000030// Parses a linker script. Calling this function updates
31// Config and ScriptConfig.
32void readLinkerScript(MemoryBufferRef MB);
33
Rui Ueyama717677a2016-02-11 21:17:59 +000034class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000035template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000036template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000037
George Rimareea31142016-07-21 14:26:59 +000038// This enum is used to implement linker script SECTIONS command.
39// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
40enum SectionsCommandKind {
41 AssignmentKind,
42 OutputSectionKind,
43 InputSectionKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000044};
45
George Rimar076fe152016-07-21 06:43:01 +000046struct BaseCommand {
47 BaseCommand(int K) : Kind(K) {}
48 virtual ~BaseCommand() {}
49 int Kind;
50};
51
52struct SymbolAssignment : BaseCommand {
Rui Ueyama708019c2016-07-24 18:19:40 +000053 SymbolAssignment(StringRef Name, Expr E)
54 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
George Rimar076fe152016-07-21 06:43:01 +000055 static bool classof(const BaseCommand *C);
56 StringRef Name;
Rui Ueyama708019c2016-07-24 18:19:40 +000057 Expr Expression;
Eugene Levianta31c91b2016-07-22 07:38:40 +000058 bool Provide = false;
59 // Hidden and Ignore can be true, only if Provide is true
60 bool Hidden = false;
61 bool Ignore = false;
George Rimar076fe152016-07-21 06:43:01 +000062};
63
Davide Italiano246f6812016-07-22 03:36:24 +000064// Linker scripts allow additional constraints to be put on ouput sections.
65// An output section will only be created if all of its input sections are
66// read-only
67// or all of its input sections are read-write by using the keyword ONLY_IF_RO
68// and ONLY_IF_RW respectively.
Rui Ueyamaefc40662016-07-25 22:00:10 +000069enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
Davide Italiano246f6812016-07-22 03:36:24 +000070
George Rimar076fe152016-07-21 06:43:01 +000071struct OutputSectionCommand : BaseCommand {
72 OutputSectionCommand(StringRef Name)
73 : BaseCommand(OutputSectionKind), Name(Name) {}
74 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000075 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000076 Expr AddrExpr;
George Rimar630c6172016-07-26 18:06:29 +000077 Expr AlignExpr;
George Rimareea31142016-07-21 14:26:59 +000078 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000079 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000080 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000081 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000082};
83
George Rimareea31142016-07-21 14:26:59 +000084struct InputSectionDescription : BaseCommand {
85 InputSectionDescription() : BaseCommand(InputSectionKind) {}
86 static bool classof(const BaseCommand *C);
George Rimar06598002016-07-28 21:51:30 +000087 StringRef FilePattern;
Davide Italianoe7282792016-07-27 01:44:01 +000088 std::vector<StringRef> ExcludedFiles;
George Rimar06598002016-07-28 21:51:30 +000089 std::vector<StringRef> SectionPatterns;
George Rimareea31142016-07-21 14:26:59 +000090};
91
Eugene Leviantbbe38602016-07-19 09:25:43 +000092struct PhdrsCommand {
93 StringRef Name;
94 unsigned Type;
95 bool HasFilehdr;
96 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +000097 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +000098};
99
Rui Ueyama07320e42016-04-20 20:13:41 +0000100// ScriptConfiguration holds linker script parse results.
101struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000102 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000103 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000104
Eugene Leviantbbe38602016-07-19 09:25:43 +0000105 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000106 std::vector<PhdrsCommand> PhdrsCommands;
107
Rui Ueyama07320e42016-04-20 20:13:41 +0000108 bool DoLayout = false;
109
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000110 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000111
112 // List of section patterns specified with KEEP commands. They will
113 // be kept even if they are unused and --gc-sections is specified.
114 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000115};
116
Rui Ueyama07320e42016-04-20 20:13:41 +0000117extern ScriptConfiguration *ScriptConfig;
118
119// This is a runner of the linker script.
120template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000121 typedef typename ELFT::uint uintX_t;
122
Rui Ueyama07320e42016-04-20 20:13:41 +0000123public:
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000124 std::vector<OutputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000125 createSections(OutputSectionFactory<ELFT> &Factory);
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000126
Rui Ueyamaadca2452016-07-23 14:18:48 +0000127 std::vector<PhdrEntry<ELFT>>
128 createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
129
Rui Ueyama07320e42016-04-20 20:13:41 +0000130 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000131 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000132 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000133 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000134 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000135 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000136
137private:
Davide Italianoe7282792016-07-27 01:44:01 +0000138 std::vector<std::pair<StringRef, const InputSectionDescription *>>
139 getSectionMap();
Rui Ueyama6b274812016-07-25 22:51:07 +0000140
141 std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000142 getInputSections(const InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000143
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000144 // "ScriptConfig" is a bit too long, so define a short name for it.
145 ScriptConfiguration &Opt = *ScriptConfig;
146
Rui Ueyama3c291e12016-07-25 21:30:00 +0000147 std::vector<OutputSectionBase<ELFT> *>
148 filter(std::vector<OutputSectionBase<ELFT> *> &Sections);
149
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000150 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000151 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000152 size_t getPhdrIndex(StringRef PhdrName);
George Rimar10e576e2016-07-21 16:07:40 +0000153 void dispatchAssignment(SymbolAssignment *Cmd);
Rui Ueyama07320e42016-04-20 20:13:41 +0000154
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000155 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000156};
157
158// Variable template is a C++14 feature, so we can't template
159// a global variable. Use a struct to workaround.
160template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
161template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000162
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000163} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000164} // namespace lld
165
166#endif