diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp index 98bd102d8f4c1..b12a12436db81 100644 --- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp +++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp @@ -511,11 +511,17 @@ void AArch64TargetELFStreamer::finish() { })) { auto *Text = static_cast(Ctx.getObjectFileInfo()->getTextSection()); - for (auto &F : *Text) - if (auto *DF = dyn_cast(&F)) - if (!DF->getContents().empty()) - return; - Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE); + bool Empty = true; + for (auto &F : *Text) { + if (auto *DF = dyn_cast(&F)) { + if (!DF->getContents().empty()) { + Empty = false; + break; + } + } + } + if (Empty) + Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE); } MCSectionELF *MemtagSec = nullptr; diff --git a/llvm/test/MC/AArch64/execute-only-memtag.ll b/llvm/test/MC/AArch64/execute-only-memtag.ll new file mode 100644 index 0000000000000..02daf3179101f --- /dev/null +++ b/llvm/test/MC/AArch64/execute-only-memtag.ll @@ -0,0 +1,18 @@ +; RUN: llc %s -mtriple=aarch64-linux-android31 -filetype=obj -o %t.o +; RUN: llvm-readelf -r %t.o | FileCheck %s + +; CHECK: Relocation section '.rela.memtag.globals.static' at offset {{.*}} contains 1 entries: +; CHECK-NEXT: Type {{.*}} Symbol's Name +; CHECK-NEXT: R_AARCH64_NONE {{.*}} global + +@global = global i32 1, sanitize_memtag + +define void @foo() { + ret void +} + +define void @bar() #0 { + ret void +} + +attributes #0 = { "target-features"="+execute-only" }