Skip to content

Commit c3c67e9

Browse files
committed
[XCOFF][AIX] Relocation support for SymB
This patch intends to provide relocation support for the expression contains two unpaired relocatable terms with opposite signs. Differential Revision: https://reviews.llvm.org/D77424
1 parent 4578fa8 commit c3c67e9

File tree

2 files changed

+133
-19
lines changed

2 files changed

+133
-19
lines changed

Diff for: llvm/lib/MC/XCOFFObjectWriter.cpp

+50-19
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,26 @@ void XCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
384384
const MCFragment *Fragment,
385385
const MCFixup &Fixup, MCValue Target,
386386
uint64_t &FixedValue) {
387-
388-
if (Target.getSymB())
389-
report_fatal_error("Handling Target.SymB for relocation is unimplemented.");
390-
391-
const MCSymbol &SymA = Target.getSymA()->getSymbol();
387+
auto getIndex = [this](const MCSymbol *Sym,
388+
const MCSectionXCOFF *ContainingCsect) {
389+
// If we could not find the symbol directly in SymbolIndexMap, this symbol
390+
// could either be a temporary symbol or an undefined symbol. In this case,
391+
// we would need to have the relocation reference its csect instead.
392+
return SymbolIndexMap.find(Sym) != SymbolIndexMap.end()
393+
? SymbolIndexMap[Sym]
394+
: SymbolIndexMap[ContainingCsect->getQualNameSymbol()];
395+
};
396+
397+
auto getVirtualAddress = [this,
398+
&Layout](const MCSymbol *Sym,
399+
const MCSectionXCOFF *ContainingCsect) {
400+
// If Sym is a csect, return csect's address.
401+
// If Sym is a label, return csect's address + label's offset from the csect.
402+
return SectionMap[ContainingCsect]->Address +
403+
(Sym->isDefined() ? Layout.getSymbolOffset(*Sym) : 0);
404+
};
405+
406+
const MCSymbol *const SymA = &Target.getSymA()->getSymbol();
392407

393408
MCAsmBackend &Backend = Asm.getBackend();
394409
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
@@ -399,26 +414,15 @@ void XCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
399414
std::tie(Type, SignAndSize) =
400415
TargetObjectWriter->getRelocTypeAndSignSize(Target, Fixup, IsPCRel);
401416

402-
const MCSectionXCOFF *SymASec =
403-
getContainingCsect(cast<MCSymbolXCOFF>(&SymA));
417+
const MCSectionXCOFF *SymASec = getContainingCsect(cast<MCSymbolXCOFF>(SymA));
404418
assert(SectionMap.find(SymASec) != SectionMap.end() &&
405419
"Expected containing csect to exist in map.");
406420

407-
// If we could not find SymA directly in SymbolIndexMap, this symbol could
408-
// either be a temporary symbol or an undefined symbol. In this case, we
409-
// would need to have the relocation reference its csect instead.
410-
uint32_t Index = SymbolIndexMap.find(&SymA) != SymbolIndexMap.end()
411-
? SymbolIndexMap[&SymA]
412-
: SymbolIndexMap[SymASec->getQualNameSymbol()];
413-
421+
const uint32_t Index = getIndex(SymA, SymASec);
414422
if (Type == XCOFF::RelocationType::R_POS)
415423
// The FixedValue should be symbol's virtual address in this object file
416424
// plus any constant value that we might get.
417-
// Notice that SymA.isDefined() could return false, but SymASec could still
418-
// be a defined csect. One of the example is the TOC-base symbol.
419-
FixedValue = SectionMap[SymASec]->Address +
420-
(SymA.isDefined() ? Layout.getSymbolOffset(SymA) : 0) +
421-
Target.getConstant();
425+
FixedValue = getVirtualAddress(SymA, SymASec) + Target.getConstant();
422426
else if (Type == XCOFF::RelocationType::R_TOC)
423427
// The FixedValue should be the TC entry offset from TOC-base.
424428
FixedValue = SectionMap[SymASec]->Address - TOCCsects.front().Address;
@@ -435,6 +439,33 @@ void XCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
435439
assert(SectionMap.find(RelocationSec) != SectionMap.end() &&
436440
"Expected containing csect to exist in map.");
437441
SectionMap[RelocationSec]->Relocations.push_back(Reloc);
442+
443+
if (!Target.getSymB())
444+
return;
445+
446+
const MCSymbol *const SymB = &Target.getSymB()->getSymbol();
447+
if (SymA == SymB)
448+
report_fatal_error("relocation for opposite term is not yet supported");
449+
450+
const MCSectionXCOFF *SymBSec = getContainingCsect(cast<MCSymbolXCOFF>(SymB));
451+
assert(SectionMap.find(SymBSec) != SectionMap.end() &&
452+
"Expected containing csect to exist in map.");
453+
if (SymASec == SymBSec)
454+
report_fatal_error(
455+
"relocation for paired relocatable term is not yet supported");
456+
457+
assert(Type == XCOFF::RelocationType::R_POS &&
458+
"SymA must be R_POS here if it's not opposite term or paired "
459+
"relocatable term.");
460+
const uint32_t IndexB = getIndex(SymB, SymBSec);
461+
// SymB must be R_NEG here, given the general form of Target(MCValue) is
462+
// "SymbolA - SymbolB + imm64".
463+
const uint8_t TypeB = XCOFF::RelocationType::R_NEG;
464+
XCOFFRelocation RelocB = {IndexB, FixupOffsetInCsect, SignAndSize, TypeB};
465+
SectionMap[RelocationSec]->Relocations.push_back(RelocB);
466+
// We already folded "SymbolA + imm64" above when Type is R_POS for SymbolA,
467+
// now we just need to fold "- SymbolB" here.
468+
FixedValue -= getVirtualAddress(SymB, SymBSec);
438469
}
439470

