blob: 7e9f157556eef66576f5a9e250fc93e6e22b2141 [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 Ueyama717677a2016-02-11 21:17:59 +000019
20namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000021namespace elf {
Eugene Leviante63d81b2016-07-20 14:43:20 +000022template <class ELFT> class InputSectionBase;
23template <class ELFT> class OutputSectionBase;
24template <class ELFT> class OutputSectionFactory;
Rui Ueyama717677a2016-02-11 21:17:59 +000025
Rui Ueyama07320e42016-04-20 20:13:41 +000026// Parses a linker script. Calling this function updates
27// Config and ScriptConfig.
28void readLinkerScript(MemoryBufferRef MB);
29
Rui Ueyama717677a2016-02-11 21:17:59 +000030class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000031template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000032template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000033
George Rimareea31142016-07-21 14:26:59 +000034// This enum is used to implement linker script SECTIONS command.
35// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
36enum SectionsCommandKind {
37 AssignmentKind,
38 OutputSectionKind,
39 InputSectionKind
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000040};
41
George Rimar076fe152016-07-21 06:43:01 +000042struct BaseCommand {
43 BaseCommand(int K) : Kind(K) {}
44 virtual ~BaseCommand() {}
45 int Kind;
46};
47
48struct SymbolAssignment : BaseCommand {
49 SymbolAssignment(StringRef Name, std::vector<StringRef> &Expr)
50 : BaseCommand(AssignmentKind), Name(Name), Expr(std::move(Expr)) {}
51 static bool classof(const BaseCommand *C);
52 StringRef Name;
George Rimar652852c2016-04-16 10:10:32 +000053 std::vector<StringRef> Expr;
Eugene Levianta31c91b2016-07-22 07:38:40 +000054 bool Provide = false;
55 // Hidden and Ignore can be true, only if Provide is true
56 bool Hidden = false;
57 bool Ignore = false;
George Rimar076fe152016-07-21 06:43:01 +000058};
59
Davide Italiano246f6812016-07-22 03:36:24 +000060// Linker scripts allow additional constraints to be put on ouput sections.
61// An output section will only be created if all of its input sections are
62// read-only
63// or all of its input sections are read-write by using the keyword ONLY_IF_RO
64// and ONLY_IF_RW respectively.
65enum ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
66
George Rimar076fe152016-07-21 06:43:01 +000067struct OutputSectionCommand : BaseCommand {
68 OutputSectionCommand(StringRef Name)
69 : BaseCommand(OutputSectionKind), Name(Name) {}
70 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000071 StringRef Name;
George Rimareea31142016-07-21 14:26:59 +000072 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000073 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000074 std::vector<uint8_t> Filler;
Davide Italiano246f6812016-07-22 03:36:24 +000075 ConstraintKind Constraint = NoConstraint;
Eugene Leviantbbe38602016-07-19 09:25:43 +000076};
77
George Rimareea31142016-07-21 14:26:59 +000078struct InputSectionDescription : BaseCommand {
79 InputSectionDescription() : BaseCommand(InputSectionKind) {}
80 static bool classof(const BaseCommand *C);
81 std::vector<StringRef> Patterns;
82};
83
Eugene Leviantbbe38602016-07-19 09:25:43 +000084struct PhdrsCommand {
85 StringRef Name;
86 unsigned Type;
87 bool HasFilehdr;
88 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +000089 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +000090};
91
Rui Ueyama07320e42016-04-20 20:13:41 +000092// ScriptConfiguration holds linker script parse results.
93struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +000094 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +000095 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +000096
Eugene Leviantbbe38602016-07-19 09:25:43 +000097 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +000098 std::vector<PhdrsCommand> PhdrsCommands;
99
Rui Ueyama07320e42016-04-20 20:13:41 +0000100 bool DoLayout = false;
101
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000102 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000103
104 // List of section patterns specified with KEEP commands. They will
105 // be kept even if they are unused and --gc-sections is specified.
106 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000107};
108
Rui Ueyama07320e42016-04-20 20:13:41 +0000109extern ScriptConfiguration *ScriptConfig;
110
111// This is a runner of the linker script.
112template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000113 typedef typename ELFT::uint uintX_t;
114
Rui Ueyama07320e42016-04-20 20:13:41 +0000115public:
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000116 std::vector<OutputSectionBase<ELFT> *>
117 createSections(OutputSectionFactory<ELFT> &Factory);
118
Rui Ueyamaadca2452016-07-23 14:18:48 +0000119 std::vector<PhdrEntry<ELFT>>
120 createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
121
Rui Ueyama07320e42016-04-20 20:13:41 +0000122 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000123 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000124 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000125 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000126 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000127 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000128
129private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000130 // "ScriptConfig" is a bit too long, so define a short name for it.
131 ScriptConfiguration &Opt = *ScriptConfig;
132
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000133 int getSectionIndex(StringRef Name);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000134 std::vector<size_t> getPhdrIndicesForSection(StringRef Name);
George Rimar10e576e2016-07-21 16:07:40 +0000135 void dispatchAssignment(SymbolAssignment *Cmd);
Rui Ueyama07320e42016-04-20 20:13:41 +0000136
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000137 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000138};
139
140// Variable template is a C++14 feature, so we can't template
141// a global variable. Use a struct to workaround.
142template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
143template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000144
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000145} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000146} // namespace lld
147
148#endif