[MIPS] Fix calculation of the R_MICROMIPS_LO16 / HI16 relocations
authorSimon Atanasyan <simon@atanasyan.com>
Thu, 9 Nov 2017 12:10:14 +0000 (12:10 +0000)
committerSimon Atanasyan <simon@atanasyan.com>
Thu, 9 Nov 2017 12:10:14 +0000 (12:10 +0000)
llvm-svn: 317781

lld/ELF/InputSection.cpp
lld/test/ELF/mips-micro-relocs.s

index bdc56c8..9fe31e0 100644 (file)
@@ -540,9 +540,14 @@ static uint64_t getRelocTargetVA(RelType Type, int64_t A, uint64_t P,
     // is _gp_disp symbol. In that case we should use the following
     // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
+    // microMIPS variants of these relocations use slightly different
+    // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
+    // to correctly handle less-sugnificant bit of the microMIPS symbol.
     uint64_t V = InX::MipsGot->getGp() + A - P;
     if (Type == R_MIPS_LO16 || Type == R_MICROMIPS_LO16)
       V += 4;
+    if (Type == R_MICROMIPS_LO16 || Type == R_MICROMIPS_HI16)
+      V -= 1;
     return V;
   }
   case R_MIPS_GOT_LOCAL_PAGE:
index f018b9a..3986711 100644 (file)
 
 # REQUIRES: mips
 
-# Check disassembled code when LLD starts to setup
-# the least-significant bit for microMIPS symbols.
 # EB:      __start:
 # EB-NEXT:      20010:       41 a3 00 01     lui     $3, 1
-# EB-NEXT:      20014:       30 63 7f e0     addiu   $3, $3, 32736
+# EB-NEXT:      20014:       30 63 7f df     addiu   $3, $3, 32735
 # EB-NEXT:      20018:       fc 7c 80 18     lw      $3, -32744($gp)
 # EB-NEXT:      2001c:       fc 63 80 18     lw      $3, -32744($3)
 # EB-NEXT:      20020:       8f 70           beqz16  $6, -32
@@ -33,7 +31,7 @@
 
 # EL:      __start:
 # EL-NEXT:      20010:       a3 41 01 00     lui     $3, 1
-# EL-NEXT:      20014:       63 30 e0 7f     addiu   $3, $3, 32736
+# EL-NEXT:      20014:       63 30 df 7f     addiu   $3, $3, 32735
 # EL-NEXT:      20018:       7c fc 18 80     lw      $3, -32744($gp)
 # EL-NEXT:      2001c:       63 fc 18 80     lw      $3, -32744($3)
 # EL-NEXT:      20020:       70 8f           beqz16  $6, -32