This is an archive of the discontinued LLVM Phabricator instance.

[Driver] Add PIE support on Solaris
ClosedPublic

Authored by ro on Aug 17 2023, 12:37 PM.

Details

Summary

clang currently lacks PIE support on Solaris. This patch fixes this, linking with crtbeginS.o and crtendS.o for -pie and -shared.

Tested on amd64-pc-solaris2.11, sparcv9-sun-solaris2.11, and x86_64-pc-linux-gnu.

Diff Detail

Event Timeline

ro created this revision.Aug 17 2023, 12:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2023, 12:37 PM
ro requested review of this revision.Aug 17 2023, 12:37 PM
MaskRay added inline comments.Aug 17 2023, 12:41 PM
clang/lib/Driver/ToolChains/Solaris.cpp
67

On Linux, clang -r -static also disable the linker option -pie.
-static-pie is handled as a special case which also disables the regular -pie code path.

clang/test/Driver/solaris-ld.c
110

The convention is to put 2>&1 at the end of the command, aka before |

I'll submit a follow-up patch to make use of crtbeginS.o and crtendS.o shortly.

I think this patch should make this change, so that the change is correct on itself.

ro marked 2 inline comments as done.Aug 18 2023, 7:55 AM

I'll submit a follow-up patch to make use of crtbeginS.o and crtendS.o shortly.

I think this patch should make this change, so that the change is correct on itself.

Ok, done. I'll update with the merge patch once another round of testing has finished.

clang/lib/Driver/ToolChains/Solaris.cpp
67

I've taken Gnu.cpp (getPIE), removing the -static-pie handling.

Solaris supports neither -static (no libc.a etc. since Solaris 10) nor -static-pie. Given that gcc silently ignores that, I'm doing the same.

clang/test/Driver/solaris-ld.c
110

I've changed just the newly added tests. There are a few others, but I've left the as is for now to avoid cluttering the patch with unrelated changes.

ro updated this revision to Diff 551539.Aug 18 2023, 9:11 AM
ro marked 2 inline comments as done.
ro edited the summary of this revision. (Show Details)
  • Merged D158207.
  • Incorporate review comments.
MaskRay accepted this revision.Aug 18 2023, 9:32 AM
MaskRay added inline comments.
clang/lib/Driver/ToolChains/Solaris.cpp
136

Q: Interesting. If -shared used crtbegin.o before, how did it work?

On Linux, using GCC crtbegin.o (compiled with -fno-PIC or similar non-PIC option) will cause:

ld.lld: error: relocation R_X86_64_32S cannot be used against local symbol; recompile with -fPIC
>>> defined in /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o
>>> referenced by crtstuff.c
>>>               /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o:(.text+0x7)

ld.lld: error: relocation R_X86_64_32 cannot be used against symbol '_ITM_deregisterTMCloneTable'; recompile with -fPIC
>>> defined in /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o
>>> referenced by crtstuff.c
>>>               /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o:(.text+0xE)

ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol; recompile with -fPIC
>>> defined in /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o
>>> referenced by crtstuff.c
>>>               /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o:(.text+0x18)
clang/test/Driver/solaris-ld.c
110

Ultra nit: you may pack --target=sparc-sun-solaris2.11 on the first line to make the commands compacter, like %clang --target=sparc-sun-solaris2.11 -### %s below.

112

Ultra nit: you may pack --target=sparc-sun-solaris2.11 on the first line to make the commands compacter, like %clang --target=sparc-sun-solaris2.11 -### %s below.

FYI: I think --gcc-toolchain="" is to support GCC_INSTALL_PREFIX. I propose that we deprecate the CMake variable to make test maintaining easier: D158218.

This revision is now accepted and ready to land.Aug 18 2023, 9:32 AM
ro marked an inline comment as done.Aug 18 2023, 12:03 PM
ro added inline comments.
clang/lib/Driver/ToolChains/Solaris.cpp
136

When the Solaris crts were reworked for Solaris 11.4 back in 2015, the were intentionally built as PIC to avoid issues just like that.

clang/test/Driver/solaris-ld.c
110

Good point, done.

This revision was landed with ongoing or failed builds.Aug 18 2023, 12:10 PM
This revision was automatically updated to reflect the committed changes.
ro marked an inline comment as done.