diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index dc7591a1d1226..934768c244b31 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -4190,6 +4190,11 @@ void RewriteInstance::patchELFPHDRTable() { NewWritableSegmentSize = NextAvailableAddress - NewWritableSegmentAddress; } + if (!NewTextSegmentSize && !NewWritableSegmentSize) { + BC->outs() << "BOLT-INFO: not adding new segments\n"; + return; + } + const uint64_t SavedPos = OS.tell(); OS.seek(PHDRTableOffset); diff --git a/bolt/test/program-header.test b/bolt/test/program-header.test new file mode 100644 index 0000000000000..4552303ea5af2 --- /dev/null +++ b/bolt/test/program-header.test @@ -0,0 +1,14 @@ +# Check that llvm-bolt does not add new segments when writing code in-place. + +REQUIRES: system-linux + +RUN: %clang %cflags %p/Inputs/hello.c -o %t -no-pie -Wl,-q +RUN: llvm-bolt %t -o %t.bolt --use-old-text --align-functions=1 \ +RUN: --no-huge-pages --align-text=1 --use-gnu-stack \ +RUN: | FileCheck %s --check-prefix=CHECK-BOLT +RUN: llvm-readelf -WS %t.bolt | FileCheck %s + +CHECK-BOLT: rewriting .eh_frame_hdr in-place +CHECK-BOLT: not adding new segments + +CHECK-NOT: .bolt.org.eh_frame_hdr