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()) + 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,11 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - -ffreestanding -triple=x86_64-pc-linux-gnu | FileCheck %s + +extern int aa __attribute__((section(".sdata"))); +// CHECK-DAG: @aa = external global i32, section ".sdata", align 4 + +extern int bb __attribute__((section(".sdata"))) = 1; +// CHECK-DAG: @bb = global i32 1, section ".sdata", align 4 + +int foo() { + return aa + bb; +}