llvm.org GIT mirror llvm / 18d49ac
MC: Support COFF image-relative MCSymbolRefs Add support for the COFF relocation types IMAGE_REL_I386_DIR32NB and IMAGE_REL_AMD64_ADDR32NB for 32- and 64-bit respectively. These are similar to normal 4-byte relocations except that they do not include the base address of the image. Image-relative relocations are used for debug information (32-bit) and SEH unwind tables (64-bit). A new MCSymbolRef variant called 'VK_COFF_IMGREL32' is introduced to specify such relocations. For AT&T assembly, this variant can be accessed using the symbol suffix '@imgrel'. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179240 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Rieck 6 years ago
8 changed file(s) with 101 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
0 ===============
1 LLVM Extensions
2 ===============
3
4 .. contents::
5 :local:
6 :depth: 1
7
8 .. toctree::
9 :hidden:
10
11 Introduction
12 ============
13
14 This document describes extensions to tools and formats LLVM seeks compatibility
15 with.
16
17 Machine-specific Assembly Syntax
18 ================================
19
20 X86/COFF-Dependent
21 ------------------
22
23 The following additional relocation type is supported:
24
25 **@IMGREL** (AT&T syntax only) generates an image-relative relocation that
26 corresponds to the COFF relocation types ``IMAGE_REL_I386_DIR32NB`` (32-bit) or
27 ``IMAGE_REL_AMD64_ADDR32NB`` (64-bit).
28
29 .. code-block:: gas
30
31 .text
32 fun:
33 mov foo@IMGREL(%ebx, %ecx, 4), %eax
34
35 .section .pdata
36 .long fun@IMGREL
37 .long (fun@imgrel + 0x3F)
38 .long $unwind$fun@imgrel
159159 ExtendingLLVM
160160 HowToSetUpLLVMStyleRTTI
161161 ProgrammersManual
162 Extensions
162163
163164 :doc:`LLVM Language Reference Manual `
164165 Defines the LLVM intermediate representation and the assembly form of the
170171 :doc:`ProgrammersManual`
171172 Introduction to the general layout of the LLVM sourcebase, important classes
172173 and APIs, and some tips & tricks.
174
175 :doc:`Extensions`
176 LLVM-specific extensions to tools and formats LLVM seeks compatibility with.
173177
174178 :doc:`CommandLine`
175179 Provides information on using the command line parsing library.
215215 VK_Mips_GOT_HI16,
216216 VK_Mips_GOT_LO16,
217217 VK_Mips_CALL_HI16,
218 VK_Mips_CALL_LO16
218 VK_Mips_CALL_LO16,
219
220 VK_COFF_IMGREL32 // symbol@imgrel (image-relative)
219221 };
220222
221223 private:
1010 #define LLVM_MC_MCWINCOFFOBJECTWRITER_H
1111
1212 namespace llvm {
13 class MCFixup;
1314 class MCObjectWriter;
15 class MCValue;
1416 class raw_ostream;
1517
1618 class MCWinCOFFObjectTargetWriter {
2325 virtual ~MCWinCOFFObjectTargetWriter() {}
2426
2527 unsigned getMachine() const { return Machine; }
26 virtual unsigned getRelocType(unsigned FixupKind) const = 0;
28 virtual unsigned getRelocType(const MCValue &Target,
29 const MCFixup &Fixup,
30 bool IsCrossSection) const = 0;
2731 };
2832
2933 /// \brief Construct a new Win COFF writer instance.
249249 case VK_Mips_GOT_LO16: return "GOT_LO16";
250250 case VK_Mips_CALL_HI16: return "CALL_HI16";
251251 case VK_Mips_CALL_LO16: return "CALL_LO16";
252 case VK_COFF_IMGREL32: return "IMGREL32";
252253 }
253254 llvm_unreachable("Invalid variant kind");
254255 }
284285 .Case("dtpoff", VK_DTPOFF)
285286 .Case("TLVP", VK_TLVP)
286287 .Case("tlvp", VK_TLVP)
288 .Case("IMGREL", VK_COFF_IMGREL32)
289 .Case("imgrel", VK_COFF_IMGREL32)
287290 .Default(VK_Invalid);
288291 }
289292
688688 ++Reloc.Symb->Relocations;
689689
690690 Reloc.Data.VirtualAddress += Fixup.getOffset();
691
692 unsigned FixupKind = Fixup.getKind();
693
694 if (CrossSection)
695 FixupKind = FK_PCRel_4;
696
697 Reloc.Data.Type = TargetObjectWriter->getRelocType(FixupKind);
691 Reloc.Data.Type = TargetObjectWriter->getRelocType(Target, Fixup,
692 CrossSection);
698693
699694 // FIXME: Can anyone explain what this does other than adjust for the size
700695 // of the offset?
88
99 #include "MCTargetDesc/X86FixupKinds.h"
1010 #include "MCTargetDesc/X86MCTargetDesc.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/MC/MCValue.h"
1113 #include "llvm/MC/MCWinCOFFObjectWriter.h"
1214 #include "llvm/Support/COFF.h"
1315 #include "llvm/Support/ErrorHandling.h"
2628 X86WinCOFFObjectWriter(bool Is64Bit_);
2729 ~X86WinCOFFObjectWriter();
2830
29 virtual unsigned getRelocType(unsigned FixupKind) const;
31 virtual unsigned getRelocType(const MCValue &Target,
32 const MCFixup &Fixup,
33 bool IsCrossSection) const LLVM_OVERRIDE;
3034 };
3135 }
3236
3741
3842 X86WinCOFFObjectWriter::~X86WinCOFFObjectWriter() {}
3943
40 unsigned X86WinCOFFObjectWriter::getRelocType(unsigned FixupKind) const {
44 unsigned X86WinCOFFObjectWriter::getRelocType(const MCValue &Target,
45 const MCFixup &Fixup,
46 bool IsCrossSection) const {
47 unsigned FixupKind = IsCrossSection ? FK_PCRel_4 : Fixup.getKind();
48
49 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
50 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
51
4152 switch (FixupKind) {
4253 case FK_PCRel_4:
4354 case X86::reloc_riprel_4byte:
4556 return Is64Bit ? COFF::IMAGE_REL_AMD64_REL32 : COFF::IMAGE_REL_I386_REL32;
4657 case FK_Data_4:
4758 case X86::reloc_signed_4byte:
59 if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
60 return Is64Bit ? COFF::IMAGE_REL_AMD64_ADDR32NB :
61 COFF::IMAGE_REL_I386_DIR32NB;
4862 return Is64Bit ? COFF::IMAGE_REL_AMD64_ADDR32 : COFF::IMAGE_REL_I386_DIR32;
4963 case FK_Data_8:
5064 if (Is64Bit)
0 // COFF Image-relative relocations
1 //
2 // Test that we produce image-relative relocations (IMAGE_REL_I386_DIR32NB
3 // and IMAGE_REL_AMD64_ADDR32NB) when accessing foo@imgrel.
4
5 // RUN: llvm-mc -filetype=obj -triple i686-pc-win32 %s | llvm-readobj -r | FileCheck --check-prefix=W32 %s
6 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-win32 %s | llvm-readobj -r | FileCheck --check-prefix=W64 %s
7
8 .data
9 foo:
10 .long 1
11
12 .text
13 mov foo@IMGREL(%ebx, %ecx, 4), %eax
14 mov foo@imgrel(%ebx, %ecx, 4), %eax
15
16 // W32: Relocations [
17 // W32-NEXT: Section (1) .text {
18 // W32-NEXT: 0x3 IMAGE_REL_I386_DIR32NB foo
19 // W32-NEXT: 0xA IMAGE_REL_I386_DIR32NB foo
20 // W32-NEXT: }
21 // W32-NEXT: ]
22
23 // W64: Relocations [
24 // W64-NEXT: Section (1) .text {
25 // W64-NEXT: 0x4 IMAGE_REL_AMD64_ADDR32NB foo
26 // W64-NEXT: 0xC IMAGE_REL_AMD64_ADDR32NB foo
27 // W64-NEXT: }
28 // W64-NEXT: ]