Some MIPS relocations depend on "gp" value. By default, this value has 0x7ff0 offset from a .got section. But relocatable files produced by a compiler or a linker might redefine this default value and we have to use it for a calculation of the relocation result. When we generate EXE or DSO it's trivial. Generating a relocatable output is more difficult case because the linker does calculate relocations in this case and cannot store individual "gp" values used by each input object file. As a workaround we add the "gp" value to the relocation addend.
This fixes https://llvm.org/pr31149
I would probably make addend calculation a bit more explicit
to avoid additional variable, something like:
int64_t Addend = getAddend<ELFT>(Rel); if (!RelTy::IsRela) { const uint8_t *BufLoc = Sec->Data.begin() + Rel.r_offset; Addend += Target->getImplicitAddend(BufLoc, Type); } if (Config->EMachine == EM_MIPS && ...) Addend += Sec->getFile<ELFT>()->MipsGp0; ...