cmBinUtilsLinuxELFLinker.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmBinUtilsLinuxELFLinker.h"
  4. #include <sstream>
  5. #include <cm/memory>
  6. #include <cm/string_view>
  7. #include <cmsys/RegularExpression.hxx>
  8. #include "cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.h"
  9. #include "cmLDConfigLDConfigTool.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmRuntimeDependencyArchive.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #ifdef CMake_USE_ELF_PARSER
  16. # include "cmELF.h"
  17. #endif
  18. static std::string ReplaceOrigin(const std::string& rpath,
  19. const std::string& origin)
  20. {
  21. static const cmsys::RegularExpression originRegex(
  22. "(\\$ORIGIN)([^a-zA-Z0-9_]|$)");
  23. static const cmsys::RegularExpression originCurlyRegex("\\${ORIGIN}");
  24. cmsys::RegularExpressionMatch match;
  25. if (originRegex.find(rpath.c_str(), match)) {
  26. cm::string_view pathv(rpath);
  27. auto begin = pathv.substr(0, match.start(1));
  28. auto end = pathv.substr(match.end(1));
  29. return cmStrCat(begin, origin, end);
  30. }
  31. if (originCurlyRegex.find(rpath.c_str(), match)) {
  32. cm::string_view pathv(rpath);
  33. auto begin = pathv.substr(0, match.start());
  34. auto end = pathv.substr(match.end());
  35. return cmStrCat(begin, origin, end);
  36. }
  37. return rpath;
  38. }
  39. cmBinUtilsLinuxELFLinker::cmBinUtilsLinuxELFLinker(
  40. cmRuntimeDependencyArchive* archive)
  41. : cmBinUtilsLinker(archive)
  42. {
  43. }
  44. bool cmBinUtilsLinuxELFLinker::Prepare()
  45. {
  46. std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
  47. if (tool.empty()) {
  48. tool = "objdump";
  49. }
  50. if (tool == "objdump") {
  51. this->Tool =
  52. cm::make_unique<cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool>(
  53. this->Archive);
  54. } else {
  55. std::ostringstream e;
  56. e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
  57. this->SetError(e.str());
  58. return false;
  59. }
  60. std::string ldConfigTool =
  61. this->Archive->GetMakefile()->GetSafeDefinition("CMAKE_LDCONFIG_TOOL");
  62. if (ldConfigTool.empty()) {
  63. ldConfigTool = "ldconfig";
  64. }
  65. if (ldConfigTool == "ldconfig") {
  66. this->LDConfigTool =
  67. cm::make_unique<cmLDConfigLDConfigTool>(this->Archive);
  68. } else {
  69. std::ostringstream e;
  70. e << "Invalid value for CMAKE_LDCONFIG_TOOL: " << ldConfigTool;
  71. this->SetError(e.str());
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool cmBinUtilsLinuxELFLinker::ScanDependencies(
  77. std::string const& file, cmStateEnums::TargetType /* unused */)
  78. {
  79. std::vector<std::string> parentRpaths;
  80. #ifdef CMake_USE_ELF_PARSER
  81. cmELF elf(file.c_str());
  82. if (!elf) {
  83. return false;
  84. }
  85. if (elf.GetMachine() != 0) {
  86. if (this->Machine != 0) {
  87. if (elf.GetMachine() != this->Machine) {
  88. this->SetError("All files must have the same architecture.");
  89. return false;
  90. }
  91. } else {
  92. this->Machine = elf.GetMachine();
  93. }
  94. }
  95. #endif
  96. return this->ScanDependencies(file, parentRpaths);
  97. }
  98. bool cmBinUtilsLinuxELFLinker::ScanDependencies(
  99. std::string const& file, std::vector<std::string> const& parentRpaths)
  100. {
  101. std::string origin = cmSystemTools::GetFilenamePath(file);
  102. std::vector<std::string> needed;
  103. std::vector<std::string> rpaths;
  104. std::vector<std::string> runpaths;
  105. if (!this->Tool->GetFileInfo(file, needed, rpaths, runpaths)) {
  106. return false;
  107. }
  108. for (auto& runpath : runpaths) {
  109. runpath = ReplaceOrigin(runpath, origin);
  110. }
  111. for (auto& rpath : rpaths) {
  112. rpath = ReplaceOrigin(rpath, origin);
  113. }
  114. std::vector<std::string> searchPaths;
  115. if (!runpaths.empty()) {
  116. searchPaths = runpaths;
  117. } else {
  118. searchPaths = rpaths;
  119. searchPaths.insert(searchPaths.end(), parentRpaths.begin(),
  120. parentRpaths.end());
  121. }
  122. std::vector<std::string> ldConfigPaths;
  123. if (!this->LDConfigTool->GetLDConfigPaths(ldConfigPaths)) {
  124. return false;
  125. }
  126. searchPaths.insert(searchPaths.end(), ldConfigPaths.begin(),
  127. ldConfigPaths.end());
  128. for (auto const& dep : needed) {
  129. if (!this->Archive->IsPreExcluded(dep)) {
  130. std::string path;
  131. bool resolved = false;
  132. if (dep.find('/') != std::string::npos) {
  133. this->SetError("Paths to dependencies are not supported");
  134. return false;
  135. }
  136. if (!this->ResolveDependency(dep, searchPaths, path, resolved)) {
  137. return false;
  138. }
  139. if (resolved) {
  140. if (!this->Archive->IsPostExcluded(path)) {
  141. bool unique;
  142. this->Archive->AddResolvedPath(dep, path, unique);
  143. if (unique && !this->ScanDependencies(path, rpaths)) {
  144. return false;
  145. }
  146. }
  147. } else {
  148. this->Archive->AddUnresolvedPath(dep);
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. namespace {
  155. bool FileHasArchitecture(const char* filename, std::uint16_t machine)
  156. {
  157. #ifdef CMake_USE_ELF_PARSER
  158. cmELF elf(filename);
  159. if (!elf) {
  160. return false;
  161. }
  162. return machine == 0 || machine == elf.GetMachine();
  163. #else
  164. return true;
  165. #endif
  166. }
  167. }
  168. bool cmBinUtilsLinuxELFLinker::ResolveDependency(
  169. std::string const& name, std::vector<std::string> const& searchPaths,
  170. std::string& path, bool& resolved)
  171. {
  172. for (auto const& searchPath : searchPaths) {
  173. path = cmStrCat(searchPath, '/', name);
  174. if (cmSystemTools::PathExists(path) &&
  175. FileHasArchitecture(path.c_str(), this->Machine)) {
  176. resolved = true;
  177. return true;
  178. }
  179. }
  180. for (auto const& searchPath : this->Archive->GetSearchDirectories()) {
  181. path = cmStrCat(searchPath, '/', name);
  182. if (cmSystemTools::PathExists(path) &&
  183. FileHasArchitecture(path.c_str(), this->Machine)) {
  184. std::ostringstream warning;
  185. warning << "Dependency " << name << " found in search directory:\n "
  186. << searchPath
  187. << "\nSee file(GET_RUNTIME_DEPENDENCIES) documentation for "
  188. << "more information.";
  189. this->Archive->GetMakefile()->IssueMessage(MessageType::WARNING,
  190. warning.str());
  191. resolved = true;
  192. return true;
  193. }
  194. }
  195. resolved = false;
  196. return true;
  197. }