[PATCH] D78572: [Clang][Sema] Capturing section type conflicts on #pragma clang section

Lucas Prates via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 21 09:42:13 PDT 2020


pratlucas created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Section names used in clang section pragmas were not validated against
previously defined sections, causing section type conflicts to be
ignored by Sema.

This patch enables Clang to capture these section type conflicts by
using the existing Sema's UnifySection method to validate section names
from clang section pragmas.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78572

Files:
  clang/include/clang/AST/ASTContext.h
  clang/lib/Parse/ParsePragma.cpp
  clang/lib/Sema/SemaAttr.cpp
  clang/test/Sema/pragma-clang-section.c


Index: clang/test/Sema/pragma-clang-section.c
===================================================================
--- clang/test/Sema/pragma-clang-section.c
+++ clang/test/Sema/pragma-clang-section.c
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple arm-none-eabi
-#pragma clang section bss="mybss.1" data="mydata.1" rodata="myrodata.1" text="mytext.1"
+#pragma clang section bss="mybss.1" data="mydata.1" rodata="myrodata.1" text="mytext.1" // expected-note {{#pragma entered here}} expected-note {{#pragma entered here}}
 #pragma clang section bss="" data="" rodata="" text=""
 #pragma clang section
 
@@ -16,4 +16,10 @@
 #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'}}
+
+#pragma clang section bss="mybss.3" data="mybss.3" // expected-error {{this causes a section type conflict with a prior #pragma section}} expected-note {{#pragma entered here}} expected-note {{#pragma entered here}}
+#pragma clang section rodata="mydata.1" // expected-error {{this causes a section type conflict with a prior #pragma section}}
+#pragma clang section bss="myrodata.1" // expected-error {{this causes a section type conflict with a prior #pragma section}}
+#pragma clang section text="mybss.3" // expected-error {{this causes a section type conflict with a prior #pragma section}}
+
 int a;
Index: clang/lib/Sema/SemaAttr.cpp
===================================================================
--- clang/lib/Sema/SemaAttr.cpp
+++ clang/lib/Sema/SemaAttr.cpp
@@ -256,12 +256,15 @@
 void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action,
                                    PragmaClangSectionKind SecKind, StringRef SecName) {
   PragmaClangSection *CSec;
+  int SectionFlags = ASTContext::PSF_Read;
   switch (SecKind) {
     case PragmaClangSectionKind::PCSK_BSS:
       CSec = &PragmaClangBSSSection;
+      SectionFlags |= ASTContext::PSF_Write | ASTContext::PSF_ZeroInit;
       break;
     case PragmaClangSectionKind::PCSK_Data:
       CSec = &PragmaClangDataSection;
+      SectionFlags |= ASTContext::PSF_Write;
       break;
     case PragmaClangSectionKind::PCSK_Rodata:
       CSec = &PragmaClangRodataSection;
@@ -271,6 +274,7 @@
       break;
     case PragmaClangSectionKind::PCSK_Text:
       CSec = &PragmaClangTextSection;
+      SectionFlags |= ASTContext::PSF_Execute;
       break;
     default:
       llvm_unreachable("invalid clang section kind");
@@ -281,6 +285,9 @@
     return;
   }
 
+  if (UnifySection(SecName, SectionFlags, PragmaLoc))
+    return;
+
   CSec->Valid = true;
   CSec->SectionName = std::string(SecName);
   CSec->PragmaLocation = PragmaLoc;
Index: clang/lib/Parse/ParsePragma.cpp
===================================================================
--- clang/lib/Parse/ParsePragma.cpp
+++ clang/lib/Parse/ParsePragma.cpp
@@ -1845,6 +1845,7 @@
       return;
     }
 
+    SourceLocation PragmaLocation = Tok.getLocation();
     PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
     if (Tok.isNot(tok::equal)) {
       PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
@@ -1855,7 +1856,7 @@
     if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
       return;
 
-    Actions.ActOnPragmaClangSection(Tok.getLocation(),
+    Actions.ActOnPragmaClangSection(PragmaLocation,
       (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
                        Sema::PragmaClangSectionAction::PCSA_Clear),
        SecKind, SecName);
Index: clang/include/clang/AST/ASTContext.h
===================================================================
--- clang/include/clang/AST/ASTContext.h
+++ clang/include/clang/AST/ASTContext.h
@@ -2946,6 +2946,7 @@
     PSF_Write = 0x2,
     PSF_Execute = 0x4,
     PSF_Implicit = 0x8,
+    PSF_ZeroInit = 0x10,
     PSF_Invalid = 0x80000000U,
   };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78572.259030.patch
Type: text/x-patch
Size: 4184 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200421/e0fc2f24/attachment.bin>


More information about the cfe-commits mailing list