42 #define DEBUG_TYPE "mc"
46 IndirectSymBase.clear();
48 LocalSymbolData.clear();
49 ExternalSymbolData.clear();
50 UndefinedSymbolData.clear();
61 if (cast<MCSymbolMachO>(S).isWeakDefinition())
70 return Symbol->getName() < RHS.Symbol->getName();
133 unsigned NumLoadCommands,
134 unsigned LoadCommandsSize,
135 bool SubsectionsViaSymbols) {
138 if (SubsectionsViaSymbols)
149 write32(TargetObjectWriter->getCPUType());
150 write32(TargetObjectWriter->getCPUSubtype());
169 StringRef Name,
unsigned NumSections, uint64_t VMAddr, uint64_t VMSize,
170 uint64_t SectionDataStartOffset, uint64_t SectionDataSize,
uint32_t MaxProt,
178 unsigned SegmentLoadCommandSize =
182 write32(SegmentLoadCommandSize +
191 write64(SectionDataStartOffset);
196 write32(SectionDataStartOffset);
211 uint64_t FileOffset,
unsigned Flags,
212 uint64_t RelocationsStart,
213 unsigned NumRelocations) {
242 write32(NumRelocations ? RelocationsStart : 0);
245 write32(IndirectSymBase.lookup(&Sec));
310 MachObjectWriter::MachSymbolData *
311 MachObjectWriter::findSymbolData(
const MCSymbol &Sym) {
312 for (
auto *SymbolData :
313 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
314 for (MachSymbolData &Entry : *SymbolData)
315 if (Entry.Symbol == &Sym)
328 S = &Ref->getSymbol();
338 uint8_t SectionIndex = MSD.SectionIndex;
341 bool IsAlias = Symbol != AliasedSymbol;
344 MachSymbolData *AliaseeInfo;
346 AliaseeInfo = findSymbolData(*AliasedSymbol);
348 SectionIndex = AliaseeInfo->SectionIndex;
349 Symbol = AliasedSymbol;
376 Address = AliaseeInfo->StringIndex;
393 bool EncodeAsAltEntry =
394 IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry();
395 write16(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry));
417 const std::vector<std::string> &Options,
bool is64Bit)
420 for (
const std::string &Option : Options)
421 Size += Option.size() + 1;
422 return alignTo(Size, is64Bit ? 8 : 4);
426 const std::vector<std::string> &Options)
432 write32(MachO::LC_LINKER_OPTION);
436 for (
const std::string &Option : Options) {
439 BytesWritten += Option.size() + 1;
452 uint64_t &FixedValue) {
453 TargetObjectWriter->recordRelocation(
this, Asm, Layout, Fragment, Fixup,
477 "' not in a symbol pointer or stub section");
482 unsigned IndirectIndex = 0;
492 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
508 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
516 cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(
true);
523 std::vector<MachSymbolData> &ExternalSymbolData,
524 std::vector<MachSymbolData> &UndefinedSymbolData) {
529 ie = Asm.
end(); it != ie; ++it, ++Index)
530 SectionIndexMap[&*it] = Index;
531 assert(Index <= 256 &&
"Too many sections!");
538 StringTable.add(
Symbol.getName());
540 StringTable.finalize();
557 MSD.StringIndex = StringTable.getOffset(
Symbol.getName());
559 if (
Symbol.isUndefined()) {
560 MSD.SectionIndex = 0;
561 UndefinedSymbolData.push_back(MSD);
562 }
else if (
Symbol.isAbsolute()) {
563 MSD.SectionIndex = 0;
564 ExternalSymbolData.push_back(MSD);
566 MSD.SectionIndex = SectionIndexMap.
lookup(&
Symbol.getSection());
567 assert(MSD.SectionIndex &&
"Invalid section index!");
568 ExternalSymbolData.push_back(MSD);
583 MSD.StringIndex = StringTable.getOffset(
Symbol.getName());
585 if (
Symbol.isAbsolute()) {
586 MSD.SectionIndex = 0;
587 LocalSymbolData.push_back(MSD);
589 MSD.SectionIndex = SectionIndexMap.
lookup(&
Symbol.getSection());
590 assert(MSD.SectionIndex &&
"Invalid section index!");
591 LocalSymbolData.push_back(MSD);
596 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
597 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
601 for (
auto *SymbolData :
602 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
603 for (MachSymbolData &Entry : *SymbolData)
604 Entry.Symbol->setIndex(Index++);
607 for (RelAndSymbol &Rel : Relocations[&
Section]) {
612 unsigned Index = Rel.Sym->getIndex();
615 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27);
617 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4);
624 uint64_t StartAddress = 0;
626 StartAddress =
alignTo(StartAddress, Sec->getAlignment());
627 SectionAddress[Sec] = StartAddress;
659 bool InSet,
bool IsPCRel)
const {
686 bool hasReliableSymbolDifference =
isX86_64();
687 if (!hasReliableSymbolDifference) {
728 UndefinedSymbolData);
730 unsigned NumSections = Asm.
size();
736 unsigned NumLoadCommands = 1;
737 uint64_t LoadCommandsSize =
is64Bit() ?
742 if (VersionInfo.Major != 0) {
749 if (NumDataRegions) {
763 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
764 UndefinedSymbolData.size();
766 NumLoadCommands += 2;
781 uint64_t SectionDataSize = 0;
782 uint64_t SectionDataFileSize = 0;
790 VMSize =
std::max(VMSize, Address + Size);
792 if (Sec.isVirtualSection())
795 SectionDataSize =
std::max(SectionDataSize, Address + Size);
796 SectionDataFileSize =
std::max(SectionDataFileSize, Address + FileSize);
803 SectionDataFileSize += SectionDataPadding;
807 Asm.getSubsectionsViaSymbols());
811 SectionDataSize, Prot, Prot);
814 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
816 const auto &Sec = cast<MCSectionMachO>(
Section);
817 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
818 unsigned NumRelocs = Relocs.size();
820 unsigned Flags = Sec.getTypeAndAttributes();
821 if (Sec.hasInstructions())
824 RelocTableEnd, NumRelocs);
829 if (VersionInfo.Major != 0) {
830 assert(VersionInfo.Update < 256 &&
"unencodable update target version");
831 assert(VersionInfo.Minor < 256 &&
"unencodable minor target version");
832 assert(VersionInfo.Major < 65536 &&
"unencodable major target version");
833 uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) |
834 (VersionInfo.Major << 16);
836 switch (VersionInfo.Kind) {
838 LCType = MachO::LC_VERSION_MIN_MACOSX;
841 LCType = MachO::LC_VERSION_MIN_IPHONEOS;
844 LCType = MachO::LC_VERSION_MIN_TVOS;
847 LCType = MachO::LC_VERSION_MIN_WATCHOS;
857 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
858 if (NumDataRegions) {
859 uint64_t DataRegionsOffset = RelocTableEnd;
860 uint64_t DataRegionsSize = NumDataRegions * 8;
866 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
869 DataInCodeTableEnd, LOHSize);
873 unsigned FirstLocalSymbol = 0;
874 unsigned NumLocalSymbols = LocalSymbolData.size();
875 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
876 unsigned NumExternalSymbols = ExternalSymbolData.size();
877 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
878 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
879 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
880 unsigned NumSymTabSymbols =
881 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
882 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
883 uint64_t IndirectSymbolOffset = 0;
886 if (NumIndirectSymbols)
887 IndirectSymbolOffset = LOHTableEnd;
890 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
893 uint64_t StringTableOffset =
894 SymbolTableOffset + NumSymTabSymbols * (
is64Bit() ?
898 StringTableOffset, StringTable.getSize());
901 FirstExternalSymbol, NumExternalSymbols,
902 FirstUndefinedSymbol, NumUndefinedSymbols,
903 IndirectSymbolOffset, NumIndirectSymbols);
907 for (
const auto &Option : Asm.getLinkerOptions())
912 Asm.writeSectionData(&Sec, Layout);
925 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
926 for (
const RelAndSymbol &Rel :
make_range(Relocs.rbegin(), Relocs.rend())) {
934 it = Asm.data_region_begin(), ie = Asm.data_region_end();
940 <<
" start: " << Start <<
"(" << Data->
Start->
getName() <<
")"
941 <<
" end: " << End <<
"(" << Data->
End->
getName() <<
")"
942 <<
" size: " << End - Start
954 Asm.getLOHContainer().emit(*
this, Layout);
964 it = Asm.indirect_symbol_begin(),
965 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
972 if (it->Symbol->isDefined() && !it->Symbol->isExternal()) {
974 if (it->Symbol->isAbsolute())
981 write32(it->Symbol->getIndex());
987 for (
auto *SymbolData :
988 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
989 for (MachSymbolData &Entry : *SymbolData)
999 bool IsLittleEndian) {
Instances of this class represent a uniqued identifier for a section in the current translation unit...
S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
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...
void WriteZeros(unsigned N)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This represents a section on a Mach-O system (used by Mac OS X).
void computeSectionAddresses(const MCAssembler &Asm, const MCAsmLayout &Layout)
const MCSymbol & getSymbol() const
bool doesSymbolRequireExternRelocation(const MCSymbol &S)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
raw_pwrite_stream & getStream()
void write64(uint64_t Value)
This represents an "assembler immediate".
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
uint64_t getSectionAddress(const MCSection *Sec) const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) override
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
uint64_t getEmitSize(const MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Get the size of the directives if emitted.
const VersionMinInfoType & getVersionMinInfo() const
MachO deployment target version information.
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
unsigned getAlignment() const
Defines the object file and target independent interfaces used by the assembler backend to write nati...
void write8(uint8_t Value)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
enum llvm::DataRegionData::KindTy Kind
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)
void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override
Write the object file.
bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const override
Encapsulates the layout of an assembly file at a particular point in time.
Base class for the full range of assembler expressions which are needed for parsing.
Represent a reference to a symbol from inside an expression.
struct{MCVersionMinType Kind VersionMinInfoType
MachO specific deployment target version info.
uint64_t tell() const
tell - Return the current offset with the file.
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.
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind)
StringRef getSectionName() const
void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
unsigned getStubSize() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
void writeLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
void write32(uint32_t Value)
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
bool isAbsolute(bool SetUsed=true) const
isAbsolute - Check if this is an absolute symbol.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
The instances of the Type class are immutable: once they are created, they are never changed...
S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const
static bool is64Bit(const char *name)
virtual void reset()
lifetime management
MCFixupKind
Extensible enumeration to represent the type of a fixup.
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
static const unsigned End
std::vector< DataRegionData >::const_iterator const_data_region_iterator
void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols)
std::vector< IndirectSymbolData >::const_iterator const_indirect_symbol_iterator
llvm::SmallVectorImpl< MCSection * > & getSectionOrder()
bool isInSection(bool SetUsed=true) const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
const MCSymbolRefExpr * getSymB() const
void write16(uint16_t Value)
bool isSymbolLinkerVisible(const MCSymbol &SD) const
Check whether a particular symbol is visible to the linker and is required in the symbol table...
PowerPC TLS Dynamic Call Fixup
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
std::vector< std::vector< std::string > > & getLinkerOptions()
void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, uint32_t DataSize)
void writeSection(const MCAsmLayout &Layout, const MCSection &Sec, uint64_t VMAddr, uint64_t FileOffset, unsigned Flags, uint64_t RelocationsStart, unsigned NumRelocations)
indirect_symbol_iterator indirect_symbol_begin()
const MCSymbolRefExpr * getSymA() const
virtual bool isVirtualSection() const =0
Check whether this section is "virtual", that is has no actual object file contents.
An iterator type that allows iterating over the pointees via some other iterator. ...
S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread local structures.
MachO::SectionType getType() const
static const char * Target
uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const
uint64_t getFragmentAddress(const MCFragment *Fragment, const MCAsmLayout &Layout) const
MCLOHContainer & getLOHContainer()
static unsigned ComputeLinkerOptionsLoadCommandSize(const std::vector< std::string > &Options, bool is64Bit)
MCSection * getParent() const
bool isDefined(bool SetUsed=true) const
isDefined - Check if this symbol is defined (i.e., it has an address).
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Target - Wrapper for Target specific information.
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
MCObjectWriter * createMachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
StringRef getSegmentName() const
uint64_t getCommonSize() const
Return the size of a 'common' symbol.
MCSection & getSection(bool SetUsed=true) const
Get the section associated with a defined, non-absolute symbol.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
bool getSubsectionsViaSymbols() const
void computeSymbolTable(MCAssembler &Asm, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
Compute the symbol table data.
static std::vector< std::string > Flags
const MCSymbol * getAtom() const
StringRef getName() const
getName - Get the symbol name.
unsigned getLayoutOrder() const
MCAsmBackend & getBackend() const
bool isCommon() const
Is this a 'common' symbol.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Target independent information on a fixup kind.
void writeBytes(const SmallVectorImpl< char > &ByteVec, unsigned ZeroFillSize=0)
bool isVariable() const
isVariable - Check if this is a variable symbol.
MCFragment * getFragment(bool SetUsed=true) const
std::vector< IndirectSymbolData >::iterator indirect_symbol_iterator
An abstract base class for streams implementations that also support a pwrite operation.
void reset() override
lifetime management
bool isVirtualSection() const override
Check whether this section is "virtual", that is has no actual object file contents.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int64_t getConstant() const
bool isPrivateExtern() const
bool operator<(int64_t V1, const APSInt &V2)
LLVM Value Representation.
indirect_symbol_iterator indirect_symbol_end()
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
uint64_t getSectionFileSize(const MCSection *Sec) const
Get the data size of the given section, as emitted to the object file.
StringRef - Represent a constant reference to a string, i.e.
void bindIndirectSymbols(MCAssembler &Asm)
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
std::vector< DataRegionData > & getDataRegions()
uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
const MCSymbol & findAliasedSymbol(const MCSymbol &Sym) const
void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout)
unsigned Flags
Flags describing additional information on this fixup kind.
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).