[PATCH] D120850: [BOLT] Handle ifuncs trampolines for aarch64

Vladislav Khmelevsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 2 14:34:29 PST 2022


yota9 created this revision.
yota9 added reviewers: maksfb, rafauler, Amir.
Herald added subscribers: ayermolo, kristof.beyls.
Herald added a project: All.
yota9 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The aarch64 uses the trampolines located in .iplt section, which
contains plt-like trampolines on the value stored in .got. In this case
we don't have JUMP_SLOT relocation, but we have a symbol that belongs to
ifunc trampoline, so use it and set set plt symbol for such functions.

Vladislav Khmelevsky,
Advanced Software Technology Lab, Huawei


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120850

Files:
  bolt/include/bolt/Rewrite/RewriteInstance.h
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/runtime/AArch64/iplt.c


Index: bolt/test/runtime/AArch64/iplt.c
===================================================================
--- /dev/null
+++ bolt/test/runtime/AArch64/iplt.c
@@ -0,0 +1,29 @@
+// This test checks that the ifuncs works after bolt.
+
+// RUN: %clang %cflags %s -fuse-ld=lld \
+// RUN:    -o %t.exe -Wl,-q
+// RUN: llvm-bolt %t.exe -o %t.bolt.exe -use-old-text=0 -lite=0
+// RUN: %t.bolt.exe  | FileCheck %s
+
+// CHECK: foo
+
+#include <stdio.h>
+#include <string.h>
+
+static void foo() { printf("foo\n"); }
+
+static void *resolver_foo(void) { return foo; }
+
+__attribute__((ifunc("resolver_foo"))) void ifoo();
+
+static void *resolver_memcpy(void) { return memcpy; }
+
+__attribute__((ifunc("resolver_memcpy"))) void *
+imemcpy(void *dest, const void *src, size_t n);
+
+int main() {
+  int a = 0xdeadbeef, b = 0;
+  ifoo();
+  imemcpy(&b, &a, sizeof(b));
+  return a != b;
+}
Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -1250,19 +1250,30 @@
   if (!TargetAddress)
     return;
 
+  auto setPLTSymbol = [&](BinaryFunction *BF, StringRef Name) {
+    const unsigned PtrSize = BC->AsmInfo->getCodePointerSize();
+    MCSymbol *TargetSymbol = BC->registerNameAtAddress(
+        Name.str() + "@GOT", TargetAddress, PtrSize, PtrSize);
+    BF->setPLTSymbol(TargetSymbol);
+  };
+
+  BinaryFunction *BF = BC->getBinaryFunctionAtAddress(EntryAddress);
+  if (BF && BC->isAArch64()) {
+    // Handle IFUNC trampoline
+    setPLTSymbol(BF, BF->getOneName());
+    return;
+  }
+
   const Relocation *Rel = BC->getDynamicRelocationAt(TargetAddress);
   if (!Rel || !Rel->Symbol)
     return;
 
-  const unsigned PtrSize = BC->AsmInfo->getCodePointerSize();
   ErrorOr<BinarySection &> Section = BC->getSectionForAddress(EntryAddress);
   assert(Section && "cannot get section for address");
-  BinaryFunction *BF = BC->createBinaryFunction(
-      Rel->Symbol->getName().str() + "@PLT", *Section, EntryAddress, 0,
-      EntrySize, Section->getAlignment());
-  MCSymbol *TargetSymbol = BC->registerNameAtAddress(
-      Rel->Symbol->getName().str() + "@GOT", TargetAddress, PtrSize, PtrSize);
-  BF->setPLTSymbol(TargetSymbol);
+  BF = BC->createBinaryFunction(Rel->Symbol->getName().str() + "@PLT", *Section,
+                                EntryAddress, 0, EntrySize,
+                                Section->getAlignment());
+  setPLTSymbol(BF, Rel->Symbol->getName());
 }
 
 void RewriteInstance::disassemblePLTSectionAArch64(BinarySection &Section) {
Index: bolt/include/bolt/Rewrite/RewriteInstance.h
===================================================================
--- bolt/include/bolt/Rewrite/RewriteInstance.h
+++ bolt/include/bolt/Rewrite/RewriteInstance.h
@@ -498,7 +498,8 @@
   };
 
   /// AArch64 PLT sections.
-  const PLTSectionInfo AArch64_PLTSections[2] = {{".plt"}, {nullptr}};
+  const PLTSectionInfo AArch64_PLTSections[3] = {
+      {".plt"}, {".iplt"}, {nullptr}};
 
   /// Return PLT information for a section with \p SectionName or nullptr
   /// if the section is not PLT.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120850.412546.patch
Type: text/x-patch
Size: 3152 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220302/a323345d/attachment.bin>


More information about the llvm-commits mailing list