<div dir="ltr">Hi Erich,<div><br></div><div>this breaks existing code. NaCl does this:</div><div><br></div><div><div>#include <link.h></div></div><div><div>struct r_debug _r_debug __attribute__((nocommon, section(".r_debug")));</div></div><div><br></div><div>(There's a lengthy-ish comment for why in <a href="https://cs.chromium.org/chromium/src/native_client/src/trusted/service_runtime/linux/nacl_bootstrap.c?q=nacl_bootstrap.c&sq=package:chromium&dr&l=424">https://cs.chromium.org/chromium/src/native_client/src/trusted/service_runtime/linux/nacl_bootstrap.c?q=nacl_bootstrap.c&sq=package:chromium&dr&l=424</a>)</div><div><br></div><div>link.h in e.g. the debian jessie sysroot says:</div><div>extern struct r_debug _r_debug;<br></div><div><br></div><div>After this change, clang complains:</div><div><br></div><div><div>../../native_client/src/trusted/service_runtime/linux/nacl_bootstrap.c:434:16: error: section attribute is specified on redeclared variable [-Werror,-Wsection]</div><div>struct r_debug _r_debug __attribute__((nocommon, section(".r_debug")));</div><div>               ^</div><div>../../build/linux/debian_jessie_amd64-sysroot/usr/include/link.h:67:23: note: previous declaration is here</div><div>extern struct r_debug _r_debug;</div></div><div><br></div><div><br></div><div>This code used to work in clang, and continues to work in gcc. So this patch probably isn't quite the right approach. Ideas?</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 26, 2017 at 7:42 PM, Erich Keane via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: erichkeane<br>
Date: Tue Sep 26 16:42:34 2017<br>
New Revision: 314262<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=314262&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=314262&view=rev</a><br>
Log:<br>
Emit section information for extern variables.<br>
<br>
Currently, if _attribute_((section())) is used for extern variables,<br>
section information is not emitted in generated IR when the variables are used.<br>
This is expected since sections are not generated for external linkage objects.<br>
However NiosII requires this information as it uses special GP-relative accesses<br>
for any objects that use attribute section (.sdata). GCC keeps this attribute in<br>
  middle-end.<br>
<br>
This change emits the section information for all targets.<br>
<br>
Patch By: Elizabeth Andrews<br>
<br>
Differential Revision:<a href="https://reviews.llvm.org/D36487" rel="noreferrer" target="_blank">https://reviews.llvm.<wbr>org/D36487</a><br>
<br>
<br>
Modified:<br>
    cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td<br>
    cfe/trunk/lib/CodeGen/<wbr>CodeGenModule.cpp<br>
    cfe/trunk/lib/Sema/SemaDecl.<wbr>cpp<br>
    cfe/trunk/test/Sema/attr-<wbr>section.c<br>
<br>
Modified: cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=314262&r1=314261&r2=314262&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/include/<wbr>clang/Basic/<wbr>DiagnosticSemaKinds.td?rev=<wbr>314262&r1=314261&r2=314262&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td (original)<br>
+++ cfe/trunk/include/clang/Basic/<wbr>DiagnosticSemaKinds.td Tue Sep 26 16:42:34 2017<br>
@@ -2620,6 +2620,8 @@ def err_attribute_section_invalid_<wbr>for_ta<br>
   "argument to 'section' attribute is not valid for this target: %0">;<br>
 def warn_mismatched_section : Warning<<br>
   "section does not match previous declaration">, InGroup<Section>;<br>
+def warn_attribute_section_on_<wbr>redeclaration : Warning<<br>
+  "section attribute is specified on redeclared variable">, InGroup<Section>;<br>
<br>
 def err_anonymous_property: Error<<br>
   "anonymous property is not supported">;<br>
<br>
Modified: cfe/trunk/lib/CodeGen/<wbr>CodeGenModule.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=314262&r1=314261&r2=314262&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/CodeGen/<wbr>CodeGenModule.cpp?rev=314262&<wbr>r1=314261&r2=314262&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/lib/CodeGen/<wbr>CodeGenModule.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/<wbr>CodeGenModule.cpp Tue Sep 26 16:42:34 2017<br>
@@ -2432,6 +2432,12 @@ CodeGenModule::<wbr>GetOrCreateLLVMGlobal(Str<br>
       EmitGlobalVarDefinition(D);<br>
     }<br>
<br>
+    // Emit section information for extern variables.<br>
+    if (D->hasExternalStorage()) {<br>
+      if (const SectionAttr *SA = D->getAttr<SectionAttr>())<br>
+        GV->setSection(SA->getName());<br>
+    }<br>
+<br>
     // Handle XCore specific ABI requirements.<br>
     if (getTriple().getArch() == llvm::Triple::xcore &&<br>
         D->getLanguageLinkage() == CLanguageLinkage &&<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaDecl.<wbr>cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=314262&r1=314261&r2=314262&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/Sema/<wbr>SemaDecl.cpp?rev=314262&r1=<wbr>314261&r2=314262&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/lib/Sema/SemaDecl.<wbr>cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaDecl.<wbr>cpp Tue Sep 26 16:42:34 2017<br>
@@ -2607,6 +2607,16 @@ void Sema::mergeDeclAttributes(<wbr>NamedDecl<br>
     }<br>
   }<br>
<br>
+  // This redeclaration adds a section attribute.<br>
+  if (New->hasAttr<SectionAttr>() && !Old->hasAttr<SectionAttr>()) {<br>
+    if (auto *VD = dyn_cast<VarDecl>(New)) {<br>
+      if (VD-><wbr>isThisDeclarationADefinition() != VarDecl::Definition) {<br>
+        Diag(New->getLocation(), diag::warn_attribute_section_<wbr>on_redeclaration);<br>
+        Diag(Old->getLocation(), diag::note_previous_<wbr>declaration);<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
   if (!Old->hasAttrs())<br>
     return;<br>
<br>
<br>
Modified: cfe/trunk/test/Sema/attr-<wbr>section.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-section.c?rev=314262&r1=314261&r2=314262&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/test/Sema/<wbr>attr-section.c?rev=314262&r1=<wbr>314261&r2=314262&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/test/Sema/attr-<wbr>section.c (original)<br>
+++ cfe/trunk/test/Sema/attr-<wbr>section.c Tue Sep 26 16:42:34 2017<br>
@@ -19,3 +19,7 @@ void __attribute__((section("foo,<wbr>zed")))<br>
 void __attribute__((section("bar,<wbr>zed"))) test2(void) {} // expected-warning {{section does not match previous declaration}}<br>
<br>
 enum __attribute__((section("NEAR,<wbr>x"))) e { one }; // expected-error {{'section' attribute only applies to functions, methods, properties, and global variables}}<br>
+<br>
+extern int a; // expected-note {{previous declaration is here}}<br>
+int *b = &a;<br>
+extern int a __attribute__((section("foo,<wbr>zed"))); // expected-warning {{section attribute is specified on redeclared variable}}<br>
<br>
<br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>