| File: | build/source/lld/MachO/SyntheticSections.cpp |
| Warning: | line 1887, column 42 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===- SyntheticSections.cpp ---------------------------------------------===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | ||||
| 9 | #include "SyntheticSections.h" | |||
| 10 | #include "ConcatOutputSection.h" | |||
| 11 | #include "Config.h" | |||
| 12 | #include "ExportTrie.h" | |||
| 13 | #include "InputFiles.h" | |||
| 14 | #include "MachOStructs.h" | |||
| 15 | #include "OutputSegment.h" | |||
| 16 | #include "SymbolTable.h" | |||
| 17 | #include "Symbols.h" | |||
| 18 | ||||
| 19 | #include "lld/Common/CommonLinkerContext.h" | |||
| 20 | #include "llvm/ADT/STLExtras.h" | |||
| 21 | #include "llvm/Config/llvm-config.h" | |||
| 22 | #include "llvm/Support/EndianStream.h" | |||
| 23 | #include "llvm/Support/FileSystem.h" | |||
| 24 | #include "llvm/Support/LEB128.h" | |||
| 25 | #include "llvm/Support/Parallel.h" | |||
| 26 | #include "llvm/Support/Path.h" | |||
| 27 | #include "llvm/Support/xxhash.h" | |||
| 28 | ||||
| 29 | #if defined(__APPLE__) | |||
| 30 | #include <sys/mman.h> | |||
| 31 | ||||
| 32 | #define COMMON_DIGEST_FOR_OPENSSL | |||
| 33 | #include <CommonCrypto/CommonDigest.h> | |||
| 34 | #else | |||
| 35 | #include "llvm/Support/SHA256.h" | |||
| 36 | #endif | |||
| 37 | ||||
| 38 | #ifdef LLVM_HAVE_LIBXAR | |||
| 39 | #include <fcntl.h> | |||
| 40 | extern "C" { | |||
| 41 | #include <xar/xar.h> | |||
| 42 | } | |||
| 43 | #endif | |||
| 44 | ||||
| 45 | using namespace llvm; | |||
| 46 | using namespace llvm::MachO; | |||
| 47 | using namespace llvm::support; | |||
| 48 | using namespace llvm::support::endian; | |||
| 49 | using namespace lld; | |||
| 50 | using namespace lld::macho; | |||
| 51 | ||||
| 52 | // Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`. | |||
| 53 | static void sha256(const uint8_t *data, size_t len, uint8_t *output) { | |||
| 54 | #if defined(__APPLE__) | |||
| 55 | // FIXME: Make LLVM's SHA256 faster and use it unconditionally. See PR56121 | |||
| 56 | // for some notes on this. | |||
| 57 | CC_SHA256(data, len, output); | |||
| 58 | #else | |||
| 59 | ArrayRef<uint8_t> block(data, len); | |||
| 60 | std::array<uint8_t, 32> hash = SHA256::hash(block); | |||
| 61 | static_assert(hash.size() == CodeSignatureSection::hashSize); | |||
| 62 | memcpy(output, hash.data(), hash.size()); | |||
| 63 | #endif | |||
| 64 | } | |||
| 65 | ||||
| 66 | InStruct macho::in; | |||
| 67 | std::vector<SyntheticSection *> macho::syntheticSections; | |||
| 68 | ||||
| 69 | SyntheticSection::SyntheticSection(const char *segname, const char *name) | |||
| 70 | : OutputSection(SyntheticKind, name) { | |||
| 71 | std::tie(this->segname, this->name) = maybeRenameSection({segname, name}); | |||
| 72 | isec = makeSyntheticInputSection(segname, name); | |||
| 73 | isec->parent = this; | |||
| 74 | syntheticSections.push_back(this); | |||
| 75 | } | |||
| 76 | ||||
| 77 | // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts | |||
| 78 | // from the beginning of the file (i.e. the header). | |||
| 79 | MachHeaderSection::MachHeaderSection() | |||
| 80 | : SyntheticSection(segment_names::text, section_names::header) { | |||
| 81 | // XXX: This is a hack. (See D97007) | |||
| 82 | // Setting the index to 1 to pretend that this section is the text | |||
| 83 | // section. | |||
| 84 | index = 1; | |||
| 85 | isec->isFinal = true; | |||
| 86 | } | |||
| 87 | ||||
| 88 | void MachHeaderSection::addLoadCommand(LoadCommand *lc) { | |||
| 89 | loadCommands.push_back(lc); | |||
| 90 | sizeOfCmds += lc->getSize(); | |||
| 91 | } | |||
| 92 | ||||
| 93 | uint64_t MachHeaderSection::getSize() const { | |||
| 94 | uint64_t size = target->headerSize + sizeOfCmds + config->headerPad; | |||
| 95 | // If we are emitting an encryptable binary, our load commands must have a | |||
| 96 | // separate (non-encrypted) page to themselves. | |||
| 97 | if (config->emitEncryptionInfo) | |||
| 98 | size = alignToPowerOf2(size, target->getPageSize()); | |||
| 99 | return size; | |||
| 100 | } | |||
| 101 | ||||
| 102 | static uint32_t cpuSubtype() { | |||
| 103 | uint32_t subtype = target->cpuSubtype; | |||
| 104 | ||||
| 105 | if (config->outputType == MH_EXECUTE && !config->staticLink && | |||
| 106 | target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL && | |||
| 107 | config->platform() == PLATFORM_MACOS && | |||
| 108 | config->platformInfo.target.MinDeployment >= VersionTuple(10, 5)) | |||
| 109 | subtype |= CPU_SUBTYPE_LIB64; | |||
| 110 | ||||
| 111 | return subtype; | |||
| 112 | } | |||
| 113 | ||||
| 114 | static bool hasWeakBinding() { | |||
| 115 | return config->emitChainedFixups ? in.chainedFixups->hasWeakBinding() | |||
| 116 | : in.weakBinding->hasEntry(); | |||
| 117 | } | |||
| 118 | ||||
| 119 | static bool hasNonWeakDefinition() { | |||
| 120 | return config->emitChainedFixups ? in.chainedFixups->hasNonWeakDefinition() | |||
| 121 | : in.weakBinding->hasNonWeakDefinition(); | |||
| 122 | } | |||
| 123 | ||||
| 124 | void MachHeaderSection::writeTo(uint8_t *buf) const { | |||
| 125 | auto *hdr = reinterpret_cast<mach_header *>(buf); | |||
| 126 | hdr->magic = target->magic; | |||
| 127 | hdr->cputype = target->cpuType; | |||
| 128 | hdr->cpusubtype = cpuSubtype(); | |||
| 129 | hdr->filetype = config->outputType; | |||
| 130 | hdr->ncmds = loadCommands.size(); | |||
| 131 | hdr->sizeofcmds = sizeOfCmds; | |||
| 132 | hdr->flags = MH_DYLDLINK; | |||
| 133 | ||||
| 134 | if (config->namespaceKind == NamespaceKind::twolevel) | |||
| 135 | hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL; | |||
| 136 | ||||
| 137 | if (config->outputType == MH_DYLIB && !config->hasReexports) | |||
| 138 | hdr->flags |= MH_NO_REEXPORTED_DYLIBS; | |||
| 139 | ||||
| 140 | if (config->markDeadStrippableDylib) | |||
| 141 | hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB; | |||
| 142 | ||||
| 143 | if (config->outputType == MH_EXECUTE && config->isPic) | |||
| 144 | hdr->flags |= MH_PIE; | |||
| 145 | ||||
| 146 | if (config->outputType == MH_DYLIB && config->applicationExtension) | |||
| 147 | hdr->flags |= MH_APP_EXTENSION_SAFE; | |||
| 148 | ||||
| 149 | if (in.exports->hasWeakSymbol || hasNonWeakDefinition()) | |||
| 150 | hdr->flags |= MH_WEAK_DEFINES; | |||
| 151 | ||||
| 152 | if (in.exports->hasWeakSymbol || hasWeakBinding()) | |||
| 153 | hdr->flags |= MH_BINDS_TO_WEAK; | |||
| 154 | ||||
| 155 | for (const OutputSegment *seg : outputSegments) { | |||
| 156 | for (const OutputSection *osec : seg->getSections()) { | |||
| 157 | if (isThreadLocalVariables(osec->flags)) { | |||
| 158 | hdr->flags |= MH_HAS_TLV_DESCRIPTORS; | |||
| 159 | break; | |||
| 160 | } | |||
| 161 | } | |||
| 162 | } | |||
| 163 | ||||
| 164 | uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize; | |||
| 165 | for (const LoadCommand *lc : loadCommands) { | |||
| 166 | lc->writeTo(p); | |||
| 167 | p += lc->getSize(); | |||
| 168 | } | |||
| 169 | } | |||
| 170 | ||||
| 171 | PageZeroSection::PageZeroSection() | |||
| 172 | : SyntheticSection(segment_names::pageZero, section_names::pageZero) {} | |||
| 173 | ||||
| 174 | RebaseSection::RebaseSection() | |||
| 175 | : LinkEditSection(segment_names::linkEdit, section_names::rebase) {} | |||
| 176 | ||||
| 177 | namespace { | |||
| 178 | struct RebaseState { | |||
| 179 | uint64_t sequenceLength; | |||
| 180 | uint64_t skipLength; | |||
| 181 | }; | |||
| 182 | } // namespace | |||
| 183 | ||||
| 184 | static void emitIncrement(uint64_t incr, raw_svector_ostream &os) { | |||
| 185 | assert(incr != 0)(static_cast <bool> (incr != 0) ? void (0) : __assert_fail ("incr != 0", "lld/MachO/SyntheticSections.cpp", 185, __extension__ __PRETTY_FUNCTION__)); | |||
| 186 | ||||
| 187 | if ((incr >> target->p2WordSize) <= REBASE_IMMEDIATE_MASK && | |||
| 188 | (incr % target->wordSize) == 0) { | |||
| 189 | os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_IMM_SCALED | | |||
| 190 | (incr >> target->p2WordSize)); | |||
| 191 | } else { | |||
| 192 | os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB); | |||
| 193 | encodeULEB128(incr, os); | |||
| 194 | } | |||
| 195 | } | |||
| 196 | ||||
| 197 | static void flushRebase(const RebaseState &state, raw_svector_ostream &os) { | |||
| 198 | assert(state.sequenceLength > 0)(static_cast <bool> (state.sequenceLength > 0) ? void (0) : __assert_fail ("state.sequenceLength > 0", "lld/MachO/SyntheticSections.cpp" , 198, __extension__ __PRETTY_FUNCTION__)); | |||
| 199 | ||||
| 200 | if (state.skipLength == target->wordSize) { | |||
| 201 | if (state.sequenceLength <= REBASE_IMMEDIATE_MASK) { | |||
| 202 | os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES | | |||
| 203 | state.sequenceLength); | |||
| 204 | } else { | |||
| 205 | os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES); | |||
| 206 | encodeULEB128(state.sequenceLength, os); | |||
| 207 | } | |||
| 208 | } else if (state.sequenceLength == 1) { | |||
| 209 | os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB); | |||
| 210 | encodeULEB128(state.skipLength - target->wordSize, os); | |||
| 211 | } else { | |||
| 212 | os << static_cast<uint8_t>( | |||
| 213 | REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB); | |||
| 214 | encodeULEB128(state.sequenceLength, os); | |||
| 215 | encodeULEB128(state.skipLength - target->wordSize, os); | |||
| 216 | } | |||
| 217 | } | |||
| 218 | ||||
| 219 | // Rebases are communicated to dyld using a bytecode, whose opcodes cause the | |||
| 220 | // memory location at a specific address to be rebased and/or the address to be | |||
| 221 | // incremented. | |||
| 222 | // | |||
| 223 | // Opcode REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB is the most generic | |||
| 224 | // one, encoding a series of evenly spaced addresses. This algorithm works by | |||
| 225 | // splitting up the sorted list of addresses into such chunks. If the locations | |||
| 226 | // are consecutive or the sequence consists of a single location, flushRebase | |||
| 227 | // will use a smaller, more specialized encoding. | |||
| 228 | static void encodeRebases(const OutputSegment *seg, | |||
| 229 | MutableArrayRef<Location> locations, | |||
| 230 | raw_svector_ostream &os) { | |||
| 231 | // dyld operates on segments. Translate section offsets into segment offsets. | |||
| 232 | for (Location &loc : locations) | |||
| 233 | loc.offset = | |||
| 234 | loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset); | |||
| 235 | // The algorithm assumes that locations are unique. | |||
| 236 | Location *end = | |||
| 237 | llvm::unique(locations, [](const Location &a, const Location &b) { | |||
| 238 | return a.offset == b.offset; | |||
| 239 | }); | |||
| 240 | size_t count = end - locations.begin(); | |||
| 241 | ||||
| 242 | os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | | |||
| 243 | seg->index); | |||
| 244 | assert(!locations.empty())(static_cast <bool> (!locations.empty()) ? void (0) : __assert_fail ("!locations.empty()", "lld/MachO/SyntheticSections.cpp", 244 , __extension__ __PRETTY_FUNCTION__)); | |||
| 245 | uint64_t offset = locations[0].offset; | |||
| 246 | encodeULEB128(offset, os); | |||
| 247 | ||||
| 248 | RebaseState state{1, target->wordSize}; | |||
| 249 | ||||
| 250 | for (size_t i = 1; i < count; ++i) { | |||
| 251 | offset = locations[i].offset; | |||
| 252 | ||||
| 253 | uint64_t skip = offset - locations[i - 1].offset; | |||
| 254 | assert(skip != 0 && "duplicate locations should have been weeded out")(static_cast <bool> (skip != 0 && "duplicate locations should have been weeded out" ) ? void (0) : __assert_fail ("skip != 0 && \"duplicate locations should have been weeded out\"" , "lld/MachO/SyntheticSections.cpp", 254, __extension__ __PRETTY_FUNCTION__ )); | |||
| 255 | ||||
| 256 | if (skip == state.skipLength) { | |||
| 257 | ++state.sequenceLength; | |||
| 258 | } else if (state.sequenceLength == 1) { | |||
| 259 | ++state.sequenceLength; | |||
| 260 | state.skipLength = skip; | |||
| 261 | } else if (skip < state.skipLength) { | |||
| 262 | // The address is lower than what the rebase pointer would be if the last | |||
| 263 | // location would be part of a sequence. We start a new sequence from the | |||
| 264 | // previous location. | |||
| 265 | --state.sequenceLength; | |||
| 266 | flushRebase(state, os); | |||
| 267 | ||||
| 268 | state.sequenceLength = 2; | |||
| 269 | state.skipLength = skip; | |||
| 270 | } else { | |||
| 271 | // The address is at some positive offset from the rebase pointer. We | |||
| 272 | // start a new sequence which begins with the current location. | |||
| 273 | flushRebase(state, os); | |||
| 274 | emitIncrement(skip - state.skipLength, os); | |||
| 275 | state.sequenceLength = 1; | |||
| 276 | state.skipLength = target->wordSize; | |||
| 277 | } | |||
| 278 | } | |||
| 279 | flushRebase(state, os); | |||
| 280 | } | |||
| 281 | ||||
| 282 | void RebaseSection::finalizeContents() { | |||
| 283 | if (locations.empty()) | |||
| 284 | return; | |||
| 285 | ||||
| 286 | raw_svector_ostream os{contents}; | |||
| 287 | os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER); | |||
| 288 | ||||
| 289 | llvm::sort(locations, [](const Location &a, const Location &b) { | |||
| 290 | return a.isec->getVA(a.offset) < b.isec->getVA(b.offset); | |||
| 291 | }); | |||
| 292 | ||||
| 293 | for (size_t i = 0, count = locations.size(); i < count;) { | |||
| 294 | const OutputSegment *seg = locations[i].isec->parent->parent; | |||
| 295 | size_t j = i + 1; | |||
| 296 | while (j < count && locations[j].isec->parent->parent == seg) | |||
| 297 | ++j; | |||
| 298 | encodeRebases(seg, {locations.data() + i, locations.data() + j}, os); | |||
| 299 | i = j; | |||
| 300 | } | |||
| 301 | os << static_cast<uint8_t>(REBASE_OPCODE_DONE); | |||
| 302 | } | |||
| 303 | ||||
| 304 | void RebaseSection::writeTo(uint8_t *buf) const { | |||
| 305 | memcpy(buf, contents.data(), contents.size()); | |||
| 306 | } | |||
| 307 | ||||
| 308 | NonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname, | |||
| 309 | const char *name) | |||
| 310 | : SyntheticSection(segname, name) { | |||
| 311 | align = target->wordSize; | |||
| 312 | } | |||
| 313 | ||||
| 314 | void macho::addNonLazyBindingEntries(const Symbol *sym, | |||
| 315 | const InputSection *isec, uint64_t offset, | |||
| 316 | int64_t addend) { | |||
| 317 | if (config->emitChainedFixups) { | |||
| 318 | if (needsBinding(sym)) | |||
| 319 | in.chainedFixups->addBinding(sym, isec, offset, addend); | |||
| 320 | else if (isa<Defined>(sym)) | |||
| 321 | in.chainedFixups->addRebase(isec, offset); | |||
| 322 | else | |||
| 323 | llvm_unreachable("cannot bind to an undefined symbol")::llvm::llvm_unreachable_internal("cannot bind to an undefined symbol" , "lld/MachO/SyntheticSections.cpp", 323); | |||
| 324 | return; | |||
| 325 | } | |||
| 326 | ||||
| 327 | if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
| 328 | in.binding->addEntry(dysym, isec, offset, addend); | |||
| 329 | if (dysym->isWeakDef()) | |||
| 330 | in.weakBinding->addEntry(sym, isec, offset, addend); | |||
| 331 | } else if (const auto *defined = dyn_cast<Defined>(sym)) { | |||
| 332 | in.rebase->addEntry(isec, offset); | |||
| 333 | if (defined->isExternalWeakDef()) | |||
| 334 | in.weakBinding->addEntry(sym, isec, offset, addend); | |||
| 335 | else if (defined->interposable) | |||
| 336 | in.binding->addEntry(sym, isec, offset, addend); | |||
| 337 | } else { | |||
| 338 | // Undefined symbols are filtered out in scanRelocations(); we should never | |||
| 339 | // get here | |||
| 340 | llvm_unreachable("cannot bind to an undefined symbol")::llvm::llvm_unreachable_internal("cannot bind to an undefined symbol" , "lld/MachO/SyntheticSections.cpp", 340); | |||
| 341 | } | |||
| 342 | } | |||
| 343 | ||||
| 344 | void NonLazyPointerSectionBase::addEntry(Symbol *sym) { | |||
| 345 | if (entries.insert(sym)) { | |||
| 346 | assert(!sym->isInGot())(static_cast <bool> (!sym->isInGot()) ? void (0) : __assert_fail ("!sym->isInGot()", "lld/MachO/SyntheticSections.cpp", 346 , __extension__ __PRETTY_FUNCTION__)); | |||
| 347 | sym->gotIndex = entries.size() - 1; | |||
| 348 | ||||
| 349 | addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize); | |||
| 350 | } | |||
| 351 | } | |||
| 352 | ||||
| 353 | void macho::writeChainedRebase(uint8_t *buf, uint64_t targetVA) { | |||
| 354 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 354, __extension__ __PRETTY_FUNCTION__)); | |||
| 355 | assert(target->wordSize == 8 && "Only 64-bit platforms are supported")(static_cast <bool> (target->wordSize == 8 && "Only 64-bit platforms are supported") ? void (0) : __assert_fail ("target->wordSize == 8 && \"Only 64-bit platforms are supported\"" , "lld/MachO/SyntheticSections.cpp", 355, __extension__ __PRETTY_FUNCTION__ )); | |||
| 356 | auto *rebase = reinterpret_cast<dyld_chained_ptr_64_rebase *>(buf); | |||
| 357 | rebase->target = targetVA & 0xf'ffff'ffff; | |||
| 358 | rebase->high8 = (targetVA >> 56); | |||
| 359 | rebase->reserved = 0; | |||
| 360 | rebase->next = 0; | |||
| 361 | rebase->bind = 0; | |||
| 362 | ||||
| 363 | // The fixup format places a 64 GiB limit on the output's size. | |||
| 364 | // Should we handle this gracefully? | |||
| 365 | uint64_t encodedVA = rebase->target | ((uint64_t)rebase->high8 << 56); | |||
| 366 | if (encodedVA != targetVA) | |||
| 367 | error("rebase target address 0x" + Twine::utohexstr(targetVA) + | |||
| 368 | " does not fit into chained fixup. Re-link with -no_fixup_chains"); | |||
| 369 | } | |||
| 370 | ||||
| 371 | static void writeChainedBind(uint8_t *buf, const Symbol *sym, int64_t addend) { | |||
| 372 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 372, __extension__ __PRETTY_FUNCTION__)); | |||
| 373 | assert(target->wordSize == 8 && "Only 64-bit platforms are supported")(static_cast <bool> (target->wordSize == 8 && "Only 64-bit platforms are supported") ? void (0) : __assert_fail ("target->wordSize == 8 && \"Only 64-bit platforms are supported\"" , "lld/MachO/SyntheticSections.cpp", 373, __extension__ __PRETTY_FUNCTION__ )); | |||
| 374 | auto *bind = reinterpret_cast<dyld_chained_ptr_64_bind *>(buf); | |||
| 375 | auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend); | |||
| 376 | bind->ordinal = ordinal; | |||
| 377 | bind->addend = inlineAddend; | |||
| 378 | bind->reserved = 0; | |||
| 379 | bind->next = 0; | |||
| 380 | bind->bind = 1; | |||
| 381 | } | |||
| 382 | ||||
| 383 | void macho::writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend) { | |||
| 384 | if (needsBinding(sym)) | |||
| 385 | writeChainedBind(buf, sym, addend); | |||
| 386 | else | |||
| 387 | writeChainedRebase(buf, sym->getVA() + addend); | |||
| 388 | } | |||
| 389 | ||||
| 390 | void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const { | |||
| 391 | if (config->emitChainedFixups) { | |||
| 392 | for (const auto &[i, entry] : llvm::enumerate(entries)) | |||
| 393 | writeChainedFixup(&buf[i * target->wordSize], entry, 0); | |||
| 394 | } else { | |||
| 395 | for (const auto &[i, entry] : llvm::enumerate(entries)) | |||
| 396 | if (auto *defined = dyn_cast<Defined>(entry)) | |||
| 397 | write64le(&buf[i * target->wordSize], defined->getVA()); | |||
| 398 | } | |||
| 399 | } | |||
| 400 | ||||
| 401 | GotSection::GotSection() | |||
| 402 | : NonLazyPointerSectionBase(segment_names::data, section_names::got) { | |||
| 403 | flags = S_NON_LAZY_SYMBOL_POINTERS; | |||
| 404 | } | |||
| 405 | ||||
| 406 | TlvPointerSection::TlvPointerSection() | |||
| 407 | : NonLazyPointerSectionBase(segment_names::data, | |||
| 408 | section_names::threadPtrs) { | |||
| 409 | flags = S_THREAD_LOCAL_VARIABLE_POINTERS; | |||
| 410 | } | |||
| 411 | ||||
| 412 | BindingSection::BindingSection() | |||
| 413 | : LinkEditSection(segment_names::linkEdit, section_names::binding) {} | |||
| 414 | ||||
| 415 | namespace { | |||
| 416 | struct Binding { | |||
| 417 | OutputSegment *segment = nullptr; | |||
| 418 | uint64_t offset = 0; | |||
| 419 | int64_t addend = 0; | |||
| 420 | }; | |||
| 421 | struct BindIR { | |||
| 422 | // Default value of 0xF0 is not valid opcode and should make the program | |||
| 423 | // scream instead of accidentally writing "valid" values. | |||
| 424 | uint8_t opcode = 0xF0; | |||
| 425 | uint64_t data = 0; | |||
| 426 | uint64_t consecutiveCount = 0; | |||
| 427 | }; | |||
| 428 | } // namespace | |||
| 429 | ||||
| 430 | // Encode a sequence of opcodes that tell dyld to write the address of symbol + | |||
| 431 | // addend at osec->addr + outSecOff. | |||
| 432 | // | |||
| 433 | // The bind opcode "interpreter" remembers the values of each binding field, so | |||
| 434 | // we only need to encode the differences between bindings. Hence the use of | |||
| 435 | // lastBinding. | |||
| 436 | static void encodeBinding(const OutputSection *osec, uint64_t outSecOff, | |||
| 437 | int64_t addend, Binding &lastBinding, | |||
| 438 | std::vector<BindIR> &opcodes) { | |||
| 439 | OutputSegment *seg = osec->parent; | |||
| 440 | uint64_t offset = osec->getSegmentOffset() + outSecOff; | |||
| 441 | if (lastBinding.segment != seg) { | |||
| 442 | opcodes.push_back( | |||
| 443 | {static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | | |||
| 444 | seg->index), | |||
| 445 | offset}); | |||
| 446 | lastBinding.segment = seg; | |||
| 447 | lastBinding.offset = offset; | |||
| 448 | } else if (lastBinding.offset != offset) { | |||
| 449 | opcodes.push_back({BIND_OPCODE_ADD_ADDR_ULEB, offset - lastBinding.offset}); | |||
| 450 | lastBinding.offset = offset; | |||
| 451 | } | |||
| 452 | ||||
| 453 | if (lastBinding.addend != addend) { | |||
| 454 | opcodes.push_back( | |||
| 455 | {BIND_OPCODE_SET_ADDEND_SLEB, static_cast<uint64_t>(addend)}); | |||
| 456 | lastBinding.addend = addend; | |||
| 457 | } | |||
| 458 | ||||
| 459 | opcodes.push_back({BIND_OPCODE_DO_BIND, 0}); | |||
| 460 | // DO_BIND causes dyld to both perform the binding and increment the offset | |||
| 461 | lastBinding.offset += target->wordSize; | |||
| 462 | } | |||
| 463 | ||||
| 464 | static void optimizeOpcodes(std::vector<BindIR> &opcodes) { | |||
| 465 | // Pass 1: Combine bind/add pairs | |||
| 466 | size_t i; | |||
| 467 | int pWrite = 0; | |||
| 468 | for (i = 1; i < opcodes.size(); ++i, ++pWrite) { | |||
| 469 | if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) && | |||
| 470 | (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) { | |||
| 471 | opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB; | |||
| 472 | opcodes[pWrite].data = opcodes[i].data; | |||
| 473 | ++i; | |||
| 474 | } else { | |||
| 475 | opcodes[pWrite] = opcodes[i - 1]; | |||
| 476 | } | |||
| 477 | } | |||
| 478 | if (i == opcodes.size()) | |||
| 479 | opcodes[pWrite] = opcodes[i - 1]; | |||
| 480 | opcodes.resize(pWrite + 1); | |||
| 481 | ||||
| 482 | // Pass 2: Compress two or more bind_add opcodes | |||
| 483 | pWrite = 0; | |||
| 484 | for (i = 1; i < opcodes.size(); ++i, ++pWrite) { | |||
| 485 | if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
| 486 | (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
| 487 | (opcodes[i].data == opcodes[i - 1].data)) { | |||
| 488 | opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB; | |||
| 489 | opcodes[pWrite].consecutiveCount = 2; | |||
| 490 | opcodes[pWrite].data = opcodes[i].data; | |||
| 491 | ++i; | |||
| 492 | while (i < opcodes.size() && | |||
| 493 | (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
| 494 | (opcodes[i].data == opcodes[i - 1].data)) { | |||
| 495 | opcodes[pWrite].consecutiveCount++; | |||
| 496 | ++i; | |||
| 497 | } | |||
| 498 | } else { | |||
| 499 | opcodes[pWrite] = opcodes[i - 1]; | |||
| 500 | } | |||
| 501 | } | |||
| 502 | if (i == opcodes.size()) | |||
| 503 | opcodes[pWrite] = opcodes[i - 1]; | |||
| 504 | opcodes.resize(pWrite + 1); | |||
| 505 | ||||
| 506 | // Pass 3: Use immediate encodings | |||
| 507 | // Every binding is the size of one pointer. If the next binding is a | |||
| 508 | // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the | |||
| 509 | // opcode can be scaled by wordSize into a single byte and dyld will | |||
| 510 | // expand it to the correct address. | |||
| 511 | for (auto &p : opcodes) { | |||
| 512 | // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK, | |||
| 513 | // but ld64 currently does this. This could be a potential bug, but | |||
| 514 | // for now, perform the same behavior to prevent mysterious bugs. | |||
| 515 | if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
| 516 | ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) && | |||
| 517 | ((p.data % target->wordSize) == 0)) { | |||
| 518 | p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED; | |||
| 519 | p.data /= target->wordSize; | |||
| 520 | } | |||
| 521 | } | |||
| 522 | } | |||
| 523 | ||||
| 524 | static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) { | |||
| 525 | uint8_t opcode = op.opcode & BIND_OPCODE_MASK; | |||
| 526 | switch (opcode) { | |||
| 527 | case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: | |||
| 528 | case BIND_OPCODE_ADD_ADDR_ULEB: | |||
| 529 | case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: | |||
| 530 | os << op.opcode; | |||
| 531 | encodeULEB128(op.data, os); | |||
| 532 | break; | |||
| 533 | case BIND_OPCODE_SET_ADDEND_SLEB: | |||
| 534 | os << op.opcode; | |||
| 535 | encodeSLEB128(static_cast<int64_t>(op.data), os); | |||
| 536 | break; | |||
| 537 | case BIND_OPCODE_DO_BIND: | |||
| 538 | os << op.opcode; | |||
| 539 | break; | |||
| 540 | case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: | |||
| 541 | os << op.opcode; | |||
| 542 | encodeULEB128(op.consecutiveCount, os); | |||
| 543 | encodeULEB128(op.data, os); | |||
| 544 | break; | |||
| 545 | case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: | |||
| 546 | os << static_cast<uint8_t>(op.opcode | op.data); | |||
| 547 | break; | |||
| 548 | default: | |||
| 549 | llvm_unreachable("cannot bind to an unrecognized symbol")::llvm::llvm_unreachable_internal("cannot bind to an unrecognized symbol" , "lld/MachO/SyntheticSections.cpp", 549); | |||
| 550 | } | |||
| 551 | } | |||
| 552 | ||||
| 553 | // Non-weak bindings need to have their dylib ordinal encoded as well. | |||
| 554 | static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) { | |||
| 555 | if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup()) | |||
| 556 | return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP); | |||
| 557 | assert(dysym.getFile()->isReferenced())(static_cast <bool> (dysym.getFile()->isReferenced() ) ? void (0) : __assert_fail ("dysym.getFile()->isReferenced()" , "lld/MachO/SyntheticSections.cpp", 557, __extension__ __PRETTY_FUNCTION__ )); | |||
| 558 | return dysym.getFile()->ordinal; | |||
| 559 | } | |||
| 560 | ||||
| 561 | static int16_t ordinalForSymbol(const Symbol &sym) { | |||
| 562 | if (const auto *dysym = dyn_cast<DylibSymbol>(&sym)) | |||
| 563 | return ordinalForDylibSymbol(*dysym); | |||
| 564 | assert(cast<Defined>(&sym)->interposable)(static_cast <bool> (cast<Defined>(&sym)-> interposable) ? void (0) : __assert_fail ("cast<Defined>(&sym)->interposable" , "lld/MachO/SyntheticSections.cpp", 564, __extension__ __PRETTY_FUNCTION__ )); | |||
| 565 | return BIND_SPECIAL_DYLIB_FLAT_LOOKUP; | |||
| 566 | } | |||
| 567 | ||||
| 568 | static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) { | |||
| 569 | if (ordinal <= 0) { | |||
| 570 | os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | | |||
| 571 | (ordinal & BIND_IMMEDIATE_MASK)); | |||
| 572 | } else if (ordinal <= BIND_IMMEDIATE_MASK) { | |||
| 573 | os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal); | |||
| 574 | } else { | |||
| 575 | os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); | |||
| 576 | encodeULEB128(ordinal, os); | |||
| 577 | } | |||
| 578 | } | |||
| 579 | ||||
| 580 | static void encodeWeakOverride(const Defined *defined, | |||
| 581 | raw_svector_ostream &os) { | |||
| 582 | os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | | |||
| 583 | BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) | |||
| 584 | << defined->getName() << '\0'; | |||
| 585 | } | |||
| 586 | ||||
| 587 | // Organize the bindings so we can encoded them with fewer opcodes. | |||
| 588 | // | |||
| 589 | // First, all bindings for a given symbol should be grouped together. | |||
| 590 | // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it | |||
| 591 | // has an associated symbol string), so we only want to emit it once per symbol. | |||
| 592 | // | |||
| 593 | // Within each group, we sort the bindings by address. Since bindings are | |||
| 594 | // delta-encoded, sorting them allows for a more compact result. Note that | |||
| 595 | // sorting by address alone ensures that bindings for the same segment / section | |||
| 596 | // are located together, minimizing the number of times we have to emit | |||
| 597 | // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB. | |||
| 598 | // | |||
| 599 | // Finally, we sort the symbols by the address of their first binding, again | |||
| 600 | // to facilitate the delta-encoding process. | |||
| 601 | template <class Sym> | |||
| 602 | std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> | |||
| 603 | sortBindings(const BindingsMap<const Sym *> &bindingsMap) { | |||
| 604 | std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec( | |||
| 605 | bindingsMap.begin(), bindingsMap.end()); | |||
| 606 | for (auto &p : bindingsVec) { | |||
| 607 | std::vector<BindingEntry> &bindings = p.second; | |||
| 608 | llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) { | |||
| 609 | return a.target.getVA() < b.target.getVA(); | |||
| 610 | }); | |||
| 611 | } | |||
| 612 | llvm::sort(bindingsVec, [](const auto &a, const auto &b) { | |||
| 613 | return a.second[0].target.getVA() < b.second[0].target.getVA(); | |||
| 614 | }); | |||
| 615 | return bindingsVec; | |||
| 616 | } | |||
| 617 | ||||
| 618 | // Emit bind opcodes, which are a stream of byte-sized opcodes that dyld | |||
| 619 | // interprets to update a record with the following fields: | |||
| 620 | // * segment index (of the segment to write the symbol addresses to, typically | |||
| 621 | // the __DATA_CONST segment which contains the GOT) | |||
| 622 | // * offset within the segment, indicating the next location to write a binding | |||
| 623 | // * symbol type | |||
| 624 | // * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command) | |||
| 625 | // * symbol name | |||
| 626 | // * addend | |||
| 627 | // When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind | |||
| 628 | // a symbol in the GOT, and increments the segment offset to point to the next | |||
| 629 | // entry. It does *not* clear the record state after doing the bind, so | |||
| 630 | // subsequent opcodes only need to encode the differences between bindings. | |||
| 631 | void BindingSection::finalizeContents() { | |||
| 632 | raw_svector_ostream os{contents}; | |||
| 633 | Binding lastBinding; | |||
| 634 | int16_t lastOrdinal = 0; | |||
| 635 | ||||
| 636 | for (auto &p : sortBindings(bindingsMap)) { | |||
| 637 | const Symbol *sym = p.first; | |||
| 638 | std::vector<BindingEntry> &bindings = p.second; | |||
| 639 | uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM; | |||
| 640 | if (sym->isWeakRef()) | |||
| 641 | flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT; | |||
| 642 | os << flags << sym->getName() << '\0' | |||
| 643 | << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER); | |||
| 644 | int16_t ordinal = ordinalForSymbol(*sym); | |||
| 645 | if (ordinal != lastOrdinal) { | |||
| 646 | encodeDylibOrdinal(ordinal, os); | |||
| 647 | lastOrdinal = ordinal; | |||
| 648 | } | |||
| 649 | std::vector<BindIR> opcodes; | |||
| 650 | for (const BindingEntry &b : bindings) | |||
| 651 | encodeBinding(b.target.isec->parent, | |||
| 652 | b.target.isec->getOffset(b.target.offset), b.addend, | |||
| 653 | lastBinding, opcodes); | |||
| 654 | if (config->optimize > 1) | |||
| 655 | optimizeOpcodes(opcodes); | |||
| 656 | for (const auto &op : opcodes) | |||
| 657 | flushOpcodes(op, os); | |||
| 658 | } | |||
| 659 | if (!bindingsMap.empty()) | |||
| 660 | os << static_cast<uint8_t>(BIND_OPCODE_DONE); | |||
| 661 | } | |||
| 662 | ||||
| 663 | void BindingSection::writeTo(uint8_t *buf) const { | |||
| 664 | memcpy(buf, contents.data(), contents.size()); | |||
| 665 | } | |||
| 666 | ||||
| 667 | WeakBindingSection::WeakBindingSection() | |||
| 668 | : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {} | |||
| 669 | ||||
| 670 | void WeakBindingSection::finalizeContents() { | |||
| 671 | raw_svector_ostream os{contents}; | |||
| 672 | Binding lastBinding; | |||
| 673 | ||||
| 674 | for (const Defined *defined : definitions) | |||
| 675 | encodeWeakOverride(defined, os); | |||
| 676 | ||||
| 677 | for (auto &p : sortBindings(bindingsMap)) { | |||
| 678 | const Symbol *sym = p.first; | |||
| 679 | std::vector<BindingEntry> &bindings = p.second; | |||
| 680 | os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM) | |||
| 681 | << sym->getName() << '\0' | |||
| 682 | << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER); | |||
| 683 | std::vector<BindIR> opcodes; | |||
| 684 | for (const BindingEntry &b : bindings) | |||
| 685 | encodeBinding(b.target.isec->parent, | |||
| 686 | b.target.isec->getOffset(b.target.offset), b.addend, | |||
| 687 | lastBinding, opcodes); | |||
| 688 | if (config->optimize > 1) | |||
| 689 | optimizeOpcodes(opcodes); | |||
| 690 | for (const auto &op : opcodes) | |||
| 691 | flushOpcodes(op, os); | |||
| 692 | } | |||
| 693 | if (!bindingsMap.empty() || !definitions.empty()) | |||
| 694 | os << static_cast<uint8_t>(BIND_OPCODE_DONE); | |||
| 695 | } | |||
| 696 | ||||
| 697 | void WeakBindingSection::writeTo(uint8_t *buf) const { | |||
| 698 | memcpy(buf, contents.data(), contents.size()); | |||
| 699 | } | |||
| 700 | ||||
| 701 | StubsSection::StubsSection() | |||
| 702 | : SyntheticSection(segment_names::text, section_names::stubs) { | |||
| 703 | flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; | |||
| 704 | // The stubs section comprises machine instructions, which are aligned to | |||
| 705 | // 4 bytes on the archs we care about. | |||
| 706 | align = 4; | |||
| 707 | reserved2 = target->stubSize; | |||
| 708 | } | |||
| 709 | ||||
| 710 | uint64_t StubsSection::getSize() const { | |||
| 711 | return entries.size() * target->stubSize; | |||
| 712 | } | |||
| 713 | ||||
| 714 | void StubsSection::writeTo(uint8_t *buf) const { | |||
| 715 | size_t off = 0; | |||
| 716 | for (const Symbol *sym : entries) { | |||
| 717 | uint64_t pointerVA = | |||
| 718 | config->emitChainedFixups ? sym->getGotVA() : sym->getLazyPtrVA(); | |||
| 719 | target->writeStub(buf + off, *sym, pointerVA); | |||
| 720 | off += target->stubSize; | |||
| 721 | } | |||
| 722 | } | |||
| 723 | ||||
| 724 | void StubsSection::finalize() { isFinal = true; } | |||
| 725 | ||||
| 726 | static void addBindingsForStub(Symbol *sym) { | |||
| 727 | assert(!config->emitChainedFixups)(static_cast <bool> (!config->emitChainedFixups) ? void (0) : __assert_fail ("!config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 727, __extension__ __PRETTY_FUNCTION__)); | |||
| 728 | if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
| 729 | if (sym->isWeakDef()) { | |||
| 730 | in.binding->addEntry(dysym, in.lazyPointers->isec, | |||
| 731 | sym->stubsIndex * target->wordSize); | |||
| 732 | in.weakBinding->addEntry(sym, in.lazyPointers->isec, | |||
| 733 | sym->stubsIndex * target->wordSize); | |||
| 734 | } else { | |||
| 735 | in.lazyBinding->addEntry(dysym); | |||
| 736 | } | |||
| 737 | } else if (auto *defined = dyn_cast<Defined>(sym)) { | |||
| 738 | if (defined->isExternalWeakDef()) { | |||
| 739 | in.rebase->addEntry(in.lazyPointers->isec, | |||
| 740 | sym->stubsIndex * target->wordSize); | |||
| 741 | in.weakBinding->addEntry(sym, in.lazyPointers->isec, | |||
| 742 | sym->stubsIndex * target->wordSize); | |||
| 743 | } else if (defined->interposable) { | |||
| 744 | in.lazyBinding->addEntry(sym); | |||
| 745 | } else { | |||
| 746 | llvm_unreachable("invalid stub target")::llvm::llvm_unreachable_internal("invalid stub target", "lld/MachO/SyntheticSections.cpp" , 746); | |||
| 747 | } | |||
| 748 | } else { | |||
| 749 | llvm_unreachable("invalid stub target symbol type")::llvm::llvm_unreachable_internal("invalid stub target symbol type" , "lld/MachO/SyntheticSections.cpp", 749); | |||
| 750 | } | |||
| 751 | } | |||
| 752 | ||||
| 753 | void StubsSection::addEntry(Symbol *sym) { | |||
| 754 | bool inserted = entries.insert(sym); | |||
| 755 | if (inserted) { | |||
| 756 | sym->stubsIndex = entries.size() - 1; | |||
| 757 | ||||
| 758 | if (config->emitChainedFixups) | |||
| 759 | in.got->addEntry(sym); | |||
| 760 | else | |||
| 761 | addBindingsForStub(sym); | |||
| 762 | } | |||
| 763 | } | |||
| 764 | ||||
| 765 | StubHelperSection::StubHelperSection() | |||
| 766 | : SyntheticSection(segment_names::text, section_names::stubHelper) { | |||
| 767 | flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; | |||
| 768 | align = 4; // This section comprises machine instructions | |||
| 769 | } | |||
| 770 | ||||
| 771 | uint64_t StubHelperSection::getSize() const { | |||
| 772 | return target->stubHelperHeaderSize + | |||
| 773 | in.lazyBinding->getEntries().size() * target->stubHelperEntrySize; | |||
| 774 | } | |||
| 775 | ||||
| 776 | bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); } | |||
| 777 | ||||
| 778 | void StubHelperSection::writeTo(uint8_t *buf) const { | |||
| 779 | target->writeStubHelperHeader(buf); | |||
| 780 | size_t off = target->stubHelperHeaderSize; | |||
| 781 | for (const Symbol *sym : in.lazyBinding->getEntries()) { | |||
| 782 | target->writeStubHelperEntry(buf + off, *sym, addr + off); | |||
| 783 | off += target->stubHelperEntrySize; | |||
| 784 | } | |||
| 785 | } | |||
| 786 | ||||
| 787 | void StubHelperSection::setUp() { | |||
| 788 | Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, | |||
| 789 | /*isWeakRef=*/false); | |||
| 790 | if (auto *undefined = dyn_cast<Undefined>(binder)) | |||
| 791 | treatUndefinedSymbol(*undefined, | |||
| 792 | "lazy binding (normally in libSystem.dylib)"); | |||
| 793 | ||||
| 794 | // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check. | |||
| 795 | stubBinder = dyn_cast_or_null<DylibSymbol>(binder); | |||
| 796 | if (stubBinder == nullptr) | |||
| 797 | return; | |||
| 798 | ||||
| 799 | in.got->addEntry(stubBinder); | |||
| 800 | ||||
| 801 | in.imageLoaderCache->parent = | |||
| 802 | ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache); | |||
| 803 | inputSections.push_back(in.imageLoaderCache); | |||
| 804 | // Since this isn't in the symbol table or in any input file, the noDeadStrip | |||
| 805 | // argument doesn't matter. | |||
| 806 | dyldPrivate = | |||
| 807 | make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0, | |||
| 808 | /*isWeakDef=*/false, | |||
| 809 | /*isExternal=*/false, /*isPrivateExtern=*/false, | |||
| 810 | /*includeInSymtab=*/true, | |||
| 811 | /*isThumb=*/false, /*isReferencedDynamically=*/false, | |||
| 812 | /*noDeadStrip=*/false); | |||
| 813 | dyldPrivate->used = true; | |||
| 814 | } | |||
| 815 | ||||
| 816 | ObjCStubsSection::ObjCStubsSection() | |||
| 817 | : SyntheticSection(segment_names::text, section_names::objcStubs) { | |||
| 818 | flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; | |||
| 819 | align = target->objcStubsAlignment; | |||
| 820 | } | |||
| 821 | ||||
| 822 | void ObjCStubsSection::addEntry(Symbol *sym) { | |||
| 823 | assert(sym->getName().startswith(symbolPrefix) && "not an objc stub")(static_cast <bool> (sym->getName().startswith(symbolPrefix ) && "not an objc stub") ? void (0) : __assert_fail ( "sym->getName().startswith(symbolPrefix) && \"not an objc stub\"" , "lld/MachO/SyntheticSections.cpp", 823, __extension__ __PRETTY_FUNCTION__ )); | |||
| 824 | StringRef methname = sym->getName().drop_front(symbolPrefix.size()); | |||
| 825 | offsets.push_back( | |||
| 826 | in.objcMethnameSection->getStringOffset(methname).outSecOff); | |||
| 827 | Defined *newSym = replaceSymbol<Defined>( | |||
| 828 | sym, sym->getName(), nullptr, isec, | |||
| 829 | /*value=*/symbols.size() * target->objcStubsFastSize, | |||
| 830 | /*size=*/target->objcStubsFastSize, | |||
| 831 | /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true, | |||
| 832 | /*includeInSymtab=*/true, /*isThumb=*/false, | |||
| 833 | /*isReferencedDynamically=*/false, /*noDeadStrip=*/false); | |||
| 834 | symbols.push_back(newSym); | |||
| 835 | } | |||
| 836 | ||||
| 837 | void ObjCStubsSection::setUp() { | |||
| 838 | Symbol *objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr, | |||
| 839 | /*isWeakRef=*/false); | |||
| 840 | objcMsgSend->used = true; | |||
| 841 | in.got->addEntry(objcMsgSend); | |||
| 842 | assert(objcMsgSend->isInGot())(static_cast <bool> (objcMsgSend->isInGot()) ? void ( 0) : __assert_fail ("objcMsgSend->isInGot()", "lld/MachO/SyntheticSections.cpp" , 842, __extension__ __PRETTY_FUNCTION__)); | |||
| 843 | objcMsgSendGotIndex = objcMsgSend->gotIndex; | |||
| 844 | ||||
| 845 | size_t size = offsets.size() * target->wordSize; | |||
| 846 | uint8_t *selrefsData = bAlloc().Allocate<uint8_t>(size); | |||
| 847 | for (size_t i = 0, n = offsets.size(); i < n; ++i) | |||
| 848 | write64le(&selrefsData[i * target->wordSize], offsets[i]); | |||
| 849 | ||||
| 850 | in.objcSelrefs = | |||
| 851 | makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs, | |||
| 852 | S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP, | |||
| 853 | ArrayRef<uint8_t>{selrefsData, size}, | |||
| 854 | /*align=*/target->wordSize); | |||
| 855 | in.objcSelrefs->live = true; | |||
| 856 | ||||
| 857 | for (size_t i = 0, n = offsets.size(); i < n; ++i) { | |||
| 858 | in.objcSelrefs->relocs.push_back( | |||
| 859 | {/*type=*/target->unsignedRelocType, | |||
| 860 | /*pcrel=*/false, /*length=*/3, | |||
| 861 | /*offset=*/static_cast<uint32_t>(i * target->wordSize), | |||
| 862 | /*addend=*/offsets[i] * in.objcMethnameSection->align, | |||
| 863 | /*referent=*/in.objcMethnameSection->isec}); | |||
| 864 | } | |||
| 865 | ||||
| 866 | in.objcSelrefs->parent = | |||
| 867 | ConcatOutputSection::getOrCreateForInput(in.objcSelrefs); | |||
| 868 | inputSections.push_back(in.objcSelrefs); | |||
| 869 | in.objcSelrefs->isFinal = true; | |||
| 870 | } | |||
| 871 | ||||
| 872 | uint64_t ObjCStubsSection::getSize() const { | |||
| 873 | return target->objcStubsFastSize * symbols.size(); | |||
| 874 | } | |||
| 875 | ||||
| 876 | void ObjCStubsSection::writeTo(uint8_t *buf) const { | |||
| 877 | assert(in.objcSelrefs->live)(static_cast <bool> (in.objcSelrefs->live) ? void (0 ) : __assert_fail ("in.objcSelrefs->live", "lld/MachO/SyntheticSections.cpp" , 877, __extension__ __PRETTY_FUNCTION__)); | |||
| 878 | assert(in.objcSelrefs->isFinal)(static_cast <bool> (in.objcSelrefs->isFinal) ? void (0) : __assert_fail ("in.objcSelrefs->isFinal", "lld/MachO/SyntheticSections.cpp" , 878, __extension__ __PRETTY_FUNCTION__)); | |||
| 879 | ||||
| 880 | uint64_t stubOffset = 0; | |||
| 881 | for (size_t i = 0, n = symbols.size(); i < n; ++i) { | |||
| 882 | Defined *sym = symbols[i]; | |||
| 883 | target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr, | |||
| 884 | stubOffset, in.objcSelrefs->getVA(), i, | |||
| 885 | in.got->addr, objcMsgSendGotIndex); | |||
| 886 | stubOffset += target->objcStubsFastSize; | |||
| 887 | } | |||
| 888 | } | |||
| 889 | ||||
| 890 | LazyPointerSection::LazyPointerSection() | |||
| 891 | : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) { | |||
| 892 | align = target->wordSize; | |||
| 893 | flags = S_LAZY_SYMBOL_POINTERS; | |||
| 894 | } | |||
| 895 | ||||
| 896 | uint64_t LazyPointerSection::getSize() const { | |||
| 897 | return in.stubs->getEntries().size() * target->wordSize; | |||
| 898 | } | |||
| 899 | ||||
| 900 | bool LazyPointerSection::isNeeded() const { | |||
| 901 | return !in.stubs->getEntries().empty(); | |||
| 902 | } | |||
| 903 | ||||
| 904 | void LazyPointerSection::writeTo(uint8_t *buf) const { | |||
| 905 | size_t off = 0; | |||
| 906 | for (const Symbol *sym : in.stubs->getEntries()) { | |||
| 907 | if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
| 908 | if (dysym->hasStubsHelper()) { | |||
| 909 | uint64_t stubHelperOffset = | |||
| 910 | target->stubHelperHeaderSize + | |||
| 911 | dysym->stubsHelperIndex * target->stubHelperEntrySize; | |||
| 912 | write64le(buf + off, in.stubHelper->addr + stubHelperOffset); | |||
| 913 | } | |||
| 914 | } else { | |||
| 915 | write64le(buf + off, sym->getVA()); | |||
| 916 | } | |||
| 917 | off += target->wordSize; | |||
| 918 | } | |||
| 919 | } | |||
| 920 | ||||
| 921 | LazyBindingSection::LazyBindingSection() | |||
| 922 | : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {} | |||
| 923 | ||||
| 924 | void LazyBindingSection::finalizeContents() { | |||
| 925 | // TODO: Just precompute output size here instead of writing to a temporary | |||
| 926 | // buffer | |||
| 927 | for (Symbol *sym : entries) | |||
| 928 | sym->lazyBindOffset = encode(*sym); | |||
| 929 | } | |||
| 930 | ||||
| 931 | void LazyBindingSection::writeTo(uint8_t *buf) const { | |||
| 932 | memcpy(buf, contents.data(), contents.size()); | |||
| 933 | } | |||
| 934 | ||||
| 935 | void LazyBindingSection::addEntry(Symbol *sym) { | |||
| 936 | assert(!config->emitChainedFixups && "Chained fixups always bind eagerly")(static_cast <bool> (!config->emitChainedFixups && "Chained fixups always bind eagerly") ? void (0) : __assert_fail ("!config->emitChainedFixups && \"Chained fixups always bind eagerly\"" , "lld/MachO/SyntheticSections.cpp", 936, __extension__ __PRETTY_FUNCTION__ )); | |||
| 937 | if (entries.insert(sym)) { | |||
| 938 | sym->stubsHelperIndex = entries.size() - 1; | |||
| 939 | in.rebase->addEntry(in.lazyPointers->isec, | |||
| 940 | sym->stubsIndex * target->wordSize); | |||
| 941 | } | |||
| 942 | } | |||
| 943 | ||||
| 944 | // Unlike the non-lazy binding section, the bind opcodes in this section aren't | |||
| 945 | // interpreted all at once. Rather, dyld will start interpreting opcodes at a | |||
| 946 | // given offset, typically only binding a single symbol before it finds a | |||
| 947 | // BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case, | |||
| 948 | // we cannot encode just the differences between symbols; we have to emit the | |||
| 949 | // complete bind information for each symbol. | |||
| 950 | uint32_t LazyBindingSection::encode(const Symbol &sym) { | |||
| 951 | uint32_t opstreamOffset = contents.size(); | |||
| 952 | OutputSegment *dataSeg = in.lazyPointers->parent; | |||
| 953 | os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | | |||
| 954 | dataSeg->index); | |||
| 955 | uint64_t offset = | |||
| 956 | in.lazyPointers->addr - dataSeg->addr + sym.stubsIndex * target->wordSize; | |||
| 957 | encodeULEB128(offset, os); | |||
| 958 | encodeDylibOrdinal(ordinalForSymbol(sym), os); | |||
| 959 | ||||
| 960 | uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM; | |||
| 961 | if (sym.isWeakRef()) | |||
| 962 | flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT; | |||
| 963 | ||||
| 964 | os << flags << sym.getName() << '\0' | |||
| 965 | << static_cast<uint8_t>(BIND_OPCODE_DO_BIND) | |||
| 966 | << static_cast<uint8_t>(BIND_OPCODE_DONE); | |||
| 967 | return opstreamOffset; | |||
| 968 | } | |||
| 969 | ||||
| 970 | ExportSection::ExportSection() | |||
| 971 | : LinkEditSection(segment_names::linkEdit, section_names::export_) {} | |||
| 972 | ||||
| 973 | void ExportSection::finalizeContents() { | |||
| 974 | trieBuilder.setImageBase(in.header->addr); | |||
| 975 | for (const Symbol *sym : symtab->getSymbols()) { | |||
| 976 | if (const auto *defined = dyn_cast<Defined>(sym)) { | |||
| 977 | if (defined->privateExtern || !defined->isLive()) | |||
| 978 | continue; | |||
| 979 | trieBuilder.addSymbol(*defined); | |||
| 980 | hasWeakSymbol = hasWeakSymbol || sym->isWeakDef(); | |||
| 981 | } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
| 982 | if (dysym->shouldReexport) | |||
| 983 | trieBuilder.addSymbol(*dysym); | |||
| 984 | } | |||
| 985 | } | |||
| 986 | size = trieBuilder.build(); | |||
| 987 | } | |||
| 988 | ||||
| 989 | void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); } | |||
| 990 | ||||
| 991 | DataInCodeSection::DataInCodeSection() | |||
| 992 | : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {} | |||
| 993 | ||||
| 994 | template <class LP> | |||
| 995 | static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() { | |||
| 996 | std::vector<MachO::data_in_code_entry> dataInCodeEntries; | |||
| 997 | for (const InputFile *inputFile : inputFiles) { | |||
| 998 | if (!isa<ObjFile>(inputFile)) | |||
| 999 | continue; | |||
| 1000 | const ObjFile *objFile = cast<ObjFile>(inputFile); | |||
| 1001 | ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode(); | |||
| 1002 | if (entries.empty()) | |||
| 1003 | continue; | |||
| 1004 | ||||
| 1005 | assert(is_sorted(entries, [](const data_in_code_entry &lhs,(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1008, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1006 | const data_in_code_entry &rhs) {(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1008, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1007 | return lhs.offset < rhs.offset;(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1008, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1008 | }))(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1008, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1009 | // For each code subsection find 'data in code' entries residing in it. | |||
| 1010 | // Compute the new offset values as | |||
| 1011 | // <offset within subsection> + <subsection address> - <__TEXT address>. | |||
| 1012 | for (const Section *section : objFile->sections) { | |||
| 1013 | for (const Subsection &subsec : section->subsections) { | |||
| 1014 | const InputSection *isec = subsec.isec; | |||
| 1015 | if (!isCodeSection(isec)) | |||
| 1016 | continue; | |||
| 1017 | if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput()) | |||
| 1018 | continue; | |||
| 1019 | const uint64_t beginAddr = section->addr + subsec.offset; | |||
| 1020 | auto it = llvm::lower_bound( | |||
| 1021 | entries, beginAddr, | |||
| 1022 | [](const MachO::data_in_code_entry &entry, uint64_t addr) { | |||
| 1023 | return entry.offset < addr; | |||
| 1024 | }); | |||
| 1025 | const uint64_t endAddr = beginAddr + isec->getSize(); | |||
| 1026 | for (const auto end = entries.end(); | |||
| 1027 | it != end && it->offset + it->length <= endAddr; ++it) | |||
| 1028 | dataInCodeEntries.push_back( | |||
| 1029 | {static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) - | |||
| 1030 | in.header->addr), | |||
| 1031 | it->length, it->kind}); | |||
| 1032 | } | |||
| 1033 | } | |||
| 1034 | } | |||
| 1035 | ||||
| 1036 | // ld64 emits the table in sorted order too. | |||
| 1037 | llvm::sort(dataInCodeEntries, | |||
| 1038 | [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { | |||
| 1039 | return lhs.offset < rhs.offset; | |||
| 1040 | }); | |||
| 1041 | return dataInCodeEntries; | |||
| 1042 | } | |||
| 1043 | ||||
| 1044 | void DataInCodeSection::finalizeContents() { | |||
| 1045 | entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>() | |||
| 1046 | : collectDataInCodeEntries<ILP32>(); | |||
| 1047 | } | |||
| 1048 | ||||
| 1049 | void DataInCodeSection::writeTo(uint8_t *buf) const { | |||
| 1050 | if (!entries.empty()) | |||
| 1051 | memcpy(buf, entries.data(), getRawSize()); | |||
| 1052 | } | |||
| 1053 | ||||
| 1054 | FunctionStartsSection::FunctionStartsSection() | |||
| 1055 | : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {} | |||
| 1056 | ||||
| 1057 | void FunctionStartsSection::finalizeContents() { | |||
| 1058 | raw_svector_ostream os{contents}; | |||
| 1059 | std::vector<uint64_t> addrs; | |||
| 1060 | for (const InputFile *file : inputFiles) { | |||
| 1061 | if (auto *objFile = dyn_cast<ObjFile>(file)) { | |||
| 1062 | for (const Symbol *sym : objFile->symbols) { | |||
| 1063 | if (const auto *defined = dyn_cast_or_null<Defined>(sym)) { | |||
| 1064 | if (!defined->isec || !isCodeSection(defined->isec) || | |||
| 1065 | !defined->isLive()) | |||
| 1066 | continue; | |||
| 1067 | // TODO: Add support for thumbs, in that case | |||
| 1068 | // the lowest bit of nextAddr needs to be set to 1. | |||
| 1069 | addrs.push_back(defined->getVA()); | |||
| 1070 | } | |||
| 1071 | } | |||
| 1072 | } | |||
| 1073 | } | |||
| 1074 | llvm::sort(addrs); | |||
| 1075 | uint64_t addr = in.header->addr; | |||
| 1076 | for (uint64_t nextAddr : addrs) { | |||
| 1077 | uint64_t delta = nextAddr - addr; | |||
| 1078 | if (delta == 0) | |||
| 1079 | continue; | |||
| 1080 | encodeULEB128(delta, os); | |||
| 1081 | addr = nextAddr; | |||
| 1082 | } | |||
| 1083 | os << '\0'; | |||
| 1084 | } | |||
| 1085 | ||||
| 1086 | void FunctionStartsSection::writeTo(uint8_t *buf) const { | |||
| 1087 | memcpy(buf, contents.data(), contents.size()); | |||
| 1088 | } | |||
| 1089 | ||||
| 1090 | SymtabSection::SymtabSection(StringTableSection &stringTableSection) | |||
| 1091 | : LinkEditSection(segment_names::linkEdit, section_names::symbolTable), | |||
| 1092 | stringTableSection(stringTableSection) {} | |||
| 1093 | ||||
| 1094 | void SymtabSection::emitBeginSourceStab(StringRef sourceFile) { | |||
| 1095 | StabsEntry stab(N_SO); | |||
| 1096 | stab.strx = stringTableSection.addString(saver().save(sourceFile)); | |||
| 1097 | stabs.emplace_back(std::move(stab)); | |||
| 1098 | } | |||
| 1099 | ||||
| 1100 | void SymtabSection::emitEndSourceStab() { | |||
| 1101 | StabsEntry stab(N_SO); | |||
| 1102 | stab.sect = 1; | |||
| 1103 | stabs.emplace_back(std::move(stab)); | |||
| 1104 | } | |||
| 1105 | ||||
| 1106 | void SymtabSection::emitObjectFileStab(ObjFile *file) { | |||
| 1107 | StabsEntry stab(N_OSO); | |||
| 1108 | stab.sect = target->cpuSubtype; | |||
| 1109 | SmallString<261> path(!file->archiveName.empty() ? file->archiveName | |||
| 1110 | : file->getName()); | |||
| 1111 | std::error_code ec = sys::fs::make_absolute(path); | |||
| 1112 | if (ec) | |||
| 1113 | fatal("failed to get absolute path for " + path); | |||
| 1114 | ||||
| 1115 | if (!file->archiveName.empty()) | |||
| 1116 | path.append({"(", file->getName(), ")"}); | |||
| 1117 | ||||
| 1118 | StringRef adjustedPath = saver().save(path.str()); | |||
| 1119 | adjustedPath.consume_front(config->osoPrefix); | |||
| 1120 | ||||
| 1121 | stab.strx = stringTableSection.addString(adjustedPath); | |||
| 1122 | stab.desc = 1; | |||
| 1123 | stab.value = file->modTime; | |||
| 1124 | stabs.emplace_back(std::move(stab)); | |||
| 1125 | } | |||
| 1126 | ||||
| 1127 | void SymtabSection::emitEndFunStab(Defined *defined) { | |||
| 1128 | StabsEntry stab(N_FUN); | |||
| 1129 | stab.value = defined->size; | |||
| 1130 | stabs.emplace_back(std::move(stab)); | |||
| 1131 | } | |||
| 1132 | ||||
| 1133 | void SymtabSection::emitStabs() { | |||
| 1134 | if (config->omitDebugInfo) | |||
| 1135 | return; | |||
| 1136 | ||||
| 1137 | for (const std::string &s : config->astPaths) { | |||
| 1138 | StabsEntry astStab(N_AST); | |||
| 1139 | astStab.strx = stringTableSection.addString(s); | |||
| 1140 | stabs.emplace_back(std::move(astStab)); | |||
| 1141 | } | |||
| 1142 | ||||
| 1143 | // Cache the file ID for each symbol in an std::pair for faster sorting. | |||
| 1144 | using SortingPair = std::pair<Defined *, int>; | |||
| 1145 | std::vector<SortingPair> symbolsNeedingStabs; | |||
| 1146 | for (const SymtabEntry &entry : | |||
| 1147 | concat<SymtabEntry>(localSymbols, externalSymbols)) { | |||
| 1148 | Symbol *sym = entry.sym; | |||
| 1149 | assert(sym->isLive() &&(static_cast <bool> (sym->isLive() && "dead symbols should not be in localSymbols, externalSymbols" ) ? void (0) : __assert_fail ("sym->isLive() && \"dead symbols should not be in localSymbols, externalSymbols\"" , "lld/MachO/SyntheticSections.cpp", 1150, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1150 | "dead symbols should not be in localSymbols, externalSymbols")(static_cast <bool> (sym->isLive() && "dead symbols should not be in localSymbols, externalSymbols" ) ? void (0) : __assert_fail ("sym->isLive() && \"dead symbols should not be in localSymbols, externalSymbols\"" , "lld/MachO/SyntheticSections.cpp", 1150, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1151 | if (auto *defined = dyn_cast<Defined>(sym)) { | |||
| 1152 | // Excluded symbols should have been filtered out in finalizeContents(). | |||
| 1153 | assert(defined->includeInSymtab)(static_cast <bool> (defined->includeInSymtab) ? void (0) : __assert_fail ("defined->includeInSymtab", "lld/MachO/SyntheticSections.cpp" , 1153, __extension__ __PRETTY_FUNCTION__)); | |||
| 1154 | ||||
| 1155 | if (defined->isAbsolute()) | |||
| 1156 | continue; | |||
| 1157 | ||||
| 1158 | // Constant-folded symbols go in the executable's symbol table, but don't | |||
| 1159 | // get a stabs entry. | |||
| 1160 | if (defined->wasIdenticalCodeFolded) | |||
| 1161 | continue; | |||
| 1162 | ||||
| 1163 | ObjFile *file = defined->getObjectFile(); | |||
| 1164 | if (!file || !file->compileUnit) | |||
| 1165 | continue; | |||
| 1166 | ||||
| 1167 | symbolsNeedingStabs.emplace_back(defined, defined->isec->getFile()->id); | |||
| 1168 | } | |||
| 1169 | } | |||
| 1170 | ||||
| 1171 | llvm::stable_sort(symbolsNeedingStabs, | |||
| 1172 | [&](const SortingPair &a, const SortingPair &b) { | |||
| 1173 | return a.second < b.second; | |||
| 1174 | }); | |||
| 1175 | ||||
| 1176 | // Emit STABS symbols so that dsymutil and/or the debugger can map address | |||
| 1177 | // regions in the final binary to the source and object files from which they | |||
| 1178 | // originated. | |||
| 1179 | InputFile *lastFile = nullptr; | |||
| 1180 | for (SortingPair &pair : symbolsNeedingStabs) { | |||
| 1181 | Defined *defined = pair.first; | |||
| 1182 | InputSection *isec = defined->isec; | |||
| 1183 | ObjFile *file = cast<ObjFile>(isec->getFile()); | |||
| 1184 | ||||
| 1185 | if (lastFile == nullptr || lastFile != file) { | |||
| 1186 | if (lastFile != nullptr) | |||
| 1187 | emitEndSourceStab(); | |||
| 1188 | lastFile = file; | |||
| 1189 | ||||
| 1190 | emitBeginSourceStab(file->sourceFile()); | |||
| 1191 | emitObjectFileStab(file); | |||
| 1192 | } | |||
| 1193 | ||||
| 1194 | StabsEntry symStab; | |||
| 1195 | symStab.sect = defined->isec->parent->index; | |||
| 1196 | symStab.strx = stringTableSection.addString(defined->getName()); | |||
| 1197 | symStab.value = defined->getVA(); | |||
| 1198 | ||||
| 1199 | if (isCodeSection(isec)) { | |||
| 1200 | symStab.type = N_FUN; | |||
| 1201 | stabs.emplace_back(std::move(symStab)); | |||
| 1202 | emitEndFunStab(defined); | |||
| 1203 | } else { | |||
| 1204 | symStab.type = defined->isExternal() ? N_GSYM : N_STSYM; | |||
| 1205 | stabs.emplace_back(std::move(symStab)); | |||
| 1206 | } | |||
| 1207 | } | |||
| 1208 | ||||
| 1209 | if (!stabs.empty()) | |||
| 1210 | emitEndSourceStab(); | |||
| 1211 | } | |||
| 1212 | ||||
| 1213 | void SymtabSection::finalizeContents() { | |||
| 1214 | auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) { | |||
| 1215 | uint32_t strx = stringTableSection.addString(sym->getName()); | |||
| 1216 | symbols.push_back({sym, strx}); | |||
| 1217 | }; | |||
| 1218 | ||||
| 1219 | std::function<void(Symbol *)> localSymbolsHandler; | |||
| 1220 | switch (config->localSymbolsPresence) { | |||
| 1221 | case SymtabPresence::All: | |||
| 1222 | localSymbolsHandler = [&](Symbol *sym) { addSymbol(localSymbols, sym); }; | |||
| 1223 | break; | |||
| 1224 | case SymtabPresence::None: | |||
| 1225 | localSymbolsHandler = [&](Symbol *) { /* Do nothing*/ }; | |||
| 1226 | break; | |||
| 1227 | case SymtabPresence::SelectivelyIncluded: | |||
| 1228 | localSymbolsHandler = [&](Symbol *sym) { | |||
| 1229 | if (config->localSymbolPatterns.match(sym->getName())) | |||
| 1230 | addSymbol(localSymbols, sym); | |||
| 1231 | }; | |||
| 1232 | break; | |||
| 1233 | case SymtabPresence::SelectivelyExcluded: | |||
| 1234 | localSymbolsHandler = [&](Symbol *sym) { | |||
| 1235 | if (!config->localSymbolPatterns.match(sym->getName())) | |||
| 1236 | addSymbol(localSymbols, sym); | |||
| 1237 | }; | |||
| 1238 | break; | |||
| 1239 | } | |||
| 1240 | ||||
| 1241 | // Local symbols aren't in the SymbolTable, so we walk the list of object | |||
| 1242 | // files to gather them. | |||
| 1243 | // But if `-x` is set, then we don't need to. localSymbolsHandler() will do | |||
| 1244 | // the right thing regardless, but this check is a perf optimization because | |||
| 1245 | // iterating through all the input files and their symbols is expensive. | |||
| 1246 | if (config->localSymbolsPresence != SymtabPresence::None) { | |||
| 1247 | for (const InputFile *file : inputFiles) { | |||
| 1248 | if (auto *objFile = dyn_cast<ObjFile>(file)) { | |||
| 1249 | for (Symbol *sym : objFile->symbols) { | |||
| 1250 | if (auto *defined = dyn_cast_or_null<Defined>(sym)) { | |||
| 1251 | if (defined->isExternal() || !defined->isLive() || | |||
| 1252 | !defined->includeInSymtab) | |||
| 1253 | continue; | |||
| 1254 | localSymbolsHandler(sym); | |||
| 1255 | } | |||
| 1256 | } | |||
| 1257 | } | |||
| 1258 | } | |||
| 1259 | } | |||
| 1260 | ||||
| 1261 | // __dyld_private is a local symbol too. It's linker-created and doesn't | |||
| 1262 | // exist in any object file. | |||
| 1263 | if (in.stubHelper && in.stubHelper->dyldPrivate) | |||
| 1264 | localSymbolsHandler(in.stubHelper->dyldPrivate); | |||
| 1265 | ||||
| 1266 | for (Symbol *sym : symtab->getSymbols()) { | |||
| 1267 | if (!sym->isLive()) | |||
| 1268 | continue; | |||
| 1269 | if (auto *defined = dyn_cast<Defined>(sym)) { | |||
| 1270 | if (!defined->includeInSymtab) | |||
| 1271 | continue; | |||
| 1272 | assert(defined->isExternal())(static_cast <bool> (defined->isExternal()) ? void ( 0) : __assert_fail ("defined->isExternal()", "lld/MachO/SyntheticSections.cpp" , 1272, __extension__ __PRETTY_FUNCTION__)); | |||
| 1273 | if (defined->privateExtern) | |||
| 1274 | localSymbolsHandler(defined); | |||
| 1275 | else | |||
| 1276 | addSymbol(externalSymbols, defined); | |||
| 1277 | } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
| 1278 | if (dysym->isReferenced()) | |||
| 1279 | addSymbol(undefinedSymbols, sym); | |||
| 1280 | } | |||
| 1281 | } | |||
| 1282 | ||||
| 1283 | emitStabs(); | |||
| 1284 | uint32_t symtabIndex = stabs.size(); | |||
| 1285 | for (const SymtabEntry &entry : | |||
| 1286 | concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) { | |||
| 1287 | entry.sym->symtabIndex = symtabIndex++; | |||
| 1288 | } | |||
| 1289 | } | |||
| 1290 | ||||
| 1291 | uint32_t SymtabSection::getNumSymbols() const { | |||
| 1292 | return stabs.size() + localSymbols.size() + externalSymbols.size() + | |||
| 1293 | undefinedSymbols.size(); | |||
| 1294 | } | |||
| 1295 | ||||
| 1296 | // This serves to hide (type-erase) the template parameter from SymtabSection. | |||
| 1297 | template <class LP> class SymtabSectionImpl final : public SymtabSection { | |||
| 1298 | public: | |||
| 1299 | SymtabSectionImpl(StringTableSection &stringTableSection) | |||
| 1300 | : SymtabSection(stringTableSection) {} | |||
| 1301 | uint64_t getRawSize() const override; | |||
| 1302 | void writeTo(uint8_t *buf) const override; | |||
| 1303 | }; | |||
| 1304 | ||||
| 1305 | template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const { | |||
| 1306 | return getNumSymbols() * sizeof(typename LP::nlist); | |||
| 1307 | } | |||
| 1308 | ||||
| 1309 | template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const { | |||
| 1310 | auto *nList = reinterpret_cast<typename LP::nlist *>(buf); | |||
| 1311 | // Emit the stabs entries before the "real" symbols. We cannot emit them | |||
| 1312 | // after as that would render Symbol::symtabIndex inaccurate. | |||
| 1313 | for (const StabsEntry &entry : stabs) { | |||
| 1314 | nList->n_strx = entry.strx; | |||
| 1315 | nList->n_type = entry.type; | |||
| 1316 | nList->n_sect = entry.sect; | |||
| 1317 | nList->n_desc = entry.desc; | |||
| 1318 | nList->n_value = entry.value; | |||
| 1319 | ++nList; | |||
| 1320 | } | |||
| 1321 | ||||
| 1322 | for (const SymtabEntry &entry : concat<const SymtabEntry>( | |||
| 1323 | localSymbols, externalSymbols, undefinedSymbols)) { | |||
| 1324 | nList->n_strx = entry.strx; | |||
| 1325 | // TODO populate n_desc with more flags | |||
| 1326 | if (auto *defined = dyn_cast<Defined>(entry.sym)) { | |||
| 1327 | uint8_t scope = 0; | |||
| 1328 | if (defined->privateExtern) { | |||
| 1329 | // Private external -- dylib scoped symbol. | |||
| 1330 | // Promote to non-external at link time. | |||
| 1331 | scope = N_PEXT; | |||
| 1332 | } else if (defined->isExternal()) { | |||
| 1333 | // Normal global symbol. | |||
| 1334 | scope = N_EXT; | |||
| 1335 | } else { | |||
| 1336 | // TU-local symbol from localSymbols. | |||
| 1337 | scope = 0; | |||
| 1338 | } | |||
| 1339 | ||||
| 1340 | if (defined->isAbsolute()) { | |||
| 1341 | nList->n_type = scope | N_ABS; | |||
| 1342 | nList->n_sect = NO_SECT; | |||
| 1343 | nList->n_value = defined->value; | |||
| 1344 | } else { | |||
| 1345 | nList->n_type = scope | N_SECT; | |||
| 1346 | nList->n_sect = defined->isec->parent->index; | |||
| 1347 | // For the N_SECT symbol type, n_value is the address of the symbol | |||
| 1348 | nList->n_value = defined->getVA(); | |||
| 1349 | } | |||
| 1350 | nList->n_desc |= defined->thumb ? N_ARM_THUMB_DEF : 0; | |||
| 1351 | nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0; | |||
| 1352 | nList->n_desc |= | |||
| 1353 | defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0; | |||
| 1354 | } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) { | |||
| 1355 | uint16_t n_desc = nList->n_desc; | |||
| 1356 | int16_t ordinal = ordinalForDylibSymbol(*dysym); | |||
| 1357 | if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP) | |||
| 1358 | SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL); | |||
| 1359 | else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE) | |||
| 1360 | SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL); | |||
| 1361 | else { | |||
| 1362 | assert(ordinal > 0)(static_cast <bool> (ordinal > 0) ? void (0) : __assert_fail ("ordinal > 0", "lld/MachO/SyntheticSections.cpp", 1362, __extension__ __PRETTY_FUNCTION__)); | |||
| 1363 | SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal)); | |||
| 1364 | } | |||
| 1365 | ||||
| 1366 | nList->n_type = N_EXT; | |||
| 1367 | n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0; | |||
| 1368 | n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0; | |||
| 1369 | nList->n_desc = n_desc; | |||
| 1370 | } | |||
| 1371 | ++nList; | |||
| 1372 | } | |||
| 1373 | } | |||
| 1374 | ||||
| 1375 | template <class LP> | |||
| 1376 | SymtabSection * | |||
| 1377 | macho::makeSymtabSection(StringTableSection &stringTableSection) { | |||
| 1378 | return make<SymtabSectionImpl<LP>>(stringTableSection); | |||
| 1379 | } | |||
| 1380 | ||||
| 1381 | IndirectSymtabSection::IndirectSymtabSection() | |||
| 1382 | : LinkEditSection(segment_names::linkEdit, | |||
| 1383 | section_names::indirectSymbolTable) {} | |||
| 1384 | ||||
| 1385 | uint32_t IndirectSymtabSection::getNumSymbols() const { | |||
| 1386 | uint32_t size = in.got->getEntries().size() + | |||
| 1387 | in.tlvPointers->getEntries().size() + | |||
| 1388 | in.stubs->getEntries().size(); | |||
| 1389 | if (!config->emitChainedFixups) | |||
| 1390 | size += in.stubs->getEntries().size(); | |||
| 1391 | return size; | |||
| 1392 | } | |||
| 1393 | ||||
| 1394 | bool IndirectSymtabSection::isNeeded() const { | |||
| 1395 | return in.got->isNeeded() || in.tlvPointers->isNeeded() || | |||
| 1396 | in.stubs->isNeeded(); | |||
| 1397 | } | |||
| 1398 | ||||
| 1399 | void IndirectSymtabSection::finalizeContents() { | |||
| 1400 | uint32_t off = 0; | |||
| 1401 | in.got->reserved1 = off; | |||
| 1402 | off += in.got->getEntries().size(); | |||
| 1403 | in.tlvPointers->reserved1 = off; | |||
| 1404 | off += in.tlvPointers->getEntries().size(); | |||
| 1405 | in.stubs->reserved1 = off; | |||
| 1406 | if (in.lazyPointers) { | |||
| 1407 | off += in.stubs->getEntries().size(); | |||
| 1408 | in.lazyPointers->reserved1 = off; | |||
| 1409 | } | |||
| 1410 | } | |||
| 1411 | ||||
| 1412 | static uint32_t indirectValue(const Symbol *sym) { | |||
| 1413 | if (sym->symtabIndex == UINT32_MAX(4294967295U)) | |||
| 1414 | return INDIRECT_SYMBOL_LOCAL; | |||
| 1415 | if (auto *defined = dyn_cast<Defined>(sym)) | |||
| 1416 | if (defined->privateExtern) | |||
| 1417 | return INDIRECT_SYMBOL_LOCAL; | |||
| 1418 | return sym->symtabIndex; | |||
| 1419 | } | |||
| 1420 | ||||
| 1421 | void IndirectSymtabSection::writeTo(uint8_t *buf) const { | |||
| 1422 | uint32_t off = 0; | |||
| 1423 | for (const Symbol *sym : in.got->getEntries()) { | |||
| 1424 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
| 1425 | ++off; | |||
| 1426 | } | |||
| 1427 | for (const Symbol *sym : in.tlvPointers->getEntries()) { | |||
| 1428 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
| 1429 | ++off; | |||
| 1430 | } | |||
| 1431 | for (const Symbol *sym : in.stubs->getEntries()) { | |||
| 1432 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
| 1433 | ++off; | |||
| 1434 | } | |||
| 1435 | ||||
| 1436 | if (in.lazyPointers) { | |||
| 1437 | // There is a 1:1 correspondence between stubs and LazyPointerSection | |||
| 1438 | // entries. But giving __stubs and __la_symbol_ptr the same reserved1 | |||
| 1439 | // (the offset into the indirect symbol table) so that they both refer | |||
| 1440 | // to the same range of offsets confuses `strip`, so write the stubs | |||
| 1441 | // symbol table offsets a second time. | |||
| 1442 | for (const Symbol *sym : in.stubs->getEntries()) { | |||
| 1443 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
| 1444 | ++off; | |||
| 1445 | } | |||
| 1446 | } | |||
| 1447 | } | |||
| 1448 | ||||
| 1449 | StringTableSection::StringTableSection() | |||
| 1450 | : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {} | |||
| 1451 | ||||
| 1452 | uint32_t StringTableSection::addString(StringRef str) { | |||
| 1453 | uint32_t strx = size; | |||
| 1454 | strings.push_back(str); // TODO: consider deduplicating strings | |||
| 1455 | size += str.size() + 1; // account for null terminator | |||
| 1456 | return strx; | |||
| 1457 | } | |||
| 1458 | ||||
| 1459 | void StringTableSection::writeTo(uint8_t *buf) const { | |||
| 1460 | uint32_t off = 0; | |||
| 1461 | for (StringRef str : strings) { | |||
| 1462 | memcpy(buf + off, str.data(), str.size()); | |||
| 1463 | off += str.size() + 1; // account for null terminator | |||
| 1464 | } | |||
| 1465 | } | |||
| 1466 | ||||
| 1467 | static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0); | |||
| 1468 | static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0); | |||
| 1469 | ||||
| 1470 | CodeSignatureSection::CodeSignatureSection() | |||
| 1471 | : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) { | |||
| 1472 | align = 16; // required by libstuff | |||
| 1473 | // FIXME: Consider using finalOutput instead of outputFile. | |||
| 1474 | fileName = config->outputFile; | |||
| 1475 | size_t slashIndex = fileName.rfind("/"); | |||
| 1476 | if (slashIndex != std::string::npos) | |||
| 1477 | fileName = fileName.drop_front(slashIndex + 1); | |||
| 1478 | ||||
| 1479 | // NOTE: Any changes to these calculations should be repeated | |||
| 1480 | // in llvm-objcopy's MachOLayoutBuilder::layoutTail. | |||
| 1481 | allHeadersSize = alignTo<16>(fixedHeadersSize + fileName.size() + 1); | |||
| 1482 | fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size(); | |||
| 1483 | } | |||
| 1484 | ||||
| 1485 | uint32_t CodeSignatureSection::getBlockCount() const { | |||
| 1486 | return (fileOff + blockSize - 1) / blockSize; | |||
| 1487 | } | |||
| 1488 | ||||
| 1489 | uint64_t CodeSignatureSection::getRawSize() const { | |||
| 1490 | return allHeadersSize + getBlockCount() * hashSize; | |||
| 1491 | } | |||
| 1492 | ||||
| 1493 | void CodeSignatureSection::writeHashes(uint8_t *buf) const { | |||
| 1494 | // NOTE: Changes to this functionality should be repeated in llvm-objcopy's | |||
| 1495 | // MachOWriter::writeSignatureData. | |||
| 1496 | uint8_t *hashes = buf + fileOff + allHeadersSize; | |||
| 1497 | parallelFor(0, getBlockCount(), [&](size_t i) { | |||
| 1498 | sha256(buf + i * blockSize, | |||
| 1499 | std::min(static_cast<size_t>(fileOff - i * blockSize), blockSize), | |||
| 1500 | hashes + i * hashSize); | |||
| 1501 | }); | |||
| 1502 | #if defined(__APPLE__) | |||
| 1503 | // This is macOS-specific work-around and makes no sense for any | |||
| 1504 | // other host OS. See https://openradar.appspot.com/FB8914231 | |||
| 1505 | // | |||
| 1506 | // The macOS kernel maintains a signature-verification cache to | |||
| 1507 | // quickly validate applications at time of execve(2). The trouble | |||
| 1508 | // is that for the kernel creates the cache entry at the time of the | |||
| 1509 | // mmap(2) call, before we have a chance to write either the code to | |||
| 1510 | // sign or the signature header+hashes. The fix is to invalidate | |||
| 1511 | // all cached data associated with the output file, thus discarding | |||
| 1512 | // the bogus prematurely-cached signature. | |||
| 1513 | msync(buf, fileOff + getSize(), MS_INVALIDATE); | |||
| 1514 | #endif | |||
| 1515 | } | |||
| 1516 | ||||
| 1517 | void CodeSignatureSection::writeTo(uint8_t *buf) const { | |||
| 1518 | // NOTE: Changes to this functionality should be repeated in llvm-objcopy's | |||
| 1519 | // MachOWriter::writeSignatureData. | |||
| 1520 | uint32_t signatureSize = static_cast<uint32_t>(getSize()); | |||
| 1521 | auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf); | |||
| 1522 | write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE); | |||
| 1523 | write32be(&superBlob->length, signatureSize); | |||
| 1524 | write32be(&superBlob->count, 1); | |||
| 1525 | auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]); | |||
| 1526 | write32be(&blobIndex->type, CSSLOT_CODEDIRECTORY); | |||
| 1527 | write32be(&blobIndex->offset, blobHeadersSize); | |||
| 1528 | auto *codeDirectory = | |||
| 1529 | reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize); | |||
| 1530 | write32be(&codeDirectory->magic, CSMAGIC_CODEDIRECTORY); | |||
| 1531 | write32be(&codeDirectory->length, signatureSize - blobHeadersSize); | |||
| 1532 | write32be(&codeDirectory->version, CS_SUPPORTSEXECSEG); | |||
| 1533 | write32be(&codeDirectory->flags, CS_ADHOC | CS_LINKER_SIGNED); | |||
| 1534 | write32be(&codeDirectory->hashOffset, | |||
| 1535 | sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad); | |||
| 1536 | write32be(&codeDirectory->identOffset, sizeof(CS_CodeDirectory)); | |||
| 1537 | codeDirectory->nSpecialSlots = 0; | |||
| 1538 | write32be(&codeDirectory->nCodeSlots, getBlockCount()); | |||
| 1539 | write32be(&codeDirectory->codeLimit, fileOff); | |||
| 1540 | codeDirectory->hashSize = static_cast<uint8_t>(hashSize); | |||
| 1541 | codeDirectory->hashType = kSecCodeSignatureHashSHA256; | |||
| 1542 | codeDirectory->platform = 0; | |||
| 1543 | codeDirectory->pageSize = blockSizeShift; | |||
| 1544 | codeDirectory->spare2 = 0; | |||
| 1545 | codeDirectory->scatterOffset = 0; | |||
| 1546 | codeDirectory->teamOffset = 0; | |||
| 1547 | codeDirectory->spare3 = 0; | |||
| 1548 | codeDirectory->codeLimit64 = 0; | |||
| 1549 | OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text); | |||
| 1550 | write64be(&codeDirectory->execSegBase, textSeg->fileOff); | |||
| 1551 | write64be(&codeDirectory->execSegLimit, textSeg->fileSize); | |||
| 1552 | write64be(&codeDirectory->execSegFlags, | |||
| 1553 | config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0); | |||
| 1554 | auto *id = reinterpret_cast<char *>(&codeDirectory[1]); | |||
| 1555 | memcpy(id, fileName.begin(), fileName.size()); | |||
| 1556 | memset(id + fileName.size(), 0, fileNamePad); | |||
| 1557 | } | |||
| 1558 | ||||
| 1559 | BitcodeBundleSection::BitcodeBundleSection() | |||
| 1560 | : SyntheticSection(segment_names::llvm, section_names::bitcodeBundle) {} | |||
| 1561 | ||||
| 1562 | class ErrorCodeWrapper { | |||
| 1563 | public: | |||
| 1564 | explicit ErrorCodeWrapper(std::error_code ec) : errorCode(ec.value()) {} | |||
| 1565 | explicit ErrorCodeWrapper(int ec) : errorCode(ec) {} | |||
| 1566 | operator int() const { return errorCode; } | |||
| 1567 | ||||
| 1568 | private: | |||
| 1569 | int errorCode; | |||
| 1570 | }; | |||
| 1571 | ||||
| 1572 | #define CHECK_EC(exp)do { ErrorCodeWrapper ec(exp); if (ec) fatal(Twine("operation failed with error code " ) + Twine(ec) + ": " + "exp"); } while (0); \ | |||
| 1573 | do { \ | |||
| 1574 | ErrorCodeWrapper ec(exp); \ | |||
| 1575 | if (ec) \ | |||
| 1576 | fatal(Twine("operation failed with error code ") + Twine(ec) + ": " + \ | |||
| 1577 | #exp); \ | |||
| 1578 | } while (0); | |||
| 1579 | ||||
| 1580 | void BitcodeBundleSection::finalize() { | |||
| 1581 | #ifdef LLVM_HAVE_LIBXAR | |||
| 1582 | using namespace llvm::sys::fs; | |||
| 1583 | CHECK_EC(createTemporaryFile("bitcode-bundle", "xar", xarPath))do { ErrorCodeWrapper ec(createTemporaryFile("bitcode-bundle" , "xar", xarPath)); if (ec) fatal(Twine("operation failed with error code " ) + Twine(ec) + ": " + "createTemporaryFile(\"bitcode-bundle\", \"xar\", xarPath)" ); } while (0);; | |||
| 1584 | ||||
| 1585 | #pragma clang diagnostic push | |||
| 1586 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |||
| 1587 | xar_t xar(xar_open(xarPath.data(), O_RDWR)); | |||
| 1588 | #pragma clang diagnostic pop | |||
| 1589 | if (!xar) | |||
| 1590 | fatal("failed to open XAR temporary file at " + xarPath); | |||
| 1591 | CHECK_EC(xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE))do { ErrorCodeWrapper ec(xar_opt_set(xar, XAR_OPT_COMPRESSION , XAR_OPT_VAL_NONE)); if (ec) fatal(Twine("operation failed with error code " ) + Twine(ec) + ": " + "xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE)" ); } while (0);; | |||
| 1592 | // FIXME: add more data to XAR | |||
| 1593 | CHECK_EC(xar_close(xar))do { ErrorCodeWrapper ec(xar_close(xar)); if (ec) fatal(Twine ("operation failed with error code ") + Twine(ec) + ": " + "xar_close(xar)" ); } while (0);; | |||
| 1594 | ||||
| 1595 | file_size(xarPath, xarSize); | |||
| 1596 | #endif // defined(LLVM_HAVE_LIBXAR) | |||
| 1597 | } | |||
| 1598 | ||||
| 1599 | void BitcodeBundleSection::writeTo(uint8_t *buf) const { | |||
| 1600 | using namespace llvm::sys::fs; | |||
| 1601 | file_t handle = | |||
| 1602 | CHECK(openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None),check2((openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None )), [&] { return toString("failed to open XAR file"); }) | |||
| 1603 | "failed to open XAR file")check2((openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None )), [&] { return toString("failed to open XAR file"); }); | |||
| 1604 | std::error_code ec; | |||
| 1605 | mapped_file_region xarMap(handle, mapped_file_region::mapmode::readonly, | |||
| 1606 | xarSize, 0, ec); | |||
| 1607 | if (ec) | |||
| 1608 | fatal("failed to map XAR file"); | |||
| 1609 | memcpy(buf, xarMap.const_data(), xarSize); | |||
| 1610 | ||||
| 1611 | closeFile(handle); | |||
| 1612 | remove(xarPath); | |||
| 1613 | } | |||
| 1614 | ||||
| 1615 | CStringSection::CStringSection(const char *name) | |||
| 1616 | : SyntheticSection(segment_names::text, name) { | |||
| 1617 | flags = S_CSTRING_LITERALS; | |||
| 1618 | } | |||
| 1619 | ||||
| 1620 | void CStringSection::addInput(CStringInputSection *isec) { | |||
| 1621 | isec->parent = this; | |||
| 1622 | inputs.push_back(isec); | |||
| 1623 | if (isec->align > align) | |||
| 1624 | align = isec->align; | |||
| 1625 | } | |||
| 1626 | ||||
| 1627 | void CStringSection::writeTo(uint8_t *buf) const { | |||
| 1628 | for (const CStringInputSection *isec : inputs) { | |||
| 1629 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
| 1630 | if (!piece.live) | |||
| 1631 | continue; | |||
| 1632 | StringRef string = isec->getStringRef(i); | |||
| 1633 | memcpy(buf + piece.outSecOff, string.data(), string.size()); | |||
| 1634 | } | |||
| 1635 | } | |||
| 1636 | } | |||
| 1637 | ||||
| 1638 | void CStringSection::finalizeContents() { | |||
| 1639 | uint64_t offset = 0; | |||
| 1640 | for (CStringInputSection *isec : inputs) { | |||
| 1641 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
| 1642 | if (!piece.live) | |||
| 1643 | continue; | |||
| 1644 | // See comment above DeduplicatedCStringSection for how alignment is | |||
| 1645 | // handled. | |||
| 1646 | uint32_t pieceAlign = 1 | |||
| 1647 | << llvm::countr_zero(isec->align | piece.inSecOff); | |||
| 1648 | offset = alignToPowerOf2(offset, pieceAlign); | |||
| 1649 | piece.outSecOff = offset; | |||
| 1650 | isec->isFinal = true; | |||
| 1651 | StringRef string = isec->getStringRef(i); | |||
| 1652 | offset += string.size() + 1; // account for null terminator | |||
| 1653 | } | |||
| 1654 | } | |||
| 1655 | size = offset; | |||
| 1656 | } | |||
| 1657 | ||||
| 1658 | // Mergeable cstring literals are found under the __TEXT,__cstring section. In | |||
| 1659 | // contrast to ELF, which puts strings that need different alignments into | |||
| 1660 | // different sections, clang's Mach-O backend puts them all in one section. | |||
| 1661 | // Strings that need to be aligned have the .p2align directive emitted before | |||
| 1662 | // them, which simply translates into zero padding in the object file. In other | |||
| 1663 | // words, we have to infer the desired alignment of these cstrings from their | |||
| 1664 | // addresses. | |||
| 1665 | // | |||
| 1666 | // We differ slightly from ld64 in how we've chosen to align these cstrings. | |||
| 1667 | // Both LLD and ld64 preserve the number of trailing zeros in each cstring's | |||
| 1668 | // address in the input object files. When deduplicating identical cstrings, | |||
| 1669 | // both linkers pick the cstring whose address has more trailing zeros, and | |||
| 1670 | // preserve the alignment of that address in the final binary. However, ld64 | |||
| 1671 | // goes a step further and also preserves the offset of the cstring from the | |||
| 1672 | // last section-aligned address. I.e. if a cstring is at offset 18 in the | |||
| 1673 | // input, with a section alignment of 16, then both LLD and ld64 will ensure the | |||
| 1674 | // final address is 2-byte aligned (since 18 == 16 + 2). But ld64 will also | |||
| 1675 | // ensure that the final address is of the form 16 * k + 2 for some k. | |||
| 1676 | // | |||
| 1677 | // Note that ld64's heuristic means that a dedup'ed cstring's final address is | |||
| 1678 | // dependent on the order of the input object files. E.g. if in addition to the | |||
| 1679 | // cstring at offset 18 above, we have a duplicate one in another file with a | |||
| 1680 | // `.cstring` section alignment of 2 and an offset of zero, then ld64 will pick | |||
| 1681 | // the cstring from the object file earlier on the command line (since both have | |||
| 1682 | // the same number of trailing zeros in their address). So the final cstring may | |||
| 1683 | // either be at some address `16 * k + 2` or at some address `2 * k`. | |||
| 1684 | // | |||
| 1685 | // I've opted not to follow this behavior primarily for implementation | |||
| 1686 | // simplicity, and secondarily to save a few more bytes. It's not clear to me | |||
| 1687 | // that preserving the section alignment + offset is ever necessary, and there | |||
| 1688 | // are many cases that are clearly redundant. In particular, if an x86_64 object | |||
| 1689 | // file contains some strings that are accessed via SIMD instructions, then the | |||
| 1690 | // .cstring section in the object file will be 16-byte-aligned (since SIMD | |||
| 1691 | // requires its operand addresses to be 16-byte aligned). However, there will | |||
| 1692 | // typically also be other cstrings in the same file that aren't used via SIMD | |||
| 1693 | // and don't need this alignment. They will be emitted at some arbitrary address | |||
| 1694 | // `A`, but ld64 will treat them as being 16-byte aligned with an offset of `16 | |||
| 1695 | // % A`. | |||
| 1696 | void DeduplicatedCStringSection::finalizeContents() { | |||
| 1697 | // Find the largest alignment required for each string. | |||
| 1698 | for (const CStringInputSection *isec : inputs) { | |||
| 1699 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
| 1700 | if (!piece.live) | |||
| 1701 | continue; | |||
| 1702 | auto s = isec->getCachedHashStringRef(i); | |||
| 1703 | assert(isec->align != 0)(static_cast <bool> (isec->align != 0) ? void (0) : __assert_fail ("isec->align != 0", "lld/MachO/SyntheticSections.cpp", 1703 , __extension__ __PRETTY_FUNCTION__)); | |||
| 1704 | uint8_t trailingZeros = llvm::countr_zero(isec->align | piece.inSecOff); | |||
| 1705 | auto it = stringOffsetMap.insert( | |||
| 1706 | std::make_pair(s, StringOffset(trailingZeros))); | |||
| 1707 | if (!it.second && it.first->second.trailingZeros < trailingZeros) | |||
| 1708 | it.first->second.trailingZeros = trailingZeros; | |||
| 1709 | } | |||
| 1710 | } | |||
| 1711 | ||||
| 1712 | // Assign an offset for each string and save it to the corresponding | |||
| 1713 | // StringPieces for easy access. | |||
| 1714 | for (CStringInputSection *isec : inputs) { | |||
| 1715 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
| 1716 | if (!piece.live) | |||
| 1717 | continue; | |||
| 1718 | auto s = isec->getCachedHashStringRef(i); | |||
| 1719 | auto it = stringOffsetMap.find(s); | |||
| 1720 | assert(it != stringOffsetMap.end())(static_cast <bool> (it != stringOffsetMap.end()) ? void (0) : __assert_fail ("it != stringOffsetMap.end()", "lld/MachO/SyntheticSections.cpp" , 1720, __extension__ __PRETTY_FUNCTION__)); | |||
| 1721 | StringOffset &offsetInfo = it->second; | |||
| 1722 | if (offsetInfo.outSecOff == UINT64_MAX(18446744073709551615UL)) { | |||
| 1723 | offsetInfo.outSecOff = | |||
| 1724 | alignToPowerOf2(size, 1ULL << offsetInfo.trailingZeros); | |||
| 1725 | size = | |||
| 1726 | offsetInfo.outSecOff + s.size() + 1; // account for null terminator | |||
| 1727 | } | |||
| 1728 | piece.outSecOff = offsetInfo.outSecOff; | |||
| 1729 | } | |||
| 1730 | isec->isFinal = true; | |||
| 1731 | } | |||
| 1732 | } | |||
| 1733 | ||||
| 1734 | void DeduplicatedCStringSection::writeTo(uint8_t *buf) const { | |||
| 1735 | for (const auto &p : stringOffsetMap) { | |||
| 1736 | StringRef data = p.first.val(); | |||
| 1737 | uint64_t off = p.second.outSecOff; | |||
| 1738 | if (!data.empty()) | |||
| 1739 | memcpy(buf + off, data.data(), data.size()); | |||
| 1740 | } | |||
| 1741 | } | |||
| 1742 | ||||
| 1743 | DeduplicatedCStringSection::StringOffset | |||
| 1744 | DeduplicatedCStringSection::getStringOffset(StringRef str) const { | |||
| 1745 | // StringPiece uses 31 bits to store the hashes, so we replicate that | |||
| 1746 | uint32_t hash = xxHash64(str) & 0x7fffffff; | |||
| 1747 | auto offset = stringOffsetMap.find(CachedHashStringRef(str, hash)); | |||
| 1748 | assert(offset != stringOffsetMap.end() &&(static_cast <bool> (offset != stringOffsetMap.end() && "Looked-up strings should always exist in section") ? void ( 0) : __assert_fail ("offset != stringOffsetMap.end() && \"Looked-up strings should always exist in section\"" , "lld/MachO/SyntheticSections.cpp", 1749, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1749 | "Looked-up strings should always exist in section")(static_cast <bool> (offset != stringOffsetMap.end() && "Looked-up strings should always exist in section") ? void ( 0) : __assert_fail ("offset != stringOffsetMap.end() && \"Looked-up strings should always exist in section\"" , "lld/MachO/SyntheticSections.cpp", 1749, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1750 | return offset->second; | |||
| 1751 | } | |||
| 1752 | ||||
| 1753 | // This section is actually emitted as __TEXT,__const by ld64, but clang may | |||
| 1754 | // emit input sections of that name, and LLD doesn't currently support mixing | |||
| 1755 | // synthetic and concat-type OutputSections. To work around this, I've given | |||
| 1756 | // our merged-literals section a different name. | |||
| 1757 | WordLiteralSection::WordLiteralSection() | |||
| 1758 | : SyntheticSection(segment_names::text, section_names::literals) { | |||
| 1759 | align = 16; | |||
| 1760 | } | |||
| 1761 | ||||
| 1762 | void WordLiteralSection::addInput(WordLiteralInputSection *isec) { | |||
| 1763 | isec->parent = this; | |||
| 1764 | inputs.push_back(isec); | |||
| 1765 | } | |||
| 1766 | ||||
| 1767 | void WordLiteralSection::finalizeContents() { | |||
| 1768 | for (WordLiteralInputSection *isec : inputs) { | |||
| 1769 | // We do all processing of the InputSection here, so it will be effectively | |||
| 1770 | // finalized. | |||
| 1771 | isec->isFinal = true; | |||
| 1772 | const uint8_t *buf = isec->data.data(); | |||
| 1773 | switch (sectionType(isec->getFlags())) { | |||
| 1774 | case S_4BYTE_LITERALS: { | |||
| 1775 | for (size_t off = 0, e = isec->data.size(); off < e; off += 4) { | |||
| 1776 | if (!isec->isLive(off)) | |||
| 1777 | continue; | |||
| 1778 | uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off); | |||
| 1779 | literal4Map.emplace(value, literal4Map.size()); | |||
| 1780 | } | |||
| 1781 | break; | |||
| 1782 | } | |||
| 1783 | case S_8BYTE_LITERALS: { | |||
| 1784 | for (size_t off = 0, e = isec->data.size(); off < e; off += 8) { | |||
| 1785 | if (!isec->isLive(off)) | |||
| 1786 | continue; | |||
| 1787 | uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off); | |||
| 1788 | literal8Map.emplace(value, literal8Map.size()); | |||
| 1789 | } | |||
| 1790 | break; | |||
| 1791 | } | |||
| 1792 | case S_16BYTE_LITERALS: { | |||
| 1793 | for (size_t off = 0, e = isec->data.size(); off < e; off += 16) { | |||
| 1794 | if (!isec->isLive(off)) | |||
| 1795 | continue; | |||
| 1796 | UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off); | |||
| 1797 | literal16Map.emplace(value, literal16Map.size()); | |||
| 1798 | } | |||
| 1799 | break; | |||
| 1800 | } | |||
| 1801 | default: | |||
| 1802 | llvm_unreachable("invalid literal section type")::llvm::llvm_unreachable_internal("invalid literal section type" , "lld/MachO/SyntheticSections.cpp", 1802); | |||
| 1803 | } | |||
| 1804 | } | |||
| 1805 | } | |||
| 1806 | ||||
| 1807 | void WordLiteralSection::writeTo(uint8_t *buf) const { | |||
| 1808 | // Note that we don't attempt to do any endianness conversion in addInput(), | |||
| 1809 | // so we don't do it here either -- just write out the original value, | |||
| 1810 | // byte-for-byte. | |||
| 1811 | for (const auto &p : literal16Map) | |||
| 1812 | memcpy(buf + p.second * 16, &p.first, 16); | |||
| 1813 | buf += literal16Map.size() * 16; | |||
| 1814 | ||||
| 1815 | for (const auto &p : literal8Map) | |||
| 1816 | memcpy(buf + p.second * 8, &p.first, 8); | |||
| 1817 | buf += literal8Map.size() * 8; | |||
| 1818 | ||||
| 1819 | for (const auto &p : literal4Map) | |||
| 1820 | memcpy(buf + p.second * 4, &p.first, 4); | |||
| 1821 | } | |||
| 1822 | ||||
| 1823 | ObjCImageInfoSection::ObjCImageInfoSection() | |||
| 1824 | : SyntheticSection(segment_names::data, section_names::objCImageInfo) {} | |||
| 1825 | ||||
| 1826 | ObjCImageInfoSection::ImageInfo | |||
| 1827 | ObjCImageInfoSection::parseImageInfo(const InputFile *file) { | |||
| 1828 | ImageInfo info; | |||
| 1829 | ArrayRef<uint8_t> data = file->objCImageInfo; | |||
| 1830 | // The image info struct has the following layout: | |||
| 1831 | // struct { | |||
| 1832 | // uint32_t version; | |||
| 1833 | // uint32_t flags; | |||
| 1834 | // }; | |||
| 1835 | if (data.size() < 8) { | |||
| 1836 | warn(toString(file) + ": invalid __objc_imageinfo size"); | |||
| 1837 | return info; | |||
| 1838 | } | |||
| 1839 | ||||
| 1840 | auto *buf = reinterpret_cast<const uint32_t *>(data.data()); | |||
| 1841 | if (read32le(buf) != 0) { | |||
| 1842 | warn(toString(file) + ": invalid __objc_imageinfo version"); | |||
| 1843 | return info; | |||
| 1844 | } | |||
| 1845 | ||||
| 1846 | uint32_t flags = read32le(buf + 1); | |||
| 1847 | info.swiftVersion = (flags >> 8) & 0xff; | |||
| 1848 | info.hasCategoryClassProperties = flags & 0x40; | |||
| 1849 | return info; | |||
| 1850 | } | |||
| 1851 | ||||
| 1852 | static std::string swiftVersionString(uint8_t version) { | |||
| 1853 | switch (version) { | |||
| 1854 | case 1: | |||
| 1855 | return "1.0"; | |||
| 1856 | case 2: | |||
| 1857 | return "1.1"; | |||
| 1858 | case 3: | |||
| 1859 | return "2.0"; | |||
| 1860 | case 4: | |||
| 1861 | return "3.0"; | |||
| 1862 | case 5: | |||
| 1863 | return "4.0"; | |||
| 1864 | default: | |||
| 1865 | return ("0x" + Twine::utohexstr(version)).str(); | |||
| 1866 | } | |||
| 1867 | } | |||
| 1868 | ||||
| 1869 | // Validate each object file's __objc_imageinfo and use them to generate the | |||
| 1870 | // image info for the output binary. Only two pieces of info are relevant: | |||
| 1871 | // 1. The Swift version (should be identical across inputs) | |||
| 1872 | // 2. `bool hasCategoryClassProperties` (true only if true for all inputs) | |||
| 1873 | void ObjCImageInfoSection::finalizeContents() { | |||
| 1874 | assert(files.size() != 0)(static_cast <bool> (files.size() != 0) ? void (0) : __assert_fail ("files.size() != 0", "lld/MachO/SyntheticSections.cpp", 1874 , __extension__ __PRETTY_FUNCTION__)); // should have already been checked via isNeeded() | |||
| ||||
| 1875 | ||||
| 1876 | info.hasCategoryClassProperties = true; | |||
| 1877 | const InputFile *firstFile; | |||
| 1878 | for (const InputFile *file : files) { | |||
| 1879 | ImageInfo inputInfo = parseImageInfo(file); | |||
| 1880 | info.hasCategoryClassProperties &= inputInfo.hasCategoryClassProperties; | |||
| 1881 | ||||
| 1882 | // swiftVersion 0 means no Swift is present, so no version checking required | |||
| 1883 | if (inputInfo.swiftVersion == 0) | |||
| 1884 | continue; | |||
| 1885 | ||||
| 1886 | if (info.swiftVersion != 0 && info.swiftVersion != inputInfo.swiftVersion) { | |||
| 1887 | error("Swift version mismatch: " + toString(firstFile) + " has version " + | |||
| ||||
| 1888 | swiftVersionString(info.swiftVersion) + " but " + toString(file) + | |||
| 1889 | " has version " + swiftVersionString(inputInfo.swiftVersion)); | |||
| 1890 | } else { | |||
| 1891 | info.swiftVersion = inputInfo.swiftVersion; | |||
| 1892 | firstFile = file; | |||
| 1893 | } | |||
| 1894 | } | |||
| 1895 | } | |||
| 1896 | ||||
| 1897 | void ObjCImageInfoSection::writeTo(uint8_t *buf) const { | |||
| 1898 | uint32_t flags = info.hasCategoryClassProperties ? 0x40 : 0x0; | |||
| 1899 | flags |= info.swiftVersion << 8; | |||
| 1900 | write32le(buf + 4, flags); | |||
| 1901 | } | |||
| 1902 | ||||
| 1903 | InitOffsetsSection::InitOffsetsSection() | |||
| 1904 | : SyntheticSection(segment_names::text, section_names::initOffsets) { | |||
| 1905 | flags = S_INIT_FUNC_OFFSETS; | |||
| 1906 | align = 4; // This section contains 32-bit integers. | |||
| 1907 | } | |||
| 1908 | ||||
| 1909 | uint64_t InitOffsetsSection::getSize() const { | |||
| 1910 | size_t count = 0; | |||
| 1911 | for (const ConcatInputSection *isec : sections) | |||
| 1912 | count += isec->relocs.size(); | |||
| 1913 | return count * sizeof(uint32_t); | |||
| 1914 | } | |||
| 1915 | ||||
| 1916 | void InitOffsetsSection::writeTo(uint8_t *buf) const { | |||
| 1917 | // FIXME: Add function specified by -init when that argument is implemented. | |||
| 1918 | for (ConcatInputSection *isec : sections) { | |||
| 1919 | for (const Reloc &rel : isec->relocs) { | |||
| 1920 | const Symbol *referent = rel.referent.dyn_cast<Symbol *>(); | |||
| 1921 | assert(referent && "section relocation should have been rejected")(static_cast <bool> (referent && "section relocation should have been rejected" ) ? void (0) : __assert_fail ("referent && \"section relocation should have been rejected\"" , "lld/MachO/SyntheticSections.cpp", 1921, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1922 | uint64_t offset = referent->getVA() - in.header->addr; | |||
| 1923 | // FIXME: Can we handle this gracefully? | |||
| 1924 | if (offset > UINT32_MAX(4294967295U)) | |||
| 1925 | fatal(isec->getLocation(rel.offset) + ": offset to initializer " + | |||
| 1926 | referent->getName() + " (" + utohexstr(offset) + | |||
| 1927 | ") does not fit in 32 bits"); | |||
| 1928 | ||||
| 1929 | // Entries need to be added in the order they appear in the section, but | |||
| 1930 | // relocations aren't guaranteed to be sorted. | |||
| 1931 | size_t index = rel.offset >> target->p2WordSize; | |||
| 1932 | write32le(&buf[index * sizeof(uint32_t)], offset); | |||
| 1933 | } | |||
| 1934 | buf += isec->relocs.size() * sizeof(uint32_t); | |||
| 1935 | } | |||
| 1936 | } | |||
| 1937 | ||||
| 1938 | // The inputs are __mod_init_func sections, which contain pointers to | |||
| 1939 | // initializer functions, therefore all relocations should be of the UNSIGNED | |||
| 1940 | // type. InitOffsetsSection stores offsets, so if the initializer's address is | |||
| 1941 | // not known at link time, stub-indirection has to be used. | |||
| 1942 | void InitOffsetsSection::setUp() { | |||
| 1943 | for (const ConcatInputSection *isec : sections) { | |||
| 1944 | for (const Reloc &rel : isec->relocs) { | |||
| 1945 | RelocAttrs attrs = target->getRelocAttrs(rel.type); | |||
| 1946 | if (!attrs.hasAttr(RelocAttrBits::UNSIGNED)) | |||
| 1947 | error(isec->getLocation(rel.offset) + | |||
| 1948 | ": unsupported relocation type: " + attrs.name); | |||
| 1949 | if (rel.addend != 0) | |||
| 1950 | error(isec->getLocation(rel.offset) + | |||
| 1951 | ": relocation addend is not representable in __init_offsets"); | |||
| 1952 | if (rel.referent.is<InputSection *>()) | |||
| 1953 | error(isec->getLocation(rel.offset) + | |||
| 1954 | ": unexpected section relocation"); | |||
| 1955 | ||||
| 1956 | Symbol *sym = rel.referent.dyn_cast<Symbol *>(); | |||
| 1957 | if (auto *undefined = dyn_cast<Undefined>(sym)) | |||
| 1958 | treatUndefinedSymbol(*undefined, isec, rel.offset); | |||
| 1959 | if (needsBinding(sym)) | |||
| 1960 | in.stubs->addEntry(sym); | |||
| 1961 | } | |||
| 1962 | } | |||
| 1963 | } | |||
| 1964 | ||||
| 1965 | void macho::createSyntheticSymbols() { | |||
| 1966 | auto addHeaderSymbol = [](const char *name) { | |||
| 1967 | symtab->addSynthetic(name, in.header->isec, /*value=*/0, | |||
| 1968 | /*isPrivateExtern=*/true, /*includeInSymtab=*/false, | |||
| 1969 | /*referencedDynamically=*/false); | |||
| 1970 | }; | |||
| 1971 | ||||
| 1972 | switch (config->outputType) { | |||
| 1973 | // FIXME: Assign the right address value for these symbols | |||
| 1974 | // (rather than 0). But we need to do that after assignAddresses(). | |||
| 1975 | case MH_EXECUTE: | |||
| 1976 | // If linking PIE, __mh_execute_header is a defined symbol in | |||
| 1977 | // __TEXT, __text) | |||
| 1978 | // Otherwise, it's an absolute symbol. | |||
| 1979 | if (config->isPic) | |||
| 1980 | symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0, | |||
| 1981 | /*isPrivateExtern=*/false, /*includeInSymtab=*/true, | |||
| 1982 | /*referencedDynamically=*/true); | |||
| 1983 | else | |||
| 1984 | symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0, | |||
| 1985 | /*isPrivateExtern=*/false, /*includeInSymtab=*/true, | |||
| 1986 | /*referencedDynamically=*/true); | |||
| 1987 | break; | |||
| 1988 | ||||
| 1989 | // The following symbols are N_SECT symbols, even though the header is not | |||
| 1990 | // part of any section and that they are private to the bundle/dylib/object | |||
| 1991 | // they are part of. | |||
| 1992 | case MH_BUNDLE: | |||
| 1993 | addHeaderSymbol("__mh_bundle_header"); | |||
| 1994 | break; | |||
| 1995 | case MH_DYLIB: | |||
| 1996 | addHeaderSymbol("__mh_dylib_header"); | |||
| 1997 | break; | |||
| 1998 | case MH_DYLINKER: | |||
| 1999 | addHeaderSymbol("__mh_dylinker_header"); | |||
| 2000 | break; | |||
| 2001 | case MH_OBJECT: | |||
| 2002 | addHeaderSymbol("__mh_object_header"); | |||
| 2003 | break; | |||
| 2004 | default: | |||
| 2005 | llvm_unreachable("unexpected outputType")::llvm::llvm_unreachable_internal("unexpected outputType", "lld/MachO/SyntheticSections.cpp" , 2005); | |||
| 2006 | break; | |||
| 2007 | } | |||
| 2008 | ||||
| 2009 | // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit | |||
| 2010 | // which does e.g. cleanup of static global variables. The ABI document | |||
| 2011 | // says that the pointer can point to any address in one of the dylib's | |||
| 2012 | // segments, but in practice ld64 seems to set it to point to the header, | |||
| 2013 | // so that's what's implemented here. | |||
| 2014 | addHeaderSymbol("___dso_handle"); | |||
| 2015 | } | |||
| 2016 | ||||
| 2017 | ChainedFixupsSection::ChainedFixupsSection() | |||
| 2018 | : LinkEditSection(segment_names::linkEdit, section_names::chainFixups) {} | |||
| 2019 | ||||
| 2020 | bool ChainedFixupsSection::isNeeded() const { | |||
| 2021 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 2021, __extension__ __PRETTY_FUNCTION__)); | |||
| 2022 | // dyld always expects LC_DYLD_CHAINED_FIXUPS to point to a valid | |||
| 2023 | // dyld_chained_fixups_header, so we create this section even if there aren't | |||
| 2024 | // any fixups. | |||
| 2025 | return true; | |||
| 2026 | } | |||
| 2027 | ||||
| 2028 | static bool needsWeakBind(const Symbol &sym) { | |||
| 2029 | if (auto *dysym = dyn_cast<DylibSymbol>(&sym)) | |||
| 2030 | return dysym->isWeakDef(); | |||
| 2031 | if (auto *defined = dyn_cast<Defined>(&sym)) | |||
| 2032 | return defined->isExternalWeakDef(); | |||
| 2033 | return false; | |||
| 2034 | } | |||
| 2035 | ||||
| 2036 | void ChainedFixupsSection::addBinding(const Symbol *sym, | |||
| 2037 | const InputSection *isec, uint64_t offset, | |||
| 2038 | int64_t addend) { | |||
| 2039 | locations.emplace_back(isec, offset); | |||
| 2040 | int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0; | |||
| 2041 | auto [it, inserted] = bindings.insert( | |||
| 2042 | {{sym, outlineAddend}, static_cast<uint32_t>(bindings.size())}); | |||
| 2043 | ||||
| 2044 | if (inserted) { | |||
| 2045 | symtabSize += sym->getName().size() + 1; | |||
| 2046 | hasWeakBind = hasWeakBind || needsWeakBind(*sym); | |||
| 2047 | if (!isInt<23>(outlineAddend)) | |||
| 2048 | needsLargeAddend = true; | |||
| 2049 | else if (outlineAddend != 0) | |||
| 2050 | needsAddend = true; | |||
| 2051 | } | |||
| 2052 | } | |||
| 2053 | ||||
| 2054 | std::pair<uint32_t, uint8_t> | |||
| 2055 | ChainedFixupsSection::getBinding(const Symbol *sym, int64_t addend) const { | |||
| 2056 | int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0; | |||
| 2057 | auto it = bindings.find({sym, outlineAddend}); | |||
| 2058 | assert(it != bindings.end() && "binding not found in the imports table")(static_cast <bool> (it != bindings.end() && "binding not found in the imports table" ) ? void (0) : __assert_fail ("it != bindings.end() && \"binding not found in the imports table\"" , "lld/MachO/SyntheticSections.cpp", 2058, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2059 | if (outlineAddend == 0) | |||
| 2060 | return {it->second, addend}; | |||
| 2061 | return {it->second, 0}; | |||
| 2062 | } | |||
| 2063 | ||||
| 2064 | static size_t writeImport(uint8_t *buf, int format, uint32_t libOrdinal, | |||
| 2065 | bool weakRef, uint32_t nameOffset, int64_t addend) { | |||
| 2066 | switch (format) { | |||
| 2067 | case DYLD_CHAINED_IMPORT: { | |||
| 2068 | auto *import = reinterpret_cast<dyld_chained_import *>(buf); | |||
| 2069 | import->lib_ordinal = libOrdinal; | |||
| 2070 | import->weak_import = weakRef; | |||
| 2071 | import->name_offset = nameOffset; | |||
| 2072 | return sizeof(dyld_chained_import); | |||
| 2073 | } | |||
| 2074 | case DYLD_CHAINED_IMPORT_ADDEND: { | |||
| 2075 | auto *import = reinterpret_cast<dyld_chained_import_addend *>(buf); | |||
| 2076 | import->lib_ordinal = libOrdinal; | |||
| 2077 | import->weak_import = weakRef; | |||
| 2078 | import->name_offset = nameOffset; | |||
| 2079 | import->addend = addend; | |||
| 2080 | return sizeof(dyld_chained_import_addend); | |||
| 2081 | } | |||
| 2082 | case DYLD_CHAINED_IMPORT_ADDEND64: { | |||
| 2083 | auto *import = reinterpret_cast<dyld_chained_import_addend64 *>(buf); | |||
| 2084 | import->lib_ordinal = libOrdinal; | |||
| 2085 | import->weak_import = weakRef; | |||
| 2086 | import->name_offset = nameOffset; | |||
| 2087 | import->addend = addend; | |||
| 2088 | return sizeof(dyld_chained_import_addend64); | |||
| 2089 | } | |||
| 2090 | default: | |||
| 2091 | llvm_unreachable("Unknown import format")::llvm::llvm_unreachable_internal("Unknown import format", "lld/MachO/SyntheticSections.cpp" , 2091); | |||
| 2092 | } | |||
| 2093 | } | |||
| 2094 | ||||
| 2095 | size_t ChainedFixupsSection::SegmentInfo::getSize() const { | |||
| 2096 | assert(pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?")(static_cast <bool> (pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?") ? void (0) : __assert_fail ("pageStarts.size() > 0 && \"SegmentInfo for segment with no fixups?\"" , "lld/MachO/SyntheticSections.cpp", 2096, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2097 | return alignTo<8>(sizeof(dyld_chained_starts_in_segment) + | |||
| 2098 | pageStarts.back().first * sizeof(uint16_t)); | |||
| 2099 | } | |||
| 2100 | ||||
| 2101 | size_t ChainedFixupsSection::SegmentInfo::writeTo(uint8_t *buf) const { | |||
| 2102 | auto *segInfo = reinterpret_cast<dyld_chained_starts_in_segment *>(buf); | |||
| 2103 | segInfo->size = getSize(); | |||
| 2104 | segInfo->page_size = target->getPageSize(); | |||
| 2105 | // FIXME: Use DYLD_CHAINED_PTR_64_OFFSET on newer OS versions. | |||
| 2106 | segInfo->pointer_format = DYLD_CHAINED_PTR_64; | |||
| 2107 | segInfo->segment_offset = oseg->addr - in.header->addr; | |||
| 2108 | segInfo->max_valid_pointer = 0; // not used on 64-bit | |||
| 2109 | segInfo->page_count = pageStarts.back().first + 1; | |||
| 2110 | ||||
| 2111 | uint16_t *starts = segInfo->page_start; | |||
| 2112 | for (size_t i = 0; i < segInfo->page_count; ++i) | |||
| 2113 | starts[i] = DYLD_CHAINED_PTR_START_NONE; | |||
| 2114 | ||||
| 2115 | for (auto [pageIdx, startAddr] : pageStarts) | |||
| 2116 | starts[pageIdx] = startAddr; | |||
| 2117 | return segInfo->size; | |||
| 2118 | } | |||
| 2119 | ||||
| 2120 | static size_t importEntrySize(int format) { | |||
| 2121 | switch (format) { | |||
| 2122 | case DYLD_CHAINED_IMPORT: | |||
| 2123 | return sizeof(dyld_chained_import); | |||
| 2124 | case DYLD_CHAINED_IMPORT_ADDEND: | |||
| 2125 | return sizeof(dyld_chained_import_addend); | |||
| 2126 | case DYLD_CHAINED_IMPORT_ADDEND64: | |||
| 2127 | return sizeof(dyld_chained_import_addend64); | |||
| 2128 | default: | |||
| 2129 | llvm_unreachable("Unknown import format")::llvm::llvm_unreachable_internal("Unknown import format", "lld/MachO/SyntheticSections.cpp" , 2129); | |||
| 2130 | } | |||
| 2131 | } | |||
| 2132 | ||||
| 2133 | // This is step 3 of the algorithm described in the class comment of | |||
| 2134 | // ChainedFixupsSection. | |||
| 2135 | // | |||
| 2136 | // LC_DYLD_CHAINED_FIXUPS data consists of (in this order): | |||
| 2137 | // * A dyld_chained_fixups_header | |||
| 2138 | // * A dyld_chained_starts_in_image | |||
| 2139 | // * One dyld_chained_starts_in_segment per segment | |||
| 2140 | // * List of all imports (dyld_chained_import, dyld_chained_import_addend, or | |||
| 2141 | // dyld_chained_import_addend64) | |||
| 2142 | // * Names of imported symbols | |||
| 2143 | void ChainedFixupsSection::writeTo(uint8_t *buf) const { | |||
| 2144 | auto *header = reinterpret_cast<dyld_chained_fixups_header *>(buf); | |||
| 2145 | header->fixups_version = 0; | |||
| 2146 | header->imports_count = bindings.size(); | |||
| 2147 | header->imports_format = importFormat; | |||
| 2148 | header->symbols_format = 0; | |||
| 2149 | ||||
| 2150 | buf += alignTo<8>(sizeof(*header)); | |||
| 2151 | ||||
| 2152 | auto curOffset = [&buf, &header]() -> uint32_t { | |||
| 2153 | return buf - reinterpret_cast<uint8_t *>(header); | |||
| 2154 | }; | |||
| 2155 | ||||
| 2156 | header->starts_offset = curOffset(); | |||
| 2157 | ||||
| 2158 | auto *imageInfo = reinterpret_cast<dyld_chained_starts_in_image *>(buf); | |||
| 2159 | imageInfo->seg_count = outputSegments.size(); | |||
| 2160 | uint32_t *segStarts = imageInfo->seg_info_offset; | |||
| 2161 | ||||
| 2162 | // dyld_chained_starts_in_image ends in a flexible array member containing an | |||
| 2163 | // uint32_t for each segment. Leave room for it, and fill it via segStarts. | |||
| 2164 | buf += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset)__builtin_offsetof(dyld_chained_starts_in_image, seg_info_offset ) + | |||
| 2165 | outputSegments.size() * sizeof(uint32_t)); | |||
| 2166 | ||||
| 2167 | // Initialize all offsets to 0, which indicates that the segment does not have | |||
| 2168 | // fixups. Those that do have them will be filled in below. | |||
| 2169 | for (size_t i = 0; i < outputSegments.size(); ++i) | |||
| 2170 | segStarts[i] = 0; | |||
| 2171 | ||||
| 2172 | for (const SegmentInfo &seg : fixupSegments) { | |||
| 2173 | segStarts[seg.oseg->index] = curOffset() - header->starts_offset; | |||
| 2174 | buf += seg.writeTo(buf); | |||
| 2175 | } | |||
| 2176 | ||||
| 2177 | // Write imports table. | |||
| 2178 | header->imports_offset = curOffset(); | |||
| 2179 | uint64_t nameOffset = 0; | |||
| 2180 | for (auto [import, idx] : bindings) { | |||
| 2181 | const Symbol &sym = *import.first; | |||
| 2182 | int16_t libOrdinal = needsWeakBind(sym) | |||
| 2183 | ? (int64_t)BIND_SPECIAL_DYLIB_WEAK_LOOKUP | |||
| 2184 | : ordinalForSymbol(sym); | |||
| 2185 | buf += writeImport(buf, importFormat, libOrdinal, sym.isWeakRef(), | |||
| 2186 | nameOffset, import.second); | |||
| 2187 | nameOffset += sym.getName().size() + 1; | |||
| 2188 | } | |||
| 2189 | ||||
| 2190 | // Write imported symbol names. | |||
| 2191 | header->symbols_offset = curOffset(); | |||
| 2192 | for (auto [import, idx] : bindings) { | |||
| 2193 | StringRef name = import.first->getName(); | |||
| 2194 | memcpy(buf, name.data(), name.size()); | |||
| 2195 | buf += name.size() + 1; // account for null terminator | |||
| 2196 | } | |||
| 2197 | ||||
| 2198 | assert(curOffset() == getRawSize())(static_cast <bool> (curOffset() == getRawSize()) ? void (0) : __assert_fail ("curOffset() == getRawSize()", "lld/MachO/SyntheticSections.cpp" , 2198, __extension__ __PRETTY_FUNCTION__)); | |||
| 2199 | } | |||
| 2200 | ||||
| 2201 | // This is step 2 of the algorithm described in the class comment of | |||
| 2202 | // ChainedFixupsSection. | |||
| 2203 | void ChainedFixupsSection::finalizeContents() { | |||
| 2204 | assert(target->wordSize == 8 && "Only 64-bit platforms are supported")(static_cast <bool> (target->wordSize == 8 && "Only 64-bit platforms are supported") ? void (0) : __assert_fail ("target->wordSize == 8 && \"Only 64-bit platforms are supported\"" , "lld/MachO/SyntheticSections.cpp", 2204, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2205 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 2205, __extension__ __PRETTY_FUNCTION__)); | |||
| 2206 | ||||
| 2207 | if (!isUInt<32>(symtabSize)) | |||
| 2208 | error("cannot encode chained fixups: imported symbols table size " + | |||
| 2209 | Twine(symtabSize) + " exceeds 4 GiB"); | |||
| 2210 | ||||
| 2211 | if (needsLargeAddend || !isUInt<23>(symtabSize)) | |||
| 2212 | importFormat = DYLD_CHAINED_IMPORT_ADDEND64; | |||
| 2213 | else if (needsAddend) | |||
| 2214 | importFormat = DYLD_CHAINED_IMPORT_ADDEND; | |||
| 2215 | else | |||
| 2216 | importFormat = DYLD_CHAINED_IMPORT; | |||
| 2217 | ||||
| 2218 | for (Location &loc : locations) | |||
| 2219 | loc.offset = | |||
| 2220 | loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset); | |||
| 2221 | ||||
| 2222 | llvm::sort(locations, [](const Location &a, const Location &b) { | |||
| 2223 | const OutputSegment *segA = a.isec->parent->parent; | |||
| 2224 | const OutputSegment *segB = b.isec->parent->parent; | |||
| 2225 | if (segA == segB) | |||
| 2226 | return a.offset < b.offset; | |||
| 2227 | return segA->addr < segB->addr; | |||
| 2228 | }); | |||
| 2229 | ||||
| 2230 | auto sameSegment = [](const Location &a, const Location &b) { | |||
| 2231 | return a.isec->parent->parent == b.isec->parent->parent; | |||
| 2232 | }; | |||
| 2233 | ||||
| 2234 | const uint64_t pageSize = target->getPageSize(); | |||
| 2235 | for (size_t i = 0, count = locations.size(); i < count;) { | |||
| 2236 | const Location &firstLoc = locations[i]; | |||
| 2237 | fixupSegments.emplace_back(firstLoc.isec->parent->parent); | |||
| 2238 | while (i < count && sameSegment(locations[i], firstLoc)) { | |||
| 2239 | uint32_t pageIdx = locations[i].offset / pageSize; | |||
| 2240 | fixupSegments.back().pageStarts.emplace_back( | |||
| 2241 | pageIdx, locations[i].offset % pageSize); | |||
| 2242 | ++i; | |||
| 2243 | while (i < count && sameSegment(locations[i], firstLoc) && | |||
| 2244 | locations[i].offset / pageSize == pageIdx) | |||
| 2245 | ++i; | |||
| 2246 | } | |||
| 2247 | } | |||
| 2248 | ||||
| 2249 | // Compute expected encoded size. | |||
| 2250 | size = alignTo<8>(sizeof(dyld_chained_fixups_header)); | |||
| 2251 | size += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset)__builtin_offsetof(dyld_chained_starts_in_image, seg_info_offset ) + | |||
| 2252 | outputSegments.size() * sizeof(uint32_t)); | |||
| 2253 | for (const SegmentInfo &seg : fixupSegments) | |||
| 2254 | size += seg.getSize(); | |||
| 2255 | size += importEntrySize(importFormat) * bindings.size(); | |||
| 2256 | size += symtabSize; | |||
| 2257 | } | |||
| 2258 | ||||
| 2259 | template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &); | |||
| 2260 | template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &); |