From 203c5b9f39cbd9bc4ab3538537b36614af3274a8 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 12 Feb 2015 17:46:49 +0000 Subject: [PATCH] On ELF, put PIC jump tables in a non executable section. Fixes PR22558. llvm-svn: 228939 --- .../llvm/CodeGen/TargetLoweringObjectFileImpl.h | 3 +++ llvm/include/llvm/Target/TargetLoweringObjectFile.h | 3 +++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 17 ++++------------- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 7 +++++++ llvm/lib/Target/TargetLoweringObjectFile.cpp | 18 ++++++++++++++++++ llvm/test/CodeGen/X86/global-sections.ll | 6 ++++++ 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h index 0dc0410..348c634 100644 --- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -60,6 +60,9 @@ public: getSectionForJumpTable(const Function &F, Mangler &Mang, const TargetMachine &TM) const override; + bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, + const Function &F) const override; + /// Return an MCExpr to use for a reference to the specified type info global /// variable from exception handling information. const MCExpr * diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h index ec29283..2c30087 100644 --- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h +++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h @@ -98,6 +98,9 @@ public: getSectionForJumpTable(const Function &F, Mangler &Mang, const TargetMachine &TM) const; + virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, + const Function &F) const; + /// Targets should implement this method to assign a section to globals with /// an explicit section specfied. The implementation of this method can /// assume that GV->hasSection() is true. diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index aacc486..5a01e0e 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1178,23 +1178,14 @@ void AsmPrinter::EmitJumpTableInfo() { // the appropriate section. const Function *F = MF->getFunction(); const TargetLoweringObjectFile &TLOF = getObjFileLowering(); - bool JTInDiffSection = false; - if (// In PIC mode, we need to emit the jump table to the same section as the - // function body itself, otherwise the label differences won't make sense. - // FIXME: Need a better predicate for this: what about custom entries? - MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 || - // We should also do if the section name is NULL or function is declared - // in discardable section - // FIXME: this isn't the right predicate, should be based on the MCSection - // for the function. - F->isWeakForLinker()) { - OutStreamer.SwitchSection(TLOF.SectionForGlobal(F, *Mang, TM)); - } else { + bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection( + MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32, + *F); + if (JTInDiffSection) { // Otherwise, drop it in the readonly section. const MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(*F, *Mang, TM); OutStreamer.SwitchSection(ReadOnlySection); - JTInDiffSection = true; } EmitAlignment(Log2_32( diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 0a49f5f..4f46e37 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -356,6 +356,13 @@ const MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, Group); } +bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( + bool UsesLabelDifference, const Function &F) const { + // We can always create relative relocations, so use another section + // that can be marked non-executable. + return false; +} + /// getSectionForConstant - Given a mergeable constant with the /// specified size and relocation information, return a section that it /// should be placed in. diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp index c098035..faa6fbe 100644 --- a/llvm/lib/Target/TargetLoweringObjectFile.cpp +++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp @@ -275,6 +275,24 @@ const MCSection *TargetLoweringObjectFile::getSectionForJumpTable( return getSectionForConstant(SectionKind::getReadOnly(), /*C=*/nullptr); } +bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection( + bool UsesLabelDifference, const Function &F) const { + // In PIC mode, we need to emit the jump table to the same section as the + // function body itself, otherwise the label differences won't make sense. + // FIXME: Need a better predicate for this: what about custom entries? + if (UsesLabelDifference) + return true; + + // We should also do if the section name is NULL or function is declared + // in discardable section + // FIXME: this isn't the right predicate, should be based on the MCSection + // for the function. + if (F.isWeakForLinker()) + return true; + + return false; +} + /// getSectionForConstant - Given a mergable constant with the /// specified size and relocation information, return a section that it /// should be placed in. diff --git a/llvm/test/CodeGen/X86/global-sections.ll b/llvm/test/CodeGen/X86/global-sections.ll index 4dc0f7d..11b72c1 100644 --- a/llvm/test/CodeGen/X86/global-sections.ll +++ b/llvm/test/CodeGen/X86/global-sections.ll @@ -3,6 +3,7 @@ ; RUN: llc < %s -mtriple=i386-apple-darwin10 -relocation-model=static | FileCheck %s -check-prefix=DARWIN-STATIC ; RUN: llc < %s -mtriple=x86_64-apple-darwin10 | FileCheck %s -check-prefix=DARWIN64 ; RUN: llc < %s -mtriple=i386-unknown-linux-gnu -data-sections -function-sections | FileCheck %s -check-prefix=LINUX-SECTIONS +; RUN: llc < %s -mtriple=x86_64-pc-linux -data-sections -function-sections -relocation-model=pic | FileCheck %s -check-prefix=LINUX-SECTIONS-PIC ; RUN: llc < %s -mtriple=i686-pc-win32 -data-sections -function-sections | FileCheck %s -check-prefix=WIN32-SECTIONS define void @F1() { @@ -41,6 +42,11 @@ bb5: ; LINUX-SECTIONS-NEXT: .cfi_endproc ; LINUX-SECTIONS-NEXT: .section .rodata.F2,"a",@progbits +; LINUX-SECTIONS-PIC: .section .text.F2,"ax",@progbits +; LINUX-SECTIONS-PIC: .size F2, +; LINUX-SECTIONS-PIC-NEXT: .cfi_endproc +; LINUX-SECTIONS-PIC-NEXT: .section .rodata.F2,"a",@progbits + ; int G1; @G1 = common global i32 0 -- 2.7.4