r374934 - Added support for "#pragma clang section relro=<name>"
Dmitry Mikulin via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 15 11:31:10 PDT 2019
Author: dmikulin
Date: Tue Oct 15 11:31:10 2019
New Revision: 374934
URL: http://llvm.org/viewvc/llvm-project?rev=374934&view=rev
Log:
Added support for "#pragma clang section relro=<name>"
Differential Revision: https://reviews.llvm.org/D68806
Modified:
cfe/trunk/docs/LanguageExtensions.rst
cfe/trunk/include/clang/Basic/Attr.td
cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
cfe/trunk/include/clang/Sema/Sema.h
cfe/trunk/lib/CodeGen/CGDecl.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/Parse/ParsePragma.cpp
cfe/trunk/lib/Sema/SemaAttr.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/test/CodeGenCXX/clang-sections.cpp
cfe/trunk/test/Sema/pragma-clang-section.c
Modified: cfe/trunk/docs/LanguageExtensions.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LanguageExtensions.rst?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/docs/LanguageExtensions.rst (original)
+++ cfe/trunk/docs/LanguageExtensions.rst Tue Oct 15 11:31:10 2019
@@ -3445,14 +3445,14 @@ The section names can be specified as:
.. code-block:: c++
- #pragma clang section bss="myBSS" data="myData" rodata="myRodata" text="myText"
+ #pragma clang section bss="myBSS" data="myData" rodata="myRodata" relro="myRelro" text="myText"
The section names can be reverted back to default name by supplying an empty
string to the section kind, for example:
.. code-block:: c++
- #pragma clang section bss="" data="" text="" rodata=""
+ #pragma clang section bss="" data="" text="" rodata="" relro=""
The ``#pragma clang section`` directive obeys the following rules:
Modified: cfe/trunk/include/clang/Basic/Attr.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Attr.td (original)
+++ cfe/trunk/include/clang/Basic/Attr.td Tue Oct 15 11:31:10 2019
@@ -2020,6 +2020,14 @@ def PragmaClangRodataSection : Inheritab
let Documentation = [Undocumented];
}
+def PragmaClangRelroSection : InheritableAttr {
+ // This attribute has no spellings as it is only ever created implicitly.
+ let Spellings = [];
+ let Args = [StringArgument<"Name">];
+ let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
+ let Documentation = [Undocumented];
+}
+
def PragmaClangTextSection : InheritableAttr {
// This attribute has no spellings as it is only ever created implicitly.
let Spellings = [];
Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue Oct 15 11:31:10 2019
@@ -976,9 +976,9 @@ def err_pragma_misplaced_in_decl : Error
// '#pragma clang section' related errors
def err_pragma_expected_clang_section_name : Error<
- "expected one of [bss|data|rodata|text] section kind in '#pragma %0'">;
+ "expected one of [bss|data|rodata|text|relro] section kind in '#pragma %0'">;
def err_pragma_clang_section_expected_equal : Error<
- "expected '=' following '#pragma clang section %select{invalid|bss|data|rodata|text}0'">;
+ "expected '=' following '#pragma clang section %select{invalid|bss|data|rodata|text|relro}0'">;
def warn_pragma_expected_section_name : Warning<
"expected a string literal for the section name in '#pragma %0' - ignored">,
InGroup<IgnoredPragmas>;
Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Tue Oct 15 11:31:10 2019
@@ -418,7 +418,8 @@ public:
PCSK_BSS = 1,
PCSK_Data = 2,
PCSK_Rodata = 3,
- PCSK_Text = 4
+ PCSK_Text = 4,
+ PCSK_Relro = 5
};
enum PragmaClangSectionAction {
@@ -439,6 +440,7 @@ public:
PragmaClangSection PragmaClangBSSSection;
PragmaClangSection PragmaClangDataSection;
PragmaClangSection PragmaClangRodataSection;
+ PragmaClangSection PragmaClangRelroSection;
PragmaClangSection PragmaClangTextSection;
enum PragmaMsStackAction {
Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Tue Oct 15 11:31:10 2019
@@ -419,6 +419,8 @@ void CodeGenFunction::EmitStaticVarDecl(
var->addAttribute("data-section", SA->getName());
if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
var->addAttribute("rodata-section", SA->getName());
+ if (auto *SA = D.getAttr<PragmaClangRelroSectionAttr>())
+ var->addAttribute("relro-section", SA->getName());
if (const SectionAttr *SA = D.getAttr<SectionAttr>())
var->setSection(SA->getName());
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Oct 15 11:31:10 2019
@@ -1717,6 +1717,8 @@ void CodeGenModule::setNonAliasAttribute
GV->addAttribute("data-section", SA->getName());
if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
GV->addAttribute("rodata-section", SA->getName());
+ if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
+ GV->addAttribute("relro-section", SA->getName());
}
if (auto *F = dyn_cast<llvm::Function>(GO)) {
@@ -4118,6 +4120,7 @@ static bool isVarDeclStrongDefinition(co
// If no specialized section name is applicable, it will resort to default.
if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
D->hasAttr<PragmaClangDataSectionAttr>() ||
+ D->hasAttr<PragmaClangRelroSectionAttr>() ||
D->hasAttr<PragmaClangRodataSectionAttr>())
return true;
Modified: cfe/trunk/lib/Parse/ParsePragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.cpp?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParsePragma.cpp (original)
+++ cfe/trunk/lib/Parse/ParsePragma.cpp Tue Oct 15 11:31:10 2019
@@ -1790,7 +1790,7 @@ void PragmaMSStructHandler::HandlePragma
/*IsReinject=*/false);
}
-// #pragma clang section bss="abc" data="" rodata="def" text=""
+// #pragma clang section bss="abc" data="" rodata="def" text="" relro=""
void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &FirstToken) {
@@ -1812,6 +1812,8 @@ void PragmaClangSectionHandler::HandlePr
SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
else if (SecType->isStr("rodata"))
SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
+ else if (SecType->isStr("relro"))
+ SecKind = Sema::PragmaClangSectionKind::PCSK_Relro;
else if (SecType->isStr("text"))
SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
else {
Modified: cfe/trunk/lib/Sema/SemaAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAttr.cpp?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAttr.cpp Tue Oct 15 11:31:10 2019
@@ -266,6 +266,9 @@ void Sema::ActOnPragmaClangSection(Sourc
case PragmaClangSectionKind::PCSK_Rodata:
CSec = &PragmaClangRodataSection;
break;
+ case PragmaClangSectionKind::PCSK_Relro:
+ CSec = &PragmaClangRelroSection;
+ break;
case PragmaClangSectionKind::PCSK_Text:
CSec = &PragmaClangTextSection;
break;
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Oct 15 11:31:10 2019
@@ -12657,6 +12657,11 @@ void Sema::FinalizeDeclaration(Decl *Thi
Context, PragmaClangRodataSection.SectionName,
PragmaClangRodataSection.PragmaLocation,
AttributeCommonInfo::AS_Pragma));
+ if (PragmaClangRelroSection.Valid)
+ VD->addAttr(PragmaClangRelroSectionAttr::CreateImplicit(
+ Context, PragmaClangRelroSection.SectionName,
+ PragmaClangRelroSection.PragmaLocation,
+ AttributeCommonInfo::AS_Pragma));
}
if (auto *DD = dyn_cast<DecompositionDecl>(ThisDecl)) {
Modified: cfe/trunk/test/CodeGenCXX/clang-sections.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/clang-sections.cpp?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/clang-sections.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/clang-sections.cpp Tue Oct 15 11:31:10 2019
@@ -29,17 +29,20 @@ int goo(void) { // my_text.2
static int lstat_h; // my_bss.2
return zoo(g, &lstat_h);
}
-#pragma clang section rodata="my_rodata.2" data="my_data.2"
+#pragma clang section rodata="my_rodata.2" data="my_data.2" relro="my_relro.2"
int l = 5; // my_data.2
extern const int m;
const int m = 6; // my_rodata.2
+
+typedef int (*fptr_t)(void);
+const fptr_t fptrs[2] = {&foo, &goo};
#pragma clang section rodata="" data="" bss="" text=""
int n; // default
int o = 6; // default
extern const int p;
const int p = 7; // default
int hoo(void) {
- return b;
+ return b + fptrs[f]();
}
}
//CHECK: @a = global i32 0, align 4 #0
@@ -62,17 +65,19 @@ int hoo(void) {
//CHECK: @n = global i32 0, align 4
//CHECK: @o = global i32 6, align 4
//CHECK: @p = constant i32 7, align 4
+//CHECK: @_ZL5fptrs = internal constant [2 x i32 ()*] [i32 ()* @foo, i32 ()* @goo], align 4 #3
-//CHECK: define i32 @foo() #4 {
-//CHECK: define i32 @goo() #5 {
-//CHECK: declare i32 @zoo(i32*, i32*) #6
-//CHECK: define i32 @hoo() #7 {
+//CHECK: define i32 @foo() #5 {
+//CHECK: define i32 @goo() #6 {
+//CHECK: declare i32 @zoo(i32*, i32*) #7
+//CHECK: define i32 @hoo() #8 {
//CHECK: attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
//CHECK: attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
//CHECK: attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
-//CHECK: attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "rodata-section"="my_rodata.2" }
-//CHECK: attributes #4 = { {{.*"implicit-section-name"="my_text.1".*}} }
-//CHECK: attributes #5 = { {{.*"implicit-section-name"="my_text.2".*}} }
-//CHECK-NOT: attributes #6 = { {{.*"implicit-section-name".*}} }
+//CHECK: attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "relro-section"="my_relro.2" "rodata-section"="my_rodata.2" }
+//CHECK: attributes #4 = { "relro-section"="my_relro.2" }
+//CHECK: attributes #5 = { {{.*"implicit-section-name"="my_text.1".*}} }
+//CHECK: attributes #6 = { {{.*"implicit-section-name"="my_text.2".*}} }
//CHECK-NOT: attributes #7 = { {{.*"implicit-section-name".*}} }
+//CHECK-NOT: attributes #8 = { {{.*"implicit-section-name".*}} }
Modified: cfe/trunk/test/Sema/pragma-clang-section.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/pragma-clang-section.c?rev=374934&r1=374933&r2=374934&view=diff
==============================================================================
--- cfe/trunk/test/Sema/pragma-clang-section.c (original)
+++ cfe/trunk/test/Sema/pragma-clang-section.c Tue Oct 15 11:31:10 2019
@@ -3,15 +3,17 @@
#pragma clang section bss="" data="" rodata="" text=""
#pragma clang section
-#pragma clang section dss="mybss.2" // expected-error {{expected one of [bss|data|rodata|text] section kind in '#pragma clang section'}}
-#pragma clang section deta="mydata.2" // expected-error {{expected one of [bss|data|rodata|text] section kind in '#pragma clang section'}}
-#pragma clang section rodeta="rodata.2" // expected-error {{expected one of [bss|data|rodata|text] section kind in '#pragma clang section'}}
-#pragma clang section taxt="text.2" // expected-error {{expected one of [bss|data|rodata|text] section kind in '#pragma clang section'}}
+#pragma clang section dss="mybss.2" // expected-error {{expected one of [bss|data|rodata|text|relro] section kind in '#pragma clang section'}}
+#pragma clang section deta="mydata.2" // expected-error {{expected one of [bss|data|rodata|text|relro] section kind in '#pragma clang section'}}
+#pragma clang section rodeta="rodata.2" // expected-error {{expected one of [bss|data|rodata|text|relro] section kind in '#pragma clang section'}}
+#pragma clang section taxt="text.2" // expected-error {{expected one of [bss|data|rodata|text|relro] section kind in '#pragma clang section'}}
-#pragma clang section section bss="mybss.2" // expected-error {{expected one of [bss|data|rodata|text] section kind in '#pragma clang section'}}
+#pragma clang section section bss="mybss.2" // expected-error {{expected one of [bss|data|rodata|text|relro] section kind in '#pragma clang section'}}
#pragma clang section bss "mybss.2" // expected-error {{expected '=' following '#pragma clang section bss'}}
#pragma clang section data "mydata.2" // expected-error {{expected '=' following '#pragma clang section data'}}
#pragma clang section rodata "myrodata.2" // expected-error {{expected '=' following '#pragma clang section rodata'}}
-#pragma clang section bss="" data="" rodata="" text="" more //expected-error {{expected one of [bss|data|rodata|text] section kind in '#pragma clang section'}}
+#pragma clang section text "text.2" // expected-error {{expected '=' following '#pragma clang section text'}}
+#pragma clang section relro "relro.2" // expected-error {{expected '=' following '#pragma clang section relro'}}
+#pragma clang section bss="" data="" rodata="" text="" more //expected-error {{expected one of [bss|data|rodata|text|relro] section kind in '#pragma clang section'}}
int a;
More information about the cfe-commits
mailing list