Lines Matching +full:write +full:- +full:to +full:- +full:write

1 //===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
72 // References to weak definitions require external relocation entries; the
83 return Symbol->getName() < RHS.Symbol->getName();
96 return getSectionAddress(Fragment->getParent()) +
106 return C->getValue();
109 if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Asm, nullptr))
110 report_fatal_error("unable to evaluate offset for variable '" +
114 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
115 report_fatal_error("unable to evaluate offset to undefined symbol '" +
116 Target.getSymA()->getSymbol().getName() + "'");
117 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
118 report_fatal_error("unable to evaluate offset to undefined symbol '" +
119 Target.getSymB()->getSymbol().getName() + "'");
123 Address += getSymbolAddress(Target.getSymA()->getSymbol(), Asm);
125 Address += getSymbolAddress(Target.getSymB()->getSymbol(), Asm);
129 return getSectionAddress(S.getFragment()->getParent()) +
136 unsigned Next = cast<MCSectionMachO>(Sec)->getLayoutOrder() + 1;
147 // Non-temporary labels should always be visible to the linker.
166 // Non-linker visible symbols in sections which can't be atomized have no
169 *S.getFragment()->getParent()))
173 return S.getFragment()->getAtom();
191 W.write<uint32_t>(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
193 W.write<uint32_t>(TargetObjectWriter->getCPUType());
194 W.write<uint32_t>(TargetObjectWriter->getCPUSubtype());
196 W.write<uint32_t>(Type);
197 W.write<uint32_t>(NumLoadCommands);
198 W.write<uint32_t>(LoadCommandsSize);
199 W.write<uint32_t>(Flags);
201 W.write<uint32_t>(0); // reserved
203 assert(W.OS.tell() - Start == (is64Bit() ? sizeof(MachO::mach_header_64)
210 W.OS.write_zeros(Size - Str.size());
213 /// writeSegmentLoadCommand - Write a segment load command.
230 W.write<uint32_t>(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
231 W.write<uint32_t>(SegmentLoadCommandSize +
237 W.write<uint64_t>(VMAddr); // vmaddr
238 W.write<uint64_t>(VMSize); // vmsize
239 W.write<uint64_t>(SectionDataStartOffset); // file offset
240 W.write<uint64_t>(SectionDataSize); // file size
242 W.write<uint32_t>(VMAddr); // vmaddr
243 W.write<uint32_t>(VMSize); // vmsize
244 W.write<uint32_t>(SectionDataStartOffset); // file offset
245 W.write<uint32_t>(SectionDataSize); // file size
248 W.write<uint32_t>(MaxProt);
250 W.write<uint32_t>(InitProt);
251 W.write<uint32_t>(NumSections);
252 W.write<uint32_t>(0); // flags
254 assert(W.OS.tell() - Start == SegmentLoadCommandSize);
280 W.write<uint64_t>(VMAddr); // address
281 W.write<uint64_t>(SectionSize); // size
283 W.write<uint32_t>(VMAddr); // address
284 W.write<uint32_t>(SectionSize); // size
287 W.write<uint32_t>(FileOffset);
289 W.write<uint32_t>(Log2(Section.getAlign()));
292 W.write<uint32_t>(NumRelocations ? RelocationsStart : 0);
293 W.write<uint32_t>(NumRelocations);
294 W.write<uint32_t>(Flags);
295 W.write<uint32_t>(IndirectSymBase.lookup(&Sec)); // reserved1
296 W.write<uint32_t>(Section.getStubSize()); // reserved2
298 W.write<uint32_t>(0); // reserved3
300 assert(W.OS.tell() - Start ==
313 W.write<uint32_t>(MachO::LC_SYMTAB);
314 W.write<uint32_t>(sizeof(MachO::symtab_command));
315 W.write<uint32_t>(SymbolOffset);
316 W.write<uint32_t>(NumSymbols);
317 W.write<uint32_t>(StringTableOffset);
318 W.write<uint32_t>(StringTableSize);
320 assert(W.OS.tell() - Start == sizeof(MachO::symtab_command));
336 W.write<uint32_t>(MachO::LC_DYSYMTAB);
337 W.write<uint32_t>(sizeof(MachO::dysymtab_command));
338 W.write<uint32_t>(FirstLocalSymbol);
339 W.write<uint32_t>(NumLocalSymbols);
340 W.write<uint32_t>(FirstExternalSymbol);
341 W.write<uint32_t>(NumExternalSymbols);
342 W.write<uint32_t>(FirstUndefinedSymbol);
343 W.write<uint32_t>(NumUndefinedSymbols);
344 W.write<uint32_t>(0); // tocoff
345 W.write<uint32_t>(0); // ntoc
346 W.write<uint32_t>(0); // modtaboff
347 W.write<uint32_t>(0); // nmodtab
348 W.write<uint32_t>(0); // extrefsymoff
349 W.write<uint32_t>(0); // nextrefsyms
350 W.write<uint32_t>(IndirectSymbolOffset);
351 W.write<uint32_t>(NumIndirectSymbols);
352 W.write<uint32_t>(0); // extreloff
353 W.write<uint32_t>(0); // nextrel
354 W.write<uint32_t>(0); // locreloff
355 W.write<uint32_t>(0); // nlocrel
357 assert(W.OS.tell() - Start == sizeof(MachO::dysymtab_command));
373 while (S->isVariable()) {
374 const MCExpr *Value = S->getVariableValue();
378 S = &Ref->getSymbol();
397 SectionIndex = AliaseeInfo->SectionIndex;
402 // Set the N_TYPE bits. See <mach-o/nlist.h>.
405 if (IsAlias && Symbol->isUndefined())
407 else if (Symbol->isUndefined())
409 else if (Symbol->isAbsolute())
420 if (Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
424 if (IsAlias && Symbol->isUndefined())
425 Address = AliaseeInfo->StringIndex;
426 else if (Symbol->isDefined())
428 else if (Symbol->isCommon()) {
431 Address = Symbol->getCommonSize();
436 W.write<uint32_t>(MSD.StringIndex);
440 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
444 W.write<uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry));
446 W.write<uint64_t>(Address);
448 W.write<uint32_t>(Address);
457 W.write<uint32_t>(Type);
458 W.write<uint32_t>(sizeof(MachO::linkedit_data_command));
459 W.write<uint32_t>(DataOffset);
460 W.write<uint32_t>(DataSize);
462 assert(W.OS.tell() - Start == sizeof(MachO::linkedit_data_command));
481 W.write<uint32_t>(MachO::LC_LINKER_OPTION);
482 W.write<uint32_t>(Size);
483 W.write<uint32_t>(Options.size());
486 // Write each string, including the null byte.
491 // Pad to a multiple of the pointer size.
495 assert(W.OS.tell() - Start == Size);
499 // Target is (LHS - RHS + cst).
500 // We don't support the form where LHS is null: -RHS + cst
516 TargetObjectWriter->recordRelocation(this, Asm, Fragment, Fixup, Target,
522 // (in the following two passes). It would be easier for us to do this sooner
543 // Bind non-lazy symbol pointers first.
568 // Set the symbol type to undefined lazy, but only on construction.
572 cast<MCSymbolMachO>(ISD.Symbol)->setReferenceTypeUndefinedLazy(true);
576 /// computeSymbolTable - Compute the symbol table data
597 // Build the symbol arrays but only for non-local symbols.
599 // The particular order that we collect and then sort the symbols is chosen to
603 // Ignore non-linker visible symbols.
629 // Ignore non-linker visible symbols.
650 // External and undefined symbols are required to be in lexicographic order.
659 Entry.Symbol->setIndex(Index++);
667 unsigned Index = Rel.Sym->getIndex();
678 // Assign layout order indices to sections.
696 StartAddress = alignTo(StartAddress, Sec->getAlign());
700 // Explicitly pad the section to match the alignment requirements of the
722 // - addr(atom(B)) - offset(B)
724 // addr(atom(A)) - addr(atom(B)) == 0.
730 // The simple (Darwin, except on x86_64) way of dealing with this was to
731 // assume that any reference to a temporary symbol *must* be a temporary
733 // relocation to a temporary symbol (in the same section) is fully
735 // requires the compiler to use .set to absolutize the differences between
736 // symbols which the compiler knows to be assembly time constants, so we
737 // don't need to worry about considering symbol differences fully resolved.
739 // If the file isn't using sub-sections-via-symbols, we can make the
746 (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() &&
757 // If the atoms are the same, they are guaranteed to have the same address.
758 return SA.getFragment()->getAtom() == FB.getAtom();
773 Asm.getContext().getObjectFileInfo()->getAddrSigSection();
776 if (!S->isRegistered())
787 auto NumBytesWritten = [&] { return W.OS.tell() - StartOffset; };
798 auto &Frag = cast<MCDataFragment>(*CGProfileSection->begin());
802 uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
803 uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
804 support::endian::write(OS, FromIndex, W.Endian);
805 support::endian::write(OS, ToIndex, W.Endian);
806 support::endian::write(OS, CGPE.Count, W.Endian);
810 unsigned NumSections = Asm.end() - Asm.begin();
836 // Add the data-in-code load command size, if used.
888 // The section data is padded to pointer size bytes.
895 // Write the prolog, starting with the header and load command...
930 // Write out the deployment target information, if it's available.
933 auto EncodeVersion = [](VersionTuple V) -> uint32_t {
949 W.write<uint32_t>(MachO::LC_BUILD_VERSION);
950 W.write<uint32_t>(sizeof(MachO::build_version_command));
951 W.write<uint32_t>(VersionInfo.TypeOrPlatform.Platform);
952 W.write<uint32_t>(EncodedVersion);
953 W.write<uint32_t>(SDKVersion);
954 W.write<uint32_t>(0); // Empty tools list.
958 W.write<uint32_t>(LCType);
959 W.write<uint32_t>(sizeof(MachO::version_min_command));
960 W.write<uint32_t>(EncodedVersion);
961 W.write<uint32_t>(SDKVersion);
969 // Write the data-in-code load command, if used.
978 // Write the loh load command, if used.
984 // Write the symbol table load command, if used.
1019 // Write the linker options load commands.
1023 // Write the actual section data.
1031 // Write the extra padding.
1034 // Write the relocation entries.
1036 // Write the section relocation entries, in reverse order to match 'as'
1040 W.write<uint32_t>(Rel.MRE.r_word0);
1041 W.write<uint32_t>(Rel.MRE.r_word1);
1045 // Write out the data-in-code region payload, if there is one.
1054 LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data.Kind
1055 << " start: " << Start << "(" << Data.Start->getName()
1056 << ")" << " end: " << End << "(" << Data.End->getName()
1057 << ")" << " size: " << End - Start << "\n");
1058 W.write<uint32_t>(Start);
1059 W.write<uint16_t>(End - Start);
1060 W.write<uint16_t>(Data.Kind);
1063 // Write out the loh commands, if there is one.
1069 // Pad to a multiple of the pointer size.
1072 assert(W.OS.tell() - Start == LOHSize);
1075 // Write the symbol table data, if used.
1077 // Write the indirect symbol entries.
1079 // Indirect symbols in the non-lazy symbol pointer section have some
1085 if (ISD.Symbol->isDefined() && !ISD.Symbol->isExternal()) {
1087 if (ISD.Symbol->isAbsolute())
1089 W.write<uint32_t>(Flags);
1094 W.write<uint32_t>(ISD.Symbol->getIndex());
1099 // Write the symbol table entries.
1105 // Write the string table.
1106 StringTable.write(W.OS);