Skip to content

Commit e6c78ea

Browse files
committedJul 27, 2018
Reland r338088, "ELF: Make --print-icf-sections output deterministic."
The xxHash64 function has been made unsigned-char-independent, so we can reland this change now. Original commit message: > The icf-safe.s test currently fails on 32-bit platforms because it uses > the --print-icf-sections flag and depends on the output appearing in > a specific order. However, this flag causes the output to depend on > the order of the sections in the Sections array, which depends on the > hash values returned from hash_combine, which happen to be different > for that test between 32-bit and 64-bit platforms. > > This change makes the output deterministic by using xxHash64 instead of > hash_combine. Differential Revision: https://reviews.llvm.org/D49877 llvm-svn: 338153
1 parent f611ce8 commit e6c78ea

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed
 

‎lld/ELF/ICF.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@
8080
#include "SyntheticSections.h"
8181
#include "Writer.h"
8282
#include "lld/Common/Threads.h"
83-
#include "llvm/ADT/Hashing.h"
83+
#include "llvm/ADT/StringExtras.h"
8484
#include "llvm/BinaryFormat/ELF.h"
8585
#include "llvm/Object/ELF.h"
86+
#include "llvm/Support/xxhash.h"
8687
#include <algorithm>
8788
#include <atomic>
8889

@@ -155,12 +156,6 @@ template <class ELFT> class ICF {
155156
};
156157
}
157158

