From c09a356552c87daf27e97f054b9ef293afa8647a Mon Sep 17 00:00:00 2001 From: Changli Gao Date: Thu, 23 Oct 2014 01:41:47 +0800 Subject: Add option --no-default-lib Marks the object that the search for dependencies of this object will ignore any default library search paths. --- patchelf.1 | 4 ++++ src/patchelf.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/patchelf.1 b/patchelf.1 index 34f9461..5976e3c 100644 --- a/patchelf.1 +++ b/patchelf.1 @@ -51,6 +51,10 @@ DT_RUNPATH. By default DT_RPATH is converted to DT_RUNPATH. Removes a declared depency on LIBRARY (DT_NEEDED entry). This option can be given multiple times. +.IP "--no-default-lib" +Marks the object that the search for dependencies of this object will ignore any +default library search paths. + .IP --debug Prints details of the changes made to the input file. diff --git a/src/patchelf.cc b/src/patchelf.cc index 731b9ad..eca80a6 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -170,6 +170,8 @@ public: void replaceNeeded(map& libs); + void noDefaultLib(); + private: /* Convert an integer in big or little endian representation (as @@ -1289,6 +1291,46 @@ void ElfFile::addNeeded(set libs) } +template +void ElfFile::noDefaultLib() +{ + Elf_Shdr & shdrDynamic = findSection(".dynamic"); + + Elf_Dyn * dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset)); + Elf_Dyn * dynFlags1 = 0; + for ( ; rdi(dyn->d_tag) != DT_NULL; dyn++) { + if (rdi(dyn->d_tag) == DT_FLAGS_1) { + dynFlags1 = dyn; + break; + } + } + if (dynFlags1) { + if (dynFlags1->d_un.d_val & DF_1_NODEFLIB) + return; + dynFlags1->d_un.d_val |= DF_1_NODEFLIB; + } else { + string & newDynamic = replaceSection(".dynamic", + rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn)); + + unsigned int idx = 0; + for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ; + debug("DT_NULL index is %d\n", idx); + + /* Shift all entries down by one. */ + setSubstr(newDynamic, sizeof(Elf_Dyn), + string(newDynamic, 0, sizeof(Elf_Dyn) * (idx + 1))); + + /* Add the DT_FLAGS_1 entry at the top. */ + Elf_Dyn newDyn; + wri(newDyn.d_tag, DT_FLAGS_1); + newDyn.d_un.d_val = DF_1_NODEFLIB; + setSubstr(newDynamic, 0, string((char *) &newDyn, sizeof(Elf_Dyn))); + } + + changed = true; +} + + static bool printInterpreter = false; static bool printSoname = false; static bool setSoname = false; @@ -1302,6 +1344,7 @@ static string newRPath; static set neededLibsToRemove; static map neededLibsToReplace; static set neededLibsToAdd; +static bool noDefaultLib = false; template static void patchElf2(ElfFile & elfFile, mode_t fileMode) @@ -1332,6 +1375,9 @@ static void patchElf2(ElfFile & elfFile, mode_t fileMode) elfFile.replaceNeeded(neededLibsToReplace); elfFile.addNeeded(neededLibsToAdd); + if (noDefaultLib) + elfFile.noDefaultLib(); + if (elfFile.isChanged()){ elfFile.rewriteSections(); writeFile(fileName, fileMode); @@ -1387,6 +1433,7 @@ void showHelp(const string & progName) [--add-needed LIBRARY]\n\ [--remove-needed LIBRARY]\n\ [--replace-needed LIBRARY NEW_LIBRARY]\n\ + [--no-default-lib]\n\ [--debug]\n\ [--version]\n\ FILENAME\n", progName.c_str()); @@ -1461,6 +1508,9 @@ int main(int argc, char * * argv) else if (arg == "--debug") { debugMode = true; } + else if (arg == "--no-default-lib") { + noDefaultLib = true; + } else if (arg == "--help" || arg == "-h" ) { showHelp(argv[0]); return 0; -- cgit v0.12