blob: 3ef318d3dace5d14923a93521a9013149b59ba43 [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 Rimar652852c2016-04-16 10:10:32 +000044// This enum represents what we can observe in SECTIONS tag of script:
George Rimara3ab1092016-04-19 12:09:25 +000045// ExprKind is a location counter change, like ". = . + 0x1000"
46// SectionKind is a description of output section, like ".data :..."
Rui Ueyama05ef4cf2016-07-15 04:19:37 +000047enum SectionsCommandKind { SectionKind, AssignmentKind };
George Rimar652852c2016-04-16 10:10:32 +000048
Rui Ueyama9e957a02016-04-18 21:00:45 +000049struct SectionsCommand {
50 SectionsCommandKind Kind;
George Rimar652852c2016-04-16 10:10:32 +000051 std::vector<StringRef> Expr;
Eugene Levianteda81a12016-07-12 06:39:48 +000052 StringRef Name;
Eugene Leviantbbe38602016-07-19 09:25:43 +000053 std::vector<StringRef> Phdrs;
54};
55
56struct PhdrsCommand {
57 StringRef Name;
58 unsigned Type;
59 bool HasFilehdr;
60 bool HasPhdrs;
George Rimar652852c2016-04-16 10:10:32 +000061};
62
Rui Ueyama07320e42016-04-20 20:13:41 +000063// ScriptConfiguration holds linker script parse results.
64struct ScriptConfiguration {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000065 // SECTIONS commands.
66 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000067
Rui Ueyama3e808972016-02-28 05:09:11 +000068 // Section fill attribute for each section.
69 llvm::StringMap<std::vector<uint8_t>> Filler;
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000070
George Rimar652852c2016-04-16 10:10:32 +000071 // Used to assign addresses to sections.
Rui Ueyama9e957a02016-04-18 21:00:45 +000072 std::vector<SectionsCommand> Commands;
George Rimar652852c2016-04-16 10:10:32 +000073
Eugene Leviantbbe38602016-07-19 09:25:43 +000074 // Used to assign sections to headers.
George Rimar70ce0a92016-07-20 15:09:10 +000075 std::vector<PhdrsCommand> PhdrsCommands;
76
Rui Ueyama07320e42016-04-20 20:13:41 +000077 bool DoLayout = false;
78
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000079 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +000080
81 // List of section patterns specified with KEEP commands. They will
82 // be kept even if they are unused and --gc-sections is specified.
83 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +000084};
85
Rui Ueyama07320e42016-04-20 20:13:41 +000086extern ScriptConfiguration *ScriptConfig;
87
88// This is a runner of the linker script.
89template <class ELFT> class LinkerScript {
Rui Ueyama0b3868e2016-04-22 20:41:07 +000090 typedef typename ELFT::uint uintX_t;
91
Rui Ueyama07320e42016-04-20 20:13:41 +000092public:
Rafael Espindola74df5c72016-07-19 12:33:46 +000093 typedef PhdrEntry<ELFT> Phdr;
Eugene Leviantbbe38602016-07-19 09:25:43 +000094
Rui Ueyamaa7f78842016-07-20 17:19:03 +000095 std::vector<OutputSectionBase<ELFT> *>
96 createSections(OutputSectionFactory<ELFT> &Factory);
97
Rui Ueyama07320e42016-04-20 20:13:41 +000098 StringRef getOutputSection(InputSectionBase<ELFT> *S);
99 ArrayRef<uint8_t> getFiller(StringRef Name);
100 bool isDiscarded(InputSectionBase<ELFT> *S);
101 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +0000102 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +0000103 int compareSections(StringRef A, StringRef B);
Eugene Levianteda81a12016-07-12 06:39:48 +0000104 void addScriptedSymbols();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000105 std::vector<Phdr> createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S);
106 bool hasPhdrsCommands();
Rui Ueyama07320e42016-04-20 20:13:41 +0000107
108private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000109 // "ScriptConfig" is a bit too long, so define a short name for it.
110 ScriptConfiguration &Opt = *ScriptConfig;
111
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000112 int getSectionIndex(StringRef Name);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000113 std::vector<size_t> getPhdrIndicesForSection(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +0000114
Rui Ueyama0b3868e2016-04-22 20:41:07 +0000115 uintX_t Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +0000116};
117
118// Variable template is a C++14 feature, so we can't template
119// a global variable. Use a struct to workaround.
120template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
121template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000122
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000123} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000124} // namespace lld
125
126#endif