[PATCH] D48792: [ARM] Set execute-only flags in .text.

Ivan Lozano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 29 14:59:36 PDT 2018


ivanlozano created this revision.
ivanlozano added reviewers: srhines, eugenis, peter.smith, echristo.
Herald added a reviewer: javed.absar.
Herald added subscribers: llvm-commits, chrib, kristof.beyls.

The initial .text section generated in object files was missing the SHF_ARM_PURECODE flag when being built with the -mexecute-only flag. All code sections of an ELF must have the flag set for the final .text section to be execute-only, otherwise the flag gets removed.

This change restores some code that performed this from https://reviews.llvm.org/rL289784, though the subtarget information is accessed differently.


Repository:
  rL LLVM

https://reviews.llvm.org/D48792

Files:
  lib/Target/ARM/ARMTargetObjectFile.cpp
  test/MC/ARM/elf-execute-only-section.ll


Index: test/MC/ARM/elf-execute-only-section.ll
===================================================================
--- /dev/null
+++ test/MC/ARM/elf-execute-only-section.ll
@@ -0,0 +1,13 @@
+; RUN: llc < %s -mtriple=thumbv8m.base-eabi -mattr=+execute-only -filetype=obj %s -o - | \
+; RUN: llvm-readelf -s | FileCheck %s
+; RUN: llc < %s -mtriple=thumbv8m.main-eabi -mattr=+execute-only -filetype=obj %s -o - | \
+; RUN: llvm-readelf -s | FileCheck %s
+; RUN: llc < %s -mtriple=thumbv7m-eabi -mattr=+execute-only -filetype=obj %s -o - | \
+; RUN: llvm-readelf -s | FileCheck %s
+
+; CHECK-NOT: {{.text[ ]+PROGBITS[ ]+[0-9]+ [0-9]+ [0-9]+ [0-9]+ AX[^p]}}
+; CHECK: {{.text[ ]+PROGBITS[ ]+[0-9]+ [0-9]+ [0-9]+ [0-9]+ AXp}}
+define void @test_func() {
+entry:
+  ret void
+}
Index: lib/Target/ARM/ARMTargetObjectFile.cpp
===================================================================
--- lib/Target/ARM/ARMTargetObjectFile.cpp
+++ lib/Target/ARM/ARMTargetObjectFile.cpp
@@ -32,14 +32,24 @@
                                         const TargetMachine &TM) {
   const ARMBaseTargetMachine &ARM_TM = static_cast<const ARMBaseTargetMachine &>(TM);
   bool isAAPCS_ABI = ARM_TM.TargetABI == ARMBaseTargetMachine::ARMABI::ARM_ABI_AAPCS;
-  //  genExecuteOnly = ARM_TM.getSubtargetImpl()->genExecuteOnly();
+  bool genExecuteOnly = ARM_TM.getMCSubtargetInfo()->hasFeature(ARM::FeatureExecuteOnly);
 
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
   InitializeELF(isAAPCS_ABI);
 
   if (isAAPCS_ABI) {
     LSDASection = nullptr;
   }
+
+  // Make code section unreadable when in execute-only mode
+  if (genExecuteOnly) {
+    unsigned  Type = ELF::SHT_PROGBITS;
+    unsigned Flags = ELF::SHF_EXECINSTR | ELF::SHF_ALLOC | ELF::SHF_ARM_PURECODE;
+    // Since we cannot modify flags for an existing section, we create a new
+    // section with the right flags, and use 0 as the unique ID for
+    // execute-only text
+    TextSection = Ctx.getELFSection(".text", Type, Flags, 0, "", 0U);
+  }
 }
 
 const MCExpr *ARMElfTargetObjectFile::getTTypeGlobalReference(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48792.153586.patch
Type: text/x-patch
Size: 2067 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180629/638f7553/attachment.bin>


More information about the llvm-commits mailing list