Skip to content

Commit e5fd61b

Browse files
MaskRaygithub-actions[bot]
authored andcommitted
Automerge: MC: Generate relocation for a branch crossing linker-relaxable FT_Align fragment
"Encode FT_Align in fragment's variable-size tail" or a neighbor change caused a regression that was similar to the root cause of ab0931b (See the new test (.text3 with a call at the start")) For a FT_Align fragment, the offset between location A (with offset <= FixedSize) and B (offset == FixedSize+VarSize) cannot be resolved. In addition, delete unneeded condition `F->isLinkerRelaxable()`. LoongArch linker relaxation is largely under-tested, but update it as well.
2 parents 7fabfb0 + 69d0078 commit e5fd61b

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

llvm/lib/MC/MCExpr.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,16 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
346346
Displacement *= -1;
347347
}
348348

349-
// Track whether B is before a relaxable instruction and whether A is after
350-
// a relaxable instruction. If SA and SB are separated by a linker-relaxable
351-
// instruction, the difference cannot be resolved as it may be changed by
352-
// the linker.
349+
// Track whether B is before a relaxable instruction/alignment and whether A
350+
// is after a relaxable instruction/alignment. If SA and SB are separated by
351+
// a linker-relaxable instruction/alignment, the difference cannot be
352+
// resolved as it may be changed by the linker.
353353
bool BBeforeRelax = false, AAfterRelax = false;
354354
for (auto F = FB; F; F = F->getNext()) {
355-
auto DF = F->getKind() == MCFragment::FT_Data ? F : nullptr;
356-
if (DF && DF->isLinkerRelaxable()) {
357-
if (&*F != FB || SBOffset != DF->getContents().size())
355+
if (F && F->isLinkerRelaxable()) {
356+
if (&*F != FB || SBOffset != F->getSize())
358357
BBeforeRelax = true;
359-
if (&*F != FA || SAOffset == DF->getContents().size())
358+
if (&*F != FA || SAOffset == F->getSize())
360359
AAfterRelax = true;
361360
if (BBeforeRelax && AAfterRelax)
362361
return;
@@ -370,17 +369,15 @@ static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
370369
}
371370

372371
int64_t Num;
373-
if (DF) {
374-
Displacement += DF->getContents().size();
375-
} else if (F->getKind() == MCFragment::FT_Relaxable &&
372+
if (F->getKind() == MCFragment::FT_Data) {
373+
Displacement += F->getFixedSize();
374+
} else if ((F->getKind() == MCFragment::FT_Relaxable ||
375+
F->getKind() == MCFragment::FT_Align) &&
376376
Asm->hasFinalLayout()) {
377377
// Before finishLayout, a relaxable fragment's size is indeterminate.
378378
// After layout, during relocation generation, it can be treated as a
379379
// data fragment.
380380
Displacement += F->getSize();
381-
} else if (F->getKind() == MCFragment::FT_Align && Layout &&
382-
F->isLinkerRelaxable()) {
383-
Displacement += Asm->computeFragmentSize(*F);
384381
} else if (auto *FF = dyn_cast<MCFillFragment>(F);
385382
FF && FF->getNumValues().evaluateAsAbsolute(Num)) {
386383
Displacement += Num * FF->getValueSize();

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ bool LoongArchAsmBackend::relaxAlign(MCFragment &F, unsigned &Size) {
254254
MCFixup Fixup =
255255
MCFixup::create(0, Expr, FirstLiteralRelocationKind + ELF::R_LARCH_ALIGN);
256256
F.setVarFixups({Fixup});
257+
F.setLinkerRelaxable();
257258
F.getParent()->setLinkerRelaxable();
258259
return true;
259260
}

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ bool RISCVAsmBackend::relaxAlign(MCFragment &F, unsigned &Size) {
320320
MCFixup Fixup =
321321
MCFixup::create(0, Expr, FirstLiteralRelocationKind + ELF::R_RISCV_ALIGN);
322322
F.setVarFixups({Fixup});
323+
F.setLinkerRelaxable();
323324
F.getParent()->setLinkerRelaxable();
324325
return true;
325326
}

llvm/test/MC/RISCV/Relocations/mc-dump.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
# CHECK-NEXT:0 Data LinkerRelaxable Size:8 [97,00,00,00,e7,80,00,00]
1010
# CHECK-NEXT: Fixup @0 Value:specifier(19,ext) Kind:4023
1111
# CHECK-NEXT: Symbol @0 $x
12-
# CHECK-NEXT:8 Align Size:0+4 []
12+
# CHECK-NEXT:8 Align LinkerRelaxable Size:0+4 []
1313
# CHECK-NEXT: Align:8 Fill:0 FillLen:1 MaxBytesToEmit:8 Nops
1414
# CHECK-NEXT: Fixup @0 Value:4 Kind:[[#]]
15-
# CHECK-NEXT:12 Align Size:4+4 [13,05,30,00]
15+
# CHECK-NEXT:12 Align LinkerRelaxable Size:4+4 [13,05,30,00]
1616
# CHECK-NEXT: Align:8 Fill:0 FillLen:1 MaxBytesToEmit:8 Nops
1717
# CHECK-NEXT: Fixup @4 Value:4 Kind:[[#]]
1818
# CHECK-NEXT:]

llvm/test/MC/RISCV/align.s

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,41 @@ data2:
154154
2:
155155
bnez t1, 1b
156156
bnez t1, 2b
157+
158+
## .text3 with a call at the start
159+
# NORELAX-RELOC: .rela.text3a
160+
# C-OR-ZCA-EXT-NORELAX-RELOC: .rela.text3a
161+
# RELAX-RELOC: .rela.text3a {
162+
# RELAX-RELOC-NEXT: 0x0 R_RISCV_CALL_PLT foo 0x0
163+
# RELAX-RELOC-NEXT: 0x0 R_RISCV_RELAX - 0x0
164+
# RELAX-RELOC-NEXT: 0xC R_RISCV_BRANCH .Ltmp[[#]] 0x0
165+
# RELAX-RELOC-NEXT: 0x10 R_RISCV_ALIGN - 0x4
166+
# RELAX-RELOC-NEXT: 0x14 R_RISCV_BRANCH .Ltmp[[#]] 0x0
167+
# RELAX-RELOC-NEXT: }
168+
.section .text3a, "ax"
169+
call foo
170+
bnez t1, 1f
171+
bnez t2, 2f
172+
1:
173+
.p2align 3
174+
2:
175+
bnez t1, 1b
176+
bnez t1, 2b
177+
178+
## .text3 with a call at the end
179+
# RELAX-RELOC: .rela.text3b {
180+
# RELAX-RELOC-NEXT: 0x4 R_RISCV_BRANCH .Ltmp[[#]] 0x0
181+
# RELAX-RELOC-NEXT: 0x8 R_RISCV_ALIGN - 0x4
182+
# RELAX-RELOC-NEXT: 0xC R_RISCV_BRANCH .Ltmp[[#]] 0x0
183+
# RELAX-RELOC-NEXT: 0x14 R_RISCV_CALL_PLT foo 0x0
184+
# RELAX-RELOC-NEXT: 0x14 R_RISCV_RELAX - 0x0
185+
# RELAX-RELOC-NEXT: }
186+
.section .text3b, "ax"
187+
bnez t1, 1f
188+
bnez t2, 2f
189+
1:
190+
.p2align 3
191+
2:
192+
bnez t1, 1b
193+
bnez t1, 2b
194+
call foo

0 commit comments

Comments
 (0)