WinCOFF: Transform IR expressions featuring __ImageBase into image relative relocations
[oota-llvm.git] / lib / Target / X86 / X86TargetObjectFile.cpp
1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "X86TargetObjectFile.h"
11 #include "llvm/IR/Mangler.h"
12 #include "llvm/IR/Operator.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/Support/Dwarf.h"
17
18 using namespace llvm;
19 using namespace dwarf;
20
21 const MCExpr *X86_64MachoTargetObjectFile::
22 getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
23                         MachineModuleInfo *MMI, unsigned Encoding,
24                         MCStreamer &Streamer) const {
25
26   // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
27   // is an indirect pc-relative reference.
28   if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
29     const MCSymbol *Sym = getSymbol(*Mang, GV);
30     const MCExpr *Res =
31       MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
32     const MCExpr *Four = MCConstantExpr::Create(4, getContext());
33     return MCBinaryExpr::CreateAdd(Res, Four, getContext());
34   }
35
36   return TargetLoweringObjectFileMachO::
37     getTTypeGlobalReference(GV, Mang, MMI, Encoding, Streamer);
38 }
39
40 MCSymbol *X86_64MachoTargetObjectFile::
41 getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
42                         MachineModuleInfo *MMI) const {
43   return getSymbol(*Mang, GV);
44 }
45
46 void
47 X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
48   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
49   InitializeELF(TM.Options.UseInitArray);
50 }
51
52 const MCExpr *
53 X86LinuxTargetObjectFile::getDebugThreadLocalSymbol(
54     const MCSymbol *Sym) const {
55   return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
56 }
57
58 const MCExpr *
59 X86WindowsTargetObjectFile::getExecutableRelativeSymbol(const ConstantExpr *CE,
60                                                         Mangler *Mang) const {
61   // We are looking for the difference of two symbols, need a subtraction
62   // operation.
63   const SubOperator *Sub = dyn_cast<SubOperator>(CE);
64   if (!Sub)
65     return 0;
66
67   // Symbols must first be numbers before we can subtract them, we need to see a
68   // ptrtoint on both subtraction operands.
69   const PtrToIntOperator *SubLHS =
70       dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
71   const PtrToIntOperator *SubRHS =
72       dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
73   if (!SubLHS || !SubRHS)
74     return 0;
75
76   // Our symbols should exist in address space zero, cowardly no-op if
77   // otherwise.
78   if (SubLHS->getPointerAddressSpace() != 0 ||
79       SubRHS->getPointerAddressSpace() != 0)
80     return 0;
81
82   // Both ptrtoint instructions must wrap global variables:
83   // - Only global variables are eligible for image relative relocations.
84   // - The subtrahend refers to the special symbol __ImageBase, a global.
85   const GlobalVariable *GVLHS =
86       dyn_cast<GlobalVariable>(SubLHS->getPointerOperand());
87   const GlobalVariable *GVRHS =
88       dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
89   if (!GVLHS || !GVRHS)
90     return 0;
91
92   // We expect __ImageBase to be a global variable without a section, externally
93   // defined.
94   //
95   // It should look something like this: @__ImageBase = external constant i8
96   if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
97       !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
98       GVRHS->hasSection())
99     return 0;
100
101   // An image-relative, thread-local, symbol makes no sense.
102   if (GVLHS->isThreadLocal())
103     return 0;
104
105   return MCSymbolRefExpr::Create(
106       getSymbol(*Mang, GVLHS), MCSymbolRefExpr::VK_COFF_IMGREL32, getContext());
107 }