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 | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 19 | #include <functional> |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 20 | |
| 21 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 22 | namespace elf { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 23 | template <class ELFT> class InputSectionBase; |
| 24 | template <class ELFT> class OutputSectionBase; |
| 25 | template <class ELFT> class OutputSectionFactory; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 26 | |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 27 | typedef std::function<uint64_t(uint64_t)> Expr; |
| 28 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 29 | // Parses a linker script. Calling this function updates |
| 30 | // Config and ScriptConfig. |
| 31 | void readLinkerScript(MemoryBufferRef MB); |
| 32 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 33 | class ScriptParser; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 34 | template <class ELFT> class InputSectionBase; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 35 | template <class ELFT> class OutputSectionBase; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 36 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 37 | // This enum is used to implement linker script SECTIONS command. |
| 38 | // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS |
| 39 | enum SectionsCommandKind { |
| 40 | AssignmentKind, |
| 41 | OutputSectionKind, |
| 42 | InputSectionKind |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 45 | struct BaseCommand { |
| 46 | BaseCommand(int K) : Kind(K) {} |
| 47 | virtual ~BaseCommand() {} |
| 48 | int Kind; |
| 49 | }; |
| 50 | |
| 51 | struct SymbolAssignment : BaseCommand { |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 52 | SymbolAssignment(StringRef Name, Expr E) |
| 53 | : BaseCommand(AssignmentKind), Name(Name), Expression(E) {} |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 54 | static bool classof(const BaseCommand *C); |
| 55 | StringRef Name; |
Rui Ueyama | 708019c | 2016-07-24 18:19:40 +0000 | [diff] [blame] | 56 | Expr Expression; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 57 | bool Provide = false; |
| 58 | // Hidden and Ignore can be true, only if Provide is true |
| 59 | bool Hidden = false; |
| 60 | bool Ignore = false; |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 63 | // Linker scripts allow additional constraints to be put on ouput sections. |
| 64 | // An output section will only be created if all of its input sections are |
| 65 | // read-only |
| 66 | // or all of its input sections are read-write by using the keyword ONLY_IF_RO |
| 67 | // and ONLY_IF_RW respectively. |
| 68 | enum ConstraintKind { NoConstraint, ReadOnly, ReadWrite }; |
| 69 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 70 | struct OutputSectionCommand : BaseCommand { |
| 71 | OutputSectionCommand(StringRef Name) |
| 72 | : BaseCommand(OutputSectionKind), Name(Name) {} |
| 73 | static bool classof(const BaseCommand *C); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 74 | StringRef Name; |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 75 | std::vector<std::unique_ptr<BaseCommand>> Commands; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 76 | std::vector<StringRef> Phdrs; |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 77 | std::vector<uint8_t> Filler; |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 78 | ConstraintKind Constraint = NoConstraint; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 81 | struct InputSectionDescription : BaseCommand { |
| 82 | InputSectionDescription() : BaseCommand(InputSectionKind) {} |
| 83 | static bool classof(const BaseCommand *C); |
| 84 | std::vector<StringRef> Patterns; |
| 85 | }; |
| 86 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 87 | struct PhdrsCommand { |
| 88 | StringRef Name; |
| 89 | unsigned Type; |
| 90 | bool HasFilehdr; |
| 91 | bool HasPhdrs; |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 92 | unsigned Flags; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 95 | // ScriptConfiguration holds linker script parse results. |
| 96 | struct ScriptConfiguration { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 97 | // Used to assign addresses to sections. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 98 | std::vector<std::unique_ptr<BaseCommand>> Commands; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 99 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 100 | // Used to assign sections to headers. |
George Rimar | 70ce0a9 | 2016-07-20 15:09:10 +0000 | [diff] [blame] | 101 | std::vector<PhdrsCommand> PhdrsCommands; |
| 102 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 103 | bool DoLayout = false; |
| 104 | |
Rui Ueyama | f9de0d6 | 2016-02-11 21:38:55 +0000 | [diff] [blame] | 105 | llvm::BumpPtrAllocator Alloc; |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 106 | |
| 107 | // List of section patterns specified with KEEP commands. They will |
| 108 | // be kept even if they are unused and --gc-sections is specified. |
| 109 | std::vector<StringRef> KeptSections; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 112 | extern ScriptConfiguration *ScriptConfig; |
| 113 | |
| 114 | // This is a runner of the linker script. |
| 115 | template <class ELFT> class LinkerScript { |
Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 116 | typedef typename ELFT::uint uintX_t; |
| 117 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 118 | public: |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 119 | std::vector<OutputSectionBase<ELFT> *> |
| 120 | createSections(OutputSectionFactory<ELFT> &Factory); |
| 121 | |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 122 | std::vector<PhdrEntry<ELFT>> |
| 123 | createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> S); |
| 124 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 125 | ArrayRef<uint8_t> getFiller(StringRef Name); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 126 | bool shouldKeep(InputSectionBase<ELFT> *S); |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 127 | void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 128 | int compareSections(StringRef A, StringRef B); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 129 | void addScriptedSymbols(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 130 | bool hasPhdrsCommands(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 131 | |
| 132 | private: |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 133 | // "ScriptConfig" is a bit too long, so define a short name for it. |
| 134 | ScriptConfiguration &Opt = *ScriptConfig; |
| 135 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 136 | int getSectionIndex(StringRef Name); |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame^] | 137 | std::vector<size_t> getPhdrIndices(StringRef SectionName); |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 138 | void dispatchAssignment(SymbolAssignment *Cmd); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 139 | |
Rui Ueyama | 0b3868e | 2016-04-22 20:41:07 +0000 | [diff] [blame] | 140 | uintX_t Dot; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | // Variable template is a C++14 feature, so we can't template |
| 144 | // a global variable. Use a struct to workaround. |
| 145 | template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; |
| 146 | template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 147 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 148 | } // namespace elf |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 149 | } // namespace lld |
| 150 | |
| 151 | #endif |