41#define DEBUG_TYPE "mc"
45 IndirectSymBase.clear();
46 IndirectSymbols.clear();
48 SectionAddress.clear();
51 LocalSymbolData.clear();
52 ExternalSymbolData.clear();
53 UndefinedSymbolData.clear();
55 VersionInfo.Major = 0;
57 TargetVariantVersionInfo.Major = 0;
59 LinkerOptions.clear();
65 TargetObjectWriter->setAssembler(
Asm);
82bool MachObjectWriter::
83MachSymbolData::operator<(
const MachSymbolData &RHS)
const {
84 return Symbol->getName() <
RHS.Symbol->
getName();
107 if (
Target.getAddSym() &&
Target.getAddSym()->isUndefined())
110 if (
Target.getSubSym() &&
Target.getSubSym()->isUndefined())
131 if (Next >= SectionOrder.
size())
134 const MCSection &NextSec = *SectionOrder[Next];
142 if (!Symbol.isTemporary())
145 if (Symbol.isUsedInReloc())
171 unsigned NumLoadCommands,
172 unsigned LoadCommandsSize,
173 bool SubsectionsViaSymbols) {
189 uint32_t Cpusubtype = TargetObjectWriter->getCPUSubtype();
232 unsigned SegmentLoadCommandSize =
240 writeWithPadding(
Name, 16);
264 uint64_t FileOffset,
unsigned Flags,
266 unsigned NumRelocations) {
279 writeWithPadding(Sec.
getName(), 16);
288 assert(isUInt<32>(FileOffset) &&
"Cannot encode offset of section");
292 assert((!NumRelocations || isUInt<32>(RelocationsStart)) &&
293 "Cannot encode offset of relocations");
362MachObjectWriter::MachSymbolData *
363MachObjectWriter::findSymbolData(
const MCSymbol &
Sym) {
364 for (
auto *SymbolData :
365 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
366 for (MachSymbolData &Entry : *SymbolData)
367 if (Entry.Symbol == &
Sym)
377 const auto *
Ref = dyn_cast<MCSymbolRefExpr>(
Value);
380 S = &
Ref->getSymbol();
386 auto *Symbol = MSD.Symbol;
388 auto *AliasedSymbol =
390 uint8_t SectionIndex = MSD.SectionIndex;
393 bool IsAlias = Symbol != AliasedSymbol;
396 MachSymbolData *AliaseeInfo;
398 AliaseeInfo = findSymbolData(*AliasedSymbol);
400 SectionIndex = AliaseeInfo->SectionIndex;
401 Symbol = AliasedSymbol;
408 if (IsAlias && Symbol->isUndefined())
410 else if (Symbol->isUndefined())
412 else if (Symbol->isAbsolute())
419 if (
Data.isPrivateExtern())
423 if (
Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
427 if (IsAlias && Symbol->isUndefined())
428 Address = AliaseeInfo->StringIndex;
429 else if (Symbol->isDefined())
431 else if (Symbol->isCommon()) {
434 Address = Symbol->getCommonSize();
445 bool EncodeAsAltEntry = IsAlias && OrigSymbol.
isAltEntry();
471 for (
const std::string &Option :
Options)
472 Size += Option.size() + 1;
477 const std::vector<std::string> &
Options)
487 for (
const std::string &Option :
Options) {
489 W.
OS << Option <<
'\0';
490 BytesWritten += Option.size() + 1;
513 "unsupported relocation expression");
531 for (IndirectSymbolData &ISD : IndirectSymbols) {
540 "' not in a symbol pointer or stub section");
545 for (
auto [IndirectIndex, ISD] :
enumerate(IndirectSymbols)) {
546 const auto &Section =
static_cast<MCSectionMachO &
>(*ISD.Section);
553 IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));
559 for (
auto [IndirectIndex, ISD] :
enumerate(IndirectSymbols)) {
560 const auto &Section =
static_cast<MCSectionMachO &
>(*ISD.Section);
567 IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));
580 MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
581 std::vector<MachSymbolData> &ExternalSymbolData,
582 std::vector<MachSymbolData> &UndefinedSymbolData) {
587 SectionIndexMap[&Sec] = Index++;
588 assert(Index <= 256 &&
"Too many sections!");
607 if (!
Sym.isSymbolLinkerVisible())
610 if (!
Sym.isExternal() && !
Sym.isUndefined())
615 MSD.StringIndex =
StringTable.getOffset(Symbol.getName());
617 if (Symbol.isUndefined()) {
618 MSD.SectionIndex = 0;
619 UndefinedSymbolData.push_back(MSD);
620 }
else if (Symbol.isAbsolute()) {
621 MSD.SectionIndex = 0;
622 ExternalSymbolData.push_back(MSD);
624 MSD.SectionIndex = SectionIndexMap.
lookup(&Symbol.getSection());
625 assert(MSD.SectionIndex &&
"Invalid section index!");
626 ExternalSymbolData.push_back(MSD);
634 if (!
Sym.isSymbolLinkerVisible())
637 if (
Sym.isExternal() ||
Sym.isUndefined())
642 MSD.StringIndex =
StringTable.getOffset(Symbol.getName());
644 if (Symbol.isAbsolute()) {
645 MSD.SectionIndex = 0;
646 LocalSymbolData.push_back(MSD);
648 MSD.SectionIndex = SectionIndexMap.
lookup(&Symbol.getSection());
649 assert(MSD.SectionIndex &&
"Invalid section index!");
650 LocalSymbolData.push_back(MSD);
660 for (
auto *SymbolData :
661 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
662 for (MachSymbolData &Entry : *SymbolData)
663 Entry.Symbol->setIndex(Index++);
666 for (RelAndSymbol &Rel : Relocations[&Section]) {
671 unsigned Index = Rel.Sym->getIndex();
674 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27);
676 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4);
686 if (!Sec.isBssSection()) {
692 if (Sec.isBssSection()) {
699 for (
const MCSection *Sec : SectionOrder) {
700 StartAddress =
alignTo(StartAddress, Sec->getAlign());
701 SectionAddress[Sec] = StartAddress;
720 bool IsPCRel)
const {
747 bool hasReliableSymbolDifference =
isX86_64();
748 if (!hasReliableSymbolDifference) {
778 unsigned Log2Size =
is64Bit() ? 3 : 2;
780 if (!S->isRegistered())
792 auto NumBytesWritten = [&] {
return W.
OS.
tell() - StartOffset; };
798 UndefinedSymbolData);
804 uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
805 uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
819 unsigned NumLoadCommands = 1;
825 if (VersionInfo.Major != 0) {
827 if (VersionInfo.EmitBuildVersion)
834 if (TargetVariantVersionInfo.Major != 0) {
836 assert(TargetVariantVersionInfo.EmitBuildVersion &&
837 "target variant should use build version");
842 unsigned NumDataRegions = DataRegions.size();
843 if (NumDataRegions) {
857 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
858 UndefinedSymbolData.size();
860 NumLoadCommands += 2;
866 for (
const auto &Option : LinkerOptions) {
886 if (Sec.isBssSection())
889 SectionDataSize = std::max(SectionDataSize,
Address +
Size);
890 SectionDataFileSize = std::max(SectionDataFileSize,
Address + FileSize);
896 unsigned SectionDataPadding =
898 SectionDataFileSize += SectionDataPadding;
906 SectionDataSize, Prot, Prot);
909 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
912 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
913 unsigned NumRelocs = Relocs.size();
915 unsigned Flags = Sec.getTypeAndAttributes();
916 if (Sec.hasInstructions())
918 if (!cast<MCSectionMachO>(Sec).isBssSection() &&
919 !isUInt<32>(SectionStart)) {
921 SMLoc(),
"cannot encode offset of section; object file too large");
922 return NumBytesWritten();
924 if (NumRelocs && !isUInt<32>(RelocTableEnd)) {
927 "cannot encode offset of relocations; object file too large");
928 return NumBytesWritten();
931 RelocTableEnd, NumRelocs);
936 auto EmitDeploymentTargetVersion =
939 assert(!V.empty() &&
"empty version");
940 unsigned Update = V.getSubminor().value_or(0);
941 unsigned Minor = V.getMinor().value_or(0);
942 assert(
Update < 256 &&
"unencodable update target version");
943 assert(
Minor < 256 &&
"unencodable minor target version");
944 assert(V.getMajor() < 65536 &&
"unencodable major target version");
945 return Update | (
Minor << 8) | (V.getMajor() << 16);
948 VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update));
950 ? EncodeVersion(VersionInfo.SDKVersion)
952 if (VersionInfo.EmitBuildVersion) {
969 if (VersionInfo.Major != 0)
970 EmitDeploymentTargetVersion(VersionInfo);
971 if (TargetVariantVersionInfo.Major != 0)
972 EmitDeploymentTargetVersion(TargetVariantVersionInfo);
975 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
976 if (NumDataRegions) {
977 uint64_t DataRegionsOffset = RelocTableEnd;
978 uint64_t DataRegionsSize = NumDataRegions * 8;
984 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
987 DataInCodeTableEnd, LOHSize);
991 unsigned FirstLocalSymbol = 0;
992 unsigned NumLocalSymbols = LocalSymbolData.size();
993 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
994 unsigned NumExternalSymbols = ExternalSymbolData.size();
995 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
996 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
997 unsigned NumIndirectSymbols = IndirectSymbols.size();
998 unsigned NumSymTabSymbols =
999 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
1000 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
1004 if (NumIndirectSymbols)
1005 IndirectSymbolOffset = LOHTableEnd;
1008 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
1012 SymbolTableOffset + NumSymTabSymbols * (
is64Bit() ?
1019 FirstExternalSymbol, NumExternalSymbols,
1020 FirstUndefinedSymbol, NumUndefinedSymbols,
1021 IndirectSymbolOffset, NumIndirectSymbols);
1025 for (
const auto &Option : LinkerOptions)
1043 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
1060 <<
" start: " << Start <<
"(" <<
Data.Start->getName()
1061 <<
")" <<
" end: " <<
End <<
"(" <<
Data.End->getName()
1062 <<
")" <<
" size: " <<
End - Start <<
"\n");
1073 LOHContainer.
emit(
Asm, *
this);
1083 for (
auto &ISD : IndirectSymbols) {
1090 if (ISD.Symbol->isDefined() && !ISD.Symbol->isExternal()) {
1092 if (ISD.Symbol->isAbsolute())
1105 for (
auto *SymbolData :
1106 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
1107 for (MachSymbolData &Entry : *SymbolData)
1114 return NumBytesWritten();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
static unsigned ComputeLinkerOptionsLoadCommandSize(const std::vector< std::string > &Options, bool is64Bit)
static bool isFixupTargetValid(const MCValue &Target)
static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type)
static bool isSymbolLinkerVisible(const MCSymbol &Symbol)
PowerPC TLS Dynamic Call Fixup
static bool is64Bit(const char *name)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
static bool isSectionAtomizableBySymbols(const MCSection &Section)
True if the section is atomized using the symbols in it.
const_iterator begin() const
LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
LLVM_ABI uint64_t getSectionAddressSize(const MCSection &Sec) const
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
const_iterator end() const
iterator_range< pointee_iterator< typename SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
LLVM_ABI uint64_t getSectionFileSize(const MCSection &Sec) const
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
uint64_t getFragmentOffset(const MCFragment &F) const
const MCObjectFileInfo * getObjectFileInfo() const
LLVM_ABI MCSectionMachO * getMachOSection(StringRef Segment, StringRef Section, unsigned TypeAndAttributes, unsigned Reserved2, SectionKind K, const char *BeginSymName=nullptr)
Return the MCSection for the specified mach-o section.
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Base class for the full range of assembler expressions which are needed for parsing.
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
LLVM_ABI const MCSymbol * getAtom() const
MCSection * getParent() const
MutableArrayRef< char > getVarContents()
void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const
Emit all Linker Optimization Hint in one big table.
uint64_t getEmitSize(const MCAssembler &Asm, const MachObjectWriter &ObjWriter) const
Get the size of the directives if emitted.
MCSection * getAddrSigSection() const
SmallVector< CGProfileEntry, 0 > CGProfile
virtual void setAssembler(MCAssembler *A)
bool SubsectionsViaSymbols
std::vector< const MCSymbol * > & getAddrsigSyms()
virtual void reset()
lifetime management
MCContext & getContext() const
This represents a section on a Mach-O system (used by Mac OS X).
StringRef getSegmentName() const
unsigned getStubSize() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
bool isBssSection() const
Check whether this section is "virtual", that is has no actual object file contents.
StringRef getName() const
FragList * curFragList() const
void setReferenceTypeUndefinedLazy(bool Value) const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
StringRef getName() const
getName - Get the symbol name.
bool isVariable() const
isVariable - Check if this is a variable symbol.
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
MCFragment * getFragment() const
uint64_t getPaddingSize(const MCAssembler &Asm, const MCSection *SD) const
void computeSectionAddresses(const MCAssembler &Asm)
bool doesSymbolRequireExternRelocation(const MCSymbol &S)
void recordRelocation(const MCFragment &F, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
void computeSymbolTable(MCAssembler &Asm, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
Compute the symbol table data.
uint64_t writeObject() override
Write the object file and returns the number of bytes written.
uint64_t getFragmentAddress(const MCAssembler &Asm, const MCFragment *Fragment) const
{ bool EmitBuildVersion VersionInfoType
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const override
uint64_t getSectionAddress(const MCSection *Sec) const
void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec, MachO::any_relocation_info &MRE)
void writeSection(const MCAssembler &Asm, const MCSectionMachO &Sec, uint64_t VMAddr, uint64_t FileOffset, unsigned Flags, uint64_t RelocationsStart, unsigned NumRelocations)
void populateAddrSigSection(MCAssembler &Asm)
support::endian::Writer W
void writeLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
void executePostLayoutBinding() override
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
void writeNlist(MachSymbolData &MSD, const MCAssembler &Asm)
VersionTuple SDKVersion
An optional version of the SDK that was used to build the source.
void writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, uint32_t NumLocalSymbols, uint32_t FirstExternalSymbol, uint32_t NumExternalSymbols, uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols, uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols)
const MCSymbol & findAliasedSymbol(const MCSymbol &Sym) const
MCVersionMinType Type
Used when EmitBuildVersion==false.
void writeSegmentLoadCommand(StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, uint32_t InitProt)
Write a segment load command.
const MCSymbol * getAtom(const MCSymbol &S) const
void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, uint32_t DataSize)
void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols)
void setAssembler(MCAssembler *Asm) override
void reset() override
lifetime management
void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
void bindIndirectSymbols(MCAssembler &Asm)
uint64_t getSymbolAddress(const MCSymbol &S) const
Represents a location in source code.
static SectionKind getMetadata()
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
A table of densely packed, null-terminated strings indexed by offset.
Target - Wrapper for Target specific information.
const char * getName() const
getName - Get the target name.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Represents a version number in the form major[.minor[.subminor[.build]]].
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
@ S_ATTR_SOME_INSTRUCTIONS
S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
@ S_THREAD_LOCAL_VARIABLE_POINTERS
S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread local structures.
@ S_LAZY_SYMBOL_POINTERS
S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
@ S_NON_LAZY_SYMBOL_POINTERS
S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
@ S_SYMBOL_STUBS
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
uint32_t CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION(unsigned PtrAuthABIVersion, bool PtrAuthKernelABIVersion)
@ MH_SUBSECTIONS_VIA_SYMBOLS
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
auto reverse(ContainerTy &&C)
void sort(IteratorTy Start, IteratorTy End)
@ MCVM_WatchOSVersionMin
.watchos_version_min
@ MCVM_OSXVersionMin
.macosx_version_min
@ MCVM_TvOSVersionMin
.tvos_version_min
@ MCVM_IOSVersionMin
.ios_version_min
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
@ Ref
The access may reference the value stored in memory.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
OutputIt copy(R &&Range, OutputIt Out)
unsigned Log2(Align A)
Returns the log2 of the alignment.
This struct is a compact representation of a valid (non-zero power of two) alignment.
void write(ArrayRef< value_type > Val)