Skip to content

Commit 49bb993

Browse files
authored
[BOLT] Fix build-time assertion in RewriteInstance (#90540)
We use pwrite() in RewriteInstance to update contents of existing sections. pwrite() requires file position to be set past the written offset which we guarantee at the start of rewriteFile(). Then we had an implicit assumption in patchBuildID() that the file position will be set again in patchELFSymTabs() after being reset in patchELFPHDRTable(). That assumption was broken in #90300. The fix is to save and restore file position in patchELFPHDRTable(). Then we don't have to update it again in patchELFSymTabs().
1 parent 7a8d15e commit 49bb993

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,7 @@ void RewriteInstance::patchELFPHDRTable() {
40474047
NewWritableSegmentSize = NextAvailableAddress - NewWritableSegmentAddress;
40484048
}
40494049

4050+
const uint64_t SavedPos = OS.tell();
40504051
OS.seek(PHDRTableOffset);
40514052

40524053
auto createNewTextPhdr = [&]() {
@@ -4151,6 +4152,8 @@ void RewriteInstance::patchELFPHDRTable() {
41514152
<< "BOLT-ERROR: could not find PT_GNU_STACK program header to modify\n";
41524153
exit(1);
41534154
}
4155+
4156+
OS.seek(SavedPos);
41544157
}
41554158

41564159
namespace {
@@ -5041,10 +5044,6 @@ void RewriteInstance::patchELFSymTabs(ELFObjectFile<ELFT> *File) {
50415044
assert((DynSymSection || BC->IsStaticExecutable) &&
50425045
"dynamic symbol table expected");
50435046
if (DynSymSection) {
5044-
// Set pointer to the end of the section, so we can use pwrite to update
5045-
// the dynamic symbol table.
5046-
Out->os().seek(DynSymSection->sh_offset + DynSymSection->sh_size);
5047-
50485047
updateELFSymbolTable(
50495048
File,
50505049
/*IsDynSym=*/true,

0 commit comments

Comments
 (0)