| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 1 | //===- LinkerScript.cpp ---------------------------------------------------===// |
| 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 | // This file contains the parser/evaluator of the linker script. |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 14 | #include "LinkerScript.h" |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 15 | #include "Config.h" |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 16 | #include "InputSection.h" |
| Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 17 | #include "Memory.h" |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 18 | #include "OutputSections.h" |
| Rui Ueyama | 93c9af4 | 2016-06-29 08:01:32 +0000 | [diff] [blame] | 19 | #include "Strings.h" |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 20 | #include "SymbolTable.h" |
| Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 21 | #include "Symbols.h" |
| George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 22 | #include "SyntheticSections.h" |
| Rafael Espindola | 55b169b | 2017-05-24 18:08:04 +0000 | [diff] [blame] | 23 | #include "Target.h" |
| 24 | #include "Threads.h" |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 25 | #include "Writer.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/ADT/StringRef.h" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 28 | #include "llvm/BinaryFormat/ELF.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Casting.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Endian.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FileSystem.h" |
| Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Path.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <cassert> |
| 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | #include <iterator> |
| 39 | #include <limits> |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 40 | #include <string> |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 41 | #include <vector> |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace llvm; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 44 | using namespace llvm::ELF; |
| Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 45 | using namespace llvm::object; |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 46 | using namespace llvm::support::endian; |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 47 | using namespace lld; |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 48 | using namespace lld::elf; |
| Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 49 | |
| Rui Ueyama | a34da93 | 2017-03-21 23:03:09 +0000 | [diff] [blame] | 50 | LinkerScript *elf::Script; |
| 51 | |
| George Rimar | 5d0ea70 | 2017-08-21 07:57:12 +0000 | [diff] [blame] | 52 | static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) { |
| 53 | if (OutputSection *OS = InputSec->getOutputSection()) |
| 54 | return OS->Addr; |
| 55 | error(Loc + ": unable to evaluate expression: input section " + |
| 56 | InputSec->Name + " has no output section assigned"); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 60 | uint64_t ExprValue::getValue() const { |
| George Rimar | 5d0ea70 | 2017-08-21 07:57:12 +0000 | [diff] [blame] | 61 | if (Sec) |
| 62 | return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc), |
| 63 | Alignment); |
| Petr Hosek | 3c6de1a | 2017-05-30 03:18:28 +0000 | [diff] [blame] | 64 | return alignTo(Val, Alignment); |
| Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| Rafael Espindola | 7ba5f47 | 2017-03-17 14:55:36 +0000 | [diff] [blame] | 67 | uint64_t ExprValue::getSecAddr() const { |
| 68 | if (Sec) |
| George Rimar | 5d0ea70 | 2017-08-21 07:57:12 +0000 | [diff] [blame] | 69 | return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc); |
| Rafael Espindola | 7ba5f47 | 2017-03-17 14:55:36 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| Rafael Espindola | a6acd23 | 2017-09-12 00:06:00 +0000 | [diff] [blame] | 73 | uint64_t ExprValue::getSectionOffset() const { |
| Rafael Espindola | 8b25034 | 2017-09-20 17:43:44 +0000 | [diff] [blame] | 74 | // If the alignment is trivial, we don't have to compute the full |
| 75 | // value to know the offset. This allows this function to succeed in |
| 76 | // cases where the output section is not yet known. |
| 77 | if (Alignment == 1) |
| 78 | return Val; |
| Rafael Espindola | a6acd23 | 2017-09-12 00:06:00 +0000 | [diff] [blame] | 79 | return getValue() - getSecAddr(); |
| 80 | } |
| 81 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 82 | OutputSection *LinkerScript::createOutputSection(StringRef Name, |
| 83 | StringRef Location) { |
| 84 | OutputSection *&SecRef = NameToOutputSection[Name]; |
| 85 | OutputSection *Sec; |
| 86 | if (SecRef && SecRef->Location.empty()) { |
| Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 87 | // There was a forward reference. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 88 | Sec = SecRef; |
| Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 89 | } else { |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 90 | Sec = make<OutputSection>(Name, SHT_PROGBITS, 0); |
| 91 | if (!SecRef) |
| 92 | SecRef = Sec; |
| Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 93 | } |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 94 | Sec->Location = Location; |
| 95 | return Sec; |
| George Rimar | 851dc1e | 2017-03-14 10:15:53 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 98 | OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) { |
| 99 | OutputSection *&CmdRef = NameToOutputSection[Name]; |
| Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 100 | if (!CmdRef) |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 101 | CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0); |
| Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 102 | return CmdRef; |
| George Rimar | d83ce1b | 2017-03-14 10:24:47 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 105 | void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) { |
| Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 106 | uint64_t Val = E().getValue(); |
| George Rimar | 8c804d9 | 2017-07-12 14:50:25 +0000 | [diff] [blame] | 107 | if (Val < Dot && InSec) |
| 108 | error(Loc + ": unable to move location counter backward for: " + |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 109 | Ctx->OutSec->Name); |
| Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 110 | Dot = Val; |
| Rui Ueyama | 18d1968 | 2017-10-11 01:03:37 +0000 | [diff] [blame] | 111 | |
| Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 112 | // Update to location counter means update to section size. |
| 113 | if (InSec) |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 114 | Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr; |
| Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| Rui Ueyama | 5908c2f | 2017-10-11 02:28:28 +0000 | [diff] [blame] | 117 | // This function is called from processSectionCommands, |
| 118 | // while we are fixing the output section layout. |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 119 | void LinkerScript::addSymbol(SymbolAssignment *Cmd) { |
| Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 120 | if (Cmd->Name == ".") |
| Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 121 | return; |
| 122 | |
| 123 | // If a symbol was in PROVIDE(), we need to define it only when |
| 124 | // it is a referenced undefined symbol. |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 125 | SymbolBody *B = Symtab->find(Cmd->Name); |
| Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 126 | if (Cmd->Provide && (!B || B->isDefined())) |
| 127 | return; |
| 128 | |
| Rui Ueyama | 18d1968 | 2017-10-11 01:03:37 +0000 | [diff] [blame] | 129 | // Define a symbol. |
| 130 | Symbol *Sym; |
| 131 | uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; |
| 132 | std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility, |
| 133 | /*CanOmitFromDynSym*/ false, |
| 134 | /*File*/ nullptr); |
| 135 | Sym->Binding = STB_GLOBAL; |
| 136 | ExprValue Value = Cmd->Expression(); |
| 137 | SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec; |
| 138 | |
| 139 | // When this function is called, section addresses have not been |
| 140 | // fixed yet. So, we may or may not know the value of the RHS |
| 141 | // expression. |
| 142 | // |
| 143 | // For example, if an expression is `x = 42`, we know x is always 42. |
| 144 | // However, if an expression is `x = .`, there's no way to know its |
| 145 | // value at the moment. |
| 146 | // |
| 147 | // We want to set symbol values early if we can. This allows us to |
| 148 | // use symbols as variables in linker scripts. Doing so allows us to |
| 149 | // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`. |
| 150 | uint64_t SymValue = Value.Sec ? 0 : Value.getValue(); |
| 151 | |
| 152 | replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false, |
| 153 | Visibility, STT_NOTYPE, SymValue, 0, Sec); |
| 154 | Cmd->Sym = cast<DefinedRegular>(Sym->body()); |
| 155 | } |
| 156 | |
| 157 | // This function is called from assignAddresses, while we are |
| 158 | // fixing the output section addresses. This function is supposed |
| 159 | // to set the final value for a given symbol assignment. |
| 160 | void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) { |
| 161 | if (Cmd->Name == ".") { |
| 162 | setDot(Cmd->Expression, Cmd->Location, InSec); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (!Cmd->Sym) |
| 167 | return; |
| 168 | |
| 169 | ExprValue V = Cmd->Expression(); |
| 170 | if (V.isAbsolute()) { |
| 171 | Cmd->Sym->Section = nullptr; |
| 172 | Cmd->Sym->Value = V.getValue(); |
| 173 | } else { |
| 174 | Cmd->Sym->Section = V.Sec; |
| 175 | Cmd->Sym->Value = V.getSectionOffset(); |
| 176 | } |
| Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| Rui Ueyama | 04c9ca7 | 2017-10-11 02:54:52 +0000 | [diff] [blame] | 179 | static std::string getFilename(InputFile *File) { |
| Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 180 | if (!File) |
| Dmitry Mikulin | f300ca2 | 2017-08-24 22:01:40 +0000 | [diff] [blame] | 181 | return ""; |
| Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 182 | if (File->ArchiveName.empty()) |
| 183 | return File->getName(); |
| 184 | return (File->ArchiveName + "(" + File->getName() + ")").str(); |
| Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 187 | bool LinkerScript::shouldKeep(InputSectionBase *S) { |
| Rui Ueyama | 04c9ca7 | 2017-10-11 02:54:52 +0000 | [diff] [blame] | 188 | std::string Filename = getFilename(S->File); |
| Rui Ueyama | 7f1c266 | 2017-10-11 01:26:22 +0000 | [diff] [blame] | 189 | for (InputSectionDescription *ID : KeptSections) |
| Dmitry Mikulin | f300ca2 | 2017-08-24 22:01:40 +0000 | [diff] [blame] | 190 | if (ID->FilePat.match(Filename)) |
| Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 191 | for (SectionPattern &P : ID->SectionPatterns) |
| 192 | if (P.SectionPat.match(S->Name)) |
| 193 | return true; |
| George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
| Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 197 | // A helper function for the SORT() command. |
| Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 198 | static std::function<bool(InputSectionBase *, InputSectionBase *)> |
| George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 199 | getComparator(SortSectionPolicy K) { |
| 200 | switch (K) { |
| 201 | case SortSectionPolicy::Alignment: |
| Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 202 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 203 | // ">" is not a mistake. Sections with larger alignments are placed |
| 204 | // before sections with smaller alignments in order to reduce the |
| 205 | // amount of padding necessary. This is compatible with GNU. |
| 206 | return A->Alignment > B->Alignment; |
| 207 | }; |
| George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 208 | case SortSectionPolicy::Name: |
| Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 209 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 210 | return A->Name < B->Name; |
| 211 | }; |
| George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 212 | case SortSectionPolicy::Priority: |
| Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 213 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 214 | return getPriority(A->Name) < getPriority(B->Name); |
| 215 | }; |
| George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 216 | default: |
| 217 | llvm_unreachable("unknown sort policy"); |
| 218 | } |
| Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 219 | } |
| George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 220 | |
| Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 221 | // A helper function for the SORT() command. |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 222 | static bool matchConstraints(ArrayRef<InputSectionBase *> Sections, |
| George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 223 | ConstraintKind Kind) { |
| George Rimar | 8f66df9 | 2016-08-12 20:38:20 +0000 | [diff] [blame] | 224 | if (Kind == ConstraintKind::NoConstraint) |
| 225 | return true; |
| Rui Ueyama | 2c7171b | 2017-04-05 00:43:45 +0000 | [diff] [blame] | 226 | |
| Rui Ueyama | b801441 | 2017-10-11 02:55:05 +0000 | [diff] [blame] | 227 | bool IsRW = llvm::any_of( |
| 228 | Sections, [](InputSectionBase *Sec) { return Sec->Flags & SHF_WRITE; }); |
| Rui Ueyama | 2c7171b | 2017-04-05 00:43:45 +0000 | [diff] [blame] | 229 | |
| Rafael Espindola | e746e52 | 2016-09-21 18:33:44 +0000 | [diff] [blame] | 230 | return (IsRW && Kind == ConstraintKind::ReadWrite) || |
| 231 | (!IsRW && Kind == ConstraintKind::ReadOnly); |
| George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 234 | static void sortSections(MutableArrayRef<InputSection *> Vec, |
| Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 235 | SortSectionPolicy K) { |
| 236 | if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 237 | std::stable_sort(Vec.begin(), Vec.end(), getComparator(K)); |
| Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 240 | static void sortBySymbolOrder(MutableArrayRef<InputSection *> Vec) { |
| George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 241 | if (Config->SymbolOrderingFile.empty()) |
| 242 | return; |
| George Rimar | 696a7f9 | 2017-09-19 09:20:54 +0000 | [diff] [blame] | 243 | static llvm::DenseMap<SectionBase *, int> Order = buildSectionOrder(); |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 244 | sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); }); |
| George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 247 | // Compute and remember which sections the InputSectionDescription matches. |
| Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 248 | std::vector<InputSection *> |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 249 | LinkerScript::computeInputSections(const InputSectionDescription *Cmd) { |
| Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 250 | std::vector<InputSection *> Ret; |
| Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 251 | |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 252 | // Collects all sections that satisfy constraints of Cmd. |
| 253 | for (const SectionPattern &Pat : Cmd->SectionPatterns) { |
| 254 | size_t SizeBefore = Ret.size(); |
| 255 | |
| 256 | for (InputSectionBase *Sec : InputSections) { |
| 257 | if (Sec->Assigned) |
| Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 258 | continue; |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 259 | |
| Rafael Espindola | e39709b | 2017-05-30 20:40:03 +0000 | [diff] [blame] | 260 | if (!Sec->Live) { |
| 261 | reportDiscarded(Sec); |
| 262 | continue; |
| 263 | } |
| 264 | |
| Rafael Espindola | 908a3d3 | 2017-02-16 14:36:09 +0000 | [diff] [blame] | 265 | // For -emit-relocs we have to ignore entries like |
| 266 | // .rela.dyn : { *(.rela.data) } |
| 267 | // which are common because they are in the default bfd script. |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 268 | if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA) |
| Rafael Espindola | 908a3d3 | 2017-02-16 14:36:09 +0000 | [diff] [blame] | 269 | continue; |
| Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 270 | |
| Rui Ueyama | 04c9ca7 | 2017-10-11 02:54:52 +0000 | [diff] [blame] | 271 | std::string Filename = getFilename(Sec->File); |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 272 | if (!Cmd->FilePat.match(Filename) || |
| 273 | Pat.ExcludedFilePat.match(Filename) || |
| 274 | !Pat.SectionPat.match(Sec->Name)) |
| Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 275 | continue; |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 276 | |
| Rui Ueyama | ec5c4ad | 2017-10-11 03:23:17 +0000 | [diff] [blame] | 277 | // It is safe to assume that Sec is an InputSection |
| 278 | // because mergeable or EH input sections have already been |
| 279 | // handled and eliminated. |
| Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 280 | Ret.push_back(cast<InputSection>(Sec)); |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 281 | Sec->Assigned = true; |
| George Rimar | 395281c | 2016-09-16 17:42:10 +0000 | [diff] [blame] | 282 | } |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 283 | |
| George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 284 | // Sort sections as instructed by SORT-family commands and --sort-section |
| 285 | // option. Because SORT-family commands can be nested at most two depth |
| 286 | // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command |
| 287 | // line option is respected even if a SORT command is given, the exact |
| 288 | // behavior we have here is a bit complicated. Here are the rules. |
| 289 | // |
| 290 | // 1. If two SORT commands are given, --sort-section is ignored. |
| 291 | // 2. If one SORT command is given, and if it is not SORT_NONE, |
| 292 | // --sort-section is handled as an inner SORT command. |
| 293 | // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. |
| 294 | // 4. If no SORT command is given, sort according to --sort-section. |
| George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 295 | // 5. If no SORT commands are given and --sort-section is not specified, |
| 296 | // apply sorting provided by --symbol-ordering-file if any exist. |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 297 | auto Vec = MutableArrayRef<InputSection *>(Ret).slice(SizeBefore); |
| 298 | |
| George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 299 | if (Pat.SortOuter == SortSectionPolicy::Default && |
| 300 | Config->SortSection == SortSectionPolicy::Default) { |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 301 | sortBySymbolOrder(Vec); |
| George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 302 | continue; |
| 303 | } |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 304 | |
| George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 305 | if (Pat.SortOuter != SortSectionPolicy::None) { |
| 306 | if (Pat.SortInner == SortSectionPolicy::Default) |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 307 | sortSections(Vec, Config->SortSection); |
| George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 308 | else |
| Rui Ueyama | 1f4d7b5 | 2017-10-11 03:23:29 +0000 | [diff] [blame^] | 309 | sortSections(Vec, Pat.SortInner); |
| 310 | sortSections(Vec, Pat.SortOuter); |
| George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 311 | } |
| Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 312 | } |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 313 | return Ret; |
| Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 316 | void LinkerScript::discard(ArrayRef<InputSectionBase *> V) { |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 317 | for (InputSectionBase *S : V) { |
| Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 318 | S->Live = false; |
| Dmitry Mikulin | 1e30f07 | 2017-09-08 16:22:43 +0000 | [diff] [blame] | 319 | if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab || |
| 320 | S == InX::DynStrTab) |
| Rafael Espindola | 2af64b0 | 2017-06-16 23:45:35 +0000 | [diff] [blame] | 321 | error("discarding " + S->Name + " section is not allowed"); |
| George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 322 | discard(S->DependentSections); |
| Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 326 | std::vector<InputSectionBase *> |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 327 | LinkerScript::createInputSectionList(OutputSection &OutCmd) { |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 328 | std::vector<InputSectionBase *> Ret; |
| Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 329 | |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 330 | for (BaseCommand *Base : OutCmd.SectionCommands) { |
| Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 331 | auto *Cmd = dyn_cast<InputSectionDescription>(Base); |
| Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 332 | if (!Cmd) |
| Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 333 | continue; |
| Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 334 | |
| 335 | Cmd->Sections = computeInputSections(Cmd); |
| Rafael Espindola | e4c8b9b | 2017-04-07 16:10:46 +0000 | [diff] [blame] | 336 | Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end()); |
| Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 337 | } |
| Rafael Espindola | e71a3f8a | 2016-09-16 20:34:02 +0000 | [diff] [blame] | 338 | |
| Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 339 | return Ret; |
| 340 | } |
| 341 | |
| Rui Ueyama | 5908c2f | 2017-10-11 02:28:28 +0000 | [diff] [blame] | 342 | void LinkerScript::processSectionCommands(OutputSectionFactory &Factory) { |
| Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 343 | // A symbol can be assigned before any section is mentioned in the linker |
| 344 | // script. In an DSO, the symbol values are addresses, so the only important |
| 345 | // section values are: |
| 346 | // * SHN_UNDEF |
| 347 | // * SHN_ABS |
| 348 | // * Any value meaning a regular section. |
| 349 | // To handle that, create a dummy aether section that fills the void before |
| 350 | // the linker scripts switches to another section. It has an index of one |
| 351 | // which will map to whatever the first actual section is. |
| 352 | Aether = make<OutputSection>("", 0, SHF_ALLOC); |
| 353 | Aether->SectionIndex = 1; |
| Rui Ueyama | 18d1968 | 2017-10-11 01:03:37 +0000 | [diff] [blame] | 354 | |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 355 | // Ctx captures the local AddressState and makes it accessible deliberately. |
| 356 | // This is needed as there are some cases where we cannot just |
| Peter Smith | c1ace40 | 2017-07-11 09:28:27 +0000 | [diff] [blame] | 357 | // thread the current state through to a lambda function created by the |
| 358 | // script parser. |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 359 | Ctx = make_unique<AddressState>(); |
| 360 | Ctx->OutSec = Aether; |
| Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 361 | |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 362 | for (size_t I = 0; I < SectionCommands.size(); ++I) { |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 363 | // Handle symbol assignments outside of any output section. |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 364 | if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[I])) { |
| Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 365 | addSymbol(Cmd); |
| Rui Ueyama | 2ab5f73 | 2016-08-12 03:33:04 +0000 | [diff] [blame] | 366 | continue; |
| 367 | } |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 368 | |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 369 | if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) { |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 370 | std::vector<InputSectionBase *> V = createInputSectionList(*Sec); |
| Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 371 | |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 372 | // The output section name `/DISCARD/' is special. |
| 373 | // Any input section assigned to it is discarded. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 374 | if (Sec->Name == "/DISCARD/") { |
| Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 375 | discard(V); |
| Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 376 | continue; |
| 377 | } |
| Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 378 | |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 379 | // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive |
| 380 | // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input |
| 381 | // sections satisfy a given constraint. If not, a directive is handled |
| George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 382 | // as if it wasn't present from the beginning. |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 383 | // |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 384 | // Because we'll iterate over SectionCommands many more times, the easiest |
| George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 385 | // way to "make it as if it wasn't present" is to just remove it. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 386 | if (!matchConstraints(V, Sec->Constraint)) { |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 387 | for (InputSectionBase *S : V) |
| Rui Ueyama | f94efdd | 2016-11-20 23:15:52 +0000 | [diff] [blame] | 388 | S->Assigned = false; |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 389 | SectionCommands.erase(SectionCommands.begin() + I); |
| George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 390 | --I; |
| Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 391 | continue; |
| 392 | } |
| 393 | |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 394 | // A directive may contain symbol definitions like this: |
| 395 | // ".foo : { ...; bar = .; }". Handle them. |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 396 | for (BaseCommand *Base : Sec->SectionCommands) |
| Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 397 | if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base)) |
| Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 398 | addSymbol(OutCmd); |
| Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 399 | |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 400 | // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign |
| 401 | // is given, input sections are aligned to that value, whether the |
| 402 | // given value is larger or smaller than the original section alignment. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 403 | if (Sec->SubalignExpr) { |
| 404 | uint32_t Subalign = Sec->SubalignExpr().getValue(); |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 405 | for (InputSectionBase *S : V) |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 406 | S->Alignment = Subalign; |
| George Rimar | db24d9c | 2016-08-19 15:18:23 +0000 | [diff] [blame] | 407 | } |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 408 | |
| 409 | // Add input sections to an output section. |
| George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 410 | for (InputSectionBase *S : V) |
| Rui Ueyama | 0e2bfb1 | 2017-10-07 00:43:31 +0000 | [diff] [blame] | 411 | Sec->addSection(cast<InputSection>(S)); |
| Rui Ueyama | c54d5b1 | 2017-10-06 23:34:43 +0000 | [diff] [blame] | 412 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 413 | assert(Sec->SectionIndex == INT_MAX); |
| 414 | Sec->SectionIndex = I; |
| 415 | if (Sec->Noload) |
| 416 | Sec->Type = SHT_NOBITS; |
| Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 417 | } |
| Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 418 | } |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 419 | Ctx = nullptr; |
| Eugene Leviant | 20d0319 | 2016-09-16 15:30:47 +0000 | [diff] [blame] | 420 | } |
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 421 | |
| Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 422 | void LinkerScript::fabricateDefaultCommands() { |
| Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 423 | // Define start address |
| Rui Ueyama | 761f0b6 | 2017-09-25 17:40:21 +0000 | [diff] [blame] | 424 | uint64_t StartAddr = UINT64_MAX; |
| Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 425 | |
| Peter Smith | c60b451 | 2017-05-03 08:44:50 +0000 | [diff] [blame] | 426 | // The Sections with -T<section> have been sorted in order of ascending |
| 427 | // address. We must lower StartAddr if the lowest -T<section address> as |
| 428 | // calls to setDot() must be monotonically increasing. |
| George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 429 | for (auto &KV : Config->SectionStartMap) |
| Peter Smith | c60b451 | 2017-05-03 08:44:50 +0000 | [diff] [blame] | 430 | StartAddr = std::min(StartAddr, KV.second); |
| 431 | |
| Rui Ueyama | f5db0b3 | 2017-09-25 17:19:17 +0000 | [diff] [blame] | 432 | auto Expr = [=] { |
| James Henderson | b5ca92e | 2017-10-10 10:09:35 +0000 | [diff] [blame] | 433 | return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize()); |
| Rui Ueyama | f5db0b3 | 2017-09-25 17:19:17 +0000 | [diff] [blame] | 434 | }; |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 435 | SectionCommands.insert(SectionCommands.begin(), |
| 436 | make<SymbolAssignment>(".", Expr, "")); |
| Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| Rui Ueyama | d2f225f | 2017-10-06 23:06:55 +0000 | [diff] [blame] | 439 | static OutputSection *findByName(ArrayRef<BaseCommand *> Vec, |
| 440 | StringRef Name) { |
| 441 | for (BaseCommand *Base : Vec) |
| 442 | if (auto *Sec = dyn_cast<OutputSection>(Base)) |
| 443 | if (Sec->Name == Name) |
| 444 | return Sec; |
| 445 | return nullptr; |
| 446 | } |
| 447 | |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 448 | // Add sections that didn't match any sections command. |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 449 | void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) { |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 450 | unsigned End = SectionCommands.size(); |
| Rui Ueyama | d2f225f | 2017-10-06 23:06:55 +0000 | [diff] [blame] | 451 | |
| Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 452 | for (InputSectionBase *S : InputSections) { |
| Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 453 | if (!S->Live || S->Parent) |
| Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 454 | continue; |
| Rui Ueyama | d2f225f | 2017-10-06 23:06:55 +0000 | [diff] [blame] | 455 | |
| Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 456 | StringRef Name = getOutputSectionName(S->Name); |
| George Rimar | 347c70d | 2017-09-25 09:41:32 +0000 | [diff] [blame] | 457 | log(toString(S) + " is being placed in '" + Name + "'"); |
| Rui Ueyama | d2f225f | 2017-10-06 23:06:55 +0000 | [diff] [blame] | 458 | |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 459 | if (OutputSection *Sec = |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 460 | findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) { |
| Rui Ueyama | 0e2bfb1 | 2017-10-07 00:43:31 +0000 | [diff] [blame] | 461 | Sec->addSection(cast<InputSection>(S)); |
| Rui Ueyama | c54d5b1 | 2017-10-06 23:34:43 +0000 | [diff] [blame] | 462 | continue; |
| Rafael Espindola | de8d989 | 2017-04-29 15:44:03 +0000 | [diff] [blame] | 463 | } |
| Rui Ueyama | c54d5b1 | 2017-10-06 23:34:43 +0000 | [diff] [blame] | 464 | |
| 465 | if (OutputSection *OS = Factory.addInputSec(S, Name)) |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 466 | SectionCommands.push_back(OS); |
| Rui Ueyama | c54d5b1 | 2017-10-06 23:34:43 +0000 | [diff] [blame] | 467 | assert(S->getOutputSection()->SectionIndex == INT_MAX); |
| Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 468 | } |
| Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| Rui Ueyama | 71f8406 | 2017-10-11 02:46:09 +0000 | [diff] [blame] | 471 | uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) { |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 472 | bool IsTbss = |
| 473 | (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS; |
| 474 | uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot; |
| Rui Ueyama | 71f8406 | 2017-10-11 02:46:09 +0000 | [diff] [blame] | 475 | Start = alignTo(Start, Alignment); |
| Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 476 | uint64_t End = Start + Size; |
| 477 | |
| 478 | if (IsTbss) |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 479 | Ctx->ThreadBssOffset = End - Dot; |
| Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 480 | else |
| 481 | Dot = End; |
| 482 | return End; |
| Rafael Espindola | a940e53 | 2016-09-22 12:35:44 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 485 | void LinkerScript::output(InputSection *S) { |
| George Rimar | f694d33 | 2017-07-25 08:29:29 +0000 | [diff] [blame] | 486 | uint64_t Before = advance(0, 1); |
| Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 487 | uint64_t Pos = advance(S->getSize(), S->Alignment); |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 488 | S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 489 | |
| 490 | // Update output section size after adding each section. This is so that |
| 491 | // SIZEOF works correctly in the case below: |
| 492 | // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 493 | Ctx->OutSec->Size = Pos - Ctx->OutSec->Addr; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 494 | |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 495 | // If there is a memory region associated with this input section, then |
| 496 | // place the section in that region and update the region index. |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 497 | if (Ctx->MemRegion) { |
| 498 | uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion]; |
| George Rimar | f694d33 | 2017-07-25 08:29:29 +0000 | [diff] [blame] | 499 | CurOffset += Pos - Before; |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 500 | uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin; |
| 501 | if (CurSize > Ctx->MemRegion->Length) { |
| 502 | uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length; |
| 503 | error("section '" + Ctx->OutSec->Name + "' will not fit in region '" + |
| 504 | Ctx->MemRegion->Name + "': overflowed by " + Twine(OverflowAmt) + |
| 505 | " bytes"); |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 510 | void LinkerScript::switchTo(OutputSection *Sec) { |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 511 | if (Ctx->OutSec == Sec) |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 512 | return; |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 513 | |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 514 | Ctx->OutSec = Sec; |
| 515 | Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment); |
| Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 516 | |
| 517 | // If neither AT nor AT> is specified for an allocatable section, the linker |
| 518 | // will set the LMA such that the difference between VMA and LMA for the |
| 519 | // section is the same as the preceding output section in the same region |
| 520 | // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 521 | if (Ctx->LMAOffset) |
| 522 | Ctx->OutSec->LMAOffset = Ctx->LMAOffset(); |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 525 | void LinkerScript::process(BaseCommand &Base) { |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 526 | // This handles the assignments to symbol or to the dot. |
| 527 | if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) { |
| 528 | assignSymbol(Cmd, true); |
| Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 529 | return; |
| Rui Ueyama | 2de509c | 2016-08-12 00:55:08 +0000 | [diff] [blame] | 530 | } |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 531 | |
| 532 | // Handle BYTE(), SHORT(), LONG(), or QUAD(). |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 533 | if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) { |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 534 | Cmd->Offset = Dot - Ctx->OutSec->Addr; |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 535 | Dot += Cmd->Size; |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 536 | Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr; |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 540 | // Handle ASSERT(). |
| 541 | if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) { |
| 542 | Cmd->Expression(); |
| Meador Inge | b2d99d6 | 2016-11-22 18:01:50 +0000 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 546 | // Handle a single input section description command. |
| 547 | // It calculates and assigns the offsets for each section and also |
| George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 548 | // updates the output section size. |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 549 | auto &Cmd = cast<InputSectionDescription>(Base); |
| Rafael Espindola | a85e8dd | 2017-05-30 20:24:52 +0000 | [diff] [blame] | 550 | for (InputSection *Sec : Cmd.Sections) { |
| George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 551 | // We tentatively added all synthetic sections at the beginning and removed |
| 552 | // empty ones afterwards (because there is no way to know whether they were |
| 553 | // going be empty or not other than actually running linker scripts.) |
| 554 | // We need to ignore remains of empty sections. |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 555 | if (auto *S = dyn_cast<SyntheticSection>(Sec)) |
| 556 | if (S->empty()) |
| George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 557 | continue; |
| 558 | |
| Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 559 | if (!Sec->Live) |
| George Rimar | 78ef645 | 2017-02-21 15:46:43 +0000 | [diff] [blame] | 560 | continue; |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 561 | assert(Ctx->OutSec == Sec->getParent()); |
| Rafael Espindola | a85e8dd | 2017-05-30 20:24:52 +0000 | [diff] [blame] | 562 | output(Sec); |
| Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 566 | // This function searches for a memory region to place the given output |
| 567 | // section in. If found, a pointer to the appropriate memory region is |
| 568 | // returned. Otherwise, a nullptr is returned. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 569 | MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) { |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 570 | // If a memory region name was specified in the output section command, |
| 571 | // then try to find that region first. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 572 | if (!Sec->MemoryRegionName.empty()) { |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 573 | auto It = MemoryRegions.find(Sec->MemoryRegionName); |
| 574 | if (It != MemoryRegions.end()) |
| George Rimar | 5f37541 | 2017-09-08 08:23:15 +0000 | [diff] [blame] | 575 | return It->second; |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 576 | error("memory region '" + Sec->MemoryRegionName + "' not declared"); |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 577 | return nullptr; |
| 578 | } |
| 579 | |
| Rui Ueyama | d7c5400 | 2017-04-05 03:19:43 +0000 | [diff] [blame] | 580 | // If at least one memory region is defined, all sections must |
| 581 | // belong to some memory region. Otherwise, we don't need to do |
| 582 | // anything for memory regions. |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 583 | if (MemoryRegions.empty()) |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 584 | return nullptr; |
| 585 | |
| 586 | // See if a region can be found by matching section flags. |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 587 | for (auto &Pair : MemoryRegions) { |
| George Rimar | 5f37541 | 2017-09-08 08:23:15 +0000 | [diff] [blame] | 588 | MemoryRegion *M = Pair.second; |
| 589 | if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0) |
| 590 | return M; |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Otherwise, no suitable region was found. |
| 594 | if (Sec->Flags & SHF_ALLOC) |
| 595 | error("no memory region specified for section '" + Sec->Name + "'"); |
| 596 | return nullptr; |
| 597 | } |
| 598 | |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 599 | // This function assigns offsets to input sections and an output section |
| 600 | // for a single sections command (e.g. ".text { *(.text); }"). |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 601 | void LinkerScript::assignOffsets(OutputSection *Sec) { |
| Rafael Espindola | dece280 | 2017-06-13 20:57:43 +0000 | [diff] [blame] | 602 | if (!(Sec->Flags & SHF_ALLOC)) |
| 603 | Dot = 0; |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 604 | else if (Sec->AddrExpr) |
| 605 | setDot(Sec->AddrExpr, Sec->Location, false); |
| Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 606 | |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 607 | Ctx->MemRegion = Sec->MemRegion; |
| 608 | if (Ctx->MemRegion) |
| 609 | Dot = Ctx->MemRegionOffset[Ctx->MemRegion]; |
| George Rimar | c2dffe3 | 2017-09-06 09:35:09 +0000 | [diff] [blame] | 610 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 611 | if (Sec->LMAExpr) { |
| George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 612 | uint64_t D = Dot; |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 613 | Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; }; |
| Eugene Leviant | 5784e96 | 2017-03-14 08:57:09 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 616 | switchTo(Sec); |
| Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 617 | |
| George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 618 | // We do not support custom layout for compressed debug sectons. |
| 619 | // At this point we already know their size and have compressed content. |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 620 | if (Ctx->OutSec->Flags & SHF_COMPRESSED) |
| George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 621 | return; |
| 622 | |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 623 | for (BaseCommand *C : Sec->SectionCommands) |
| Rafael Espindola | de8d989 | 2017-04-29 15:44:03 +0000 | [diff] [blame] | 624 | process(*C); |
| Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 627 | void LinkerScript::removeEmptyCommands() { |
| Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 628 | // It is common practice to use very generic linker scripts. So for any |
| 629 | // given run some of the output sections in the script will be empty. |
| 630 | // We could create corresponding empty output sections, but that would |
| 631 | // clutter the output. |
| 632 | // We instead remove trivially empty sections. The bfd linker seems even |
| 633 | // more aggressive at removing them. |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 634 | llvm::erase_if(SectionCommands, [&](BaseCommand *Base) { |
| George Rimar | 60608a8 | 2017-08-28 09:28:15 +0000 | [diff] [blame] | 635 | if (auto *Sec = dyn_cast<OutputSection>(Base)) |
| 636 | return !Sec->Live; |
| 637 | return false; |
| 638 | }); |
| Rafael Espindola | 07fe612 | 2016-11-14 14:23:35 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 641 | static bool isAllSectionDescription(const OutputSection &Cmd) { |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 642 | for (BaseCommand *Base : Cmd.SectionCommands) |
| Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 643 | if (!isa<InputSectionDescription>(*Base)) |
| Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 644 | return false; |
| 645 | return true; |
| 646 | } |
| Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 647 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 648 | void LinkerScript::adjustSectionsBeforeSorting() { |
| Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 649 | // If the output section contains only symbol assignments, create a |
| 650 | // corresponding output section. The bfd linker seems to only create them if |
| 651 | // '.' is assigned to, but creating these section should not have any bad |
| 652 | // consequeces and gives us a section to put the symbol in. |
| George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 653 | uint64_t Flags = SHF_ALLOC; |
| Rafael Espindola | 660c9ab | 2017-05-05 21:34:26 +0000 | [diff] [blame] | 654 | |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 655 | for (BaseCommand *Cmd : SectionCommands) { |
| George Rimar | 8962db9 | 2017-09-18 08:43:44 +0000 | [diff] [blame] | 656 | auto *Sec = dyn_cast<OutputSection>(Cmd); |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 657 | if (!Sec) |
| Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 658 | continue; |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 659 | if (Sec->Live) { |
| Rafael Espindola | 2b07455 | 2017-02-03 22:27:05 +0000 | [diff] [blame] | 660 | Flags = Sec->Flags; |
| Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 661 | continue; |
| 662 | } |
| 663 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 664 | if (isAllSectionDescription(*Sec)) |
| Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 665 | continue; |
| 666 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 667 | Sec->Live = true; |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 668 | Sec->Flags = Flags; |
| Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 669 | } |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 672 | void LinkerScript::adjustSectionsAfterSorting() { |
| Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 673 | // Try and find an appropriate memory region to assign offsets in. |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 674 | for (BaseCommand *Base : SectionCommands) { |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 675 | if (auto *Sec = dyn_cast<OutputSection>(Base)) { |
| George Rimar | ba45584 | 2017-10-02 09:11:13 +0000 | [diff] [blame] | 676 | if (!Sec->Live) |
| 677 | continue; |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 678 | Sec->MemRegion = findMemoryRegion(Sec); |
| Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 679 | // Handle align (e.g. ".foo : ALIGN(16) { ... }"). |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 680 | if (Sec->AlignExpr) |
| Rui Ueyama | 8befefb | 2017-10-07 00:58:34 +0000 | [diff] [blame] | 681 | Sec->Alignment = |
| 682 | std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue()); |
| Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 685 | |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 686 | // If output section command doesn't specify any segments, |
| 687 | // and we haven't previously assigned any section to segment, |
| 688 | // then we simply assign section to the very first load segment. |
| 689 | // Below is an example of such linker script: |
| 690 | // PHDRS { seg PT_LOAD; } |
| 691 | // SECTIONS { .aaa : { *(.aaa) } } |
| 692 | std::vector<StringRef> DefPhdrs; |
| 693 | auto FirstPtLoad = |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 694 | std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(), |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 695 | [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 696 | if (FirstPtLoad != PhdrsCommands.end()) |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 697 | DefPhdrs.push_back(FirstPtLoad->Name); |
| 698 | |
| 699 | // Walk the commands and propagate the program headers to commands that don't |
| 700 | // explicitly specify them. |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 701 | for (BaseCommand *Base : SectionCommands) { |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 702 | auto *Sec = dyn_cast<OutputSection>(Base); |
| 703 | if (!Sec) |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 704 | continue; |
| Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 705 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 706 | if (Sec->Phdrs.empty()) { |
| Andrew Ng | a020d34 | 2017-07-03 10:11:25 +0000 | [diff] [blame] | 707 | // To match the bfd linker script behaviour, only propagate program |
| 708 | // headers to sections that are allocated. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 709 | if (Sec->Flags & SHF_ALLOC) |
| 710 | Sec->Phdrs = DefPhdrs; |
| Andrew Ng | a020d34 | 2017-07-03 10:11:25 +0000 | [diff] [blame] | 711 | } else { |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 712 | DefPhdrs = Sec->Phdrs; |
| Andrew Ng | a020d34 | 2017-07-03 10:11:25 +0000 | [diff] [blame] | 713 | } |
| Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 714 | } |
| Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| George Rimar | 582ede8 | 2017-09-07 10:53:07 +0000 | [diff] [blame] | 717 | static OutputSection *findFirstSection(PhdrEntry *Load) { |
| 718 | for (OutputSection *Sec : OutputSections) |
| 719 | if (Sec->PtLoad == Load) |
| 720 | return Sec; |
| 721 | return nullptr; |
| 722 | } |
| 723 | |
| Petr Hosek | b93c5b9 | 2017-08-23 18:44:34 +0000 | [diff] [blame] | 724 | // Try to find an address for the file and program headers output sections, |
| 725 | // which were unconditionally added to the first PT_LOAD segment earlier. |
| 726 | // |
| 727 | // When using the default layout, we check if the headers fit below the first |
| 728 | // allocated section. When using a linker script, we also check if the headers |
| 729 | // are covered by the output section. This allows omitting the headers by not |
| 730 | // leaving enough space for them in the linker script; this pattern is common |
| 731 | // in embedded systems. |
| 732 | // |
| 733 | // If there isn't enough space for these sections, we'll remove them from the |
| 734 | // PT_LOAD segment, and we'll also remove the PT_PHDR segment. |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 735 | void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) { |
| Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 736 | uint64_t Min = std::numeric_limits<uint64_t>::max(); |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 737 | for (OutputSection *Sec : OutputSections) |
| Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 738 | if (Sec->Flags & SHF_ALLOC) |
| 739 | Min = std::min<uint64_t>(Min, Sec->Addr); |
| Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 740 | |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 741 | auto It = llvm::find_if( |
| 742 | Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; }); |
| 743 | if (It == Phdrs.end()) |
| George Rimar | d971e70 | 2017-07-03 16:05:51 +0000 | [diff] [blame] | 744 | return; |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 745 | PhdrEntry *FirstPTLoad = *It; |
| Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 746 | |
| 747 | uint64_t HeaderSize = getHeaderSize(); |
| Petr Hosek | b93c5b9 | 2017-08-23 18:44:34 +0000 | [diff] [blame] | 748 | // When linker script with SECTIONS is being used, don't output headers |
| 749 | // unless there's a space for them. |
| Rui Ueyama | a323e2a | 2017-10-11 01:34:51 +0000 | [diff] [blame] | 750 | uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0; |
| Petr Hosek | b93c5b9 | 2017-08-23 18:44:34 +0000 | [diff] [blame] | 751 | if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) { |
| Rafael Espindola | 1f0fe88 | 2017-09-28 18:12:13 +0000 | [diff] [blame] | 752 | Min = alignDown(Min - HeaderSize, Config->MaxPageSize); |
| Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 753 | Out::ElfHeader->Addr = Min; |
| 754 | Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size; |
| George Rimar | d971e70 | 2017-07-03 16:05:51 +0000 | [diff] [blame] | 755 | return; |
| Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| George Rimar | 582ede8 | 2017-09-07 10:53:07 +0000 | [diff] [blame] | 758 | Out::ElfHeader->PtLoad = nullptr; |
| 759 | Out::ProgramHeaders->PtLoad = nullptr; |
| George Rimar | 6823c5f | 2017-09-07 11:01:10 +0000 | [diff] [blame] | 760 | FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad); |
| Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 761 | |
| George Rimar | 60608a8 | 2017-08-28 09:28:15 +0000 | [diff] [blame] | 762 | llvm::erase_if(Phdrs, |
| 763 | [](const PhdrEntry *E) { return E->p_type == PT_PHDR; }); |
| Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 766 | LinkerScript::AddressState::AddressState() { |
| 767 | for (auto &MRI : Script->MemoryRegions) { |
| George Rimar | 5f37541 | 2017-09-08 08:23:15 +0000 | [diff] [blame] | 768 | const MemoryRegion *MR = MRI.second; |
| Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 769 | MemRegionOffset[MR] = MR->Origin; |
| 770 | } |
| 771 | } |
| 772 | |
| James Henderson | b5ca92e | 2017-10-10 10:09:35 +0000 | [diff] [blame] | 773 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
| Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 774 | void LinkerScript::assignAddresses() { |
| James Henderson | b5ca92e | 2017-10-10 10:09:35 +0000 | [diff] [blame] | 775 | // By default linker scripts use an initial value of 0 for '.', but prefer |
| 776 | // -image-base if set. |
| 777 | Dot = Config->ImageBase ? *Config->ImageBase : 0; |
| Rui Ueyama | 18d1968 | 2017-10-11 01:03:37 +0000 | [diff] [blame] | 778 | |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 779 | // Ctx captures the local AddressState and makes it accessible |
| Peter Smith | c1ace40 | 2017-07-11 09:28:27 +0000 | [diff] [blame] | 780 | // deliberately. This is needed as there are some cases where we cannot just |
| 781 | // thread the current state through to a lambda function created by the |
| 782 | // script parser. |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 783 | Ctx = make_unique<AddressState>(); |
| Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 784 | ErrorOnMissingSection = true; |
| Rafael Espindola | 06f4743 | 2017-02-06 22:21:46 +0000 | [diff] [blame] | 785 | switchTo(Aether); |
| 786 | |
| Rui Ueyama | 6b394ca | 2017-10-11 01:50:56 +0000 | [diff] [blame] | 787 | for (BaseCommand *Base : SectionCommands) { |
| Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 788 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { |
| Rui Ueyama | d379f73 | 2017-04-05 03:20:22 +0000 | [diff] [blame] | 789 | assignSymbol(Cmd, false); |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 790 | continue; |
| 791 | } |
| 792 | |
| Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 793 | if (auto *Cmd = dyn_cast<AssertCommand>(Base)) { |
| Rafael Espindola | 4595df9 | 2017-03-10 16:04:26 +0000 | [diff] [blame] | 794 | Cmd->Expression(); |
| George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 795 | continue; |
| 796 | } |
| 797 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 798 | assignOffsets(cast<OutputSection>(Base)); |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 799 | } |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 800 | Ctx = nullptr; |
| George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 803 | // Creates program headers as instructed by PHDRS linker script command. |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 804 | std::vector<PhdrEntry *> LinkerScript::createPhdrs() { |
| 805 | std::vector<PhdrEntry *> Ret; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 806 | |
| Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 807 | // Process PHDRS and FILEHDR keywords because they are not |
| 808 | // real output sections and cannot be added in the following loop. |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 809 | for (const PhdrsCommand &Cmd : PhdrsCommands) { |
| Rui Ueyama | 0ae2c24 | 2017-10-08 03:45:49 +0000 | [diff] [blame] | 810 | PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R); |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 811 | |
| 812 | if (Cmd.HasFilehdr) |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 813 | Phdr->add(Out::ElfHeader); |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 814 | if (Cmd.HasPhdrs) |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 815 | Phdr->add(Out::ProgramHeaders); |
| Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 816 | |
| 817 | if (Cmd.LMAExpr) { |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 818 | Phdr->p_paddr = Cmd.LMAExpr().getValue(); |
| 819 | Phdr->HasLMA = true; |
| Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 820 | } |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 821 | Ret.push_back(Phdr); |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 824 | // Add output sections to program headers. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 825 | for (OutputSection *Sec : OutputSections) { |
| Eugene Leviant | ce30b1c | 2016-10-19 15:04:49 +0000 | [diff] [blame] | 826 | // Assign headers specified by linker script |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 827 | for (size_t Id : getPhdrIndices(Sec)) { |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 828 | Ret[Id]->add(Sec); |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 829 | if (!PhdrsCommands[Id].Flags.hasValue()) |
| George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 830 | Ret[Id]->p_flags |= Sec->getPhdrFlags(); |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 831 | } |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 832 | } |
| Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 833 | return Ret; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| Rui Ueyama | e03ba02 | 2017-10-08 03:52:15 +0000 | [diff] [blame] | 836 | // Returns true if we should emit an .interp section. |
| 837 | // |
| 838 | // We usually do. But if PHDRS commands are given, and |
| 839 | // no PT_INTERP is there, there's no place to emit an |
| 840 | // .interp, so we don't do that in that case. |
| 841 | bool LinkerScript::needsInterpSection() { |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 842 | if (PhdrsCommands.empty()) |
| Rui Ueyama | e03ba02 | 2017-10-08 03:52:15 +0000 | [diff] [blame] | 843 | return true; |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 844 | for (PhdrsCommand &Cmd : PhdrsCommands) |
| Rui Ueyama | e31d988 | 2017-04-05 05:06:17 +0000 | [diff] [blame] | 845 | if (Cmd.Type == PT_INTERP) |
| Rui Ueyama | e03ba02 | 2017-10-08 03:52:15 +0000 | [diff] [blame] | 846 | return true; |
| 847 | return false; |
| Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 850 | ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) { |
| George Rimar | 7e5b0a59 | 2017-08-17 08:47:21 +0000 | [diff] [blame] | 851 | if (S == ".") { |
| Rui Ueyama | 29b240c | 2017-10-11 02:45:54 +0000 | [diff] [blame] | 852 | if (Ctx) |
| 853 | return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc}; |
| George Rimar | 7e5b0a59 | 2017-08-17 08:47:21 +0000 | [diff] [blame] | 854 | error(Loc + ": unable to get location counter value"); |
| 855 | return 0; |
| 856 | } |
| Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 857 | if (SymbolBody *B = Symtab->find(S)) { |
| Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 858 | if (auto *D = dyn_cast<DefinedRegular>(B)) |
| Rui Ueyama | 4fbe351 | 2017-10-11 00:06:27 +0000 | [diff] [blame] | 859 | return {D->Section, false, D->Value, Loc}; |
| Petr Hosek | 30f16b2 | 2017-03-23 03:52:34 +0000 | [diff] [blame] | 860 | if (auto *C = dyn_cast<DefinedCommon>(B)) |
| Rui Ueyama | 4fbe351 | 2017-10-11 00:06:27 +0000 | [diff] [blame] | 861 | return {C->Section, false, 0, Loc}; |
| Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 862 | } |
| Eugene Leviant | f6aeed3 | 2016-12-22 13:13:12 +0000 | [diff] [blame] | 863 | error(Loc + ": symbol not found: " + S); |
| George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
| Rui Ueyama | 656be31 | 2017-10-08 02:54:42 +0000 | [diff] [blame] | 867 | // Returns the index of the segment named Name. |
| 868 | static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec, |
| 869 | StringRef Name) { |
| 870 | for (size_t I = 0; I < Vec.size(); ++I) |
| 871 | if (Vec[I].Name == Name) |
| 872 | return I; |
| 873 | return None; |
| 874 | } |
| 875 | |
| Rafael Espindola | 2c923c2 | 2017-05-10 14:28:31 +0000 | [diff] [blame] | 876 | // Returns indices of ELF headers containing specific section. Each index is a |
| 877 | // zero based number of ELF header listed within PHDRS {} script block. |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 878 | std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) { |
| Rafael Espindola | 9f9e99b | 2017-07-05 22:30:04 +0000 | [diff] [blame] | 879 | std::vector<size_t> Ret; |
| Rui Ueyama | 656be31 | 2017-10-08 02:54:42 +0000 | [diff] [blame] | 880 | |
| 881 | for (StringRef S : Cmd->Phdrs) { |
| Rui Ueyama | ac27de9 | 2017-10-11 01:19:33 +0000 | [diff] [blame] | 882 | if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S)) |
| Rui Ueyama | 8722357 | 2017-10-08 02:44:08 +0000 | [diff] [blame] | 883 | Ret.push_back(*Idx); |
| Rui Ueyama | 656be31 | 2017-10-08 02:54:42 +0000 | [diff] [blame] | 884 | else if (S != "NONE") |
| 885 | error(Cmd->Location + ": section header '" + S + |
| 886 | "' is not listed in PHDRS"); |
| 887 | } |
| Rafael Espindola | 9f9e99b | 2017-07-05 22:30:04 +0000 | [diff] [blame] | 888 | return Ret; |
| Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 889 | } |