LLVM  4.0.0
X86TargetObjectFile.cpp
Go to the documentation of this file.
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"
22 
23 using namespace llvm;
24 using namespace dwarf;
25 
27  const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
28  MachineModuleInfo *MMI, MCStreamer &Streamer) const {
29 
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);
34  const MCExpr *Res =
36  const MCExpr *Four = MCConstantExpr::create(4, getContext());
37  return MCBinaryExpr::createAdd(Res, Four, getContext());
38  }
39 
41  GV, Encoding, TM, MMI, Streamer);
42 }
43 
45  const GlobalValue *GV, const TargetMachine &TM,
46  MachineModuleInfo *MMI) const {
47  return TM.getSymbol(GV);
48 }
49 
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 =
59  const MCExpr *Off = MCConstantExpr::create(FinalOff, getContext());
60  return MCBinaryExpr::createAdd(Res, Off, getContext());
61 }
62 
64  const MCSymbol *Sym) const {
65  return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
66 }
67 
68 void
69 X86FreeBSDTargetObjectFile::Initialize(MCContext &Ctx,
70  const TargetMachine &TM) {
72  InitializeELF(TM.Options.UseInitArray);
73 }
74 
75 void
76 X86FuchsiaTargetObjectFile::Initialize(MCContext &Ctx,
77  const TargetMachine &TM) {
79  InitializeELF(TM.Options.UseInitArray);
80 }
81 
82 void
83 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
84  const TargetMachine &TM) {
86  InitializeELF(TM.Options.UseInitArray);
87 }
88 
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
93  // otherwise.
94  if (LHS->getType()->getPointerAddressSpace() != 0 ||
95  RHS->getType()->getPointerAddressSpace() != 0)
96  return nullptr;
97 
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
102  // defined.
103  //
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())
109  return nullptr;
110 
111  return MCSymbolRefExpr::create(TM.getSymbol(LHS),
113  getContext());
114 }
115 
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');
122 
123  return HexString;
124 }
125 
126 static std::string scalarConstantToHexString(const Constant *C) {
127  Type *Ty = C->getType();
128  if (isa<UndefValue>(C)) {
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());
134  } else {
135  unsigned NumElements;
136  if (isa<VectorType>(Ty))
137  NumElements = Ty->getVectorNumElements();
138  else
139  NumElements = Ty->getArrayNumElements();
140  std::string HexString;
141  for (int I = NumElements - 1, E = -1; I != E; --I)
143  return HexString;
144  }
145 }
146 
147 MCSection *X86WindowsTargetObjectFile::getSectionForConstant(
148  const DataLayout &DL, SectionKind Kind, const Constant *C,
149  unsigned &Align) const {
150  if (Kind.isMergeableConst() && C) {
154  std::string COMDATSymName;
155  if (Kind.isMergeableConst4()) {
156  if (Align <= 4) {
157  COMDATSymName = "__real@" + scalarConstantToHexString(C);
158  Align = 4;
159  }
160  } else if (Kind.isMergeableConst8()) {
161  if (Align <= 8) {
162  COMDATSymName = "__real@" + scalarConstantToHexString(C);
163  Align = 8;
164  }
165  } else if (Kind.isMergeableConst16()) {
166  if (Align <= 16) {
167  COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
168  Align = 16;
169  }
170  } else if (Kind.isMergeableConst32()) {
171  if (Align <= 32) {
172  COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
173  Align = 32;
174  }
175  }
176 
177  if (!COMDATSymName.empty())
178  return getContext().getCOFFSection(".rdata", Characteristics, Kind,
179  COMDATSymName,
181  }
182 
183  return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
184 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
bool isMergeableConst() const
Definition: SectionKind.h:136
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const override
static std::string scalarConstantToHexString(const Constant *C)
MCSymbol * getSymbol(const GlobalValue *GV) const
static std::string APIntToHexString(const APInt &AI)
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
Definition: APInt.h:409
uint64_t getArrayNumElements() const
Definition: DerivedTypes.h:364
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
bool hasSection() const
Definition: GlobalValue.h:255
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
const MCExpr * getDebugThreadLocalSymbol(const MCSymbol *Sym) const override
Describe a TLS variable address within debug info.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Context object for machine code objects.
Definition: MCContext.h:51
bool isMergeableConst32() const
Definition: SectionKind.h:143
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:429
bool isMergeableConst16() const
Definition: SectionKind.h:142
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Streaming machine code generation interface.
Definition: MCStreamer.h:161
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:42
virtual MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const
Given a constant with the SectionKind, return a section that it should be placed in.
uint32_t Offset
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1255
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:265
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:232
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
bool hasExternalLinkage() const
Definition: GlobalValue.h:401
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
bool isMergeableConst8() const
Definition: SectionKind.h:141
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Class for arbitrary precision integers.
Definition: APInt.h:77
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:438
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:259
#define I(x, y, z)
Definition: MD5.cpp:54
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:270
const MCExpr * getIndirectSymViaGOTPCRel(const MCSymbol *Sym, const MCValue &MV, int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
Get MachO PC relative GOT entry relocation.
static std::string utohexstr(uint64_t X, bool LowerCase=false)
Definition: StringExtras.h:48
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int64_t getConstant() const
Definition: MCValue.h:46
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:108
bool isMergeableConst4() const
Definition: SectionKind.h:140
Primary interface to the complete machine description for the target machine.
static APInt getNullValue(unsigned numBits)
Get the '0' value.
Definition: APInt.h:465
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:479
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149
This class contains meta information specific to a module.
This file describes how to lower LLVM code to machine code.