158-
// Returns a hash value for S. Note that the information about
159-
// relocation targets is not included in the hash value.
160-
template <class ELFT> static uint32_t getHash(InputSection *S) {
161-
return hash_combine(S->Flags, S->getSize(), S->NumRelocations, S->Data);
162-
}
163-
164159
// Returns true if section S is subject of ICF.
165160
static bool isEligible(InputSection *S) {
166161
if (!S->Live || S->KeepUnique || !(S->Flags & SHF_ALLOC))
@@ -441,7 +436,7 @@ template <class ELFT> void ICF<ELFT>::run() {
441436
// Initially, we use hash values to partition sections.
442437
parallelForEach(Sections, [&](InputSection *S) {
443438
// Set MSB to 1 to avoid collisions with non-hash IDs.
444-
S->Class[0] = getHash<ELFT>(S) | (1U << 31);
439+
S->Class[0] = xxHash64(toStringRef(S->Data)) | (1U << 31);
445440
});
446441

447442
// From now on, sections in Sections vector are ordered so that sections

‎lld/test/ELF/icf-safe.s

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
# RUN: ld.lld %t1.o %t2.o -o %t2 --icf=all --print-icf-sections --export-dynamic | FileCheck --check-prefix=ALL-EXPORT %s
1111
# RUN: ld.lld %t1copy.o -o %t4 --icf=safe 2>&1 | FileCheck --check-prefix=OBJCOPY %s
1212

13-
# CHECK-NOT: selected section {{.*}}:(.rodata.l1)
14-
# CHECK: selected section {{.*}}:(.rodata.l3)
15-
# CHECK: removing identical section {{.*}}:(.rodata.l4)
16-
1713
# CHECK-NOT: selected section {{.*}}:(.text.f1)
1814
# CHECK: selected section {{.*}}:(.text.f3)
1915
# CHECK: removing identical section {{.*}}:(.text.f4)
@@ -22,6 +18,10 @@
2218
# CHECK: selected section {{.*}}:(.rodata.h3)
2319
# CHECK: removing identical section {{.*}}:(.rodata.h4)
2420

21+
# CHECK-NOT: selected section {{.*}}:(.rodata.l1)
22+
# CHECK: selected section {{.*}}:(.rodata.l3)
23+
# CHECK: removing identical section {{.*}}:(.rodata.l4)
24+
2525
# CHECK-NOT: selected section {{.*}}:(.rodata.g1)
2626
# CHECK: selected section {{.*}}:(.rodata.g3)
2727
# CHECK: removing identical section {{.*}}:(.rodata.g4)
@@ -30,26 +30,26 @@
3030

3131
# With --icf=all address-significance implies keep-unique only for rodata, not
3232
# text.
33-
# ALL-NOT: selected section {{.*}}:(.rodata.l1)
34-
# ALL: selected section {{.*}}:(.rodata.l3)
35-
# ALL: removing identical section {{.*}}:(.rodata.l4)
36-
3733
# ALL: selected section {{.*}}:(.text.f3)
3834
# ALL: removing identical section {{.*}}:(.text.f4)
3935

40-
# ALL: selected section {{.*}}:(.text.f1)
41-
# ALL: removing identical section {{.*}}:(.text.f2)
42-
# ALL: removing identical section {{.*}}:(.text.non_addrsig1)
43-
# ALL: removing identical section {{.*}}:(.text.non_addrsig2)
44-
4536
# ALL-NOT: selected section {{.*}}:(.rodata.h1)
4637
# ALL: selected section {{.*}}:(.rodata.h3)
4738
# ALL: removing identical section {{.*}}:(.rodata.h4)
4839

40+
# ALL-NOT: selected section {{.*}}:(.rodata.l1)
41+
# ALL: selected section {{.*}}:(.rodata.l3)
42+
# ALL: removing identical section {{.*}}:(.rodata.l4)
43+
4944
# ALL-NOT: selected section {{.*}}:(.rodata.g1)
5045
# ALL: selected section {{.*}}:(.rodata.g3)
5146
# ALL: removing identical section {{.*}}:(.rodata.g4)
5247

48+
# ALL: selected section {{.*}}:(.text.f1)
49+
# ALL: removing identical section {{.*}}:(.text.f2)
50+
# ALL: removing identical section {{.*}}:(.text.non_addrsig1)
51+
# ALL: removing identical section {{.*}}:(.text.non_addrsig2)
52+
5353
# llvm-mc normally emits an empty .text section into every object file. Since
5454
# nothing actually refers to it via a relocation, it doesn't have any associated
5555
# symbols (thus nor can anything refer to it via a relocation, making it safe to
@@ -58,36 +58,36 @@
5858
# STB_LOCAL or STV_HIDDEN symbols. The dynsym entries should have prevented
5959
# anything else from being merged.
6060
# EXPORT-NOT: selected section
61-
# EXPORT: selected section {{.*}}:(.rodata.l3)
62-
# EXPORT: removing identical section {{.*}}:(.rodata.l4)
63-
# EXPORT-NOT: selected section
6461
# EXPORT: selected section {{.*}}:(.rodata.h3)
6562
# EXPORT: removing identical section {{.*}}:(.rodata.h4)
6663
# EXPORT-NOT: selected section
6764
# EXPORT: selected section {{.*}}:(.text)
6865
# EXPORT: removing identical section {{.*}}:(.text)
6966
# EXPORT-NOT: selected section
67+
# EXPORT: selected section {{.*}}:(.rodata.l3)
68+
# EXPORT: removing identical section {{.*}}:(.rodata.l4)
69+
# EXPORT-NOT: selected section
7070

7171
# If --icf=all is specified when exporting we can also merge the exported text
7272
# sections, but not the exported rodata.
7373
# ALL-EXPORT-NOT: selected section
74-
# ALL-EXPORT: selected section {{.*}}:(.rodata.l3)
75-
# ALL-EXPORT: removing identical section {{.*}}:(.rodata.l4)
76-
# ALL-EXPORT-NOT: selected section
7774
# ALL-EXPORT: selected section {{.*}}:(.text.f3)
7875
# ALL-EXPORT: removing identical section {{.*}}:(.text.f4)
7976
# ALL-EXPORT-NOT: selected section
80-
# ALL-EXPORT: selected section {{.*}}:(.text.f1)
81-
# ALL-EXPORT: removing identical section {{.*}}:(.text.f2)
82-
# ALL-EXPORT: removing identical section {{.*}}:(.text.non_addrsig1)
83-
# ALL-EXPORT: removing identical section {{.*}}:(.text.non_addrsig2)
84-
# ALL-EXPORT-NOT: selected section
8577
# ALL-EXPORT: selected section {{.*}}:(.rodata.h3)
8678
# ALL-EXPORT: removing identical section {{.*}}:(.rodata.h4)
8779
# ALL-EXPORT-NOT: selected section
8880
# ALL-EXPORT: selected section {{.*}}:(.text)
8981
# ALL-EXPORT: removing identical section {{.*}}:(.text)
9082
# ALL-EXPORT-NOT: selected section
83+
# ALL-EXPORT: selected section {{.*}}:(.rodata.l3)
84+
# ALL-EXPORT: removing identical section {{.*}}:(.rodata.l4)
85+
# ALL-EXPORT-NOT: selected section
86+
# ALL-EXPORT: selected section {{.*}}:(.text.f1)
87+
# ALL-EXPORT: removing identical section {{.*}}:(.text.f2)
88+
# ALL-EXPORT: removing identical section {{.*}}:(.text.non_addrsig1)
89+
# ALL-EXPORT: removing identical section {{.*}}:(.text.non_addrsig2)
90+
# ALL-EXPORT-NOT: selected section
9191

9292
# OBJCOPY: --icf=safe is incompatible with object files created using objcopy or ld -r
9393

0 commit comments

Comments
 (0)
Please sign in to comment.