[llvm] 4ba9956 - MachObjectWriter: replace the MCAsmLayout parameter with MCAssembler
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 1 17:01:56 PDT 2024
Author: Fangrui Song
Date: 2024-07-01T17:01:51-07:00
New Revision: 4ba9956958b5646853a0abd81a96d935749eef8b
URL: https://github.com/llvm/llvm-project/commit/4ba9956958b5646853a0abd81a96d935749eef8b
DIFF: https://github.com/llvm/llvm-project/commit/4ba9956958b5646853a0abd81a96d935749eef8b.diff
LOG: MachObjectWriter: replace the MCAsmLayout parameter with MCAssembler
Added:
Modified:
llvm/include/llvm/MC/MCLinkerOptimizationHint.h
llvm/include/llvm/MC/MCMachObjectWriter.h
llvm/lib/MC/MCLinkerOptimizationHint.cpp
llvm/lib/MC/MachObjectWriter.cpp
llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCLinkerOptimizationHint.h b/llvm/include/llvm/MC/MCLinkerOptimizationHint.h
index b91fbc62aa756..530f2883ab9ee 100644
--- a/llvm/include/llvm/MC/MCLinkerOptimizationHint.h
+++ b/llvm/include/llvm/MC/MCLinkerOptimizationHint.h
@@ -25,7 +25,7 @@
namespace llvm {
class MachObjectWriter;
-class MCAsmLayout;
+class MCAssembler;
class MCSymbol;
class raw_ostream;
@@ -108,8 +108,8 @@ class MCLOHDirective {
/// Emit this directive in \p OutStream using the information available
/// in the given \p ObjWriter and \p Layout to get the address of the
/// arguments within the object file.
- void emit_impl(raw_ostream &OutStream, const MachObjectWriter &ObjWriter,
- const MCAsmLayout &Layout) const;
+ void emit_impl(const MCAssembler &Asm, raw_ostream &OutStream,
+ const MachObjectWriter &ObjWriter) const;
public:
using LOHArgs = SmallVectorImpl<MCSymbol *>;
@@ -125,12 +125,12 @@ class MCLOHDirective {
/// Emit this directive as:
/// <kind, numArgs, addr1, ..., addrN>
- void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const;
+ void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const;
/// Get the size in bytes of this directive if emitted in \p ObjWriter with
/// the given \p Layout.
- uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
- const MCAsmLayout &Layout) const;
+ uint64_t getEmitSize(const MCAssembler &Asm,
+ const MachObjectWriter &ObjWriter) const;
};
class MCLOHContainer {
@@ -157,20 +157,20 @@ class MCLOHContainer {
}
/// Get the size of the directives if emitted.
- uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
- const MCAsmLayout &Layout) const {
+ uint64_t getEmitSize(const MCAssembler &Asm,
+ const MachObjectWriter &ObjWriter) const {
if (!EmitSize) {
for (const MCLOHDirective &D : Directives)
- EmitSize += D.getEmitSize(ObjWriter, Layout);
+ EmitSize += D.getEmitSize(Asm, ObjWriter);
}
return EmitSize;
}
/// Emit all Linker Optimization Hint in one big table.
/// Each line of the table is emitted by LOHDirective::emit.
- void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
+ void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const {
for (const MCLOHDirective &D : Directives)
- D.emit(ObjWriter, Layout);
+ D.emit(Asm, ObjWriter);
}
void reset() {
diff --git a/llvm/include/llvm/MC/MCMachObjectWriter.h b/llvm/include/llvm/MC/MCMachObjectWriter.h
index c685e27d6837c..172dadb128192 100644
--- a/llvm/include/llvm/MC/MCMachObjectWriter.h
+++ b/llvm/include/llvm/MC/MCMachObjectWriter.h
@@ -161,7 +161,7 @@ class MachObjectWriter : public MCObjectWriter {
uint64_t getSectionAddress(const MCSection *Sec) const {
return SectionAddress.lookup(Sec);
}
- uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
+ uint64_t getSymbolAddress(const MCSymbol &S, const MCAssembler &Asm) const;
uint64_t getFragmentAddress(const MCAssembler &Asm,
const MCFragment *Fragment) const;
@@ -212,7 +212,7 @@ class MachObjectWriter : public MCObjectWriter {
uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
- void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
+ void writeNlist(MachSymbolData &MSD, const MCAssembler &Asm);
void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
uint32_t DataSize);
diff --git a/llvm/lib/MC/MCLinkerOptimizationHint.cpp b/llvm/lib/MC/MCLinkerOptimizationHint.cpp
index 9ab321872b113..2e6fde236536a 100644
--- a/llvm/lib/MC/MCLinkerOptimizationHint.cpp
+++ b/llvm/lib/MC/MCLinkerOptimizationHint.cpp
@@ -24,23 +24,24 @@ using namespace llvm;
// - Its argN.
// <arg1> to <argN> are absolute addresses in the object file, i.e.,
// relative addresses from the beginning of the object file.
-void MCLOHDirective::emit_impl(raw_ostream &OutStream,
- const MachObjectWriter &ObjWriter,
- const MCAsmLayout &Layout) const {
+void MCLOHDirective::emit_impl(const MCAssembler &Asm, raw_ostream &OutStream,
+ const MachObjectWriter &ObjWriter
+
+) const {
encodeULEB128(Kind, OutStream);
encodeULEB128(Args.size(), OutStream);
for (const MCSymbol *Arg : Args)
- encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
+ encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Asm), OutStream);
}
-void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
- const MCAsmLayout &Layout) const {
+void MCLOHDirective::emit(const MCAssembler &Asm,
+ MachObjectWriter &ObjWriter) const {
raw_ostream &OutStream = ObjWriter.W.OS;
- emit_impl(OutStream, ObjWriter, Layout);
+ emit_impl(Asm, OutStream, ObjWriter);
}
-uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
- const MCAsmLayout &Layout) const {
+uint64_t MCLOHDirective::getEmitSize(const MCAssembler &Asm,
+ const MachObjectWriter &ObjWriter) const {
class raw_counting_ostream : public raw_ostream {
uint64_t Count = 0;
@@ -54,6 +55,6 @@ uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
};
raw_counting_ostream OutStream;
- emit_impl(OutStream, ObjWriter, Layout);
+ emit_impl(Asm, OutStream, ObjWriter);
return OutStream.tell();
}
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 5d656a04c35ef..3f5bdd88574ac 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -11,7 +11,6 @@
#include "llvm/ADT/iterator_range.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmBackend.h"
-#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAsmInfoDarwin.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
@@ -91,7 +90,7 @@ MachObjectWriter::getFragmentAddress(const MCAssembler &Asm,
}
uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
- const MCAsmLayout &Layout) const {
+ const MCAssembler &Asm) const {
// If this is a variable, then recursively evaluate now.
if (S.isVariable()) {
if (const MCConstantExpr *C =
@@ -99,8 +98,7 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
return C->getValue();
MCValue Target;
- if (!S.getVariableValue()->evaluateAsRelocatable(
- Target, &Layout.getAssembler(), nullptr))
+ if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Asm, nullptr))
report_fatal_error("unable to evaluate offset for variable '" +
S.getName() + "'");
@@ -114,14 +112,14 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
uint64_t Address = Target.getConstant();
if (Target.getSymA())
- Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout);
+ Address += getSymbolAddress(Target.getSymA()->getSymbol(), Asm);
if (Target.getSymB())
- Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout);
+ Address += getSymbolAddress(Target.getSymB()->getSymbol(), Asm);
return Address;
}
return getSectionAddress(S.getFragment()->getParent()) +
- Layout.getAssembler().getSymbolOffset(S);
+ Asm.getSymbolOffset(S);
}
uint64_t MachObjectWriter::getPaddingSize(const MCAssembler &Asm,
@@ -371,8 +369,7 @@ const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const {
return *S;
}
-void MachObjectWriter::writeNlist(MachSymbolData &MSD,
- const MCAsmLayout &Layout) {
+void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCAssembler &Asm) {
const MCSymbol *Symbol = MSD.Symbol;
const MCSymbol &Data = *Symbol;
const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol);
@@ -416,7 +413,7 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD,
if (IsAlias && Symbol->isUndefined())
Address = AliaseeInfo->StringIndex;
else if (Symbol->isDefined())
- Address = getSymbolAddress(OrigSymbol, Layout);
+ Address = getSymbolAddress(OrigSymbol, Asm);
else if (Symbol->isCommon()) {
// Common symbols are encoded with the size in the address
// field, and their alignment in the flags.
@@ -781,7 +778,6 @@ void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
}
uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
- auto &Layout = *Asm.getLayout();
uint64_t StartOffset = W.OS.tell();
populateAddrSigSection(Asm);
@@ -843,7 +839,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
}
// Add the loh load command size, if used.
- uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout);
+ uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(Asm, *this);
uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4);
if (LOHSize) {
++NumLoadCommands;
@@ -1037,10 +1033,10 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
it = Asm.data_region_begin(), ie = Asm.data_region_end();
it != ie; ++it) {
const DataRegionData *Data = &(*it);
- uint64_t Start = getSymbolAddress(*Data->Start, Layout);
+ uint64_t Start = getSymbolAddress(*Data->Start, Asm);
uint64_t End;
if (Data->End)
- End = getSymbolAddress(*Data->End, Layout);
+ End = getSymbolAddress(*Data->End, Asm);
else
report_fatal_error("Data region not terminated");
@@ -1059,7 +1055,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
#ifndef NDEBUG
unsigned Start = W.OS.tell();
#endif
- Asm.getLOHContainer().emit(*this, Layout);
+ Asm.getLOHContainer().emit(Asm, *this);
// Pad to a multiple of the pointer size.
W.OS.write_zeros(
offsetToAlignment(LOHRawSize, is64Bit() ? Align(8) : Align(4)));
@@ -1096,7 +1092,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
for (auto *SymbolData :
{&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
for (MachSymbolData &Entry : *SymbolData)
- writeNlist(Entry, Layout);
+ writeNlist(Entry, Asm);
// Write the string table.
StringTable.write(W.OS);
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
index d5177aa7a6ad1..a1f2259dc4d0d 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
@@ -278,18 +278,14 @@ void AArch64MachObjectWriter::recordRelocation(
return;
}
- Value +=
- (!A->getFragment() ? 0
- : Writer->getSymbolAddress(*A, *Asm.getLayout())) -
- (!A_Base || !A_Base->getFragment()
- ? 0
- : Writer->getSymbolAddress(*A_Base, *Asm.getLayout()));
- Value -=
- (!B->getFragment() ? 0
- : Writer->getSymbolAddress(*B, *Asm.getLayout())) -
- (!B_Base || !B_Base->getFragment()
- ? 0
- : Writer->getSymbolAddress(*B_Base, *Asm.getLayout()));
+ Value += (!A->getFragment() ? 0 : Writer->getSymbolAddress(*A, Asm)) -
+ (!A_Base || !A_Base->getFragment()
+ ? 0
+ : Writer->getSymbolAddress(*A_Base, Asm));
+ Value -= (!B->getFragment() ? 0 : Writer->getSymbolAddress(*B, Asm)) -
+ (!B_Base || !B_Base->getFragment()
+ ? 0
+ : Writer->getSymbolAddress(*B_Base, Asm));
Type = MachO::ARM64_RELOC_UNSIGNED;
@@ -358,7 +354,7 @@ void AArch64MachObjectWriter::recordRelocation(
// The index is the section ordinal (1-based).
const MCSection &Sec = Symbol->getSection();
Index = Sec.getOrdinal() + 1;
- Value += Writer->getSymbolAddress(*Symbol, *Asm.getLayout());
+ Value += Writer->getSymbolAddress(*Symbol, Asm);
if (IsPCRel)
Value -= Writer->getFragmentAddress(Asm, Fragment) + Fixup.getOffset() +
diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
index 2a2372d44b5d5..e9f2c3eaa05bd 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp
@@ -158,7 +158,7 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
return;
}
- uint32_t Value = Writer->getSymbolAddress(*A, *Asm.getLayout());
+ uint32_t Value = Writer->getSymbolAddress(*A, Asm);
uint32_t Value2 = 0;
uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
FixedValue += SecAddr;
@@ -175,7 +175,7 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
// Select the appropriate
diff erence relocation type.
Type = MachO::ARM_RELOC_HALF_SECTDIFF;
- Value2 = Writer->getSymbolAddress(B->getSymbol(), *Asm.getLayout());
+ Value2 = Writer->getSymbolAddress(B->getSymbol(), Asm);
FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
}
@@ -267,7 +267,7 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
return;
}
- uint32_t Value = Writer->getSymbolAddress(*A, *Asm.getLayout());
+ uint32_t Value = Writer->getSymbolAddress(*A, Asm);
uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
FixedValue += SecAddr;
uint32_t Value2 = 0;
@@ -285,7 +285,7 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
// Select the appropriate
diff erence relocation type.
Type = MachO::ARM_RELOC_SECTDIFF;
- Value2 = Writer->getSymbolAddress(B->getSymbol(), *Asm.getLayout());
+ Value2 = Writer->getSymbolAddress(B->getSymbol(), Asm);
FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
}
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
index d28195f070efa..a956ae323a5da 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
@@ -185,12 +185,10 @@ void X86MachObjectWriter::RecordX86_64Relocation(
return;
}
- Value +=
- Writer->getSymbolAddress(*A, *Asm.getLayout()) -
- (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, *Asm.getLayout()));
- Value -=
- Writer->getSymbolAddress(*B, *Asm.getLayout()) -
- (!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, *Asm.getLayout()));
+ Value += Writer->getSymbolAddress(*A, Asm) -
+ (!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Asm));
+ Value -= Writer->getSymbolAddress(*B, Asm) -
+ (!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, Asm));
if (!A_Base)
Index = A->getFragment()->getParent()->getOrdinal() + 1;
@@ -237,7 +235,7 @@ void X86MachObjectWriter::RecordX86_64Relocation(
} else if (Symbol->isInSection() && !Symbol->isVariable()) {
// The index is the section ordinal (1-based).
Index = Symbol->getFragment()->getParent()->getOrdinal() + 1;
- Value += Writer->getSymbolAddress(*Symbol, *Asm.getLayout());
+ Value += Writer->getSymbolAddress(*Symbol, Asm);
if (IsPCRel)
Value -= FixupAddress + (1 << Log2Size);
@@ -376,7 +374,7 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
return false;
}
- uint32_t Value = Writer->getSymbolAddress(*A, *Asm.getLayout());
+ uint32_t Value = Writer->getSymbolAddress(*A, Asm);
uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
FixedValue += SecAddr;
uint32_t Value2 = 0;
@@ -399,7 +397,7 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
// pedantic compatibility with 'as'.
Type = A->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF
: (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
- Value2 = Writer->getSymbolAddress(*SB, *Asm.getLayout());
+ Value2 = Writer->getSymbolAddress(*SB, Asm);
FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
}
@@ -476,7 +474,7 @@ void X86MachObjectWriter::recordTLVPRelocation(MachObjectWriter *Writer,
Writer->getFragmentAddress(Asm, Fragment) + Fixup.getOffset();
IsPCRel = 1;
FixedValue = FixupAddress -
- Writer->getSymbolAddress(SymB->getSymbol(), *Asm.getLayout()) +
+ Writer->getSymbolAddress(SymB->getSymbol(), Asm) +
Target.getConstant();
FixedValue += 1ULL << Log2Size;
} else {
More information about the llvm-commits
mailing list