blob: c67ab1fc3174f7eb8d6caf2167f01bce1a313841 [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
22class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000023template <class ELFT> class InputSectionBase;
George Rimar652852c2016-04-16 10:10:32 +000024template <class ELFT> class OutputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000025
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000026// This class represents each rule in SECTIONS command.
27class SectionRule {
28public:
George Rimar481c2ce2016-02-23 07:47:54 +000029 SectionRule(StringRef D, StringRef S, bool Keep)
30 : Dest(D), Keep(Keep), SectionPattern(S) {}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000031
32 // Returns true if S should be in Dest section.
33 template <class ELFT> bool match(InputSectionBase<ELFT> *S);
34
35 StringRef Dest;
36
George Rimar481c2ce2016-02-23 07:47:54 +000037 // KEEP command saves unused sections even if --gc-sections is specified.
38 bool Keep = false;
39
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000040private:
41 StringRef SectionPattern;
42};
43
George Rimar652852c2016-04-16 10:10:32 +000044// This enum represents what we can observe in SECTIONS tag of script:
45// Expr is a location counter change, like ". = . + 0x1000"
46// Section is a description of output section, like ".data :..."
47enum class Command { Expr, Section };
48
49struct LocationNode {
50 Command Type;
51 std::vector<StringRef> Expr;
52 StringRef SectionName;
53};
54
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000055// This is a runner of the linker script.
Rui Ueyama717677a2016-02-11 21:17:59 +000056class LinkerScript {
57 friend class ScriptParser;
58
59public:
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000060 // Parses a linker script. Calling this function may update
61 // this object and Config.
62 void read(MemoryBufferRef MB);
63
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000064 template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S);
George Rimare2ee72b2016-02-26 14:48:31 +000065 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000066 template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S);
George Rimar481c2ce2016-02-23 07:47:54 +000067 template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S);
George Rimar652852c2016-04-16 10:10:32 +000068 template <class ELFT>
69 void assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S);
Rui Ueyama717677a2016-02-11 21:17:59 +000070 int compareSections(StringRef A, StringRef B);
Rui Ueyama717677a2016-02-11 21:17:59 +000071
George Rimar652852c2016-04-16 10:10:32 +000072 bool DoLayout = false;
73
Rui Ueyama717677a2016-02-11 21:17:59 +000074private:
George Rimar652852c2016-04-16 10:10:32 +000075 template <class ELFT>
76 void fixupLocations(std::vector<OutputSectionBase<ELFT> *> &);
77 uint64_t evaluate(std::vector<StringRef> &Tokens, uint64_t LocCounter);
George Rimar481c2ce2016-02-23 07:47:54 +000078 template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S);
79
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000080 // SECTIONS commands.
81 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000082
Rui Ueyama3e808972016-02-28 05:09:11 +000083 // Output sections are sorted by this order.
84 std::vector<StringRef> SectionOrder;
85
86 // Section fill attribute for each section.
87 llvm::StringMap<std::vector<uint8_t>> Filler;
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000088
George Rimar652852c2016-04-16 10:10:32 +000089 // Used to assign addresses to sections.
90 std::vector<LocationNode> Locations;
91
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000092 llvm::BumpPtrAllocator Alloc;
Rui Ueyama717677a2016-02-11 21:17:59 +000093};
94
95extern LinkerScript *Script;
96
Rafael Espindolae0df00b2016-02-28 00:25:54 +000097} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +000098} // namespace lld
99
100#endif