#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
return true;
// References to weak definitions require external relocation entries; the
// definition may not always be the one in the same object file.
return true;
// References to weak definitions require external relocation entries; the
// definition may not always be the one in the same object file.
uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment,
const MCAsmLayout &Layout) const {
return getSectionAddress(Fragment->getParent()) +
uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment,
const MCAsmLayout &Layout) const {
return getSectionAddress(Fragment->getParent()) +
// If this is a variable, then recursively evaluate now.
if (S.isVariable()) {
if (const MCConstantExpr *C =
// If this is a variable, then recursively evaluate now.
if (S.isVariable()) {
if (const MCConstantExpr *C =
- uint64_t EndAddr = getSectionAddress(SD) + Layout.getSectionAddressSize(SD);
- unsigned Next = SD->getLayoutOrder() + 1;
+ uint64_t EndAddr = getSectionAddress(Sec) + Layout.getSectionAddressSize(Sec);
+ unsigned Next = Sec->getLayoutOrder() + 1;
- const MCSectionData &NextSD = *Layout.getSectionOrder()[Next];
- if (NextSD.getSection().isVirtualSection())
+ const MCSection &NextSec = *Layout.getSectionOrder()[Next];
+ if (NextSec.isVirtualSection())
unsigned LoadCommandsSize,
bool SubsectionsViaSymbols) {
uint32_t Flags = 0;
unsigned LoadCommandsSize,
bool SubsectionsViaSymbols) {
uint32_t Flags = 0;
- Write32(MachO::MH_OBJECT);
- Write32(NumLoadCommands);
- Write32(LoadCommandsSize);
- Write32(Flags);
+ write32(MachO::MH_OBJECT);
+ write32(NumLoadCommands);
+ write32(LoadCommandsSize);
+ write32(Flags);
///
/// \param NumSections The number of sections in this segment.
/// \param SectionDataSize The total size of the sections.
///
/// \param NumSections The number of sections in this segment.
/// \param SectionDataSize The total size of the sections.
uint64_t VMSize,
uint64_t SectionDataStartOffset,
uint64_t SectionDataSize) {
uint64_t VMSize,
uint64_t SectionDataStartOffset,
uint64_t SectionDataSize) {
unsigned SegmentLoadCommandSize =
is64Bit() ? sizeof(MachO::segment_command_64):
sizeof(MachO::segment_command);
unsigned SegmentLoadCommandSize =
is64Bit() ? sizeof(MachO::segment_command_64):
sizeof(MachO::segment_command);
- Write64(0); // vmaddr
- Write64(VMSize); // vmsize
- Write64(SectionDataStartOffset); // file offset
- Write64(SectionDataSize); // file size
+ write64(0); // vmaddr
+ write64(VMSize); // vmsize
+ write64(SectionDataStartOffset); // file offset
+ write64(SectionDataSize); // file size
- Write32(0); // vmaddr
- Write32(VMSize); // vmsize
- Write32(SectionDataStartOffset); // file offset
- Write32(SectionDataSize); // file size
+ write32(0); // vmaddr
+ write32(VMSize); // vmsize
+ write32(SectionDataStartOffset); // file offset
+ write32(SectionDataSize); // file size
uint64_t RelocationsStart,
unsigned NumRelocations) {
uint64_t RelocationsStart,
unsigned NumRelocations) {
- if (SD.getSection().isVirtualSection()) {
- assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
+ if (Section.isVirtualSection()) {
+ assert(Layout.getSectionFileSize(&Sec) == 0 && "Invalid file size!");
- const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
- WriteBytes(Section.getSectionName(), 16);
- WriteBytes(Section.getSegmentName(), 16);
+ writeBytes(Section.getSectionName(), 16);
+ writeBytes(Section.getSegmentName(), 16);
- assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
- Write32(Log2_32(SD.getAlignment()));
- Write32(NumRelocations ? RelocationsStart : 0);
- Write32(NumRelocations);
- Write32(Flags);
- Write32(IndirectSymBase.lookup(&SD)); // reserved1
- Write32(Section.getStubSize()); // reserved2
+ assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!");
+ write32(Log2_32(Section.getAlignment()));
+ write32(NumRelocations ? RelocationsStart : 0);
+ write32(NumRelocations);
+ write32(Flags);
+ write32(IndirectSymBase.lookup(&Sec)); // reserved1
+ write32(Section.getStubSize()); // reserved2
uint32_t NumSymbols,
uint32_t StringTableOffset,
uint32_t StringTableSize) {
uint32_t NumSymbols,
uint32_t StringTableOffset,
uint32_t StringTableSize) {
- Write32(MachO::LC_SYMTAB);
- Write32(sizeof(MachO::symtab_command));
- Write32(SymbolOffset);
- Write32(NumSymbols);
- Write32(StringTableOffset);
- Write32(StringTableSize);
+ write32(MachO::LC_SYMTAB);
+ write32(sizeof(MachO::symtab_command));
+ write32(SymbolOffset);
+ write32(NumSymbols);
+ write32(StringTableOffset);
+ write32(StringTableSize);
uint32_t NumLocalSymbols,
uint32_t FirstExternalSymbol,
uint32_t NumExternalSymbols,
uint32_t NumLocalSymbols,
uint32_t FirstExternalSymbol,
uint32_t NumExternalSymbols,
- Write32(MachO::LC_DYSYMTAB);
- Write32(sizeof(MachO::dysymtab_command));
- Write32(FirstLocalSymbol);
- Write32(NumLocalSymbols);
- Write32(FirstExternalSymbol);
- Write32(NumExternalSymbols);
- Write32(FirstUndefinedSymbol);
- Write32(NumUndefinedSymbols);
- Write32(0); // tocoff
- Write32(0); // ntoc
- Write32(0); // modtaboff
- Write32(0); // nmodtab
- Write32(0); // extrefsymoff
- Write32(0); // nextrefsyms
- Write32(IndirectSymbolOffset);
- Write32(NumIndirectSymbols);
- Write32(0); // extreloff
- Write32(0); // nextrel
- Write32(0); // locreloff
- Write32(0); // nlocrel
+ write32(MachO::LC_DYSYMTAB);
+ write32(sizeof(MachO::dysymtab_command));
+ write32(FirstLocalSymbol);
+ write32(NumLocalSymbols);
+ write32(FirstExternalSymbol);
+ write32(NumExternalSymbols);
+ write32(FirstUndefinedSymbol);
+ write32(NumUndefinedSymbols);
+ write32(0); // tocoff
+ write32(0); // ntoc
+ write32(0); // modtaboff
+ write32(0); // nmodtab
+ write32(0); // extrefsymoff
+ write32(0); // nextrefsyms
+ write32(IndirectSymbolOffset);
+ write32(NumIndirectSymbols);
+ write32(0); // extreloff
+ write32(0); // nextrel
+ write32(0); // locreloff
+ write32(0); // nlocrel
assert(OS.tell() - Start == sizeof(MachO::dysymtab_command));
}
MachObjectWriter::MachSymbolData *
MachObjectWriter::findSymbolData(const MCSymbol &Sym) {
assert(OS.tell() - Start == sizeof(MachO::dysymtab_command));
}
MachObjectWriter::MachSymbolData *
MachObjectWriter::findSymbolData(const MCSymbol &Sym) {
- for (auto &Entry : LocalSymbolData)
- if (&Entry.SymbolData->getSymbol() == &Sym)
- return &Entry;
-
- for (auto &Entry : ExternalSymbolData)
- if (&Entry.SymbolData->getSymbol() == &Sym)
- return &Entry;
-
- for (auto &Entry : UndefinedSymbolData)
- if (&Entry.SymbolData->getSymbol() == &Sym)
- return &Entry;
+ for (auto *SymbolData :
+ {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
+ for (MachSymbolData &Entry : *SymbolData)
+ if (Entry.Symbol == &Sym)
+ return &Entry;
-void MachObjectWriter::WriteNlist(MachSymbolData &MSD,
+const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const {
+ const MCSymbol *S = &Sym;
+ while (S->isVariable()) {
+ const MCExpr *Value = S->getVariableValue();
+ const auto *Ref = dyn_cast<MCSymbolRefExpr>(Value);
+ if (!Ref)
+ return *S;
+ S = &Ref->getSymbol();
+ }
+ return *S;
+}
+
+void MachObjectWriter::writeNlist(MachSymbolData &MSD,
- MCSymbolData &Data = *MSD.SymbolData;
- const MCSymbol *Symbol = &Data.getSymbol();
- const MCSymbol *AliasedSymbol = &Symbol->AliasedSymbol();
+ const MCSymbol *Symbol = MSD.Symbol;
+ const MCSymbol &Data = *Symbol;
+ const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol);
MachSymbolData *AliaseeInfo;
if (IsAlias) {
AliaseeInfo = findSymbolData(*AliasedSymbol);
if (AliaseeInfo)
SectionIndex = AliaseeInfo->SectionIndex;
Symbol = AliasedSymbol;
MachSymbolData *AliaseeInfo;
if (IsAlias) {
AliaseeInfo = findSymbolData(*AliasedSymbol);
if (AliaseeInfo)
SectionIndex = AliaseeInfo->SectionIndex;
Symbol = AliasedSymbol;
if (IsAlias && Symbol->isUndefined())
Address = AliaseeInfo->StringIndex;
else if (Symbol->isDefined())
if (IsAlias && Symbol->isUndefined())
Address = AliaseeInfo->StringIndex;
else if (Symbol->isDefined())
- Address = getSymbolAddress(&Data, Layout);
- else if (Data.isCommon()) {
+ Address = getSymbolAddress(OrigSymbol, Layout);
+ else if (Symbol->isCommon()) {
// Common symbols are encoded with the size in the address
// field, and their alignment in the flags.
// Common symbols are encoded with the size in the address
// field, and their alignment in the flags.
- Address = Data.getCommonSize();
-
- // Common alignment is packed into the 'desc' bits.
- if (unsigned Align = Data.getCommonAlignment()) {
- unsigned Log2Size = Log2_32(Align);
- assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
- if (Log2Size > 15)
- report_fatal_error("invalid 'common' alignment '" +
- Twine(Align) + "' for '" + Symbol->getName() + "'",
- false);
- // FIXME: Keep this mask with the SymbolFlags enumeration.
- Flags = (Flags & 0xF0FF) | (Log2Size << 8);
- }
+ Address = Symbol->getCommonSize();
const std::vector<std::string> &Options, bool is64Bit)
{
unsigned Size = sizeof(MachO::linker_option_command);
const std::vector<std::string> &Options, bool is64Bit)
{
unsigned Size = sizeof(MachO::linker_option_command);
- for (unsigned i = 0, e = Options.size(); i != e; ++i)
- Size += Options[i].size() + 1;
+ for (const std::string &Option : Options)
+ Size += Option.size() + 1;
const std::vector<std::string> &Options)
{
unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit());
uint64_t Start = OS.tell();
(void) Start;
const std::vector<std::string> &Options)
{
unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit());
uint64_t Start = OS.tell();
(void) Start;
const MCAsmLayout &Layout,
const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
bool &IsPCRel, uint64_t &FixedValue) {
const MCAsmLayout &Layout,
const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
bool &IsPCRel, uint64_t &FixedValue) {
- TargetObjectWriter->RecordRelocation(this, Asm, Layout, Fragment, Fixup,
+ TargetObjectWriter->recordRelocation(this, Asm, Layout, Fragment, Fixup,
// This is the point where 'as' creates actual symbols for indirect symbols
// (in the following two passes). It would be easier for us to do this sooner
// when we see the attribute, but that makes getting the order in the symbol
// This is the point where 'as' creates actual symbols for indirect symbols
// (in the following two passes). It would be easier for us to do this sooner
// when we see the attribute, but that makes getting the order in the symbol
// or stub section.
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it) {
// or stub section.
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it) {
if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
unsigned IndirectIndex = 0;
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
unsigned IndirectIndex = 0;
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS)
continue;
// Initialize the section indirect symbol base, if necessary.
if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS)
continue;
// Initialize the section indirect symbol base, if necessary.
}
// Then lazy symbol pointers and symbol stubs.
IndirectIndex = 0;
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
}
// Then lazy symbol pointers and symbol stubs.
IndirectIndex = 0;
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
Section.getType() != MachO::S_SYMBOL_STUBS)
continue;
// Initialize the section indirect symbol base, if necessary.
if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
Section.getType() != MachO::S_SYMBOL_STUBS)
continue;
// Initialize the section indirect symbol base, if necessary.
// Set the symbol type to undefined lazy, but only on construction.
//
// FIXME: Do not hardcode.
bool Created;
// Set the symbol type to undefined lazy, but only on construction.
//
// FIXME: Do not hardcode.
bool Created;
MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
std::vector<MachSymbolData> &ExternalSymbolData,
std::vector<MachSymbolData> &UndefinedSymbolData) {
MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
std::vector<MachSymbolData> &ExternalSymbolData,
std::vector<MachSymbolData> &UndefinedSymbolData) {
unsigned Index = 1;
for (MCAssembler::iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it, ++Index)
unsigned Index = 1;
for (MCAssembler::iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it, ++Index)
// The particular order that we collect and then sort the symbols is chosen to
// match 'as'. Even though it doesn't matter for correctness, this is
// important for letting us diff .o files.
// The particular order that we collect and then sort the symbols is chosen to
// match 'as'. Even though it doesn't matter for correctness, this is
// important for letting us diff .o files.
- for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
- LocalSymbolData[i].SymbolData->setIndex(Index++);
- for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
- ExternalSymbolData[i].SymbolData->setIndex(Index++);
- for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
- UndefinedSymbolData[i].SymbolData->setIndex(Index++);
-
- for (const MCSectionData &SD : Asm) {
- std::vector<RelAndSymbol> &Relocs = Relocations[&SD];
- for (RelAndSymbol &Rel : Relocs) {
+ for (auto *SymbolData :
+ {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
+ for (MachSymbolData &Entry : *SymbolData)
+ Entry.Symbol->setIndex(Index++);
+
+ for (const MCSection &Section : Asm) {
+ for (RelAndSymbol &Rel : Relocations[&Section]) {
void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
const MCAsmLayout &Layout) {
uint64_t StartAddress = 0;
void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
const MCAsmLayout &Layout) {
uint64_t StartAddress = 0;
- const SmallVectorImpl<MCSectionData*> &Order = Layout.getSectionOrder();
- for (int i = 0, n = Order.size(); i != n ; ++i) {
- const MCSectionData *SD = Order[i];
- StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
- SectionAddress[SD] = StartAddress;
- StartAddress += Layout.getSectionAddressSize(SD);
+ for (const MCSection *Sec : Layout.getSectionOrder()) {
+ StartAddress = RoundUpToAlignment(StartAddress, Sec->getAlignment());
+ SectionAddress[Sec] = StartAddress;
+ StartAddress += Layout.getSectionAddressSize(Sec);
// Explicitly pad the section to match the alignment requirements of the
// following one. This is for 'gas' compatibility, it shouldn't
/// strictly be necessary.
// Explicitly pad the section to match the alignment requirements of the
// following one. This is for 'gas' compatibility, it shouldn't
/// strictly be necessary.
const MCAsmLayout &Layout) {
computeSectionAddresses(Asm, Layout);
// Create symbol data for any indirect symbols.
const MCAsmLayout &Layout) {
computeSectionAddresses(Asm, Layout);
// Create symbol data for any indirect symbols.
-bool MachObjectWriter::
-IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
- const MCSymbolData &DataA,
- const MCSymbolData *DataB,
- const MCFragment &FB,
- bool InSet,
- bool IsPCRel) const {
+bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
+ const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
+ bool InSet, bool IsPCRel) const {
// - addr(atom(B)) - offset(B)
// and the offsets are not relocatable, so the fixup is fully resolved when
// addr(atom(A)) - addr(atom(B)) == 0.
// - addr(atom(B)) - offset(B)
// and the offsets are not relocatable, so the fixup is fully resolved when
// addr(atom(A)) - addr(atom(B)) == 0.
bool hasReliableSymbolDifference = isX86_64();
if (!hasReliableSymbolDifference) {
if (!SA.isInSection() || &SecA != &SecB ||
bool hasReliableSymbolDifference = isX86_64();
if (!hasReliableSymbolDifference) {
if (!SA.isInSection() || &SecA != &SecB ||
- ComputeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
+ computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
- const std::vector<std::vector<std::string> > &LinkerOptions =
- Asm.getLinkerOptions();
- for (unsigned i = 0, e = LinkerOptions.size(); i != e; ++i) {
+ for (const auto &Option : Asm.getLinkerOptions()) {
- for (MCAssembler::const_iterator it = Asm.begin(),
- ie = Asm.end(); it != ie; ++it) {
- const MCSectionData &SD = *it;
- uint64_t Address = getSectionAddress(&SD);
- uint64_t Size = Layout.getSectionAddressSize(&SD);
- uint64_t FileSize = Layout.getSectionFileSize(&SD);
- FileSize += getPaddingSize(&SD, Layout);
+ for (const MCSection &Sec : Asm) {
+ uint64_t Address = getSectionAddress(&Sec);
+ uint64_t Size = Layout.getSectionAddressSize(&Sec);
+ uint64_t FileSize = Layout.getSectionFileSize(&Sec);
+ FileSize += getPaddingSize(&Sec, Layout);
SectionDataFileSize += SectionDataPadding;
// Write the prolog, starting with the header and load command...
SectionDataFileSize += SectionDataPadding;
// Write the prolog, starting with the header and load command...
SectionDataStart, SectionDataSize);
// ... and then the section headers.
uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
SectionDataStart, SectionDataSize);
// ... and then the section headers.
uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
- for (MCAssembler::const_iterator it = Asm.begin(),
- ie = Asm.end(); it != ie; ++it) {
- std::vector<RelAndSymbol> &Relocs = Relocations[it];
+ for (const MCSection &Sec : Asm) {
+ std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
- uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
- WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
+ uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec);
+ writeSection(Asm, Layout, Sec, SectionStart, RelocTableEnd, NumRelocs);
assert(VersionInfo.Major < 65536 && "unencodable major target version");
uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) |
(VersionInfo.Major << 16);
assert(VersionInfo.Major < 65536 && "unencodable major target version");
uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) |
(VersionInfo.Major << 16);
- Write32(sizeof(MachO::version_min_command));
- Write32(EncodedVersion);
- Write32(0); // reserved.
+ write32(sizeof(MachO::version_min_command));
+ write32(EncodedVersion);
+ write32(0); // reserved.
DataRegionsSize);
}
// Write the loh load command, if used.
uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
if (LOHSize)
DataRegionsSize);
}
// Write the loh load command, if used.
uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
if (LOHSize)
DataInCodeTableEnd, LOHSize);
// Write the symbol table load command, if used.
DataInCodeTableEnd, LOHSize);
// Write the symbol table load command, if used.
FirstExternalSymbol, NumExternalSymbols,
FirstUndefinedSymbol, NumUndefinedSymbols,
IndirectSymbolOffset, NumIndirectSymbols);
}
// Write the linker options load commands.
FirstExternalSymbol, NumExternalSymbols,
FirstUndefinedSymbol, NumUndefinedSymbols,
IndirectSymbolOffset, NumIndirectSymbols);
}
// Write the linker options load commands.
- for (unsigned i = 0, e = LinkerOptions.size(); i != e; ++i) {
- WriteLinkerOptionsLoadCommand(LinkerOptions[i]);
- }
+ for (const auto &Option : Asm.getLinkerOptions())
+ writeLinkerOptionsLoadCommand(Option);
- for (MCAssembler::const_iterator it = Asm.begin(),
- ie = Asm.end(); it != ie; ++it) {
- Asm.writeSectionData(it, Layout);
+ for (const MCSection &Sec : Asm) {
+ Asm.writeSectionData(&Sec, Layout);
}
// Write the extra padding.
WriteZeros(SectionDataPadding);
// Write the relocation entries.
}
// Write the extra padding.
WriteZeros(SectionDataPadding);
// Write the relocation entries.
// Write the section relocation entries, in reverse order to match 'as'
// (approximately, the exact algorithm is more complicated than this).
// Write the section relocation entries, in reverse order to match 'as'
// (approximately, the exact algorithm is more complicated than this).
- std::vector<RelAndSymbol> &Relocs = Relocations[it];
- for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
- Write32(Relocs[e - i - 1].MRE.r_word0);
- Write32(Relocs[e - i - 1].MRE.r_word1);
+ std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
+ for (const RelAndSymbol &Rel : make_range(Relocs.rbegin(), Relocs.rend())) {
+ write32(Rel.MRE.r_word0);
+ write32(Rel.MRE.r_word1);
it = Asm.data_region_begin(), ie = Asm.data_region_end();
it != ie; ++it) {
const DataRegionData *Data = &(*it);
it = Asm.data_region_begin(), ie = Asm.data_region_end();
it != ie; ++it) {
const DataRegionData *Data = &(*it);
- uint64_t Start =
- getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->Start),
- Layout);
- uint64_t End =
- getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->End),
- Layout);
+ uint64_t Start = getSymbolAddress(*Data->Start, Layout);
+ uint64_t End = getSymbolAddress(*Data->End, Layout);
DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
<< " start: " << Start << "(" << Data->Start->getName() << ")"
<< " end: " << End << "(" << Data->End->getName() << ")"
<< " size: " << End - Start
<< "\n");
DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
<< " start: " << Start << "(" << Data->Start->getName() << ")"
<< " end: " << End << "(" << Data->End->getName() << ")"
<< " size: " << End - Start
<< "\n");
if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) {
// If this symbol is defined and internal, mark it as such.
if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) {
// If this symbol is defined and internal, mark it as such.
uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
if (it->Symbol->isAbsolute())
Flags |= MachO::INDIRECT_SYMBOL_ABS;
uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
if (it->Symbol->isAbsolute())
Flags |= MachO::INDIRECT_SYMBOL_ABS;
- for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
- WriteNlist(LocalSymbolData[i], Layout);
- for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
- WriteNlist(ExternalSymbolData[i], Layout);
- for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
- WriteNlist(UndefinedSymbolData[i], Layout);
+ for (auto *SymbolData :
+ {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
+ for (MachSymbolData &Entry : *SymbolData)
+ writeNlist(Entry, Layout);