This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Check address ranges when applying relocations
ClosedPublic

Authored by int3 on Mar 10 2021, 7:43 PM.

Details

Reviewers
thakis
Group Reviewers
Restricted Project
Commits
rGdc8bee92658e: [lld-macho] Check address ranges when applying relocations
Summary

This diff required fixing getEmbeddedAddend to apply sign
extension to 32-bit values. We were previously passing around wrong
64-bit addend values that became "right" after being truncated back to
32-bit.

I've also made getEmbeddedAddend return a signed int, which is similar
to what LLD-ELF does for its getImplicitAddend.

reportRangeError, checkUInt, and checkInt are counterparts of similar
functions in LLD-ELF.

Diff Detail

Event Timeline

int3 created this revision.Mar 10 2021, 7:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2021, 7:43 PM
int3 requested review of this revision.Mar 10 2021, 7:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2021, 7:43 PM
thakis accepted this revision.Mar 11 2021, 6:06 AM
This revision is now accepted and ready to land.Mar 11 2021, 6:06 AM
thakis added inline comments.Mar 11 2021, 6:07 AM
lld/MachO/Arch/ARM64.cpp
173

(Should the check be in encodeBranch26() instead (etc)? The we won't miss it for other calls, and the 26/28 discrepancy is maybe a bit less magical when the check is closer to the shift.

191

(same question)

int3 updated this revision to Diff 330012.Mar 11 2021, 10:19 AM
int3 marked 2 inline comments as done.

check range in encoding function

int3 added inline comments.Mar 11 2021, 10:22 AM
lld/MachO/Arch/ARM64.cpp
173

Good idea. In fact while implementing it I realized we were actually missing ranges checks for the pointers to stubs, which lld-ELF does check. I've therefore added support for it by introducing SymbolDiagnostic. Want to have another look before I land this?

This revision was landed with ongoing or failed builds.Mar 12 2021, 2:26 PM
This revision was automatically updated to reflect the committed changes.

This breaks tests everywhere, eg http://45.33.8.238/linux/41614/step_11.txt (but every platform really)

Please take a look, and revert for now if it takes a while to fix.

int3 added a comment.Mar 12 2021, 3:42 PM

Weird, it seems like the addresses are off by 8 bytes. Perhaps llvm-mc is somehow being configured to auto-align things on my machine? Anyway, I guess we can replace those numbers with [[#]], their exact values aren't that important