0001-Add-option-to-make-the-rpath-relative-under-a-specif.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. From 618220bfb55c875d6a4d197cb24fe632ac93ec85 Mon Sep 17 00:00:00 2001
  2. From: Wolfgang Grandegger <wg@grandegger.com>
  3. Date: Mon, 20 Feb 2017 16:29:24 +0100
  4. Subject: [PATCH] Add option to make the rpath relative under a specified root
  5. directory
  6. Running "patchelf" with the option "--make-rpath-relative ROOTDIR" will
  7. modify or delete the RPATHDIRs according the following rules
  8. similar to Martin's patches [1] making the Buildroot toolchaing/SDK
  9. relocatable.
  10. RPATHDIR starts with "$ORIGIN":
  11. The original build-system already took care of setting a relative
  12. RPATH, resolve it and test if it's valid (does exist)
  13. RPATHDIR starts with ROOTDIR:
  14. The original build-system added some absolute RPATH (absolute on
  15. the build machine). Test if it's valid (does exist).
  16. ROOTDIR/RPATHDIR exists:
  17. The original build-system already took care of setting an absolute
  18. RPATH (absolute in the final rootfs), resolve it and test if it's
  19. valid (does exist).
  20. RPATHDIR points somewhere else:
  21. (can be anywhere: build trees, staging tree, host location,
  22. non-existing location, etc.). Just discard such a path.
  23. The option "--no-standard-libs" will discard RPATHDIRs ROOTDIR/lib and
  24. ROOTDIR/usr/lib. Like "--shrink-rpath", RPATHDIRs are also discarded
  25. if the directories do not contain a library referenced by the
  26. DT_NEEDED fields.
  27. If the option "--relative-to-file" is given, the rpath will start
  28. with "$ORIGIN" making it relative to the ELF file, otherwise an
  29. absolute path relative to ROOTDIR will be used.
  30. A pull request for a similar patch [2] for mainline inclusion is
  31. pending.
  32. [1] http://lists.busybox.net/pipermail/buildroot/2016-April/159422.html
  33. [2] https://github.com/NixOS/patchelf/pull/118
  34. Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
  35. [Fabrice: update for 0.13]
  36. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  37. ---
  38. src/patchelf.cc | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++------
  39. 1 file changed, 175 insertions(+), 21 deletions(-)
  40. diff --git a/src/patchelf.cc b/src/patchelf.cc
  41. index 1d9a772..35b4a33 100644
  42. --- a/src/patchelf.cc
  43. +++ b/src/patchelf.cc
  44. @@ -46,6 +46,10 @@ static bool debugMode = false;
  45. static bool forceRPath = false;
  46. +static bool noStandardLibDirs = false;
  47. +
  48. +static bool relativeToFile = false;
  49. +
  50. static std::vector<std::string> fileNames;
  51. static std::string outputFileName;
  52. static bool alwaysWrite = false;
  53. @@ -77,6 +81,49 @@ static unsigned int getPageSize(){
  54. return pageSize;
  55. }
  56. +static bool absolutePathExists(const std::string & path, std::string & canonicalPath)
  57. +{
  58. + char *cpath = realpath(path.c_str(), NULL);
  59. + if (cpath) {
  60. + canonicalPath = cpath;
  61. + free(cpath);
  62. + return true;
  63. + } else {
  64. + return false;
  65. + }
  66. +}
  67. +
  68. +static std::string makePathRelative(const std::string & path,
  69. + const std::string & refPath)
  70. +{
  71. + std::string relPath = "$ORIGIN";
  72. + std::string p = path, refP = refPath;
  73. + size_t pos;
  74. +
  75. + /* Strip the common part of path and refPath */
  76. + while (true) {
  77. + pos = p.find_first_of('/', 1);
  78. + if (refP.find_first_of('/', 1) != pos)
  79. + break;
  80. + if (p.substr(0, pos) != refP.substr(0, pos))
  81. + break;
  82. + if (pos == std::string::npos)
  83. + break;
  84. + p = p.substr(pos);
  85. + refP = refP.substr(pos);
  86. + }
  87. + /* Check if both pathes are equal */
  88. + if (p != refP) {
  89. + pos = 0;
  90. + while (pos != std::string::npos) {
  91. + pos =refP.find_first_of('/', pos + 1);
  92. + relPath.append("/..");
  93. + }
  94. + relPath.append(p);
  95. + }
  96. +
  97. + return relPath;
  98. +}
  99. template<ElfFileParams>
  100. class ElfFile
  101. @@ -183,9 +230,13 @@ public:
  102. void setInterpreter(const string & newInterpreter);
  103. - typedef enum { rpPrint, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;
  104. + typedef enum { rpPrint, rpShrink, rpMakeRelative, rpSet, rpAdd, rpRemove} RPathOp;
  105. +
  106. + bool libFoundInRPath(const std::string & dirName,
  107. + const std::vector<std::string> neededLibs,
  108. + std::vector<bool> & neededLibFound);
  109. - void modifyRPath(RPathOp op, const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath);
  110. + void modifyRPath(RPathOp op, std::string rootDir, const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath, const std::string & fileName);
  111. void addNeeded(set<string> libs);
  112. @@ -1041,8 +1092,28 @@ static void concatToRPath(string & rpath, const string & path)
  113. template<ElfFileParams>
  114. -void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
  115. - const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath)
  116. +bool ElfFile<ElfFileParamNames>::libFoundInRPath(const std::string & dirName,
  117. + const std::vector<std::string> neededLibs, std::vector<bool> & neededLibFound)
  118. +{
  119. + /* For each library that we haven't found yet, see if it
  120. + exists in this directory. */
  121. + bool libFound = false;
  122. + for (unsigned int j = 0; j < neededLibs.size(); ++j)
  123. + if (!neededLibFound[j]) {
  124. + std::string libName = dirName + "/" + neededLibs[j];
  125. + struct stat st;
  126. + if (stat(libName.c_str(), &st) == 0) {
  127. + neededLibFound[j] = true;
  128. + libFound = true;
  129. + }
  130. + }
  131. + return libFound;
  132. +}
  133. +
  134. +
  135. +template<ElfFileParams>
  136. +void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, std::string rootDir,
  137. + const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath, const std::string & fileName)
  138. {
  139. Elf_Shdr & shdrDynamic = findSection(".dynamic");
  140. @@ -1096,6 +1167,11 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
  141. return;
  142. }
  143. + if (op == rpMakeRelative && !rpath) {
  144. + debug("no RPATH to make relative\n");
  145. + return;
  146. + }
  147. +
  148. if (op == rpShrink && !rpath) {
  149. debug("no RPATH to shrink\n");
  150. return;
  151. @@ -1120,31 +1196,80 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
  152. continue;
  153. }
  154. - /* For each library that we haven't found yet, see if it
  155. - exists in this directory. */
  156. - bool libFound = false;
  157. - for (unsigned int j = 0; j < neededLibs.size(); ++j)
  158. - if (!neededLibFound[j]) {
  159. - std::string libName = dirName + "/" + neededLibs[j];
  160. - try {
  161. - Elf32_Half library_e_machine = getElfType(readFile(libName, sizeof(Elf32_Ehdr))).machine;
  162. - if (rdi(library_e_machine) == rdi(hdr->e_machine)) {
  163. - neededLibFound[j] = true;
  164. - libFound = true;
  165. - } else
  166. - debug("ignoring library '%s' because its machine type differs\n", libName.c_str());
  167. - } catch (SysError & e) {
  168. - if (e.errNo != ENOENT) throw;
  169. - }
  170. - }
  171. -
  172. - if (!libFound)
  173. + if (!libFoundInRPath(dirName, neededLibs, neededLibFound))
  174. debug("removing directory '%s' from RPATH\n", dirName.c_str());
  175. else
  176. concatToRPath(newRPath, dirName);
  177. }
  178. }
  179. + /* Make the the RPATH relative to the specified path */
  180. + if (op == rpMakeRelative) {
  181. + std::vector<bool> neededLibFound(neededLibs.size(), false);
  182. + std::string fileDir = fileName.substr(0, fileName.find_last_of("/"));
  183. +
  184. + newRPath = "";
  185. +
  186. + std::vector<std::string> rpathDirs = splitColonDelimitedString(rpath);
  187. + for (std::vector<std::string>::iterator it = rpathDirs.begin(); it != rpathDirs.end(); ++it) {
  188. + const std::string & dirName = *it;
  189. +
  190. + std::string canonicalPath;
  191. +
  192. + /* Figure out if we should keep or discard the path. There are several
  193. + cases to be handled:
  194. + "dirName" starts with "$ORIGIN":
  195. + The original build-system already took care of setting a relative
  196. + RPATH. Resolve it and test if it's valid (does exist).
  197. + "dirName" start with "rootDir":
  198. + The original build-system added some absolute RPATH (absolute on
  199. + the build machine). Test if it's valid (does exist).
  200. + "rootDir"/"dirName" exists:
  201. + The original build-system already took care of setting an absolute
  202. + RPATH (absolute in the final rootfs). Resolve it and test if it's
  203. + valid (does exist).
  204. + "dirName" points somewhere else:
  205. + (can be anywhere: build trees, staging tree, host location,
  206. + non-existing location, etc.). Just discard such a path. */
  207. + if (!dirName.compare(0, 7, "$ORIGIN")) {
  208. + std::string path = fileDir + dirName.substr(7);
  209. + if (!absolutePathExists(path, canonicalPath)) {
  210. + debug("removing directory '%s' from RPATH because '%s' doesn't exist\n",
  211. + dirName.c_str(), path.c_str());
  212. + continue;
  213. + }
  214. + } else if (!dirName.compare(0, rootDir.length(), rootDir)) {
  215. + if (!absolutePathExists(dirName, canonicalPath)) {
  216. + debug("removing directory '%s' from RPATH because it doesn't exist\n", dirName.c_str());
  217. + continue;
  218. + }
  219. + } else {
  220. + std::string path = rootDir + dirName;
  221. + if (!absolutePathExists(path, canonicalPath)) {
  222. + debug("removing directory '%s' from RPATH because it's not in rootdir\n",
  223. + dirName.c_str());
  224. + continue;
  225. + }
  226. + }
  227. +
  228. + if (noStandardLibDirs) {
  229. + if (!canonicalPath.compare(rootDir + "/lib") ||
  230. + !canonicalPath.compare(rootDir + "/usr/lib")) {
  231. + debug("removing directory '%s' from RPATH because it's a standard library directory\n",
  232. + dirName.c_str());
  233. + continue;
  234. + }
  235. + }
  236. +
  237. + /* Finally make "canonicalPath" relative to "filedir" in "rootDir" */
  238. + if (relativeToFile)
  239. + concatToRPath(newRPath, makePathRelative(canonicalPath, fileDir));
  240. + else
  241. + concatToRPath(newRPath, canonicalPath.substr(rootDir.length()));
  242. + debug("keeping relative path of %s\n", canonicalPath.c_str());
  243. + }
  244. + }
  245. +
  246. if (op == rpRemove) {
  247. if (!rpath) {
  248. debug("no RPATH to delete\n");
  249. @@ -1413,7 +1543,9 @@ static bool shrinkRPath = false;
  250. static bool setRPath = false;
  251. static bool addRPath = false;
  252. static bool printRPath = false;
  253. +static bool makeRPathRelative = false;
  254. static std::string newRPath;
  255. +static std::string rootDir;
  256. static std::set<std::string> neededLibsToRemove;
  257. static std::map<std::string, std::string> neededLibsToReplace;
  258. static std::set<std::string> neededLibsToAdd;
  259. @@ -1438,16 +1570,18 @@ static void patchElf2(ElfFile & elfFile)
  260. elfFile.setInterpreter(newInterpreter);
  261. if (printRPath)
  262. - elfFile.modifyRPath(elfFile.rpPrint, {}, "");
  263. + elfFile.modifyRPath(elfFile.rpPrint, "", {}, "", fileName);
  264. if (shrinkRPath)
  265. - elfFile.modifyRPath(elfFile.rpShrink, allowedRpathPrefixes, "");
  266. + elfFile.modifyRPath(elfFile.rpShrink, "", allowedRpathPrefixes, "", fileName);
  267. else if (removeRPath)
  268. - elfFile.modifyRPath(elfFile.rpRemove, {}, "");
  269. + elfFile.modifyRPath(elfFile.rpRemove, "", {}, "", fileName);
  270. else if (setRPath)
  271. - elfFile.modifyRPath(elfFile.rpSet, {}, newRPath);
  272. + elfFile.modifyRPath(elfFile.rpSet, "", {}, newRPath, fileName);
  273. else if (addRPath)
  274. - elfFile.modifyRPath(elfFile.rpAdd, {}, newRPath);
  275. + elfFile.modifyRPath(elfFile.rpAdd, "", {}, newRPath, fileName);
  276. + else if (makeRPathRelative)
  277. + elfFile.modifyRPath(elfFile.rpMakeRelative, rootDir, {}, "", fileName);
  278. if (printNeeded) elfFile.printNeededLibs();
  279. @@ -1508,6 +1642,9 @@ void showHelp(const string & progName)
  280. [--remove-rpath]\n\
  281. [--shrink-rpath]\n\
  282. [--allowed-rpath-prefixes PREFIXES]\t\tWith '--shrink-rpath', reject rpath entries not starting with the allowed prefix\n\
  283. + [--make-rpath-relative ROOTDIR]\n\
  284. + [--no-standard-lib-dirs]\n\
  285. + [--relative-to-file]\n\
  286. [--print-rpath]\n\
  287. [--force-rpath]\n\
  288. [--add-needed LIBRARY]\n\
  289. @@ -1564,6 +1701,17 @@ int main(int argc, char * * argv)
  290. setRPath = true;
  291. newRPath = argv[i];
  292. }
  293. + else if (arg == "--make-rpath-relative") {
  294. + if (++i == argc) error("missing argument to --make-rpath-relative");
  295. + makeRPathRelative = true;
  296. + rootDir = argv[i];
  297. + }
  298. + else if (arg == "--no-standard-lib-dirs") {
  299. + noStandardLibDirs = true;
  300. + }
  301. + else if (arg == "--relative-to-file") {
  302. + relativeToFile = true;
  303. + }
  304. else if (arg == "--print-rpath") {
  305. printRPath = true;
  306. }
  307. --
  308. 1.9.1