Skip to content

write out replace sections in original order #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -601,20 +601,26 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
}

std::set<unsigned int> noted_phdrs = {};
for (auto & i : replacedSections) {
const std::string & sectionName = i.first;
auto & shdr = findSectionHeader(sectionName);

/* We iterate over the sorted section headers here, so that the relative
position between replaced sections stays the same. */
for (auto & shdr : shdrs) {
std::string sectionName = getSectionName(shdr);
auto i = replacedSections.find(sectionName);
if (i == replacedSections.end())
continue;

Elf_Shdr orig_shdr = shdr;
debug("rewriting section '%s' from offset 0x%x (size %d) to offset 0x%x (size %d)\n",
sectionName.c_str(), rdi(shdr.sh_offset), rdi(shdr.sh_size), curOff, i.second.size());
sectionName.c_str(), rdi(shdr.sh_offset), rdi(shdr.sh_size), curOff, i->second.size());

memcpy(fileContents->data() + curOff, (unsigned char *) i.second.c_str(),
i.second.size());
memcpy(fileContents->data() + curOff, (unsigned char *) i->second.c_str(),
i->second.size());

/* Update the section header for this section. */
wri(shdr.sh_offset, curOff);
wri(shdr.sh_addr, startAddr + (curOff - startOffset));
wri(shdr.sh_size, i.second.size());
wri(shdr.sh_size, i->second.size());
wri(shdr.sh_addralign, sectionAlignment);

/* If this is the .interp section, then the PT_INTERP segment
Expand Down Expand Up @@ -704,7 +710,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
}
}

curOff += roundUp(i.second.size(), sectionAlignment);
curOff += roundUp(i->second.size(), sectionAlignment);
}

replacedSections.clear();
Expand Down