This is an archive of the discontinued LLVM Phabricator instance.

[Bolt] fix a relocation bug for R_AARCH64_CALL26
ClosedPublic

Authored by sinan on Sep 14 2023, 2:13 AM.

Details

Summary

If the R_AARCH64_CALL26 against a symbol that has a lower address, then encodeValueAArch64 will return a wrong value.

In the included test case, the expected output of encodeValueAArch64 is 97ffffff, but it returns 3fffffffffffffff, and then an invalid instruction is encoded.

Diff Detail

Event Timeline

sinan created this revision.Sep 14 2023, 2:13 AM
Herald added a project: Restricted Project. · View Herald Transcript
sinan requested review of this revision.Sep 14 2023, 2:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 14 2023, 2:13 AM
Kepontry accepted this revision.Sep 14 2023, 2:34 AM

Yes, you are absolutely right. This is my mistake, I didn't consider the sign bit when performing the right shift.

Thank you for your correction; I'm just getting started in this field, and I'll improve the quality of the patches I submit in the future.

Let's wait for the opinion from the Meta team.

This revision is now accepted and ready to land.Sep 14 2023, 2:34 AM
yota9 added a comment.EditedSep 14 2023, 2:48 AM

Thanks for the fix! Could you please incorporate your test to the old reloc-call26.s file? It seems to be easy to do, thanks!

sinan updated this revision to Diff 556772.Sep 14 2023, 4:31 AM

Merged into test case bolt/test/AArch64/reloc-call26.s

  1. Enhanced the test for relocations against functions with both higher and lower addresses.
  2. Added --force-patch option in the test case; otherwise, encodeValueAArch64 won't be invoked.
sinan added a comment.EditedSep 14 2023, 4:34 AM

Yes, you are absolutely right. This is my mistake, I didn't consider the sign bit when performing the right shift.

Thank you for your correction; I'm just getting started in this field, and I'll improve the quality of the patches I submit in the future.

Let's wait for the opinion from the Meta team.

Hi @Kepontry

Take it easy, I've made even worse mistakes when I just started. Keep going and good luck :)

yota9 added a comment.EditedSep 14 2023, 7:14 AM

One more thing. It seems to be without force-patch the encodeValueAArch64 still would be called, but since functions would be moved to the new text there won't be negative address problem. I don't think there is a better way then using force-funcs so it would fail to patch entries & functions would stay on its current place, but please:

  1. Please write the reason of using force-funcs option in the test, because after some time even I won't remember what I wrote above :)
  2. Please add checks on llvm-bolt output that func1 and func2 were failed to patch and won't be optimised, since otherwise the test might pass, but the problem won't be checked. Alternatively (maybe even better) please check that in output binary the addresses are func1 < _start < func2. Maybe I would do both since the first option also shows more clearly why force-patch was used, but it's up to you.

Thanks!

sinan updated this revision to Diff 556932.Sep 18 2023, 1:33 AM

update the test case.

  1. check address order with llvm-nm --numeric-sort;
  2. check the message of fail to patch entries;
  3. add comment
sinan added a comment.Sep 18 2023, 2:30 AM

One more thing. It seems to be without force-patch the encodeValueAArch64 still would be called, but since functions would be moved to the new text there won't be negative address problem. I don't think there is a better way then using force-funcs so it would fail to patch entries & functions would stay on its current place, but please:

  1. Please write the reason of using force-funcs option in the test, because after some time even I won't remember what I wrote above :)
  2. Please add checks on llvm-bolt output that func1 and func2 were failed to patch and won't be optimised, since otherwise the test might pass, but the problem won't be checked. Alternatively (maybe even better) please check that in output binary the addresses are func1 < _start < func2. Maybe I would do both since the first option also shows more clearly why force-patch was used, but it's up to you.

Thanks!

your analysis is right. encodeValueAArch64 will be called without force-patch. What this option really does is keep the address order of func1, _start and func2 since func1 and func2 fail to patch entries. Thanks!

yota9 accepted this revision.Sep 18 2023, 2:53 AM

Thanks for the fix, @sinan ! LGTM

This revision was automatically updated to reflect the committed changes.
Amir added a comment.Sep 18 2023, 10:33 AM

@sinan – please properly capitalize the title and use 50-72 commit formatting rule to align with the rest of BOLT commits for your future commits. Thank you for the fix!