Skip to content
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

Do not link shared library parts with "-pie -fPIE" as it breaks LTO #180

Closed
darix opened this issue Oct 13, 2021 · 5 comments
Closed

Do not link shared library parts with "-pie -fPIE" as it breaks LTO #180

darix opened this issue Oct 13, 2021 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@darix
Copy link

darix commented Oct 13, 2021

Changing the linker commandline from

/usr/bin/cc -fPIC -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -g -DNDEBUG -Wformat -Wformat-security -fstack-protector-strong -fPIC -fPIE -O2 -g -DNDEBUG -flto=auto -Wl,--as-needed -Wl,--no-undefined -Wl,-z,now -Wl,--no-undefined -pie -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libpgagroal.so.1 -o libpgagroal.so.1.3.1 CMakeFiles/pgagroal.dir/libpgagroal/configuration.c.o CMakeFiles/pgagroal.dir/libpgagroal/logging.c.o CMakeFiles/pgagroal.dir/libpgagroal/management.c.o CMakeFiles/pgagroal.dir/libpgagroal/memory.c.o CMakeFiles/pgagroal.dir/libpgagroal/message.c.o CMakeFiles/pgagroal.dir/libpgagroal/network.c.o CMakeFiles/pgagroal.dir/libpgagroal/pipeline_perf.c.o CMakeFiles/pgagroal.dir/libpgagroal/pipeline_session.c.o CMakeFiles/pgagroal.dir/libpgagroal/pipeline_transaction.c.o CMakeFiles/pgagroal.dir/libpgagroal/pool.c.o CMakeFiles/pgagroal.dir/libpgagroal/prometheus.c.o CMakeFiles/pgagroal.dir/libpgagroal/remote.c.o CMakeFiles/pgagroal.dir/libpgagroal/security.c.o CMakeFiles/pgagroal.dir/libpgagroal/server.c.o CMakeFiles/pgagroal.dir/libpgagroal/shmem.c.o CMakeFiles/pgagroal.dir/libpgagroal/tracker.c.o CMakeFiles/pgagroal.dir/libpgagroal/utils.c.o CMakeFiles/pgagroal.dir/libpgagroal/worker.c.o  /usr/lib64/libev.so /usr/lib64/libssl.so /usr/lib64/libcrypto.so /usr/lib64/libsystemd.so 
/usr/lib64/gcc/x86_64-suse-linux/11/../../../../x86_64-suse-linux/bin/ld: /tmp/ccoNOkYP.ltrans0.ltrans.o: warning: relocation against `stdout@@GLIBC_2.2.5' in read-only section `.text'
/usr/lib64/gcc/x86_64-suse-linux/11/../../../../x86_64-suse-linux/bin/ld: /tmp/ccoNOkYP.ltrans0.ltrans.o: relocation R_X86_64_PC32 against symbol `shmem' can not be used when making a shared object; recompile with -fPIC
/usr/lib64/gcc/x86_64-suse-linux/11/../../../../x86_64-suse-linux/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

to

/usr/bin/cc -fPIC -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -g -DNDEBUG -Wformat -Wformat-security -fstack-protector-strong -fPIC -O2 -g -DNDEBUG -flto=auto -Wl,--as-needed -Wl,--no-undefined -Wl,-z,now -Wl,--no-undefined -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libpgagroal.so.1 -o libpgagroal.so.1.3.1 CMakeFiles/pgagroal.dir/libpgagroal/configuration.c.o CMakeFiles/pgagroal.dir/libpgagroal/logging.c.o CMakeFiles/pgagroal.dir/libpgagroal/management.c.o CMakeFiles/pgagroal.dir/libpgagroal/memory.c.o CMakeFiles/pgagroal.dir/libpgagroal/message.c.o CMakeFiles/pgagroal.dir/libpgagroal/network.c.o CMakeFiles/pgagroal.dir/libpgagroal/pipeline_perf.c.o CMakeFiles/pgagroal.dir/libpgagroal/pipeline_session.c.o CMakeFiles/pgagroal.dir/libpgagroal/pipeline_transaction.c.o CMakeFiles/pgagroal.dir/libpgagroal/pool.c.o CMakeFiles/pgagroal.dir/libpgagroal/prometheus.c.o CMakeFiles/pgagroal.dir/libpgagroal/remote.c.o CMakeFiles/pgagroal.dir/libpgagroal/security.c.o CMakeFiles/pgagroal.dir/libpgagroal/server.c.o CMakeFiles/pgagroal.dir/libpgagroal/shmem.c.o CMakeFiles/pgagroal.dir/libpgagroal/tracker.c.o CMakeFiles/pgagroal.dir/libpgagroal/utils.c.o CMakeFiles/pgagroal.dir/libpgagroal/worker.c.o  /usr/lib64/libev.so /usr/lib64/libssl.so /usr/lib64/libcrypto.so /usr/lib64/libsystemd.so 

fixes the build with LTO

@jesperpedersen
Copy link
Collaborator

Thanks @darix !

Will be in 1.3.2 -- pushed similar fix for pgmoneta and pgexporter

@darix
Copy link
Author

darix commented Oct 20, 2021

The fix in git was not enough

Index: pgagroal-1.3.2/src/CMakeLists.txt
===================================================================
--- pgagroal-1.3.2.orig/src/CMakeLists.txt
+++ pgagroal-1.3.2/src/CMakeLists.txt
@@ -143,7 +143,6 @@ if (CMAKE_BUILD_TYPE MATCHES Release OR
   if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
     check_c_compiler_flag(-fPIE HAS_PIE)
     if (HAS_PIE)
-      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE")
       set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
     endif()
   endif()

@darix
Copy link
Author

darix commented Oct 20, 2021

the whole PIE check can probably replaced with

https://cmake.org/cmake/help/latest/module/CheckPIESupported.html

@jesperpedersen
Copy link
Collaborator

What is your compiler tool chain ?

You have to compile with -fPIE for the linking with -pie to work.

Will have to look into the CheckPIESupported check - thanks for the link !

@darix
Copy link
Author

darix commented Oct 21, 2021

you can also build all object files with -fPIC ​and then just link with -fPIE -pie

the problem arises from trying to link with LTO. I thought fedora would have turned on LTO by default as well and you would see the same issue there.

with the code you had so far ... it passes -fPIE also when linking the shared library. and that is wrong. but i think relying on the cmake helper is probably a better idea anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants