This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Delete TargetInfo::getPltEntryOffset and simplify Symbol::getPltVA
AbandonedPublic

Authored by MaskRay on Nov 26 2018, 2:20 PM.

Details

Event Timeline

MaskRay created this revision.Nov 26 2018, 2:20 PM
ruiu added a comment.Nov 27 2018, 10:19 AM

Now I think it is more clear than before that adding the size of the header in getPltVA is weird. Maybe that should be handled in getPltEntryOffset that's where we should compute an offset for an PLT entry. Also, please don't casually add an optional parameter to an existing function to "tweak" existing behavior in a subtle way. That kind of changes can quickly accumulate and make functions really hard to understand.

ruiu added a comment.Nov 27 2018, 10:24 AM

I mean we probably should set PltHeaderSize to 0 if IPLT because IPLT's header size is actually zero (unless it is a retpoline IPLT, in which case it should be set to an appropriate value.)

Now I think it is more clear than before that adding the size of the header in getPltVA is weird. Maybe that should be handled in getPltEntryOffset that's where we should compute an offset for an PLT entry. Also, please don't casually add an optional parameter to an existing function to "tweak" existing behavior in a subtle way. That kind of changes can quickly accumulate and make functions really hard to understand.

TargetInfo::getPltEntryOffset does not know which PLT section (normal PLT or IPLT) the caller is referring to.

ruiu added a comment.Nov 27 2018, 1:02 PM

Yes, but you can teach which is the case.

I mean we probably should set PltHeaderSize to 0 if IPLT because IPLT's header size is actually zero (unless it is a retpoline IPLT, in which case it should be set to an appropriate value.)

TargetInfo::PltHeaderSize is a field of TargetInfo, not PltSection. I cannot set PltHeaderSize to 0 for IPLT because otherwise when both normal PLT and IPLT are used, the PltHeaderSize of the normal PLT would be incorrect (0).

ruiu added a comment.Nov 27 2018, 1:08 PM

Then maybe the function is currently at a wrong place?

MaskRay added a comment.EditedNov 27 2018, 1:18 PM

Then maybe the function is currently at a wrong place?

  • InputSection::writeTo calls PltSection::writeTo. This may happen twice, one for normal PLT and the other for IPLT
  • PltSection::writeTo calls Target->writePltHeader and Target->writePlt. The two methods are overriden by Arch/*.cpp targets.
  • X86_64<ELFT>::writePlt (and some other targets) calls Target->getPltEntryOffset.

The bit of information (whether it is PLT or IPLT) is lost when PltSection::writeTo calls into TargetInfo methods. I am confusing about a better place to place the PltHeaderSize information.

ruiu added a comment.Nov 27 2018, 1:40 PM

This is a rough sketch but I believe this is towards a right direction: https://gist.github.com/rui314/d919496a6db34483d673f9ebe34d6f89

MaskRay updated this revision to Diff 175582.Nov 27 2018, 2:24 PM
MaskRay retitled this revision from [ELF] Comment about retpoline IPLT in getPltVA to [ELF] Simplify Symbol::getPltVA logic.

Essentially used ruiu's suggestion to make HeaderSize public

MaskRay updated this revision to Diff 175583.Nov 27 2018, 2:25 PM

Delete unintended change to Target.h

ruiu added a comment.Nov 27 2018, 2:41 PM

Yeah, but the reason why this was just a sketch is because I believe TargetInfo::getPltEntryOffset no longer make much sense with this patch. Perhaps that should be removed.

MaskRay updated this revision to Diff 175588.Nov 27 2018, 3:05 PM
MaskRay retitled this revision from [ELF] Simplify Symbol::getPltVA logic to [ELF] Delete TargetInfo::getPltEntryOffset and simplify Symbol::getPltVA.

Update title

Delete TargetInfo::getPltEntryOffset

ruiu added a comment.Nov 27 2018, 3:13 PM

Hm, the new code doesn't look great as it contains too many repetitions. Do you have any suggestion to make this code better?

Let me play with this code too.

There are pros and cons. It makes the code slightly longer. The duplication mostly happens in getPltEntryOffset(Index); call sites in X86_64 X86 SPARCV9

- uint64_t Off = getPltEntryOffset(Index);
+ uint64_t Off = PltHeaderSize + Index * PltEntrySize;

The advantage is that a casual reader does not need to read the definition of getPltEntryOffset to understand how the offsets are computed. I am biased but I find such duplication tolerable.

ruiu added a comment.Nov 27 2018, 3:29 PM

I see your point, and I agree with that. But the new code is also error-prone because you can make a mistake to not to add the header size, for example. So not sure which is better.

MaskRay abandoned this revision.Nov 27 2018, 4:00 PM