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; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 24 | template <class ELFT> class OutputSectionBase; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 25 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 26 | // This class represents each rule in SECTIONS command. |
| 27 | class SectionRule { |
| 28 | public: |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 29 | SectionRule(StringRef D, StringRef S, bool Keep) |
| 30 | : Dest(D), Keep(Keep), SectionPattern(S) {} |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 31 | |
| 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 Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 37 | // KEEP command saves unused sections even if --gc-sections is specified. |
| 38 | bool Keep = false; |
| 39 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 40 | private: |
| 41 | StringRef SectionPattern; |
| 42 | }; |
| 43 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 44 | // 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 :..." |
| 47 | enum class Command { Expr, Section }; |
| 48 | |
| 49 | struct LocationNode { |
| 50 | Command Type; |
| 51 | std::vector<StringRef> Expr; |
| 52 | StringRef SectionName; |
| 53 | }; |
| 54 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 55 | // This is a runner of the linker script. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 56 | class LinkerScript { |
| 57 | friend class ScriptParser; |
| 58 | |
| 59 | public: |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 60 | // Parses a linker script. Calling this function may update |
| 61 | // this object and Config. |
| 62 | void read(MemoryBufferRef MB); |
| 63 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 64 | template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 65 | ArrayRef<uint8_t> getFiller(StringRef Name); |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 66 | template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 67 | template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 68 | template <class ELFT> |
| 69 | void assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S); |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 70 | int compareSections(StringRef A, StringRef B); |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 71 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 72 | bool DoLayout = false; |
| 73 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 74 | private: |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 75 | template <class ELFT> |
| 76 | void fixupLocations(std::vector<OutputSectionBase<ELFT> *> &); |
| 77 | uint64_t evaluate(std::vector<StringRef> &Tokens, uint64_t LocCounter); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 78 | template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S); |
| 79 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 80 | // SECTIONS commands. |
| 81 | std::vector<SectionRule> Sections; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 82 | |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 83 | // 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 Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 88 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame^] | 89 | // Used to assign addresses to sections. |
| 90 | std::vector<LocationNode> Locations; |
| 91 | |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 92 | llvm::BumpPtrAllocator Alloc; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | extern LinkerScript *Script; |
| 96 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 97 | } // namespace elf |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 98 | } // namespace lld |
| 99 | |
| 100 | #endif |