(original) (raw)

diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp index 434ae32502d48..b662e75741d38 100644 --- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp +++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp @@ -16,6 +16,7 @@ #include "llvm/IR/Module.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCValue.h" using namespace llvm; @@ -27,6 +28,14 @@ void AArch64_ELFTargetObjectFile::Initialize(MCContext &Ctx, // AARCH64 ELF ABI does not define static relocation type for TLS offset // within a module. Do not generate AT_location for TLS variables. SupportDebugThreadLocalLocation = false; + + // Make sure the implicitly created empty .text section has the + // SHF_AARCH64_PURECODE flag set if the "+execute-only" target feature is + // present. + if (TM.getMCSubtargetInfo()->hasFeature(AArch64::FeatureExecuteOnly)) { + auto *Text = cast(TextSection); + Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE); + } } void AArch64_ELFTargetObjectFile::emitPersonalityValueImpl( diff --git a/llvm/test/CodeGen/AArch64/execute-only-empty.ll b/llvm/test/CodeGen/AArch64/execute-only-empty.ll new file mode 100644 index 0000000000000..cdf9b3afe8daf --- /dev/null +++ b/llvm/test/CodeGen/AArch64/execute-only-empty.ll @@ -0,0 +1,13 @@ +; RUN: llc -filetype=obj -mtriple=aarch64 -mattr=+execute-only %s -o %t.o +; RUN: llvm-readobj -S %t.o | FileCheck %s + +; CHECK: Name: .text +; CHECK-NEXT: Type: SHT_PROGBITS +; CHECK-NEXT: Flags [ +; CHECK-NEXT: SHF_AARCH64_PURECODE +; CHECK-NEXT: SHF_ALLOC +; CHECK-NEXT: SHF_EXECINSTR +; CHECK-NEXT: ] +; CHECK-NEXT: Address: +; CHECK-NEXT: Offset: +; CHECK-NEXT: Size: 0