Index: lld/COFF/ICF.cpp =================================================================== --- lld/COFF/ICF.cpp +++ lld/COFF/ICF.cpp @@ -270,9 +270,14 @@ // hash. parallelForEach(Chunks, [&](SectionChunk *SC) { uint32_t Hash = SC->Class[1]; - for (Symbol *B : SC->symbols()) - if (auto *Sym = dyn_cast_or_null(B)) + for (Symbol *B : SC->symbols()) { + if (auto *Sym = dyn_cast_or_null(B)) { + // Rotate the hash by 1 to prevent it from being cancelled out by a + // self-relocation. + Hash = (Hash << 1) | (Hash >> 31); Hash ^= Sym->getChunk()->Class[1]; + } + } // Set MSB to 1 to avoid collisions with non-hash classs. SC->Class[0] = Hash | (1U << 31); }); Index: lld/ELF/ICF.cpp =================================================================== --- lld/ELF/ICF.cpp +++ lld/ELF/ICF.cpp @@ -430,9 +430,14 @@ uint32_t Hash = IS->Class[1]; for (RelTy Rel : Rels) { Symbol &S = IS->template getFile()->getRelocTargetSym(Rel); - if (auto *D = dyn_cast(&S)) - if (auto *RelSec = dyn_cast_or_null(D->Section)) + if (auto *D = dyn_cast(&S)) { + if (auto *RelSec = dyn_cast_or_null(D->Section)) { + // Rotate the hash by 1 to prevent it from being cancelled out by a + // self-relocation. + Hash = (Hash << 1) | (Hash >> 31); Hash ^= RelSec->Class[1]; + } + } } // Set MSB to 1 to avoid collisions with non-hash IDs. IS->Class[0] = Hash | (1U << 31);