[PATCH] D93102: [Clang][Sema] Detect section type conflicts between functions and variables

Tomas Matheson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 11 03:39:52 PST 2020


tmatheson created this revision.
Herald added a reviewer: aaron.ballman.
tmatheson requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If two variables are declared with __attribute__((section(name))) and
the implicit section types (e.g. read only vs writeable) conflict, an
error is raised. Extend this mechanism so that an error is raised if the
section type implied by a function's __attribute__((section)) conflicts
with that of another variable.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93102

Files:
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Sema/attr-section.c


Index: clang/test/Sema/attr-section.c
===================================================================
--- clang/test/Sema/attr-section.c
+++ clang/test/Sema/attr-section.c
@@ -26,9 +26,17 @@
 
 // Not a warning.
 int c;
-int c __attribute__((section("foo,zed")));
+int c __attribute__((section("seg1,sec1")));
 
 // Also OK.
 struct r_debug {};
 extern struct r_debug _r_debug;
 struct r_debug _r_debug __attribute__((nocommon, section(".r_debug,bar")));
+
+// Section type conflicts between functions and variables
+void test3(void) __attribute__((section("seg2,sec2"))); // expected-note {{declared here}}
+void test3(void) {}
+const int ro __attribute__((section("seg2,sec2"))) = 10; // expected-error {{'ro' causes a section type conflict with 'test3'}}
+void test4(void) __attribute__((section("seg3,sec3"))); // expected-note {{declared here}}
+void test4(void) {}
+int rw __attribute__((section("seg3,sec3"))) = 10;       // expected-error {{'rw' causes a section type conflict with 'test4'}}
Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -3043,8 +3043,15 @@
   }
 
   SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
-  if (NewAttr)
+  if (NewAttr) {
     D->addAttr(NewAttr);
+    if (auto FD = dyn_cast<FunctionDecl>(D))
+      if (auto SA = dyn_cast<SectionAttr>(NewAttr))
+        S.UnifySection(SA->getName(),
+                       ASTContext::PSF_Implicit | ASTContext::PSF_Execute |
+                           ASTContext::PSF_Read,
+                       FD);
+  }
 }
 
 // This is used for `__declspec(code_seg("segname"))` on a decl.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93102.311161.patch
Type: text/x-patch
Size: 1701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201211/419f6621/attachment-0001.bin>


More information about the cfe-commits mailing list