Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm] Fix symbol name offset in parsing chained-fixup entry function #83564

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fengzhichu
Copy link

I could not find any symbol name when using objdump to parse chained-fixup entries. Then I found this bug that NameOffset had a wrong value because NameOffset and WeakImport varibles were exchanged and calculation for NameOffset was also wrong.

The definition of dyld_chained_import_addend64 is in MachO.h.

struct dyld_chained_import_addend64 {
  uint64_t lib_ordinal : 16;
  uint64_t weak_import : 1;
  uint64_t reserved : 15;
  uint64_t name_offset : 32;
  uint64_t addend;
};

https://github.com/llvm/llvm-project/blame/main/llvm/include/llvm/BinaryFormat/MachO.h#L1109-L1115

I could not find any symbol name when using objdump to parse chained-fixup entries. Then I found this bug that NameOffset had a wrong value because NameOffset and WeakImport varibles were exchanged and calculation for NameOffset was also wrong.

The definition of dyld_chained_import_addend64 is in MachO.h.
https://github.com/llvm/llvm-project/blame/main/llvm/include/llvm/BinaryFormat/MachO.h#L1109-L1115
Copy link

github-actions bot commented Mar 1, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 1, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Hummer (fengzhichu)

Changes

I could not find any symbol name when using objdump to parse chained-fixup entries. Then I found this bug that NameOffset had a wrong value because NameOffset and WeakImport varibles were exchanged and calculation for NameOffset was also wrong.

The definition of dyld_chained_import_addend64 is in MachO.h.

struct dyld_chained_import_addend64 {
  uint64_t lib_ordinal : 16;
  uint64_t weak_import : 1;
  uint64_t reserved : 15;
  uint64_t name_offset : 32;
  uint64_t addend;
};

https://github.com/llvm/llvm-project/blame/main/llvm/include/llvm/BinaryFormat/MachO.h#L1109-L1115


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

1 Files Affected:

  • (modified) llvm/lib/Object/MachOObjectFile.cpp (+2-2)
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp
index 1cfd0a069463e9..fd310d36e65c2e 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -5231,8 +5231,8 @@ MachOObjectFile::getDyldChainedFixupTargets() const {
       auto RawValue = getArray<uint64_t, 2>(*this, ImportPtr);
 
       LibOrdinal = getEncodedOrdinal<uint16_t>(RawValue[0] & 0xFFFF);
-      NameOffset = (RawValue[0] >> 16) & 1;
-      WeakImport = RawValue[0] >> 17;
+      WeakImport = (RawValue[0] >> 16) & 1;
+      NameOffset = RawValue[0] >> 32;
       Addend = RawValue[1];
     } else {
       llvm_unreachable("Import format should have been checked");

@fengzhichu fengzhichu changed the title fix symbol name offset in parsing chained-fixup entry function [llvm][Object][MachO]fix symbol name offset in parsing chained-fixup entry function Mar 1, 2024
@fengzhichu fengzhichu changed the title [llvm][Object][MachO]fix symbol name offset in parsing chained-fixup entry function [llvm][Object][MachO] Fix symbol name offset in parsing chained-fixup entry function Mar 1, 2024
@fengzhichu fengzhichu changed the title [llvm][Object][MachO] Fix symbol name offset in parsing chained-fixup entry function [llvm] Fix symbol name offset in parsing chained-fixup entry function Mar 1, 2024
@dwblaikie
Copy link
Collaborator

Test coverage?

@MaskRay
Copy link
Member

MaskRay commented Mar 1, 2024

https://maskray.me/blog/2021-08-08-toolchain-testing#i-dont-know-where-to-add-a-test

You can comment out code that your patch has modified, or make an intentional mistake, then run the whole testsuite to locate relevant tests. You can use git log -- file to learn the history of these tests.

The llvm-objdump Mach-O code generally has quite poor test coverage. So it's possible you cannot find the test immediately. You may need to study tests under llvm/test/Object and llvm/test/tools/llvm-objdump/MachO, and sometimes a refactoring before fixing an issue is useful.

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.

None yet

4 participants