Skip to content

Commit 60ab8c8

Browse files
authored
[lld][macho] Use reloc length correctly in hash computation (#165287)
`Reloc::length` actually encodes the log2 of the length. Thanks @int3 for pointing this out in #160894 (comment). This code computes hashes of relocations. With the correct length, the hashes should more accurately represent the relocation. In my testing of some large binaries, the compressed size change is negligable.
1 parent 585da50 commit 60ab8c8

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

lld/MachO/BPSectionOrderer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ struct BPOrdererMachO : lld::BPOrderer<BPOrdererMachO> {
6161

6262
// Calculate relocation hashes
6363
for (const auto &r : sec.relocs) {
64-
if (r.length == 0 || r.referent.isNull() || r.offset >= data.size())
64+
uint32_t relocLength = 1 << r.length;
65+
if (r.referent.isNull() || r.offset + relocLength > data.size())
6566
continue;
6667

6768
uint64_t relocHash = getRelocHash(r, sectionToIdx);
6869
uint32_t start = (r.offset < windowSize) ? 0 : r.offset - windowSize + 1;
69-
for (uint32_t i = start; i < r.offset + r.length; i++) {
70+
for (uint32_t i = start; i < r.offset + relocLength; i++) {
7071
auto window = data.drop_front(i).take_front(windowSize);
7172
hashes.push_back(xxh3_64bits(window) ^ relocHash);
7273
}

0 commit comments

Comments
 (0)