Skip to content

Commit 976de53

Browse files
committed
[MC,MachO] Replace SectionAddrMap workaround with cleaner variable handling
Mach-O's ARM and X86 writers use MCExpr's `SectionAddrMap *Addrs` argument to compute label differences, which was a bit of a hack. The AArch64MachObjectWriter does this better by using `getSymbolAddress` in its `recordRelocation` function. This commit: 1. Moves the `SectionAddrMap` logic into the Mach-O code, removing the workaround. 2. Fixes a bug in `MachObjectWriter::getSymbolAddress` where it failed to subtract the `SymB` value. This bug has been present since commit b200f93 (2011).
1 parent eb70253 commit 976de53

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
120120
if (Target.getAddSym())
121121
Address += getSymbolAddress(*Target.getAddSym(), Asm);
122122
if (Target.getSubSym())
123-
Address += getSymbolAddress(*Target.getSubSym(), Asm);
123+
Address -= getSymbolAddress(*Target.getSubSym(), Asm);
124124
return Address;
125125
}
126126

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,17 @@ void ARMMachObjectWriter::recordRelocation(MachObjectWriter *Writer,
416416
} else {
417417
// Resolve constant variables.
418418
if (A->isVariable()) {
419-
int64_t Res;
420-
if (A->getVariableValue()->evaluateAsAbsolute(
421-
Res, Asm, Writer->getSectionAddressMap())) {
419+
MCValue Val;
420+
bool Relocatable =
421+
A->getVariableValue()->evaluateAsRelocatable(Val, &Asm);
422+
int64_t Res = Val.getConstant();
423+
bool isAbs = Val.isAbsolute();
424+
if (Relocatable && Val.getAddSym() && Val.getSubSym()) {
425+
Res += Writer->getSymbolAddress(*Val.getAddSym(), Asm) -
426+
Writer->getSymbolAddress(*Val.getSubSym(), Asm);
427+
isAbs = true;
428+
}
429+
if (isAbs) {
422430
FixedValue = Res;
423431
return;
424432
}

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,8 @@ void X86MachObjectWriter::RecordX86_64Relocation(
246246
if (IsPCRel)
247247
Value -= FixupAddress + (1 << Log2Size);
248248
} else if (Symbol->isVariable()) {
249-
const MCExpr *Value = Symbol->getVariableValue();
250-
int64_t Res;
251-
bool isAbs =
252-
Value->evaluateAsAbsolute(Res, Asm, Writer->getSectionAddressMap());
253-
if (isAbs) {
254-
FixedValue = Res;
255-
return;
256-
} else {
257-
Asm.getContext().reportError(Fixup.getLoc(),
258-
"unsupported relocation of variable '" +
259-
Symbol->getName() + "'");
260-
return;
261-
}
249+
FixedValue = Writer->getSymbolAddress(*Symbol, Asm);
250+
return;
262251
} else {
263252
Asm.getContext().reportError(
264253
Fixup.getLoc(), "unsupported relocation of undefined symbol '" +
@@ -548,9 +537,17 @@ void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer,
548537

549538
// Resolve constant variables.
550539
if (A->isVariable()) {
551-
int64_t Res;
552-
if (A->getVariableValue()->evaluateAsAbsolute(
553-
Res, Asm, Writer->getSectionAddressMap())) {
540+
MCValue Val;
541+
bool Relocatable =
542+
A->getVariableValue()->evaluateAsRelocatable(Val, &Asm);
543+
int64_t Res = Val.getConstant();
544+
bool isAbs = Val.isAbsolute();
545+
if (Relocatable && Val.getAddSym() && Val.getSubSym()) {
546+
Res += Writer->getSymbolAddress(*Val.getAddSym(), Asm) -
547+
Writer->getSymbolAddress(*Val.getSubSym(), Asm);
548+
isAbs = true;
549+
}
550+
if (isAbs) {
554551
FixedValue = Res;
555552
return;
556553
}

0 commit comments

Comments
 (0)