This is an archive of the discontinued LLVM Phabricator instance.

[LLD][ELF] - Simplify TLS LD handling code.
AbandonedPublic

Authored by grimar on Aug 13 2018, 6:34 AM.

Details

Summary

Our code in handleTlsRelocation is hard to read.
It is possible to rewrite it and reduce a bit.
That is what this patch does. It should be NFC.

Diff Detail

Event Timeline

grimar created this revision.Aug 13 2018, 6:34 AM

If I calculate it right, your code has the same cyclomatic complexity as the original one. At the same time, it is one level deeper, taking into account the number of conditions for the particular lines.

grimar added a comment.Sep 3 2018, 2:18 AM

Rui, what do you think? For me, it looks simpler than the original code.

ruiu added inline comments.Sep 3 2018, 6:27 PM
ELF/Relocations.cpp
189–191

I like the new code because it's shorter, but this part looks odd to me, because only R_ABS is not a TLSLD type, though other relocations are all TLSLD. So it is hard to judge.

grimar updated this revision to Diff 163771.Sep 4 2018, 3:02 AM
  • Addressed review comment.
ELF/Relocations.cpp
189–191

What if I keep logic about R_ABS outside? Like in the updated version.

grimar updated this revision to Diff 175845.Nov 29 2018, 3:31 AM

Rui, what do you think about this one?
I think the new code is better because it groups handling of the R_TLSLD_* relocations at one place.

  • Rebased.
ruiu added inline comments.Nov 29 2018, 11:15 AM
ELF/Arch/PPC64.cpp
742–743

The old code doesn't say anything about these relocations, but your new code contains code for these relocations. Where did they come from? What is special about them?

grimar added inline comments.Nov 30 2018, 1:28 AM
ELF/Arch/PPC64.cpp
742–743

The original code did that:

if (Expr == R_TLSLD_GOT_OFF) {
  // Local-Dynamic relocs can be relaxed to local-exec
  if (!Config->Shared) {
    C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});

The new code does:

if (isRelExprOneOf<R_TLSLD_* ..., R_TLSLD_GOT_OFF>(Expr)) {
    // Local-Dynamic relocs can be relaxed to Local-Exec.
    if (!Config->Shared) {
      C.Relocations.push_back(
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
           Offset, Addend, &Sym});

See, the new code calls Target->adjustRelaxExpr before pushing the relocations
(btw, that is consistent with the other places in this method).
Target->adjustRelaxExpr should leave some relocations along than
(not convert the expression for them).

(Note, such an approach is not new, for example, we are doing the same on AArch64 I think:
https://github.com/llvm-mirror/lld/blob/master/ELF/Arch/AArch64.cpp#L125)

ruiu added a comment.Dec 5 2018, 7:54 AM

I'd like to see more drastic refactoring of this code. Honestly we should completely rewrite this code because the current code is really hard to read. As to this patch, I'm not sure if this is an improvement from readability perspective, as both the current code and the new code are hard to understand to me.

grimar abandoned this revision.Dec 6 2018, 12:32 AM

Ok, abandoned then.