[ELF][MIPS] Support R_MIPS_TLS_DTPREL_HI16/LO16 and R_MIPS_TLS_TPREL_HI16/LO16 relocations

That is initial and the most trivial patch to support TLS for MIPS targets.

llvm-svn: 263712
diff --git a/lld/ELF/Target.cpp b/lld/ELF/Target.cpp
index e76e0a9..fda8975 100644
--- a/lld/ELF/Target.cpp
+++ b/lld/ELF/Target.cpp
@@ -1677,6 +1677,10 @@
                                        uint32_t Type, uint64_t P, uint64_t S,
                                        uint64_t ZA, uint8_t *PairedLoc) const {
   const endianness E = ELFT::TargetEndianness;
+  // Thread pointer and DRP offsets from the start of TLS data area.
+  // https://www.linux-mips.org/wiki/NPTL
+  const uint32_t TPOffset = 0x7000;
+  const uint32_t DTPOffset = 0x8000;
   switch (Type) {
   case R_MIPS_32:
     add32<E>(Loc, S);
@@ -1747,6 +1751,18 @@
   case R_MIPS_PCLO16:
     writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
     break;
+  case R_MIPS_TLS_DTPREL_HI16:
+    writeMipsHi16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
+    break;
+  case R_MIPS_TLS_DTPREL_LO16:
+    writeMipsLo16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
+    break;
+  case R_MIPS_TLS_TPREL_HI16:
+    writeMipsHi16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
+    break;
+  case R_MIPS_TLS_TPREL_LO16:
+    writeMipsLo16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
+    break;
   default:
     fatal("unrecognized reloc " + Twine(Type));
   }
@@ -1767,6 +1783,10 @@
   case R_MIPS_64:
   case R_MIPS_HI16:
   case R_MIPS_LO16:
+  case R_MIPS_TLS_DTPREL_HI16:
+  case R_MIPS_TLS_DTPREL_LO16:
+  case R_MIPS_TLS_TPREL_HI16:
+  case R_MIPS_TLS_TPREL_LO16:
     return false;
   }
 }