Skip to content

Commit d5d94ba

Browse files
authored
[BOLT] More refactoring of PHDR handling. NFC (#148932)
Replace ad-hoc adjustment of the program header count with info from the new segment list.
1 parent 47b5917 commit d5d94ba

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4260,31 +4260,25 @@ void RewriteInstance::patchELFPHDRTable() {
42604260
const ELFFile<ELF64LE> &Obj = ELF64LEFile->getELFFile();
42614261
raw_fd_ostream &OS = Out->os();
42624262

4263-
// Write/re-write program headers.
42644263
Phnum = Obj.getHeader().e_phnum;
4265-
if (PHDRTableOffset) {
4266-
// Writing new pheader table and adding one new entry for R+X segment.
4267-
Phnum += 1;
4268-
if (NewWritableSegmentSize) {
4269-
// Adding one more entry for R+W segment.
4270-
Phnum += 1;
4271-
}
4272-
} else {
4264+
4265+
if (BC->NewSegments.empty()) {
4266+
BC->outs() << "BOLT-INFO: not adding new segments\n";
4267+
return;
4268+
}
4269+
4270+
if (opts::UseGnuStack) {
42734271
assert(!PHDRTableAddress && "unexpected address for program header table");
4274-
PHDRTableOffset = Obj.getHeader().e_phoff;
4275-
if (NewWritableSegmentSize) {
4272+
if (BC->NewSegments.size() > 1) {
42764273
BC->errs() << "BOLT-ERROR: unable to add writable segment\n";
42774274
exit(1);
42784275
}
4276+
} else {
4277+
Phnum += BC->NewSegments.size();
42794278
}
42804279

4281-
if (opts::Instrument)
4282-
Phnum += 2;
4283-
4284-
if (BC->NewSegments.empty()) {
4285-
BC->outs() << "BOLT-INFO: not adding new segments\n";
4286-
return;
4287-
}
4280+
if (!PHDRTableOffset)
4281+
PHDRTableOffset = Obj.getHeader().e_phoff;
42884282

42894283
const uint64_t SavedPos = OS.tell();
42904284
OS.seek(PHDRTableOffset);

0 commit comments

Comments
 (0)