Skip to content

Commit dbcbded

Browse files
committed
Skip NOBITS sections when finding start of executable rewrite
This avoids an assertion failure when 'lastReplaced' is followed by one or mo NOBITS sections, since they won't have a valid startOffset. Fixes: #373
1 parent 81f59ad commit dbcbded

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/patchelf.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -997,9 +997,6 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
997997
Stop when we reach an irreplacable section (such as one of type
998998
SHT_PROGBITS). These cannot be moved in virtual address space
999999
since that would invalidate absolute references to them. */
1000-
assert(lastReplaced + 1 < shdrs.size()); /* !!! I'm lazy. */
1001-
size_t startOffset = rdi(shdrs.at(lastReplaced + 1).sh_offset);
1002-
Elf_Addr startAddr = rdi(shdrs.at(lastReplaced + 1).sh_addr);
10031000
std::string prevSection;
10041001
for (unsigned int i = 1; i <= lastReplaced; ++i) {
10051002
Elf_Shdr & shdr(shdrs.at(i));
@@ -1010,8 +1007,6 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
10101007
if ((rdi(shdr.sh_type) == SHT_PROGBITS && sectionName != ".interp")
10111008
|| prevSection == ".dynstr")
10121009
{
1013-
startOffset = rdi(shdr.sh_offset);
1014-
startAddr = rdi(shdr.sh_addr);
10151010
lastReplaced = i - 1;
10161011
break;
10171012
}
@@ -1022,6 +1017,13 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
10221017
prevSection = std::move(sectionName);
10231018
}
10241019

1020+
while (lastReplaced < shdrs.size() && shdrs.at(lastReplaced).sh_type == SHT_NOBITS)
1021+
++lastReplaced;
1022+
1023+
assert(lastReplaced + 1 < shdrs.size()); /* !!! I'm lazy. */
1024+
size_t startOffset = rdi(shdrs.at(lastReplaced + 1).sh_offset);
1025+
Elf_Addr startAddr = rdi(shdrs.at(lastReplaced + 1).sh_addr);
1026+
10251027
debug("first reserved offset/addr is 0x%x/0x%llx\n",
10261028
startOffset, (unsigned long long) startAddr);
10271029

0 commit comments

Comments
 (0)