summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-07-23 23:33:34 (GMT)
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-07-23 23:33:34 (GMT)
commit884413a71229866c29a370db3c94b3d93716960c (patch)
tree7b276fdd888777a073afdf3fe46a35a4a225b372
parent719b4aa4c8cc70727e80ec1af34766d4ddcafcea (diff)
parent4e8c47fa84b858bd13770bb1bff4b0072e56e03d (diff)
downloadpatchelf-884413a71229866c29a370db3c94b3d93716960c.zip
patchelf-884413a71229866c29a370db3c94b3d93716960c.tar.gz
patchelf-884413a71229866c29a370db3c94b3d93716960c.tar.bz2
Merge branch 'move-sections' of https://github.com/ttuegel/patchelf
-rw-r--r--src/patchelf.cc21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/patchelf.cc b/src/patchelf.cc
index 8771776..b80f0a7 100644
--- a/src/patchelf.cc
+++ b/src/patchelf.cc
@@ -696,11 +696,22 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
Elf_Addr firstPage = startAddr - startOffset;
debug("first page is 0x%llx\n", (unsigned long long) firstPage);
- /* Right now we assume that the section headers are somewhere near
- the end, which appears to be the case most of the time.
- Therefore they're not accidentally overwritten by the replaced
- sections. !!! Fix this. */
- assert((off_t) rdi(hdr->e_shoff) >= startOffset);
+ if ((off_t) rdi(hdr->e_shoff) < startOffset) {
+ /* The section headers occur too early in the file and would be
+ overwritten by the replaced sections. Move them to the end of the file
+ before proceeding. */
+ off_t shoffNew = fileSize;
+ off_t shSize = rdi(hdr->e_shoff) + rdi(hdr->e_shnum) * rdi(hdr->e_shentsize);
+ growFile (fileSize + shSize);
+ wri(hdr->e_shoff, shoffNew);
+
+ /* Rewrite the section header table. For neatness, keep the
+ sections sorted. */
+ assert(rdi(hdr->e_shnum) == shdrs.size());
+ sortShdrs();
+ for (unsigned int i = 1; i < rdi(hdr->e_shnum); ++i)
+ * ((Elf_Shdr *) (contents + rdi(hdr->e_shoff)) + i) = shdrs[i];
+ }
/* Compute the total space needed for the replaced sections, the