[llvm-branch-commits] [lld] [PAC][lld][AArch64][ELF] Support signed GOT with tiny code model (PR #113816)

Daniil Kovalev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 10 11:04:34 PST 2024


https://github.com/kovdan01 updated https://github.com/llvm/llvm-project/pull/113816

>From 7eddd301c02588dd56fea43996e089ab181ae0c5 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Fri, 25 Oct 2024 21:28:18 +0300
Subject: [PATCH 1/2] [PAC][lld][AArch64][ELF] Support signed GOT with tiny
 code model

Support `R_AARCH64_AUTH_GOT_ADR_PREL_LO21` and `R_AARCH64_AUTH_GOT_LD_PREL19`
GOT-generating relocations.
---
 lld/ELF/Arch/AArch64.cpp                     |  5 ++
 lld/ELF/InputSection.cpp                     |  1 +
 lld/ELF/Relocations.cpp                      | 17 ++---
 lld/ELF/Relocations.h                        |  1 +
 lld/test/ELF/aarch64-got-relocations-pauth.s | 73 ++++++++++++++++++++
 5 files changed, 89 insertions(+), 8 deletions(-)

diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index c1cce6d437a10b..84ca814e2e2efb 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -205,6 +205,9 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
   case R_AARCH64_AUTH_LD64_GOT_LO12_NC:
   case R_AARCH64_AUTH_GOT_ADD_LO12_NC:
     return R_AARCH64_AUTH_GOT;
+  case R_AARCH64_AUTH_GOT_LD_PREL19:
+  case R_AARCH64_AUTH_GOT_ADR_PREL_LO21:
+    return R_AARCH64_AUTH_GOT_PC;
   case R_AARCH64_LD64_GOTPAGE_LO15:
     return R_AARCH64_GOT_PAGE;
   case R_AARCH64_ADR_GOT_PAGE:
@@ -549,6 +552,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
     write32AArch64Addr(loc, val >> 12);
     break;
   case R_AARCH64_ADR_PREL_LO21:
+  case R_AARCH64_AUTH_GOT_ADR_PREL_LO21:
     checkInt(ctx, loc, val, 21, rel);
     write32AArch64Addr(loc, val);
     break;
@@ -569,6 +573,7 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
   case R_AARCH64_CONDBR19:
   case R_AARCH64_LD_PREL_LO19:
   case R_AARCH64_GOT_LD_PREL19:
+  case R_AARCH64_AUTH_GOT_LD_PREL19:
     checkAlignment(ctx, loc, val, 4, rel);
     checkInt(ctx, loc, val, 21, rel);
     writeMaskedBits32le(loc, (val & 0x1FFFFC) << 3, 0x1FFFFC << 3);
diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index e7c02225ea29de..79d68878a17949 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -795,6 +795,7 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
   case R_AARCH64_GOT_PAGE:
     return r.sym->getGotVA(ctx) + a - getAArch64Page(ctx.in.got->getVA());
   case R_GOT_PC:
+  case R_AARCH64_AUTH_GOT_PC:
   case R_RELAX_TLS_GD_TO_IE:
     return r.sym->getGotVA(ctx) + a - p;
   case R_GOTPLT_GOTREL:
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 6d5da5b50fd37c..5d29c601036136 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -210,11 +210,11 @@ static bool needsPlt(RelExpr expr) {
 }
 
 bool lld::elf::needsGot(RelExpr expr) {
-  return oneof<R_GOT, R_AARCH64_AUTH_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
-               R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC,
-               R_AARCH64_AUTH_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT,
-               R_AARCH64_GOT_PAGE, R_LOONGARCH_GOT, R_LOONGARCH_GOT_PAGE_PC>(
-      expr);
+  return oneof<R_GOT, R_AARCH64_AUTH_GOT, R_AARCH64_AUTH_GOT_PC, R_GOT_OFF,
+               R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32,
+               R_AARCH64_GOT_PAGE_PC, R_AARCH64_AUTH_GOT_PAGE_PC, R_GOT_PC,
+               R_GOTPLT, R_AARCH64_GOT_PAGE, R_LOONGARCH_GOT,
+               R_LOONGARCH_GOT_PAGE_PC>(expr);
 }
 
 // True if this expression is of the form Sym - X, where X is a position in the
@@ -1010,8 +1010,8 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
             R_GOTONLY_PC, R_GOTPLTONLY_PC, R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT,
             R_GOTPLT_GOTREL, R_GOTPLT_PC, R_PPC32_PLTREL, R_PPC64_CALL_PLT,
             R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE,
-            R_AARCH64_AUTH_GOT, R_LOONGARCH_PLT_PAGE_PC, R_LOONGARCH_GOT,
-            R_LOONGARCH_GOT_PAGE_PC>(e))
+            R_AARCH64_AUTH_GOT, R_AARCH64_AUTH_GOT_PC, R_LOONGARCH_PLT_PAGE_PC,
+            R_LOONGARCH_GOT, R_LOONGARCH_GOT_PAGE_PC>(e))
     return true;
 
   // These never do, except if the entire file is position dependent or if
