eandrews created this revision.

Currently, if  _attribute_((section())) is used for extern variables, section 
information is not emitted in generated IR when the variables are used. This is 
expected since sections are not generated for external linkage objects. However 
NiosII requires this information as it uses special GP-relative accesses for 
any objects that use attribute section (.sdata). GCC keeps this attribute in 
middle-end.

This change emits the section information for all targets.


https://reviews.llvm.org/D36487

Files:
  lib/CodeGen/CodeGenModule.cpp
  test/CodeGenCXX/extern-section-attribute.cpp


Index: test/CodeGenCXX/extern-section-attribute.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/extern-section-attribute.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=i386-pc-linux-gnu 
| FileCheck %s --check-prefix=CHECK-LIN
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding 
-triple=x86_64-pc-linux-gnu | FileCheck %s --check-prefix=CHECK-LIN
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=i386-pc-win32 | 
FileCheck %s --check-prefix=CHECK-WIN
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=x86_64-pc-win32 | 
FileCheck %s  --check-prefix=CHECK-WIN
+
+extern int aa __attribute__((section(".sdata")));
+// CHECK-LIN-DAG: @aa = external global i32, section ".sdata", align 4
+// CHECK-WIN-DAG: @"\01?aa@@3HA" = external global i32, section ".sdata", 
align 4
+
+extern int bb __attribute__((section(".sdata"))) = 1;
+// CHECK-LIN-DAG: @bb = global i32 1, section ".sdata", align 4
+// CHECK-WIN-DAG: @"\01?bb@@3HA" = global i32 1, section ".sdata", align 4
+
+int foo() {
+  return aa + bb;
+}
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -2430,6 +2430,12 @@
       EmitGlobalVarDefinition(D);
     }
 
+    // Emit section information for extern variables.
+    if (D->hasExternalStorage() && !D->isThisDeclarationADefinition()) {
+      if (const SectionAttr *SA = D->getAttr<SectionAttr>())
+        GV->setSection(SA->getName());
+    }
+
     // Handle XCore specific ABI requirements.
     if (getTriple().getArch() == llvm::Triple::xcore &&
         D->getLanguageLinkage() == CLanguageLinkage &&


Index: test/CodeGenCXX/extern-section-attribute.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/extern-section-attribute.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=i386-pc-linux-gnu | FileCheck %s --check-prefix=CHECK-LIN
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=x86_64-pc-linux-gnu | FileCheck %s --check-prefix=CHECK-LIN
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=i386-pc-win32 | FileCheck %s --check-prefix=CHECK-WIN
+// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=x86_64-pc-win32 | FileCheck %s  --check-prefix=CHECK-WIN
+
+extern int aa __attribute__((section(".sdata")));
+// CHECK-LIN-DAG: @aa = external global i32, section ".sdata", align 4
+// CHECK-WIN-DAG: @"\01?aa@@3HA" = external global i32, section ".sdata", align 4
+
+extern int bb __attribute__((section(".sdata"))) = 1;
+// CHECK-LIN-DAG: @bb = global i32 1, section ".sdata", align 4
+// CHECK-WIN-DAG: @"\01?bb@@3HA" = global i32 1, section ".sdata", align 4
+
+int foo() {
+  return aa + bb;
+}
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -2430,6 +2430,12 @@
       EmitGlobalVarDefinition(D);
     }
 
+    // Emit section information for extern variables.
+    if (D->hasExternalStorage() && !D->isThisDeclarationADefinition()) {
+      if (const SectionAttr *SA = D->getAttr<SectionAttr>())
+        GV->setSection(SA->getName());
+    }
+
     // Handle XCore specific ABI requirements.
     if (getTriple().getArch() == llvm::Triple::xcore &&
         D->getLanguageLinkage() == CLanguageLinkage &&
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to