Skip to content

Commit f1d9cab

Browse files
committed
[LoongArch] Use R_LARCH_ALIGN with section symbol
In LoongArch psABI v2.30, the R_LARCH_ALIGN requires symbol index to support the third parameter of alignment directive. Create symbol for each section is redundant because they have section symbol which can also be used as symbol index. So use section symbol directly for R_LARCH_ALIGN.
1 parent 60dda1f commit f1d9cab

File tree

6 files changed

+46
-15
lines changed

6 files changed

+46
-15
lines changed

lld/ELF/InputSection.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,11 @@ void InputSection::copyRelocations(uint8_t *buf,
462462
addend += sec->getFile<ELFT>()->mipsGp0;
463463
}
464464

465-
if (RelTy::IsRela)
465+
if (config->emachine == EM_LOONGARCH && type == R_LARCH_ALIGN)
466+
// LoongArch psABI v2.30, the R_LARCH_ALIGN requires symbol index.
467+
// If it use the section symbol, the addend should not be changed.
468+
p->r_addend = addend;
469+
else if (RelTy::IsRela)
466470
p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
467471
// For SHF_ALLOC sections relocated by REL, append a relocation to
468472
// sec->relocations so that relocateAlloc transitively called by
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# REQUIRES: loongarch
2+
3+
# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+relax %s -o %t.64.o
4+
# RUN: ld.lld -r %t.64.o %t.64.o -o %t.64.r
5+
# RUN: llvm-objdump -dr --no-show-raw-insn %t.64.r | FileCheck %s
6+
7+
# CHECK: <.text>:
8+
# CHECK-NEXT: break 1
9+
# CHECK-NEXT: nop
10+
# CHECK-NEXT: {{0*}}04: R_LARCH_ALIGN .text+0x804
11+
# CHECK-NEXT: nop
12+
# CHECK-NEXT: nop
13+
# CHECK-NEXT: break 2
14+
# CHECK-NEXT: break 0
15+
# CHECK-NEXT: break 0
16+
# CHECK-NEXT: break 0
17+
# CHECK-NEXT: break 1
18+
# CHECK-NEXT: nop
19+
# CHECK-NEXT: {{0*}}24: R_LARCH_ALIGN .text+0x804
20+
# CHECK-NEXT: nop
21+
# CHECK-NEXT: nop
22+
# CHECK-NEXT: break 2
23+
24+
.text
25+
break 1
26+
.p2align 4, , 8
27+
break 2

lld/test/ELF/loongarch-relax-emit-relocs.s

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# CHECK-NEXT: R_LARCH_PCALA_LO12 _start
2626
# CHECK-NEXT: R_LARCH_RELAX *ABS*
2727
# CHECK-NEXT: nop
28-
# CHECK-NEXT: R_LARCH_ALIGN .Lla-relax-align0+0x4
28+
# CHECK-NEXT: R_LARCH_ALIGN .text+0x4
2929
# CHECK-NEXT: nop
3030
# CHECK-NEXT: ret
3131

@@ -37,11 +37,12 @@
3737
# CHECKR-NEXT: R_LARCH_PCALA_LO12 _start
3838
# CHECKR-NEXT: R_LARCH_RELAX *ABS*
3939
# CHECKR-NEXT: nop
40-
# CHECKR-NEXT: R_LARCH_ALIGN .Lla-relax-align0+0x4
40+
# CHECKR-NEXT: R_LARCH_ALIGN .text+0x4
4141
# CHECKR-NEXT: nop
4242
# CHECKR-NEXT: nop
4343
# CHECKR-NEXT: ret
4444

45+
.text
4546
.global _start
4647
_start:
4748
la.pcrel $a0, _start

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,8 @@ bool LoongArchAsmBackend::shouldInsertFixupForCodeAlign(
226226
MCFixup::create(0, Dummy, MCFixupKind(LoongArch::fixup_loongarch_align));
227227
const MCSymbolRefExpr *MCSym = getSecToAlignSym()[Sec];
228228
if (MCSym == nullptr) {
229-
// Create a symbol and make the value of symbol is zero.
230-
MCSymbol *Sym = Ctx.createNamedTempSymbol("la-relax-align");
231-
Sym->setFragment(&*Sec->getBeginSymbol()->getFragment());
232-
Asm.registerSymbol(*Sym);
233-
MCSym = MCSymbolRefExpr::create(Sym, Ctx);
229+
// Use section symbol directly.
230+
MCSym = MCSymbolRefExpr::create(Sec->getBeginSymbol(), Ctx);
234231
getSecToAlignSym()[Sec] = MCSym;
235232
}
236233

llvm/test/MC/LoongArch/Relocations/relax-addsub.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
# RELAX: Relocations [
3030
# RELAX-NEXT: Section ({{.*}}) .rela.text {
31-
# RELAX-NEXT: 0x4 R_LARCH_ALIGN {{.*}} 0x4
31+
# RELAX-NEXT: 0x4 R_LARCH_ALIGN .text 0x4
3232
# RELAX-NEXT: 0x10 R_LARCH_PCALA_HI20 .L1 0x0
3333
# RELAX-NEXT: 0x10 R_LARCH_RELAX - 0x0
3434
# RELAX-NEXT: 0x14 R_LARCH_PCALA_LO12 .L1 0x0

llvm/test/MC/LoongArch/Relocations/relax-align.s

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ ret
6363
## Test the symbol index is different from .text.
6464
.section .text2, "ax"
6565
.p2align 4
66+
.p2align 4, , 4
6667
break 7
6768

6869
# RELOC: Relocations [
6970
# RELAX-RELOC-NEXT: Section ({{.*}}) .rela.text {
70-
# RELAX-RELOC-NEXT: 0x24 R_LARCH_ALIGN .Lla-relax-align0 0x4
71-
# RELAX-RELOC-NEXT: 0x34 R_LARCH_ALIGN .Lla-relax-align0 0x5
72-
# RELAX-RELOC-NEXT: 0x50 R_LARCH_ALIGN .Lla-relax-align0 0x4
73-
# RELAX-RELOC-NEXT: 0x60 R_LARCH_ALIGN .Lla-relax-align0 0xB04
74-
# RELAX-RELOC-NEXT: 0x70 R_LARCH_ALIGN .Lla-relax-align0 0x4
71+
# RELAX-RELOC-NEXT: 0x24 R_LARCH_ALIGN .text 0x4
72+
# RELAX-RELOC-NEXT: 0x34 R_LARCH_ALIGN .text 0x5
73+
# RELAX-RELOC-NEXT: 0x50 R_LARCH_ALIGN .text 0x4
74+
# RELAX-RELOC-NEXT: 0x60 R_LARCH_ALIGN .text 0xB04
75+
# RELAX-RELOC-NEXT: 0x70 R_LARCH_ALIGN .text 0x4
7576
# RELAX-RELOC-NEXT: }
7677
# RELAX-RELOC-NEXT: Section ({{.*}}) .rela.text2 {
77-
# RELAX-RELOC-NEXT: 0x0 R_LARCH_ALIGN .Lla-relax-align1 0x4
78+
# RELAX-RELOC-NEXT: 0x0 R_LARCH_ALIGN .text2 0x4
79+
# RELAX-RELOC-NEXT: 0xC R_LARCH_ALIGN .text2 0x404
7880
# RELAX-RELOC-NEXT: }
7981
# RELOC-NEXT: ]

0 commit comments

Comments
 (0)