cmBinUtilsLinuxELFLinker.cxx 6.7 KB

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