@@ -380,8 +380,8 @@ static void checkPointer(const FileContents & contents, void * p, unsigned int s
380380
381381
382382template <ElfFileParams>
383- ElfFile<ElfFileParamNames>::ElfFile(FileContents fileContents )
384- : fileContents(fileContents )
383+ ElfFile<ElfFileParamNames>::ElfFile(FileContents fContents )
384+ : fileContents(fContents )
385385 , contents(fileContents->data ())
386386{
387387 /* Check the ELF header for basic validity. */
@@ -673,15 +673,15 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
673673 *before* writing the new section contents (below) to prevent
674674 clobbering previously written new section contents. */
675675 for (auto & i : replacedSections) {
676- std::string sectionName = i.first ;
676+ const std::string & sectionName = i.first ;
677677 Elf_Shdr & shdr = findSection (sectionName);
678678 if (rdi (shdr.sh_type ) != SHT_NOBITS)
679679 memset (contents + rdi (shdr.sh_offset ), ' X' , rdi (shdr.sh_size ));
680680 }
681681
682682 std::set<unsigned int > noted_phdrs = {};
683683 for (auto & i : replacedSections) {
684- std::string sectionName = i.first ;
684+ const std::string & sectionName = i.first ;
685685 auto & shdr = findSection (sectionName);
686686 Elf_Shdr orig_shdr = shdr;
687687 debug (" rewriting section '%s' from offset 0x%x (size %d) to offset 0x%x (size %d)\n " ,
@@ -710,7 +710,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
710710
711711 /* If this is the .dynamic section, then the PT_DYNAMIC segment
712712 must be sync'ed with it. */
713- if (sectionName == " .dynamic" ) {
713+ else if (sectionName == " .dynamic" ) {
714714 for (auto & phdr : phdrs) {
715715 if (rdi (phdr.p_type ) == PT_DYNAMIC) {
716716 phdr.p_offset = shdr.sh_offset ;
@@ -802,11 +802,11 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
802802
803803 /* Compute the total space needed for the replaced sections */
804804 off_t neededSpace = 0 ;
805- for (auto & i : replacedSections)
806- neededSpace += roundUp (i .second .size (), sectionAlignment);
805+ for (auto & s : replacedSections)
806+ neededSpace += roundUp (s .second .size (), sectionAlignment);
807807 debug (" needed space is %d\n " , neededSpace);
808808
809- size_t startOffset = roundUp (fileContents->size (), getPageSize ());
809+ Elf_Off startOffset = roundUp (fileContents->size (), getPageSize ());
810810
811811 growFile (fileContents, startOffset + neededSpace);
812812
@@ -903,7 +903,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
903903 debug (" replacing section '%s' which is in the way\n " , sectionName.c_str ());
904904 replaceSection (sectionName, rdi (shdr.sh_size ));
905905 }
906- prevSection = sectionName;
906+ prevSection = std::move ( sectionName) ;
907907 }
908908
909909 debug (" first reserved offset/addr is 0x%x/0x%llx\n " ,
@@ -1003,8 +1003,9 @@ void ElfFile<ElfFileParamNames>::normalizeNoteSegments()
10031003 size_t size = 0 ;
10041004 for (const auto & shdr : shdrs) {
10051005 if (rdi (shdr.sh_type ) != SHT_NOTE) continue ;
1006- if (rdi (shdr.sh_offset ) != curr_off) continue ;
1006+ if (rdi (shdr.sh_offset ) != roundUp ( curr_off, rdi (shdr. sh_addralign )) ) continue ;
10071007 size = rdi (shdr.sh_size );
1008+ curr_off = roundUp (curr_off, rdi (shdr.sh_addralign ));
10081009 break ;
10091010 }
10101011 if (size == 0 )
@@ -1146,7 +1147,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11461147 fprintf (stderr, " warning: entry %d in symbol table refers to a non-existent section, skipping\n " , shndx);
11471148 continue ;
11481149 }
1149- std::string section = sectionsByOldIndex.at (shndx);
1150+ const std::string & section = sectionsByOldIndex.at (shndx);
11501151 assert (!section.empty ());
11511152 auto newIndex = findSection3 (section); // inefficient
11521153 // debug("rewriting symbol %d: index = %d (%s) -> %d\n", entry, shndx, section.c_str(), newIndex);
@@ -1201,7 +1202,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
12011202
12021203 if (op == printSoname) {
12031204 if (soname) {
1204- if (std::string (soname ? soname : " " ). empty () )
1205+ if (strlen (soname) == 0 )
12051206 debug (" DT_SONAME is empty\n " );
12061207 else
12071208 printf (" %s\n " , soname);
@@ -1211,7 +1212,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
12111212 return ;
12121213 }
12131214
1214- if (std::string ( soname ? soname : " " ) == newSoname) {
1215+ if (soname && soname == newSoname) {
12151216 debug (" current and proposed new SONAMEs are equal keeping DT_SONAME entry\n " );
12161217 return ;
12171218 }
@@ -1372,7 +1373,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
13721373 return ;
13731374 }
13741375
1375- auto dyn = (Elf_Dyn *)(contents + rdi (shdrDynamic.sh_offset ));
1376+ dyn = (Elf_Dyn *)(contents + rdi (shdrDynamic.sh_offset ));
13761377 Elf_Dyn * last = dyn;
13771378 for ( ; rdi (dyn->d_tag ) != DT_NULL; dyn++) {
13781379 if (rdi (dyn->d_tag ) == DT_RPATH) {
@@ -1402,7 +1403,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
14021403 changed = true ;
14031404 }
14041405
1405- if (std::string ( rpath ? rpath : " " ) == newRPath) {
1406+ if (rpath && rpath == newRPath) {
14061407 return ;
14071408 }
14081409
@@ -1507,7 +1508,7 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
15071508 if (rdi (dyn->d_tag ) == DT_NEEDED) {
15081509 char * name = strTab + rdi (dyn->d_un .d_val );
15091510 auto i = libs.find (name);
1510- if (i != libs.end ()) {
1511+ if (i != libs.end () && name != i-> second ) {
15111512 auto replacement = i->second ;
15121513
15131514 debug (" replacing DT_NEEDED entry '%s' with '%s'\n " , name, replacement.c_str ());
@@ -1559,7 +1560,7 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
15591560 while (verNeedNum > 0 ) {
15601561 char * file = verStrTab + rdi (need->vn_file );
15611562 auto i = libs.find (file);
1562- if (i != libs.end ()) {
1563+ if (i != libs.end () && file != i-> second ) {
15631564 auto replacement = i->second ;
15641565
15651566 debug (" replacing .gnu.version_r entry '%s' with '%s'\n " , file, replacement.c_str ());
@@ -1592,12 +1593,15 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
15921593 auto shdrDynamic = findSection (" .dynamic" );
15931594 auto shdrDynStr = findSection (" .dynstr" );
15941595
1596+ unsigned int length = 0 ;
1597+
15951598 /* add all new libs to the dynstr string table */
1596- unsigned int length = std::count_if (libs. begin (), libs. end (),
1597- []( const std::string & lib) { return lib .size () + 1 ; }) ;
1599+ for ( auto &lib : libs)
1600+ length += lib.size () + 1 ;
15981601
15991602 std::string & newDynStr = replaceSection (" .dynstr" ,
16001603 rdi (shdrDynStr.sh_size ) + length + 1 );
1604+
16011605 std::set<Elf64_Xword> libStrings;
16021606 unsigned int pos = 0 ;
16031607 for (auto & i : libs) {
@@ -1735,7 +1739,7 @@ static bool printNeeded = false;
17351739static bool noDefaultLib = false ;
17361740
17371741template <class ElfFile >
1738- static void patchElf2 (ElfFile && elfFile, const FileContents & fileContents, std::string fileName)
1742+ static void patchElf2 (ElfFile && elfFile, const FileContents & fileContents, const std::string & fileName)
17391743{
17401744 if (printInterpreter)
17411745 printf (" %s\n " , elfFile.getInterpreter ().c_str ());
@@ -1786,7 +1790,7 @@ static void patchElf()
17861790 debug (" patching ELF file '%s'\n " , fileName.c_str ());
17871791
17881792 auto fileContents = readFile (fileName);
1789- std::string outputFileName2 = outputFileName.empty () ? fileName : outputFileName;
1793+ const std::string & outputFileName2 = outputFileName.empty () ? fileName : outputFileName;
17901794
17911795 if (getElfType (fileContents).is32Bit )
17921796 patchElf2 (ElfFile<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Addr, Elf32_Off, Elf32_Dyn, Elf32_Sym, Elf32_Verneed, Elf32_Versym>(fileContents), fileContents, outputFileName2);
0 commit comments