cmBinUtilsLinuxELFLinker.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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) {
  140. std::vector<std::string> combinedParentRpaths = parentRpaths;
  141. combinedParentRpaths.insert(combinedParentRpaths.end(),
  142. rpaths.begin(), rpaths.end());
  143. if (!this->ScanDependencies(path, combinedParentRpaths)) {
  144. return false;
  145. }
  146. }
  147. }
  148. } else {
  149. this->Archive->AddUnresolvedPath(dep);
  150. }
  151. }
  152. }
  153. return true;
  154. }
  155. namespace {
  156. bool FileHasArchitecture(const char* filename, std::uint16_t machine)
  157. {
  158. cmELF elf(filename);
  159. if (!elf) {
  160. return false;
  161. }
  162. return machine == 0 || machine == elf.GetMachine();
  163. }
  164. }
  165. bool cmBinUtilsLinuxELFLinker::ResolveDependency(
  166. std::string const& name, std::vector<std::string> const& searchPaths,
  167. std::string& path, bool& resolved)
  168. {
  169. for (auto const& searchPath : searchPaths) {
  170. path = cmStrCat(searchPath, '/', name);
  171. if (cmSystemTools::PathExists(path) &&
  172. FileHasArchitecture(path.c_str(), this->Machine)) {
  173. resolved = true;
  174. return true;
  175. }
  176. }
  177. for (auto const& searchPath : this->Archive->GetSearchDirectories()) {
  178. path = cmStrCat(searchPath, '/', name);
  179. if (cmSystemTools::PathExists(path) &&
  180. FileHasArchitecture(path.c_str(), this->Machine)) {
  181. std::ostringstream warning;
  182. warning << "Dependency " << name << " found in search directory:\n "
  183. << searchPath
  184. << "\nSee file(GET_RUNTIME_DEPENDENCIES) documentation for "
  185. << "more information.";
  186. this->Archive->GetMakefile()->IssueMessage(MessageType::WARNING,
  187. warning.str());
  188. resolved = true;
  189. return true;
  190. }
  191. }
  192. resolved = false;
  193. return true;
  194. }