r375022 - Tag CFI-generated data structures with "#pragma clang section" attributes.
Dmitry Mikulin via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 16 10:51:40 PDT 2019
Author: dmikulin
Date: Wed Oct 16 10:51:40 2019
New Revision: 375022
URL: http://llvm.org/viewvc/llvm-project?rev=375022&view=rev
Log:
Tag CFI-generated data structures with "#pragma clang section" attributes.
Differential Revision: https://reviews.llvm.org/D68808
Added:
cfe/trunk/test/CodeGen/cfi-pragma-section.c
Modified:
cfe/trunk/lib/CodeGen/CGExpr.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.h
cfe/trunk/lib/Sema/SemaDecl.cpp
Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=375022&r1=375021&r2=375022&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Wed Oct 16 10:51:40 2019
@@ -2821,6 +2821,7 @@ llvm::Constant *CodeGenFunction::EmitChe
CGM.getModule(), Descriptor->getType(),
/*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
+ CGM.setPragmaSectionAttributes(CurFuncDecl, GV);
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
// Remember the descriptor for this type.
@@ -2900,6 +2901,7 @@ llvm::Constant *CodeGenFunction::EmitChe
}
auto FilenameGV = CGM.GetAddrOfConstantCString(FilenameString, ".src");
+ CGM.setPragmaSectionAttributes(CurFuncDecl, cast<llvm::GlobalVariable>(FilenameGV.getPointer()));
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(
cast<llvm::GlobalVariable>(FilenameGV.getPointer()));
Filename = FilenameGV.getPointer();
@@ -3073,6 +3075,7 @@ void CodeGenFunction::EmitCheck(
new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
llvm::GlobalVariable::PrivateLinkage, Info);
InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
+ CGM.setPragmaSectionAttributes(CurFuncDecl, InfoPtr);
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy));
ArgTypes.push_back(Int8PtrTy);
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=375022&r1=375021&r2=375022&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Wed Oct 16 10:51:40 2019
@@ -1699,11 +1699,8 @@ bool CodeGenModule::GetCPUAndFeaturesAtt
return AddedAttr;
}
-void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
- llvm::GlobalObject *GO) {
- const Decl *D = GD.getDecl();
- SetCommonAttributes(GD, GO);
-
+void CodeGenModule::setPragmaSectionAttributes(const Decl *D,
+ llvm::GlobalObject *GO) {
if (D) {
if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
@@ -1721,6 +1718,26 @@ void CodeGenModule::setNonAliasAttribute
if (!D->getAttr<SectionAttr>())
F->addFnAttr("implicit-section-name", SA->getName());
+ if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
+ F->addFnAttr("bss-section", SA->getName());
+ if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
+ F->addFnAttr("data-section", SA->getName());
+ if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
+ F->addFnAttr("rodata-section", SA->getName());
+ if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
+ F->addFnAttr("relro-section", SA->getName());
+ }
+ }
+}
+
+void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
+ llvm::GlobalObject *GO) {
+ const Decl *D = GD.getDecl();
+ SetCommonAttributes(GD, GO);
+ setPragmaSectionAttributes(D, GO);
+
+ if (D) {
+ if (auto *F = dyn_cast<llvm::Function>(GO)) {
llvm::AttrBuilder Attrs;
if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
// We know that GetCPUAndFeaturesAttributes will always have the
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=375022&r1=375021&r2=375022&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Wed Oct 16 10:51:40 2019
@@ -1347,6 +1347,11 @@ public:
/// \param QT is the clang QualType of the null pointer.
llvm::Constant *getNullPointer(llvm::PointerType *T, QualType QT);
+ /// Set section attributes requested by "#pragma clang section"
+ /// \param D is the declaration to read semantic attributes from.
+ /// \param GO is the global object to set section attributes.
+ void setPragmaSectionAttributes(const Decl *D, llvm::GlobalObject *GO);
+
private:
llvm::Constant *GetOrCreateLLVMFunction(
StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=375022&r1=375021&r2=375022&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Oct 16 10:51:40 2019
@@ -9062,6 +9062,25 @@ Sema::ActOnFunctionDeclarator(Scope *S,
Context, PragmaClangTextSection.SectionName,
PragmaClangTextSection.PragmaLocation, AttributeCommonInfo::AS_Pragma));
+ if (D.isFunctionDefinition()) {
+ if (PragmaClangBSSSection.Valid)
+ NewFD->addAttr(PragmaClangBSSSectionAttr::CreateImplicit(
+ Context, PragmaClangBSSSection.SectionName,
+ PragmaClangBSSSection.PragmaLocation));
+ if (PragmaClangDataSection.Valid)
+ NewFD->addAttr(PragmaClangDataSectionAttr::CreateImplicit(
+ Context, PragmaClangDataSection.SectionName,
+ PragmaClangDataSection.PragmaLocation));
+ if (PragmaClangRodataSection.Valid)
+ NewFD->addAttr(PragmaClangRodataSectionAttr::CreateImplicit(
+ Context, PragmaClangRodataSection.SectionName,
+ PragmaClangRodataSection.PragmaLocation));
+ if (PragmaClangRelroSection.Valid)
+ NewFD->addAttr(PragmaClangRelroSectionAttr::CreateImplicit(
+ Context, PragmaClangRelroSection.SectionName,
+ PragmaClangRelroSection.PragmaLocation));
+ }
+
// Apply an implicit SectionAttr if #pragma code_seg is active.
if (CodeSegStack.CurrentValue && D.isFunctionDefinition() &&
!NewFD->hasAttr<SectionAttr>()) {
Added: cfe/trunk/test/CodeGen/cfi-pragma-section.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/cfi-pragma-section.c?rev=375022&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/cfi-pragma-section.c (added)
+++ cfe/trunk/test/CodeGen/cfi-pragma-section.c Wed Oct 16 10:51:40 2019
@@ -0,0 +1,32 @@
+// Check that CFI-generated data structures are tagged with
+// "#pragma clang section" attributes
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=cfi-icall \
+// RUN: -fno-sanitize-trap=cfi-icall -emit-llvm -o - %s | FileCheck %s
+
+// CHECK-DAG: attributes [[ATTR:#[0-9]+]]{{.*}}bss-section{{.*}}data-section{{.*}}rodata-section
+// CHECK-DAG: @.src = private unnamed_addr constant{{.*}}cfi-pragma-section.c{{.*}}[[ATTR]]
+// CHECK-DAG: @{{[0-9]+}} = private unnamed_addr constant{{.*}}int (int){{.*}}[[ATTR]]
+// CHECK-DAG: @{{[0-9]+}} = private unnamed_addr global{{.*}}@.src{{.*}}[[ATTR]]
+
+typedef int (*int_arg_fn)(int);
+
+static int int_arg1(int arg) {
+ return 0;
+}
+
+static int int_arg2(int arg) {
+ return 1;
+}
+
+int_arg_fn int_funcs[2] = {int_arg1, int_arg2};
+
+#pragma clang section bss = ".bss.mycfi"
+#pragma clang section data = ".data.mycfi"
+#pragma clang section rodata = ".rodata.mycfi"
+
+int main(int argc, const char *argv[]) {
+
+ int idx = argv[1][0] - '0';
+ return int_funcs[argc](idx);
+}
More information about the cfe-commits
mailing list