Skip to content

Commit 5d14274

Browse files
MaskRaysvkeerthy
authored andcommitted
MC: Set MCSection::LinkerRelaxable for linker-relaxable MCAlignFragment
Commit bb03cdc caused a Linux kernel regression ClangBuiltLinux/linux#2091 When a section contains linker-relaxable MCAlignmentFragment but no linker-relaxable instructions, the RISCVAsmBackend::isPCRelFixupResolved code path should be taken as well. The #76552 condition in the fragment walk code will make the fixup unresolvable, leading to a relocation.
1 parent 6e3f7df commit 5d14274

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,14 @@ void MCObjectStreamer::emitCodeAlignment(Align Alignment,
586586
const MCSubtargetInfo *STI,
587587
unsigned MaxBytesToEmit) {
588588
emitValueToAlignment(Alignment, 0, 1, MaxBytesToEmit);
589-
cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true, STI);
589+
auto *F = cast<MCAlignFragment>(getCurrentFragment());
590+
F->setEmitNops(true, STI);
591+
// With RISC-V style linker relaxation, mark the section as linker-relaxable
592+
// if the alignment is larger than the minimum NOP size.
593+
unsigned Size;
594+
if (getAssembler().getBackend().shouldInsertExtraNopBytesForCodeAlign(*F,
595+
Size))
596+
getCurrentSectionOnly()->setLinkerRelaxable();
590597
}
591598

592599
void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
546546

547547
bool RISCVAsmBackend::isPCRelFixupResolved(const MCSymbol *SymA,
548548
const MCFragment &F) {
549-
// If the section does not contain linker-relaxable instructions, PC-relative
549+
// If the section does not contain linker-relaxable fragments, PC-relative
550550
// fixups can be resolved.
551551
if (!F.getParent()->isLinkerRelaxable())
552552
return true;

llvm/test/MC/RISCV/align.s

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,23 @@ data2:
134134
.option norvc
135135
.balign 4
136136
add a0, a0, a1
137+
138+
## Branches crossing the linker-relaxable R_RISCV_ALIGN need relocations.
139+
# RELAX-RELOC: .rela.text3 {
140+
# RELAX-RELOC-NEXT: 0x4 R_RISCV_BRANCH .Ltmp[[#]] 0x0
141+
# RELAX-RELOC-NEXT: 0x8 R_RISCV_ALIGN - 0x4
142+
# RELAX-RELOC-NEXT: 0xC R_RISCV_BRANCH .Ltmp[[#]] 0x0
143+
# RELAX-RELOC-NEXT: }
144+
# C-OR-ZCA-EXT-RELAX-RELOC: .rela.text3 {
145+
# C-OR-ZCA-EXT-RELAX-RELOC-NEXT: 0x4 R_RISCV_BRANCH .Ltmp[[#]] 0x0
146+
# C-OR-ZCA-EXT-RELAX-RELOC-NEXT: 0x8 R_RISCV_ALIGN - 0x4
147+
# C-OR-ZCA-EXT-RELAX-RELOC-NEXT: 0xC R_RISCV_BRANCH .Ltmp[[#]] 0x0
148+
# C-OR-ZCA-EXT-RELAX-RELOC-NEXT: }
149+
.section .text3, "ax"
150+
bnez t1, 1f
151+
bnez t2, 2f
152+
1:
153+
.p2align 3
154+
2:
155+
bnez t1, 1b
156+
bnez t1, 2b

llvm/test/MC/RISCV/cfi-advance.s

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@
1313
# RELAX: R_RISCV_CALL_PLT
1414
# RELAX-NEXT: R_RISCV_RELAX
1515
# RELAX-EMPTY:
16-
# CHECK-NEXT: Relocation section '.rela.eh_frame' at offset {{.*}} contains 1 entries:
17-
# CHECK: Offset Info Type Sym. Value Symbol's Name + Addend
18-
# CHECK-NEXT: 0000001c 00000139 R_RISCV_32_PCREL 00000000 .L0 + 0
16+
# NORELAX-NEXT: Relocation section '.rela.eh_frame' at offset {{.*}} contains 1 entries:
17+
# NORELAX: Offset Info Type Sym. Value Symbol's Name + Addend
18+
# NORELAX-NEXT: 0000001c 00000139 R_RISCV_32_PCREL 00000000 .L0 + 0
19+
# RELAX-NEXT: Relocation section '.rela.eh_frame' at offset {{.*}} contains 5 entries:
20+
# RELAX: Offset Info Type Sym. Value Symbol's Name + Addend
21+
# RELAX-NEXT: 0000001c 00000139 R_RISCV_32_PCREL 00000000 .L0 + 0
22+
# RELAX-NEXT: 00000020 00000c23 R_RISCV_ADD32 0001017a .L0 + 0
23+
# RELAX-NEXT: 00000020 00000127 R_RISCV_SUB32 00000000 .L0 + 0
24+
# RELAX-NEXT: 00000035 00000b35 R_RISCV_SET6 00010176 .L0 + 0
25+
# RELAX-NEXT: 00000035 00000934 R_RISCV_SUB6 0001016e .L0 + 0
1926
# CHECK-EMPTY:
20-
# CHECK: Symbol table '.symtab' contains 13 entries:
21-
# CHECK-NEXT: Num: Value Size Type Bind Vis Ndx Name
22-
# CHECK-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
23-
# CHECK-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
24-
# CHECK: 3: 00000004 0 NOTYPE LOCAL DEFAULT 2 .L0{{$}}
25-
# CHECK-NOT: .L0
27+
# NORELAX: Symbol table '.symtab' contains 13 entries:
28+
# RELAX: Symbol table '.symtab' contains 16 entries:
29+
# RELAX-NEXT: Num: Value Size Type Bind Vis Ndx Name
30+
# RELAX-NEXT: 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
31+
# RELAX-NEXT: 1: 00000000 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
32+
# RELAX: 3: 00000004 0 NOTYPE LOCAL DEFAULT 2 .L0{{$}}
33+
# RELAX: 9: 0001016e 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
34+
# RELAX: 11: 00010176 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
35+
# RELAX: 12: 0001017a 0 NOTYPE LOCAL DEFAULT 2 .L0 {{$}}
2636

2737
# CHECK-DWARFDUMP: DW_CFA_advance_loc1: 104
2838
# CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +8

0 commit comments

Comments
 (0)