blob: c534478998f5e7be79ba7c53d93af10ddc9d4aee [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 {
20namespace elf2 {
21
22class ScriptParser;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000023template <class ELFT> class InputSectionBase;
Rui Ueyama717677a2016-02-11 21:17:59 +000024
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000025// This class represents each rule in SECTIONS command.
26class SectionRule {
27public:
George Rimar481c2ce2016-02-23 07:47:54 +000028 SectionRule(StringRef D, StringRef S, bool Keep)
29 : Dest(D), Keep(Keep), SectionPattern(S) {}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000030
31 // Returns true if S should be in Dest section.
32 template <class ELFT> bool match(InputSectionBase<ELFT> *S);
33
34 StringRef Dest;
35
George Rimar481c2ce2016-02-23 07:47:54 +000036 // KEEP command saves unused sections even if --gc-sections is specified.
37 bool Keep = false;
38
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000039private:
40 StringRef SectionPattern;
41};
42
George Rimare2ee72b2016-02-26 14:48:31 +000043struct OutSection {
44 StringRef Name;
45 std::vector<uint8_t> Filler;
46};
47
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000048// This is a runner of the linker script.
Rui Ueyama717677a2016-02-11 21:17:59 +000049class LinkerScript {
50 friend class ScriptParser;
51
52public:
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000053 // Parses a linker script. Calling this function may update
54 // this object and Config.
55 void read(MemoryBufferRef MB);
56
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000057 template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S);
George Rimare2ee72b2016-02-26 14:48:31 +000058 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000059 template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S);
George Rimar481c2ce2016-02-23 07:47:54 +000060 template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S);
Rui Ueyama717677a2016-02-11 21:17:59 +000061 int compareSections(StringRef A, StringRef B);
Rui Ueyama717677a2016-02-11 21:17:59 +000062
63private:
George Rimar481c2ce2016-02-23 07:47:54 +000064 template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S);
65
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000066 // SECTIONS commands.
67 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000068
George Rimare2ee72b2016-02-26 14:48:31 +000069 // Output sections information.
70 // They are sorted by the order of the container.
71 std::vector<OutSection> OutSections;
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000072
73 llvm::BumpPtrAllocator Alloc;
Rui Ueyama717677a2016-02-11 21:17:59 +000074};
75
76extern LinkerScript *Script;
77
78} // namespace elf2
79} // namespace lld
80
81#endif