blob: e4a7a900bfd1007a94b0693562414396e3cc07bf [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;
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
43// This is a runner of the linker script.
Rui Ueyama717677a2016-02-11 21:17:59 +000044class LinkerScript {
45 friend class ScriptParser;
46
47public:
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000048 // Parses a linker script. Calling this function may update
49 // this object and Config.
50 void read(MemoryBufferRef MB);
51
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000052 template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S);
George Rimare2ee72b2016-02-26 14:48:31 +000053 ArrayRef<uint8_t> getFiller(StringRef Name);
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000054 template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S);
George Rimar481c2ce2016-02-23 07:47:54 +000055 template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S);
Rui Ueyama717677a2016-02-11 21:17:59 +000056 int compareSections(StringRef A, StringRef B);
Rui Ueyama717677a2016-02-11 21:17:59 +000057
58private:
George Rimar481c2ce2016-02-23 07:47:54 +000059 template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S);
60
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000061 // SECTIONS commands.
62 std::vector<SectionRule> Sections;
Rui Ueyama717677a2016-02-11 21:17:59 +000063
Rui Ueyama3e808972016-02-28 05:09:11 +000064 // Output sections are sorted by this order.
65 std::vector<StringRef> SectionOrder;
66
67 // Section fill attribute for each section.
68 llvm::StringMap<std::vector<uint8_t>> Filler;
Rui Ueyamaf9de0d62016-02-11 21:38:55 +000069
70 llvm::BumpPtrAllocator Alloc;
Rui Ueyama717677a2016-02-11 21:17:59 +000071};
72
73extern LinkerScript *Script;
74
Rafael Espindolae0df00b2016-02-28 00:25:54 +000075} // namespace elf
Rui Ueyama717677a2016-02-11 21:17:59 +000076} // namespace lld
77
78#endif