@@ -601,20 +601,26 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
601
601
}
602
602
603
603
std::set<unsigned int > noted_phdrs = {};
604
- for (auto & i : replacedSections) {
605
- const std::string & sectionName = i.first ;
606
- auto & shdr = findSectionHeader (sectionName);
604
+
605
+ /* We iterate over the sorted section headers here, so that the relative
606
+ position between replaced sections stays the same. */
607
+ for (auto & shdr : shdrs) {
608
+ std::string sectionName = getSectionName (shdr);
609
+ auto i = replacedSections.find (sectionName);
610
+ if (i == replacedSections.end ())
611
+ continue ;
612
+
607
613
Elf_Shdr orig_shdr = shdr;
608
614
debug (" rewriting section '%s' from offset 0x%x (size %d) to offset 0x%x (size %d)\n " ,
609
- sectionName.c_str (), rdi (shdr.sh_offset ), rdi (shdr.sh_size ), curOff, i. second .size ());
615
+ sectionName.c_str (), rdi (shdr.sh_offset ), rdi (shdr.sh_size ), curOff, i-> second .size ());
610
616
611
- memcpy (fileContents->data () + curOff, (unsigned char *) i. second .c_str (),
612
- i. second .size ());
617
+ memcpy (fileContents->data () + curOff, (unsigned char *) i-> second .c_str (),
618
+ i-> second .size ());
613
619
614
620
/* Update the section header for this section. */
615
621
wri (shdr.sh_offset , curOff);
616
622
wri (shdr.sh_addr , startAddr + (curOff - startOffset));
617
- wri (shdr.sh_size , i. second .size ());
623
+ wri (shdr.sh_size , i-> second .size ());
618
624
wri (shdr.sh_addralign , sectionAlignment);
619
625
620
626
/* If this is the .interp section, then the PT_INTERP segment
@@ -704,7 +710,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
704
710
}
705
711
}
706
712
707
- curOff += roundUp (i. second .size (), sectionAlignment);
713
+ curOff += roundUp (i-> second .size (), sectionAlignment);
708
714
}
709
715
710
716
replacedSections.clear ();
0 commit comments