1//===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "X86TargetObjectFile.h"
10#include "llvm/BinaryFormat/Dwarf.h"
11#include "llvm/MC/MCExpr.h"
12#include "llvm/MC/MCValue.h"
13#include "llvm/Target/TargetMachine.h"
14
15using namespace llvm;
16using namespace dwarf;
17
18const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference(
19 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
20 MachineModuleInfo *MMI, MCStreamer &Streamer) const {
21
22 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which
23 // is an indirect pc-relative reference.
24 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) {
25 const MCSymbol *Sym = TM.getSymbol(GV);
26 const MCExpr *Res =
27 MCSymbolRefExpr::create(Symbol: Sym, Kind: MCSymbolRefExpr::VK_GOTPCREL, Ctx&: getContext());
28 const MCExpr *Four = MCConstantExpr::create(Value: 4, Ctx&: getContext());
29 return MCBinaryExpr::createAdd(LHS: Res, RHS: Four, Ctx&: getContext());
30 }
31
32 return TargetLoweringObjectFileMachO::getTTypeGlobalReference(
33 GV, Encoding, TM, MMI, Streamer);
34}
35
36MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol(
37 const GlobalValue *GV, const TargetMachine &TM,
38 MachineModuleInfo *MMI) const {
39 return TM.getSymbol(GV);
40}
41
42const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel(
43 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
44 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
45 // On Darwin/X86-64, we need to use foo@GOTPCREL+4 to access the got entry
46 // from a data section. In case there's an additional offset, then use
47 // foo@GOTPCREL+4+<offset>.
48 unsigned FinalOff = Offset+MV.getConstant()+4;
49 const MCExpr *Res =
50 MCSymbolRefExpr::create(Symbol: Sym, Kind: MCSymbolRefExpr::VK_GOTPCREL, Ctx&: getContext());
51 const MCExpr *Off = MCConstantExpr::create(Value: FinalOff, Ctx&: getContext());
52 return MCBinaryExpr::createAdd(LHS: Res, RHS: Off, Ctx&: getContext());
53}
54
55const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol(
56 const MCSymbol *Sym) const {
57 return MCSymbolRefExpr::create(Symbol: Sym, Kind: MCSymbolRefExpr::VK_DTPOFF, Ctx&: getContext());
58}
59
60const MCExpr *X86_64ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
61 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV,
62 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
63 int64_t FinalOffset = Offset + MV.getConstant();
64 const MCExpr *Res =
65 MCSymbolRefExpr::create(Symbol: Sym, Kind: MCSymbolRefExpr::VK_GOTPCREL, Ctx&: getContext());
66 const MCExpr *Off = MCConstantExpr::create(Value: FinalOffset, Ctx&: getContext());
67 return MCBinaryExpr::createAdd(LHS: Res, RHS: Off, Ctx&: getContext());
68}
69