@@ -1126,7 +1126,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
       // Many LoongArch TLS relocs reuse the R_LOONGARCH_GOT type, in which
       // case the NEEDS_GOT flag shouldn't get set.
       bool needsGotAuth =
-          (expr == R_AARCH64_AUTH_GOT || expr == R_AARCH64_AUTH_GOT_PAGE_PC);
+          (expr == R_AARCH64_AUTH_GOT || expr == R_AARCH64_AUTH_GOT_PC ||
+           expr == R_AARCH64_AUTH_GOT_PAGE_PC);
       uint16_t flags = sym.flags.load(std::memory_order_relaxed);
       if (!(flags & NEEDS_GOT)) {
         sym.setFlags(needsGotAuth ? (NEEDS_GOT | NEEDS_GOT_AUTH) : NEEDS_GOT);
diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h
index 20d88de402ac18..38d55d46116569 100644
--- a/lld/ELF/Relocations.h
+++ b/lld/ELF/Relocations.h
@@ -89,6 +89,7 @@ enum RelExpr {
   R_AARCH64_AUTH_GOT_PAGE_PC,
   R_AARCH64_GOT_PAGE,
   R_AARCH64_AUTH_GOT,
+  R_AARCH64_AUTH_GOT_PC,
   R_AARCH64_PAGE_PC,
   R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
   R_AARCH64_TLSDESC_PAGE,
diff --git a/lld/test/ELF/aarch64-got-relocations-pauth.s b/lld/test/ELF/aarch64-got-relocations-pauth.s
index 5760579bcb02e2..85b14fc1bee361 100644
--- a/lld/test/ELF/aarch64-got-relocations-pauth.s
+++ b/lld/test/ELF/aarch64-got-relocations-pauth.s
@@ -78,6 +78,79 @@ _start:
   adrp x1, :got_auth:zed
   add  x1, x1, :got_auth_lo12:zed
 
+#--- ok-tiny.s
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux ok-tiny.s -o ok-tiny.o
+
+# RUN: ld.lld ok-tiny.o a.so -pie -o external-tiny
+# RUN: llvm-readelf -r -S -x .got external-tiny | FileCheck %s --check-prefix=EXTERNAL-TINY
+
+# RUN: ld.lld ok-tiny.o a.o -pie -o local-tiny
+# RUN: llvm-readelf -r -S -x .got -s local-tiny | FileCheck %s --check-prefix=LOCAL-TINY
+
+# EXTERNAL-TINY:      Offset            Info             Type                    Symbol's Value   Symbol's Name + Addend
+# EXTERNAL-TINY-NEXT: 0000000000020380  000000010000e201 R_AARCH64_AUTH_GLOB_DAT 0000000000000000 bar + 0
+# EXTERNAL-TINY-NEXT: 0000000000020388  000000020000e201 R_AARCH64_AUTH_GLOB_DAT 0000000000000000 zed + 0
+
+## Symbol's values for bar and zed are equal since they contain no content (see Inputs/shared.s)
+# LOCAL-TINY:         Offset            Info             Type                    Symbol's Value   Symbol's Name + Addend
+# LOCAL-TINY-NEXT:    0000000000020320  0000000000000411 R_AARCH64_AUTH_RELATIVE 10260
+# LOCAL-TINY-NEXT:    0000000000020328  0000000000000411 R_AARCH64_AUTH_RELATIVE 10260
+
+# EXTERNAL-TINY:      Hex dump of section '.got':
+# EXTERNAL-TINY-NEXT: 0x00020380 00000000 00000080 00000000 000000a0
+#                                               ^^
+#                                               0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
+#                                                                 ^^
+#                                                                 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
+
+# LOCAL-TINY: Symbol table '.symtab' contains {{.*}} entries:
+# LOCAL-TINY:    Num:    Value          Size Type    Bind   Vis       Ndx Name
+# LOCAL-TINY:         0000000000010260     0 FUNC    GLOBAL DEFAULT     6 bar
+# LOCAL-TINY:         0000000000010260     0 NOTYPE  GLOBAL DEFAULT     6 zed
+
+# LOCAL-TINY:         Hex dump of section '.got':
+# LOCAL-TINY-NEXT:    0x00020320 00000000 00000080 00000000 000000a0
+#                                               ^^
+#                                               0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
+#                                                                 ^^
+#                                                                 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
+
+# RUN: llvm-objdump -d external-tiny | FileCheck %s --check-prefix=EXTERNAL-TINY-ASM
+
+# EXTERNAL-TINY-ASM:      <_start>:
+# EXTERNAL-TINY-ASM-NEXT: adr x0, 0x20380
+# EXTERNAL-TINY-ASM-NEXT: ldr x1, [x0]
+# EXTERNAL-TINY-ASM-NEXT: adr x0, 0x20380
+# EXTERNAL-TINY-ASM-NEXT: ldr x1, 0x20380
+# EXTERNAL-TINY-ASM-NEXT: adr x0, 0x20388
+# EXTERNAL-TINY-ASM-NEXT: ldr x1, [x0]
+# EXTERNAL-TINY-ASM-NEXT: adr x0, 0x20388
+# EXTERNAL-TINY-ASM-NEXT: ldr x1, 0x20388
+
+# RUN: llvm-objdump -d local-tiny | FileCheck %s --check-prefix=LOCAL-TINY-ASM
+
+# LOCAL-TINY-ASM:         <_start>:
+# LOCAL-TINY-ASM-NEXT:    adr x0, 0x20320
+# LOCAL-TINY-ASM-NEXT:    ldr x1, [x0]
+# LOCAL-TINY-ASM-NEXT:    adr x0, 0x20320
+# LOCAL-TINY-ASM-NEXT:    ldr x1, 0x20320
+# LOCAL-TINY-ASM-NEXT:    adr x0, 0x20328
+# LOCAL-TINY-ASM-NEXT:    ldr x1, [x0]
+# LOCAL-TINY-ASM-NEXT:    adr x0, 0x20328
+# LOCAL-TINY-ASM-NEXT:    ldr x1, 0x20328
+
+.globl _start
+_start:
+  adr  x0, :got_auth:bar
+  ldr  x1, [x0]
+  adr  x0, :got_auth:bar
+  ldr  x1, :got_auth:bar
+  adr  x0, :got_auth:zed
+  ldr  x1, [x0]
+  adr  x0, :got_auth:zed
+  ldr  x1, :got_auth:zed
+
 #--- err.s
 
 # RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux err.s -o err.o

>From 69bc31015e7fd7e74bbe4e614d551957d1efe456 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Fri, 1 Nov 2024 13:18:43 +0300
Subject: [PATCH 2/2] Use `##` for comments in tests

---
 lld/test/ELF/aarch64-got-relocations-pauth.s | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lld/test/ELF/aarch64-got-relocations-pauth.s b/lld/test/ELF/aarch64-got-relocations-pauth.s
index 85b14fc1bee361..42b0b187ebc023 100644
--- a/lld/test/ELF/aarch64-got-relocations-pauth.s
+++ b/lld/test/ELF/aarch64-got-relocations-pauth.s
@@ -99,10 +99,10 @@ _start:
 
 # EXTERNAL-TINY:      Hex dump of section '.got':
 # EXTERNAL-TINY-NEXT: 0x00020380 00000000 00000080 00000000 000000a0
-#                                               ^^
-#                                               0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
-#                                                                 ^^
-#                                                                 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
+##                                              ^^
+##                                              0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
+##                                                                ^^
+##                                                                0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
 
 # LOCAL-TINY: Symbol table '.symtab' contains {{.*}} entries:
 # LOCAL-TINY:    Num:    Value          Size Type    Bind   Vis       Ndx Name
@@ -111,10 +111,10 @@ _start:
 
 # LOCAL-TINY:         Hex dump of section '.got':
 # LOCAL-TINY-NEXT:    0x00020320 00000000 00000080 00000000 000000a0
-#                                               ^^
-#                                               0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
-#                                                                 ^^
-#                                                                 0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
+##                                              ^^
+##                                              0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
+##                                                                ^^
+##                                                                0b10100000 bit 63 address diversity = true, bits 61..60 key = DA
 
 # RUN: llvm-objdump -d external-tiny | FileCheck %s --check-prefix=EXTERNAL-TINY-ASM
 



More information about the llvm-branch-commits mailing list