Skip to content

Commit 4ba9956

Browse files
committed
MachObjectWriter: replace the MCAsmLayout parameter with MCAssembler
1 parent 08969ca commit 4ba9956

File tree

7 files changed

+57
-66
lines changed

7 files changed

+57
-66
lines changed

llvm/include/llvm/MC/MCLinkerOptimizationHint.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace llvm {
2626

2727
class MachObjectWriter;
28-
class MCAsmLayout;
28+
class MCAssembler;
2929
class MCSymbol;
3030
class raw_ostream;
3131

@@ -108,8 +108,8 @@ class MCLOHDirective {
108108
/// Emit this directive in \p OutStream using the information available
109109
/// in the given \p ObjWriter and \p Layout to get the address of the
110110
/// arguments within the object file.
111-
void emit_impl(raw_ostream &OutStream, const MachObjectWriter &ObjWriter,
112-
const MCAsmLayout &Layout) const;
111+
void emit_impl(const MCAssembler &Asm, raw_ostream &OutStream,
112+
const MachObjectWriter &ObjWriter) const;
113113

114114
public:
115115
using LOHArgs = SmallVectorImpl<MCSymbol *>;
@@ -125,12 +125,12 @@ class MCLOHDirective {
125125

126126
/// Emit this directive as:
127127
/// <kind, numArgs, addr1, ..., addrN>
128-
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const;
128+
void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const;
129129

130130
/// Get the size in bytes of this directive if emitted in \p ObjWriter with
131131
/// the given \p Layout.
132-
uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
133-
const MCAsmLayout &Layout) const;
132+
uint64_t getEmitSize(const MCAssembler &Asm,
133+
const MachObjectWriter &ObjWriter) const;
134134
};
135135

136136
class MCLOHContainer {
@@ -157,20 +157,20 @@ class MCLOHContainer {
157157
}
158158

159159
/// Get the size of the directives if emitted.
160-
uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
161-
const MCAsmLayout &Layout) const {
160+
uint64_t getEmitSize(const MCAssembler &Asm,
161+
const MachObjectWriter &ObjWriter) const {
162162
if (!EmitSize) {
163163
for (const MCLOHDirective &D : Directives)
164-
EmitSize += D.getEmitSize(ObjWriter, Layout);
164+
EmitSize += D.getEmitSize(Asm, ObjWriter);
165165
}
166166
return EmitSize;
167167
}
168168

169169
/// Emit all Linker Optimization Hint in one big table.
170170
/// Each line of the table is emitted by LOHDirective::emit.
171-
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
171+
void emit(const MCAssembler &Asm, MachObjectWriter &ObjWriter) const {
172172
for (const MCLOHDirective &D : Directives)
173-
D.emit(ObjWriter, Layout);
173+
D.emit(Asm, ObjWriter);
174174
}
175175

176176
void reset() {

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class MachObjectWriter : public MCObjectWriter {
161161
uint64_t getSectionAddress(const MCSection *Sec) const {
162162
return SectionAddress.lookup(Sec);
163163
}
164-
uint64_t getSymbolAddress(const MCSymbol &S, const MCAsmLayout &Layout) const;
164+
uint64_t getSymbolAddress(const MCSymbol &S, const MCAssembler &Asm) const;
165165

166166
uint64_t getFragmentAddress(const MCAssembler &Asm,
167167
const MCFragment *Fragment) const;
@@ -212,7 +212,7 @@ class MachObjectWriter : public MCObjectWriter {
212212
uint32_t FirstUndefinedSymbol, uint32_t NumUndefinedSymbols,
213213
uint32_t IndirectSymbolOffset, uint32_t NumIndirectSymbols);
214214

215-
void writeNlist(MachSymbolData &MSD, const MCAsmLayout &Layout);
215+
void writeNlist(MachSymbolData &MSD, const MCAssembler &Asm);
216216

217217
void writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset,
218218
uint32_t DataSize);

llvm/lib/MC/MCLinkerOptimizationHint.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,24 @@ using namespace llvm;
2424
// - Its argN.
2525
// <arg1> to <argN> are absolute addresses in the object file, i.e.,
2626
// relative addresses from the beginning of the object file.
27-
void MCLOHDirective::emit_impl(raw_ostream &OutStream,
28-
const MachObjectWriter &ObjWriter,
29-
const MCAsmLayout &Layout) const {
27+
void MCLOHDirective::emit_impl(const MCAssembler &Asm, raw_ostream &OutStream,
28+
const MachObjectWriter &ObjWriter
29+
30+
) const {
3031
encodeULEB128(Kind, OutStream);
3132
encodeULEB128(Args.size(), OutStream);
3233
for (const MCSymbol *Arg : Args)
33-
encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Layout), OutStream);
34+
encodeULEB128(ObjWriter.getSymbolAddress(*Arg, Asm), OutStream);
3435
}
3536

36-
void MCLOHDirective::emit(MachObjectWriter &ObjWriter,
37-
const MCAsmLayout &Layout) const {
37+
void MCLOHDirective::emit(const MCAssembler &Asm,
38+
MachObjectWriter &ObjWriter) const {
3839
raw_ostream &OutStream = ObjWriter.W.OS;
39-
emit_impl(OutStream, ObjWriter, Layout);
40+
emit_impl(Asm, OutStream, ObjWriter);
4041
}
4142

42-
uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
43-
const MCAsmLayout &Layout) const {
43+
uint64_t MCLOHDirective::getEmitSize(const MCAssembler &Asm,
44+
const MachObjectWriter &ObjWriter) const {
4445
class raw_counting_ostream : public raw_ostream {
4546
uint64_t Count = 0;
4647

@@ -54,6 +55,6 @@ uint64_t MCLOHDirective::getEmitSize(const MachObjectWriter &ObjWriter,
5455
};
5556

5657
raw_counting_ostream OutStream;
57-
emit_impl(OutStream, ObjWriter, Layout);
58+
emit_impl(Asm, OutStream, ObjWriter);
5859
return OutStream.tell();
5960
}

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "llvm/ADT/iterator_range.h"
1212
#include "llvm/BinaryFormat/MachO.h"
1313
#include "llvm/MC/MCAsmBackend.h"
14-
#include "llvm/MC/MCAsmLayout.h"
1514
#include "llvm/MC/MCAsmInfoDarwin.h"
1615
#include "llvm/MC/MCAssembler.h"
1716
#include "llvm/MC/MCContext.h"
@@ -91,16 +90,15 @@ MachObjectWriter::getFragmentAddress(const MCAssembler &Asm,
9190
}
9291

9392
uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
94-
const MCAsmLayout &Layout) const {
93+
const MCAssembler &Asm) const {
9594
// If this is a variable, then recursively evaluate now.
9695
if (S.isVariable()) {
9796
if (const MCConstantExpr *C =
9897
dyn_cast<const MCConstantExpr>(S.getVariableValue()))
9998
return C->getValue();
10099

101100
MCValue Target;
102-
if (!S.getVariableValue()->evaluateAsRelocatable(
103-
Target, &Layout.getAssembler(), nullptr))
101+
if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Asm, nullptr))
104102
report_fatal_error("unable to evaluate offset for variable '" +
105103
S.getName() + "'");
106104

@@ -114,14 +112,14 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
114112

115113
uint64_t Address = Target.getConstant();
116114
if (Target.getSymA())
117-
Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout);
115+
Address += getSymbolAddress(Target.getSymA()->getSymbol(), Asm);
118116
if (Target.getSymB())
119-
Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout);
117+
Address += getSymbolAddress(Target.getSymB()->getSymbol(), Asm);
120118
return Address;
121119
}
122120

