diff --git a/src/patchelf.cc b/src/patchelf.cc index 126cada8..1345898f 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -787,24 +787,22 @@ void ElfFile::rewriteSectionsLibrary() /* When normalizing note segments we will in the worst case be adding 1 program header for each SHT_NOTE section. */ unsigned int num_notes = std::count_if(shdrs.begin(), shdrs.end(), - [this](Elf_Shdr shdr) { return rdi(shdr.sh_type) == SHT_NOTE; }); + [this](const Elf_Shdr & shdr) { return rdi(shdr.sh_type) == SHT_NOTE; }); /* Because we're adding a new section header, we're necessarily increasing the size of the program header table. This can cause the first section to overlap the program header table in memory; we need to shift the first few segments to someplace else. */ /* Some sections may already be replaced so account for that */ - unsigned int i = 1; Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + num_notes + 1)*sizeof(Elf_Phdr); - while( i < rdi(hdr()->e_shnum) && rdi(shdrs.at(i).sh_offset) <= pht_size ) { + for (unsigned int i = 1; i < rdi(hdr()->e_shnum) && rdi(shdrs.at(i).sh_offset) <= pht_size; i++) { if (not haveReplacedSection(getSectionName(shdrs.at(i)))) replaceSection(getSectionName(shdrs.at(i)), rdi(shdrs.at(i).sh_size)); - i++; } /* Compute the total space needed for the replaced sections */ off_t neededSpace = 0; - for (auto & s : replacedSections) + for (const auto & s : replacedSections) neededSpace += roundUp(s.second.size(), sectionAlignment); debug("needed space is %d\n", neededSpace); @@ -844,7 +842,17 @@ void ElfFile::rewriteSectionsLibrary() wri(phdr.p_offset, startOffset); wri(phdr.p_vaddr, wri(phdr.p_paddr, startPage)); wri(phdr.p_filesz, wri(phdr.p_memsz, neededSpace)); - wri(phdr.p_flags, PF_R | PF_W); + { + bool need_write = false, need_exec = false; + for (const auto & rs : replacedSections) { + const auto flags = findSectionHeader(rs.first).sh_flags; + if (flags & SHF_WRITE) + need_write = true; + if (flags & SHF_EXECINSTR) + need_exec = true; + } + wri(phdr.p_flags, PF_R | (need_write ? PF_W : 0) | (need_exec ? PF_X : 0)); + } wri(phdr.p_align, getPageSize());