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