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" |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 23 | #include "Writer.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Casting.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ELF.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Endian.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <cassert> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | #include <iterator> |
| 37 | #include <limits> |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 38 | #include <string> |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 39 | #include <vector> |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace llvm; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 42 | using namespace llvm::ELF; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 43 | using namespace llvm::object; |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 44 | using namespace llvm::support::endian; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 45 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 46 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 47 | |
Rui Ueyama | a34da93 | 2017-03-21 23:03:09 +0000 | [diff] [blame] | 48 | LinkerScript *elf::Script; |
| 49 | |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 50 | uint64_t ExprValue::getValue() const { |
| 51 | if (Sec) |
| 52 | return Sec->getOffset(Val) + Sec->getOutputSection()->Addr; |
| 53 | return Val; |
| 54 | } |
| 55 | |
Rafael Espindola | 7ba5f47 | 2017-03-17 14:55:36 +0000 | [diff] [blame] | 56 | uint64_t ExprValue::getSecAddr() const { |
| 57 | if (Sec) |
| 58 | return Sec->getOffset(0) + Sec->getOutputSection()->Addr; |
| 59 | return 0; |
| 60 | } |
| 61 | |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 62 | template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) { |
Petr Hosek | 5e51f7d | 2017-02-21 22:32:51 +0000 | [diff] [blame] | 63 | Symbol *Sym; |
Rafael Espindola | 3dabfc6 | 2016-10-31 13:14:53 +0000 | [diff] [blame] | 64 | uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; |
Petr Hosek | 5e51f7d | 2017-02-21 22:32:51 +0000 | [diff] [blame] | 65 | std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert( |
| 66 | Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false, |
| 67 | /*File*/ nullptr); |
| 68 | Sym->Binding = STB_GLOBAL; |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 69 | ExprValue Value = Cmd->Expression(); |
| 70 | SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec; |
George Rimar | 01aa795 | 2017-04-14 09:23:26 +0000 | [diff] [blame] | 71 | |
| 72 | // We want to set symbol values early if we can. This allows us to use symbols |
| 73 | // as variables in linker scripts. Doing so allows us to write expressions |
| 74 | // like this: `alignment = 16; . = ALIGN(., alignment)` |
| 75 | uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0; |
Rui Ueyama | 80474a2 | 2017-02-28 19:29:55 +0000 | [diff] [blame] | 76 | replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility, |
George Rimar | 01aa795 | 2017-04-14 09:23:26 +0000 | [diff] [blame] | 77 | STT_NOTYPE, SymValue, 0, Sec, nullptr); |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 78 | return Sym->body(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 81 | OutputSection *LinkerScript::getOutputSection(const Twine &Loc, |
| 82 | StringRef Name) { |
George Rimar | 851dc1e | 2017-03-14 10:15:53 +0000 | [diff] [blame] | 83 | for (OutputSection *Sec : *OutputSections) |
| 84 | if (Sec->Name == Name) |
| 85 | return Sec; |
| 86 | |
Rui Ueyama | a08fa2e | 2017-04-05 00:42:45 +0000 | [diff] [blame] | 87 | static OutputSection Dummy("", 0, 0); |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 88 | if (ErrorOnMissingSection) |
| 89 | error(Loc + ": undefined section " + Name); |
Rui Ueyama | a08fa2e | 2017-04-05 00:42:45 +0000 | [diff] [blame] | 90 | return &Dummy; |
George Rimar | 851dc1e | 2017-03-14 10:15:53 +0000 | [diff] [blame] | 91 | } |
| 92 | |
George Rimar | d83ce1b | 2017-03-14 10:24:47 +0000 | [diff] [blame] | 93 | // This function is essentially the same as getOutputSection(Name)->Size, |
| 94 | // but it won't print out an error message if a given section is not found. |
| 95 | // |
| 96 | // Linker script does not create an output section if its content is empty. |
| 97 | // We want to allow SIZEOF(.foo) where .foo is a section which happened to |
| 98 | // be empty. That is why this function is different from getOutputSection(). |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 99 | uint64_t LinkerScript::getOutputSectionSize(StringRef Name) { |
George Rimar | d83ce1b | 2017-03-14 10:24:47 +0000 | [diff] [blame] | 100 | for (OutputSection *Sec : *OutputSections) |
| 101 | if (Sec->Name == Name) |
| 102 | return Sec->Size; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 106 | void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) { |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 107 | uint64_t Val = E().getValue(); |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 108 | if (Val < Dot) { |
| 109 | if (InSec) |
George Rimar | 2ee2d2d | 2017-02-21 14:50:38 +0000 | [diff] [blame] | 110 | error(Loc + ": unable to move location counter backward for: " + |
| 111 | CurOutSec->Name); |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 112 | else |
George Rimar | 2ee2d2d | 2017-02-21 14:50:38 +0000 | [diff] [blame] | 113 | error(Loc + ": unable to move location counter backward"); |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 114 | } |
| 115 | Dot = Val; |
| 116 | // Update to location counter means update to section size. |
| 117 | if (InSec) |
| 118 | CurOutSec->Size = Dot - CurOutSec->Addr; |
| 119 | } |
| 120 | |
George Rimar | b2b7097 | 2017-02-07 10:23:28 +0000 | [diff] [blame] | 121 | // Sets value of a symbol. Two kinds of symbols are processed: synthetic |
| 122 | // symbols, whose value is an offset from beginning of section and regular |
| 123 | // symbols whose value is absolute. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 124 | void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) { |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 125 | if (Cmd->Name == ".") { |
George Rimar | 2ee2d2d | 2017-02-21 14:50:38 +0000 | [diff] [blame] | 126 | setDot(Cmd->Expression, Cmd->Location, InSec); |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
George Rimar | b2b7097 | 2017-02-07 10:23:28 +0000 | [diff] [blame] | 130 | if (!Cmd->Sym) |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 131 | return; |
| 132 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 133 | auto *Sym = cast<DefinedRegular>(Cmd->Sym); |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 134 | ExprValue V = Cmd->Expression(); |
| 135 | if (V.isAbsolute()) { |
| 136 | Sym->Value = V.getValue(); |
| 137 | } else { |
| 138 | Sym->Section = V.Sec; |
| 139 | if (Sym->Section->Flags & SHF_ALLOC) |
| 140 | Sym->Value = V.Val; |
| 141 | else |
| 142 | Sym->Value = V.getValue(); |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 143 | } |
Eugene Leviant | db741e7 | 2016-09-07 07:08:43 +0000 | [diff] [blame] | 144 | } |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 145 | |
George Rimar | a8dba48 | 2017-03-20 10:09:58 +0000 | [diff] [blame] | 146 | static SymbolBody *findSymbol(StringRef S) { |
| 147 | switch (Config->EKind) { |
| 148 | case ELF32LEKind: |
| 149 | return Symtab<ELF32LE>::X->find(S); |
| 150 | case ELF32BEKind: |
| 151 | return Symtab<ELF32BE>::X->find(S); |
| 152 | case ELF64LEKind: |
| 153 | return Symtab<ELF64LE>::X->find(S); |
| 154 | case ELF64BEKind: |
| 155 | return Symtab<ELF64BE>::X->find(S); |
| 156 | default: |
| 157 | llvm_unreachable("unknown Config->EKind"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) { |
| 162 | switch (Config->EKind) { |
| 163 | case ELF32LEKind: |
| 164 | return addRegular<ELF32LE>(Cmd); |
| 165 | case ELF32BEKind: |
| 166 | return addRegular<ELF32BE>(Cmd); |
| 167 | case ELF64LEKind: |
| 168 | return addRegular<ELF64LE>(Cmd); |
| 169 | case ELF64BEKind: |
| 170 | return addRegular<ELF64BE>(Cmd); |
| 171 | default: |
| 172 | llvm_unreachable("unknown Config->EKind"); |
| 173 | } |
| 174 | } |
| 175 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 176 | void LinkerScript::addSymbol(SymbolAssignment *Cmd) { |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 177 | if (Cmd->Name == ".") |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 178 | return; |
| 179 | |
| 180 | // If a symbol was in PROVIDE(), we need to define it only when |
| 181 | // it is a referenced undefined symbol. |
George Rimar | a8dba48 | 2017-03-20 10:09:58 +0000 | [diff] [blame] | 182 | SymbolBody *B = findSymbol(Cmd->Name); |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 183 | if (Cmd->Provide && (!B || B->isDefined())) |
| 184 | return; |
| 185 | |
George Rimar | a8dba48 | 2017-03-20 10:09:58 +0000 | [diff] [blame] | 186 | Cmd->Sym = addRegularSymbol(Cmd); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 187 | } |
| 188 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 189 | bool SymbolAssignment::classof(const BaseCommand *C) { |
| 190 | return C->Kind == AssignmentKind; |
| 191 | } |
| 192 | |
| 193 | bool OutputSectionCommand::classof(const BaseCommand *C) { |
| 194 | return C->Kind == OutputSectionKind; |
| 195 | } |
| 196 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 197 | bool InputSectionDescription::classof(const BaseCommand *C) { |
| 198 | return C->Kind == InputSectionKind; |
| 199 | } |
| 200 | |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 201 | bool AssertCommand::classof(const BaseCommand *C) { |
| 202 | return C->Kind == AssertKind; |
| 203 | } |
| 204 | |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 205 | bool BytesDataCommand::classof(const BaseCommand *C) { |
| 206 | return C->Kind == BytesDataKind; |
| 207 | } |
| 208 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 209 | static StringRef basename(InputSectionBase *S) { |
| 210 | if (S->File) |
| 211 | return sys::path::filename(S->File->getName()); |
Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 212 | return ""; |
| 213 | } |
| 214 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 215 | bool LinkerScript::shouldKeep(InputSectionBase *S) { |
Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 216 | for (InputSectionDescription *ID : Opt.KeptSections) |
| 217 | if (ID->FilePat.match(basename(S))) |
| 218 | for (SectionPattern &P : ID->SectionPatterns) |
| 219 | if (P.SectionPat.match(S->Name)) |
| 220 | return true; |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 224 | // A helper function for the SORT() command. |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 225 | static std::function<bool(InputSectionBase *, InputSectionBase *)> |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 226 | getComparator(SortSectionPolicy K) { |
| 227 | switch (K) { |
| 228 | case SortSectionPolicy::Alignment: |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 229 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 230 | // ">" is not a mistake. Sections with larger alignments are placed |
| 231 | // before sections with smaller alignments in order to reduce the |
| 232 | // amount of padding necessary. This is compatible with GNU. |
| 233 | return A->Alignment > B->Alignment; |
| 234 | }; |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 235 | case SortSectionPolicy::Name: |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 236 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 237 | return A->Name < B->Name; |
| 238 | }; |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 239 | case SortSectionPolicy::Priority: |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 240 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 241 | return getPriority(A->Name) < getPriority(B->Name); |
| 242 | }; |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 243 | default: |
| 244 | llvm_unreachable("unknown sort policy"); |
| 245 | } |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 246 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 247 | |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 248 | // A helper function for the SORT() command. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 249 | static bool matchConstraints(ArrayRef<InputSectionBase *> Sections, |
George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 250 | ConstraintKind Kind) { |
George Rimar | 8f66df9 | 2016-08-12 20:38:20 +0000 | [diff] [blame] | 251 | if (Kind == ConstraintKind::NoConstraint) |
| 252 | return true; |
Rui Ueyama | 2c7171b | 2017-04-05 00:43:45 +0000 | [diff] [blame] | 253 | |
| 254 | bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) { |
| 255 | return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE; |
George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 256 | }); |
Rui Ueyama | 2c7171b | 2017-04-05 00:43:45 +0000 | [diff] [blame] | 257 | |
Rafael Espindola | e746e52 | 2016-09-21 18:33:44 +0000 | [diff] [blame] | 258 | return (IsRW && Kind == ConstraintKind::ReadWrite) || |
| 259 | (!IsRW && Kind == ConstraintKind::ReadOnly); |
George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 262 | static void sortSections(InputSectionBase **Begin, InputSectionBase **End, |
Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 263 | SortSectionPolicy K) { |
| 264 | if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) |
George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 265 | std::stable_sort(Begin, End, getComparator(K)); |
Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 268 | // Compute and remember which sections the InputSectionDescription matches. |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 269 | std::vector<InputSectionBase *> |
| 270 | LinkerScript::computeInputSections(const InputSectionDescription *Cmd) { |
| 271 | std::vector<InputSectionBase *> Ret; |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 272 | |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 273 | // Collects all sections that satisfy constraints of Cmd. |
| 274 | for (const SectionPattern &Pat : Cmd->SectionPatterns) { |
| 275 | size_t SizeBefore = Ret.size(); |
| 276 | |
| 277 | for (InputSectionBase *Sec : InputSections) { |
| 278 | if (Sec->Assigned) |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 279 | continue; |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 280 | |
Rafael Espindola | 908a3d3 | 2017-02-16 14:36:09 +0000 | [diff] [blame] | 281 | // For -emit-relocs we have to ignore entries like |
| 282 | // .rela.dyn : { *(.rela.data) } |
| 283 | // which are common because they are in the default bfd script. |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 284 | if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA) |
Rafael Espindola | 908a3d3 | 2017-02-16 14:36:09 +0000 | [diff] [blame] | 285 | continue; |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 286 | |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 287 | StringRef Filename = basename(Sec); |
| 288 | if (!Cmd->FilePat.match(Filename) || |
| 289 | Pat.ExcludedFilePat.match(Filename) || |
| 290 | !Pat.SectionPat.match(Sec->Name)) |
Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 291 | continue; |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 292 | |
| 293 | Ret.push_back(Sec); |
| 294 | Sec->Assigned = true; |
George Rimar | 395281c | 2016-09-16 17:42:10 +0000 | [diff] [blame] | 295 | } |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 296 | |
George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 297 | // Sort sections as instructed by SORT-family commands and --sort-section |
| 298 | // option. Because SORT-family commands can be nested at most two depth |
| 299 | // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command |
| 300 | // line option is respected even if a SORT command is given, the exact |
| 301 | // behavior we have here is a bit complicated. Here are the rules. |
| 302 | // |
| 303 | // 1. If two SORT commands are given, --sort-section is ignored. |
| 304 | // 2. If one SORT command is given, and if it is not SORT_NONE, |
| 305 | // --sort-section is handled as an inner SORT command. |
| 306 | // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. |
| 307 | // 4. If no SORT command is given, sort according to --sort-section. |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 308 | InputSectionBase **Begin = Ret.data() + SizeBefore; |
| 309 | InputSectionBase **End = Ret.data() + Ret.size(); |
George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 310 | if (Pat.SortOuter != SortSectionPolicy::None) { |
| 311 | if (Pat.SortInner == SortSectionPolicy::Default) |
| 312 | sortSections(Begin, End, Config->SortSection); |
| 313 | else |
| 314 | sortSections(Begin, End, Pat.SortInner); |
| 315 | sortSections(Begin, End, Pat.SortOuter); |
| 316 | } |
Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 317 | } |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 318 | return Ret; |
Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 321 | void LinkerScript::discard(ArrayRef<InputSectionBase *> V) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 322 | for (InputSectionBase *S : V) { |
Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 323 | S->Live = false; |
George Rimar | 503206c | 2017-03-15 15:42:44 +0000 | [diff] [blame] | 324 | if (S == InX::ShStrTab) |
Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 325 | error("discarding .shstrtab section is not allowed"); |
George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 326 | discard(S->DependentSections); |
Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 330 | std::vector<InputSectionBase *> |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 331 | LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 332 | std::vector<InputSectionBase *> Ret; |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 333 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 334 | for (BaseCommand *Base : OutCmd.Commands) { |
| 335 | auto *Cmd = dyn_cast<InputSectionDescription>(Base); |
Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 336 | if (!Cmd) |
Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 337 | continue; |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 338 | |
| 339 | Cmd->Sections = computeInputSections(Cmd); |
Rafael Espindola | e4c8b9b | 2017-04-07 16:10:46 +0000 | [diff] [blame] | 340 | Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end()); |
Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 341 | } |
Rafael Espindola | e71a3f8a | 2016-09-16 20:34:02 +0000 | [diff] [blame] | 342 | |
Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 343 | return Ret; |
| 344 | } |
| 345 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 346 | void LinkerScript::processCommands(OutputSectionFactory &Factory) { |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 347 | // A symbol can be assigned before any section is mentioned in the linker |
| 348 | // script. In an DSO, the symbol values are addresses, so the only important |
| 349 | // section values are: |
| 350 | // * SHN_UNDEF |
| 351 | // * SHN_ABS |
| 352 | // * Any value meaning a regular section. |
| 353 | // To handle that, create a dummy aether section that fills the void before |
| 354 | // the linker scripts switches to another section. It has an index of one |
| 355 | // which will map to whatever the first actual section is. |
| 356 | Aether = make<OutputSection>("", 0, SHF_ALLOC); |
| 357 | Aether->SectionIndex = 1; |
| 358 | CurOutSec = Aether; |
Rafael Espindola | 49592cf | 2017-03-20 14:33:33 +0000 | [diff] [blame] | 359 | Dot = 0; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 360 | |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 361 | for (size_t I = 0; I < Opt.Commands.size(); ++I) { |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 362 | // Handle symbol assignments outside of any output section. |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 363 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) { |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 364 | addSymbol(Cmd); |
Rui Ueyama | 2ab5f73 | 2016-08-12 03:33:04 +0000 | [diff] [blame] | 365 | continue; |
| 366 | } |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 367 | |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 368 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 369 | std::vector<InputSectionBase *> V = createInputSectionList(*Cmd); |
Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 370 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 371 | // The output section name `/DISCARD/' is special. |
| 372 | // Any input section assigned to it is discarded. |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 373 | if (Cmd->Name == "/DISCARD/") { |
Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 374 | discard(V); |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 375 | continue; |
| 376 | } |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 377 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 378 | // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive |
| 379 | // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input |
| 380 | // sections satisfy a given constraint. If not, a directive is handled |
George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 381 | // as if it wasn't present from the beginning. |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 382 | // |
| 383 | // Because we'll iterate over Commands many more times, the easiest |
George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 384 | // way to "make it as if it wasn't present" is to just remove it. |
George Rimar | f7f0d08 | 2017-03-14 11:23:33 +0000 | [diff] [blame] | 385 | if (!matchConstraints(V, Cmd->Constraint)) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 386 | for (InputSectionBase *S : V) |
Rui Ueyama | f94efdd | 2016-11-20 23:15:52 +0000 | [diff] [blame] | 387 | S->Assigned = false; |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 388 | Opt.Commands.erase(Opt.Commands.begin() + I); |
George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 389 | --I; |
Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 390 | continue; |
| 391 | } |
| 392 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 393 | // A directive may contain symbol definitions like this: |
| 394 | // ".foo : { ...; bar = .; }". Handle them. |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 395 | for (BaseCommand *Base : Cmd->Commands) |
| 396 | if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base)) |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 397 | addSymbol(OutCmd); |
Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 398 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 399 | // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign |
| 400 | // is given, input sections are aligned to that value, whether the |
| 401 | // given value is larger or smaller than the original section alignment. |
| 402 | if (Cmd->SubalignExpr) { |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 403 | uint32_t Subalign = Cmd->SubalignExpr().getValue(); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 404 | for (InputSectionBase *S : V) |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 405 | S->Alignment = Subalign; |
George Rimar | db24d9c | 2016-08-19 15:18:23 +0000 | [diff] [blame] | 406 | } |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 407 | |
| 408 | // Add input sections to an output section. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 409 | for (InputSectionBase *S : V) |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 410 | Factory.addInputSec(S, Cmd->Name, Cmd->Sec); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 411 | } |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 412 | } |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 413 | CurOutSec = nullptr; |
Eugene Leviant | 20d0319 | 2016-09-16 15:30:47 +0000 | [diff] [blame] | 414 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 415 | |
Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 416 | void LinkerScript::fabricateDefaultCommands(bool AllocateHeader) { |
| 417 | std::vector<BaseCommand *> Commands; |
| 418 | |
| 419 | // Define start address |
| 420 | uint64_t StartAddr = Config->ImageBase; |
| 421 | if (AllocateHeader) |
| 422 | StartAddr += elf::getHeaderSize(); |
| 423 | |
| 424 | // The Sections with -T<section> are sorted in order of ascending address |
| 425 | // we must use this if it is lower than StartAddr as calls to setDot() must |
| 426 | // be monotonically increasing |
| 427 | if (!Config->SectionStartMap.empty()) { |
| 428 | uint64_t LowestSecStart = Config->SectionStartMap.begin()->second; |
| 429 | StartAddr = std::min(StartAddr, LowestSecStart); |
| 430 | } |
| 431 | Commands.push_back( |
| 432 | make<SymbolAssignment>(".", [=] { return StartAddr; }, "")); |
| 433 | |
| 434 | // For each OutputSection that needs a VA fabricate an OutputSectionCommand |
| 435 | // with an InputSectionDescription describing the InputSections |
| 436 | for (OutputSection *Sec : *OutputSections) { |
| 437 | if (!(Sec->Flags & SHF_ALLOC)) |
| 438 | continue; |
| 439 | |
| 440 | auto I = Config->SectionStartMap.find(Sec->Name); |
| 441 | if (I != Config->SectionStartMap.end()) |
| 442 | Commands.push_back( |
| 443 | make<SymbolAssignment>(".", [=] { return I->second; }, "")); |
| 444 | |
| 445 | auto *OSCmd = make<OutputSectionCommand>(Sec->Name); |
| 446 | OSCmd->Sec = Sec; |
| 447 | if (Sec->PageAlign) |
| 448 | OSCmd->AddrExpr = [=] { |
| 449 | return alignTo(Script->getDot(), Config->MaxPageSize); |
| 450 | }; |
| 451 | Commands.push_back(OSCmd); |
| 452 | if (Sec->Sections.size()) { |
| 453 | auto *ISD = make<InputSectionDescription>(""); |
| 454 | OSCmd->Commands.push_back(ISD); |
| 455 | for (InputSection *ISec : Sec->Sections) { |
| 456 | ISD->Sections.push_back(ISec); |
| 457 | ISec->Assigned = true; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | // SECTIONS commands run before other non SECTIONS commands |
| 462 | Commands.insert(Commands.end(), Opt.Commands.begin(), Opt.Commands.end()); |
| 463 | Opt.Commands = std::move(Commands); |
| 464 | } |
| 465 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 466 | // Add sections that didn't match any sections command. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 467 | void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) { |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 468 | for (InputSectionBase *S : InputSections) { |
| 469 | if (!S->Live || S->OutSec) |
| 470 | continue; |
| 471 | StringRef Name = getOutputSectionName(S->Name); |
| 472 | auto I = std::find_if( |
| 473 | Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) { |
| 474 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) |
| 475 | return Cmd->Name == Name; |
| 476 | return false; |
| 477 | }); |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 478 | if (I == Opt.Commands.end()) |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 479 | Factory.addInputSec(S, Name); |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 480 | else |
| 481 | Factory.addInputSec(S, Name, cast<OutputSectionCommand>(*I)->Sec); |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 482 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 483 | } |
| 484 | |
George Rimar | f7f0d08 | 2017-03-14 11:23:33 +0000 | [diff] [blame] | 485 | static bool isTbss(OutputSection *Sec) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 486 | return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS; |
Rafael Espindola | a940e53 | 2016-09-22 12:35:44 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 489 | void LinkerScript::output(InputSection *S) { |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 490 | if (!AlreadyOutputIS.insert(S).second) |
| 491 | return; |
George Rimar | f7f0d08 | 2017-03-14 11:23:33 +0000 | [diff] [blame] | 492 | bool IsTbss = isTbss(CurOutSec); |
Eugene Leviant | 20889c5 | 2016-08-31 08:13:33 +0000 | [diff] [blame] | 493 | |
George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 494 | uint64_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot; |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 495 | Pos = alignTo(Pos, S->Alignment); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 496 | S->OutSecOff = Pos - CurOutSec->Addr; |
Rafael Espindola | 76b6bd3 | 2017-03-08 15:44:30 +0000 | [diff] [blame] | 497 | Pos += S->getSize(); |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 498 | |
| 499 | // Update output section size after adding each section. This is so that |
| 500 | // SIZEOF works correctly in the case below: |
| 501 | // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 502 | CurOutSec->Size = Pos - CurOutSec->Addr; |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 503 | |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 504 | // If there is a memory region associated with this input section, then |
| 505 | // place the section in that region and update the region index. |
| 506 | if (CurMemRegion) { |
| 507 | CurMemRegion->Offset += CurOutSec->Size; |
| 508 | uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin; |
| 509 | if (CurSize > CurMemRegion->Length) { |
| 510 | uint64_t OverflowAmt = CurSize - CurMemRegion->Length; |
| 511 | error("section '" + CurOutSec->Name + "' will not fit in region '" + |
| 512 | CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) + |
| 513 | " bytes"); |
| 514 | } |
| 515 | } |
| 516 | |
Rafael Espindola | 7252ae5 | 2016-09-22 12:00:08 +0000 | [diff] [blame] | 517 | if (IsTbss) |
| 518 | ThreadBssOffset = Pos - Dot; |
| 519 | else |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 520 | Dot = Pos; |
| 521 | } |
| 522 | |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 523 | void LinkerScript::flush() { |
| 524 | assert(CurOutSec); |
| 525 | if (!AlreadyOutputOS.insert(CurOutSec).second) |
| 526 | return; |
| 527 | for (InputSection *I : CurOutSec->Sections) |
| 528 | output(I); |
| 529 | } |
| 530 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 531 | void LinkerScript::switchTo(OutputSection *Sec) { |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 532 | if (CurOutSec == Sec) |
| 533 | return; |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 534 | if (AlreadyOutputOS.count(Sec)) |
| 535 | return; |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 536 | |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 537 | CurOutSec = Sec; |
| 538 | |
Rafael Espindola | 3770763 | 2017-03-07 14:55:52 +0000 | [diff] [blame] | 539 | Dot = alignTo(Dot, CurOutSec->Alignment); |
George Rimar | f7f0d08 | 2017-03-14 11:23:33 +0000 | [diff] [blame] | 540 | CurOutSec->Addr = isTbss(CurOutSec) ? Dot + ThreadBssOffset : Dot; |
Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 541 | |
| 542 | // If neither AT nor AT> is specified for an allocatable section, the linker |
| 543 | // will set the LMA such that the difference between VMA and LMA for the |
| 544 | // section is the same as the preceding output section in the same region |
| 545 | // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html |
George Rimar | 2146787 | 2017-02-23 07:57:55 +0000 | [diff] [blame] | 546 | if (LMAOffset) |
Rafael Espindola | 29c1afb | 2017-02-24 14:34:12 +0000 | [diff] [blame] | 547 | CurOutSec->LMAOffset = LMAOffset(); |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 550 | void LinkerScript::process(BaseCommand &Base) { |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 551 | // This handles the assignments to symbol or to the dot. |
| 552 | if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) { |
| 553 | assignSymbol(Cmd, true); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 554 | return; |
Rui Ueyama | 2de509c | 2016-08-12 00:55:08 +0000 | [diff] [blame] | 555 | } |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 556 | |
| 557 | // Handle BYTE(), SHORT(), LONG(), or QUAD(). |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 558 | if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) { |
| 559 | Cmd->Offset = Dot - CurOutSec->Addr; |
| 560 | Dot += Cmd->Size; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 561 | CurOutSec->Size = Dot - CurOutSec->Addr; |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 565 | // Handle ASSERT(). |
| 566 | if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) { |
| 567 | Cmd->Expression(); |
Meador Inge | b2d99d6 | 2016-11-22 18:01:50 +0000 | [diff] [blame] | 568 | return; |
| 569 | } |
| 570 | |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 571 | // Handle a single input section description command. |
| 572 | // It calculates and assigns the offsets for each section and also |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 573 | // updates the output section size. |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 574 | auto &Cmd = cast<InputSectionDescription>(Base); |
| 575 | for (InputSectionBase *Sec : Cmd.Sections) { |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 576 | // We tentatively added all synthetic sections at the beginning and removed |
| 577 | // empty ones afterwards (because there is no way to know whether they were |
| 578 | // going be empty or not other than actually running linker scripts.) |
| 579 | // We need to ignore remains of empty sections. |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 580 | if (auto *S = dyn_cast<SyntheticSection>(Sec)) |
| 581 | if (S->empty()) |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 582 | continue; |
| 583 | |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 584 | if (!Sec->Live) |
George Rimar | 78ef645 | 2017-02-21 15:46:43 +0000 | [diff] [blame] | 585 | continue; |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 586 | assert(CurOutSec == Sec->OutSec || AlreadyOutputOS.count(Sec->OutSec)); |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 587 | output(cast<InputSection>(Sec)); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 591 | // This function searches for a memory region to place the given output |
| 592 | // section in. If found, a pointer to the appropriate memory region is |
| 593 | // returned. Otherwise, a nullptr is returned. |
Rafael Espindola | 1902b33 | 2017-04-06 21:26:03 +0000 | [diff] [blame] | 594 | MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) { |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 595 | // If a memory region name was specified in the output section command, |
| 596 | // then try to find that region first. |
| 597 | if (!Cmd->MemoryRegionName.empty()) { |
| 598 | auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName); |
| 599 | if (It != Opt.MemoryRegions.end()) |
| 600 | return &It->second; |
| 601 | error("memory region '" + Cmd->MemoryRegionName + "' not declared"); |
| 602 | return nullptr; |
| 603 | } |
| 604 | |
Rui Ueyama | d7c5400 | 2017-04-05 03:19:43 +0000 | [diff] [blame] | 605 | // If at least one memory region is defined, all sections must |
| 606 | // belong to some memory region. Otherwise, we don't need to do |
| 607 | // anything for memory regions. |
Rui Ueyama | cc400cc | 2017-04-05 03:19:24 +0000 | [diff] [blame] | 608 | if (Opt.MemoryRegions.empty()) |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 609 | return nullptr; |
| 610 | |
Rafael Espindola | 1902b33 | 2017-04-06 21:26:03 +0000 | [diff] [blame] | 611 | OutputSection *Sec = Cmd->Sec; |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 612 | // See if a region can be found by matching section flags. |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 613 | for (auto &Pair : Opt.MemoryRegions) { |
| 614 | MemoryRegion &M = Pair.second; |
| 615 | if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0) |
| 616 | return &M; |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // Otherwise, no suitable region was found. |
| 620 | if (Sec->Flags & SHF_ALLOC) |
| 621 | error("no memory region specified for section '" + Sec->Name + "'"); |
| 622 | return nullptr; |
| 623 | } |
| 624 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 625 | // This function assigns offsets to input sections and an output section |
| 626 | // for a single sections command (e.g. ".text { *(.text); }"). |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 627 | void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) { |
Rafael Espindola | 9b98009 | 2017-04-06 21:05:39 +0000 | [diff] [blame] | 628 | OutputSection *Sec = Cmd->Sec; |
Rafael Espindola | 2b07455 | 2017-02-03 22:27:05 +0000 | [diff] [blame] | 629 | if (!Sec) |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 630 | return; |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 631 | |
Rui Ueyama | cba4101 | 2017-04-05 03:20:03 +0000 | [diff] [blame] | 632 | if (Cmd->AddrExpr && (Sec->Flags & SHF_ALLOC)) |
Rui Ueyama | d379f73 | 2017-04-05 03:20:22 +0000 | [diff] [blame] | 633 | setDot(Cmd->AddrExpr, Cmd->Location, false); |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 634 | |
Eugene Leviant | 5784e96 | 2017-03-14 08:57:09 +0000 | [diff] [blame] | 635 | if (Cmd->LMAExpr) { |
George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 636 | uint64_t D = Dot; |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 637 | LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; }; |
Eugene Leviant | 5784e96 | 2017-03-14 08:57:09 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 640 | CurMemRegion = Cmd->MemRegion; |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 641 | if (CurMemRegion) |
| 642 | Dot = CurMemRegion->Offset; |
| 643 | switchTo(Sec); |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 644 | |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 645 | // flush() may add orphan sections, so the order of flush() and |
| 646 | // symbol assignments is important. We want to call flush() first so |
| 647 | // that symbols pointing the end of the current section points to |
| 648 | // the location after orphan sections. |
| 649 | auto Mid = |
| 650 | std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(), |
| 651 | [](BaseCommand *Cmd) { return !isa<SymbolAssignment>(Cmd); }) |
| 652 | .base(); |
| 653 | for (auto I = Cmd->Commands.begin(); I != Mid; ++I) |
| 654 | process(**I); |
| 655 | flush(); |
| 656 | for (auto I = Mid, E = Cmd->Commands.end(); I != E; ++I) |
| 657 | process(**I); |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 660 | void LinkerScript::removeEmptyCommands() { |
Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 661 | // It is common practice to use very generic linker scripts. So for any |
| 662 | // given run some of the output sections in the script will be empty. |
| 663 | // We could create corresponding empty output sections, but that would |
| 664 | // clutter the output. |
| 665 | // We instead remove trivially empty sections. The bfd linker seems even |
| 666 | // more aggressive at removing them. |
| 667 | auto Pos = std::remove_if( |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 668 | Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) { |
| 669 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 670 | return std::find(OutputSections->begin(), OutputSections->end(), |
| 671 | Cmd->Sec) == OutputSections->end(); |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 672 | return false; |
Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 673 | }); |
| 674 | Opt.Commands.erase(Pos, Opt.Commands.end()); |
Rafael Espindola | 07fe612 | 2016-11-14 14:23:35 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 677 | static bool isAllSectionDescription(const OutputSectionCommand &Cmd) { |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 678 | for (BaseCommand *Base : Cmd.Commands) |
| 679 | if (!isa<InputSectionDescription>(*Base)) |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 680 | return false; |
| 681 | return true; |
| 682 | } |
Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 683 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 684 | void LinkerScript::adjustSectionsBeforeSorting() { |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 685 | // If the output section contains only symbol assignments, create a |
| 686 | // corresponding output section. The bfd linker seems to only create them if |
| 687 | // '.' is assigned to, but creating these section should not have any bad |
| 688 | // consequeces and gives us a section to put the symbol in. |
George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 689 | uint64_t Flags = SHF_ALLOC; |
George Rimar | 1022112 | 2017-04-14 09:37:00 +0000 | [diff] [blame] | 690 | uint32_t Type = SHT_PROGBITS; |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 691 | for (BaseCommand *Base : Opt.Commands) { |
| 692 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base); |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 693 | if (!Cmd) |
| 694 | continue; |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 695 | if (OutputSection *Sec = Cmd->Sec) { |
Rafael Espindola | 2b07455 | 2017-02-03 22:27:05 +0000 | [diff] [blame] | 696 | Flags = Sec->Flags; |
| 697 | Type = Sec->Type; |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 698 | continue; |
| 699 | } |
| 700 | |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 701 | if (isAllSectionDescription(*Cmd)) |
| 702 | continue; |
| 703 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 704 | auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags); |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 705 | OutputSections->push_back(OutSec); |
Rafael Espindola | 9b98009 | 2017-04-06 21:05:39 +0000 | [diff] [blame] | 706 | Cmd->Sec = OutSec; |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 707 | } |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 710 | void LinkerScript::adjustSectionsAfterSorting() { |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 711 | placeOrphanSections(); |
| 712 | |
Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 713 | // Try and find an appropriate memory region to assign offsets in. |
Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 714 | for (BaseCommand *Base : Opt.Commands) { |
| 715 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) { |
Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 716 | Cmd->MemRegion = findMemoryRegion(Cmd); |
Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 717 | // Handle align (e.g. ".foo : ALIGN(16) { ... }"). |
| 718 | if (Cmd->AlignExpr) |
| 719 | Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue()); |
| 720 | } |
| 721 | } |
Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 722 | |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 723 | // If output section command doesn't specify any segments, |
| 724 | // and we haven't previously assigned any section to segment, |
| 725 | // then we simply assign section to the very first load segment. |
| 726 | // Below is an example of such linker script: |
| 727 | // PHDRS { seg PT_LOAD; } |
| 728 | // SECTIONS { .aaa : { *(.aaa) } } |
| 729 | std::vector<StringRef> DefPhdrs; |
| 730 | auto FirstPtLoad = |
| 731 | std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(), |
| 732 | [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); |
| 733 | if (FirstPtLoad != Opt.PhdrsCommands.end()) |
| 734 | DefPhdrs.push_back(FirstPtLoad->Name); |
| 735 | |
| 736 | // Walk the commands and propagate the program headers to commands that don't |
| 737 | // explicitly specify them. |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 738 | for (BaseCommand *Base : Opt.Commands) { |
| 739 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base); |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 740 | if (!Cmd) |
| 741 | continue; |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 742 | |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 743 | if (Cmd->Phdrs.empty()) |
| 744 | Cmd->Phdrs = DefPhdrs; |
| 745 | else |
| 746 | DefPhdrs = Cmd->Phdrs; |
| 747 | } |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 748 | |
| 749 | removeEmptyCommands(); |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Rafael Espindola | 15c5795 | 2016-09-22 18:05:49 +0000 | [diff] [blame] | 752 | // When placing orphan sections, we want to place them after symbol assignments |
| 753 | // so that an orphan after |
| 754 | // begin_foo = .; |
| 755 | // foo : { *(foo) } |
| 756 | // end_foo = .; |
| 757 | // doesn't break the intended meaning of the begin/end symbols. |
| 758 | // We don't want to go over sections since Writer<ELFT>::sortSections is the |
| 759 | // one in charge of deciding the order of the sections. |
| 760 | // We don't want to go over alignments, since doing so in |
| 761 | // rx_sec : { *(rx_sec) } |
| 762 | // . = ALIGN(0x1000); |
| 763 | // /* The RW PT_LOAD starts here*/ |
| 764 | // rw_sec : { *(rw_sec) } |
| 765 | // would mean that the RW PT_LOAD would become unaligned. |
Rui Ueyama | 4e1e88e | 2017-04-05 03:52:28 +0000 | [diff] [blame] | 766 | static bool shouldSkip(BaseCommand *Cmd) { |
Rafael Espindola | 15c5795 | 2016-09-22 18:05:49 +0000 | [diff] [blame] | 767 | if (isa<OutputSectionCommand>(Cmd)) |
| 768 | return false; |
Rui Ueyama | 4e1e88e | 2017-04-05 03:52:28 +0000 | [diff] [blame] | 769 | if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd)) |
| 770 | return Assign->Name != "."; |
| 771 | return true; |
Rafael Espindola | 15c5795 | 2016-09-22 18:05:49 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Rui Ueyama | 6697ec2 | 2017-02-02 23:26:12 +0000 | [diff] [blame] | 774 | // Orphan sections are sections present in the input files which are |
| 775 | // not explicitly placed into the output file by the linker script. |
| 776 | // |
| 777 | // When the control reaches this function, Opt.Commands contains |
| 778 | // output section commands for non-orphan sections only. This function |
Rui Ueyama | 81cb710 | 2017-03-24 00:15:57 +0000 | [diff] [blame] | 779 | // adds new elements for orphan sections so that all sections are |
| 780 | // explicitly handled by Opt.Commands. |
Rui Ueyama | 6697ec2 | 2017-02-02 23:26:12 +0000 | [diff] [blame] | 781 | // |
| 782 | // Writer<ELFT>::sortSections has already sorted output sections. |
| 783 | // What we need to do is to scan OutputSections vector and |
| 784 | // Opt.Commands in parallel to find orphan sections. If there is an |
| 785 | // output section that doesn't have a corresponding entry in |
| 786 | // Opt.Commands, we will insert a new entry to Opt.Commands. |
| 787 | // |
| 788 | // There is some ambiguity as to where exactly a new entry should be |
| 789 | // inserted, because Opt.Commands contains not only output section |
Rui Ueyama | 81cb710 | 2017-03-24 00:15:57 +0000 | [diff] [blame] | 790 | // commands but also other types of commands such as symbol assignment |
Rui Ueyama | 6697ec2 | 2017-02-02 23:26:12 +0000 | [diff] [blame] | 791 | // expressions. There's no correct answer here due to the lack of the |
| 792 | // formal specification of the linker script. We use heuristics to |
| 793 | // determine whether a new output command should be added before or |
| 794 | // after another commands. For the details, look at shouldSkip |
| 795 | // function. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 796 | void LinkerScript::placeOrphanSections() { |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 797 | // The OutputSections are already in the correct order. |
| 798 | // This loops creates or moves commands as needed so that they are in the |
| 799 | // correct order. |
| 800 | int CmdIndex = 0; |
Rafael Espindola | 5fcc99c | 2016-11-27 09:44:45 +0000 | [diff] [blame] | 801 | |
| 802 | // As a horrible special case, skip the first . assignment if it is before any |
| 803 | // section. We do this because it is common to set a load address by starting |
| 804 | // the script with ". = 0xabcd" and the expectation is that every section is |
| 805 | // after that. |
Rui Ueyama | 4e1e88e | 2017-04-05 03:52:28 +0000 | [diff] [blame] | 806 | auto FirstSectionOrDotAssignment = |
| 807 | std::find_if(Opt.Commands.begin(), Opt.Commands.end(), |
| 808 | [](BaseCommand *Cmd) { return !shouldSkip(Cmd); }); |
Rafael Espindola | 5fcc99c | 2016-11-27 09:44:45 +0000 | [diff] [blame] | 809 | if (FirstSectionOrDotAssignment != Opt.Commands.end()) { |
| 810 | CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin(); |
| 811 | if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment)) |
| 812 | ++CmdIndex; |
| 813 | } |
| 814 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 815 | for (OutputSection *Sec : *OutputSections) { |
Rafael Espindola | 4084941 | 2017-02-24 14:28:00 +0000 | [diff] [blame] | 816 | StringRef Name = Sec->Name; |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 817 | |
| 818 | // Find the last spot where we can insert a command and still get the |
Rafael Espindola | 15c5795 | 2016-09-22 18:05:49 +0000 | [diff] [blame] | 819 | // correct result. |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 820 | auto CmdIter = Opt.Commands.begin() + CmdIndex; |
| 821 | auto E = Opt.Commands.end(); |
Rui Ueyama | 4e1e88e | 2017-04-05 03:52:28 +0000 | [diff] [blame] | 822 | while (CmdIter != E && shouldSkip(*CmdIter)) { |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 823 | ++CmdIter; |
| 824 | ++CmdIndex; |
| 825 | } |
| 826 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 827 | auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) { |
| 828 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base); |
| 829 | return Cmd && Cmd->Name == Name; |
| 830 | }); |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 831 | if (Pos == E) { |
Rafael Espindola | 9b98009 | 2017-04-06 21:05:39 +0000 | [diff] [blame] | 832 | auto *Cmd = make<OutputSectionCommand>(Name); |
Rui Ueyama | 216aa11 | 2017-04-28 22:40:58 +0000 | [diff] [blame] | 833 | Cmd->Sec = Sec; |
Rafael Espindola | 9b98009 | 2017-04-06 21:05:39 +0000 | [diff] [blame] | 834 | Opt.Commands.insert(CmdIter, Cmd); |
Rafael Espindola | 15c5795 | 2016-09-22 18:05:49 +0000 | [diff] [blame] | 835 | ++CmdIndex; |
| 836 | continue; |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 837 | } |
Rafael Espindola | 15c5795 | 2016-09-22 18:05:49 +0000 | [diff] [blame] | 838 | |
| 839 | // Continue from where we found it. |
| 840 | CmdIndex = (Pos - Opt.Commands.begin()) + 1; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 841 | } |
Rafael Espindola | 337f903 | 2016-11-14 14:13:32 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 844 | void LinkerScript::processNonSectionCommands() { |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 845 | for (BaseCommand *Base : Opt.Commands) { |
| 846 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) |
Rui Ueyama | d379f73 | 2017-04-05 03:20:22 +0000 | [diff] [blame] | 847 | assignSymbol(Cmd, false); |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 848 | else if (auto *Cmd = dyn_cast<AssertCommand>(Base)) |
Petr Hosek | 02ad516 | 2017-03-15 03:33:23 +0000 | [diff] [blame] | 849 | Cmd->Expression(); |
| 850 | } |
| 851 | } |
| 852 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 853 | void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) { |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 854 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rafael Espindola | be60733 | 2016-09-30 00:16:11 +0000 | [diff] [blame] | 855 | Dot = 0; |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 856 | ErrorOnMissingSection = true; |
Rafael Espindola | 06f4743 | 2017-02-06 22:21:46 +0000 | [diff] [blame] | 857 | switchTo(Aether); |
| 858 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 859 | for (BaseCommand *Base : Opt.Commands) { |
| 860 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { |
Rui Ueyama | d379f73 | 2017-04-05 03:20:22 +0000 | [diff] [blame] | 861 | assignSymbol(Cmd, false); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 862 | continue; |
| 863 | } |
| 864 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 865 | if (auto *Cmd = dyn_cast<AssertCommand>(Base)) { |
Rafael Espindola | 4595df9 | 2017-03-10 16:04:26 +0000 | [diff] [blame] | 866 | Cmd->Expression(); |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 867 | continue; |
| 868 | } |
| 869 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 870 | auto *Cmd = cast<OutputSectionCommand>(Base); |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 871 | assignOffsets(Cmd); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 872 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 873 | |
George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 874 | uint64_t MinVA = std::numeric_limits<uint64_t>::max(); |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 875 | for (OutputSection *Sec : *OutputSections) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 876 | if (Sec->Flags & SHF_ALLOC) |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 877 | MinVA = std::min<uint64_t>(MinVA, Sec->Addr); |
Rafael Espindola | ea590d9 | 2017-02-08 15:19:03 +0000 | [diff] [blame] | 878 | else |
| 879 | Sec->Addr = 0; |
| 880 | } |
Rafael Espindola | aab6d5c | 2016-09-16 21:29:07 +0000 | [diff] [blame] | 881 | |
George Rimar | 2d26210 | 2017-03-14 09:03:53 +0000 | [diff] [blame] | 882 | allocateHeaders(Phdrs, *OutputSections, MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 885 | // Creates program headers as instructed by PHDRS linker script command. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 886 | std::vector<PhdrEntry> LinkerScript::createPhdrs() { |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 887 | std::vector<PhdrEntry> Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 888 | |
Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 889 | // Process PHDRS and FILEHDR keywords because they are not |
| 890 | // real output sections and cannot be added in the following loop. |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 891 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 892 | Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 893 | PhdrEntry &Phdr = Ret.back(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 894 | |
| 895 | if (Cmd.HasFilehdr) |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 896 | Phdr.add(Out::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 897 | if (Cmd.HasPhdrs) |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 898 | Phdr.add(Out::ProgramHeaders); |
Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 899 | |
| 900 | if (Cmd.LMAExpr) { |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 901 | Phdr.p_paddr = Cmd.LMAExpr().getValue(); |
Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 902 | Phdr.HasLMA = true; |
| 903 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 906 | // Add output sections to program headers. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 907 | for (OutputSection *Sec : *OutputSections) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 908 | if (!(Sec->Flags & SHF_ALLOC)) |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 909 | break; |
| 910 | |
Eugene Leviant | ce30b1c | 2016-10-19 15:04:49 +0000 | [diff] [blame] | 911 | // Assign headers specified by linker script |
Rafael Espindola | 4084941 | 2017-02-24 14:28:00 +0000 | [diff] [blame] | 912 | for (size_t Id : getPhdrIndices(Sec->Name)) { |
Eugene Leviant | ce30b1c | 2016-10-19 15:04:49 +0000 | [diff] [blame] | 913 | Ret[Id].add(Sec); |
| 914 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
Rafael Espindola | 17cb7c0 | 2016-12-19 17:01:01 +0000 | [diff] [blame] | 915 | Ret[Id].p_flags |= Sec->getPhdrFlags(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 916 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 917 | } |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 918 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 921 | bool LinkerScript::ignoreInterpSection() { |
Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 922 | // Ignore .interp section in case we have PHDRS specification |
| 923 | // and PT_INTERP isn't listed. |
Rui Ueyama | e31d988 | 2017-04-05 05:06:17 +0000 | [diff] [blame] | 924 | if (Opt.PhdrsCommands.empty()) |
| 925 | return false; |
| 926 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) |
| 927 | if (Cmd.Type == PT_INTERP) |
| 928 | return false; |
| 929 | return true; |
Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 930 | } |
| 931 | |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 932 | Optional<uint32_t> LinkerScript::getFiller(StringRef Name) { |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 933 | for (BaseCommand *Base : Opt.Commands) |
| 934 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 935 | if (Cmd->Name == Name) |
| 936 | return Cmd->Filler; |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 937 | return None; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 938 | } |
| 939 | |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 940 | static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) { |
Rui Ueyama | f62d260 | 2017-04-05 05:06:37 +0000 | [diff] [blame] | 941 | if (Size == 1) |
| 942 | *Buf = Data; |
| 943 | else if (Size == 2) |
Rui Ueyama | f93ed4d | 2017-03-21 21:40:08 +0000 | [diff] [blame] | 944 | write16(Buf, Data, Config->Endianness); |
Rui Ueyama | f62d260 | 2017-04-05 05:06:37 +0000 | [diff] [blame] | 945 | else if (Size == 4) |
Rui Ueyama | f93ed4d | 2017-03-21 21:40:08 +0000 | [diff] [blame] | 946 | write32(Buf, Data, Config->Endianness); |
Rui Ueyama | f62d260 | 2017-04-05 05:06:37 +0000 | [diff] [blame] | 947 | else if (Size == 8) |
Rui Ueyama | f93ed4d | 2017-03-21 21:40:08 +0000 | [diff] [blame] | 948 | write64(Buf, Data, Config->Endianness); |
Rui Ueyama | f62d260 | 2017-04-05 05:06:37 +0000 | [diff] [blame] | 949 | else |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 950 | llvm_unreachable("unsupported Size argument"); |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 953 | void LinkerScript::writeDataBytes(StringRef Name, uint8_t *Buf) { |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 954 | int I = getSectionIndex(Name); |
| 955 | if (I == INT_MAX) |
| 956 | return; |
| 957 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 958 | auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]); |
| 959 | for (BaseCommand *Base : Cmd->Commands) |
| 960 | if (auto *Data = dyn_cast<BytesDataCommand>(Base)) |
George Rimar | a8dba48 | 2017-03-20 10:09:58 +0000 | [diff] [blame] | 961 | writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size); |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 964 | bool LinkerScript::hasLMA(StringRef Name) { |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 965 | for (BaseCommand *Base : Opt.Commands) |
| 966 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) |
Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 967 | if (Cmd->LMAExpr && Cmd->Name == Name) |
| 968 | return true; |
| 969 | return false; |
George Rimar | 8ceadb3 | 2016-08-17 07:44:19 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 972 | // Returns the index of the given section name in linker script |
| 973 | // SECTIONS commands. Sections are laid out as the same order as they |
| 974 | // were in the script. If a given name did not appear in the script, |
| 975 | // it returns INT_MAX, so that it will be laid out at end of file. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 976 | int LinkerScript::getSectionIndex(StringRef Name) { |
Rui Ueyama | 6e68c5e | 2016-11-19 18:05:58 +0000 | [diff] [blame] | 977 | for (int I = 0, E = Opt.Commands.size(); I != E; ++I) |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 978 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) |
Rui Ueyama | f510fa6 | 2016-07-26 00:21:15 +0000 | [diff] [blame] | 979 | if (Cmd->Name == Name) |
| 980 | return I; |
Rui Ueyama | f510fa6 | 2016-07-26 00:21:15 +0000 | [diff] [blame] | 981 | return INT_MAX; |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 984 | ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) { |
Rafael Espindola | 4595df9 | 2017-03-10 16:04:26 +0000 | [diff] [blame] | 985 | if (S == ".") |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 986 | return {CurOutSec, Dot - CurOutSec->Addr}; |
George Rimar | a8dba48 | 2017-03-20 10:09:58 +0000 | [diff] [blame] | 987 | if (SymbolBody *B = findSymbol(S)) { |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 988 | if (auto *D = dyn_cast<DefinedRegular>(B)) |
| 989 | return {D->Section, D->Value}; |
Petr Hosek | 30f16b2 | 2017-03-23 03:52:34 +0000 | [diff] [blame] | 990 | if (auto *C = dyn_cast<DefinedCommon>(B)) |
| 991 | return {InX::Common, C->Offset}; |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 992 | } |
Eugene Leviant | f6aeed3 | 2016-12-22 13:13:12 +0000 | [diff] [blame] | 993 | error(Loc + ": symbol not found: " + S); |
George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 997 | bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; } |
George Rimar | f34f45f | 2016-09-23 13:17:23 +0000 | [diff] [blame] | 998 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 999 | // Returns indices of ELF headers containing specific section, identified |
| 1000 | // by Name. Each index is a zero based number of ELF header listed within |
| 1001 | // PHDRS {} script block. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 1002 | std::vector<size_t> LinkerScript::getPhdrIndices(StringRef SectionName) { |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 1003 | for (BaseCommand *Base : Opt.Commands) { |
| 1004 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base); |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 1005 | if (!Cmd || Cmd->Name != SectionName) |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 1006 | continue; |
| 1007 | |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 1008 | std::vector<size_t> Ret; |
| 1009 | for (StringRef PhdrName : Cmd->Phdrs) |
Eugene Leviant | 2a942c4 | 2016-12-05 16:38:32 +0000 | [diff] [blame] | 1010 | Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName)); |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 1011 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1012 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 1013 | return {}; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 1016 | size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) { |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 1017 | size_t I = 0; |
| 1018 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
| 1019 | if (Cmd.Name == PhdrName) |
| 1020 | return I; |
| 1021 | ++I; |
| 1022 | } |
Eugene Leviant | 2a942c4 | 2016-12-05 16:38:32 +0000 | [diff] [blame] | 1023 | error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS"); |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 1024 | return 0; |
| 1025 | } |