blob: 20ab4be03ad68b4717e1756ee0e34176c2ece5bb [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
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000034// This class represents each rule in SECTIONS command.
Rui Ueyamac9f402e2016-04-22 00:23:52 +000035struct SectionRule {
Rui Ueyama8ec77e62016-04-21 22:00:51 +000036 SectionRule(StringRef D, StringRef S)
37 : Dest(D), SectionPattern(S) {}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000038
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000039 StringRef Dest;
40
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000041 StringRef SectionPattern;
42};
43
George Rimar076fe152016-07-21 06:43:01 +000044// This enum represents what we can observe in SECTIONS tag of script.
45// Each sections-command may of be one of the following:
46// (https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS)
47// * An ENTRY command.
48// * A symbol assignment.
49// * An output section description.
50// * An overlay description.
51// We support only AssignmentKind and OutputSectionKind for now.
52enum SectionsCommandKind { AssignmentKind, OutputSectionKind };
George Rimar652852c2016-04-16 10:10:32 +000053
George Rimar076fe152016-07-21 06:43:01 +000054struct BaseCommand {
55 BaseCommand(int K) : Kind(K) {}
56 virtual ~BaseCommand() {}
57 int Kind;
58};
59
60struct SymbolAssignment : BaseCommand {
61 SymbolAssignment(StringRef Name, std::vector<StringRef> &Expr)
62 : BaseCommand(AssignmentKind), Name(Name), Expr(std::move(Expr)) {}
63 static bool classof(const BaseCommand *C);
64 StringRef Name;
George Rimar652852c2016-04-16 10:10:32 +000065 std::vector<StringRef> Expr;
George Rimar076fe152016-07-21 06:43:01 +000066};
67
68struct OutputSectionCommand : BaseCommand {
69 OutputSectionCommand(StringRef Name)
70 : BaseCommand(OutputSectionKind), Name(Name) {}
71 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000072 StringRef Name;
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;
Eugene Leviantbbe38602016-07-19 09:25:43 +000075};
76
77struct PhdrsCommand {
78 StringRef Name;
79 unsigned Type;
80 bool HasFilehdr;
81 bool HasPhdrs;
George Rimar652852c2016-04-16 10:10:32 +000082};
83
Rui Ueyama07320e42016-04-20 20:13:41 +000084// ScriptConfiguration holds linker script parse results.
85struct ScriptConfiguration {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000086 // SECTIONS commands.
87 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000088
Rui Ueyama3e808972016-02-28 05:09:11 +000089 // Section fill attribute for each section.
90 llvm::StringMap<std::vector<uint8_t>> Filler;
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000091
George Rimar652852c2016-04-16 10:10:32 +000092 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +000093 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +000094
Eugene Leviantbbe38602016-07-19 09:25:43 +000095 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +000096 std::vector<PhdrsCommand> PhdrsCommands;
97
Rui Ueyama07320e42016-04-20 20:13:41 +000098 bool DoLayout = false;
99
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000100 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000101
102 // List of section patterns specified with KEEP commands. They will
103 // be kept even if they are unused and --gc-sections is specified.
104 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000105};
106
Rui Ueyama07320e42016-04-20 20:13:41 +0000107extern ScriptConfiguration *ScriptConfig;
108
109// This is a runner of the linker script.
110template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000111 typedef typename ELFT::uint uintX_t;
112
Rui Ueyama07320e42016-04-20 20:13:41 +0000113public:
Rafael Espindola74df5c72016-07-19 12:33:46 +0000114 typedef PhdrEntry<ELFT> Phdr;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000115
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000116 std::vector<OutputSectionBase<ELFT> *>
117 createSections(OutputSectionFactory<ELFT> &Factory);
118
Rui Ueyama07320e42016-04-20 20:13:41 +0000119 StringRef getOutputSection(InputSectionBase<ELFT> *S);
120 ArrayRef<uint8_t> getFiller(StringRef Name);
121 bool isDiscarded(InputSectionBase<ELFT> *S);
122 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000123 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000124 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000125 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000126 std::vector<Phdr> createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
127 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);
Rui Ueyama07320e42016-04-20 20:13:41 +0000135
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000136 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000137};
138
139// Variable template is a C++14 feature, so we can't template
140// a global variable. Use a struct to workaround.
141template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
142template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000143
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000144} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000145} // namespace lld
146
147#endif