cmBinUtilsLinuxELFLinker.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if (!this->LDConfigTool->GetLDConfigPaths(this->LDConfigPaths)) {
  67. return false;
  68. }
  69. } else {
  70. std::ostringstream e;
  71. e << "Invalid value for CMAKE_LDCONFIG_TOOL: " << ldConfigTool;
  72. this->SetError(e.str());
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool cmBinUtilsLinuxELFLinker::ScanDependencies(
  78. std::string const& file, cmStateEnums::TargetType /* unused */)
  79. {
  80. std::vector<std::string> parentRpaths;
  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. return this->ScanDependencies(file, parentRpaths);
  96. }
  97. bool cmBinUtilsLinuxELFLinker::ScanDependencies(
  98. std::string const& file, std::vector<std::string> const& parentRpaths)
  99. {
  100. std::string origin = cmSystemTools::GetFilenamePath(file);
  101. std::vector<std::string> needed;
  102. std::vector<std::string> rpaths;
  103. std::vector<std::string> runpaths;
  104. if (!this->Tool->GetFileInfo(file, needed, rpaths, runpaths)) {
  105. return false;
  106. }
  107. for (auto& runpath : runpaths) {
  108. runpath = ReplaceOrigin(runpath, origin);
  109. }
  110. for (auto& rpath : rpaths) {
  111. rpath = ReplaceOrigin(rpath, origin);
  112. }
  113. std::vector<std::string> searchPaths;
  114. if (!runpaths.empty()) {
  115. searchPaths = runpaths;
  116. } else {
  117. searchPaths = rpaths;
  118. searchPaths.insert(searchPaths.end(), parentRpaths.begin(),
  119. parentRpaths.end());
  120. }
  121. searchPaths.insert(searchPaths.end(), this->LDConfigPaths.begin(),
  122. this->LDConfigPaths.end());
  123. for (auto const& dep : needed) {
  124. if (!this->Archive->IsPreExcluded(dep)) {
  125. std::string path;
  126. bool resolved = false;
  127. if (dep.find('/') != std::string::npos) {
  128. this->SetError("Paths to dependencies are not supported");
  129. return false;
  130. }
  131. if (!this->ResolveDependency(dep, searchPaths, path, resolved)) {
  132. return false;
  133. }
  134. if (resolved) {
  135. if (!this->Archive->IsPostExcluded(path)) {
  136. bool unique;
  137. this->Archive->AddResolvedPath(dep, path, unique);
  138. if (unique) {
  139. std::vector<std::string> combinedParentRpaths = parentRpaths;
  140. combinedParentRpaths.insert(combinedParentRpaths.end(),
  141. rpaths.begin(), rpaths.end());
  142. if (!this->ScanDependencies(path, combinedParentRpaths)) {
  143. return false;
  144. }
  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. cmELF elf(filename);
  158. if (!elf) {
  159. return false;
  160. }
  161. return machine == 0 || machine == elf.GetMachine();
  162. }
  163. }
  164. bool cmBinUtilsLinuxELFLinker::ResolveDependency(
  165. std::string const& name, std::vector<std::string> const& searchPaths,
  166. std::string& path, bool& resolved)
  167. {
  168. for (auto const& searchPath : searchPaths) {
  169. path = cmStrCat(searchPath, '/', name);
  170. if (cmSystemTools::PathExists(path) &&
  171. FileHasArchitecture(path.c_str(), this->Machine)) {
  172. resolved = true;
  173. return true;
  174. }
  175. }
  176. for (auto const& searchPath : this->Archive->GetSearchDirectories()) {
  177. path = cmStrCat(searchPath, '/', name);
  178. if (cmSystemTools::PathExists(path) &&
  179. FileHasArchitecture(path.c_str(), this->Machine)) {
  180. std::ostringstream warning;
  181. warning << "Dependency " << name << " found in search directory:\n "
  182. << searchPath
  183. << "\nSee file(GET_RUNTIME_DEPENDENCIES) documentation for "
  184. << "more information.";
  185. this->Archive->GetMakefile()->IssueMessage(MessageType::WARNING,
  186. warning.str());
  187. resolved = true;
  188. return true;
  189. }
  190. }
  191. resolved = false;
  192. return true;
  193. }