| 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 { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 20 | namespace elf { |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 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 | |
| 43 | // This is a runner of the linker script. |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 44 | class LinkerScript { |
| 45 | friend class ScriptParser; |
| 46 | |
| 47 | public: |
| Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 48 | // Parses a linker script. Calling this function may update |
| 49 | // this object and Config. |
| 50 | void read(MemoryBufferRef MB); |
| 51 | |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 52 | template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S); |
| George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 53 | ArrayRef<uint8_t> getFiller(StringRef Name); |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 54 | template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S); |
| George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 55 | template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S); |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 56 | int compareSections(StringRef A, StringRef B); |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 57 | |
| 58 | private: |
| George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 59 | template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S); |
| 60 | |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 61 | // SECTIONS commands. |
| 62 | std::vector<SectionRule> Sections; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 63 | |
| Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 64 | // 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 Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 69 | |
| 70 | llvm::BumpPtrAllocator Alloc; |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | extern LinkerScript *Script; |
| 74 | |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 75 | } // namespace elf |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 76 | } // namespace lld |
| 77 | |
| 78 | #endif |