-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[BOLT] Refactor patchELFPHDRTable() #90290
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mostly NFC accept for one assertion that was converted into an error.
maksfb
requested review from
aaupov,
rafaelauler,
ayermolo and
dcci
as code owners
April 26, 2024 23:02
@llvm/pr-subscribers-bolt Author: Maksim Panchenko (maksfb) ChangesMostly NFC accept for one assertion that was converted into an error. Full diff: https://github.com/llvm/llvm-project/pull/90290.diff 1 Files Affected:
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 409835537fdeee..065260936e70a5 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -3926,11 +3926,6 @@ void RewriteInstance::patchELFPHDRTable() {
OS.seek(PHDRTableOffset);
- bool ModdedGnuStack = false;
- (void)ModdedGnuStack;
- bool AddedSegment = false;
- (void)AddedSegment;
-
auto createNewTextPhdr = [&]() {
ELF64LEPhdrTy NewPhdr;
NewPhdr.p_type = ELF::PT_LOAD;
@@ -3946,38 +3941,51 @@ void RewriteInstance::patchELFPHDRTable() {
NewPhdr.p_filesz = NewTextSegmentSize;
NewPhdr.p_memsz = NewTextSegmentSize;
NewPhdr.p_flags = ELF::PF_X | ELF::PF_R;
- // FIXME: Currently instrumentation is experimental and the runtime data
- // is emitted with code, thus everything needs to be writable
- if (opts::Instrument)
+ if (opts::Instrument) {
+ // FIXME: Currently instrumentation is experimental and the runtime data
+ // is emitted with code, thus everything needs to be writable.
NewPhdr.p_flags |= ELF::PF_W;
+ }
NewPhdr.p_align = BC->PageAlign;
return NewPhdr;
};
- auto createNewWritableSectionsPhdr = [&]() {
- ELF64LEPhdrTy NewPhdr;
- NewPhdr.p_type = ELF::PT_LOAD;
- NewPhdr.p_offset = getFileOffsetForAddress(NewWritableSegmentAddress);
- NewPhdr.p_vaddr = NewWritableSegmentAddress;
- NewPhdr.p_paddr = NewWritableSegmentAddress;
- NewPhdr.p_filesz = NewWritableSegmentSize;
- NewPhdr.p_memsz = NewWritableSegmentSize;
- NewPhdr.p_align = BC->RegularPageSize;
- NewPhdr.p_flags = ELF::PF_R | ELF::PF_W;
- return NewPhdr;
+ auto writeNewSegmentPhdrs = [&]() {
+ ELF64LE::Phdr NewTextPhdr = createNewTextPhdr();
+ OS.write(reinterpret_cast<const char *>(&NewTextPhdr), sizeof(NewTextPhdr));
+
+ if (NewWritableSegmentSize) {
+ ELF64LEPhdrTy NewPhdr;
+ NewPhdr.p_type = ELF::PT_LOAD;
+ NewPhdr.p_offset = getFileOffsetForAddress(NewWritableSegmentAddress);
+ NewPhdr.p_vaddr = NewWritableSegmentAddress;
+ NewPhdr.p_paddr = NewWritableSegmentAddress;
+ NewPhdr.p_filesz = NewWritableSegmentSize;
+ NewPhdr.p_memsz = NewWritableSegmentSize;
+ NewPhdr.p_align = BC->RegularPageSize;
+ NewPhdr.p_flags = ELF::PF_R | ELF::PF_W;
+ OS.write(reinterpret_cast<const char *>(&NewPhdr), sizeof(NewPhdr));
+ }
};
+ bool ModdedGnuStack = false;
+ bool AddedSegment = false;
+
// Copy existing program headers with modifications.
for (const ELF64LE::Phdr &Phdr : cantFail(Obj.program_headers())) {
ELF64LE::Phdr NewPhdr = Phdr;
- if (PHDRTableAddress && Phdr.p_type == ELF::PT_PHDR) {
- NewPhdr.p_offset = PHDRTableOffset;
- NewPhdr.p_vaddr = PHDRTableAddress;
- NewPhdr.p_paddr = PHDRTableAddress;
- NewPhdr.p_filesz = sizeof(NewPhdr) * Phnum;
- NewPhdr.p_memsz = sizeof(NewPhdr) * Phnum;
- } else if (Phdr.p_type == ELF::PT_GNU_EH_FRAME) {
+ switch (Phdr.p_type) {
+ case ELF::PT_PHDR:
+ if (PHDRTableAddress) {
+ NewPhdr.p_offset = PHDRTableOffset;
+ NewPhdr.p_vaddr = PHDRTableAddress;
+ NewPhdr.p_paddr = PHDRTableAddress;
+ NewPhdr.p_filesz = sizeof(NewPhdr) * Phnum;
+ NewPhdr.p_memsz = sizeof(NewPhdr) * Phnum;
+ }
+ break;
+ case ELF::PT_GNU_EH_FRAME: {
ErrorOr<BinarySection &> EHFrameHdrSec = BC->getUniqueSectionByName(
getNewSecPrefix() + getEHFrameHdrSectionName());
if (EHFrameHdrSec && EHFrameHdrSec->isAllocatable() &&
@@ -3988,37 +3996,36 @@ void RewriteInstance::patchELFPHDRTable() {
NewPhdr.p_filesz = EHFrameHdrSec->getOutputSize();
NewPhdr.p_memsz = EHFrameHdrSec->getOutputSize();
}
- } else if (opts::UseGnuStack && Phdr.p_type == ELF::PT_GNU_STACK) {
- NewPhdr = createNewTextPhdr();
- ModdedGnuStack = true;
- } else if (!opts::UseGnuStack && Phdr.p_type == ELF::PT_DYNAMIC) {
- // Insert the new header before DYNAMIC.
- ELF64LE::Phdr NewTextPhdr = createNewTextPhdr();
- OS.write(reinterpret_cast<const char *>(&NewTextPhdr),
- sizeof(NewTextPhdr));
- if (NewWritableSegmentSize) {
- ELF64LEPhdrTy NewWritablePhdr = createNewWritableSectionsPhdr();
- OS.write(reinterpret_cast<const char *>(&NewWritablePhdr),
- sizeof(NewWritablePhdr));
+ break;
+ }
+ case ELF::PT_GNU_STACK:
+ if (opts::UseGnuStack) {
+ // Overwrite the header with the new text segment header.
+ NewPhdr = createNewTextPhdr();
+ ModdedGnuStack = true;
+ }
+ break;
+ case ELF::PT_DYNAMIC:
+ if (!opts::UseGnuStack) {
+ // Insert new headers before DYNAMIC.
+ writeNewSegmentPhdrs();
+ AddedSegment = true;
}
- AddedSegment = true;
+ break;
}
OS.write(reinterpret_cast<const char *>(&NewPhdr), sizeof(NewPhdr));
}
if (!opts::UseGnuStack && !AddedSegment) {
- // Append the new header to the end of the table.
- ELF64LE::Phdr NewTextPhdr = createNewTextPhdr();
- OS.write(reinterpret_cast<const char *>(&NewTextPhdr), sizeof(NewTextPhdr));
- if (NewWritableSegmentSize) {
- ELF64LEPhdrTy NewWritablePhdr = createNewWritableSectionsPhdr();
- OS.write(reinterpret_cast<const char *>(&NewWritablePhdr),
- sizeof(NewWritablePhdr));
- }
+ // Append new headers to the end of the table.
+ writeNewSegmentPhdrs();
}
- assert((!opts::UseGnuStack || ModdedGnuStack) &&
- "could not find GNU_STACK program header to modify");
+ if (opts::UseGnuStack && !ModdedGnuStack) {
+ BC->errs()
+ << "BOLT-ERROR: could not find PT_GNU_STACK program header to modify\n";
+ exit(1);
+ }
}
namespace {
|
aaupov
approved these changes
Apr 26, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mostly NFC accept for one assertion that was converted into an error.