LLVM API Documentation
00001 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "X86TargetObjectFile.h" 00011 #include "llvm/ADT/StringExtras.h" 00012 #include "llvm/IR/Mangler.h" 00013 #include "llvm/IR/Operator.h" 00014 #include "llvm/MC/MCContext.h" 00015 #include "llvm/MC/MCExpr.h" 00016 #include "llvm/MC/MCSectionCOFF.h" 00017 #include "llvm/MC/MCSectionELF.h" 00018 #include "llvm/Support/Dwarf.h" 00019 #include "llvm/Target/TargetLowering.h" 00020 00021 using namespace llvm; 00022 using namespace dwarf; 00023 00024 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference( 00025 const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 00026 const TargetMachine &TM, MachineModuleInfo *MMI, 00027 MCStreamer &Streamer) const { 00028 00029 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which 00030 // is an indirect pc-relative reference. 00031 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) { 00032 const MCSymbol *Sym = TM.getSymbol(GV, Mang); 00033 const MCExpr *Res = 00034 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext()); 00035 const MCExpr *Four = MCConstantExpr::Create(4, getContext()); 00036 return MCBinaryExpr::CreateAdd(Res, Four, getContext()); 00037 } 00038 00039 return TargetLoweringObjectFileMachO::getTTypeGlobalReference( 00040 GV, Encoding, Mang, TM, MMI, Streamer); 00041 } 00042 00043 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol( 00044 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 00045 MachineModuleInfo *MMI) const { 00046 return TM.getSymbol(GV, Mang); 00047 } 00048 00049 void 00050 X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) { 00051 TargetLoweringObjectFileELF::Initialize(Ctx, TM); 00052 InitializeELF(TM.Options.UseInitArray); 00053 } 00054 00055 const MCExpr * 00056 X86LinuxTargetObjectFile::getDebugThreadLocalSymbol( 00057 const MCSymbol *Sym) const { 00058 return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext()); 00059 } 00060 00061 const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol( 00062 const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const { 00063 // We are looking for the difference of two symbols, need a subtraction 00064 // operation. 00065 const SubOperator *Sub = dyn_cast<SubOperator>(CE); 00066 if (!Sub) 00067 return nullptr; 00068 00069 // Symbols must first be numbers before we can subtract them, we need to see a 00070 // ptrtoint on both subtraction operands. 00071 const PtrToIntOperator *SubLHS = 00072 dyn_cast<PtrToIntOperator>(Sub->getOperand(0)); 00073 const PtrToIntOperator *SubRHS = 00074 dyn_cast<PtrToIntOperator>(Sub->getOperand(1)); 00075 if (!SubLHS || !SubRHS) 00076 return nullptr; 00077 00078 // Our symbols should exist in address space zero, cowardly no-op if 00079 // otherwise. 00080 if (SubLHS->getPointerAddressSpace() != 0 || 00081 SubRHS->getPointerAddressSpace() != 0) 00082 return nullptr; 00083 00084 // Both ptrtoint instructions must wrap global variables: 00085 // - Only global variables are eligible for image relative relocations. 00086 // - The subtrahend refers to the special symbol __ImageBase, a global. 00087 const GlobalVariable *GVLHS = 00088 dyn_cast<GlobalVariable>(SubLHS->getPointerOperand()); 00089 const GlobalVariable *GVRHS = 00090 dyn_cast<GlobalVariable>(SubRHS->getPointerOperand()); 00091 if (!GVLHS || !GVRHS) 00092 return nullptr; 00093 00094 // We expect __ImageBase to be a global variable without a section, externally 00095 // defined. 00096 // 00097 // It should look something like this: @__ImageBase = external constant i8 00098 if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" || 00099 !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() || 00100 GVRHS->hasSection()) 00101 return nullptr; 00102 00103 // An image-relative, thread-local, symbol makes no sense. 00104 if (GVLHS->isThreadLocal()) 00105 return nullptr; 00106 00107 return MCSymbolRefExpr::Create(TM.getSymbol(GVLHS, Mang), 00108 MCSymbolRefExpr::VK_COFF_IMGREL32, 00109 getContext()); 00110 } 00111 00112 static std::string APIntToHexString(const APInt &AI) { 00113 unsigned Width = (AI.getBitWidth() / 8) * 2; 00114 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true); 00115 unsigned Size = HexString.size(); 00116 assert(Width >= Size && "hex string is too large!"); 00117 HexString.insert(HexString.begin(), Width - Size, '0'); 00118 00119 return HexString; 00120 } 00121 00122 00123 static std::string scalarConstantToHexString(const Constant *C) { 00124 Type *Ty = C->getType(); 00125 APInt AI; 00126 if (isa<UndefValue>(C)) { 00127 AI = APInt(Ty->getPrimitiveSizeInBits(), /*val=*/0); 00128 } else if (Ty->isFloatTy() || Ty->isDoubleTy()) { 00129 const auto *CFP = cast<ConstantFP>(C); 00130 AI = CFP->getValueAPF().bitcastToAPInt(); 00131 } else if (Ty->isIntegerTy()) { 00132 const auto *CI = cast<ConstantInt>(C); 00133 AI = CI->getValue(); 00134 } else { 00135 llvm_unreachable("unexpected constant pool element type!"); 00136 } 00137 return APIntToHexString(AI); 00138 } 00139 00140 const MCSection * 00141 X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind, 00142 const Constant *C) const { 00143 if (Kind.isReadOnly()) { 00144 if (C) { 00145 Type *Ty = C->getType(); 00146 SmallString<32> COMDATSymName; 00147 if (Ty->isFloatTy() || Ty->isDoubleTy()) { 00148 COMDATSymName = "__real@"; 00149 COMDATSymName += scalarConstantToHexString(C); 00150 } else if (const auto *VTy = dyn_cast<VectorType>(Ty)) { 00151 uint64_t NumBits = VTy->getBitWidth(); 00152 if (NumBits == 128 || NumBits == 256) { 00153 COMDATSymName = NumBits == 128 ? "__xmm@" : "__ymm@"; 00154 for (int I = VTy->getNumElements() - 1, E = -1; I != E; --I) 00155 COMDATSymName += 00156 scalarConstantToHexString(C->getAggregateElement(I)); 00157 } 00158 } 00159 if (!COMDATSymName.empty()) { 00160 unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | 00161 COFF::IMAGE_SCN_MEM_READ | 00162 COFF::IMAGE_SCN_LNK_COMDAT; 00163 return getContext().getCOFFSection(".rdata", Characteristics, Kind, 00164 COMDATSymName, 00165 COFF::IMAGE_COMDAT_SELECT_ANY); 00166 } 00167 } 00168 } 00169 00170 return TargetLoweringObjectFile::getSectionForConstant(Kind, C); 00171 }