Skip to content

[ARM] mlong-calls generate PIC code using GOT #39970 #147313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jiangxuezhi
Copy link

supoort -mlong-calls -fPIC using GOT
try fixing issuse #39970 and maybe helpful for soving the problem encourted in #142982

Copy link

github-actions bot commented Jul 7, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2025

@llvm/pr-subscribers-backend-arm

Author: None (jiangxuezhi)

Changes

supoort -mlong-calls -fPIC using GOT
try fixing issuse #39970 and maybe helpful for soving the problem encourted in #142982


Full diff: https://github.com/llvm/llvm-project/pull/147313.diff

2 Files Affected:

  • (modified) llvm/lib/Target/ARM/ARMISelLowering.cpp (+12-4)
  • (added) llvm/test/CodeGen/ARM/long-calls.ll (+32)
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index 2d73725291d11..b73a53cca84e3 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -2815,13 +2815,20 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
   auto PtrVt = getPointerTy(DAG.getDataLayout());
 
   if (Subtarget->genLongCalls()) {
-    assert((!isPositionIndependent() || Subtarget->isTargetWindows()) &&
-           "long-calls codegen is not position independent!");
     // Handle a global address or an external symbol. If it's not one of
     // those, the target's already in a register, so we don't need to do
     // anything extra.
     if (isa<GlobalAddressSDNode>(Callee)) {
-      if (Subtarget->genExecuteOnly()) {
+      if (isPositionIndependent() && !Subtarget->isTargetWindows() &&
+          !Subtarget->genExecuteOnly()) {
+        SDValue G = DAG.getTargetGlobalAddress(
+            GVal, dl, PtrVt, 0, GVal->isDSOLocal() ? 0 : ARMII::MO_GOT);
+        Callee = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVt, G);
+        if (!GVal->isDSOLocal())
+          Callee =
+              DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
+                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
+      } else if (Subtarget->genExecuteOnly()) {
         if (Subtarget->useMovt())
           ++NumMovwMovt;
         Callee = DAG.getNode(ARMISD::Wrapper, dl, PtrVt,
@@ -2839,7 +2846,8 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
             PtrVt, dl, DAG.getEntryNode(), Addr,
             MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
       }
-    } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
+    } else if (ExternalSymbolSDNode *S =
+                   dyn_cast<ExternalSymbolSDNode>(Callee)) {
       const char *Sym = S->getSymbol();
 
       if (Subtarget->genExecuteOnly()) {
diff --git a/llvm/test/CodeGen/ARM/long-calls.ll b/llvm/test/CodeGen/ARM/long-calls.ll
new file mode 100644
index 0000000000000..41763801fddd2
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/long-calls.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=armv7-unknown-linux-gnueabi -relocation-model pic -mattr=+long-calls -o - %s \
+; RUN:    | FileCheck %s
+
+@msg = private unnamed_addr constant [12 x i8] c"hello world\00", align 1
+
+declare i32 @puts(ptr)
+
+define void @test() {
+; CHECK-LABEL: test:
+; CHECK:       @ %bb.0: @ %entry
+; CHECK-NEXT:    .save {r11, lr}
+; CHECK-NEXT:    push {r11, lr}
+; CHECK-NEXT:    ldr r0, .LCPI0_0
+; CHECK-NEXT:    ldr r1, .LCPI0_1
+; CHECK-NEXT:  .LPC0_0:
+; CHECK-NEXT:    add r0, pc, r0
+; CHECK-NEXT:  .LPC0_1:
+; CHECK-NEXT:    ldr r1, [pc, r1]
+; CHECK-NEXT:    blx r1
+; CHECK-NEXT:    pop {r11, pc}
+; CHECK-NEXT:    .p2align 2
+; CHECK-NEXT:  @ %bb.1:
+; CHECK-NEXT:  .LCPI0_0:
+; CHECK-NEXT:    .long .Lmsg-(.LPC0_0+8)
+; CHECK-NEXT:  .LCPI0_1:
+; CHECK-NEXT:  .Ltmp0:
+; CHECK-NEXT:    .long puts(GOT_PREL)-(.LPC0_1+8-.Ltmp0)
+entry:
+  %call = call i32 @puts(ptr @msg)
+  ret void
+}

@jiangxuezhi
Copy link
Author

e.g.
the command is clang -O2 -mlong-calls -fPIC

the code is:
#include <stdio.h>

int main()
{
printf("hello");
return 0;
}

the Relocation section is:
Offset Info Type Sym. Value Symbol's Name
00000020 00000503 R_ARM_REL32 00000000 .L.str
00000024 00000760 R_ARM_GOT_PREL 00000000 printf

Relocation section '.rel.ARM.exidx' at offset 0x1a8 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name
00000000 0000022a R_ARM_PREL31 00000000 .text

@@ -0,0 +1,32 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=armv7-unknown-linux-gnueabi -relocation-model pic -mattr=+long-calls -o - %s \
; RUN: | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be integrated into llvm/test/CodeGen/ARM/subtarget-features-long-calls.ll?

Callee =
DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
MachinePointerInfo::getGOT(DAG.getMachineFunction()));
} else if (Subtarget->genExecuteOnly()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the execute-only codepath work correctly with PIC?

@@ -2839,7 +2846,8 @@ ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
PtrVt, dl, DAG.getEntryNode(), Addr,
MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
}
} else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
} else if (ExternalSymbolSDNode *S =
dyn_cast<ExternalSymbolSDNode>(Callee)) {
const char *Sym = S->getSymbol();

if (Subtarget->genExecuteOnly()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the external-symbol codepath work correctly with PIC? (I think you can trigger this with llvm.memset or something like that.)

@hstk30-hw hstk30-hw requested review from davemgreen and hstk30-hw July 8, 2025 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants