[lld] r259474 - ELF: Simplify and add comments.

Rafael EspĂ­ndola via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 2 14:13:42 PST 2016


On 2 February 2016 at 16:43, Rui Ueyama <ruiu at google.com> wrote:
> I guess that I still don't fully understand this piece of code. When we
> reach here for what? Can you describe it a little bit more in detail for me?

Sure. The code is trying to decide if a given relocation can be fully
resolved by the static linker or if needs to ask the dynamic linker
for help (i.e., create a dynamic relocation).

The static linker cannot know the final answer when:

* The symbol can be preempted. That is, the symbol the static linker
is seeing might not be the one used at runtime.
* The symbol is the final one, but the value the relocation wants
depends on where the code is placed in memory.

 I actually find it a bit harder to follow now that part of the code
is duplicated in the "need got" if. We have:

-------------------------------
    bool CBP = canBePreempted(Body, /*NeedsGot=*/true);
      bool Dynrel = Config->Shared && !Target->isRelRelative(Type) &&
                    !Target->isSizeRel(Type);
      if (CBP)
        Body->setUsedInDynamicReloc();
      if (CBP || Dynrel)
        Out<ELFT>::RelaDyn->addReloc({&C, &RI});
......


  if (canBePreempted(Body, /*NeedsGot=*/false)) {
      Body->setUsedInDynamicReloc();
      Out<ELFT>::RelaDyn->addReloc({&C, &RI});
      continue;
    }

    // If we get here, the code we are handling is not PIC. We need to copy
    // relocations from object files to the output file, so that the
    // dynamic linker can fix up addresses. But there are a few exceptions.
    // If the relocation will not change at runtime, we don't need to copy
    // them. For example, we don't copy PC-relative relocations because
    // the distance between two symbols won't change whereever they are
    // loaded. Likewise, if we are linking an executable, it will be loaded
    // at a fixed address, so we don't copy relocations.
    if (Config->Shared && !Target->isRelRelative(Type) &&
        !Target->isSizeRel(Type))
       Out<ELFT>::RelaDyn->addReloc({&C, &RI});
----------------------------

The two calls to setUsedInDynamicReloc and RelaDyn->addReloc should be merged.

The overall structure of how we collect information for dynamic
relocations should be fixed too, I will try to do that next.

Cheers,
Rafael


More information about the llvm-commits mailing list