This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Properly handle & validate relocation r_length
ClosedPublic

Authored by int3 on May 29 2020, 9:16 PM.

Details

Summary

We should be reading / writing our addends / relocated addresses based on
r_length, and not just based on the type of the relocation. But since only
some r_length values are valid for a given reloc type, I've also added some
validation.

ld64 has code to allow for r_length = 0 in X86_64_RELOC_BRANCH relocs, but I'm
not sure how to create such a relocation...

Depends on D80414.

Diff Detail

Event Timeline

int3 created this revision.May 29 2020, 9:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 29 2020, 9:16 PM
smeenai accepted this revision.Jun 9 2020, 6:27 PM

This is great!

lld/MachO/Arch/X86_64.cpp
58

It's an error path so efficiency doesn't matter, but in general, Twine is a better choice for concatenations like this.

76

It might be better to disallow the 0 case for now, and comment that we're diverging from ld64's behavior there. That way we'll get a loud error if we do end up seeing that case in the wild, and we can figure out what's going on with it then :)

Short jumps are a thing (where the offset is a single byte), but I can't imagine when we'd emit a relocation for one :/

124–126

In particular, this is incorrect if we're allowing the r_length for an X86_64_RELOC_BRANCH to be 0.

This revision is now accepted and ready to land.Jun 9 2020, 6:27 PM
int3 marked 4 inline comments as done.Jun 13 2020, 10:31 PM
int3 added inline comments.
lld/MachO/Arch/X86_64.cpp
58

hm I spent some time trying to use Twine but ended up with all sorts of corrupted strings. I even put all the intermediate temp values into local variables, but still no dice...

124–126

Good catch! Added to the comment.

int3 updated this revision to Diff 270602.Jun 13 2020, 10:31 PM
int3 marked an inline comment as done.

address comments

This revision was automatically updated to reflect the committed changes.