blob: f08bd0762b5f9cae47cf29dac445549775f43f00 [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 {
Rafael Espindolae7553e42016-08-31 13:28:33 +000023class DefinedCommon;
George Rimardbb76db2016-08-18 13:00:49 +000024class ScriptParser;
Rui Ueyama8d083e62016-07-29 05:48:39 +000025class SymbolBody;
Eugene Leviante63d81b2016-07-20 14:43:20 +000026template <class ELFT> class InputSectionBase;
27template <class ELFT> class OutputSectionBase;
28template <class ELFT> class OutputSectionFactory;
Rui Ueyamaf34d0e02016-08-12 01:24:53 +000029template <class ELFT> class LayoutInputSection;
Rui Ueyama717677a2016-02-11 21:17:59 +000030
Rui Ueyama708019c2016-07-24 18:19:40 +000031typedef std::function<uint64_t(uint64_t)> Expr;
32
Rui Ueyama07320e42016-04-20 20:13:41 +000033// Parses a linker script. Calling this function updates
34// Config and ScriptConfig.
35void readLinkerScript(MemoryBufferRef MB);
36
George Rimar20b65982016-08-31 09:08:26 +000037void readVersionScript(MemoryBufferRef MB);
38
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 Rimar8ceadb32016-08-17 07:44:19 +000085 Expr LmaExpr;
George Rimardb24d9c2016-08-19 15:18:23 +000086 Expr SubalignExpr;
George Rimareea31142016-07-21 14:26:59 +000087 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000088 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000089 std::vector<uint8_t> Filler;
Rui Ueyamaefc40662016-07-25 22:00:10 +000090 ConstraintKind Constraint = ConstraintKind::NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000091};
92
Rui Ueyama742c3832016-08-04 22:27:00 +000093enum SortKind { SortNone, SortByName, SortByAlignment };
George Rimar350ece42016-08-03 08:35:59 +000094
George Rimareea31142016-07-21 14:26:59 +000095struct InputSectionDescription : BaseCommand {
96 InputSectionDescription() : BaseCommand(InputSectionKind) {}
97 static bool classof(const BaseCommand *C);
George Rimar06598002016-07-28 21:51:30 +000098 StringRef FilePattern;
Rui Ueyama742c3832016-08-04 22:27:00 +000099 SortKind SortOuter = SortNone;
100 SortKind SortInner = SortNone;
Davide Italianoe7282792016-07-27 01:44:01 +0000101 std::vector<StringRef> ExcludedFiles;
George Rimar06598002016-07-28 21:51:30 +0000102 std::vector<StringRef> SectionPatterns;
George Rimareea31142016-07-21 14:26:59 +0000103};
104
George Rimareefa7582016-08-04 09:29:31 +0000105struct AssertCommand : BaseCommand {
106 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
107 static bool classof(const BaseCommand *C);
108 Expr Expression;
109};
110
Eugene Leviantbbe38602016-07-19 09:25:43 +0000111struct PhdrsCommand {
112 StringRef Name;
113 unsigned Type;
114 bool HasFilehdr;
115 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +0000116 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +0000117};
118
Rui Ueyama07320e42016-04-20 20:13:41 +0000119// ScriptConfiguration holds linker script parse results.
120struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +0000121 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +0000122 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000123
Eugene Leviantbbe38602016-07-19 09:25:43 +0000124 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000125 std::vector<PhdrsCommand> PhdrsCommands;
126
Rui Ueyama3de0a332016-07-29 03:31:09 +0000127 bool HasContents = false;
Rui Ueyama07320e42016-04-20 20:13:41 +0000128
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000129 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000130
131 // List of section patterns specified with KEEP commands. They will
132 // be kept even if they are unused and --gc-sections is specified.
133 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000134};
135
Rui Ueyama07320e42016-04-20 20:13:41 +0000136extern ScriptConfiguration *ScriptConfig;
137
138// This is a runner of the linker script.
139template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000140 typedef typename ELFT::uint uintX_t;
141
Rui Ueyama07320e42016-04-20 20:13:41 +0000142public:
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000143 LinkerScript();
144 ~LinkerScript();
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000145 void createSections(OutputSectionFactory<ELFT> &Factory);
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000146
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000147 std::vector<PhdrEntry<ELFT>> createPhdrs();
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000148 bool ignoreInterpSection();
Rui Ueyamaadca2452016-07-23 14:18:48 +0000149
Rui Ueyama07320e42016-04-20 20:13:41 +0000150 ArrayRef<uint8_t> getFiller(StringRef Name);
George Rimar8ceadb32016-08-17 07:44:19 +0000151 Expr getLma(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000152 bool shouldKeep(InputSectionBase<ELFT> *S);
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000153 void assignAddresses();
Rui Ueyama07320e42016-04-20 20:13:41 +0000154 int compareSections(StringRef A, StringRef B);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000155 bool hasPhdrsCommands();
George Rimar96659df2016-08-30 09:54:01 +0000156 uintX_t getOutputSectionAddress(StringRef Name);
George Rimar9e694502016-07-29 16:18:47 +0000157 uintX_t getOutputSectionSize(StringRef Name);
Rui Ueyama4f7500b2016-08-12 04:00:22 +0000158 uintX_t getHeaderSize();
Rui Ueyama07320e42016-04-20 20:13:41 +0000159
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000160 std::vector<OutputSectionBase<ELFT> *> *OutputSections;
161
Rui Ueyama07320e42016-04-20 20:13:41 +0000162private:
Rui Ueyama6b274812016-07-25 22:51:07 +0000163 std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000164 getInputSections(const InputSectionDescription *);
Rui Ueyama6b274812016-07-25 22:51:07 +0000165
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000166 void discard(OutputSectionCommand &Cmd);
167
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000168 std::vector<InputSectionBase<ELFT> *>
169 createInputSectionList(OutputSectionCommand &Cmd);
170
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000171 // "ScriptConfig" is a bit too long, so define a short name for it.
172 ScriptConfiguration &Opt = *ScriptConfig;
173
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000174 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000175 std::vector<size_t> getPhdrIndices(StringRef SectionName);
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000176 size_t getPhdrIndex(StringRef PhdrName);
Rui Ueyama07320e42016-04-20 20:13:41 +0000177
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000178 llvm::SpecificBumpPtrAllocator<LayoutInputSection<ELFT>> LAlloc;
179
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000180 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000181};
182
183// Variable template is a C++14 feature, so we can't template
184// a global variable. Use a struct to workaround.
185template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
186template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000187
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000188} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000189} // namespace lld
190
191#endif