Remember the maximum alignment used to refer to a common symbol.

llvm-svn: 246517
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 7eb7dda..f2b484d 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -33,10 +33,15 @@
   if (L.first && L.second) {
     if (isCommon()) {
       if (Other->isCommon()) {
-        // FIXME: We also need to remember the alignment restriction.
-        if (cast<DefinedCommon<ELFT>>(this)->Sym.st_size >=
-            cast<DefinedCommon<ELFT>>(Other)->Sym.st_size)
+        auto *ThisC = cast<DefinedCommon<ELFT>>(this);
+        auto *OtherC = cast<DefinedCommon<ELFT>>(Other);
+        typename DefinedCommon<ELFT>::uintX_t MaxAlign =
+            std::max(ThisC->MaxAlignment, OtherC->MaxAlignment);
+        if (ThisC->Sym.st_size >= OtherC->Sym.st_size) {
+          ThisC->MaxAlignment = MaxAlign;
           return 1;
+        }
+        OtherC->MaxAlignment = MaxAlign;
         return -1;
       }
       return -1;
diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h
index ba6be5c..7f7578d 100644
--- a/lld/ELF/Symbols.h
+++ b/lld/ELF/Symbols.h
@@ -141,7 +141,9 @@
   typedef typename std::conditional<ELFT::Is64Bits, uint64_t, uint32_t>::type
       uintX_t;
   explicit DefinedCommon(StringRef N, const Elf_Sym &Sym)
-      : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {}
+      : Defined<ELFT>(Base::DefinedCommonKind, N, Sym) {
+    MaxAlignment = Sym.st_value;
+  }
 
   static bool classof(const SymbolBody *S) {
     return S->kind() == Base::DefinedCommonKind;
@@ -150,6 +152,9 @@
   // The output offset of this common symbol in the output bss. Computed by the
   // writer.
   uintX_t OffsetInBSS;
+
+  // The maximum alignment we have seen for this symbol.
+  uintX_t MaxAlignment;
 };
 
 // Regular defined symbols read from object file symbol tables.
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 1ac20b4..ed1ae93 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -411,7 +411,7 @@
 template <class ELFT>
 static bool cmpAlign(const DefinedCommon<ELFT> *A,
                      const DefinedCommon<ELFT> *B) {
-  return A->Sym.st_value > B->Sym.st_value;
+  return A->MaxAlignment > B->MaxAlignment;
 }
 
 // Create output section objects and add them to OutputSections.
@@ -457,7 +457,7 @@
   uintX_t Off = BSSSec->getSize();
   for (DefinedCommon<ELFT> *C : CommonSymbols) {
     const Elf_Sym &Sym = C->Sym;
-    uintX_t Align = Sym.st_value;
+    uintX_t Align = C->MaxAlignment;
     Off = RoundUpToAlignment(Off, Align);
     C->OffsetInBSS = Off;
     Off += Sym.st_size;
diff --git a/lld/test/elf2/Inputs/common.s b/lld/test/elf2/Inputs/common.s
index fd5a99e..ea8ba91 100644
--- a/lld/test/elf2/Inputs/common.s
+++ b/lld/test/elf2/Inputs/common.s
@@ -1,2 +1,3 @@
 .comm sym1,8,4
 .comm sym2,4,4
+.comm sym4,4,16
diff --git a/lld/test/elf2/common.s b/lld/test/elf2/common.s
index 535411f..1e33933 100644
--- a/lld/test/elf2/common.s
+++ b/lld/test/elf2/common.s
@@ -12,11 +12,19 @@
 // CHECK-NEXT: ]
 // CHECK-NEXT: Address: 0x1000
 // CHECK-NEXT: Offset:
-// CHECK-NEXT: Size: 18
+// CHECK-NEXT: Size: 22
 
 
+// CHECK:      Name: sym4
+// CHECK-NEXT: Value: 0x1000
+// CHECK-NEXT: Size: 4
+// CHECK-NEXT: Binding: Global
+// CHECK-NEXT: Type: Object
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: .bss
+
 // CHECK:      Name: sym3
-// CHECK-NEXT: Value: 0x1010
+// CHECK-NEXT: Value: 0x1014
 // CHECK-NEXT: Size: 2
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
@@ -24,7 +32,7 @@
 // CHECK-NEXT: Section: .bss
 
 // CHECK:      Name: sym2
-// CHECK-NEXT: Value: 0x1008
+// CHECK-NEXT: Value: 0x100C
 // CHECK-NEXT: Size: 8
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
@@ -32,7 +40,7 @@
 // CHECK-NEXT: Section: .bss
 
 // CHECK:      Name: sym1
-// CHECK-NEXT: Value: 0x1000
+// CHECK-NEXT: Value: 0x1004
 // CHECK-NEXT: Size: 8
 // CHECK-NEXT: Binding: Global
 // CHECK-NEXT: Type: Object
@@ -46,3 +54,4 @@
 .comm sym1,4,4
 .comm sym2,8,4
 .comm sym3,2,2
+.comm sym4,4,2