blob: 8f8b293159145ca6d35ff875fb56e37d962aea79 [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;
Rui Ueyama717677a2016-02-11 21:17:59 +000026
Rui Ueyama708019c2016-07-24 18:19:40 +000027typedef std::function<uint64_t(uint64_t)> Expr;
28
Rui Ueyama07320e42016-04-20 20:13:41 +000029// Parses a linker script. Calling this function updates
30// Config and ScriptConfig.
31void readLinkerScript(MemoryBufferRef MB);
32
Rui Ueyama717677a2016-02-11 21:17:59 +000033class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000034template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000035template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000036
George Rimareea31142016-07-21 14:26:59 +000037// This enum is used to implement linker script SECTIONS command.
38// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
39enum SectionsCommandKind {
40 AssignmentKind,
41 OutputSectionKind,
42 InputSectionKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000043};
44
George Rimar076fe152016-07-21 06:43:01 +000045struct BaseCommand {
46 BaseCommand(int K) : Kind(K) {}
47 virtual ~BaseCommand() {}
48 int Kind;
49};
50
51struct SymbolAssignment : BaseCommand {
Rui Ueyama708019c2016-07-24 18:19:40 +000052 SymbolAssignment(StringRef Name, Expr E)
53 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
George Rimar076fe152016-07-21 06:43:01 +000054 static bool classof(const BaseCommand *C);
55 StringRef Name;
Rui Ueyama708019c2016-07-24 18:19:40 +000056 Expr Expression;
Eugene Levianta31c91b2016-07-22 07:38:40 +000057 bool Provide = false;
58 // Hidden and Ignore can be true, only if Provide is true
59 bool Hidden = false;
60 bool Ignore = false;
George Rimar076fe152016-07-21 06:43:01 +000061};
62
Davide Italiano246f6812016-07-22 03:36:24 +000063// Linker scripts allow additional constraints to be put on ouput sections.
64// An output section will only be created if all of its input sections are
65// read-only
66// or all of its input sections are read-write by using the keyword ONLY_IF_RO
67// and ONLY_IF_RW respectively.
68enum ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
69
George Rimar076fe152016-07-21 06:43:01 +000070struct OutputSectionCommand : BaseCommand {
71 OutputSectionCommand(StringRef Name)
72 : BaseCommand(OutputSectionKind), Name(Name) {}
73 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000074 StringRef Name;
George Rimar58e5c4d2016-07-25 08:29:46 +000075 Expr AddrExpr;
George Rimareea31142016-07-21 14:26:59 +000076 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000077 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000078 std::vector<uint8_t> Filler;
Davide Italiano246f6812016-07-22 03:36:24 +000079 ConstraintKind Constraint = NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000080};
81
George Rimareea31142016-07-21 14:26:59 +000082struct InputSectionDescription : BaseCommand {
83 InputSectionDescription() : BaseCommand(InputSectionKind) {}
84 static bool classof(const BaseCommand *C);
85 std::vector<StringRef> Patterns;
86};
87
Eugene Leviantbbe38602016-07-19 09:25:43 +000088struct PhdrsCommand {
89 StringRef Name;
90 unsigned Type;
91 bool HasFilehdr;
92 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +000093 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +000094};
95
Rui Ueyama07320e42016-04-20 20:13:41 +000096// ScriptConfiguration holds linker script parse results.
97struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +000098 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +000099 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +0000100
Eugene Leviantbbe38602016-07-19 09:25:43 +0000101 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +0000102 std::vector<PhdrsCommand> PhdrsCommands;
103
Rui Ueyama07320e42016-04-20 20:13:41 +0000104 bool DoLayout = false;
105
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000106 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000107
108 // List of section patterns specified with KEEP commands. They will
109 // be kept even if they are unused and --gc-sections is specified.
110 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000111};
112
Rui Ueyama07320e42016-04-20 20:13:41 +0000113extern ScriptConfiguration *ScriptConfig;
114
115// This is a runner of the linker script.
116template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000117 typedef typename ELFT::uint uintX_t;
118
Rui Ueyama07320e42016-04-20 20:13:41 +0000119public:
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000120 std::vector<OutputSectionBase<ELFT> *>
121 createSections(OutputSectionFactory<ELFT> &Factory);
122
Rui Ueyamaadca2452016-07-23 14:18:48 +0000123 std::vector<PhdrEntry<ELFT>>
124 createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
125
Rui Ueyama07320e42016-04-20 20:13:41 +0000126 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000127 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000128 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000129 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000130 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000131 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000132
133private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000134 // "ScriptConfig" is a bit too long, so define a short name for it.
135 ScriptConfiguration &Opt = *ScriptConfig;
136
Rui Ueyama3c291e12016-07-25 21:30:00 +0000137 std::vector<OutputSectionBase<ELFT> *>
138 filter(std::vector<OutputSectionBase<ELFT> *> &Sections);
139
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000140 int getSectionIndex(StringRef Name);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000141 std::vector<size_t> getPhdrIndices(StringRef SectionName);
George Rimar10e576e2016-07-21 16:07:40 +0000142 void dispatchAssignment(SymbolAssignment *Cmd);
Rui Ueyama07320e42016-04-20 20:13:41 +0000143
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000144 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000145};
146
147// Variable template is a C++14 feature, so we can't template
148// a global variable. Use a struct to workaround.
149template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
150template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000151
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000152} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000153} // namespace lld
154
155#endif