123121
return getSectionAddress(S.getFragment()->getParent()) +
124-
Layout.getAssembler().getSymbolOffset(S);
122+
Asm.getSymbolOffset(S);
125123
}
126124

127125
uint64_t MachObjectWriter::getPaddingSize(const MCAssembler &Asm,
@@ -371,8 +369,7 @@ const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const {
371369
return *S;
372370
}
373371

374-
void MachObjectWriter::writeNlist(MachSymbolData &MSD,
375-
const MCAsmLayout &Layout) {
372+
void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCAssembler &Asm) {
376373
const MCSymbol *Symbol = MSD.Symbol;
377374
const MCSymbol &Data = *Symbol;
378375
const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol);
@@ -416,7 +413,7 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD,
416413
if (IsAlias && Symbol->isUndefined())
417414
Address = AliaseeInfo->StringIndex;
418415
else if (Symbol->isDefined())
419-
Address = getSymbolAddress(OrigSymbol, Layout);
416+
Address = getSymbolAddress(OrigSymbol, Asm);
420417
else if (Symbol->isCommon()) {
421418
// Common symbols are encoded with the size in the address
422419
// field, and their alignment in the flags.
@@ -781,7 +778,6 @@ void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
781778
}
782779

783780
uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
784-
auto &Layout = *Asm.getLayout();
785781
uint64_t StartOffset = W.OS.tell();
786782

787783
populateAddrSigSection(Asm);
@@ -843,7 +839,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
843839
}
844840

