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 "MCTargetDesc/X86MCAsmInfo.h" 11 #include "llvm/BinaryFormat/Dwarf.h" 12 #include "llvm/MC/MCExpr.h" 13 #include "llvm/MC/MCValue.h" 14 #include "llvm/Target/TargetMachine.h" 15 16 using namespace llvm; 17 using namespace dwarf; 18 19 const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference( 20 const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, 21 MachineModuleInfo *MMI, MCStreamer &Streamer) const { 22 23 // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which 24 // is an indirect pc-relative reference. 25 if ((Encoding & DW_EH_PE_indirect) && (Encoding & DW_EH_PE_pcrel)) { 26 const MCSymbol *Sym = TM.getSymbol(GV); 27 const MCExpr *Res = 28 MCSymbolRefExpr::create(Sym, X86::S_GOTPCREL, getContext()); 29 const MCExpr *Four = MCConstantExpr::create(4, getContext()); 30 return MCBinaryExpr::createAdd(Res, Four, getContext()); 31 } 32 33 return TargetLoweringObjectFileMachO::getTTypeGlobalReference( 34 GV, Encoding, TM, MMI, Streamer); 35 } 36 37 MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol( 38 const GlobalValue *GV, const TargetMachine &TM, 39 MachineModuleInfo *MMI) const { 40 return TM.getSymbol(GV); 41 } 42 43 const MCExpr *X86_64MachoTargetObjectFile::getIndirectSymViaGOTPCRel( 44 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, 45 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const { 46 // On Darwin/X86-64, we need to use foo@GOTPCREL+4 to access the got entry 47 // from a data section. In case there's an additional offset, then use 48 // foo@GOTPCREL+4+<offset>. 49 unsigned FinalOff = Offset+MV.getConstant()+4; 50 const MCExpr *Res = 51 MCSymbolRefExpr::create(Sym, X86::S_GOTPCREL, getContext()); 52 const MCExpr *Off = MCConstantExpr::create(FinalOff, getContext()); 53 return MCBinaryExpr::createAdd(Res, Off, getContext()); 54 } 55 56 X86ELFTargetObjectFile::X86ELFTargetObjectFile() { 57 PLTRelativeSpecifier = X86::S_PLT; 58 } 59 60 const MCExpr *X86ELFTargetObjectFile::getDebugThreadLocalSymbol( 61 const MCSymbol *Sym) const { 62 return MCSymbolRefExpr::create(Sym, X86::S_DTPOFF, getContext()); 63 } 64 65 const MCExpr *X86_64ELFTargetObjectFile::getIndirectSymViaGOTPCRel( 66 const GlobalValue *GV, const MCSymbol *Sym, const MCValue &MV, 67 int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const { 68 int64_t FinalOffset = Offset + MV.getConstant(); 69 const MCExpr *Res = 70 MCSymbolRefExpr::create(Sym, X86::S_GOTPCREL, getContext()); 71 const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext()); 72 return MCBinaryExpr::createAdd(Res, Off, getContext()); 73 } 74