[PECOFF] Fix .bss section alignment
authorRui Ueyama <ruiu@google.com>
Tue, 8 Jul 2014 23:11:01 +0000 (23:11 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 8 Jul 2014 23:11:01 +0000 (23:11 +0000)
Previously the alignment of the .bss section was not
properly set because of a bug in AtomizeDefinedSymbolsInSection.
We set the alignment of the section at the end of the function,
but we use an eraly return for the .bss section, so the code had
been skipped.

llvm-svn: 212571

lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
lld/test/pecoff/Inputs/alignment.obj.yaml
lld/test/pecoff/alignment.test

index 59d429f..a9c1d31 100644 (file)
@@ -678,10 +678,6 @@ std::error_code FileCOFF::AtomizeDefinedSymbolsInSection(
     _symbolAtom[*si] = atom;
     _definedAtomLocations[section][(*si)->Value].push_back(atom);
   }
-
-  // Finally, set alignment to the first atom so that the section contents
-  // will be aligned as specified by the object section header.
-  _definedAtomLocations[section][0][0]->setAlignment(getAlignment(section));
   return std::error_code();
 }
 
@@ -698,6 +694,11 @@ std::error_code FileCOFF::AtomizeDefinedSymbols(
             AtomizeDefinedSymbolsInSection(section, symbols, atoms))
       return ec;
 
+    // Set alignment to the first atom so that the section contents
+    // will be aligned as specified by the object section header.
+    if (atoms.size() > 0)
+      atoms[0]->setAlignment(getAlignment(section));
+
     // Connect atoms with layout-before/layout-after edges.
     connectAtomsWithLayoutEdge(atoms);
 
index 2cb62e6..0d4da1a 100644 (file)
@@ -23,6 +23,14 @@ sections:
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
     Alignment:       0
     SectionData:     22
+  - Name:            .bss$1
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       16
+    SectionData:     0000
+  - Name:            .bss$2
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       16
+    SectionData:     0000
 symbols:
   - Name:            .text
     Value:           0
@@ -60,4 +68,16 @@ symbols:
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            .bss$1
+    Value:           0
+    SectionNumber:   6
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            .bss$2
+    Value:           0
+    SectionNumber:   7
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
 ...
index 31f3acf..f3d8d06 100644 (file)
@@ -1,9 +1,12 @@
 # RUN: yaml2obj %p/Inputs/alignment.obj.yaml > %t.obj
 #
-# RUN: lld -flavor link /out:%t.exe /subsystem:console /force /entry:start \
-# RUN:   -- %t.obj
+# RUN: lld -flavor link /out:%t.exe /subsystem:console /force \
+# RUN:   /entry:start /opt:noref -- %t.obj
 # RUN: llvm-readobj -sections %t.exe | FileCheck %s
 
+CHECK: Name: .bss (2E 62 73 73 00 00 00 00)
+CHECK: RawDataSize: 18
+
 CHECK:      Name: .data (2E 64 61 74 61 00 00 00)
 CHECK-NEXT: VirtualSize: 0x6