Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 1 | //===-- PPCELFObjectWriter.cpp - PPC ELF Writer ---------------------------===// |
| 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 "MCTargetDesc/PPCFixupKinds.h" |
| 11 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
| 12 | #include "llvm/MC/MCELFObjectWriter.h" |
| 13 | #include "llvm/Support/ErrorHandling.h" |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 14 | #include "llvm/MC/MCExpr.h" |
| 15 | #include "llvm/MC/MCValue.h" |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | class PPCELFObjectWriter : public MCELFObjectTargetWriter { |
| 21 | public: |
| 22 | PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI); |
| 23 | |
| 24 | virtual ~PPCELFObjectWriter(); |
| 25 | protected: |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 26 | virtual unsigned getRelocTypeInner(const MCValue &Target, |
| 27 | const MCFixup &Fixup, |
| 28 | bool IsPCRel) const; |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 29 | virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
| 30 | bool IsPCRel, bool IsRelocWithSymbol, |
| 31 | int64_t Addend) const; |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 32 | virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target, |
| 33 | const MCFixup &Fixup, |
| 34 | bool IsPCRel) const; |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 35 | virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset); |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI) |
| 40 | : MCELFObjectTargetWriter(Is64Bit, OSABI, |
| 41 | Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC, |
Rafael Espindola | f51e95a | 2011-12-22 18:38:06 +0000 | [diff] [blame] | 42 | /*HasRelocationAddend*/ true) {} |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 43 | |
| 44 | PPCELFObjectWriter::~PPCELFObjectWriter() { |
| 45 | } |
| 46 | |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 47 | unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target, |
| 48 | const MCFixup &Fixup, |
| 49 | bool IsPCRel) const |
| 50 | { |
| 51 | MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? |
| 52 | MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); |
| 53 | |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 54 | // determine the type of the relocation |
| 55 | unsigned Type; |
| 56 | if (IsPCRel) { |
| 57 | switch ((unsigned)Fixup.getKind()) { |
| 58 | default: |
| 59 | llvm_unreachable("Unimplemented"); |
| 60 | case PPC::fixup_ppc_br24: |
| 61 | Type = ELF::R_PPC_REL24; |
| 62 | break; |
| 63 | case FK_PCRel_4: |
| 64 | Type = ELF::R_PPC_REL32; |
| 65 | break; |
| 66 | } |
| 67 | } else { |
| 68 | switch ((unsigned)Fixup.getKind()) { |
| 69 | default: llvm_unreachable("invalid fixup kind!"); |
| 70 | case PPC::fixup_ppc_br24: |
| 71 | Type = ELF::R_PPC_ADDR24; |
| 72 | break; |
| 73 | case PPC::fixup_ppc_brcond14: |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 74 | Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_ |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 75 | break; |
| 76 | case PPC::fixup_ppc_ha16: |
| 77 | Type = ELF::R_PPC_ADDR16_HA; |
| 78 | break; |
| 79 | case PPC::fixup_ppc_lo16: |
| 80 | Type = ELF::R_PPC_ADDR16_LO; |
| 81 | break; |
| 82 | case PPC::fixup_ppc_lo14: |
| 83 | Type = ELF::R_PPC_ADDR14; |
| 84 | break; |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 85 | case PPC::fixup_ppc_toc: |
| 86 | Type = ELF::R_PPC64_TOC; |
| 87 | break; |
| 88 | case PPC::fixup_ppc_toc16: |
| 89 | Type = ELF::R_PPC64_TOC16; |
| 90 | break; |
| 91 | case PPC::fixup_ppc_toc16_ds: |
| 92 | Type = ELF::R_PPC64_TOC16_DS; |
| 93 | break; |
| 94 | case FK_Data_8: |
| 95 | switch (Modifier) { |
| 96 | default: llvm_unreachable("Unsupported Modifier"); |
| 97 | case MCSymbolRefExpr::VK_PPC_TOC: |
| 98 | Type = ELF::R_PPC64_TOC; |
| 99 | break; |
| 100 | case MCSymbolRefExpr::VK_None: |
| 101 | Type = ELF::R_PPC64_ADDR64; |
| 102 | break; |
| 103 | } |
| 104 | break; |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 105 | case FK_Data_4: |
| 106 | Type = ELF::R_PPC_ADDR32; |
| 107 | break; |
| 108 | case FK_Data_2: |
| 109 | Type = ELF::R_PPC_ADDR16; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | return Type; |
| 114 | } |
| 115 | |
Adhemerval Zanella | aa71428 | 2012-10-25 12:27:42 +0000 | [diff] [blame^] | 116 | unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target, |
| 117 | const MCFixup &Fixup, |
| 118 | bool IsPCRel, |
| 119 | bool IsRelocWithSymbol, |
| 120 | int64_t Addend) const { |
| 121 | return getRelocTypeInner(Target, Fixup, IsPCRel); |
| 122 | } |
| 123 | |
| 124 | const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target, |
| 125 | const MCFixup &Fixup, |
| 126 | bool IsPCRel) const { |
| 127 | assert(Target.getSymA() && "SymA cannot be 0"); |
| 128 | const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol(); |
| 129 | |
| 130 | unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel); |
| 131 | |
| 132 | // The .odp creation emits a relocation against the symbol ".TOC." which |
| 133 | // create a R_PPC64_TOC relocation. However the relocation symbol name |
| 134 | // in final object creation should be NULL, since the symbol does not |
| 135 | // really exist, it is just the reference to TOC base for the current |
| 136 | // object file. |
| 137 | bool EmitThisSym = RelocType != ELF::R_PPC64_TOC; |
| 138 | |
| 139 | if (EmitThisSym && !Symbol.isTemporary()) |
| 140 | return &Symbol; |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 144 | void PPCELFObjectWriter:: |
| 145 | adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) { |
| 146 | switch ((unsigned)Fixup.getKind()) { |
| 147 | case PPC::fixup_ppc_ha16: |
| 148 | case PPC::fixup_ppc_lo16: |
| 149 | RelocOffset += 2; |
| 150 | break; |
| 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS, |
| 157 | bool Is64Bit, |
| 158 | uint8_t OSABI) { |
| 159 | MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI); |
Rafael Espindola | f51e95a | 2011-12-22 18:38:06 +0000 | [diff] [blame] | 160 | return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false); |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 161 | } |