comparison lib/Target/X86/X86TargetObjectFile.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===// 1 //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 8
10 #include "X86TargetObjectFile.h" 9 #include "X86TargetObjectFile.h"
11 #include "llvm/ADT/StringExtras.h" 10 #include "llvm/ADT/StringExtras.h"
89 void X86SolarisTargetObjectFile::Initialize(MCContext &Ctx, 88 void X86SolarisTargetObjectFile::Initialize(MCContext &Ctx,
90 const TargetMachine &TM) { 89 const TargetMachine &TM) {
91 TargetLoweringObjectFileELF::Initialize(Ctx, TM); 90 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
92 InitializeELF(TM.Options.UseInitArray); 91 InitializeELF(TM.Options.UseInitArray);
93 } 92 }
94
95 const MCExpr *X86WindowsTargetObjectFile::lowerRelativeReference(
96 const GlobalValue *LHS, const GlobalValue *RHS,
97 const TargetMachine &TM) const {
98 // Our symbols should exist in address space zero, cowardly no-op if
99 // otherwise.
100 if (LHS->getType()->getPointerAddressSpace() != 0 ||
101 RHS->getType()->getPointerAddressSpace() != 0)
102 return nullptr;
103
104 // Both ptrtoint instructions must wrap global objects:
105 // - Only global variables are eligible for image relative relocations.
106 // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
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 (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
112 LHS->isThreadLocal() || RHS->isThreadLocal() ||
113 RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
114 cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
115 return nullptr;
116
117 return MCSymbolRefExpr::create(TM.getSymbol(LHS),
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)) {
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());
140 } else {
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;
150 }
151 }
152
153 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
154 const DataLayout &DL, SectionKind Kind, const Constant *C,
155 unsigned &Align) const {
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;
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 }
182
183 if (!COMDATSymName.empty())
184 return getContext().getCOFFSection(".rdata", Characteristics, Kind,
185 COMDATSymName,
186 COFF::IMAGE_COMDAT_SELECT_ANY);
187 }
188
189 return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
190 }