[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 17 21:34:33 PST 2024


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

>From ff01757ad3d20b9538a23b12e7c3e2cd7f6dc20d 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/3] [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 076351dd00d3b3..94e79fdf1025ce 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 84f23bf78a4e9b..d49a654c6a29b7 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -796,6 +796,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 bbb3d3210e0253..d783b5f0a674cf 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 3fe73a086c729b..14f03958482dff 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 err.s -o err.o

>From 3fa48514dac3e896c12e54892a5b5ae4fef59143 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/3] 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 14f03958482dff..a0e711c1db97d9 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
 

>From 3c80d75c601db4574bd0edd6b89dbd332cab5f2e Mon Sep 17 00:00:00 2001
From: Daniil Kovalev <dkovalev at accesssoftek.com>
Date: Mon, 18 Nov 2024 08:33:11 +0300
Subject: [PATCH 3/3] Address review comments

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

diff --git a/lld/test/ELF/aarch64-got-relocations-pauth.s b/lld/test/ELF/aarch64-got-relocations-pauth.s
index a0e711c1db97d9..9bda853344a3b2 100644
--- a/lld/test/ELF/aarch64-got-relocations-pauth.s
+++ b/lld/test/ELF/aarch64-got-relocations-pauth.s
@@ -80,7 +80,7 @@ _start:
 
 #--- ok-tiny.s
 
-# RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux ok-tiny.s -o ok-tiny.o
+# RUN: llvm-mc -filetype=obj -triple=aarch64 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
@@ -89,16 +89,16 @@ _start:
 # 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
+# EXTERNAL-TINY-NEXT: 0000000000020368  000000010000e201 R_AARCH64_AUTH_GLOB_DAT 0000000000000000 bar + 0
+# EXTERNAL-TINY-NEXT: 0000000000020370  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
+# LOCAL-TINY-NEXT:    0000000000020308  0000000000000411 R_AARCH64_AUTH_RELATIVE 10248
+# LOCAL-TINY-NEXT:    0000000000020310  0000000000000411 R_AARCH64_AUTH_RELATIVE 10248
 
 # EXTERNAL-TINY:      Hex dump of section '.got':
-# EXTERNAL-TINY-NEXT: 0x00020380 00000000 00000080 00000000 000000a0
+# EXTERNAL-TINY-NEXT: 0x00020368 00000000 00000080 00000000 000000a0
 ##                                              ^^
 ##                                              0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
 ##                                                                ^^
@@ -106,11 +106,11 @@ _start:
 
 # 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:         0000000000010248     0 FUNC    GLOBAL DEFAULT     6 bar
+# LOCAL-TINY:         0000000000010248     0 NOTYPE  GLOBAL DEFAULT     6 zed
 
 # LOCAL-TINY:         Hex dump of section '.got':
-# LOCAL-TINY-NEXT:    0x00020320 00000000 00000080 00000000 000000a0
+# LOCAL-TINY-NEXT:    0x00020308 00000000 00000080 00000000 000000a0
 ##                                              ^^
 ##                                              0b10000000 bit 63 address diversity = true, bits 61..60 key = IA
 ##                                                                ^^
@@ -119,36 +119,18 @@ _start:
 # 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
+# EXTERNAL-TINY-ASM-NEXT: adr x0, 0x20368
+# EXTERNAL-TINY-ASM-NEXT: ldr x1, 0x20370
 
 # 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
+# LOCAL-TINY-ASM-NEXT:    adr x0, 0x20308
+# LOCAL-TINY-ASM-NEXT:    ldr x1, 0x20310
 
 .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



More information about the llvm-branch-commits mailing list