-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[BOLT] Overwrite .eh_frame_hdr in-place #116730
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
Merged
Merged
Conversation
This file contains hidden or 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
If the new EH frame header can fit into the original .eh_frame_hdr section, overwrite it in-place and pad with zeroes.
Member
|
@llvm/pr-subscribers-bolt Author: Maksim Panchenko (maksfb) ChangesIf the new EH frame header can fit into the original .eh_frame_hdr section, overwrite it in-place and pad with zeroes. Full diff: https://github.com/llvm/llvm-project/pull/116730.diff 2 Files Affected:
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 1fcf2bb959bbbb..40769944e3876b 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -5791,42 +5791,64 @@ void RewriteInstance::writeEHFrameHeader() {
LLVM_DEBUG(dbgs() << "BOLT: writing a new " << getEHFrameHdrSectionName()
<< '\n');
- NextAvailableAddress =
- appendPadding(Out->os(), NextAvailableAddress, EHFrameHdrAlign);
+ // Try to overwrite the original .eh_frame_hdr if the size permits.
+ uint64_t EHFrameHdrOutputAddress = 0;
+ uint64_t EHFrameHdrFileOffset = 0;
+ std::vector<char> NewEHFrameHdr;
+ BinarySection *OldEHFrameHdrSection = getSection(getEHFrameHdrSectionName());
+ if (OldEHFrameHdrSection) {
+ NewEHFrameHdr = CFIRdWrt->generateEHFrameHeader(
+ RelocatedEHFrame, NewEHFrame, OldEHFrameHdrSection->getAddress());
+ if (NewEHFrameHdr.size() <= OldEHFrameHdrSection->getSize()) {
+ BC->outs() << "BOLT-INFO: rewriting " << getEHFrameHdrSectionName()
+ << " in-place\n";
+ EHFrameHdrOutputAddress = OldEHFrameHdrSection->getAddress();
+ EHFrameHdrFileOffset = OldEHFrameHdrSection->getInputFileOffset();
+ } else {
+ OldEHFrameHdrSection->setOutputName(getOrgSecPrefix() +
+ getEHFrameHdrSectionName());
+ OldEHFrameHdrSection = nullptr;
+ }
+ }
- const uint64_t EHFrameHdrOutputAddress = NextAvailableAddress;
- const uint64_t EHFrameHdrFileOffset =
- getFileOffsetForAddress(NextAvailableAddress);
+ // If there was not enough space, allocate more memory for .eh_frame_hdr.
+ if (!OldEHFrameHdrSection) {
+ NextAvailableAddress =
+ appendPadding(Out->os(), NextAvailableAddress, EHFrameHdrAlign);
- std::vector<char> NewEHFrameHdr = CFIRdWrt->generateEHFrameHeader(
- RelocatedEHFrame, NewEHFrame, EHFrameHdrOutputAddress);
+ EHFrameHdrOutputAddress = NextAvailableAddress;
+ EHFrameHdrFileOffset = getFileOffsetForAddress(NextAvailableAddress);
+
+ NewEHFrameHdr = CFIRdWrt->generateEHFrameHeader(
+ RelocatedEHFrame, NewEHFrame, EHFrameHdrOutputAddress);
+
+ NextAvailableAddress += NewEHFrameHdr.size();
+ if (!BC->BOLTReserved.empty() &&
+ (NextAvailableAddress > BC->BOLTReserved.end())) {
+ BC->errs() << "BOLT-ERROR: unable to fit " << getEHFrameHdrSectionName()
+ << " into reserved space\n";
+ exit(1);
+ }
+
+ // Create a new entry in the section header table.
+ const unsigned Flags = BinarySection::getFlags(/*IsReadOnly=*/true,
+ /*IsText=*/false,
+ /*IsAllocatable=*/true);
+ BinarySection &EHFrameHdrSec = BC->registerOrUpdateSection(
+ getNewSecPrefix() + getEHFrameHdrSectionName(), ELF::SHT_PROGBITS,
+ Flags, nullptr, NewEHFrameHdr.size(), /*Alignment=*/1);
+ EHFrameHdrSec.setOutputFileOffset(EHFrameHdrFileOffset);
+ EHFrameHdrSec.setOutputAddress(EHFrameHdrOutputAddress);
+ EHFrameHdrSec.setOutputName(getEHFrameHdrSectionName());
+ }
Out->os().seek(EHFrameHdrFileOffset);
Out->os().write(NewEHFrameHdr.data(), NewEHFrameHdr.size());
- const unsigned Flags = BinarySection::getFlags(/*IsReadOnly=*/true,
- /*IsText=*/false,
- /*IsAllocatable=*/true);
- BinarySection *OldEHFrameHdrSection = getSection(getEHFrameHdrSectionName());
+ // Pad the contents if overwriting in-place.
if (OldEHFrameHdrSection)
- OldEHFrameHdrSection->setOutputName(getOrgSecPrefix() +
- getEHFrameHdrSectionName());
-
- BinarySection &EHFrameHdrSec = BC->registerOrUpdateSection(
- getNewSecPrefix() + getEHFrameHdrSectionName(), ELF::SHT_PROGBITS, Flags,
- nullptr, NewEHFrameHdr.size(), /*Alignment=*/1);
- EHFrameHdrSec.setOutputFileOffset(EHFrameHdrFileOffset);
- EHFrameHdrSec.setOutputAddress(EHFrameHdrOutputAddress);
- EHFrameHdrSec.setOutputName(getEHFrameHdrSectionName());
-
- NextAvailableAddress += EHFrameHdrSec.getOutputSize();
-
- if (!BC->BOLTReserved.empty() &&
- (NextAvailableAddress > BC->BOLTReserved.end())) {
- BC->errs() << "BOLT-ERROR: unable to fit " << getEHFrameHdrSectionName()
- << " into reserved space\n";
- exit(1);
- }
+ Out->os().write_zeros(OldEHFrameHdrSection->getSize() -
+ NewEHFrameHdr.size());
// Merge new .eh_frame with the relocated original so that gdb can locate all
// FDEs.
diff --git a/bolt/test/eh-frame-hdr.test b/bolt/test/eh-frame-hdr.test
new file mode 100644
index 00000000000000..4d718c850e2f28
--- /dev/null
+++ b/bolt/test/eh-frame-hdr.test
@@ -0,0 +1,12 @@
+# Check that llvm-bolt overwrites .eh_frame_hdr in-place.
+
+REQUIRES: system-linux
+
+RUN: %clang %cflags %p/Inputs/hello.c -o %t -Wl,-q
+RUN: llvm-bolt %t -o %t.bolt --use-old-text \
+RUN: | FileCheck %s --check-prefix=CHECK-BOLT
+RUN: llvm-readelf -WS %t.bolt | FileCheck %s
+
+CHECK-BOLT: rewriting .eh_frame_hdr in-place
+
+CHECK-NOT: .bolt.org.eh_frame_hdr
|
dcci
approved these changes
Nov 19, 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.
If the new EH frame header can fit into the original .eh_frame_hdr section, overwrite it in-place and pad with zeroes.