Lines Matching +full:- +full:- +full:require +full:- +full:hashes
1 //===- Writer.cpp ---------------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
27 #include "llvm/Config/llvm-config.h"
99 c->cmd = LC_DYLD_INFO_ONLY;
100 c->cmdsize = getSize();
101 if (rebaseSection->isNeeded()) {
102 c->rebase_off = rebaseSection->fileOff;
103 c->rebase_size = rebaseSection->getFileSize();
105 if (bindingSection->isNeeded()) {
106 c->bind_off = bindingSection->fileOff;
107 c->bind_size = bindingSection->getFileSize();
109 if (weakBindingSection->isNeeded()) {
110 c->weak_bind_off = weakBindingSection->fileOff;
111 c->weak_bind_size = weakBindingSection->getFileSize();
113 if (lazyBindingSection->isNeeded()) {
114 c->lazy_bind_off = lazyBindingSection->fileOff;
115 c->lazy_bind_size = lazyBindingSection->getFileSize();
117 if (exportSection->isNeeded()) {
118 c->export_off = exportSection->fileOff;
119 c->export_size = exportSection->getFileSize();
136 target->wordSize);
143 c->cmd = LC_SUB_FRAMEWORK;
144 c->cmdsize = getSize();
145 c->umbrella = sizeof(sub_framework_command);
164 c->cmd = LC_FUNCTION_STARTS;
165 c->cmdsize = getSize();
166 c->dataoff = functionStartsSection->fileOff;
167 c->datasize = functionStartsSection->getFileSize();
183 c->cmd = LC_DATA_IN_CODE;
184 c->cmdsize = getSize();
185 c->dataoff = dataInCodeSection->fileOff;
186 c->datasize = dataInCodeSection->getFileSize();
204 c->cmd = LC_DYSYMTAB;
205 c->cmdsize = getSize();
207 c->ilocalsym = 0;
208 c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols();
209 c->nextdefsym = symtabSection->getNumExternalSymbols();
210 c->iundefsym = c->iextdefsym + c->nextdefsym;
211 c->nundefsym = symtabSection->getNumUndefinedSymbols();
213 c->indirectsymoff = indirectSymtabSection->fileOff;
214 c->nindirectsyms = indirectSymtabSection->getNumSymbols();
227 seg->numNonHiddenSections() * sizeof(typename LP::section);
237 c->cmd = LP::segmentLCType;
238 c->cmdsize = getSize();
239 memcpy(c->segname, name.data(), name.size());
240 c->fileoff = seg->fileOff;
241 c->maxprot = seg->maxProt;
242 c->initprot = seg->initProt;
244 c->vmaddr = seg->addr;
245 c->vmsize = seg->vmSize;
246 c->filesize = seg->fileSize;
247 c->nsects = seg->numNonHiddenSections();
248 c->flags = seg->flags;
250 for (const OutputSection *osec : seg->getSections()) {
251 if (osec->isHidden())
257 memcpy(sectHdr->sectname, osec->name.data(), osec->name.size());
258 memcpy(sectHdr->segname, name.data(), name.size());
260 sectHdr->addr = osec->addr;
261 sectHdr->offset = osec->fileOff;
262 sectHdr->align = Log2_32(osec->align);
263 sectHdr->flags = osec->flags;
264 sectHdr->size = osec->getSize();
265 sectHdr->reserved1 = osec->reserved1;
266 sectHdr->reserved2 = osec->reserved2;
282 c->cmd = LC_MAIN;
283 c->cmdsize = getSize();
285 if (config->entry->isInStubs())
286 c->entryoff =
287 in.stubs->fileOff + config->entry->stubsIndex * target->stubSize;
289 c->entryoff = config->entry->getVA() - in.header->addr;
291 c->stacksize = 0;
304 c->cmd = LC_SYMTAB;
305 c->cmdsize = getSize();
306 c->symoff = symtabSection->fileOff;
307 c->nsyms = symtabSection->getNumSymbols();
308 c->stroff = stringTableSection->fileOff;
309 c->strsize = stringTableSection->getFileSize();
331 target->wordSize);
338 c->cmd = type;
339 c->cmdsize = getSize();
340 c->dylib.name = sizeof(dylib_command);
341 c->dylib.timestamp = 0;
342 c->dylib.compatibility_version = compatibilityVersion;
343 c->dylib.current_version = currentVersion;
366 target->wordSize);
373 c->cmd = LC_LOAD_DYLINKER;
374 c->cmdsize = getSize();
375 c->name = sizeof(dylinker_command);
393 target->wordSize);
400 c->cmd = LC_RPATH;
401 c->cmdsize = getSize();
402 c->path = sizeof(rpath_command);
418 target->wordSize);
425 c->cmd = LC_SUB_CLIENT;
426 c->cmdsize = getSize();
427 c->client = sizeof(sub_client_command);
443 target->wordSize);
450 c->cmd = LC_DYLD_ENVIRONMENT;
451 c->cmdsize = getSize();
452 c->name = sizeof(dyld_env_command);
473 c->cmd = LC_VERSION_MIN_MACOSX;
477 c->cmd = LC_VERSION_MIN_IPHONEOS;
481 c->cmd = LC_VERSION_MIN_TVOS;
485 c->cmd = LC_VERSION_MIN_WATCHOS;
491 c->cmdsize = getSize();
492 c->version = encodeVersion(platformInfo.target.MinDeployment);
493 c->sdk = encodeVersion(platformInfo.sdk);
513 c->cmd = LC_BUILD_VERSION;
514 c->cmdsize = getSize();
516 c->platform = static_cast<uint32_t>(platformInfo.target.Platform);
517 c->minos = encodeVersion(platformInfo.target.MinDeployment);
518 c->sdk = encodeVersion(platformInfo.sdk);
520 c->ntools = ntools;
522 t->tool = TOOL_LLD;
523 t->version = encodeVersion(VersionTuple(
535 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with
543 c->cmd = LC_UUID;
544 c->cmdsize = getSize();
545 uuidBuf = c->uuid;
560 // Claim that this is an MD5-based hash. It isn't, but this signals that
561 // this is not a time-based and not a random hash. MD5 seems like the least
580 c->cmd = LP::encryptionInfoLCType;
581 c->cmdsize = getSize();
582 c->cryptoff = in.header->getSize();
584 return seg->name == segment_names::text;
587 c->cryptsize = (*it)->fileSize - c->cryptoff;
599 c->cmd = LC_CODE_SIGNATURE;
600 c->cmdsize = getSize();
601 c->dataoff = static_cast<uint32_t>(section->fileOff);
602 c->datasize = section->getSize();
616 c->cmd = LC_DYLD_EXPORTS_TRIE;
617 c->cmdsize = getSize();
618 c->dataoff = section->fileOff;
619 c->datasize = section->getSize();
633 c->cmd = LC_DYLD_CHAINED_FIXUPS;
634 c->cmdsize = getSize();
635 c->dataoff = section->fileOff;
636 c->datasize = section->getSize();
645 if (config->entry)
646 if (auto *undefined = dyn_cast<Undefined>(config->entry))
650 // via -u flag twice.
651 for (const Symbol *sym : config->explicitUndefineds) {
653 treatUndefinedSymbol(*undefined, "-u");
655 // Literal exported-symbol names must be defined, but glob
658 config->exportedSymbols.literals) {
659 if (const Symbol *sym = symtab->find(cachedName))
661 treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)");
667 if (!sym->isLive()) {
669 if (config->emitInitOffsets &&
670 defined->isec()->getName() == section_names::moduleInitFunc)
671 fatal(isec->getLocation(r.offset) + ": cannot reference " +
672 sym->getName() +
673 " defined in __mod_init_func when -init_offsets is used");
678 const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
682 in.stubs->addEntry(sym);
685 in.got->addEntry(sym);
688 in.tlvPointers->addEntry(sym);
690 // References from thread-local variable sections are treated as offsets
693 if (!(isThreadLocalVariables(isec->getFlags()) && isa<Defined>(sym)))
701 // This can't use a for-each loop: It calls treatUndefinedSymbol(), which can
706 if (isec->shouldOmitFromOutput())
709 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
715 r.referent = referentIsec->canonical();
717 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
718 // Skip over the following UNSIGNED relocation -- it's just there as the
724 if (auto *referentIsec = it->referent.dyn_cast<InputSection *>())
725 it->referent = referentIsec->canonical();
731 // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check.
736 if (config->emitChainedFixups)
737 in.chainedFixups->addRebase(isec, r.offset);
739 in.rebase->addEntry(isec, r.offset);
745 in.unwindInfo->prepare();
749 if (config->emitChainedFixups)
750 in.chainedFixups->setHasNonWeakDefinition();
752 in.weakBinding->addNonWeakDefinition(defined);
758 for (Symbol *sym : symtab->getSymbols()) {
760 if (!defined->isLive())
762 if (defined->overridesWeakDef)
764 if (!defined->isAbsolute() && isCodeSection(defined->isec()))
765 in.unwindInfo->addSymbol(defined);
768 if (dysym->isDynamicLookup())
770 dysym->getFile()->refState =
771 std::max(dysym->getFile()->refState, dysym->getRefState());
774 // When -dead_strip is enabled, we don't want to emit any dead stubs.
777 if (config->deadStrip) {
778 if (!sym->isLive())
781 in.objcStubs->addEntry(sym);
788 for (Symbol *sym : objFile->symbols) {
790 if (!defined->isLive())
792 if (!defined->isExternal() && !defined->isAbsolute() &&
793 isCodeSection(defined->isec()))
794 in.unwindInfo->addSymbol(defined);
815 : platformInfo.target.MinDeployment >= it->second;
821 in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg));
822 seg->index = segIndex++;
825 if (config->emitChainedFixups) {
826 in.header->addLoadCommand(make<LCChainedFixups>(in.chainedFixups));
827 in.header->addLoadCommand(make<LCExportsTrie>(in.exports));
829 in.header->addLoadCommand(make<LCDyldInfo>(
832 in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection));
833 in.header->addLoadCommand(
835 if (!config->umbrella.empty())
836 in.header->addLoadCommand(make<LCSubFramework>(config->umbrella));
837 if (config->emitEncryptionInfo)
838 in.header->addLoadCommand(make<LCEncryptionInfo<LP>>());
839 for (StringRef path : config->runtimePaths)
840 in.header->addLoadCommand(make<LCRPath>(path));
842 switch (config->outputType) {
844 in.header->addLoadCommand(make<LCLoadDylinker>());
847 in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
848 config->dylibCompatibilityVersion,
849 config->dylibCurrentVersion));
850 for (StringRef client : config->allowableClients)
851 in.header->addLoadCommand(make<LCSubClient>(client));
859 if (config->generateUuid) {
861 in.header->addLoadCommand(uuidCommand);
864 if (useLCBuildVersion(config->platformInfo))
865 in.header->addLoadCommand(make<LCBuildVersion>(config->platformInfo));
867 in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo));
869 if (config->secondaryPlatformInfo) {
870 in.header->addLoadCommand(
871 make<LCBuildVersion>(*config->secondaryPlatformInfo));
875 if (config->outputType == MH_EXECUTE)
876 in.header->addLoadCommand(make<LCMain>());
889 dylibFiles.insert(dylibFiles.end(), dylibFiles[i]->extraDylibs.begin(),
890 dylibFiles[i]->extraDylibs.end());
893 if (dylibFile->isBundleLoader) {
894 dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE;
895 // Shortcut since bundle-loader does not re-export the symbols.
897 dylibFile->reexport = false;
902 // - it was added implicitly (via a reexport, an LC_LOAD_DYLINKER --
904 // - or it's marked MH_DEAD_STRIPPABLE_DYLIB
905 // - or the flag -dead_strip_dylibs is used
909 if (!dylibFile->isReferenced() && !dylibFile->forceNeeded &&
910 (!dylibFile->isExplicitlyLinked() || dylibFile->deadStrippable ||
911 config->deadStripDylibs))
918 // - a new framework could change its installName to an older
920 // - symlinks (for example, libpthread.tbd is a symlink to libSystem.tbd;
925 // - a framework can be linked both explicitly on the linker
927 // framework. The re-export will usually point to the tbd file
930 // but in a --reproduce archive they will be identical but distinct
934 int64_t &ordinal = ordinalForInstallName[dylibFile->installName];
936 dylibFile->ordinal = ordinal;
940 ordinal = dylibFile->ordinal = dylibOrdinal++;
942 dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
945 in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName,
946 dylibFile->compatibilityVersion,
947 dylibFile->currentVersion));
949 if (dylibFile->reexport)
950 in.header->addLoadCommand(
951 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName));
954 for (const auto &dyldEnv : config->dyldEnvs)
955 in.header->addLoadCommand(make<LCDyldEnv>(dyldEnv));
958 in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection));
960 in.header->addLoadCommand(make<LCDataInCode>(dataInCodeSection));
962 in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection));
965 config->headerPad = std::max(
966 config->headerPad, (config->headerPadMaxInstallNames
983 seg->sortOutputSections();
984 // References from thread-local variable sections are treated as offsets
985 // relative to the start of the thread-local data memory area, which
987 // contiguous). If later data sections require a greater alignment than
992 for (const OutputSection *osec : seg->getSections())
993 if (isThreadLocalData(osec->flags) && osec->align > tlvAlign)
994 tlvAlign = osec->align;
996 for (OutputSection *osec : seg->getSections()) {
999 if (!osec->isHidden())
1000 osec->index = ++sectionIndex;
1001 if (isThreadLocalData(osec->flags)) {
1004 osec->align = tlvAlign;
1010 merged->inputs, [&](InputSection *a, InputSection *b) {
1025 if (config->adhocCodesign)
1027 if (config->emitDataInCodeInfo)
1029 if (config->emitFunctionStarts)
1032 switch (config->outputType) {
1045 if (isec->shouldOmitFromOutput())
1047 ConcatOutputSection *osec = cast<ConcatOutputSection>(isec->parent);
1048 osec->addInput(isec);
1049 osec->inputOrder =
1050 std::min(osec->inputOrder, static_cast<int>(isec->outSecOff));
1059 if (osec->isNeeded()) {
1061 if (osec->name == section_names::ehFrame &&
1063 osec->align = target->wordSize;
1065 // MC keeps the default 1-byte alignment for __thread_vars, even though it
1068 if (isThreadLocalVariables(osec->flags))
1069 osec->align = std::max<uint32_t>(osec->align, target->wordSize);
1071 getOrCreateOutputSegment(segname)->addOutputSection(osec);
1076 auto it = concatOutputSections.find({ssec->segname, ssec->name});
1082 if (ssec->isNeeded() || ssec->segname == segment_names::linkEdit) {
1084 getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
1087 toString(it->second->firstSection()->getFile()) +
1088 " conflicts with synthetic section " + ssec->segname + "," +
1089 ssec->name);
1100 uint64_t pageSize = target->getPageSize();
1107 for (OutputSection *osec : seg->getSections()) {
1108 if (!osec->isNeeded())
1112 concatOsec->finalizeContents();
1125 seg->addr = addr;
1134 seg->vmSize = addr - seg->addr;
1135 seg->fileSize = fileOff - seg->fileOff;
1136 seg->assignAddressesToStartEndSymbols();
1154 osec->finalizeContents();
1159 linkEditSegment->addr = addr;
1161 // No need to page-align fileOff / addr here since this is the last segment.
1162 linkEditSegment->vmSize = addr - linkEditSegment->addr;
1163 linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff;
1167 seg->fileOff = fileOff;
1169 for (OutputSection *osec : seg->getSections()) {
1170 if (!osec->isNeeded())
1172 addr = alignToPowerOf2(addr, osec->align);
1173 fileOff = alignToPowerOf2(fileOff, osec->align);
1174 osec->addr = addr;
1175 osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff;
1176 osec->finalize();
1177 osec->assignAddressesToStartEndSymbols();
1179 addr += osec->getSize();
1180 fileOff += osec->getFileSize();
1186 FileOutputBuffer::create(config->outputFile, fileOff,
1190 fatal("failed to open " + config->outputFile + ": " +
1193 in.bufferStart = buffer->getBufferStart();
1199 uint8_t *buf = buffer->getBufferStart();
1202 append_range(osecs, seg->getSections());
1205 osec->writeTo(buf + osec->fileOff);
1210 if (config->arch() != AK_arm64 || config->ignoreOptimizationHints)
1213 uint8_t *buf = buffer->getBufferStart();
1217 target->applyOptimizationHints(buf, *objFile);
1227 ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()};
1231 std::vector<uint64_t> hashes(chunks.size() + 1);
1233 [&](size_t i) { hashes[i] = xxh3_64bits(chunks[i]); });
1236 hashes[chunks.size()] = xxh3_64bits(sys::path::filename(config->finalOutput));
1238 uint64_t digest = xxh3_64bits({reinterpret_cast<uint8_t *>(hashes.data()),
1239 hashes.size() * sizeof(uint64_t)});
1240 uuidCommand->writeUuid(digest);
1246 if (!config->emitChainedFixups)
1249 const std::vector<Location> &loc = in.chainedFixups->getLocations();
1255 const uint64_t pageSize = target->getPageSize();
1259 const OutputSegment *oseg = loc[i].isec->parent->parent;
1260 uint8_t *buf = buffer->getBufferStart() + oseg->fileOff;
1264 while (i < count && loc[i].isec->parent->parent == oseg &&
1266 uint64_t offset = loc[i].offset - loc[i - 1].offset;
1269 error(loc[i].isec->getSegName() + "," + loc[i].isec->getName() +
1271 Twine(loc[i].offset - loc[i].isec->parent->getSegmentOffset()) +
1275 if (offset < target->wordSize)
1280 " is not a multiple of the stride). Re-link with -no_fixup_chains");
1283 reinterpret_cast<dyld_chained_ptr_64_bind *>(buf + loc[i - 1].offset)
1284 ->next = offset / stride;
1293 codeSignatureSection->writeHashes(buffer->getBufferStart());
1306 if (config->generateUuid)
1310 if (auto e = buffer->commit())
1311 fatal("failed to write output '" + buffer->getPath() +
1317 if (config->entry && needsBinding(config->entry))
1318 in.stubs->addEntry(config->entry);
1322 // InputSections, we should have `isec->canonical() == isec`.
1324 if (in.objcStubs->isNeeded())
1325 in.objcStubs->setUp();
1326 if (in.objcMethList->isNeeded())
1327 in.objcMethList->setUp();
1329 if (in.initOffsets->isNeeded())
1330 in.initOffsets->setUp();
1338 if (in.stubHelper && in.stubHelper->isNeeded())
1339 in.stubHelper->setUp();
1341 if (in.objCImageInfo->isNeeded())
1342 in.objCImageInfo->finalizeContents();
1349 // yet create branch-range extension thunks for architectures whose
1358 if (LLVM_ENABLE_THREADS && config->timeTraceEnabled)
1359 timeTraceProfilerInitialize(config->timeTraceGranularity, "writeMapFile");
1361 if (LLVM_ENABLE_THREADS && config->timeTraceEnabled)
1376 if (config->dedupStrings)
1384 if (config->emitChainedFixups) {
1406 uint8_t *arr = bAlloc().Allocate<uint8_t>(target->wordSize);
1407 memset(arr, 0, target->wordSize);
1410 ArrayRef<uint8_t>{arr, target->wordSize},
1411 /*align=*/target->wordSize);
1412 assert(in.imageLoaderCache->live);