This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Support pc-relative section relocations
ClosedPublic

Authored by int3 on Apr 30 2020, 2:56 PM.

Diff Detail

Event Timeline

int3 created this revision.Apr 30 2020, 2:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 30 2020, 2:56 PM
int3 planned changes to this revision.Apr 30 2020, 5:23 PM

This needs to be rebased after the abandonment of D79153

MaskRay added inline comments.May 3 2020, 4:41 PM
lld/MachO/InputFiles.cpp
174

Error messages are usually not capitalized.

It'd be nice to have a test for zero r_symbolnum

MaskRay added inline comments.May 4 2020, 3:21 PM
lld/test/MachO/relocations.s
45

You can also use the same string but place the both labels at the same place. Up to you.

int3 marked an inline comment as done.May 4 2020, 10:44 PM
int3 added inline comments.
lld/MachO/InputFiles.cpp
174

Do you mean test as in unit test or an if-check? I don't think we can generate a zero r_symbolnum when for a non-extern relocation. I can check for it here though

int3 updated this revision to Diff 262256.May 5 2020, 4:24 PM

add tests for SIGNED_* section relocations

smeenai added inline comments.May 6 2020, 7:01 PM
lld/MachO/InputFiles.cpp
173

Should add more details to the error message, e.g. the object file being processed and details about the relocation.

lld/MachO/InputSection.cpp
46

Perhaps "the relocated target section", to make it completely clear which section is being referred to?

48

r.offset is relative to the start of the input section containing the relocation. Doesn't this need to be adjusted for the address of that input section as well? As in:

addend -= isec->header->addr - (header->addr + r.offset + 4);

I was gonna comment that the + 4 seems specific to x86-64; on ARM64, for example, the PC refers to the current instruction and not the next one. However, I actually can't get LLVM to produce a non-extern relocation for ARM64. ld64 does handle non-extern ARM64_RELOC_UNSIGNED relocations, but for anything I feed to LLVM, it converts local symbols to linker-local symbols and just uses an extern relocation.

The other thing I noticed looking at ld64 is that we aren't using or validating the length field of the relocation anywhere, which is potentially problematic.

lld/test/MachO/relocations.s
48

How come the comment in the next test about needing to create a new section to force a section relocation doesn't apply? Is the cstring section a special case? (Wouldn't surprise me since IIRC the linker atomizes the cstring section automatically based on zero termination instead of atomizing based on symbols.)

int3 marked 6 inline comments as done.May 6 2020, 8:37 PM
int3 added inline comments.
lld/MachO/InputFiles.cpp
173

hm, I think this is a really unlikely (and therefore fatal) error for sane inputs, but okay... I do agree that error() messages should give useful hints to the user though

lld/MachO/InputSection.cpp
48

Oops, I guess I hadn't realized this since the relocations have all been in the __text section...

Re ARM, I guess we'll eventually have something similar to the getDylibSymbolVA above. I'll add a TODO comment.

And yeah, I need to figure out the significance of the length field.

lld/test/MachO/relocations.s
48

oh, good observation... yeah, it seems like __cstring is special. I tried renaming it to __not_cstring while keeping .asciz values and this turned into a symbol relocation

int3 planned changes to this revision.May 6 2020, 8:41 PM
int3 marked 2 inline comments as done.
int3 updated this revision to Diff 262539.May 6 2020, 8:57 PM
int3 marked an inline comment as done.

address comments

lld/MachO/InputSection.cpp
46

good point. I think I'll just say "target section", once it's an offset it's irrelevant whether the section has been relocated

smeenai accepted this revision.May 8 2020, 1:32 PM

LGTM

lld/MachO/InputFiles.cpp
173

Yup, but given that this is a user-facing error for a corrupted input file, it's helpful to give as many details as possible about the corruption.

lld/MachO/InputSection.cpp
48

Awesome, thanks for adding a test as well!

This revision is now accepted and ready to land.May 8 2020, 1:32 PM
This revision was automatically updated to reflect the committed changes.