blob: c4fa455f2656060cc93992e029b94fa117e8127d [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;
George Rimar076fe152016-07-21 06:43:01 +000054};
55
56struct OutputSectionCommand : BaseCommand {
57 OutputSectionCommand(StringRef Name)
58 : BaseCommand(OutputSectionKind), Name(Name) {}
59 static bool classof(const BaseCommand *C);
Eugene Levianteda81a12016-07-12 06:39:48 +000060 StringRef Name;
George Rimareea31142016-07-21 14:26:59 +000061 std::vector<std::unique_ptr<BaseCommand>> Commands;
Eugene Leviantbbe38602016-07-19 09:25:43 +000062 std::vector<StringRef> Phdrs;
George Rimar076fe152016-07-21 06:43:01 +000063 std::vector<uint8_t> Filler;
Eugene Leviantbbe38602016-07-19 09:25:43 +000064};
65
George Rimareea31142016-07-21 14:26:59 +000066struct InputSectionDescription : BaseCommand {
67 InputSectionDescription() : BaseCommand(InputSectionKind) {}
68 static bool classof(const BaseCommand *C);
69 std::vector<StringRef> Patterns;
70};
71
Eugene Leviantbbe38602016-07-19 09:25:43 +000072struct PhdrsCommand {
73 StringRef Name;
74 unsigned Type;
75 bool HasFilehdr;
76 bool HasPhdrs;
Eugene Leviant865bf862016-07-21 10:43:25 +000077 unsigned Flags;
George Rimar652852c2016-04-16 10:10:32 +000078};
79
Rui Ueyama07320e42016-04-20 20:13:41 +000080// ScriptConfiguration holds linker script parse results.
81struct ScriptConfiguration {
George Rimar652852c2016-04-16 10:10:32 +000082 // Used to assign addresses to sections.
George Rimar076fe152016-07-21 06:43:01 +000083 std::vector<std::unique_ptr<BaseCommand>> Commands;
George Rimar652852c2016-04-16 10:10:32 +000084
Eugene Leviantbbe38602016-07-19 09:25:43 +000085 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +000086 std::vector<PhdrsCommand> PhdrsCommands;
87
Rui Ueyama07320e42016-04-20 20:13:41 +000088 bool DoLayout = false;
89
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000090 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +000091
92 // List of section patterns specified with KEEP commands. They will
93 // be kept even if they are unused and --gc-sections is specified.
94 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +000095};
96
Rui Ueyama07320e42016-04-20 20:13:41 +000097extern ScriptConfiguration *ScriptConfig;
98
99// This is a runner of the linker script.
100template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000101 typedef typename ELFT::uint uintX_t;
102
Rui Ueyama07320e42016-04-20 20:13:41 +0000103public:
Rafael Espindola74df5c72016-07-19 12:33:46 +0000104 typedef PhdrEntry<ELFT> Phdr;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000105
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000106 std::vector<OutputSectionBase<ELFT> *>
107 createSections(OutputSectionFactory<ELFT> &Factory);
108
Rui Ueyama07320e42016-04-20 20:13:41 +0000109 ArrayRef<uint8_t> getFiller(StringRef Name);
110 bool isDiscarded(InputSectionBase<ELFT> *S);
111 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000112 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000113 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000114 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000115 std::vector<Phdr> createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
116 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000117
118private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000119 // "ScriptConfig" is a bit too long, so define a short name for it.
120 ScriptConfiguration &Opt = *ScriptConfig;
121
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000122 int getSectionIndex(StringRef Name);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000123 std::vector<size_t> getPhdrIndicesForSection(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000124
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000125 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000126};
127
128// Variable template is a C++14 feature, so we can't template
129// a global variable. Use a struct to workaround.
130template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
131template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000132
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000133} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000134} // namespace lld
135
136#endif