From ccbbf645522f87b38ddcaa68058f621a892e8b9f Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 24 Jun 2024 17:17:19 +0200 Subject: Tests/RunCMake/file-RPATH: Prepare to cover statically linked binaries --- Tests/RunCMake/file-RPATH/Common.cmake | 19 ++++++++++++++----- Tests/RunCMake/file-RPATH/ELF.cmake | 2 +- Tests/RunCMake/file-RPATH/XCOFF.cmake | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Tests/RunCMake/file-RPATH/Common.cmake b/Tests/RunCMake/file-RPATH/Common.cmake index 7034aad..d6d3eeb 100644 --- a/Tests/RunCMake/file-RPATH/Common.cmake +++ b/Tests/RunCMake/file-RPATH/Common.cmake @@ -1,12 +1,16 @@ # Prepare binaries on which to operate. set(in "${CMAKE_CURRENT_LIST_DIR}/${format}") set(out "${CMAKE_CURRENT_BINARY_DIR}") -foreach(f ${names}) +foreach(f ${dynamic}) file(COPY ${in}/${f} DESTINATION ${out} NO_SOURCE_PERMISSIONS) - list(APPEND files "${out}/${f}") + list(APPEND dynamic_files "${out}/${f}") +endforeach() +foreach(f ${static}) + file(COPY ${in}/${f} DESTINATION ${out} NO_SOURCE_PERMISSIONS) + list(APPEND static_files "${out}/${f}") endforeach() -foreach(f ${files}) +foreach(f ${dynamic_files}) # Check for the initial RPATH. file(RPATH_CHECK FILE "${f}" RPATH "/sample/rpath") if(NOT EXISTS "${f}") @@ -65,11 +69,11 @@ endforeach() # TODO Implement RPATH_SET in XCOFF. if(format STREQUAL "ELF") - foreach(f ${names}) + foreach(f ${dynamic}) file(COPY ${in}/${f} DESTINATION ${out} NO_SOURCE_PERMISSIONS) endforeach() - foreach(f ${files}) + foreach(f ${dynamic_files}) # Set the RPATH. file(RPATH_SET FILE "${f}" NEW_RPATH "/new/rpath") @@ -99,3 +103,8 @@ if(format STREQUAL "ELF") endif() endforeach() endif() + +# Verify that modifying rpaths on a static library is a no-op +foreach(f ${static_files}) + file(RPATH_CHANGE FILE "${f}" OLD_RPATH "/rpath/foo" NEW_RPATH "/rpath/bar") +endforeach() diff --git a/Tests/RunCMake/file-RPATH/ELF.cmake b/Tests/RunCMake/file-RPATH/ELF.cmake index 558b2e2..0f445e8 100644 --- a/Tests/RunCMake/file-RPATH/ELF.cmake +++ b/Tests/RunCMake/file-RPATH/ELF.cmake @@ -1,4 +1,4 @@ -set(names +set(dynamic elf32lsb.bin elf32msb.bin elf64lsb.bin diff --git a/Tests/RunCMake/file-RPATH/XCOFF.cmake b/Tests/RunCMake/file-RPATH/XCOFF.cmake index b570920..f7464c2 100644 --- a/Tests/RunCMake/file-RPATH/XCOFF.cmake +++ b/Tests/RunCMake/file-RPATH/XCOFF.cmake @@ -1,4 +1,4 @@ -set(names +set(dynamic xcoff32.bin xcoff64.bin ) -- cgit v0.12 From 02f3e5be6a62ecee9d4c894792455f0bb747d7f7 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 24 Jun 2024 16:18:46 +0200 Subject: file(RPATH_CHANGE ...): no-op for static binary Setting `CMAKE_INSTALL_RPATH` on a project that contains statically linked executables breaks the install step, since rpaths cannot be changed. This commit makes rpath adjustments not an error on static libraries. Fixes #26078 --- Source/cmELF.cxx | 10 +++++++++- Source/cmELF.h | 3 +++ Source/cmSystemTools.cxx | 4 ++++ Tests/RunCMake/file-RPATH/ELF.cmake | 3 +++ Tests/RunCMake/file-RPATH/ELF/elf64lsb-static.bin | Bin 0 -> 4320 bytes 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100755 Tests/RunCMake/file-RPATH/ELF/elf64lsb-static.bin diff --git a/Source/cmELF.cxx b/Source/cmELF.cxx index a71e5f1..003f47b 100644 --- a/Source/cmELF.cxx +++ b/Source/cmELF.cxx @@ -112,6 +112,9 @@ public: virtual bool IsMips() const = 0; virtual void PrintInfo(std::ostream& os) const = 0; + /** Returns true if the ELF file has a dynamic section **/ + bool HasDynamicSection() const { return this->DynamicSectionIndex >= 0; } + // Lookup the SONAME in the DYNAMIC section. StringEntry const* GetSOName() { @@ -461,7 +464,7 @@ template bool cmELFInternalImpl::LoadDynamicSection() { // If there is no dynamic section we are done. - if (this->DynamicSectionIndex < 0) { + if (!this->HasDynamicSection()) { return false; } @@ -772,6 +775,11 @@ std::vector cmELF::EncodeDynamicEntries( return std::vector(); } +bool cmELF::HasDynamicSection() const +{ + return this->Valid() && this->Internal->HasDynamicSection(); +} + bool cmELF::GetSOName(std::string& soname) { if (StringEntry const* se = this->GetSOName()) { diff --git a/Source/cmELF.h b/Source/cmELF.h index ce8bd7f..dd37c65 100644 --- a/Source/cmELF.h +++ b/Source/cmELF.h @@ -88,6 +88,9 @@ public: std::vector EncodeDynamicEntries( const DynamicEntryList& entries) const; + /** Returns true if the ELF file has a dynamic section **/ + bool HasDynamicSection() const; + /** Get the SONAME field if any. */ bool GetSOName(std::string& soname); StringEntry const* GetSOName(); diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 093a18b..3affef0 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2817,6 +2817,10 @@ cm::optional AdjustRPathELF(std::string const& file, return cm::nullopt; // Not a valid ELF file. } + if (!elf.HasDynamicSection()) { + return true; // No dynamic section to update. + } + // Get the RPATH and RUNPATH entries from it. int se_count = 0; cmELF::StringEntry const* se[2] = { nullptr, nullptr }; diff --git a/Tests/RunCMake/file-RPATH/ELF.cmake b/Tests/RunCMake/file-RPATH/ELF.cmake index 0f445e8..b8919ef 100644 --- a/Tests/RunCMake/file-RPATH/ELF.cmake +++ b/Tests/RunCMake/file-RPATH/ELF.cmake @@ -4,6 +4,9 @@ set(dynamic elf64lsb.bin elf64msb.bin ) +set(static + elf64lsb-static.bin + ) set(format ELF) include(${CMAKE_CURRENT_LIST_DIR}/Common.cmake) diff --git a/Tests/RunCMake/file-RPATH/ELF/elf64lsb-static.bin b/Tests/RunCMake/file-RPATH/ELF/elf64lsb-static.bin new file mode 100755 index 0000000..b48c4f3 Binary files /dev/null and b/Tests/RunCMake/file-RPATH/ELF/elf64lsb-static.bin differ -- cgit v0.12