[ELF] ICF should respect section alignment
authorPetr Hosek <phosek@chromium.org>
Mon, 22 Aug 2016 18:53:09 +0000 (18:53 +0000)
committerPetr Hosek <phosek@chromium.org>
Mon, 22 Aug 2016 18:53:09 +0000 (18:53 +0000)
When performing ICF, we have to respect the alignment requirement
of each section within each group.

Differential Revision: https://reviews.llvm.org/D23732

llvm-svn: 279456

lld/ELF/ICF.cpp
lld/test/ELF/icf7.s

index 10a2603..209cf20 100644 (file)
@@ -302,7 +302,11 @@ template <class ELFT> void ICF<ELFT>::run() {
   // the same group are consecutive in the vector.
   std::stable_sort(V.begin(), V.end(),
                    [](InputSection<ELFT> *A, InputSection<ELFT> *B) {
-                     return A->GroupId < B->GroupId;
+                     if (A->GroupId != B->GroupId)
+                       return A->GroupId < B->GroupId;
+                     // Within a group, put the highest alignment
+                     // requirement first, so that's the one we'll keep.
+                     return B->Alignment < A->Alignment;
                    });
 
   // Compare static contents and assign unique IDs for each static content.
index f1fca5b..e931351 100644 (file)
@@ -4,8 +4,8 @@
 # RUN: ld.lld %t -o %t2 --icf=all --verbose | FileCheck %s
 # RUN: llvm-objdump -t %t2 | FileCheck -check-prefix=ALIGN %s
 
-# CHECK: selected .text.f1
-# CHECK:   removed .text.f2
+# CHECK: selected .text.f2
+# CHECK:   removed .text.f1
 
 # ALIGN: 0000000000011000 .text 00000000 _start
 # ALIGN: 0000000000011100 .text 00000000 f1