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