Skip to content

Conversation

@ellishg
Copy link
Contributor

@ellishg ellishg commented Oct 27, 2025

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.

@ellishg ellishg requested review from MaskRay and int3 October 27, 2025 17:40
@ellishg
Copy link
Contributor Author

ellishg commented Oct 27, 2025

CC @Colibrow

@llvmbot
Copy link
Member

llvmbot commented Oct 27, 2025

@llvm/pr-subscribers-lld-macho

@llvm/pr-subscribers-lld

Author: Ellis Hoag (ellishg)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/165287.diff

1 Files Affected:

  • (modified) lld/MachO/BPSectionOrderer.cpp (+3-2)
diff --git a/lld/MachO/BPSectionOrderer.cpp b/lld/MachO/BPSectionOrderer.cpp
index 1f07ea9caf5df..d50abc22fc6c1 100644
--- a/lld/MachO/BPSectionOrderer.cpp
+++ b/lld/MachO/BPSectionOrderer.cpp
@@ -61,12 +61,13 @@ struct BPOrdererMachO : lld::BPOrderer<BPOrdererMachO> {
 
     // Calculate relocation hashes
     for (const auto &r : sec.relocs) {
-      if (r.length == 0 || r.referent.isNull() || r.offset >= data.size())
+      uint32_t relocLength = 1 << r.length;
+      if (r.referent.isNull() || r.offset + relocLength > data.size())
         continue;
 
       uint64_t relocHash = getRelocHash(r, sectionToIdx);
       uint32_t start = (r.offset < windowSize) ? 0 : r.offset - windowSize + 1;
-      for (uint32_t i = start; i < r.offset + r.length; i++) {
+      for (uint32_t i = start; i < r.offset + relocLength; i++) {
         auto window = data.drop_front(i).take_front(windowSize);
         hashes.push_back(xxh3_64bits(window) ^ relocHash);
       }

Copy link
Contributor

@int3 int3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@ellishg ellishg merged commit 60ab8c8 into llvm:main Oct 27, 2025
13 checks passed
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
)

`Reloc::length` actually encodes the log2 of the length. Thanks @int3
for pointing this out in
llvm#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.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
)

`Reloc::length` actually encodes the log2 of the length. Thanks @int3
for pointing this out in
llvm#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants