43 #define DEBUG_TYPE "mc" 47 IndirectSymBase.clear();
49 LocalSymbolData.clear();
50 ExternalSymbolData.clear();
51 UndefinedSymbolData.clear();
62 if (cast<MCSymbolMachO>(S).isWeakDefinition())
71 return Symbol->getName() < RHS.Symbol->getName();
134 unsigned NumLoadCommands,
135 unsigned LoadCommandsSize,
136 bool SubsectionsViaSymbols) {
139 if (SubsectionsViaSymbols)
164 void MachObjectWriter::writeWithPadding(
StringRef Str, uint64_t
Size) {
175 StringRef Name,
unsigned NumSections, uint64_t VMAddr, uint64_t VMSize,
176 uint64_t SectionDataStartOffset, uint64_t SectionDataSize,
uint32_t MaxProt,
184 unsigned SegmentLoadCommandSize =
192 writeWithPadding(Name, 16);
194 W.
write<uint64_t>(VMAddr);
195 W.
write<uint64_t>(VMSize);
196 W.
write<uint64_t>(SectionDataStartOffset);
197 W.
write<uint64_t>(SectionDataSize);
216 uint64_t FileOffset,
unsigned Flags,
217 uint64_t RelocationsStart,
218 unsigned NumRelocations) {
237 W.
write<uint64_t>(VMAddr);
315 MachObjectWriter::MachSymbolData *
316 MachObjectWriter::findSymbolData(
const MCSymbol &Sym) {
317 for (
auto *SymbolData :
318 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
319 for (MachSymbolData &
Entry : *SymbolData)
320 if (
Entry.Symbol == &Sym)
333 S = &
Ref->getSymbol();
343 uint8_t SectionIndex = MSD.SectionIndex;
346 bool IsAlias = Symbol != AliasedSymbol;
349 MachSymbolData *AliaseeInfo;
351 AliaseeInfo = findSymbolData(*AliasedSymbol);
353 SectionIndex = AliaseeInfo->SectionIndex;
354 Symbol = AliasedSymbol;
381 Address = AliaseeInfo->StringIndex;
398 bool EncodeAsAltEntry =
399 IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry();
402 W.
write<uint64_t>(Address);
422 const std::vector<std::string> &Options,
bool is64Bit)
425 for (
const std::string &Option : Options)
426 Size += Option.size() + 1;
427 return alignTo(Size, is64Bit ? 8 : 4);
431 const std::vector<std::string> &Options)
441 for (
const std::string &Option : Options) {
443 W.
OS << Option <<
'\0';
444 BytesWritten += Option.size() + 1;
466 uint64_t &FixedValue) {
469 "unsupported relocation expression");
473 TargetObjectWriter->recordRelocation(
this, Asm, Layout, Fragment, Fixup,
497 "' not in a symbol pointer or stub section");
502 unsigned IndirectIndex = 0;
512 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
528 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
536 cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(
true);
543 std::vector<MachSymbolData> &ExternalSymbolData,
544 std::vector<MachSymbolData> &UndefinedSymbolData) {
549 ie = Asm.
end(); it != ie; ++it, ++
Index)
550 SectionIndexMap[&*it] = Index;
551 assert(Index <= 256 &&
"Too many sections!");
579 if (
Symbol.isUndefined()) {
580 MSD.SectionIndex = 0;
581 UndefinedSymbolData.push_back(MSD);
582 }
else if (
Symbol.isAbsolute()) {
583 MSD.SectionIndex = 0;
584 ExternalSymbolData.push_back(MSD);
586 MSD.SectionIndex = SectionIndexMap.
lookup(&
Symbol.getSection());
587 assert(MSD.SectionIndex &&
"Invalid section index!");
588 ExternalSymbolData.push_back(MSD);
605 if (
Symbol.isAbsolute()) {
606 MSD.SectionIndex = 0;
607 LocalSymbolData.push_back(MSD);
609 MSD.SectionIndex = SectionIndexMap.
lookup(&
Symbol.getSection());
610 assert(MSD.SectionIndex &&
"Invalid section index!");
611 LocalSymbolData.push_back(MSD);
621 for (
auto *SymbolData :
622 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
623 for (MachSymbolData &
Entry : *SymbolData)
624 Entry.Symbol->setIndex(Index++);
627 for (RelAndSymbol &Rel : Relocations[&
Section]) {
632 unsigned Index = Rel.Sym->getIndex();
635 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27);
637 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4);
644 uint64_t StartAddress = 0;
646 StartAddress =
alignTo(StartAddress, Sec->getAlignment());
647 SectionAddress[Sec] = StartAddress;
679 bool InSet,
bool IsPCRel)
const {
706 bool hasReliableSymbolDifference =
isX86_64();
707 if (!hasReliableSymbolDifference) {
756 uint64_t StartOffset =
W.
OS.
tell();
760 UndefinedSymbolData);
762 unsigned NumSections = Asm.
size();
768 unsigned NumLoadCommands = 1;
769 uint64_t LoadCommandsSize =
is64Bit() ?
774 if (VersionInfo.Major != 0) {
776 if (VersionInfo.EmitBuildVersion)
784 if (NumDataRegions) {
798 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
799 UndefinedSymbolData.size();
801 NumLoadCommands += 2;
816 uint64_t SectionDataSize = 0;
817 uint64_t SectionDataFileSize = 0;
825 VMSize =
std::max(VMSize, Address + Size);
827 if (Sec.isVirtualSection())
830 SectionDataSize =
std::max(SectionDataSize, Address + Size);
831 SectionDataFileSize =
std::max(SectionDataFileSize, Address + FileSize);
837 unsigned SectionDataPadding =
839 SectionDataFileSize += SectionDataPadding;
843 Asm.getSubsectionsViaSymbols());
847 SectionDataSize, Prot, Prot);
850 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
852 const auto &Sec = cast<MCSectionMachO>(
Section);
853 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
854 unsigned NumRelocs = Relocs.size();
856 unsigned Flags = Sec.getTypeAndAttributes();
857 if (Sec.hasInstructions())
860 RelocTableEnd, NumRelocs);
865 if (VersionInfo.Major != 0) {
867 assert(!V.empty() &&
"empty version");
868 unsigned Update = V.getSubminor() ? *V.getSubminor() : 0;
869 unsigned Minor = V.getMinor() ? *V.getMinor() : 0;
870 assert(Update < 256 &&
"unencodable update target version");
871 assert(Minor < 256 &&
"unencodable minor target version");
872 assert(V.getMajor() < 65536 &&
"unencodable major target version");
873 return Update | (Minor << 8) | (V.getMajor() << 16);
875 uint32_t EncodedVersion = EncodeVersion(
876 VersionTuple(VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update));
877 uint32_t SDKVersion = !VersionInfo.SDKVersion.empty()
878 ? EncodeVersion(VersionInfo.SDKVersion)
880 if (VersionInfo.EmitBuildVersion) {
899 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
900 if (NumDataRegions) {
901 uint64_t DataRegionsOffset = RelocTableEnd;
902 uint64_t DataRegionsSize = NumDataRegions * 8;
908 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
911 DataInCodeTableEnd, LOHSize);
915 unsigned FirstLocalSymbol = 0;
916 unsigned NumLocalSymbols = LocalSymbolData.size();
917 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
918 unsigned NumExternalSymbols = ExternalSymbolData.size();
919 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
920 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
921 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
922 unsigned NumSymTabSymbols =
923 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
924 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
925 uint64_t IndirectSymbolOffset = 0;
928 if (NumIndirectSymbols)
929 IndirectSymbolOffset = LOHTableEnd;
932 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
935 uint64_t StringTableOffset =
936 SymbolTableOffset + NumSymTabSymbols * (
is64Bit() ?
940 StringTableOffset, StringTable.
getSize());
943 FirstExternalSymbol, NumExternalSymbols,
944 FirstUndefinedSymbol, NumUndefinedSymbols,
945 IndirectSymbolOffset, NumIndirectSymbols);
949 for (
const auto &Option : Asm.getLinkerOptions())
954 Asm.writeSectionData(
W.
OS, &Sec, Layout);
967 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
968 for (
const RelAndSymbol &Rel :
make_range(Relocs.rbegin(), Relocs.rend())) {
976 it = Asm.data_region_begin(), ie = Asm.data_region_end();
989 <<
" end: " << End <<
"(" << Data->
End->
getName() <<
")" 990 <<
" size: " << End - Start <<
"\n");
1001 Asm.getLOHContainer().emit(*
this, Layout);
1012 it = Asm.indirect_symbol_begin(),
1013 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
1020 if (it->Symbol->isDefined() && !it->Symbol->isExternal()) {
1022 if (it->Symbol->isAbsolute())
1035 for (
auto *SymbolData :
1036 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
1037 for (MachSymbolData &
Entry : *SymbolData)
1044 return W.
OS.
tell() - StartOffset;
1047 std::unique_ptr<MCObjectWriter>
1050 return std::make_unique<MachObjectWriter>(std::move(MOTW), OS,
Instances of this class represent a uniqued identifier for a section in the current translation unit...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
const MCSymbol & findAliasedSymbol(const MCSymbol &Sym) const
S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in the Reserved2 field.
uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const
This represents a section on a Mach-O system (used by Mac OS X).
void computeSectionAddresses(const MCAssembler &Asm, const MCAsmLayout &Layout)
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.
This class represents lattice values for constants.
bool isVariable() const
isVariable - Check if this is a variable symbol.
This represents an "assembler immediate".
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
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...
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
bool isCommon() const
Is this a 'common' symbol.
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...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
unsigned getAlignment() const
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
support::endian::Writer W
enum llvm::DataRegionData::KindTy Kind
MCContext & getContext() const
int64_t getConstant() const
const MCSymbolRefExpr * getSymB() const
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)
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.
bool isSymbolLinkerVisible(const MCSymbol &SD) const
Check whether a particular symbol is visible to the linker and is required in the symbol table...
Base class for the full range of assembler expressions which are needed for parsing.
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
The access may reference the value stored in memory.
Represent a reference to a symbol from inside an expression.
StringRef getSectionName() const
uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const
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)
void write(raw_ostream &OS) const
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
size_t add(CachedHashStringRef S)
Add a string to the builder.
uint64_t getEmitSize(const MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const
Get the size of the directives if emitted.
unsigned getStubSize() const
void writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, uint32_t StringTableOffset, uint32_t StringTableSize)
LLVM_NODISCARD size_t size() const
size - Get the string size.
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
const VersionInfoType & getVersionInfo() const
MachO deployment target version information.
void writeLinkerOptionsLoadCommand(const std::vector< std::string > &Options)
StringRef getSegmentName() const
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
* 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...
virtual void reset()
lifetime management
MCFixupKind
Extensible enumeration to represent the type of a fixup.
struct { bool EmitBuildVersion VersionInfoType
MachO specific deployment target version info.
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
static bool isFixupTargetValid(const MCValue &Target)
const MCSymbolRefExpr * getSymA() const
uint64_t getCommonSize() const
Return the size of a 'common' symbol.
void reportError(SMLoc L, const Twine &Msg)
std::vector< DataRegionData >::const_iterator const_data_region_iterator
const MCSymbol * getAtom() const
void writeHeader(MachO::HeaderFileType Type, unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols)
std::vector< IndirectSymbolData >::const_iterator const_indirect_symbol_iterator
llvm::SmallVectorImpl< MCSection * > & getSectionOrder()
void finalize()
Analyze the strings and build the final table.
MachO::SectionType getType() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
void sort(IteratorTy Start, IteratorTy End)
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.
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
std::vector< std::vector< std::string > > & getLinkerOptions()
Align max(MaybeAlign Lhs, Align Rhs)
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()
uint64_t getFragmentAddress(const MCFragment *Fragment, const MCAsmLayout &Layout) const
void write(ArrayRef< value_type > Val)
virtual bool isVirtualSection() const =0
Check whether this section is "virtual", that is has no actual object file contents.
MCAsmBackend & getBackend() const
const MCSymbol & getSymbol() const
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
MCFragment * getFragment(bool SetUsed=true) const
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.
bool getSubsectionsViaSymbols() const
MCLOHContainer & getLOHContainer()
static unsigned ComputeLinkerOptionsLoadCommandSize(const std::vector< std::string > &Options, bool is64Bit)
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 isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Target - Wrapper for Target specific information.
MCSection * getParent() const
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
bool isAbsolute() const
isAbsolute - Check if this is an absolute symbol.
void computeSymbolTable(MCAssembler &Asm, std::vector< MachSymbolData > &LocalSymbolData, std::vector< MachSymbolData > &ExternalSymbolData, std::vector< MachSymbolData > &UndefinedSymbolData)
Compute the symbol table data.
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Represents a version number in the form major[.minor[.subminor[.build]]].
std::unique_ptr< MCObjectWriter > createMachObjectWriter(std::unique_ptr< MCMachObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override
Write the object file and returns the number of bytes written.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getLayoutOrder() const
Target independent information on a fixup kind.
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...
std::vector< IndirectSymbolData >::iterator indirect_symbol_iterator
uint64_t getSectionAddress(const MCSection *Sec) const
StringRef getName() const
getName - Get the symbol name.
An abstract base class for streams implementations that also support a pwrite operation.
void reset() override
lifetime management
uint64_t getSectionFileSize(const MCSection *Sec) const
Get the data size of the given section, as emitted to the object file.
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())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type)
LLVM Value Representation.
indirect_symbol_iterator indirect_symbol_end()
uint64_t tell() const
tell - Return the current offset with the file.
StringRef - Represent a constant reference to a string, i.e.
void bindIndirectSymbols(MCAssembler &Asm)
bool operator<(const DeltaInfo &LHS, int64_t Delta)
std::vector< DataRegionData > & getDataRegions()
bool isPrivateExtern() const
void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout)
unsigned Flags
Flags describing additional information on this fixup kind.