cmBinUtilsLinuxELFLinker.cxx 5.9 KB

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