[Cmake-commits] [cmake-commits] king committed cmSystemTools.cxx 1.379 1.380

cmake-commits at cmake.org cmake-commits at cmake.org
Thu Aug 14 09:53:19 EDT 2008


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv32643/Source

Modified Files:
	cmSystemTools.cxx 
Log Message:
BUG: Remove both RPATH and RUNPATH entries

Removal of the RPATH and RUNPATH from ELF binaries must work when both
entries are present.  Both entries should be removed.  Previously only
one would be removed and the other would be blanked because it pointed
at the same string which was zeroed.  This fixes gentoo bug number
224901.


Index: cmSystemTools.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.cxx,v
retrieving revision 1.379
retrieving revision 1.380
diff -C 2 -d -r1.379 -r1.380
*** cmSystemTools.cxx	27 May 2008 18:46:59 -0000	1.379
--- cmSystemTools.cxx	14 Aug 2008 13:53:17 -0000	1.380
***************
*** 27,30 ****
--- 27,31 ----
  # include <cmsys/Terminal.h>
  #endif
+ #include <cmsys/stl/algorithm>
  
  #if defined(_WIN32)
***************
*** 2479,2485 ****
  {
  #if defined(CMAKE_USE_ELF_PARSER)
!   unsigned long rpathPosition = 0;
!   unsigned long rpathSize = 0;
!   unsigned long rpathEntryPosition = 0;
    std::vector<char> bytes;
    {
--- 2480,2487 ----
  {
  #if defined(CMAKE_USE_ELF_PARSER)
!   int zeroCount = 0;
!   unsigned long zeroPosition[2] = {0,0};
!   unsigned long zeroSize[2] = {0,0};
!   unsigned long bytesBegin = 0;
    std::vector<char> bytes;
    {
***************
*** 2487,2530 ****
    cmELF elf(file.c_str());
  
!   // Get the RPATH or RUNPATH entry from it.
!   cmELF::StringEntry const* se = elf.GetRPath();
!   if(!se)
      {
!     se = elf.GetRunPath();
      }
! 
!   if(se)
      {
!     // Store information about the entry.
!     rpathPosition = se->Position;
!     rpathSize = se->Size;
!     rpathEntryPosition = elf.GetDynamicEntryPosition(se->IndexInSection);
  
!     // Get the file range containing the rest of the DYNAMIC table
!     // after the RPATH entry.
!     unsigned long nextEntryPosition =
!       elf.GetDynamicEntryPosition(se->IndexInSection+1);
!     unsigned int count = elf.GetDynamicEntryCount();
!     if(count == 0)
        {
!       // This should happen only for invalid ELF files where a DT_NULL
!       // appears before the end of the table.
!       if(emsg)
!         {
!         *emsg = "DYNAMIC section contains a DT_NULL before the end.";
!         }
!       return false;
        }
!     unsigned long nullEntryPosition = elf.GetDynamicEntryPosition(count);
  
!     // Allocate and fill a buffer with zeros.
!     bytes.resize(nullEntryPosition - rpathEntryPosition, 0);
  
!     // Read the part of the DYNAMIC section header that will move.
!     // The remainder of the buffer will be left with zeros which
!     // represent a DT_NULL entry.
!     if(!elf.ReadBytes(nextEntryPosition,
!                       nullEntryPosition - nextEntryPosition,
!                       &bytes[0]))
        {
        if(emsg)
--- 2489,2564 ----
    cmELF elf(file.c_str());
  
!   // Get the RPATH and RUNPATH entries from it and sort them by index
!   // in the dynamic section header.
!   int se_count = 0;
!   cmELF::StringEntry const* se[2] = {0, 0};
!   if(cmELF::StringEntry const* se_rpath = elf.GetRPath())
      {
!     se[se_count++] = se_rpath;
      }
!   if(cmELF::StringEntry const* se_runpath = elf.GetRunPath())
      {
!     se[se_count++] = se_runpath;
!     }
!   if(se_count == 0)
!     {
!     // There is no RPATH or RUNPATH anyway.
!     return true;
!     }
!   if(se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection)
!     {
!     cmsys_stl::swap(se[0], se[1]);
!     }
  
!   // Get the size of the dynamic section header.
!   unsigned int count = elf.GetDynamicEntryCount();
!   if(count == 0)
!     {
!     // This should happen only for invalid ELF files where a DT_NULL
!     // appears before the end of the table.
!     if(emsg)
        {
!       *emsg = "DYNAMIC section contains a DT_NULL before the end.";
        }
!     return false;
!     }
  
!   // Save information about the string entries to be zeroed.
!   zeroCount = se_count;
!   for(int i=0; i < se_count; ++i)
!     {
!     zeroPosition[i] = se[i]->Position;
!     zeroSize[i] = se[i]->Size;
!     }
  
!   // Get the range of file positions corresponding to each entry and
!   // the rest of the table after them.
!   unsigned long entryBegin[3] = {0,0,0};
!   unsigned long entryEnd[2] = {0,0};
!   for(int i=0; i < se_count; ++i)
!     {
!     entryBegin[i] = elf.GetDynamicEntryPosition(se[i]->IndexInSection);
!     entryEnd[i] = elf.GetDynamicEntryPosition(se[i]->IndexInSection+1);
!     }
!   entryBegin[se_count] = elf.GetDynamicEntryPosition(count);
! 
!   // The data are to be written over the old table entries starting at
!   // the first one being removed.
!   bytesBegin = entryBegin[0];
!   unsigned long bytesEnd = entryBegin[se_count];
! 
!   // Allocate a buffer to hold the part of the file to be written.
!   // Initialize it with zeros.
!   bytes.resize(bytesEnd - bytesBegin, 0);
! 
!   // Read the part of the DYNAMIC section header that will move.
!   // The remainder of the buffer will be left with zeros which
!   // represent a DT_NULL entry.
!   char* data = &bytes[0];
!   for(int i=0; i < se_count; ++i)
!     {
!     // Read data between the entries being removed.
!     unsigned long sz = entryBegin[i+1] - entryEnd[i];
!     if(sz > 0 && !elf.ReadBytes(entryEnd[i], sz, data))
        {
        if(emsg)
***************
*** 2534,2542 ****
        return false;
        }
!     }
!   else
!     {
!     // There is no RPATH or RUNPATH anyway.
!     return true;
      }
    }
--- 2568,2572 ----
        return false;
        }
!     data += sz;
      }
    }
***************
*** 2555,2559 ****
  
    // Write the new DYNAMIC table header.
!   if(!f.seekp(rpathEntryPosition))
      {
      if(emsg)
--- 2585,2589 ----
  
    // Write the new DYNAMIC table header.
!   if(!f.seekp(bytesBegin))
      {
      if(emsg)
***************
*** 2572,2602 ****
      }
  
!   // Fill the RPATH string with zero bytes.
!   if(!f.seekp(rpathPosition))
      {
!     if(emsg)
        {
!       *emsg = "Error seeking to RPATH position.";
        }
!     return false;
!     }
!   for(unsigned long i=0; i < rpathSize; ++i)
!     {
!     f << '\0';
!     }
! 
!   // Make sure everything was okay.
!   if(f)
!     {
!     return true;
!     }
!   else
!     {
!     if(emsg)
        {
!       *emsg = "Error writing the empty rpath to the file.";
        }
-     return false;
      }
  #else
    (void)file;
--- 2602,2632 ----
      }
  
!   // Fill the RPATH and RUNPATH strings with zero bytes.
!   for(int i=0; i < zeroCount; ++i)
      {
!     if(!f.seekp(zeroPosition[i]))
        {
!       if(emsg)
!         {
!         *emsg = "Error seeking to RPATH position.";
!         }
!       return false;
        }
!     for(unsigned long j=0; j < zeroSize[i]; ++j)
        {
!       f << '\0';
!       }
!     if(!f)
!       {
!       if(emsg)
!         {
!         *emsg = "Error writing the empty rpath string to the file.";
!         }
!       return false;
        }
      }
+ 
+   // Everything was updated successfully.
+   return true;
  #else
    (void)file;



More information about the Cmake-commits mailing list