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 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 13 | #include "Writer.h" |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 14 | #include "lld/Core/LLVM.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/MapVector.h" |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Allocator.h" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 19 | |
| 20 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 21 | namespace elf { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 22 | template <class ELFT> class InputSectionBase; |
| 23 | template <class ELFT> class OutputSectionBase; |
| 24 | template <class ELFT> class OutputSectionFactory; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 25 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 26 | // Parses a linker script. Calling this function updates |
| 27 | // Config and ScriptConfig. |
| 28 | void readLinkerScript(MemoryBufferRef MB); |
| 29 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 30 | class ScriptParser; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 31 | template <class ELFT> class InputSectionBase; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 32 | template <class ELFT> class OutputSectionBase; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 33 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 34 | // This class represents each rule in SECTIONS command. |
Rui Ueyama | c9f402e | 2016-04-22 00:23:52 +0000 | [diff] [blame] | 35 | struct SectionRule { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 36 | SectionRule(StringRef D, StringRef S) |
| 37 | : Dest(D), SectionPattern(S) {} |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 38 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 39 | StringRef Dest; |
| 40 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 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: |
George Rimar | a3ab109 | 2016-04-19 12:09:25 +0000 | [diff] [blame] | 45 | // ExprKind is a location counter change, like ". = . + 0x1000" |
| 46 | // SectionKind is a description of output section, like ".data :..." |
Rui Ueyama | 05ef4cf | 2016-07-15 04:19:37 +0000 | [diff] [blame] | 47 | enum SectionsCommandKind { SectionKind, AssignmentKind }; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 48 | |
Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 49 | struct SectionsCommand { |
| 50 | SectionsCommandKind Kind; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 51 | std::vector<StringRef> Expr; |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 52 | StringRef Name; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 53 | std::vector<StringRef> Phdrs; |
| 54 | }; |
| 55 | |
| 56 | struct PhdrsCommand { |
| 57 | StringRef Name; |
| 58 | unsigned Type; |
| 59 | bool HasFilehdr; |
| 60 | bool HasPhdrs; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 63 | // ScriptConfiguration holds linker script parse results. |
| 64 | struct ScriptConfiguration { |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 65 | // SECTIONS commands. |
| 66 | std::vector<SectionRule> Sections; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 67 | |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 68 | // Section fill attribute for each section. |
| 69 | llvm::StringMap<std::vector<uint8_t>> Filler; |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 70 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 71 | // Used to assign addresses to sections. |
Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 72 | std::vector<SectionsCommand> Commands; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 73 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 74 | // Used to assign sections to headers. |
George Rimar | 70ce0a9 | 2016-07-20 15:09:10 +0000 | [diff] [blame] | 75 | std::vector<PhdrsCommand> PhdrsCommands; |
| 76 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 77 | bool DoLayout = false; |
| 78 | |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 79 | llvm::BumpPtrAllocator Alloc; |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 80 | |
| 81 | // List of section patterns specified with KEEP commands. They will |
| 82 | // be kept even if they are unused and --gc-sections is specified. |
| 83 | std::vector<StringRef> KeptSections; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 86 | extern ScriptConfiguration *ScriptConfig; |
| 87 | |
| 88 | // This is a runner of the linker script. |
| 89 | template <class ELFT> class LinkerScript { |
Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 90 | typedef typename ELFT::uint uintX_t; |
| 91 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 92 | public: |
Rafael Espindola | 74df5c7 | 2016-07-19 12:33:46 +0000 | [diff] [blame] | 93 | typedef PhdrEntry<ELFT> Phdr; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 94 | |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame^] | 95 | std::vector<OutputSectionBase<ELFT> *> |
| 96 | createSections(OutputSectionFactory<ELFT> &Factory); |
| 97 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 98 | StringRef getOutputSection(InputSectionBase<ELFT> *S); |
| 99 | ArrayRef<uint8_t> getFiller(StringRef Name); |
| 100 | bool isDiscarded(InputSectionBase<ELFT> *S); |
| 101 | bool shouldKeep(InputSectionBase<ELFT> *S); |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 102 | void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 103 | int compareSections(StringRef A, StringRef B); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 104 | void addScriptedSymbols(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 105 | std::vector<Phdr> createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S); |
| 106 | bool hasPhdrsCommands(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 107 | |
| 108 | private: |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 109 | // "ScriptConfig" is a bit too long, so define a short name for it. |
| 110 | ScriptConfiguration &Opt = *ScriptConfig; |
| 111 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 112 | int getSectionIndex(StringRef Name); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 113 | std::vector<size_t> getPhdrIndicesForSection(StringRef Name); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 114 | |
Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 115 | uintX_t Dot; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // Variable template is a C++14 feature, so we can't template |
| 119 | // a global variable. Use a struct to workaround. |
| 120 | template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; |
| 121 | template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 122 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 123 | } // namespace elf |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 124 | } // namespace lld |
| 125 | |
| 126 | #endif |