Skip to content

Commit 01f7989

Browse files
authored
[LoongArch] Use R_LARCH_ALIGN with section symbol (#84741)
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 f4960da commit 01f7989

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

lld/ELF/InputSection.cpp

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

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