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