0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 #include "X86TargetObjectFile.h"
|
77
|
11 #include "llvm/ADT/StringExtras.h"
|
|
12 #include "llvm/IR/Mangler.h"
|
|
13 #include "llvm/IR/Operator.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #include "llvm/MC/MCContext.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "llvm/MC/MCExpr.h"
|
77
|
16 #include "llvm/MC/MCSectionCOFF.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/MC/MCSectionELF.h"
|
95
|
18 #include "llvm/MC/MCValue.h"
|
|
19 #include "llvm/Support/COFF.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/Support/Dwarf.h"
|
77
|
21 #include "llvm/Target/TargetLowering.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 using namespace dwarf;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25
|
77
|
26 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
|
|
27 const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
|
|
28 const TargetMachine &TM, MachineModuleInfo *MMI,
|
|
29 MCStreamer &Streamer) const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 // is an indirect pc-relative reference.
|
77
|
33 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
|
|
34 const MCSymbol *Sym = TM.getSymbol(GV, Mang);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 const MCExpr *Res =
|
95
|
36 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
|
|
37 const MCExpr *Four = MCConstantExpr::create(4, getContext());
|
|
38 return MCBinaryExpr::createAdd(Res, Four, getContext());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40
|
77
|
41 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
|
|
42 GV, Encoding, Mang, TM, MMI, Streamer);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44
|
77
|
45 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
|
|
46 const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
|
|
47 MachineModuleInfo *MMI) const {
|
|
48 return TM.getSymbol(GV, Mang);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50
|
95
|
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
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 void
|
95
|
70 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
|
|
71 const TargetMachine &TM) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 InitializeELF(TM.Options.UseInitArray);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75
|
77
|
76 const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
|
|
77 const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
|
|
78 // We are looking for the difference of two symbols, need a subtraction
|
|
79 // operation.
|
|
80 const SubOperator *Sub = dyn_cast<SubOperator>(CE);
|
|
81 if (!Sub)
|
|
82 return nullptr;
|
|
83
|
|
84 // Symbols must first be numbers before we can subtract them, we need to see a
|
|
85 // ptrtoint on both subtraction operands.
|
|
86 const PtrToIntOperator *SubLHS =
|
|
87 dyn_cast<PtrToIntOperator>(Sub->getOperand(0));
|
|
88 const PtrToIntOperator *SubRHS =
|
|
89 dyn_cast<PtrToIntOperator>(Sub->getOperand(1));
|
|
90 if (!SubLHS || !SubRHS)
|
|
91 return nullptr;
|
|
92
|
|
93 // Our symbols should exist in address space zero, cowardly no-op if
|
|
94 // otherwise.
|
|
95 if (SubLHS->getPointerAddressSpace() != 0 ||
|
|
96 SubRHS->getPointerAddressSpace() != 0)
|
|
97 return nullptr;
|
|
98
|
95
|
99 // Both ptrtoint instructions must wrap global objects:
|
77
|
100 // - Only global variables are eligible for image relative relocations.
|
95
|
101 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
|
|
102 const auto *GOLHS = dyn_cast<GlobalObject>(SubLHS->getPointerOperand());
|
|
103 const auto *GVRHS = dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
|
|
104 if (!GOLHS || !GVRHS)
|
77
|
105 return nullptr;
|
|
106
|
|
107 // We expect __ImageBase to be a global variable without a section, externally
|
|
108 // defined.
|
|
109 //
|
|
110 // It should look something like this: @__ImageBase = external constant i8
|
|
111 if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
|
|
112 !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
|
|
113 GVRHS->hasSection())
|
|
114 return nullptr;
|
|
115
|
|
116 // An image-relative, thread-local, symbol makes no sense.
|
95
|
117 if (GOLHS->isThreadLocal())
|
77
|
118 return nullptr;
|
|
119
|
95
|
120 return MCSymbolRefExpr::create(TM.getSymbol(GOLHS, Mang),
|
77
|
121 MCSymbolRefExpr::VK_COFF_IMGREL32,
|
|
122 getContext());
|
|
123 }
|
|
124
|
|
125 static std::string APIntToHexString(const APInt &AI) {
|
|
126 unsigned Width = (AI.getBitWidth() / 8) * 2;
|
|
127 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
|
|
128 unsigned Size = HexString.size();
|
|
129 assert(Width >= Size && "hex string is too large!");
|
|
130 HexString.insert(HexString.begin(), Width - Size, '0');
|
|
131
|
|
132 return HexString;
|
|
133 }
|
|
134
|
|
135 static std::string scalarConstantToHexString(const Constant *C) {
|
|
136 Type *Ty = C->getType();
|
|
137 if (isa<UndefValue>(C)) {
|
95
|
138 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
|
|
139 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
|
|
140 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
|
|
141 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
|
|
142 return APIntToHexString(CI->getValue());
|
77
|
143 } else {
|
95
|
144 unsigned NumElements;
|
|
145 if (isa<VectorType>(Ty))
|
|
146 NumElements = Ty->getVectorNumElements();
|
|
147 else
|
|
148 NumElements = Ty->getArrayNumElements();
|
|
149 std::string HexString;
|
|
150 for (int I = NumElements - 1, E = -1; I != E; --I)
|
|
151 HexString += scalarConstantToHexString(C->getAggregateElement(I));
|
|
152 return HexString;
|
77
|
153 }
|
|
154 }
|
|
155
|
95
|
156 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
|
|
157 const DataLayout &DL, SectionKind Kind, const Constant *C) const {
|
|
158 if (Kind.isMergeableConst() && C) {
|
|
159 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
|
|
160 COFF::IMAGE_SCN_MEM_READ |
|
|
161 COFF::IMAGE_SCN_LNK_COMDAT;
|
|
162 std::string COMDATSymName;
|
|
163 if (Kind.isMergeableConst4() || Kind.isMergeableConst8())
|
|
164 COMDATSymName = "__real@" + scalarConstantToHexString(C);
|
|
165 else if (Kind.isMergeableConst16())
|
|
166 COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
|
|
167
|
|
168 if (!COMDATSymName.empty())
|
|
169 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
|
|
170 COMDATSymName,
|
|
171 COFF::IMAGE_COMDAT_SELECT_ANY);
|
77
|
172 }
|
|
173
|
95
|
174 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C);
|
77
|
175 }
|