Home | History | Annotate | Download | only in X86
      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/ADT/StringExtras.h"
     12 #include "llvm/IR/Mangler.h"
     13 #include "llvm/IR/Operator.h"
     14 #include "llvm/MC/MCContext.h"
     15 #include "llvm/MC/MCExpr.h"
     16 #include "llvm/MC/MCSectionCOFF.h"
     17 #include "llvm/MC/MCSectionELF.h"
     18 #include "llvm/MC/MCValue.h"
     19 #include "llvm/Support/COFF.h"
     20 #include "llvm/Support/Dwarf.h"
     21 #include "llvm/Target/TargetLowering.h"
     22 
     23 using namespace llvm;
     24 using namespace dwarf;
     25 
     26 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
     27     const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
     28     const TargetMachine &TM, MachineModuleInfo *MMI,
     29     MCStreamer &Streamer) const {
     30 
     31   // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
     32   // is an indirect pc-relative reference.
     33   if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
     34     const MCSymbol *Sym = TM.getSymbol(GV, Mang);
     35     const MCExpr *Res =
     36       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
     37     const MCExpr *Four = MCConstantExpr::create(4, getContext());
     38     return MCBinaryExpr::createAdd(Res, Four, getContext());
     39   }
     40 
     41   return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
     42       GV, Encoding, Mang, TM, MMI, Streamer);
     43 }
     44 
     45 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
     46     const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
     47     MachineModuleInfo *MMI) const {
     48   return TM.getSymbol(GV, Mang);
     49 }
     50 
     51 const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
     52     const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
     53     MachineModuleInfo *MMI, MCStreamer &Streamer) const {
     54   // On Darwin/X86-64, we need to use foo@GOTPCREL+4 to access the got entry
     55   // from a data section. In case there's an additional offset, then use
     56   // foo@GOTPCREL+4+<offset>.
     57   unsigned FinalOff = Offset+MV.getConstant()+4;
     58   const MCExpr *Res =
     59     MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
     60   const MCExpr *Off = MCConstantExpr::create(FinalOff, getContext());
     61   return MCBinaryExpr::createAdd(Res, Off, getContext());
     62 }
     63 
     64 const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
     65     const MCSymbol *Sym) const {
     66   return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
     67 }
     68 
     69 void
     70 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
     71                                          const TargetMachine &TM) {
     72   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
     73   InitializeELF(TM.Options.UseInitArray);
     74 }
     75 
     76 const MCExpr *X86WindowsTargetObjectFile::lowerRelativeReference(
     77     const GlobalValue *LHS, const GlobalValue *RHS, Mangler &Mang,
     78     const TargetMachine &TM) const {
     79   // Our symbols should exist in address space zero, cowardly no-op if
     80   // otherwise.
     81   if (LHS->getType()->getPointerAddressSpace() != 0 ||
     82       RHS->getType()->getPointerAddressSpace() != 0)
     83     return nullptr;
     84 
     85   // Both ptrtoint instructions must wrap global objects:
     86   // - Only global variables are eligible for image relative relocations.
     87   // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
     88   // We expect __ImageBase to be a global variable without a section, externally
     89   // defined.
     90   //
     91   // It should look something like this: @__ImageBase = external constant i8
     92   if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
     93       LHS->isThreadLocal() || RHS->isThreadLocal() ||
     94       RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
     95       cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
     96     return nullptr;
     97 
     98   return MCSymbolRefExpr::create(
     99       TM.getSymbol(LHS, Mang), MCSymbolRefExpr::VK_COFF_IMGREL32, getContext());
    100 }
    101 
    102 static std::string APIntToHexString(const APInt &AI) {
    103   unsigned Width = (AI.getBitWidth() / 8) * 2;
    104   std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
    105   unsigned Size = HexString.size();
    106   assert(Width >= Size && "hex string is too large!");
    107   HexString.insert(HexString.begin(), Width - Size, '0');
    108 
    109   return HexString;
    110 }
    111 
    112 static std::string scalarConstantToHexString(const Constant *C) {
    113   Type *Ty = C->getType();
    114   if (isa<UndefValue>(C)) {
    115     return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
    116   } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
    117     return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
    118   } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
    119     return APIntToHexString(CI->getValue());
    120   } else {
    121     unsigned NumElements;
    122     if (isa<VectorType>(Ty))
    123       NumElements = Ty->getVectorNumElements();
    124     else
    125       NumElements = Ty->getArrayNumElements();
    126     std::string HexString;
    127     for (int I = NumElements - 1, E = -1; I != E; --I)
    128       HexString += scalarConstantToHexString(C->getAggregateElement(I));
    129     return HexString;
    130   }
    131 }
    132 
    133 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
    134     const DataLayout &DL, SectionKind Kind, const Constant *C,
    135     unsigned &Align) const {
    136   if (Kind.isMergeableConst() && C) {
    137     const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
    138                                      COFF::IMAGE_SCN_MEM_READ |
    139                                      COFF::IMAGE_SCN_LNK_COMDAT;
    140     std::string COMDATSymName;
    141     if (Kind.isMergeableConst4()) {
    142       if (Align <= 4) {
    143         COMDATSymName = "__real@" + scalarConstantToHexString(C);
    144         Align = 4;
    145       }
    146     } else if (Kind.isMergeableConst8()) {
    147       if (Align <= 8) {
    148         COMDATSymName = "__real@" + scalarConstantToHexString(C);
    149         Align = 8;
    150       }
    151     } else if (Kind.isMergeableConst16()) {
    152       if (Align <= 16) {
    153         COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
    154         Align = 16;
    155       }
    156     } else if (Kind.isMergeableConst32()) {
    157       if (Align <= 32) {
    158         COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
    159         Align = 32;
    160       }
    161     }
    162 
    163     if (!COMDATSymName.empty())
    164       return getContext().getCOFFSection(".rdata", Characteristics, Kind,
    165                                          COMDATSymName,
    166                                          COFF::IMAGE_COMDAT_SELECT_ANY);
    167   }
    168 
    169   return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
    170 }
    171