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