440471
void XCOFFObjectWriter::writeSections(const MCAssembler &Asm,

Diff for: llvm/test/CodeGen/PowerPC/aix-xcoff-reloc-symb.mir

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# RUN: llc -verify-machineinstrs -mcpu=pwr4 -mtriple powerpc-ibm-aix-xcoff -x mir -verify-machineinstrs -start-after=lazy-machine-block-freq -filetype=obj -o %t.o < %s
2+
# RUN: llvm-readobj --relocs --expand-relocs -t %t.o | FileCheck --check-prefixes=RELOC,SYM %s
3+
# RUN: llvm-objdump -D %t.o | FileCheck --check-prefix=DIS %s
4+
5+
---
6+
name: foo
7+
alignment: 16
8+
tracksRegLiveness: true
9+
jumpTable:
10+
kind: label-difference32
11+
entries:
12+
- id: 0
13+
blocks: [ '%bb.0' ]
14+
body: |
15+
bb.0:
16+
successors: %bb.0(0x20000000)
17+
liveins: $r2
18+
renamable $r3 = LWZtoc %jump-table.0, $r2 :: (load 4 from got)
19+
BLR implicit $lr, implicit $rm, implicit killed $r3
20+
...
21+
22+
# RELOC: Relocation {{[{][[:space:]] *}}Virtual Address: 0x8
23+
# RELOC-NEXT: Symbol: .text ([[#TXT_INDX:]])
24+
# RELOC-NEXT: IsSigned: No
25+
# RELOC-NEXT: FixupBitValue: 0
26+
# RELOC-NEXT: Length: 32
27+
# RELOC-NEXT: Type: R_POS (0x0)
28+
# RELOC-NEXT: }
29+
# RELOC-NEXT: Relocation {
30+
# RELOC-NEXT: Virtual Address: 0x8
31+
# RELOC-NEXT: Symbol: .rodata ([[#RO_INDX:]])
32+
# RELOC-NEXT: IsSigned: No
33+
# RELOC-NEXT: FixupBitValue: 0
34+
# RELOC-NEXT: Length: 32
35+
# RELOC-NEXT: Type: R_NEG (0x1)
36+
# RELOC-NEXT: }
37+
38+
# SYM: Symbol {{[{][[:space:]] *}}Index: [[#TXT_INDX]]{{[[:space:]] *}}Name: .text
39+
# SYM-NEXT: Value (RelocatableAddress): 0x0
40+
# SYM-NEXT: Section: .text
41+
# SYM-NEXT: Type: 0x0
42+
# SYM-NEXT: StorageClass: C_HIDEXT (0x6B)
43+
# SYM-NEXT: NumberOfAuxEntries: 1
44+
# SYM-NEXT: CSECT Auxiliary Entry {
45+
# SYM-NEXT: Index: [[#TXT_INDX+1]]
46+
# SYM-NEXT: SectionLen: 8
47+
# SYM-NEXT: ParameterHashIndex: 0x0
48+
# SYM-NEXT: TypeChkSectNum: 0x0
49+
# SYM-NEXT: SymbolAlignmentLog2: 4
50+
# SYM-NEXT: SymbolType: XTY_SD (0x1)
51+
# SYM-NEXT: StorageMappingClass: XMC_PR (0x0)
52+
# SYM-NEXT: StabInfoIndex: 0x0
53+
# SYM-NEXT: StabSectNum: 0x0
54+
# SYM-NEXT: }
55+
# SYM-NEXT: }
56+
# SYM: Symbol {{[{][[:space:]] *}}Index: [[#RO_INDX]]{{[[:space:]] *}}Name: .rodata
57+
# SYM-NEXT: Value (RelocatableAddress): 0x8
58+
# SYM-NEXT: Section: .text
59+
# SYM-NEXT: Type: 0x0
60+
# SYM-NEXT: StorageClass: C_HIDEXT (0x6B)
61+
# SYM-NEXT: NumberOfAuxEntries: 1
62+
# SYM-NEXT: CSECT Auxiliary Entry {
63+
# SYM-NEXT: Index: [[#RO_INDX+1]]
64+
# SYM-NEXT: SectionLen: 4
65+
# SYM-NEXT: ParameterHashIndex: 0x0
66+
# SYM-NEXT: TypeChkSectNum: 0x0
67+
# SYM-NEXT: SymbolAlignmentLog2: 2
68+
# SYM-NEXT: SymbolType: XTY_SD (0x1)
69+
# SYM-NEXT: StorageMappingClass: XMC_RO (0x1)
70+
# SYM-NEXT: StabInfoIndex: 0x0
71+
# SYM-NEXT: StabSectNum: 0x0
72+
# SYM-NEXT: }
73+
# SYM-NEXT: }
74+
75+
# DIS: Disassembly of section .text:
76+
# DIS-EMPTY:
77+
# DIS-NEXT: 00000000 <.text>:
78+
# DIS-NEXT: 0: 80 62 00 00 lwz 3, 0(2)
79+
# DIS-NEXT: 4: 4e 80 00 20 blr
80+
# DIS-EMPTY:
81+
# DIS-NEXT: 00000008 <.rodata>:
82+
# DIS-NEXT: 8: ff ff ff f8 fmsub 31, 31, 31, 31
83+
# DIS-EMPTY:

0 commit comments

Comments
 (0)