Rafael Espindola | 38a400d | 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 | |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/PPCFixupKinds.h" |
Bill Schmidt | a4f8984 | 2012-12-14 20:28:38 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCELFObjectWriter.h" |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
| 15 | #include "llvm/MC/MCValue.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | class PPCELFObjectWriter : public MCELFObjectTargetWriter { |
| 22 | public: |
| 23 | PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI); |
| 24 | |
| 25 | virtual ~PPCELFObjectWriter(); |
| 26 | protected: |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 27 | virtual unsigned getRelocTypeInner(const MCValue &Target, |
| 28 | const MCFixup &Fixup, |
| 29 | bool IsPCRel) const; |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 30 | virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
| 31 | bool IsPCRel, bool IsRelocWithSymbol, |
| 32 | int64_t Addend) const; |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 33 | virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target, |
| 34 | const MCFixup &Fixup, |
| 35 | bool IsPCRel) const; |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 36 | virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset); |
Bill Schmidt | a4f8984 | 2012-12-14 20:28:38 +0000 | [diff] [blame] | 37 | |
| 38 | virtual void sortRelocs(const MCAssembler &Asm, |
| 39 | std::vector<ELFRelocationEntry> &Relocs); |
| 40 | }; |
| 41 | |
| 42 | class PPCELFRelocationEntry : public ELFRelocationEntry { |
| 43 | public: |
| 44 | PPCELFRelocationEntry(const ELFRelocationEntry &RE); |
| 45 | bool operator<(const PPCELFRelocationEntry &RE) const { |
| 46 | return (RE.r_offset < r_offset || |
| 47 | (RE.r_offset == r_offset && RE.Type > Type)); |
| 48 | } |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 49 | }; |
| 50 | } |
| 51 | |
Bill Schmidt | a4f8984 | 2012-12-14 20:28:38 +0000 | [diff] [blame] | 52 | PPCELFRelocationEntry::PPCELFRelocationEntry(const ELFRelocationEntry &RE) |
| 53 | : ELFRelocationEntry(RE.r_offset, RE.Index, RE.Type, RE.Symbol, |
| 54 | RE.r_addend, *RE.Fixup) {} |
| 55 | |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 56 | PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI) |
| 57 | : MCELFObjectTargetWriter(Is64Bit, OSABI, |
| 58 | Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC, |
Rafael Espindola | 2500962 | 2011-12-22 18:38:06 +0000 | [diff] [blame] | 59 | /*HasRelocationAddend*/ true) {} |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 60 | |
| 61 | PPCELFObjectWriter::~PPCELFObjectWriter() { |
| 62 | } |
| 63 | |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 64 | unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target, |
| 65 | const MCFixup &Fixup, |
| 66 | bool IsPCRel) const |
| 67 | { |
| 68 | MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? |
| 69 | MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); |
| 70 | |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 71 | // determine the type of the relocation |
| 72 | unsigned Type; |
| 73 | if (IsPCRel) { |
| 74 | switch ((unsigned)Fixup.getKind()) { |
| 75 | default: |
| 76 | llvm_unreachable("Unimplemented"); |
| 77 | case PPC::fixup_ppc_br24: |
| 78 | Type = ELF::R_PPC_REL24; |
| 79 | break; |
Adhemerval Zanella | 9b0b781 | 2013-01-04 19:08:13 +0000 | [diff] [blame] | 80 | case FK_Data_4: |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 81 | case FK_PCRel_4: |
| 82 | Type = ELF::R_PPC_REL32; |
| 83 | break; |
Adhemerval Zanella | 9b0b781 | 2013-01-04 19:08:13 +0000 | [diff] [blame] | 84 | case FK_Data_8: |
| 85 | case FK_PCRel_8: |
| 86 | Type = ELF::R_PPC64_REL64; |
| 87 | break; |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 88 | } |
| 89 | } else { |
| 90 | switch ((unsigned)Fixup.getKind()) { |
| 91 | default: llvm_unreachable("invalid fixup kind!"); |
| 92 | case PPC::fixup_ppc_br24: |
| 93 | Type = ELF::R_PPC_ADDR24; |
| 94 | break; |
| 95 | case PPC::fixup_ppc_brcond14: |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 96 | Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_ |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 97 | break; |
| 98 | case PPC::fixup_ppc_ha16: |
Ulrich Weigand | 8557850 | 2012-11-13 19:24:36 +0000 | [diff] [blame] | 99 | switch (Modifier) { |
| 100 | default: llvm_unreachable("Unsupported Modifier"); |
| 101 | case MCSymbolRefExpr::VK_PPC_TPREL16_HA: |
| 102 | Type = ELF::R_PPC_TPREL16_HA; |
| 103 | break; |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 104 | case MCSymbolRefExpr::VK_PPC_DTPREL16_HA: |
| 105 | Type = ELF::R_PPC64_DTPREL16_HA; |
| 106 | break; |
Ulrich Weigand | 8557850 | 2012-11-13 19:24:36 +0000 | [diff] [blame] | 107 | case MCSymbolRefExpr::VK_None: |
| 108 | Type = ELF::R_PPC_ADDR16_HA; |
| 109 | break; |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 110 | case MCSymbolRefExpr::VK_PPC_TOC16_HA: |
| 111 | Type = ELF::R_PPC64_TOC16_HA; |
| 112 | break; |
Bill Schmidt | 9f0b4ec | 2012-12-14 17:02:38 +0000 | [diff] [blame] | 113 | case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA: |
| 114 | Type = ELF::R_PPC64_GOT_TPREL16_HA; |
| 115 | break; |
Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 116 | case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA: |
| 117 | Type = ELF::R_PPC64_GOT_TLSGD16_HA; |
| 118 | break; |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 119 | case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA: |
| 120 | Type = ELF::R_PPC64_GOT_TLSLD16_HA; |
| 121 | break; |
Ulrich Weigand | 8557850 | 2012-11-13 19:24:36 +0000 | [diff] [blame] | 122 | } |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 123 | break; |
| 124 | case PPC::fixup_ppc_lo16: |
Ulrich Weigand | 8557850 | 2012-11-13 19:24:36 +0000 | [diff] [blame] | 125 | switch (Modifier) { |
| 126 | default: llvm_unreachable("Unsupported Modifier"); |
| 127 | case MCSymbolRefExpr::VK_PPC_TPREL16_LO: |
| 128 | Type = ELF::R_PPC_TPREL16_LO; |
| 129 | break; |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 130 | case MCSymbolRefExpr::VK_PPC_DTPREL16_LO: |
| 131 | Type = ELF::R_PPC64_DTPREL16_LO; |
| 132 | break; |
Ulrich Weigand | 8557850 | 2012-11-13 19:24:36 +0000 | [diff] [blame] | 133 | case MCSymbolRefExpr::VK_None: |
| 134 | Type = ELF::R_PPC_ADDR16_LO; |
| 135 | break; |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 136 | case MCSymbolRefExpr::VK_PPC_TOC_ENTRY: |
| 137 | Type = ELF::R_PPC64_TOC16; |
| 138 | break; |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 139 | case MCSymbolRefExpr::VK_PPC_TOC16_LO: |
| 140 | Type = ELF::R_PPC64_TOC16_LO; |
| 141 | break; |
Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 142 | case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO: |
| 143 | Type = ELF::R_PPC64_GOT_TLSGD16_LO; |
| 144 | break; |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 145 | case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO: |
| 146 | Type = ELF::R_PPC64_GOT_TLSLD16_LO; |
| 147 | break; |
Ulrich Weigand | 8557850 | 2012-11-13 19:24:36 +0000 | [diff] [blame] | 148 | } |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 149 | break; |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 150 | case PPC::fixup_ppc_lo16_ds: |
Bill Schmidt | 3822ef2 | 2013-02-21 00:05:29 +0000 | [diff] [blame] | 151 | switch (Modifier) { |
| 152 | default: llvm_unreachable("Unsupported Modifier"); |
Bill Schmidt | 3822ef2 | 2013-02-21 00:05:29 +0000 | [diff] [blame] | 153 | case MCSymbolRefExpr::VK_None: |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 154 | Type = ELF::R_PPC64_ADDR16_DS; |
Bill Schmidt | 3822ef2 | 2013-02-21 00:05:29 +0000 | [diff] [blame] | 155 | break; |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 156 | case MCSymbolRefExpr::VK_PPC_TOC_ENTRY: |
| 157 | Type = ELF::R_PPC64_TOC16_DS; |
| 158 | break; |
| 159 | case MCSymbolRefExpr::VK_PPC_TOC16_LO: |
| 160 | Type = ELF::R_PPC64_TOC16_LO_DS; |
| 161 | break; |
Bill Schmidt | 9f0b4ec | 2012-12-14 17:02:38 +0000 | [diff] [blame] | 162 | case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO: |
| 163 | Type = ELF::R_PPC64_GOT_TPREL16_LO_DS; |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 164 | break; |
Bill Schmidt | 34627e3 | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 165 | } |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 166 | break; |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 167 | case PPC::fixup_ppc_tlsreg: |
| 168 | Type = ELF::R_PPC64_TLS; |
| 169 | break; |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 170 | case PPC::fixup_ppc_nofixup: |
| 171 | switch (Modifier) { |
| 172 | default: llvm_unreachable("Unsupported Modifier"); |
| 173 | case MCSymbolRefExpr::VK_PPC_TLSGD: |
| 174 | Type = ELF::R_PPC64_TLSGD; |
| 175 | break; |
| 176 | case MCSymbolRefExpr::VK_PPC_TLSLD: |
| 177 | Type = ELF::R_PPC64_TLSLD; |
| 178 | break; |
| 179 | } |
Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 180 | break; |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 181 | case FK_Data_8: |
| 182 | switch (Modifier) { |
| 183 | default: llvm_unreachable("Unsupported Modifier"); |
| 184 | case MCSymbolRefExpr::VK_PPC_TOC: |
| 185 | Type = ELF::R_PPC64_TOC; |
| 186 | break; |
| 187 | case MCSymbolRefExpr::VK_None: |
| 188 | Type = ELF::R_PPC64_ADDR64; |
| 189 | break; |
| 190 | } |
| 191 | break; |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 192 | case FK_Data_4: |
| 193 | Type = ELF::R_PPC_ADDR32; |
| 194 | break; |
| 195 | case FK_Data_2: |
| 196 | Type = ELF::R_PPC_ADDR16; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | return Type; |
| 201 | } |
| 202 | |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 203 | unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target, |
| 204 | const MCFixup &Fixup, |
| 205 | bool IsPCRel, |
| 206 | bool IsRelocWithSymbol, |
| 207 | int64_t Addend) const { |
| 208 | return getRelocTypeInner(Target, Fixup, IsPCRel); |
| 209 | } |
| 210 | |
| 211 | const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target, |
| 212 | const MCFixup &Fixup, |
| 213 | bool IsPCRel) const { |
| 214 | assert(Target.getSymA() && "SymA cannot be 0"); |
| 215 | const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol(); |
| 216 | |
| 217 | unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel); |
| 218 | |
| 219 | // The .odp creation emits a relocation against the symbol ".TOC." which |
| 220 | // create a R_PPC64_TOC relocation. However the relocation symbol name |
| 221 | // in final object creation should be NULL, since the symbol does not |
| 222 | // really exist, it is just the reference to TOC base for the current |
| 223 | // object file. |
| 224 | bool EmitThisSym = RelocType != ELF::R_PPC64_TOC; |
| 225 | |
| 226 | if (EmitThisSym && !Symbol.isTemporary()) |
| 227 | return &Symbol; |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 231 | void PPCELFObjectWriter:: |
| 232 | adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) { |
| 233 | switch ((unsigned)Fixup.getKind()) { |
| 234 | case PPC::fixup_ppc_ha16: |
| 235 | case PPC::fixup_ppc_lo16: |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 236 | case PPC::fixup_ppc_lo16_ds: |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 237 | RelocOffset += 2; |
| 238 | break; |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
Bill Schmidt | a4f8984 | 2012-12-14 20:28:38 +0000 | [diff] [blame] | 244 | // The standard sorter only sorts on the r_offset field, but PowerPC can |
| 245 | // have multiple relocations at the same offset. Sort secondarily on the |
| 246 | // relocation type to avoid nondeterminism. |
| 247 | void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm, |
| 248 | std::vector<ELFRelocationEntry> &Relocs) { |
| 249 | |
| 250 | // Copy to a temporary vector of relocation entries having a different |
| 251 | // sort function. |
| 252 | std::vector<PPCELFRelocationEntry> TmpRelocs; |
| 253 | |
| 254 | for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin(); |
| 255 | R != Relocs.end(); ++R) { |
| 256 | TmpRelocs.push_back(PPCELFRelocationEntry(*R)); |
| 257 | } |
| 258 | |
| 259 | // Sort in place by ascending r_offset and descending r_type. |
| 260 | array_pod_sort(TmpRelocs.begin(), TmpRelocs.end()); |
| 261 | |
| 262 | // Copy back to the original vector. |
| 263 | unsigned I = 0; |
| 264 | for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin(); |
| 265 | R != TmpRelocs.end(); ++R, ++I) { |
| 266 | Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type, |
| 267 | R->Symbol, R->r_addend, *R->Fixup); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 272 | MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS, |
| 273 | bool Is64Bit, |
| 274 | uint8_t OSABI) { |
| 275 | MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI); |
Rafael Espindola | 2500962 | 2011-12-22 18:38:06 +0000 | [diff] [blame] | 276 | return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false); |
Rafael Espindola | 38a400d | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 277 | } |