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