diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 7e0ce7d711ef14c2e8cea5434f05c6f156df13df..c7b862f3928ab074e4e3b15931846409f97a5d4a 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -576,6 +576,8 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm, FixedValue = Value; // determine the type of the relocation + + MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind(); unsigned Type; if (Is64Bit) { if (IsPCRel) { @@ -587,7 +589,16 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm, case X86::reloc_signed_4byte: case X86::reloc_pcrel_4byte: assert(isInt<32>(Target.getConstant())); - Type = ELF::R_X86_64_32S; + switch (Modifier) { + case MCSymbolRefExpr::VK_None: + Type = ELF::R_X86_64_32S; + break; + case MCSymbolRefExpr::VK_GOT: + Type = ELF::R_X86_64_GOT32; + break; + default: + llvm_unreachable("Unimplemented"); + } break; case FK_Data_4: Type = ELF::R_X86_64_32; diff --git a/llvm/test/MC/ELF/got.s b/llvm/test/MC/ELF/got.s new file mode 100644 index 0000000000000000000000000000000000000000..c44245d078b236890d596103e87be910d640cc13 --- /dev/null +++ b/llvm/test/MC/ELF/got.s @@ -0,0 +1,14 @@ +// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump | FileCheck %s + +// Test that this produces a R_X86_64_GOT32. + + movl foo@GOT, %eax + +// CHECK: ('_relocations', [ +// CHECK-NEXT: # Relocation 0 +// CHECK-NEXT: (('r_offset', +// CHECK-NEXT: ('r_sym', +// CHECK-NEXT: ('r_type', 3) +// CHECK-NEXT: ('r_addend', +// CHECK-NEXT: ), +// CHECK-NEXT: ])