LLVM  3.7.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/Dwarf.h"
21 
22 using namespace llvm;
23 using namespace dwarf;
24 
26  const GlobalValue *GV, unsigned Encoding, Mangler &Mang,
27  const TargetMachine &TM, MachineModuleInfo *MMI,
28  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, Mang);
34  const MCExpr *Res =
36  const MCExpr *Four = MCConstantExpr::create(4, getContext());
37  return MCBinaryExpr::createAdd(Res, Four, getContext());
38  }
39 
41  GV, Encoding, Mang, TM, MMI, Streamer);
42 }
43 
45  const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM,
46  MachineModuleInfo *MMI) const {
47  return TM.getSymbol(GV, Mang);
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 
63 const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
64  const MCSymbol *Sym) const {
65  return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext());
66 }
67 
68 void
69 X86LinuxNaClTargetObjectFile::Initialize(MCContext &Ctx,
70  const TargetMachine &TM) {
72  InitializeELF(TM.Options.UseInitArray);
73 }
74 
75 const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol(
76  const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const {
77  // We are looking for the difference of two symbols, need a subtraction
78  // operation.
79  const SubOperator *Sub = dyn_cast<SubOperator>(CE);
80  if (!Sub)
81  return nullptr;
82 
83  // Symbols must first be numbers before we can subtract them, we need to see a
84  // ptrtoint on both subtraction operands.
85  const PtrToIntOperator *SubLHS =
87  const PtrToIntOperator *SubRHS =
89  if (!SubLHS || !SubRHS)
90  return nullptr;
91 
92  // Our symbols should exist in address space zero, cowardly no-op if
93  // otherwise.
94  if (SubLHS->getPointerAddressSpace() != 0 ||
95  SubRHS->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  const auto *GOLHS = dyn_cast<GlobalObject>(SubLHS->getPointerOperand());
102  const auto *GVRHS = dyn_cast<GlobalVariable>(SubRHS->getPointerOperand());
103  if (!GOLHS || !GVRHS)
104  return nullptr;
105 
106  // We expect __ImageBase to be a global variable without a section, externally
107  // defined.
108  //
109  // It should look something like this: @__ImageBase = external constant i8
110  if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" ||
111  !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() ||
112  GVRHS->hasSection())
113  return nullptr;
114 
115  // An image-relative, thread-local, symbol makes no sense.
116  if (GOLHS->isThreadLocal())
117  return nullptr;
118 
119  return MCSymbolRefExpr::create(TM.getSymbol(GOLHS, Mang),
121  getContext());
122 }
123 
124 static std::string APIntToHexString(const APInt &AI) {
125  unsigned Width = (AI.getBitWidth() / 8) * 2;
126  std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
127  unsigned Size = HexString.size();
128  assert(Width >= Size && "hex string is too large!");
129  HexString.insert(HexString.begin(), Width - Size, '0');
130 
131  return HexString;
132 }
133 
134 static std::string scalarConstantToHexString(const Constant *C) {
135  Type *Ty = C->getType();
136  if (isa<UndefValue>(C)) {
138  } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
139  return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
140  } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
141  return APIntToHexString(CI->getValue());
142  } else {
143  unsigned NumElements;
144  if (isa<VectorType>(Ty))
145  NumElements = Ty->getVectorNumElements();
146  else
147  NumElements = Ty->getArrayNumElements();
148  std::string HexString;
149  for (int I = NumElements - 1, E = -1; I != E; --I)
151  return HexString;
152  }
153 }
154 
155 MCSection *
156 X86WindowsTargetObjectFile::getSectionForConstant(SectionKind Kind,
157  const Constant *C) const {
158  if (Kind.isMergeableConst() && C) {
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,
172  }
173 
175 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, MachineModuleInfo *MMI) const override
bool isMergeableConst() const
Definition: SectionKind.h:152
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:315
This represents an "assembler immediate".
Definition: MCValue.h:44
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
static std::string scalarConstantToHexString(const Constant *C)
static std::string APIntToHexString(const APInt &AI)
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:404
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:475
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, Mangler &Mang, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
const MCExpr * getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding, Mangler &Mang, const TargetMachine &TM, MachineModuleInfo *MMI, MCStreamer &Streamer) const override
The mach-o version of this method defaults to returning a stub reference.
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Value * getPointerOperand()
Definition: Operator.h:459
Context object for machine code objects.
Definition: MCContext.h:48
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:446
bool isMergeableConst16() const
Definition: SectionKind.h:158
Streaming machine code generation interface.
Definition: MCStreamer.h:157
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:41
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1273
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:28
Value * getOperand(unsigned i) const
Definition: User.h:118
virtual MCSection * getSectionForConstant(SectionKind Kind, const Constant *C) const
Given a constant with the SectionKind, return a section that it should be placed in.
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
Definition: Constants.cpp:250
MCSymbol * getSymbol(const GlobalValue *GV, Mangler &Mang) const
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
unsigned getVectorNumElements() const
Definition: Type.cpp:212
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
bool isMergeableConst8() const
Definition: SectionKind.h:157
Class for arbitrary precision integers.
Definition: APInt.h:73
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
#define I(x, y, z)
Definition: MD5.cpp:54
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:268
uint64_t getArrayNumElements() const
Definition: Type.cpp:208
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:72
int64_t getConstant() const
Definition: MCValue.h:50
const ARM::ArchExtKind Kind
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:121
bool isMergeableConst4() const
Definition: SectionKind.h:156
Primary interface to the complete machine description for the target machine.
static APInt getNullValue(unsigned numBits)
Get the '0' value.
Definition: APInt.h:460
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:150
MachineModuleInfo - This class contains meta information specific to a module.
This file describes how to lower LLVM code to machine code.