Handle GOTOFF correctly on i386.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 18 Oct 2010 16:38:04 +0000 (16:38 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 18 Oct 2010 16:38:04 +0000 (16:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@116711 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/ELFObjectWriter.cpp
test/MC/ELF/relocation-386.s [new file with mode: 0644]

index 17c46f4800cc6f91821d6a54aa14c028260cde48..e003a5679fbcba1cdabcb0d6305bdf113c065189 100644 (file)
@@ -556,17 +556,19 @@ static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
   if (SD.isExternal())
     return true;
 
-  if (Section.getFlags() & MCSectionELF::SHF_MERGE)
-    return Target.getConstant() != 0;
-
   MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
   const MCSectionELF &Sec2 =
     static_cast<const MCSectionELF&>(F.getParent()->getSection());
 
   if (&Sec2 != &Section &&
-      (Kind == MCSymbolRefExpr::VK_PLT || Kind == MCSymbolRefExpr::VK_GOTPCREL))
+      (Kind == MCSymbolRefExpr::VK_PLT ||
+       Kind == MCSymbolRefExpr::VK_GOTPCREL ||
+       Kind == MCSymbolRefExpr::VK_GOTOFF))
     return true;
 
+  if (Section.getFlags() & MCSectionELF::SHF_MERGE)
+    return Target.getConstant() != 0;
+
   return false;
 }
 
@@ -663,7 +665,7 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
         case MCSymbolRefExpr::VK_GOT:
           Type = ELF::R_X86_64_GOT32;
           break;
-        case llvm::MCSymbolRefExpr::VK_GOTPCREL:
+        case MCSymbolRefExpr::VK_GOTPCREL:
           Type = ELF::R_X86_64_GOTPCREL;
           break;
         default:
@@ -689,6 +691,14 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
       // instead?
       case X86::reloc_signed_4byte:
       case X86::reloc_pcrel_4byte:
+        switch (Modifier) {
+        case MCSymbolRefExpr::VK_GOTOFF:
+          Type = ELF::R_386_GOTOFF;
+          break;
+        default:
+          llvm_unreachable("Unimplemented");
+        }
+        break;
       case FK_Data_4: Type = ELF::R_386_32; break;
       case FK_Data_2: Type = ELF::R_386_16; break;
       case X86::reloc_pcrel_1byte:
diff --git a/test/MC/ELF/relocation-386.s b/test/MC/ELF/relocation-386.s
new file mode 100644 (file)
index 0000000..e65adcc
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: llvm-mc -filetype=obj -triple i386-pc-linux-gnu %s -o - | elf-dump | FileCheck  %s
+
+// Test that we produce a GOTOFF and that the relocation uses the symbol and not
+// the section.
+
+
+// CHECK:      # Symbol 1
+// CHECK-NEXT: (('st_name', 5) # '.Lfoo'
+
+// CHECK:      # Relocation 0
+// CHECK-NEXT: (('r_offset', 2)
+// CHECK-NEXT:  ('r_sym', 1)
+// CHECK-NEXT:  ('r_type', 9)
+
+        .text
+bar:
+       leal    .Lfoo@GOTOFF(%ebx), %eax
+       .section        .rodata.str1.16,"aMS",@progbits,1
+.Lfoo:
+       .asciz   "bool llvm::llvm_start_multithreaded()"