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