[PATCH] D159543: [BOLT] Fix .relr section addend patching

Vladislav Khmelevsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 28 01:56:25 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG0053cb8ef0a1: [BOLT] Fix .relr section addend patching (authored by yota9).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159543/new/

https://reviews.llvm.org/D159543

Files:
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/AArch64/constant_island_pie_update.s


Index: bolt/test/AArch64/constant_island_pie_update.s
===================================================================
--- bolt/test/AArch64/constant_island_pie_update.s
+++ bolt/test/AArch64/constant_island_pie_update.s
@@ -13,8 +13,11 @@
 // .relr.dyn
 # RUN: %clang %cflags -fPIC -pie %t.o -o %t.relr.exe -nostdlib \
 # RUN:   -Wl,-q -Wl,-z,notext -Wl,--pack-dyn-relocs=relr
+# RUN: llvm-objcopy --remove-section .rela.mytext %t.relr.exe
 # RUN: llvm-bolt %t.relr.exe -o %t.relr.bolt --use-old-text=0 --lite=0
 # RUN: llvm-objdump -j .text -d --show-all-symbols %t.relr.bolt | FileCheck %s
+# RUN: llvm-objdump -j .text -d %t.relr.bolt | \
+# RUN:   FileCheck %s --check-prefix=ADDENDCHECK
 # RUN: llvm-readelf -rsW %t.relr.bolt | FileCheck --check-prefix=ELFCHECK %s
 # RUN: llvm-readelf -SW %t.relr.bolt | FileCheck --check-prefix=RELRSZCHECK %s
 
@@ -30,6 +33,11 @@
 # CHECK-NEXT: {{.*}} .word 0x{{[0]+}}[[#ADDR]]
 # CHECK-NEXT: {{.*}} .word 0x00000000
 
+// Check that addend was properly patched in mytextP with stripped relocations
+# ADDENDCHECK: [[#%x,ADDR:]] <exitLocal>:
+# ADDENDCHECK: {{.*}} <mytextP>:
+# ADDENDCHECK-NEXT: {{.*}} .word 0x{{[0]+}}[[#ADDR]]
+# ADDENDCHECK-NEXT: {{.*}} .word 0x00000000
 
 // Check that we've relaxed adr to adrp + add to refer external CI
 # CHECK: <addressDynCi>:
@@ -40,9 +48,10 @@
 # ELFCHECK: [[#%x,OFF:]] [[#%x,INFO_DYN:]] R_AARCH64_RELATIVE
 # ELFCHECK-NEXT: [[#OFF + 8]] {{0*}}[[#INFO_DYN]] R_AARCH64_RELATIVE
 # ELFCHECK-NEXT: [[#OFF + 24]] {{0*}}[[#INFO_DYN]] R_AARCH64_RELATIVE
+# ELFCHECK-NEXT: {{.*}} R_AARCH64_RELATIVE
 # ELFCHECK: {{.*}}[[#OFF]] {{.*}} $d
 
-// Check that .relr.dyn size is 2 bytes to ensure that last 2 relocations were
+// Check that .relr.dyn size is 2 bytes to ensure that last 3 relocations were
 // encoded as a bitmap so the total section size for 3 relocations is 2 bytes.
 # RELRSZCHECK: .relr.dyn RELR [[#%x,ADDR:]] [[#%x,OFF:]] {{0*}}10
 
@@ -81,3 +90,17 @@
   adr x1, .Lci
   bl _start
 .size addressDynCi, .-addressDynCi
+
+  .section ".mytext", "ax"
+  .balign 8
+  .global dummy
+  .type dummy, %function
+dummy:
+  nop
+  .word 0
+  .size dummy, .-dummy
+
+  .global mytextP
+mytextP:
+  .xword exitLocal
+  .size mytextP, .-mytextP
Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -4720,9 +4720,11 @@
   const uint8_t PSize = BC->AsmInfo->getCodePointerSize();
   const uint64_t MaxDelta = ((CHAR_BIT * DynamicRelrEntrySize) - 1) * PSize;
 
-  auto FixAddend = [&](const BinarySection &Section, const Relocation &Rel) {
+  auto FixAddend = [&](const BinarySection &Section, const Relocation &Rel,
+                       uint64_t FileOffset) {
     // Fix relocation symbol value in place if no static relocation found
-    // on the same address
+    // on the same address. We won't check the BF relocations here since it
+    // is rare case and no optimization is required.
     if (Section.getRelocationAt(Rel.Offset))
       return;
 
@@ -4731,11 +4733,6 @@
     if (!Addend)
       return;
 
-    uint64_t FileOffset = Section.getOutputFileOffset();
-    if (!FileOffset)
-      FileOffset = Section.getInputFileOffset();
-
-    FileOffset += Rel.Offset;
     OS.pwrite(reinterpret_cast<const char *>(&Addend), PSize, FileOffset);
   };
 
@@ -4757,7 +4754,7 @@
       RelOffset = RelOffset == 0 ? SectionAddress + Rel.Offset : RelOffset;
       assert((RelOffset & 1) == 0 && "Wrong relocation offset");
       RelOffsets.emplace(RelOffset);
-      FixAddend(Section, Rel);
+      FixAddend(Section, Rel, RelOffset);
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159543.557437.patch
Type: text/x-patch
Size: 3685 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230928/c57c7373/attachment.bin>


More information about the llvm-commits mailing list