Skip to content

Conversation

@DanielCChen
Copy link
Contributor

This is to support statical linking to shared flang-rt on AIX.
Users should be able to do flang -static t.f. The a.out generated should not have dependencies on the shared libraries.

@DanielCChen DanielCChen self-assigned this Mar 18, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:PowerPC clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Mar 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

@llvm/pr-subscribers-clang-driver
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-powerpc

Author: Daniel Chen (DanielCChen)

Changes

This is to support statical linking to shared flang-rt on AIX.
Users should be able to do flang -static t.f. The a.out generated should not have dependencies on the shared libraries.


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

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/AIX.cpp (+8-1)
diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp
index 001f3a5178943..7ed26c42c80ce 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -127,8 +127,15 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   }
 
   // Force static linking when "-static" is present.
-  if (Args.hasArg(options::OPT_static))
+  if (Args.hasArg(options::OPT_static)) {
     CmdArgs.push_back("-bnso");
+    // The folllowing linker options are needed to statically link to the
+    // shared libflang_rt.runtime.a on AIX
+    CmdArgs.push_back("-bI:/usr/lib/syscalls.exp");
+    CmdArgs.push_back("-bI:/usr/lib/aio.exp");
+    CmdArgs.push_back("-bI:/usr/lib/threads.exp");
+    CmdArgs.push_back("-lcrypt");
+  }
 
   // Add options for shared libraries.
   if (Args.hasArg(options::OPT_shared)) {

@DanielCChen DanielCChen force-pushed the daniel_staticlink branch 3 times, most recently from 0a0e52f to 21f3ec3 Compare March 29, 2025 16:15
Copy link
Member

Choose a reason for hiding this comment

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

This change needs a test somewhere at flang/test/Driver/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comment!
Yes. I will add test.

Comment on lines 133 to 139
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this add extra options even when libflang_rt.runtime.a is not being linked in?
See https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/AIX.cpp#L359-L365

Copy link
Contributor Author

@DanielCChen DanielCChen Mar 30, 2025

Choose a reason for hiding this comment

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

These extra options will be linked in when flang is the invocation driver and -static is specified. As the driver code is written in PR #131041, libflang_rt.runtime.a is always linked in with the full path name no matter if it is static or shared.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As the driver code is written in PR #131041, libflang_rt.runtime.a is always linked in with the full path name no matter if it is static or shared.

The other PR does not change the lines I referenced in a way that causes libflang_rt.runtime.a to always be linked in.

@DanielCChen
Copy link
Contributor Author

Note that the reason aio.exp is not needed is because flang-rt currently doesn't do asynchronous I/O. Once it is enabled using POSIX aio_* system calls, it will need to export those calls.

@hubert-reinterpretcast
Copy link
Collaborator

Note that the reason aio.exp is not needed is because flang-rt currently doesn't do asynchronous I/O. Once it is enabled using POSIX aio_* system calls, it will need to export those calls.

There is also a /usr/lib/posix_aio.exp.

Copy link
Member

@daltenty daltenty Apr 2, 2025

Choose a reason for hiding this comment

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

I think this is a bit misplaced, IIUC these options are needed to link libc statically, not flang_rt itself (-static just happens to mean we link both).

Thus, I think this belongs where we push_back -lc for the AIX toolchain, so here:
https://github.com/llvm/llvm-project/blob/28907a624a8d9561badc20d77254791918ba6210/clang/lib/Driver/ToolChains/AIX.cpp#L357

Also, I don't think this really belongs under the -static option, that is really an ld option which says, only link to static libs. Typically there are separate options to link particular runtimes statically. Since this is really for libc, I'd suggest -static-libc.

@DanielCChen
Copy link
Contributor Author

After further internal discussion. we decided to withdraw this PR until we hash out a few more details.
Thanks everyone for the comments.

@DanielCChen DanielCChen closed this Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:PowerPC clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants