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"
|
121
|
12 #include "llvm/BinaryFormat/COFF.h"
|
|
13 #include "llvm/BinaryFormat/Dwarf.h"
|
77
|
14 #include "llvm/IR/Mangler.h"
|
|
15 #include "llvm/IR/Operator.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/MC/MCContext.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/MC/MCExpr.h"
|
77
|
18 #include "llvm/MC/MCSectionCOFF.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #include "llvm/MC/MCSectionELF.h"
|
95
|
20 #include "llvm/MC/MCValue.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(
|
120
|
27 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
|
|
28 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 // 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
|
31 // is an indirect pc-relative reference.
|
77
|
32 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
|
120
|
33 const MCSymbol *Sym = TM.getSymbol(GV);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 const MCExpr *Res =
|
95
|
35 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
|
|
36 const MCExpr *Four = MCConstantExpr::create(4, getContext());
|
|
37 return MCBinaryExpr::createAdd(Res, Four, getContext());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39
|
77
|
40 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
|
120
|
41 GV, Encoding, TM, MMI, Streamer);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
77
|
44 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
|
120
|
45 const GlobalValue *GV, const TargetMachine &TM,
|
77
|
46 MachineModuleInfo *MMI) const {
|
120
|
47 return TM.getSymbol(GV);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49
|
95
|
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;
|
|
57 const MCExpr *Res =
|
|
58 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
|
|
59 const MCExpr *Off = MCConstantExpr::create(FinalOff, getContext());
|
|
60 return MCBinaryExpr::createAdd(Res, Off, getContext());
|
|
61 }
|
|
62
|
|
63 const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
|
|
64 const MCSymbol *Sym) const {
|
|
65 return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
|
|
66 }
|
|
67
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 void
|
120
|
69 X86FreeBSDTargetObjectFile::Initialize(MCContext &Ctx,
|
|
70 const TargetMachine &TM) {
|
|
71 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
|
72 InitializeELF(TM.Options.UseInitArray);
|
|
73 }
|
|
74
|
|
75 void
|
|
76 X86FuchsiaTargetObjectFile::Initialize(MCContext &Ctx,
|
|
77 const TargetMachine &TM) {
|
|
78 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
|
79 InitializeELF(TM.Options.UseInitArray);
|
|
80 }
|
|
81
|
|
82 void
|
95
|
83 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
|
|
84 const TargetMachine &TM) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 InitializeELF(TM.Options.UseInitArray);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88
|
121
|
89 void X86SolarisTargetObjectFile::Initialize(MCContext &Ctx,
|
|
90 const TargetMachine &TM) {
|
|
91 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
|
92 InitializeELF(TM.Options.UseInitArray);
|
|
93 }
|
|
94
|
120
|
95 const MCExpr *X86WindowsTargetObjectFile::lowerRelativeReference(
|
|
96 const GlobalValue *LHS, const GlobalValue *RHS,
|
|
97 const TargetMachine &TM) const {
|
77
|
98 // Our symbols should exist in address space zero, cowardly no-op if
|
|
99 // otherwise.
|
120
|
100 if (LHS->getType()->getPointerAddressSpace() != 0 ||
|
|
101 RHS->getType()->getPointerAddressSpace() != 0)
|
77
|
102 return nullptr;
|
|
103
|
95
|
104 // Both ptrtoint instructions must wrap global objects:
|
77
|
105 // - Only global variables are eligible for image relative relocations.
|
95
|
106 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
|
77
|
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
|
120
|
111 if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
|
|
112 LHS->isThreadLocal() || RHS->isThreadLocal() ||
|
|
113 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
|
|
114 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
|
77
|
115 return nullptr;
|
|
116
|
120
|
117 return MCSymbolRefExpr::create(TM.getSymbol(LHS),
|
77
|
118 MCSymbolRefExpr::VK_COFF_IMGREL32,
|
|
119 getContext());
|
|
120 }
|
|
121
|
|
122 static std::string APIntToHexString(const APInt &AI) {
|
|
123 unsigned Width = (AI.getBitWidth() / 8) * 2;
|
|
124 std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
|
|
125 unsigned Size = HexString.size();
|
|
126 assert(Width >= Size && "hex string is too large!");
|
|
127 HexString.insert(HexString.begin(), Width - Size, '0');
|
|
128
|
|
129 return HexString;
|
|
130 }
|
|
131
|
|
132 static std::string scalarConstantToHexString(const Constant *C) {
|
|
133 Type *Ty = C->getType();
|
|
134 if (isa<UndefValue>(C)) {
|
95
|
135 return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
|
|
136 } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
|
|
137 return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
|
|
138 } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
|
|
139 return APIntToHexString(CI->getValue());
|
77
|
140 } else {
|
95
|
141 unsigned NumElements;
|
|
142 if (isa<VectorType>(Ty))
|
|
143 NumElements = Ty->getVectorNumElements();
|
|
144 else
|
|
145 NumElements = Ty->getArrayNumElements();
|
|
146 std::string HexString;
|
|
147 for (int I = NumElements - 1, E = -1; I != E; --I)
|
|
148 HexString += scalarConstantToHexString(C->getAggregateElement(I));
|
|
149 return HexString;
|
77
|
150 }
|
|
151 }
|
|
152
|
95
|
153 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
|
120
|
154 const DataLayout &DL, SectionKind Kind, const Constant *C,
|
|
155 unsigned &Align) const {
|
95
|
156 if (Kind.isMergeableConst() && C) {
|
|
157 const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
|
|
158 COFF::IMAGE_SCN_MEM_READ |
|
|
159 COFF::IMAGE_SCN_LNK_COMDAT;
|
|
160 std::string COMDATSymName;
|
120
|
161 if (Kind.isMergeableConst4()) {
|
|
162 if (Align <= 4) {
|
|
163 COMDATSymName = "__real@" + scalarConstantToHexString(C);
|
|
164 Align = 4;
|
|
165 }
|
|
166 } else if (Kind.isMergeableConst8()) {
|
|
167 if (Align <= 8) {
|
|
168 COMDATSymName = "__real@" + scalarConstantToHexString(C);
|
|
169 Align = 8;
|
|
170 }
|
|
171 } else if (Kind.isMergeableConst16()) {
|
|
172 if (Align <= 16) {
|
|
173 COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
|
|
174 Align = 16;
|
|
175 }
|
|
176 } else if (Kind.isMergeableConst32()) {
|
|
177 if (Align <= 32) {
|
|
178 COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
|
|
179 Align = 32;
|
|
180 }
|
|
181 }
|
95
|
182
|
|
183 if (!COMDATSymName.empty())
|
|
184 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
|
|
185 COMDATSymName,
|
|
186 COFF::IMAGE_COMDAT_SELECT_ANY);
|
77
|
187 }
|
|
188
|
120
|
189 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
|
77
|
190 }
|