45#define DEBUG_TYPE "mc"
49 IndirectSymBase.clear();
50 IndirectSymbols.clear();
52 SectionAddress.
clear();
55 LocalSymbolData.clear();
56 ExternalSymbolData.clear();
57 UndefinedSymbolData.clear();
59 VersionInfo.Major = 0;
61 TargetVariantVersionInfo.Major = 0;
63 LinkerOptions.clear();
74 if (cast<MCSymbolMachO>(S).isWeakDefinition())
81bool MachObjectWriter::
82MachSymbolData::operator<(
const MachSymbolData &RHS)
const {
83 return Symbol->getName() <
RHS.Symbol->
getName();
97 Asm.getFragmentOffset(*Fragment);
106 return C->getValue();
114 if (
Target.getSymA() &&
Target.getSymA()->getSymbol().isUndefined())
117 if (
Target.getSymB() &&
Target.getSymB()->getSymbol().isUndefined())
130 Asm.getSymbolOffset(S);
136 unsigned Next = cast<MCSectionMachO>(Sec)->getLayoutOrder() + 1;
137 if (Next >= SectionOrder.
size())
140 const MCSection &NextSec = *SectionOrder[Next];
148 if (!Symbol.isTemporary())
151 if (Symbol.isUsedInReloc())
177 unsigned NumLoadCommands,
178 unsigned LoadCommandsSize,
179 bool SubsectionsViaSymbols) {
195 uint32_t Cpusubtype = TargetObjectWriter->getCPUSubtype();
238 unsigned SegmentLoadCommandSize =
246 writeWithPadding(
Name, 16);
270 uint64_t FileOffset,
unsigned Flags,
272 unsigned NumRelocations) {
273 uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
277 if (Section.isVirtualSection()) {
278 assert(Asm.getSectionFileSize(Sec) == 0 &&
"Invalid file size!");
288 writeWithPadding(Section.getName(), 16);
289 writeWithPadding(Section.getSegmentName(), 16);
297 assert(isUInt<32>(FileOffset) &&
"Cannot encode offset of section");
301 assert((!NumRelocations || isUInt<32>(RelocationsStart)) &&
302 "Cannot encode offset of relocations");
371MachObjectWriter::MachSymbolData *
372MachObjectWriter::findSymbolData(
const MCSymbol &
Sym) {
373 for (
auto *SymbolData :
374 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
375 for (MachSymbolData &Entry : *SymbolData)
376 if (Entry.Symbol == &
Sym)
386 const auto *
Ref = dyn_cast<MCSymbolRefExpr>(
Value);
389 S = &
Ref->getSymbol();
395 const MCSymbol *Symbol = MSD.Symbol;
396 const auto &
Data = cast<MCSymbolMachO>(*Symbol);
398 uint8_t SectionIndex = MSD.SectionIndex;
401 bool IsAlias = Symbol != AliasedSymbol;
403 const MCSymbol &OrigSymbol = *Symbol;
404 MachSymbolData *AliaseeInfo;
406 AliaseeInfo = findSymbolData(*AliasedSymbol);
408 SectionIndex = AliaseeInfo->SectionIndex;
409 Symbol = AliasedSymbol;
416 if (IsAlias && Symbol->isUndefined())
418 else if (Symbol->isUndefined())
420 else if (Symbol->isAbsolute())
427 if (
Data.isPrivateExtern())
431 if (
Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
435 if (IsAlias && Symbol->isUndefined())
436 Address = AliaseeInfo->StringIndex;
437 else if (Symbol->isDefined())
439 else if (Symbol->isCommon()) {
442 Address = Symbol->getCommonSize();
453 bool EncodeAsAltEntry =
454 IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry();
455 W.
write<
uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry));
480 for (
const std::string &Option :
Options)
481 Size += Option.size() + 1;
486 const std::vector<std::string> &
Options)
496 for (
const std::string &Option :
Options) {
498 W.
OS << Option <<
'\0';
499 BytesWritten += Option.size() + 1;
522 Asm.getContext().reportError(
Fixup.getLoc(),
523 "unsupported relocation expression");
527 TargetObjectWriter->recordRelocation(
this, Asm, Fragment,
Fixup,
Target,
541 for (IndirectSymbolData &ISD : IndirectSymbols) {
542 const MCSectionMachO &Section = cast<MCSectionMachO>(*ISD.Section);
550 "' not in a symbol pointer or stub section");
555 for (
auto [IndirectIndex, ISD] :
enumerate(IndirectSymbols)) {
556 const auto &Section = cast<MCSectionMachO>(*ISD.Section);
563 IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));
565 Asm.registerSymbol(*ISD.Symbol);
569 for (
auto [IndirectIndex, ISD] :
enumerate(IndirectSymbols)) {
570 const auto &Section = cast<MCSectionMachO>(*ISD.Section);
577 IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));
582 if (Asm.registerSymbol(*ISD.Symbol))
583 cast<MCSymbolMachO>(ISD.Symbol)->setReferenceTypeUndefinedLazy(
true);
589 MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
590 std::vector<MachSymbolData> &ExternalSymbolData,
591 std::vector<MachSymbolData> &UndefinedSymbolData) {
596 SectionIndexMap[&Sec] =
Index++;
600 for (
const MCSymbol &Symbol : Asm.symbols()) {
604 StringTable.
add(Symbol.getName());
613 for (
const MCSymbol &Symbol : Asm.symbols()) {
618 if (!Symbol.isExternal() && !Symbol.isUndefined())
622 MSD.Symbol = &Symbol;
623 MSD.StringIndex = StringTable.
getOffset(Symbol.getName());
625 if (Symbol.isUndefined()) {
626 MSD.SectionIndex = 0;
627 UndefinedSymbolData.push_back(MSD);
628 }
else if (Symbol.isAbsolute()) {
629 MSD.SectionIndex = 0;
630 ExternalSymbolData.push_back(MSD);
632 MSD.SectionIndex = SectionIndexMap.
lookup(&Symbol.getSection());
633 assert(MSD.SectionIndex &&
"Invalid section index!");
634 ExternalSymbolData.push_back(MSD);
639 for (
const MCSymbol &Symbol : Asm.symbols()) {
644 if (Symbol.isExternal() || Symbol.isUndefined())
648 MSD.Symbol = &Symbol;
649 MSD.StringIndex = StringTable.
getOffset(Symbol.getName());
651 if (Symbol.isAbsolute()) {
652 MSD.SectionIndex = 0;
653 LocalSymbolData.push_back(MSD);
655 MSD.SectionIndex = SectionIndexMap.
lookup(&Symbol.getSection());
656 assert(MSD.SectionIndex &&
"Invalid section index!");
657 LocalSymbolData.push_back(MSD);
667 for (
auto *SymbolData :
668 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
669 for (MachSymbolData &Entry : *SymbolData)
670 Entry.Symbol->setIndex(
Index++);
673 for (RelAndSymbol &Rel : Relocations[&Section]) {
678 unsigned Index = Rel.Sym->getIndex();
681 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) |
Index | (1 << 27);
683 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) |
Index << 8 | (1 << 4);
693 if (!Sec.isVirtualSection()) {
695 cast<MCSectionMachO>(Sec).setLayoutOrder(i++);
699 if (Sec.isVirtualSection()) {
701 cast<MCSectionMachO>(Sec).setLayoutOrder(i++);
706 for (
const MCSection *Sec : SectionOrder) {
707 StartAddress =
alignTo(StartAddress, Sec->getAlign());
708 SectionAddress[Sec] = StartAddress;
709 StartAddress += Asm.getSectionAddressSize(*Sec);
727 bool InSet,
bool IsPCRel)
const {
754 bool hasReliableSymbolDifference =
isX86_64();
755 if (!hasReliableSymbolDifference) {
784 Asm.getContext().getObjectFileInfo()->getAddrSigSection();
785 unsigned Log2Size =
is64Bit() ? 3 : 2;
787 if (!S->isRegistered())
798 auto NumBytesWritten = [&] {
return W.
OS.
tell() - StartOffset; };
804 UndefinedSymbolData);
807 MCSection *CGProfileSection = Asm.getContext().getMachOSection(
809 auto &Frag = cast<MCDataFragment>(*CGProfileSection->
begin());
810 Frag.getContents().clear();
813 uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
814 uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
821 unsigned NumSections = Asm.end() - Asm.begin();
825 unsigned NumLoadCommands = 1;
831 if (VersionInfo.Major != 0) {
833 if (VersionInfo.EmitBuildVersion)
840 if (TargetVariantVersionInfo.Major != 0) {
842 assert(TargetVariantVersionInfo.EmitBuildVersion &&
843 "target variant should use build version");
848 unsigned NumDataRegions = DataRegions.size();
849 if (NumDataRegions) {
863 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
864 UndefinedSymbolData.size();
866 NumLoadCommands += 2;
872 for (
const auto &Option : LinkerOptions) {
887 uint64_t FileSize = Asm.getSectionFileSize(Sec);
892 if (Sec.isVirtualSection())
895 SectionDataSize = std::max(SectionDataSize,
Address +
Size);
896 SectionDataFileSize = std::max(SectionDataFileSize,
Address + FileSize);
902 unsigned SectionDataPadding =
904 SectionDataFileSize += SectionDataPadding;
912 SectionDataSize, Prot, Prot);
915 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
917 const auto &Sec = cast<MCSectionMachO>(Section);
918 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
919 unsigned NumRelocs = Relocs.size();
921 unsigned Flags = Sec.getTypeAndAttributes();
922 if (Sec.hasInstructions())
924 if (!cast<MCSectionMachO>(Sec).isVirtualSection() &&
925 !isUInt<32>(SectionStart)) {
926 Asm.getContext().reportError(
927 SMLoc(),
"cannot encode offset of section; object file too large");
928 return NumBytesWritten();
930 if (NumRelocs && !isUInt<32>(RelocTableEnd)) {
931 Asm.getContext().reportError(
933 "cannot encode offset of relocations; object file too large");
934 return NumBytesWritten();
937 RelocTableEnd, NumRelocs);
942 auto EmitDeploymentTargetVersion =
945 assert(!V.empty() &&
"empty version");
946 unsigned Update = V.getSubminor().value_or(0);
947 unsigned Minor = V.getMinor().value_or(0);
948 assert(
Update < 256 &&
"unencodable update target version");
949 assert(
Minor < 256 &&
"unencodable minor target version");
950 assert(V.getMajor() < 65536 &&
"unencodable major target version");
951 return Update | (
Minor << 8) | (V.getMajor() << 16);
954 VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update));
956 ? EncodeVersion(VersionInfo.SDKVersion)
958 if (VersionInfo.EmitBuildVersion) {
975 if (VersionInfo.Major != 0)
976 EmitDeploymentTargetVersion(VersionInfo);
977 if (TargetVariantVersionInfo.Major != 0)
978 EmitDeploymentTargetVersion(TargetVariantVersionInfo);
981 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
982 if (NumDataRegions) {
983 uint64_t DataRegionsOffset = RelocTableEnd;
984 uint64_t DataRegionsSize = NumDataRegions * 8;
990 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
993 DataInCodeTableEnd, LOHSize);
997 unsigned FirstLocalSymbol = 0;
998 unsigned NumLocalSymbols = LocalSymbolData.size();
999 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
1000 unsigned NumExternalSymbols = ExternalSymbolData.size();
1001 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
1002 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
1003 unsigned NumIndirectSymbols = IndirectSymbols.size();
1004 unsigned NumSymTabSymbols =
1005 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
1006 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
1010 if (NumIndirectSymbols)
1011 IndirectSymbolOffset = LOHTableEnd;
1014 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
1018 SymbolTableOffset + NumSymTabSymbols * (
is64Bit() ?
1022 StringTableOffset, StringTable.
getSize());
1025 FirstExternalSymbol, NumExternalSymbols,
1026 FirstUndefinedSymbol, NumUndefinedSymbols,
1027 IndirectSymbolOffset, NumIndirectSymbols);
1031 for (
const auto &Option : LinkerOptions)
1036 Asm.writeSectionData(
W.
OS, &Sec);
1049 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
1066 <<
" start: " << Start <<
"(" <<
Data.Start->getName()
1067 <<
")" <<
" end: " <<
End <<
"(" <<
Data.End->getName()
1068 <<
")" <<
" size: " <<
End - Start <<
"\n");
1079 LOHContainer.
emit(Asm, *
this);
1089 for (
auto &ISD : IndirectSymbols) {
1096 if (ISD.Symbol->isDefined() && !ISD.Symbol->isExternal()) {
1098 if (ISD.Symbol->isAbsolute())
1111 for (
auto *SymbolData :
1112 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
1113 for (MachSymbolData &Entry : *SymbolData)
1120 return NumBytesWritten();
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
Base class for the full range of assembler expressions which are needed for parsing.
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) 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)...
const MCSymbol * getAtom() const
MCSection * getParent() const
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.
SmallVector< CGProfileEntry, 0 > CGProfile
bool SubsectionsViaSymbols
std::vector< const MCSymbol * > & getAddrsigSyms()
virtual void reset()
lifetime management
This represents a section on a Mach-O system (used by Mac OS X).
Instances of this class represent a uniqued identifier for a section in the current translation unit.
bool isVirtualSection() const
Check whether this section is "virtual", that is has no actual object file contents.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
StringRef getName() const
getName - Get the symbol name.
bool isVariable() const
isVariable - Check if this is a variable symbol.
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
MCFragment * getFragment(bool SetUsed=true) const
This represents an "assembler immediate".
uint64_t getPaddingSize(const MCAssembler &Asm, const MCSection *SD) const
void computeSectionAddresses(const MCAssembler &Asm)
void writeSection(const MCAssembler &Asm, const MCSection &Sec, uint64_t VMAddr, uint64_t FileOffset, unsigned Flags, uint64_t RelocationsStart, unsigned NumRelocations)
bool doesSymbolRequireExternRelocation(const MCSymbol &S)
void computeSymbolTable(MCAssembler &Asm, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
Compute the symbol table data.
uint64_t getFragmentAddress(const MCAssembler &Asm, const MCFragment *Fragment) const
{ bool EmitBuildVersion VersionInfoType
void executePostLayoutBinding(MCAssembler &Asm) override
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
uint64_t getSectionAddress(const MCSection *Sec) const
void addRelocation(const MCSymbol *RelSymbol, const MCSection *Sec, MachO::any_relocation_info &MRE)
void populateAddrSigSection(MCAssembler &Asm)
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind)
support::endian::Writer W
void writeLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
void writeNlist(MachSymbolData &MSD, const MCAssembler &Asm)
VersionTuple SDKVersion
An optional version of the SDK that was used to build the source.
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const override
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
uint64_t getSymbolAddress(const MCSymbol &S, const MCAssembler &Asm) 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 reset() override
lifetime management
void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
void bindIndirectSymbols(MCAssembler &Asm)
uint64_t writeObject(MCAssembler &Asm) override
Write the object file and returns the number of bytes written.
Represents a location in source code.
static SectionKind getMetadata()
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
void write(raw_ostream &OS) const
size_t add(CachedHashStringRef S)
Add a string to the builder.
void finalize()
Analyze the strings and build the final table.
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.
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.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#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.
unsigned 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
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
MCFixupKind
Extensible enumeration to represent the type of a fixup.
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.
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.
Target independent information on a fixup kind.
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
unsigned Flags
Flags describing additional information on this fixup kind.
void write(ArrayRef< value_type > Val)