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" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Endian.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 32 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <cassert> |
| 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | #include <iterator> |
| 39 | #include <limits> |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 40 | #include <string> |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 41 | #include <vector> |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace llvm; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 44 | using namespace llvm::ELF; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 45 | using namespace llvm::object; |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 46 | using namespace llvm::support::endian; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 47 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 48 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 49 | |
Rui Ueyama | a34da93 | 2017-03-21 23:03:09 +0000 | [diff] [blame] | 50 | LinkerScript *elf::Script; |
| 51 | |
George Rimar | 5d0ea70 | 2017-08-21 07:57:12 +0000 | [diff] [blame] | 52 | static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) { |
| 53 | if (OutputSection *OS = InputSec->getOutputSection()) |
| 54 | return OS->Addr; |
| 55 | error(Loc + ": unable to evaluate expression: input section " + |
| 56 | InputSec->Name + " has no output section assigned"); |
| 57 | return 0; |
| 58 | } |
| 59 | |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 60 | uint64_t ExprValue::getValue() const { |
George Rimar | 5d0ea70 | 2017-08-21 07:57:12 +0000 | [diff] [blame] | 61 | if (Sec) |
| 62 | return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc), |
| 63 | Alignment); |
Petr Hosek | 3c6de1a | 2017-05-30 03:18:28 +0000 | [diff] [blame] | 64 | return alignTo(Val, Alignment); |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Rafael Espindola | 7ba5f47 | 2017-03-17 14:55:36 +0000 | [diff] [blame] | 67 | uint64_t ExprValue::getSecAddr() const { |
| 68 | if (Sec) |
George Rimar | 5d0ea70 | 2017-08-21 07:57:12 +0000 | [diff] [blame] | 69 | return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc); |
Rafael Espindola | 7ba5f47 | 2017-03-17 14:55:36 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Rafael Espindola | a5e8dd3 | 2017-07-26 18:47:49 +0000 | [diff] [blame] | 73 | static SymbolBody *addRegular(SymbolAssignment *Cmd) { |
Petr Hosek | 5e51f7d | 2017-02-21 22:32:51 +0000 | [diff] [blame] | 74 | Symbol *Sym; |
Rafael Espindola | 3dabfc6 | 2016-10-31 13:14:53 +0000 | [diff] [blame] | 75 | uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 76 | std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility, |
| 77 | /*CanOmitFromDynSym*/ false, |
| 78 | /*File*/ nullptr); |
Petr Hosek | 5e51f7d | 2017-02-21 22:32:51 +0000 | [diff] [blame] | 79 | Sym->Binding = STB_GLOBAL; |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 80 | ExprValue Value = Cmd->Expression(); |
| 81 | SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec; |
George Rimar | 01aa795 | 2017-04-14 09:23:26 +0000 | [diff] [blame] | 82 | |
| 83 | // We want to set symbol values early if we can. This allows us to use symbols |
| 84 | // as variables in linker scripts. Doing so allows us to write expressions |
| 85 | // like this: `alignment = 16; . = ALIGN(., alignment)` |
| 86 | uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0; |
Rafael Espindola | 6e93d05 | 2017-08-04 22:31:42 +0000 | [diff] [blame] | 87 | replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false, |
| 88 | Visibility, STT_NOTYPE, SymValue, 0, Sec); |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 89 | return Sym->body(); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 92 | OutputSection *LinkerScript::createOutputSection(StringRef Name, |
| 93 | StringRef Location) { |
| 94 | OutputSection *&SecRef = NameToOutputSection[Name]; |
| 95 | OutputSection *Sec; |
| 96 | if (SecRef && SecRef->Location.empty()) { |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 97 | // There was a forward reference. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 98 | Sec = SecRef; |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 99 | } else { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 100 | Sec = make<OutputSection>(Name, SHT_PROGBITS, 0); |
| 101 | if (!SecRef) |
| 102 | SecRef = Sec; |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 103 | } |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 104 | Sec->Location = Location; |
| 105 | return Sec; |
George Rimar | 851dc1e | 2017-03-14 10:15:53 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 108 | OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) { |
| 109 | OutputSection *&CmdRef = NameToOutputSection[Name]; |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 110 | if (!CmdRef) |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 111 | CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0); |
Rafael Espindola | 05c4f67 | 2017-06-01 01:16:50 +0000 | [diff] [blame] | 112 | return CmdRef; |
George Rimar | d83ce1b | 2017-03-14 10:24:47 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 115 | void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) { |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 116 | uint64_t Val = E().getValue(); |
George Rimar | 8c804d9 | 2017-07-12 14:50:25 +0000 | [diff] [blame] | 117 | if (Val < Dot && InSec) |
| 118 | error(Loc + ": unable to move location counter backward for: " + |
| 119 | CurAddressState->OutSec->Name); |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 120 | Dot = Val; |
| 121 | // Update to location counter means update to section size. |
| 122 | if (InSec) |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 123 | CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr; |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 124 | } |
| 125 | |
George Rimar | b2b7097 | 2017-02-07 10:23:28 +0000 | [diff] [blame] | 126 | // Sets value of a symbol. Two kinds of symbols are processed: synthetic |
| 127 | // symbols, whose value is an offset from beginning of section and regular |
| 128 | // symbols whose value is absolute. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 129 | void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) { |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 130 | if (Cmd->Name == ".") { |
George Rimar | 2ee2d2d | 2017-02-21 14:50:38 +0000 | [diff] [blame] | 131 | setDot(Cmd->Expression, Cmd->Location, InSec); |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
George Rimar | b2b7097 | 2017-02-07 10:23:28 +0000 | [diff] [blame] | 135 | if (!Cmd->Sym) |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 136 | return; |
| 137 | |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 138 | auto *Sym = cast<DefinedRegular>(Cmd->Sym); |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 139 | ExprValue V = Cmd->Expression(); |
| 140 | if (V.isAbsolute()) { |
| 141 | Sym->Value = V.getValue(); |
| 142 | } else { |
| 143 | Sym->Section = V.Sec; |
Rafael Espindola | dece280 | 2017-06-13 20:57:43 +0000 | [diff] [blame] | 144 | Sym->Value = alignTo(V.Val, V.Alignment); |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 145 | } |
Eugene Leviant | db741e7 | 2016-09-07 07:08:43 +0000 | [diff] [blame] | 146 | } |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 147 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 148 | void LinkerScript::addSymbol(SymbolAssignment *Cmd) { |
Rui Ueyama | 1602421 | 2016-08-11 23:22:52 +0000 | [diff] [blame] | 149 | if (Cmd->Name == ".") |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 150 | return; |
| 151 | |
| 152 | // If a symbol was in PROVIDE(), we need to define it only when |
| 153 | // it is a referenced undefined symbol. |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 154 | SymbolBody *B = Symtab->find(Cmd->Name); |
Meador Inge | 8f1f3c4 | 2017-01-09 18:36:57 +0000 | [diff] [blame] | 155 | if (Cmd->Provide && (!B || B->isDefined())) |
| 156 | return; |
| 157 | |
Rafael Espindola | a5e8dd3 | 2017-07-26 18:47:49 +0000 | [diff] [blame] | 158 | Cmd->Sym = addRegular(Cmd); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 161 | bool SymbolAssignment::classof(const BaseCommand *C) { |
| 162 | return C->Kind == AssignmentKind; |
| 163 | } |
| 164 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 165 | bool InputSectionDescription::classof(const BaseCommand *C) { |
| 166 | return C->Kind == InputSectionKind; |
| 167 | } |
| 168 | |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 169 | bool AssertCommand::classof(const BaseCommand *C) { |
| 170 | return C->Kind == AssertKind; |
| 171 | } |
| 172 | |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 173 | bool BytesDataCommand::classof(const BaseCommand *C) { |
| 174 | return C->Kind == BytesDataKind; |
| 175 | } |
| 176 | |
Dmitry Mikulin | f300ca2 | 2017-08-24 22:01:40 +0000 | [diff] [blame^] | 177 | static std::string filename(InputSectionBase *S) { |
| 178 | if (!S->File) |
| 179 | return ""; |
| 180 | if (S->File->ArchiveName.empty()) |
| 181 | return S->File->getName(); |
| 182 | return (S->File->ArchiveName + "(" + S->File->getName() + ")").str(); |
Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 185 | bool LinkerScript::shouldKeep(InputSectionBase *S) { |
Dmitry Mikulin | f300ca2 | 2017-08-24 22:01:40 +0000 | [diff] [blame^] | 186 | for (InputSectionDescription *ID : Opt.KeptSections) { |
| 187 | std::string Filename = filename(S); |
| 188 | if (ID->FilePat.match(Filename)) |
Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 189 | for (SectionPattern &P : ID->SectionPatterns) |
| 190 | if (P.SectionPat.match(S->Name)) |
| 191 | return true; |
Dmitry Mikulin | f300ca2 | 2017-08-24 22:01:40 +0000 | [diff] [blame^] | 192 | } |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 196 | // A helper function for the SORT() command. |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 197 | static std::function<bool(InputSectionBase *, InputSectionBase *)> |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 198 | getComparator(SortSectionPolicy K) { |
| 199 | switch (K) { |
| 200 | case SortSectionPolicy::Alignment: |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 201 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 202 | // ">" is not a mistake. Sections with larger alignments are placed |
| 203 | // before sections with smaller alignments in order to reduce the |
| 204 | // amount of padding necessary. This is compatible with GNU. |
| 205 | return A->Alignment > B->Alignment; |
| 206 | }; |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 207 | case SortSectionPolicy::Name: |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 208 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 209 | return A->Name < B->Name; |
| 210 | }; |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 211 | case SortSectionPolicy::Priority: |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 212 | return [](InputSectionBase *A, InputSectionBase *B) { |
| 213 | return getPriority(A->Name) < getPriority(B->Name); |
| 214 | }; |
George Rimar | be394db | 2016-09-16 20:21:55 +0000 | [diff] [blame] | 215 | default: |
| 216 | llvm_unreachable("unknown sort policy"); |
| 217 | } |
Rui Ueyama | 742c383 | 2016-08-04 22:27:00 +0000 | [diff] [blame] | 218 | } |
George Rimar | 0702c4e | 2016-07-29 15:32:46 +0000 | [diff] [blame] | 219 | |
Rui Ueyama | ea93fe0 | 2017-04-05 00:43:25 +0000 | [diff] [blame] | 220 | // A helper function for the SORT() command. |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 221 | static bool matchConstraints(ArrayRef<InputSectionBase *> Sections, |
George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 222 | ConstraintKind Kind) { |
George Rimar | 8f66df9 | 2016-08-12 20:38:20 +0000 | [diff] [blame] | 223 | if (Kind == ConstraintKind::NoConstraint) |
| 224 | return true; |
Rui Ueyama | 2c7171b | 2017-04-05 00:43:45 +0000 | [diff] [blame] | 225 | |
| 226 | bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) { |
| 227 | return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE; |
George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 228 | }); |
Rui Ueyama | 2c7171b | 2017-04-05 00:43:45 +0000 | [diff] [blame] | 229 | |
Rafael Espindola | e746e52 | 2016-09-21 18:33:44 +0000 | [diff] [blame] | 230 | return (IsRW && Kind == ConstraintKind::ReadWrite) || |
| 231 | (!IsRW && Kind == ConstraintKind::ReadOnly); |
George Rimar | 06ae683 | 2016-08-12 09:07:57 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 234 | static void sortSections(InputSection **Begin, InputSection **End, |
Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 235 | SortSectionPolicy K) { |
| 236 | if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) |
George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 237 | std::stable_sort(Begin, End, getComparator(K)); |
Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 238 | } |
| 239 | |
George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 240 | static llvm::DenseMap<SectionBase *, int> getSectionOrder() { |
| 241 | switch (Config->EKind) { |
| 242 | case ELF32LEKind: |
| 243 | return buildSectionOrder<ELF32LE>(); |
| 244 | case ELF32BEKind: |
| 245 | return buildSectionOrder<ELF32BE>(); |
| 246 | case ELF64LEKind: |
| 247 | return buildSectionOrder<ELF64LE>(); |
| 248 | case ELF64BEKind: |
| 249 | return buildSectionOrder<ELF64BE>(); |
| 250 | default: |
| 251 | llvm_unreachable("unknown ELF type"); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static void sortBySymbolOrder(InputSection **Begin, InputSection **End) { |
| 256 | if (Config->SymbolOrderingFile.empty()) |
| 257 | return; |
| 258 | static llvm::DenseMap<SectionBase *, int> Order = getSectionOrder(); |
| 259 | MutableArrayRef<InputSection *> In(Begin, End - Begin); |
| 260 | sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); }); |
| 261 | } |
| 262 | |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 263 | // Compute and remember which sections the InputSectionDescription matches. |
Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 264 | std::vector<InputSection *> |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 265 | LinkerScript::computeInputSections(const InputSectionDescription *Cmd) { |
Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 266 | std::vector<InputSection *> Ret; |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 267 | |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 268 | // Collects all sections that satisfy constraints of Cmd. |
| 269 | for (const SectionPattern &Pat : Cmd->SectionPatterns) { |
| 270 | size_t SizeBefore = Ret.size(); |
| 271 | |
| 272 | for (InputSectionBase *Sec : InputSections) { |
| 273 | if (Sec->Assigned) |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 274 | continue; |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 275 | |
Rafael Espindola | e39709b | 2017-05-30 20:40:03 +0000 | [diff] [blame] | 276 | if (!Sec->Live) { |
| 277 | reportDiscarded(Sec); |
| 278 | continue; |
| 279 | } |
| 280 | |
Rafael Espindola | 908a3d3 | 2017-02-16 14:36:09 +0000 | [diff] [blame] | 281 | // For -emit-relocs we have to ignore entries like |
| 282 | // .rela.dyn : { *(.rela.data) } |
| 283 | // which are common because they are in the default bfd script. |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 284 | if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA) |
Rafael Espindola | 908a3d3 | 2017-02-16 14:36:09 +0000 | [diff] [blame] | 285 | continue; |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 286 | |
Dmitry Mikulin | f300ca2 | 2017-08-24 22:01:40 +0000 | [diff] [blame^] | 287 | std::string Filename = filename(Sec); |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 288 | if (!Cmd->FilePat.match(Filename) || |
| 289 | Pat.ExcludedFilePat.match(Filename) || |
| 290 | !Pat.SectionPat.match(Sec->Name)) |
Rui Ueyama | e0be290 | 2016-11-21 02:10:12 +0000 | [diff] [blame] | 291 | continue; |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 292 | |
Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 293 | Ret.push_back(cast<InputSection>(Sec)); |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 294 | Sec->Assigned = true; |
George Rimar | 395281c | 2016-09-16 17:42:10 +0000 | [diff] [blame] | 295 | } |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 296 | |
George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 297 | // Sort sections as instructed by SORT-family commands and --sort-section |
| 298 | // option. Because SORT-family commands can be nested at most two depth |
| 299 | // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command |
| 300 | // line option is respected even if a SORT command is given, the exact |
| 301 | // behavior we have here is a bit complicated. Here are the rules. |
| 302 | // |
| 303 | // 1. If two SORT commands are given, --sort-section is ignored. |
| 304 | // 2. If one SORT command is given, and if it is not SORT_NONE, |
| 305 | // --sort-section is handled as an inner SORT command. |
| 306 | // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. |
| 307 | // 4. If no SORT command is given, sort according to --sort-section. |
George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 308 | // 5. If no SORT commands are given and --sort-section is not specified, |
| 309 | // apply sorting provided by --symbol-ordering-file if any exist. |
Rafael Espindola | 6a1aa8d | 2017-05-23 22:47:31 +0000 | [diff] [blame] | 310 | InputSection **Begin = Ret.data() + SizeBefore; |
| 311 | InputSection **End = Ret.data() + Ret.size(); |
George Rimar | d6bcde3 | 2017-08-04 10:25:29 +0000 | [diff] [blame] | 312 | if (Pat.SortOuter == SortSectionPolicy::Default && |
| 313 | Config->SortSection == SortSectionPolicy::Default) { |
| 314 | sortBySymbolOrder(Begin, End); |
| 315 | continue; |
| 316 | } |
George Rimar | 07171f2 | 2016-09-21 15:56:44 +0000 | [diff] [blame] | 317 | if (Pat.SortOuter != SortSectionPolicy::None) { |
| 318 | if (Pat.SortInner == SortSectionPolicy::Default) |
| 319 | sortSections(Begin, End, Config->SortSection); |
| 320 | else |
| 321 | sortSections(Begin, End, Pat.SortInner); |
| 322 | sortSections(Begin, End, Pat.SortOuter); |
| 323 | } |
Rui Ueyama | ee92470 | 2016-09-20 19:42:41 +0000 | [diff] [blame] | 324 | } |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 325 | return Ret; |
Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 328 | void LinkerScript::discard(ArrayRef<InputSectionBase *> V) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 329 | for (InputSectionBase *S : V) { |
Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 330 | S->Live = false; |
George Rimar | 945cd31 | 2017-08-10 08:07:05 +0000 | [diff] [blame] | 331 | if (S == InX::ShStrTab || S == InX::Common || S == InX::Dynamic || |
| 332 | S == InX::DynSymTab || S == InX::DynStrTab) |
Rafael Espindola | 2af64b0 | 2017-06-16 23:45:35 +0000 | [diff] [blame] | 333 | error("discarding " + S->Name + " section is not allowed"); |
George Rimar | 647c168 | 2017-02-17 19:34:05 +0000 | [diff] [blame] | 334 | discard(S->DependentSections); |
Rafael Espindola | be94e1b | 2016-09-14 14:32:08 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 338 | std::vector<InputSectionBase *> |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 339 | LinkerScript::createInputSectionList(OutputSection &OutCmd) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 340 | std::vector<InputSectionBase *> Ret; |
Rui Ueyama | e7f912c | 2016-08-03 21:12:09 +0000 | [diff] [blame] | 341 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 342 | for (BaseCommand *Base : OutCmd.Commands) { |
| 343 | auto *Cmd = dyn_cast<InputSectionDescription>(Base); |
Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 344 | if (!Cmd) |
Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 345 | continue; |
Rui Ueyama | 72e107f | 2017-04-05 02:05:48 +0000 | [diff] [blame] | 346 | |
| 347 | Cmd->Sections = computeInputSections(Cmd); |
Rafael Espindola | e4c8b9b | 2017-04-07 16:10:46 +0000 | [diff] [blame] | 348 | Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end()); |
Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 349 | } |
Rafael Espindola | e71a3f8a | 2016-09-16 20:34:02 +0000 | [diff] [blame] | 350 | |
Rui Ueyama | 0b9ce6a | 2016-08-12 03:16:56 +0000 | [diff] [blame] | 351 | return Ret; |
| 352 | } |
| 353 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 354 | void LinkerScript::processCommands(OutputSectionFactory &Factory) { |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 355 | // A symbol can be assigned before any section is mentioned in the linker |
| 356 | // script. In an DSO, the symbol values are addresses, so the only important |
| 357 | // section values are: |
| 358 | // * SHN_UNDEF |
| 359 | // * SHN_ABS |
| 360 | // * Any value meaning a regular section. |
| 361 | // To handle that, create a dummy aether section that fills the void before |
| 362 | // the linker scripts switches to another section. It has an index of one |
| 363 | // which will map to whatever the first actual section is. |
| 364 | Aether = make<OutputSection>("", 0, SHF_ALLOC); |
| 365 | Aether->SectionIndex = 1; |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 366 | auto State = make_unique<AddressState>(Opt); |
Peter Smith | c1ace40 | 2017-07-11 09:28:27 +0000 | [diff] [blame] | 367 | // CurAddressState captures the local AddressState and makes it accessible |
| 368 | // deliberately. This is needed as there are some cases where we cannot just |
| 369 | // thread the current state through to a lambda function created by the |
| 370 | // script parser. |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 371 | CurAddressState = State.get(); |
| 372 | CurAddressState->OutSec = Aether; |
Rafael Espindola | 49592cf | 2017-03-20 14:33:33 +0000 | [diff] [blame] | 373 | Dot = 0; |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 374 | |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 375 | for (size_t I = 0; I < Opt.Commands.size(); ++I) { |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 376 | // Handle symbol assignments outside of any output section. |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 377 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) { |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 378 | addSymbol(Cmd); |
Rui Ueyama | 2ab5f73 | 2016-08-12 03:33:04 +0000 | [diff] [blame] | 379 | continue; |
| 380 | } |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 381 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 382 | if (auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I])) { |
| 383 | std::vector<InputSectionBase *> V = createInputSectionList(*Sec); |
Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 384 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 385 | // The output section name `/DISCARD/' is special. |
| 386 | // Any input section assigned to it is discarded. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 387 | if (Sec->Name == "/DISCARD/") { |
Rafael Espindola | 7bd3787 | 2016-09-12 16:05:16 +0000 | [diff] [blame] | 388 | discard(V); |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 389 | continue; |
| 390 | } |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 391 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 392 | // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive |
| 393 | // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input |
| 394 | // sections satisfy a given constraint. If not, a directive is handled |
George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 395 | // as if it wasn't present from the beginning. |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 396 | // |
| 397 | // Because we'll iterate over Commands many more times, the easiest |
George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 398 | // way to "make it as if it wasn't present" is to just remove it. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 399 | if (!matchConstraints(V, Sec->Constraint)) { |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 400 | for (InputSectionBase *S : V) |
Rui Ueyama | f94efdd | 2016-11-20 23:15:52 +0000 | [diff] [blame] | 401 | S->Assigned = false; |
Rui Ueyama | 92a5ba6 | 2017-04-05 16:07:44 +0000 | [diff] [blame] | 402 | Opt.Commands.erase(Opt.Commands.begin() + I); |
George Rimar | 07d7c42 | 2017-04-05 09:19:29 +0000 | [diff] [blame] | 403 | --I; |
Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 404 | continue; |
| 405 | } |
| 406 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 407 | // A directive may contain symbol definitions like this: |
| 408 | // ".foo : { ...; bar = .; }". Handle them. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 409 | for (BaseCommand *Base : Sec->Commands) |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 410 | if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base)) |
Rafael Espindola | 4cd7352 | 2017-02-17 16:01:51 +0000 | [diff] [blame] | 411 | addSymbol(OutCmd); |
Rafael Espindola | 7c3ff2e | 2016-09-16 21:05:36 +0000 | [diff] [blame] | 412 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 413 | // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign |
| 414 | // is given, input sections are aligned to that value, whether the |
| 415 | // given value is larger or smaller than the original section alignment. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 416 | if (Sec->SubalignExpr) { |
| 417 | uint32_t Subalign = Sec->SubalignExpr().getValue(); |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 418 | for (InputSectionBase *S : V) |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 419 | S->Alignment = Subalign; |
George Rimar | db24d9c | 2016-08-19 15:18:23 +0000 | [diff] [blame] | 420 | } |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 421 | |
| 422 | // Add input sections to an output section. |
George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 423 | for (InputSectionBase *S : V) |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 424 | Factory.addInputSec(S, Sec->Name, Sec); |
| 425 | assert(Sec->SectionIndex == INT_MAX); |
| 426 | Sec->SectionIndex = I; |
| 427 | if (Sec->Noload) |
| 428 | Sec->Type = SHT_NOBITS; |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 429 | } |
Rui Ueyama | 48c3f1c | 2016-08-12 00:27:23 +0000 | [diff] [blame] | 430 | } |
Peter Smith | c1ace40 | 2017-07-11 09:28:27 +0000 | [diff] [blame] | 431 | CurAddressState = nullptr; |
Eugene Leviant | 20d0319 | 2016-09-16 15:30:47 +0000 | [diff] [blame] | 432 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 433 | |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 434 | void LinkerScript::fabricateDefaultCommands() { |
Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 435 | // Define start address |
Rafael Espindola | e76231b | 2017-06-08 04:17:04 +0000 | [diff] [blame] | 436 | uint64_t StartAddr = -1; |
Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 437 | |
Peter Smith | c60b451 | 2017-05-03 08:44:50 +0000 | [diff] [blame] | 438 | // The Sections with -T<section> have been sorted in order of ascending |
| 439 | // address. We must lower StartAddr if the lowest -T<section address> as |
| 440 | // calls to setDot() must be monotonically increasing. |
George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 441 | for (auto &KV : Config->SectionStartMap) |
Peter Smith | c60b451 | 2017-05-03 08:44:50 +0000 | [diff] [blame] | 442 | StartAddr = std::min(StartAddr, KV.second); |
| 443 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 444 | Opt.Commands.insert(Opt.Commands.begin(), |
| 445 | make<SymbolAssignment>(".", |
| 446 | [=] { |
| 447 | return std::min( |
| 448 | StartAddr, |
| 449 | Config->ImageBase + |
| 450 | elf::getHeaderSize()); |
| 451 | }, |
| 452 | "")); |
Peter Smith | cbfe9e9 | 2017-04-19 12:46:32 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 455 | // Add sections that didn't match any sections command. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 456 | void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) { |
Rafael Espindola | d7faa91 | 2017-07-05 17:50:43 +0000 | [diff] [blame] | 457 | unsigned NumCommands = Opt.Commands.size(); |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 458 | for (InputSectionBase *S : InputSections) { |
Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 459 | if (!S->Live || S->Parent) |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 460 | continue; |
| 461 | StringRef Name = getOutputSectionName(S->Name); |
Rafael Espindola | d7faa91 | 2017-07-05 17:50:43 +0000 | [diff] [blame] | 462 | auto End = Opt.Commands.begin() + NumCommands; |
| 463 | auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 464 | if (auto *Sec = dyn_cast<OutputSection>(Base)) |
| 465 | return Sec->Name == Name; |
George Rimar | a951d5c | 2017-07-04 13:10:37 +0000 | [diff] [blame] | 466 | return false; |
| 467 | }); |
Rafael Espindola | d7faa91 | 2017-07-05 17:50:43 +0000 | [diff] [blame] | 468 | if (I == End) { |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 469 | Factory.addInputSec(S, Name); |
Rafael Espindola | a2df2f0 | 2017-07-27 21:42:42 +0000 | [diff] [blame] | 470 | assert(S->getOutputSection()->SectionIndex == INT_MAX); |
Rafael Espindola | de8d989 | 2017-04-29 15:44:03 +0000 | [diff] [blame] | 471 | } else { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 472 | OutputSection *Sec = cast<OutputSection>(*I); |
| 473 | Factory.addInputSec(S, Name, Sec); |
| 474 | unsigned Index = std::distance(Opt.Commands.begin(), I); |
| 475 | assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index); |
| 476 | Sec->SectionIndex = Index; |
Rafael Espindola | de8d989 | 2017-04-29 15:44:03 +0000 | [diff] [blame] | 477 | } |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 478 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 481 | uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) { |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 482 | bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) && |
| 483 | CurAddressState->OutSec->Type == SHT_NOBITS; |
| 484 | uint64_t Start = IsTbss ? Dot + CurAddressState->ThreadBssOffset : Dot; |
Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 485 | Start = alignTo(Start, Align); |
| 486 | uint64_t End = Start + Size; |
| 487 | |
| 488 | if (IsTbss) |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 489 | CurAddressState->ThreadBssOffset = End - Dot; |
Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 490 | else |
| 491 | Dot = End; |
| 492 | return End; |
Rafael Espindola | a940e53 | 2016-09-22 12:35:44 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 495 | void LinkerScript::output(InputSection *S) { |
George Rimar | f694d33 | 2017-07-25 08:29:29 +0000 | [diff] [blame] | 496 | uint64_t Before = advance(0, 1); |
Rafael Espindola | 7c4eafa | 2017-05-04 03:00:27 +0000 | [diff] [blame] | 497 | uint64_t Pos = advance(S->getSize(), S->Alignment); |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 498 | S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr; |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 499 | |
| 500 | // Update output section size after adding each section. This is so that |
| 501 | // SIZEOF works correctly in the case below: |
| 502 | // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 503 | CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->Addr; |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 504 | |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 505 | // If there is a memory region associated with this input section, then |
| 506 | // place the section in that region and update the region index. |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 507 | if (CurAddressState->MemRegion) { |
| 508 | uint64_t &CurOffset = |
| 509 | CurAddressState->MemRegionOffset[CurAddressState->MemRegion]; |
George Rimar | f694d33 | 2017-07-25 08:29:29 +0000 | [diff] [blame] | 510 | CurOffset += Pos - Before; |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 511 | uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin; |
| 512 | if (CurSize > CurAddressState->MemRegion->Length) { |
| 513 | uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length; |
| 514 | error("section '" + CurAddressState->OutSec->Name + |
| 515 | "' will not fit in region '" + CurAddressState->MemRegion->Name + |
| 516 | "': overflowed by " + Twine(OverflowAmt) + " bytes"); |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 521 | void LinkerScript::switchTo(OutputSection *Sec) { |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 522 | if (CurAddressState->OutSec == Sec) |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 523 | return; |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 524 | |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 525 | CurAddressState->OutSec = Sec; |
| 526 | CurAddressState->OutSec->Addr = |
| 527 | advance(0, CurAddressState->OutSec->Alignment); |
Eugene Leviant | b71d6f7 | 2016-10-06 09:39:28 +0000 | [diff] [blame] | 528 | |
| 529 | // If neither AT nor AT> is specified for an allocatable section, the linker |
| 530 | // will set the LMA such that the difference between VMA and LMA for the |
| 531 | // section is the same as the preceding output section in the same region |
| 532 | // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 533 | if (CurAddressState->LMAOffset) |
| 534 | CurAddressState->OutSec->LMAOffset = CurAddressState->LMAOffset(); |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 537 | void LinkerScript::process(BaseCommand &Base) { |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 538 | // This handles the assignments to symbol or to the dot. |
| 539 | if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) { |
| 540 | assignSymbol(Cmd, true); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 541 | return; |
Rui Ueyama | 2de509c | 2016-08-12 00:55:08 +0000 | [diff] [blame] | 542 | } |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 543 | |
| 544 | // Handle BYTE(), SHORT(), LONG(), or QUAD(). |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 545 | if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) { |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 546 | Cmd->Offset = Dot - CurAddressState->OutSec->Addr; |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 547 | Dot += Cmd->Size; |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 548 | CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr; |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 552 | // Handle ASSERT(). |
| 553 | if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) { |
| 554 | Cmd->Expression(); |
Meador Inge | b2d99d6 | 2016-11-22 18:01:50 +0000 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 558 | // Handle a single input section description command. |
| 559 | // It calculates and assigns the offsets for each section and also |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 560 | // updates the output section size. |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 561 | auto &Cmd = cast<InputSectionDescription>(Base); |
Rafael Espindola | a85e8dd | 2017-05-30 20:24:52 +0000 | [diff] [blame] | 562 | for (InputSection *Sec : Cmd.Sections) { |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 563 | // We tentatively added all synthetic sections at the beginning and removed |
| 564 | // empty ones afterwards (because there is no way to know whether they were |
| 565 | // going be empty or not other than actually running linker scripts.) |
| 566 | // We need to ignore remains of empty sections. |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 567 | if (auto *S = dyn_cast<SyntheticSection>(Sec)) |
| 568 | if (S->empty()) |
George Rimar | 3fb5a6d | 2016-11-29 16:05:27 +0000 | [diff] [blame] | 569 | continue; |
| 570 | |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 571 | if (!Sec->Live) |
George Rimar | 78ef645 | 2017-02-21 15:46:43 +0000 | [diff] [blame] | 572 | continue; |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 573 | assert(CurAddressState->OutSec == Sec->getParent()); |
Rafael Espindola | a85e8dd | 2017-05-30 20:24:52 +0000 | [diff] [blame] | 574 | output(Sec); |
Eugene Leviant | ceabe80 | 2016-08-11 07:56:43 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 578 | // This function searches for a memory region to place the given output |
| 579 | // section in. If found, a pointer to the appropriate memory region is |
| 580 | // returned. Otherwise, a nullptr is returned. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 581 | MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) { |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 582 | // If a memory region name was specified in the output section command, |
| 583 | // then try to find that region first. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 584 | if (!Sec->MemoryRegionName.empty()) { |
| 585 | auto It = Opt.MemoryRegions.find(Sec->MemoryRegionName); |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 586 | if (It != Opt.MemoryRegions.end()) |
| 587 | return &It->second; |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 588 | error("memory region '" + Sec->MemoryRegionName + "' not declared"); |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 589 | return nullptr; |
| 590 | } |
| 591 | |
Rui Ueyama | d7c5400 | 2017-04-05 03:19:43 +0000 | [diff] [blame] | 592 | // If at least one memory region is defined, all sections must |
| 593 | // belong to some memory region. Otherwise, we don't need to do |
| 594 | // anything for memory regions. |
Rui Ueyama | cc400cc | 2017-04-05 03:19:24 +0000 | [diff] [blame] | 595 | if (Opt.MemoryRegions.empty()) |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 596 | return nullptr; |
| 597 | |
| 598 | // See if a region can be found by matching section flags. |
Rui Ueyama | 2e081a4 | 2017-04-05 03:18:46 +0000 | [diff] [blame] | 599 | for (auto &Pair : Opt.MemoryRegions) { |
| 600 | MemoryRegion &M = Pair.second; |
| 601 | if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0) |
| 602 | return &M; |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // Otherwise, no suitable region was found. |
| 606 | if (Sec->Flags & SHF_ALLOC) |
| 607 | error("no memory region specified for section '" + Sec->Name + "'"); |
| 608 | return nullptr; |
| 609 | } |
| 610 | |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 611 | // This function assigns offsets to input sections and an output section |
| 612 | // for a single sections command (e.g. ".text { *(.text); }"). |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 613 | void LinkerScript::assignOffsets(OutputSection *Sec) { |
Rafael Espindola | dece280 | 2017-06-13 20:57:43 +0000 | [diff] [blame] | 614 | if (!(Sec->Flags & SHF_ALLOC)) |
| 615 | Dot = 0; |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 616 | else if (Sec->AddrExpr) |
| 617 | setDot(Sec->AddrExpr, Sec->Location, false); |
Rafael Espindola | 679828f | 2017-02-17 16:26:13 +0000 | [diff] [blame] | 618 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 619 | if (Sec->LMAExpr) { |
George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 620 | uint64_t D = Dot; |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 621 | CurAddressState->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; }; |
Eugene Leviant | 5784e96 | 2017-03-14 08:57:09 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 624 | CurAddressState->MemRegion = Sec->MemRegion; |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 625 | if (CurAddressState->MemRegion) |
| 626 | Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion]; |
Meador Inge | b889744 | 2017-01-24 02:34:00 +0000 | [diff] [blame] | 627 | switchTo(Sec); |
Rui Ueyama | 0b1b695 | 2016-11-21 02:11:05 +0000 | [diff] [blame] | 628 | |
George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 629 | // We do not support custom layout for compressed debug sectons. |
| 630 | // At this point we already know their size and have compressed content. |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 631 | if (CurAddressState->OutSec->Flags & SHF_COMPRESSED) |
George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 632 | return; |
| 633 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 634 | for (BaseCommand *C : Sec->Commands) |
Rafael Espindola | de8d989 | 2017-04-29 15:44:03 +0000 | [diff] [blame] | 635 | process(*C); |
Rafael Espindola | d319079 | 2016-09-16 15:10:23 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 638 | void LinkerScript::removeEmptyCommands() { |
Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 639 | // It is common practice to use very generic linker scripts. So for any |
| 640 | // given run some of the output sections in the script will be empty. |
| 641 | // We could create corresponding empty output sections, but that would |
| 642 | // clutter the output. |
| 643 | // We instead remove trivially empty sections. The bfd linker seems even |
| 644 | // more aggressive at removing them. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 645 | auto Pos = std::remove_if(Opt.Commands.begin(), Opt.Commands.end(), |
| 646 | [&](BaseCommand *Base) { |
| 647 | if (auto *Sec = dyn_cast<OutputSection>(Base)) |
| 648 | return !Sec->Live; |
| 649 | return false; |
| 650 | }); |
Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 651 | Opt.Commands.erase(Pos, Opt.Commands.end()); |
Rafael Espindola | 07fe612 | 2016-11-14 14:23:35 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 654 | static bool isAllSectionDescription(const OutputSection &Cmd) { |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 655 | for (BaseCommand *Base : Cmd.Commands) |
| 656 | if (!isa<InputSectionDescription>(*Base)) |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 657 | return false; |
| 658 | return true; |
| 659 | } |
Rafael Espindola | 6d38e4d | 2016-09-20 13:12:07 +0000 | [diff] [blame] | 660 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 661 | void LinkerScript::adjustSectionsBeforeSorting() { |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 662 | // If the output section contains only symbol assignments, create a |
| 663 | // corresponding output section. The bfd linker seems to only create them if |
| 664 | // '.' is assigned to, but creating these section should not have any bad |
| 665 | // consequeces and gives us a section to put the symbol in. |
George Rimar | 0c1c808 | 2017-03-14 10:00:19 +0000 | [diff] [blame] | 666 | uint64_t Flags = SHF_ALLOC; |
Rafael Espindola | 660c9ab | 2017-05-05 21:34:26 +0000 | [diff] [blame] | 667 | |
| 668 | for (int I = 0, E = Opt.Commands.size(); I != E; ++I) { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 669 | auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I]); |
| 670 | if (!Sec) |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 671 | continue; |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 672 | if (Sec->Live) { |
Rafael Espindola | 2b07455 | 2017-02-03 22:27:05 +0000 | [diff] [blame] | 673 | Flags = Sec->Flags; |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 674 | continue; |
| 675 | } |
| 676 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 677 | if (isAllSectionDescription(*Sec)) |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 678 | continue; |
| 679 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 680 | Sec->Live = true; |
| 681 | Sec->SectionIndex = I; |
| 682 | Sec->Flags = Flags; |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 683 | } |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 686 | void LinkerScript::adjustSectionsAfterSorting() { |
Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 687 | // Try and find an appropriate memory region to assign offsets in. |
Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 688 | for (BaseCommand *Base : Opt.Commands) { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 689 | if (auto *Sec = dyn_cast<OutputSection>(Base)) { |
| 690 | Sec->MemRegion = findMemoryRegion(Sec); |
Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 691 | // Handle align (e.g. ".foo : ALIGN(16) { ... }"). |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 692 | if (Sec->AlignExpr) |
| 693 | Sec->updateAlignment(Sec->AlignExpr().getValue()); |
Rafael Espindola | d1960dc | 2017-04-06 21:40:22 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Rafael Espindola | feed750 | 2017-04-06 21:31:24 +0000 | [diff] [blame] | 696 | |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 697 | // If output section command doesn't specify any segments, |
| 698 | // and we haven't previously assigned any section to segment, |
| 699 | // then we simply assign section to the very first load segment. |
| 700 | // Below is an example of such linker script: |
| 701 | // PHDRS { seg PT_LOAD; } |
| 702 | // SECTIONS { .aaa : { *(.aaa) } } |
| 703 | std::vector<StringRef> DefPhdrs; |
| 704 | auto FirstPtLoad = |
| 705 | std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(), |
| 706 | [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); |
| 707 | if (FirstPtLoad != Opt.PhdrsCommands.end()) |
| 708 | DefPhdrs.push_back(FirstPtLoad->Name); |
| 709 | |
| 710 | // Walk the commands and propagate the program headers to commands that don't |
| 711 | // explicitly specify them. |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 712 | for (BaseCommand *Base : Opt.Commands) { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 713 | auto *Sec = dyn_cast<OutputSection>(Base); |
| 714 | if (!Sec) |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 715 | continue; |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 716 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 717 | if (Sec->Phdrs.empty()) { |
Andrew Ng | a020d34 | 2017-07-03 10:11:25 +0000 | [diff] [blame] | 718 | // To match the bfd linker script behaviour, only propagate program |
| 719 | // headers to sections that are allocated. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 720 | if (Sec->Flags & SHF_ALLOC) |
| 721 | Sec->Phdrs = DefPhdrs; |
Andrew Ng | a020d34 | 2017-07-03 10:11:25 +0000 | [diff] [blame] | 722 | } else { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 723 | DefPhdrs = Sec->Phdrs; |
Andrew Ng | a020d34 | 2017-07-03 10:11:25 +0000 | [diff] [blame] | 724 | } |
Rafael Espindola | f7a1744 | 2016-11-14 15:39:38 +0000 | [diff] [blame] | 725 | } |
Rafael Espindola | 6a53737 | 2016-11-14 14:33:49 +0000 | [diff] [blame] | 726 | |
| 727 | removeEmptyCommands(); |
Rafael Espindola | 9546fff | 2016-09-22 14:40:50 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Petr Hosek | b93c5b9 | 2017-08-23 18:44:34 +0000 | [diff] [blame] | 730 | // Try to find an address for the file and program headers output sections, |
| 731 | // which were unconditionally added to the first PT_LOAD segment earlier. |
| 732 | // |
| 733 | // When using the default layout, we check if the headers fit below the first |
| 734 | // allocated section. When using a linker script, we also check if the headers |
| 735 | // are covered by the output section. This allows omitting the headers by not |
| 736 | // leaving enough space for them in the linker script; this pattern is common |
| 737 | // in embedded systems. |
| 738 | // |
| 739 | // If there isn't enough space for these sections, we'll remove them from the |
| 740 | // PT_LOAD segment, and we'll also remove the PT_PHDR segment. |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 741 | void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) { |
Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 742 | uint64_t Min = std::numeric_limits<uint64_t>::max(); |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 743 | for (OutputSection *Sec : OutputSections) |
Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 744 | if (Sec->Flags & SHF_ALLOC) |
| 745 | Min = std::min<uint64_t>(Min, Sec->Addr); |
Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 746 | |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 747 | auto It = llvm::find_if( |
| 748 | Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; }); |
| 749 | if (It == Phdrs.end()) |
George Rimar | d971e70 | 2017-07-03 16:05:51 +0000 | [diff] [blame] | 750 | return; |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 751 | PhdrEntry *FirstPTLoad = *It; |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 752 | |
| 753 | uint64_t HeaderSize = getHeaderSize(); |
Petr Hosek | b93c5b9 | 2017-08-23 18:44:34 +0000 | [diff] [blame] | 754 | // When linker script with SECTIONS is being used, don't output headers |
| 755 | // unless there's a space for them. |
| 756 | uint64_t Base = Opt.HasSections ? alignDown(Min, Config->MaxPageSize) : 0; |
| 757 | if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) { |
| 758 | Min = Opt.HasSections ? Base |
| 759 | : alignDown(Min - HeaderSize, Config->MaxPageSize); |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 760 | Out::ElfHeader->Addr = Min; |
| 761 | Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size; |
George Rimar | d971e70 | 2017-07-03 16:05:51 +0000 | [diff] [blame] | 762 | return; |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 765 | OutputSection *ActualFirst = nullptr; |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 766 | for (OutputSection *Sec : OutputSections) { |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 767 | if (Sec->FirstInPtLoad == Out::ElfHeader) { |
| 768 | ActualFirst = Sec; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | if (ActualFirst) { |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 773 | for (OutputSection *Sec : OutputSections) |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 774 | if (Sec->FirstInPtLoad == Out::ElfHeader) |
| 775 | Sec->FirstInPtLoad = ActualFirst; |
| 776 | FirstPTLoad->First = ActualFirst; |
| 777 | } else { |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 778 | Phdrs.erase(It); |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 779 | } |
| 780 | |
George Rimar | d971e70 | 2017-07-03 16:05:51 +0000 | [diff] [blame] | 781 | auto PhdrI = llvm::find_if( |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 782 | Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_PHDR; }); |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 783 | if (PhdrI != Phdrs.end()) |
| 784 | Phdrs.erase(PhdrI); |
Rafael Espindola | 02ed757 | 2017-05-04 19:34:17 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 787 | LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) { |
| 788 | for (auto &MRI : Opt.MemoryRegions) { |
| 789 | const MemoryRegion *MR = &MRI.second; |
| 790 | MemRegionOffset[MR] = MR->Origin; |
| 791 | } |
| 792 | } |
| 793 | |
Peter Smith | 5aedebf | 2017-07-05 09:12:54 +0000 | [diff] [blame] | 794 | void LinkerScript::assignAddresses() { |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 795 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rafael Espindola | be60733 | 2016-09-30 00:16:11 +0000 | [diff] [blame] | 796 | Dot = 0; |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 797 | auto State = make_unique<AddressState>(Opt); |
Peter Smith | c1ace40 | 2017-07-11 09:28:27 +0000 | [diff] [blame] | 798 | // CurAddressState captures the local AddressState and makes it accessible |
| 799 | // deliberately. This is needed as there are some cases where we cannot just |
| 800 | // thread the current state through to a lambda function created by the |
| 801 | // script parser. |
Peter Smith | 906e9a1 | 2017-07-07 09:11:27 +0000 | [diff] [blame] | 802 | CurAddressState = State.get(); |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 803 | ErrorOnMissingSection = true; |
Rafael Espindola | 06f4743 | 2017-02-06 22:21:46 +0000 | [diff] [blame] | 804 | switchTo(Aether); |
| 805 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 806 | for (BaseCommand *Base : Opt.Commands) { |
| 807 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { |
Rui Ueyama | d379f73 | 2017-04-05 03:20:22 +0000 | [diff] [blame] | 808 | assignSymbol(Cmd, false); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 809 | continue; |
| 810 | } |
| 811 | |
Rui Ueyama | 8f99f73 | 2017-04-05 03:20:42 +0000 | [diff] [blame] | 812 | if (auto *Cmd = dyn_cast<AssertCommand>(Base)) { |
Rafael Espindola | 4595df9 | 2017-03-10 16:04:26 +0000 | [diff] [blame] | 813 | Cmd->Expression(); |
George Rimar | eefa758 | 2016-08-04 09:29:31 +0000 | [diff] [blame] | 814 | continue; |
| 815 | } |
| 816 | |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 817 | assignOffsets(cast<OutputSection>(Base)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 818 | } |
Peter Smith | c1ace40 | 2017-07-11 09:28:27 +0000 | [diff] [blame] | 819 | CurAddressState = nullptr; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 822 | // Creates program headers as instructed by PHDRS linker script command. |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 823 | std::vector<PhdrEntry *> LinkerScript::createPhdrs() { |
| 824 | std::vector<PhdrEntry *> Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 825 | |
Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 826 | // Process PHDRS and FILEHDR keywords because they are not |
| 827 | // real output sections and cannot be added in the following loop. |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 828 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 829 | PhdrEntry *Phdr = |
| 830 | make<PhdrEntry>(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 831 | |
| 832 | if (Cmd.HasFilehdr) |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 833 | Phdr->add(Out::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 834 | if (Cmd.HasPhdrs) |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 835 | Phdr->add(Out::ProgramHeaders); |
Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 836 | |
| 837 | if (Cmd.LMAExpr) { |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 838 | Phdr->p_paddr = Cmd.LMAExpr().getValue(); |
| 839 | Phdr->HasLMA = true; |
Eugene Leviant | 56b21c8 | 2016-09-09 09:46:16 +0000 | [diff] [blame] | 840 | } |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 841 | Ret.push_back(Phdr); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Rui Ueyama | 464daad | 2016-08-22 04:55:20 +0000 | [diff] [blame] | 844 | // Add output sections to program headers. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 845 | for (OutputSection *Sec : OutputSections) { |
Eugene Leviant | ce30b1c | 2016-10-19 15:04:49 +0000 | [diff] [blame] | 846 | // Assign headers specified by linker script |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 847 | for (size_t Id : getPhdrIndices(Sec)) { |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 848 | Ret[Id]->add(Sec); |
Eugene Leviant | ce30b1c | 2016-10-19 15:04:49 +0000 | [diff] [blame] | 849 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
George Rimar | aa35418 | 2017-07-27 07:46:50 +0000 | [diff] [blame] | 850 | Ret[Id]->p_flags |= Sec->getPhdrFlags(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 851 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 852 | } |
Rui Ueyama | edebbdf | 2016-07-24 23:47:31 +0000 | [diff] [blame] | 853 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 856 | bool LinkerScript::ignoreInterpSection() { |
Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 857 | // Ignore .interp section in case we have PHDRS specification |
| 858 | // and PT_INTERP isn't listed. |
Rui Ueyama | e31d988 | 2017-04-05 05:06:17 +0000 | [diff] [blame] | 859 | if (Opt.PhdrsCommands.empty()) |
| 860 | return false; |
| 861 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) |
| 862 | if (Cmd.Type == PT_INTERP) |
| 863 | return false; |
| 864 | return true; |
Eugene Leviant | f9bc3bd | 2016-08-16 06:40:58 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 867 | ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) { |
George Rimar | 7e5b0a59 | 2017-08-17 08:47:21 +0000 | [diff] [blame] | 868 | if (S == ".") { |
| 869 | if (CurAddressState) |
| 870 | return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, |
| 871 | Loc}; |
| 872 | error(Loc + ": unable to get location counter value"); |
| 873 | return 0; |
| 874 | } |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 875 | if (SymbolBody *B = Symtab->find(S)) { |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 876 | if (auto *D = dyn_cast<DefinedRegular>(B)) |
George Rimar | 41c7ab4 | 2017-06-07 08:54:43 +0000 | [diff] [blame] | 877 | return {D->Section, D->Value, Loc}; |
Petr Hosek | 30f16b2 | 2017-03-23 03:52:34 +0000 | [diff] [blame] | 878 | if (auto *C = dyn_cast<DefinedCommon>(B)) |
George Rimar | 41c7ab4 | 2017-06-07 08:54:43 +0000 | [diff] [blame] | 879 | return {InX::Common, C->Offset, Loc}; |
Rafael Espindola | 72dc195 | 2017-03-17 13:05:04 +0000 | [diff] [blame] | 880 | } |
Eugene Leviant | f6aeed3 | 2016-12-22 13:13:12 +0000 | [diff] [blame] | 881 | error(Loc + ": symbol not found: " + S); |
George Rimar | 884e786 | 2016-09-08 08:19:13 +0000 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
Rafael Espindola | 244ef98 | 2017-07-26 18:42:48 +0000 | [diff] [blame] | 885 | bool LinkerScript::isDefined(StringRef S) { return Symtab->find(S) != nullptr; } |
George Rimar | f34f45f | 2016-09-23 13:17:23 +0000 | [diff] [blame] | 886 | |
Andrew Ng | 6e9f98c | 2017-06-19 15:28:58 +0000 | [diff] [blame] | 887 | static const size_t NoPhdr = -1; |
| 888 | |
Rafael Espindola | 2c923c2 | 2017-05-10 14:28:31 +0000 | [diff] [blame] | 889 | // Returns indices of ELF headers containing specific section. Each index is a |
| 890 | // zero based number of ELF header listed within PHDRS {} script block. |
Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 891 | std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) { |
Rafael Espindola | 9f9e99b | 2017-07-05 22:30:04 +0000 | [diff] [blame] | 892 | std::vector<size_t> Ret; |
| 893 | for (StringRef PhdrName : Cmd->Phdrs) { |
| 894 | size_t Index = getPhdrIndex(Cmd->Location, PhdrName); |
| 895 | if (Index != NoPhdr) |
| 896 | Ret.push_back(Index); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 897 | } |
Rafael Espindola | 9f9e99b | 2017-07-05 22:30:04 +0000 | [diff] [blame] | 898 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Andrew Ng | 6e9f98c | 2017-06-19 15:28:58 +0000 | [diff] [blame] | 901 | // Returns the index of the segment named PhdrName if found otherwise |
| 902 | // NoPhdr. When not found, if PhdrName is not the special case value 'NONE' |
| 903 | // (which can be used to explicitly specify that a section isn't assigned to a |
| 904 | // segment) then error. |
Rui Ueyama | b8dd23f | 2017-03-21 23:02:51 +0000 | [diff] [blame] | 905 | size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) { |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 906 | size_t I = 0; |
| 907 | for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
| 908 | if (Cmd.Name == PhdrName) |
| 909 | return I; |
| 910 | ++I; |
| 911 | } |
Andrew Ng | 6e9f98c | 2017-06-19 15:28:58 +0000 | [diff] [blame] | 912 | if (PhdrName != "NONE") |
| 913 | error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS"); |
| 914 | return NoPhdr; |
Rui Ueyama | 29c5a2a | 2016-07-26 00:27:36 +0000 | [diff] [blame] | 915 | } |