845841
// Add the loh load command size, if used.
846-
uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout);
842+
uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(Asm, *this);
847843
uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4);
848844
if (LOHSize) {
849845
++NumLoadCommands;
@@ -1037,10 +1033,10 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
10371033
it = Asm.data_region_begin(), ie = Asm.data_region_end();
10381034
it != ie; ++it) {
10391035
const DataRegionData *Data = &(*it);
1040-
uint64_t Start = getSymbolAddress(*Data->Start, Layout);
1036+
uint64_t Start = getSymbolAddress(*Data->Start, Asm);
10411037
uint64_t End;
10421038
if (Data->End)
1043-
End = getSymbolAddress(*Data->End, Layout);
1039+
End = getSymbolAddress(*Data->End, Asm);
10441040
else
10451041
report_fatal_error("Data region not terminated");
10461042

@@ -1059,7 +1055,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
10591055
#ifndef NDEBUG
10601056
unsigned Start = W.OS.tell();
10611057
#endif
1062-
Asm.getLOHContainer().emit(*this, Layout);
1058+
Asm.getLOHContainer().emit(Asm, *this);
10631059
// Pad to a multiple of the pointer size.
10641060
W.OS.write_zeros(
10651061
offsetToAlignment(LOHRawSize, is64Bit() ? Align(8) : Align(4)));
@@ -1096,7 +1092,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
10961092
for (auto *SymbolData :
10971093
{&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
10981094
for (MachSymbolData &Entry : *SymbolData)
1099-
writeNlist(Entry, Layout);
1095+
writeNlist(Entry, Asm);
11001096

11011097
// Write the string table.
11021098
StringTable.write(W.OS);

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,14 @@ void AArch64MachObjectWriter::recordRelocation(
278278
return;
279279
}
280280

281-
Value +=
282-
(!A->getFragment() ? 0
283-
: Writer->getSymbolAddress(*A, *Asm.getLayout())) -
284-
(!A_Base || !A_Base->getFragment()
285-
? 0
286-
: Writer->getSymbolAddress(*A_Base, *Asm.getLayout()));
287-
Value -=
288-
(!B->getFragment() ? 0
289-
: Writer->getSymbolAddress(*B, *Asm.getLayout())) -
290-
(!B_Base || !B_Base->getFragment()
291-
? 0
292-
: Writer->getSymbolAddress(*B_Base, *Asm.getLayout()));
281+
Value += (!A->getFragment() ? 0 : Writer->getSymbolAddress(*A, Asm)) -
282+
(!A_Base || !A_Base->getFragment()
283+
? 0
284+
: Writer->getSymbolAddress(*A_Base, Asm));
285+
Value -= (!B->getFragment() ? 0 : Writer->getSymbolAddress(*B, Asm)) -
286+
(!B_Base || !B_Base->getFragment()
287+
? 0
288+
: Writer->getSymbolAddress(*B_Base, Asm));
293289

294290
Type = MachO::ARM64_RELOC_UNSIGNED;
295291

@@ -358,7 +354,7 @@ void AArch64MachObjectWriter::recordRelocation(
358354
// The index is the section ordinal (1-based).
359355
const MCSection &Sec = Symbol->getSection();
360356
Index = Sec.getOrdinal() + 1;
361-
Value += Writer->getSymbolAddress(*Symbol, *Asm.getLayout());
357+
Value += Writer->getSymbolAddress(*Symbol, Asm);
362358

363359
if (IsPCRel)
364360
Value -= Writer->getFragmentAddress(Asm, Fragment) + Fixup.getOffset() +

llvm/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
158158
return;
159159
}
160160

161-
uint32_t Value = Writer->getSymbolAddress(*A, *Asm.getLayout());
161+
uint32_t Value = Writer->getSymbolAddress(*A, Asm);
162162
uint32_t Value2 = 0;
163163
uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
164164
FixedValue += SecAddr;
@@ -175,7 +175,7 @@ void ARMMachObjectWriter::recordARMScatteredHalfRelocation(
175175

176176
// Select the appropriate difference relocation type.
177177
Type = MachO::ARM_RELOC_HALF_SECTDIFF;
178-
Value2 = Writer->getSymbolAddress(B->getSymbol(), *Asm.getLayout());
178+
Value2 = Writer->getSymbolAddress(B->getSymbol(), Asm);
179179
FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
180180
}
181181

@@ -267,7 +267,7 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
267267
return;
268268
}
269269

270-
uint32_t Value = Writer->getSymbolAddress(*A, *Asm.getLayout());
270+
uint32_t Value = Writer->getSymbolAddress(*A, Asm);
271271
uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
272272
FixedValue += SecAddr;
273273
uint32_t Value2 = 0;
@@ -285,7 +285,7 @@ void ARMMachObjectWriter::recordARMScatteredRelocation(
285285

286286
// Select the appropriate difference relocation type.
287287
Type = MachO::ARM_RELOC_SECTDIFF;
288-
Value2 = Writer->getSymbolAddress(B->getSymbol(), *Asm.getLayout());
288+
Value2 = Writer->getSymbolAddress(B->getSymbol(), Asm);
289289
FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
290290
}
291291

llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,10 @@ void X86MachObjectWriter::RecordX86_64Relocation(
185185
return;
186186
}
187187

188-
Value +=
189-
Writer->getSymbolAddress(*A, *Asm.getLayout()) -
190-
(!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, *Asm.getLayout()));
191-
Value -=
192-
Writer->getSymbolAddress(*B, *Asm.getLayout()) -
193-
(!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, *Asm.getLayout()));
188+
Value += Writer->getSymbolAddress(*A, Asm) -
189+
(!A_Base ? 0 : Writer->getSymbolAddress(*A_Base, Asm));
190+
Value -= Writer->getSymbolAddress(*B, Asm) -
191+
(!B_Base ? 0 : Writer->getSymbolAddress(*B_Base, Asm));
194192

