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