Skip to content

Commit

Permalink
[MC,MachO] Simplify IndirectSybols
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay authored and kbluck committed Jul 6, 2024
1 parent 4350d6a commit 72238a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
16 changes: 0 additions & 16 deletions llvm/include/llvm/MC/MCAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,6 @@ class MCAssembler {
return IndirectSymbols;
}

indirect_symbol_iterator indirect_symbol_begin() {
return IndirectSymbols.begin();
}
const_indirect_symbol_iterator indirect_symbol_begin() const {
return IndirectSymbols.begin();
}

indirect_symbol_iterator indirect_symbol_end() {
return IndirectSymbols.end();
}
const_indirect_symbol_iterator indirect_symbol_end() const {
return IndirectSymbols.end();
}

size_t indirect_symbol_size() const { return IndirectSymbols.size(); }

/// @}
/// \name Linker Option List Access
/// @{
Expand Down
39 changes: 16 additions & 23 deletions llvm/lib/MC/MachObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {

// Report errors for use of .indirect_symbol not in a symbol pointer section
// or stub section.
for (IndirectSymbolData &ISD : llvm::make_range(Asm.indirect_symbol_begin(),
Asm.indirect_symbol_end())) {
for (IndirectSymbolData &ISD : Asm.getIndirectSymbols()) {
const MCSectionMachO &Section = cast<MCSectionMachO>(*ISD.Section);

if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
Expand All @@ -531,39 +530,35 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
}

// Bind non-lazy symbol pointers first.
unsigned IndirectIndex = 0;
for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
for (auto [IndirectIndex, ISD] : enumerate(Asm.getIndirectSymbols())) {
const auto &Section = cast<MCSectionMachO>(*ISD.Section);

if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
continue;

// Initialize the section indirect symbol base, if necessary.
IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));

Asm.registerSymbol(*it->Symbol);
Asm.registerSymbol(*ISD.Symbol);
}

// 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) {
const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
for (auto [IndirectIndex, ISD] : enumerate(Asm.getIndirectSymbols())) {
const auto &Section = cast<MCSectionMachO>(*ISD.Section);

if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
Section.getType() != MachO::S_SYMBOL_STUBS)
continue;

// Initialize the section indirect symbol base, if necessary.
IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
IndirectSymBase.insert(std::make_pair(ISD.Section, IndirectIndex));

// Set the symbol type to undefined lazy, but only on construction.
//
// FIXME: Do not hardcode.
if (Asm.registerSymbol(*it->Symbol))
cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(true);
if (Asm.registerSymbol(*ISD.Symbol))
cast<MCSymbolMachO>(ISD.Symbol)->setReferenceTypeUndefinedLazy(true);
}
}

Expand Down Expand Up @@ -975,7 +970,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
unsigned NumExternalSymbols = ExternalSymbolData.size();
unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
unsigned NumIndirectSymbols = Asm.getIndirectSymbols().size();
unsigned NumSymTabSymbols =
NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
Expand Down Expand Up @@ -1065,25 +1060,23 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
// Write the symbol table data, if used.
if (NumSymbols) {
// Write the indirect symbol entries.
for (MCAssembler::const_indirect_symbol_iterator
it = Asm.indirect_symbol_begin(),
ie = Asm.indirect_symbol_end(); it != ie; ++it) {
for (auto &ISD : Asm.getIndirectSymbols()) {
// Indirect symbols in the non-lazy symbol pointer section have some
// special handling.
const MCSectionMachO &Section =
static_cast<const MCSectionMachO &>(*it->Section);
static_cast<const MCSectionMachO &>(*ISD.Section);
if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) {
// If this symbol is defined and internal, mark it as such.
if (it->Symbol->isDefined() && !it->Symbol->isExternal()) {
if (ISD.Symbol->isDefined() && !ISD.Symbol->isExternal()) {
uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
if (it->Symbol->isAbsolute())
if (ISD.Symbol->isAbsolute())
Flags |= MachO::INDIRECT_SYMBOL_ABS;
W.write<uint32_t>(Flags);
continue;
}
}

W.write<uint32_t>(it->Symbol->getIndex());
W.write<uint32_t>(ISD.Symbol->getIndex());
}

// FIXME: Check that offsets match computed ones.
Expand Down

0 comments on commit 72238a4

Please sign in to comment.