195193
if (!A_Base)
196194
Index = A->getFragment()->getParent()->getOrdinal() + 1;
@@ -237,7 +235,7 @@ void X86MachObjectWriter::RecordX86_64Relocation(
237235
} else if (Symbol->isInSection() && !Symbol->isVariable()) {
238236
// The index is the section ordinal (1-based).
239237
Index = Symbol->getFragment()->getParent()->getOrdinal() + 1;
240-
Value += Writer->getSymbolAddress(*Symbol, *Asm.getLayout());
238+
Value += Writer->getSymbolAddress(*Symbol, Asm);
241239

242240
if (IsPCRel)
243241
Value -= FixupAddress + (1 << Log2Size);
@@ -376,7 +374,7 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
376374
return false;
377375
}
378376

379-
uint32_t Value = Writer->getSymbolAddress(*A, *Asm.getLayout());
377+
uint32_t Value = Writer->getSymbolAddress(*A, Asm);
380378
uint64_t SecAddr = Writer->getSectionAddress(A->getFragment()->getParent());
381379
FixedValue += SecAddr;
382380
uint32_t Value2 = 0;
@@ -399,7 +397,7 @@ bool X86MachObjectWriter::recordScatteredRelocation(MachObjectWriter *Writer,
399397
// pedantic compatibility with 'as'.
400398
Type = A->isExternal() ? (unsigned)MachO::GENERIC_RELOC_SECTDIFF
401399
: (unsigned)MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
402-
Value2 = Writer->getSymbolAddress(*SB, *Asm.getLayout());
400+
Value2 = Writer->getSymbolAddress(*SB, Asm);
403401
FixedValue -= Writer->getSectionAddress(SB->getFragment()->getParent());
404402
}
405403

@@ -476,7 +474,7 @@ void X86MachObjectWriter::recordTLVPRelocation(MachObjectWriter *Writer,
476474
Writer->getFragmentAddress(Asm, Fragment) + Fixup.getOffset();
477475
IsPCRel = 1;
478476
FixedValue = FixupAddress -
479-
Writer->getSymbolAddress(SymB->getSymbol(), *Asm.getLayout()) +
477+
Writer->getSymbolAddress(SymB->getSymbol(), Asm) +
480478
Target.getConstant();
481479
FixedValue += 1ULL << Log2Size;
482480
} else {

0 commit comments

Comments
 (0)