1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
24 using namespace dwarf;
26 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
27 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
28 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
30 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
31 // is an indirect pc-relative reference.
32 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
33 const MCSymbol *Sym = TM.getSymbol(GV);
35 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
36 const MCExpr *Four = MCConstantExpr::create(4, getContext());
37 return MCBinaryExpr::createAdd(Res, Four, getContext());
40 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
41 GV, Encoding, TM, MMI, Streamer);
44 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
45 const GlobalValue *GV, const TargetMachine &TM,
46 MachineModuleInfo *MMI) const {
47 return TM.getSymbol(GV);
50 const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
51 const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
52 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
53 // On Darwin/X86-64, we need to use foo@GOTPCREL+4 to access the got entry
54 // from a data section. In case there's an additional offset, then use
55 // foo@GOTPCREL+4+<offset>.
56 unsigned FinalOff = Offset+MV.getConstant()+4;
58 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
59 const MCExpr *Off = MCConstantExpr::create(FinalOff, getContext());
60 return MCBinaryExpr::createAdd(Res, Off, getContext());
63 const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
64 const MCSymbol *Sym) const {
65 return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
69 X86FreeBSDTargetObjectFile::Initialize(MCContext &Ctx,
70 const TargetMachine &TM) {
71 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
72 InitializeELF(TM.Options.UseInitArray);
76 X86FuchsiaTargetObjectFile::Initialize(MCContext &Ctx,
77 const TargetMachine &TM) {
78 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
79 InitializeELF(TM.Options.UseInitArray);
83 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
84 const TargetMachine &TM) {
85 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
86 InitializeELF(TM.Options.UseInitArray);
89 const MCExpr *X86WindowsTargetObjectFile::lowerRelativeReference(
90 const GlobalValue *LHS, const GlobalValue *RHS,
91 const TargetMachine &TM) const {
92 // Our symbols should exist in address space zero, cowardly no-op if
94 if (LHS->getType()->getPointerAddressSpace() != 0 ||
95 RHS->getType()->getPointerAddressSpace() != 0)
98 // Both ptrtoint instructions must wrap global objects:
99 // - Only global variables are eligible for image relative relocations.
100 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
101 // We expect __ImageBase to be a global variable without a section, externally
104 // It should look something like this: @__ImageBase = external constant i8
105 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
106 LHS->isThreadLocal() || RHS->isThreadLocal() ||
107 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
108 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
111 return MCSymbolRefExpr::create(TM.getSymbol(LHS),
112 MCSymbolRefExpr::VK_COFF_IMGREL32,
116 static std::string APIntToHexString(const APInt &AI) {
117 unsigned Width = (AI.getBitWidth() / 8) * 2;
118 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
119 unsigned Size = HexString.size();
120 assert(Width >= Size && "hex string is too large!");
121 HexString.insert(HexString.begin(), Width - Size, '0');
126 static std::string scalarConstantToHexString(const Constant *C) {
127 Type *Ty = C->getType();
128 if (isa<UndefValue>(C)) {
129 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
130 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
131 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
132 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
133 return APIntToHexString(CI->getValue());
135 unsigned NumElements;
136 if (isa<VectorType>(Ty))
137 NumElements = Ty->getVectorNumElements();
139 NumElements = Ty->getArrayNumElements();
140 std::string HexString;
141 for (int I = NumElements - 1, E = -1; I != E; --I)
142 HexString += scalarConstantToHexString(C->getAggregateElement(I));
147 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
148 const DataLayout &DL, SectionKind Kind, const Constant *C,
149 unsigned &Align) const {
150 if (Kind.isMergeableConst() && C) {
151 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
152 COFF::IMAGE_SCN_MEM_READ |
153 COFF::IMAGE_SCN_LNK_COMDAT;
154 std::string COMDATSymName;
155 if (Kind.isMergeableConst4()) {
157 COMDATSymName = "__real@" + scalarConstantToHexString(C);
160 } else if (Kind.isMergeableConst8()) {
162 COMDATSymName = "__real@" + scalarConstantToHexString(C);
165 } else if (Kind.isMergeableConst16()) {
167 COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
170 } else if (Kind.isMergeableConst32()) {
172 COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
177 if (!COMDATSymName.empty())
178 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
180 COFF::IMAGE_COMDAT_SELECT_ANY);
183 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);