blob: 311bd0bef51d1be796d5942e4a8a79dd29f08505 [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;
Eugene Leviant865bf862016-07-21 10:43:25 +000082 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +000083};
84
Rui Ueyama07320e42016-04-20 20:13:41 +000085// ScriptConfiguration holds linker script parse results.
86struct ScriptConfiguration {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000087 // SECTIONS commands.
88 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000089
George Rimar652852c2016-04-16 10:10:32 +000090 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +000091 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +000092
Eugene Leviantbbe38602016-07-19 09:25:43 +000093 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +000094 std::vector<PhdrsCommand> PhdrsCommands;
95
Rui Ueyama07320e42016-04-20 20:13:41 +000096 bool DoLayout = false;
97
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000098 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +000099
100 // List of section patterns specified with KEEP commands. They will
101 // be kept even if they are unused and --gc-sections is specified.
102 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +0000103};
104
Rui Ueyama07320e42016-04-20 20:13:41 +0000105extern ScriptConfiguration *ScriptConfig;
106
107// This is a runner of the linker script.
108template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000109 typedef typename ELFT::uint uintX_t;
110
Rui Ueyama07320e42016-04-20 20:13:41 +0000111public:
Rafael Espindola74df5c72016-07-19 12:33:46 +0000112 typedef PhdrEntry<ELFT> Phdr;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000113
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000114 std::vector<OutputSectionBase<ELFT> *>
115 createSections(OutputSectionFactory<ELFT> &Factory);
116
Rui Ueyama07320e42016-04-20 20:13:41 +0000117 StringRef getOutputSection(InputSectionBase<ELFT> *S);
118 ArrayRef<uint8_t> getFiller(StringRef Name);
119 bool isDiscarded(InputSectionBase<ELFT> *S);
120 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000121 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000122 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000123 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000124 std::vector<Phdr> createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
125 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000126
127private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000128 // "ScriptConfig" is a bit too long, so define a short name for it.
129 ScriptConfiguration &Opt = *ScriptConfig;
130
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000131 int getSectionIndex(StringRef Name);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000132 std::vector<size_t> getPhdrIndicesForSection(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000133
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000134 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000135};
136
137// Variable template is a C++14 feature, so we can't template
138// a global variable. Use a struct to workaround.
139template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
140template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000141
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000142} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000143} // namespace lld
144
145#endif