blob: 25c668fe77926dfa8c031bf2d887dc3efe735ffa [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
13#include "lld/Core/LLVM.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/MapVector.h"
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000016#include "llvm/Support/Allocator.h"
17#include "llvm/Support/MemoryBuffer.h"
Rui Ueyama717677a2016-02-11 21:17:59 +000018
19namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000020namespace elf {
Rui Ueyama717677a2016-02-11 21:17:59 +000021
Rui Ueyama07320e42016-04-20 20:13:41 +000022// Parses a linker script. Calling this function updates
23// Config and ScriptConfig.
24void readLinkerScript(MemoryBufferRef MB);
25
Rui Ueyama717677a2016-02-11 21:17:59 +000026class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000027template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000028template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000029
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000030// This class represents each rule in SECTIONS command.
Rui Ueyamac9f402e2016-04-22 00:23:52 +000031struct SectionRule {
Rui Ueyama8ec77e62016-04-21 22:00:51 +000032 SectionRule(StringRef D, StringRef S)
33 : Dest(D), SectionPattern(S) {}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000034
35 // Returns true if S should be in Dest section.
36 template <class ELFT> bool match(InputSectionBase<ELFT> *S);
37
38 StringRef Dest;
39
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000040 StringRef SectionPattern;
41};
42
George Rimar652852c2016-04-16 10:10:32 +000043// This enum represents what we can observe in SECTIONS tag of script:
George Rimara3ab1092016-04-19 12:09:25 +000044// ExprKind is a location counter change, like ". = . + 0x1000"
45// SectionKind is a description of output section, like ".data :..."
Rui Ueyama9e957a02016-04-18 21:00:45 +000046enum SectionsCommandKind { ExprKind, SectionKind };
George Rimar652852c2016-04-16 10:10:32 +000047
Rui Ueyama9e957a02016-04-18 21:00:45 +000048struct SectionsCommand {
49 SectionsCommandKind Kind;
George Rimar652852c2016-04-16 10:10:32 +000050 std::vector<StringRef> Expr;
51 StringRef SectionName;
52};
53
Rui Ueyama07320e42016-04-20 20:13:41 +000054// ScriptConfiguration holds linker script parse results.
55struct ScriptConfiguration {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000056 // SECTIONS commands.
57 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000058
Rui Ueyama3e808972016-02-28 05:09:11 +000059 // Section fill attribute for each section.
60 llvm::StringMap<std::vector<uint8_t>> Filler;
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000061
George Rimar652852c2016-04-16 10:10:32 +000062 // Used to assign addresses to sections.
Rui Ueyama9e957a02016-04-18 21:00:45 +000063 std::vector<SectionsCommand> Commands;
George Rimar652852c2016-04-16 10:10:32 +000064
Rui Ueyama07320e42016-04-20 20:13:41 +000065 bool DoLayout = false;
66
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000067 llvm::BumpPtrAllocator Alloc;
Rui Ueyama8ec77e62016-04-21 22:00:51 +000068
69 // List of section patterns specified with KEEP commands. They will
70 // be kept even if they are unused and --gc-sections is specified.
71 std::vector<StringRef> KeptSections;
Rui Ueyama717677a2016-02-11 21:17:59 +000072};
73
Rui Ueyama07320e42016-04-20 20:13:41 +000074extern ScriptConfiguration *ScriptConfig;
75
76// This is a runner of the linker script.
77template <class ELFT> class LinkerScript {
78public:
79 StringRef getOutputSection(InputSectionBase<ELFT> *S);
80 ArrayRef<uint8_t> getFiller(StringRef Name);
81 bool isDiscarded(InputSectionBase<ELFT> *S);
82 bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimardbbd8b12016-04-21 11:21:48 +000083 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S);
Rui Ueyama07320e42016-04-20 20:13:41 +000084 int compareSections(StringRef A, StringRef B);
85
86private:
Rui Ueyamac998a8c2016-04-22 00:03:13 +000087 // "ScriptConfig" is a bit too long, so define a short name for it.
88 ScriptConfiguration &Opt = *ScriptConfig;
89
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +000090 int getSectionIndex(StringRef Name);
Rui Ueyama07320e42016-04-20 20:13:41 +000091
Rui Ueyamac998a8c2016-04-22 00:03:13 +000092 uint64_t evaluate(ArrayRef<StringRef> Tokens);
93 uint64_t parseExpr(ArrayRef<StringRef> &Tokens);
94 uint64_t parsePrimary(ArrayRef<StringRef> &Tokens);
95 uint64_t parseExpr1(ArrayRef<StringRef> &Tokens, uint64_t Lhs, int MinPrec);
George Rimarfba45c42016-04-22 11:28:54 +000096 uint64_t parseTernary(ArrayRef<StringRef> &Tokens, uint64_t Cond);
Rui Ueyamac998a8c2016-04-22 00:03:13 +000097 typename ELFT::uint Dot;
Rui Ueyama07320e42016-04-20 20:13:41 +000098};
99
100// Variable template is a C++14 feature, so we can't template
101// a global variable. Use a struct to workaround.
102template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
103template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
Rui Ueyama717677a2016-02-11 21:17:59 +0000104
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000105} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +0000106} // namespace lld
107
108#endif