Skip to content

Commit 5835f1e

Browse files
[AsmPrinter] Fix crash when remarks section is unsupported (#144724)
Emit a warning and bail out instead of segfault-ing when the current object file format does not have support for emitting a remarks section.
1 parent a5b1093 commit 5835f1e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,15 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
23832383
if (!RS.needsSection())
23842384
return;
23852385

2386+
MCSection *RemarksSection =
2387+
OutContext.getObjectFileInfo()->getRemarksSection();
2388+
if (!RemarksSection) {
2389+
OutContext.reportWarning(SMLoc(), "Current object file format does not "
2390+
"support remarks sections. Use the yaml "
2391+
"remark format instead.");
2392+
return;
2393+
}
2394+
23862395
remarks::RemarkSerializer &RemarkSerializer = RS.getSerializer();
23872396

23882397
std::optional<SmallString<128>> Filename;
@@ -2400,10 +2409,7 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
24002409
MetaSerializer->emit();
24012410

24022411
// Switch to the remarks section.
2403-
MCSection *RemarksSection =
2404-
OutContext.getObjectFileInfo()->getRemarksSection();
24052412
OutStreamer->switchSection(RemarksSection);
2406-
24072413
OutStreamer->emitBinaryData(Buf);
24082414
}
24092415

llvm/test/CodeGen/X86/remarks-section.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=bitstream -remarks-section=false -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-OVERRIDE-BITSTREAM %s
66
; RUN: llc < %s -mtriple=x86_64-darwin --pass-remarks-format=yaml -remarks-section=true -pass-remarks-output=%/t.yaml | FileCheck --check-prefix=CHECK-DARWIN-OVERRIDE-YAML %s
77

8+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu --pass-remarks-format=bitstream -pass-remarks-output=%/t.yaml 2>&1 | FileCheck --check-prefix=CHECK-LINUX-DEFAULT-BITSTREAM %s
9+
810
; CHECK-DARWIN: .section __LLVM,__remarks,regular,debug
911
; CHECK-DARWIN-NEXT: .byte
1012

@@ -22,3 +24,6 @@
2224
define void @func1() {
2325
ret void
2426
}
27+
28+
; Currently no ELF support for bitstream remarks
29+
; CHECK-LINUX-DEFAULT-BITSTREAM: warning: Current object file format does not support remarks sections. Use the yaml remark format instead.

0 commit comments

Comments
 (0)