| 3925 | } |
| 3926 | |
| 3927 | static cm::optional<bool> RemoveRPathELF(std::string const& file, |
| 3928 | std::string* emsg, bool* removed) |
| 3929 | { |
| 3930 | if (removed) { |
| 3931 | *removed = false; |
| 3932 | } |
| 3933 | int zeroCount = 0; |
| 3934 | unsigned long zeroPosition[2] = { 0, 0 }; |
| 3935 | unsigned long zeroSize[2] = { 0, 0 }; |
| 3936 | unsigned long bytesBegin = 0; |
| 3937 | std::vector<char> bytes; |
| 3938 | { |
| 3939 | // Parse the ELF binary. |
| 3940 | cmELF elf(file.c_str()); |
| 3941 | if (!elf) { |
| 3942 | return cm::nullopt; // Not a valid ELF file. |
| 3943 | } |
| 3944 | |
| 3945 | // Get the RPATH and RUNPATH entries from it and sort them by index |
| 3946 | // in the dynamic section header. |
| 3947 | int se_count = 0; |
| 3948 | cmELF::StringEntry const* se[2] = { nullptr, nullptr }; |
| 3949 | if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) { |
| 3950 | se[se_count++] = se_rpath; |
| 3951 | } |
| 3952 | if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) { |
| 3953 | se[se_count++] = se_runpath; |
| 3954 | } |
| 3955 | if (se_count == 0) { |
| 3956 | // There is no RPATH or RUNPATH anyway. |
| 3957 | return true; |
| 3958 | } |
| 3959 | if (se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection) { |
| 3960 | std::swap(se[0], se[1]); |
| 3961 | } |
| 3962 | |
| 3963 | // Obtain a copy of the dynamic entries |
| 3964 | cmELF::DynamicEntryList dentries = elf.GetDynamicEntries(); |
| 3965 | if (dentries.empty()) { |
| 3966 | // This should happen only for invalid ELF files where a DT_NULL |
| 3967 | // appears before the end of the table. |
| 3968 | if (emsg) { |
| 3969 | *emsg = "DYNAMIC section contains a DT_NULL before the end."; |
| 3970 | } |
| 3971 | return false; |
| 3972 | } |
| 3973 | |
| 3974 | // Save information about the string entries to be zeroed. |
| 3975 | zeroCount = se_count; |
| 3976 | for (int i = 0; i < se_count; ++i) { |
| 3977 | zeroPosition[i] = se[i]->Position; |
| 3978 | zeroSize[i] = se[i]->Size; |
| 3979 | } |
| 3980 | |
| 3981 | // Get size of one DYNAMIC entry |
| 3982 | unsigned long const sizeof_dentry = |
| 3983 | elf.GetDynamicEntryPosition(1) - elf.GetDynamicEntryPosition(0); |
| 3984 |
no test coverage detected
searching dependent graphs…