Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 1 | //===- 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 Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Allocator.h" |
| 17 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 18 | |
| 19 | namespace lld { |
| 20 | namespace elf2 { |
| 21 | |
| 22 | class ScriptParser; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 23 | template <class ELFT> class InputSectionBase; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 24 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 25 | // This class represents each rule in SECTIONS command. |
| 26 | class SectionRule { |
| 27 | public: |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 28 | SectionRule(StringRef D, StringRef S, bool Keep) |
| 29 | : Dest(D), Keep(Keep), SectionPattern(S) {} |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 30 | |
| 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 Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 36 | // KEEP command saves unused sections even if --gc-sections is specified. |
| 37 | bool Keep = false; |
| 38 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 39 | private: |
| 40 | StringRef SectionPattern; |
| 41 | }; |
| 42 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame^] | 43 | struct OutSection { |
| 44 | StringRef Name; |
| 45 | std::vector<uint8_t> Filler; |
| 46 | }; |
| 47 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 48 | // This is a runner of the linker script. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 49 | class LinkerScript { |
| 50 | friend class ScriptParser; |
| 51 | |
| 52 | public: |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 53 | // Parses a linker script. Calling this function may update |
| 54 | // this object and Config. |
| 55 | void read(MemoryBufferRef MB); |
| 56 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 57 | template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame^] | 58 | ArrayRef<uint8_t> getFiller(StringRef Name); |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 59 | template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 60 | template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S); |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 61 | int compareSections(StringRef A, StringRef B); |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 62 | |
| 63 | private: |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 64 | template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S); |
| 65 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 66 | // SECTIONS commands. |
| 67 | std::vector<SectionRule> Sections; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 68 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame^] | 69 | // Output sections information. |
| 70 | // They are sorted by the order of the container. |
| 71 | std::vector<OutSection> OutSections; |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 72 | |
| 73 | llvm::BumpPtrAllocator Alloc; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | extern LinkerScript *Script; |
| 77 | |
| 78 | } // namespace elf2 |
| 79 | } // namespace lld |
| 80 | |
| 81 | #endif |