"This is a small fix for a bug where static object instances were being incorrectly generated.  The bug was caused by my inability to read the GNU libobjc source and is only apparent when JITing code (static compilation does not expose the bug due to the data layout of other globals)."

Patch by David Chisnall!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52680 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGObjCGNU.cpp b/lib/CodeGen/CGObjCGNU.cpp
index f2cfdca..2f02b13 100644
--- a/lib/CodeGen/CGObjCGNU.cpp
+++ b/lib/CodeGen/CGObjCGNU.cpp
@@ -755,10 +755,12 @@
     llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
   llvm::Constant *Statics = 
     MakeGlobal(StaticsListTy, Elements, ".objc_statics");
-  Statics = new
-    llvm::GlobalVariable(llvm::PointerType::getUnqual(StaticsListTy), false,
-        llvm::GlobalValue::InternalLinkage, Statics, ".objc_statics_ptr",
-        &TheModule);
+  llvm::ArrayType *StaticsListArrayTy =
+    llvm::ArrayType::get(llvm::PointerType::getUnqual(StaticsListTy), 2);
+  Elements.clear();
+  Elements.push_back(Statics);
+  Elements.push_back(llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(StaticsListTy)));
+  Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
   Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
   // Array of classes, categories, and constant objects
   llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,