Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- X86TargetObjectFile.cpp - X86 Object Info -------------------------===// |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 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 | |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 10 | #include "X86TargetObjectFile.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Mangler.h" |
David Majnemer | dee1057 | 2014-01-15 09:16:42 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Operator.h" |
Chris Lattner | 3a78ce3 | 2009-09-18 20:22:52 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSectionELF.h" |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Dwarf.h" |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetLowering.h" |
Jakub Staszak | ab3d878 | 2013-01-10 23:43:56 +0000 | [diff] [blame] | 18 | |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 20 | using namespace dwarf; |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 21 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 22 | const MCExpr *X86_64MachoTargetObjectFile::getTTypeGlobalReference( |
| 23 | const GlobalValue *GV, unsigned Encoding, Mangler &Mang, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 24 | const TargetMachine &TM, MachineModuleInfo *MMI, |
| 25 | MCStreamer &Streamer) const { |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 26 | |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 27 | // On Darwin/X86-64, we can reference dwarf symbols with foo@GOTPCREL+4, which |
| 28 | // is an indirect pc-relative reference. |
Anton Korobeynikov | c9adb6a | 2010-02-15 22:38:10 +0000 | [diff] [blame] | 29 | if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) { |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 30 | const MCSymbol *Sym = TM.getTargetLowering()->getSymbol(GV, Mang); |
Anton Korobeynikov | c9adb6a | 2010-02-15 22:38:10 +0000 | [diff] [blame] | 31 | const MCExpr *Res = |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 32 | MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext()); |
Anton Korobeynikov | c9adb6a | 2010-02-15 22:38:10 +0000 | [diff] [blame] | 33 | const MCExpr *Four = MCConstantExpr::Create(4, getContext()); |
| 34 | return MCBinaryExpr::CreateAdd(Res, Four, getContext()); |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 37 | return TargetLoweringObjectFileMachO::getTTypeGlobalReference( |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 38 | GV, Encoding, Mang, TM, MMI, Streamer); |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 41 | MCSymbol *X86_64MachoTargetObjectFile::getCFIPersonalitySymbol( |
| 42 | const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, |
| 43 | MachineModuleInfo *MMI) const { |
| 44 | return TM.getTargetLowering()->getSymbol(GV, Mang); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 45 | } |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 46 | |
| 47 | void |
| 48 | X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) { |
| 49 | TargetLoweringObjectFileELF::Initialize(Ctx, TM); |
| 50 | InitializeELF(TM.Options.UseInitArray); |
| 51 | } |
David Blaikie | 1b01ae8 | 2013-07-01 21:45:25 +0000 | [diff] [blame] | 52 | |
Ulrich Weigand | 2b6fc8d | 2013-07-02 18:47:09 +0000 | [diff] [blame] | 53 | const MCExpr * |
David Blaikie | 1b01ae8 | 2013-07-01 21:45:25 +0000 | [diff] [blame] | 54 | X86LinuxTargetObjectFile::getDebugThreadLocalSymbol( |
| 55 | const MCSymbol *Sym) const { |
| 56 | return MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_DTPOFF, getContext()); |
| 57 | } |
David Majnemer | dee1057 | 2014-01-15 09:16:42 +0000 | [diff] [blame] | 58 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 59 | const MCExpr *X86WindowsTargetObjectFile::getExecutableRelativeSymbol( |
| 60 | const ConstantExpr *CE, Mangler &Mang, const TargetMachine &TM) const { |
David Majnemer | dee1057 | 2014-01-15 09:16:42 +0000 | [diff] [blame] | 61 | // We are looking for the difference of two symbols, need a subtraction |
| 62 | // operation. |
| 63 | const SubOperator *Sub = dyn_cast<SubOperator>(CE); |
| 64 | if (!Sub) |
| 65 | return 0; |
| 66 | |
| 67 | // Symbols must first be numbers before we can subtract them, we need to see a |
| 68 | // ptrtoint on both subtraction operands. |
| 69 | const PtrToIntOperator *SubLHS = |
| 70 | dyn_cast<PtrToIntOperator>(Sub->getOperand(0)); |
| 71 | const PtrToIntOperator *SubRHS = |
| 72 | dyn_cast<PtrToIntOperator>(Sub->getOperand(1)); |
| 73 | if (!SubLHS || !SubRHS) |
| 74 | return 0; |
| 75 | |
| 76 | // Our symbols should exist in address space zero, cowardly no-op if |
| 77 | // otherwise. |
| 78 | if (SubLHS->getPointerAddressSpace() != 0 || |
| 79 | SubRHS->getPointerAddressSpace() != 0) |
| 80 | return 0; |
| 81 | |
| 82 | // Both ptrtoint instructions must wrap global variables: |
| 83 | // - Only global variables are eligible for image relative relocations. |
| 84 | // - The subtrahend refers to the special symbol __ImageBase, a global. |
| 85 | const GlobalVariable *GVLHS = |
| 86 | dyn_cast<GlobalVariable>(SubLHS->getPointerOperand()); |
| 87 | const GlobalVariable *GVRHS = |
| 88 | dyn_cast<GlobalVariable>(SubRHS->getPointerOperand()); |
| 89 | if (!GVLHS || !GVRHS) |
| 90 | return 0; |
| 91 | |
| 92 | // We expect __ImageBase to be a global variable without a section, externally |
| 93 | // defined. |
| 94 | // |
| 95 | // It should look something like this: @__ImageBase = external constant i8 |
| 96 | if (GVRHS->isThreadLocal() || GVRHS->getName() != "__ImageBase" || |
| 97 | !GVRHS->hasExternalLinkage() || GVRHS->hasInitializer() || |
| 98 | GVRHS->hasSection()) |
| 99 | return 0; |
| 100 | |
| 101 | // An image-relative, thread-local, symbol makes no sense. |
| 102 | if (GVLHS->isThreadLocal()) |
| 103 | return 0; |
| 104 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 105 | return MCSymbolRefExpr::Create(TM.getTargetLowering()->getSymbol(GVLHS, Mang), |
| 106 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 107 | getContext()); |
David Majnemer | dee1057 | 2014-01-15 09:16:42 +0000 | [diff] [blame] | 108 | } |