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/PPCMCTargetDesc.h" 11 #include "MCTargetDesc/PPCFixupKinds.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/MC/MCELFObjectWriter.h" 14 #include "llvm/MC/MCExpr.h" 15 #include "llvm/MC/MCValue.h" 16 #include "llvm/Support/ErrorHandling.h" 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: 27 virtual unsigned getRelocTypeInner(const MCValue &Target, 28 const MCFixup &Fixup, 29 bool IsPCRel) const; 30 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, 31 bool IsPCRel, bool IsRelocWithSymbol, 32 int64_t Addend) const; 33 virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target, 34 const MCFixup &Fixup, 35 bool IsPCRel) const; 36 virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset); 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 } 49 }; 50 } 51 52 PPCELFRelocationEntry::PPCELFRelocationEntry(const ELFRelocationEntry &RE) 53 : ELFRelocationEntry(RE.r_offset, RE.Index, RE.Type, RE.Symbol, 54 RE.r_addend, *RE.Fixup) {} 55 56 PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI) 57 : MCELFObjectTargetWriter(Is64Bit, OSABI, 58 Is64Bit ? ELF::EM_PPC64 : ELF::EM_PPC, 59 /*HasRelocationAddend*/ true) {} 60 61 PPCELFObjectWriter::~PPCELFObjectWriter() { 62 } 63 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 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; 80 case FK_Data_4: 81 case FK_PCRel_4: 82 Type = ELF::R_PPC_REL32; 83 break; 84 case FK_Data_8: 85 case FK_PCRel_8: 86 Type = ELF::R_PPC64_REL64; 87 break; 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: 96 Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_ 97 break; 98 case PPC::fixup_ppc_ha16: 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; 104 case MCSymbolRefExpr::VK_PPC_DTPREL16_HA: 105 Type = ELF::R_PPC64_DTPREL16_HA; 106 break; 107 case MCSymbolRefExpr::VK_None: 108 Type = ELF::R_PPC_ADDR16_HA; 109 break; 110 case MCSymbolRefExpr::VK_PPC_TOC16_HA: 111 Type = ELF::R_PPC64_TOC16_HA; 112 break; 113 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA: 114 Type = ELF::R_PPC64_GOT_TPREL16_HA; 115 break; 116 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA: 117 Type = ELF::R_PPC64_GOT_TLSGD16_HA; 118 break; 119 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA: 120 Type = ELF::R_PPC64_GOT_TLSLD16_HA; 121 break; 122 } 123 break; 124 case PPC::fixup_ppc_lo16: 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; 130 case MCSymbolRefExpr::VK_PPC_DTPREL16_LO: 131 Type = ELF::R_PPC64_DTPREL16_LO; 132 break; 133 case MCSymbolRefExpr::VK_None: 134 Type = ELF::R_PPC_ADDR16_LO; 135 break; 136 case MCSymbolRefExpr::VK_PPC_TOC16_LO: 137 Type = ELF::R_PPC64_TOC16_LO; 138 break; 139 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO: 140 Type = ELF::R_PPC64_GOT_TLSGD16_LO; 141 break; 142 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO: 143 Type = ELF::R_PPC64_GOT_TLSLD16_LO; 144 break; 145 } 146 break; 147 case PPC::fixup_ppc_lo14: 148 Type = ELF::R_PPC_ADDR14; 149 break; 150 case PPC::fixup_ppc_toc: 151 Type = ELF::R_PPC64_TOC; 152 break; 153 case PPC::fixup_ppc_toc16: 154 switch (Modifier) { 155 default: llvm_unreachable("Unsupported Modifier"); 156 case MCSymbolRefExpr::VK_PPC_TPREL16_LO: 157 Type = ELF::R_PPC64_TPREL16_LO; 158 break; 159 case MCSymbolRefExpr::VK_PPC_DTPREL16_LO: 160 Type = ELF::R_PPC64_DTPREL16_LO; 161 break; 162 case MCSymbolRefExpr::VK_None: 163 Type = ELF::R_PPC64_TOC16; 164 break; 165 case MCSymbolRefExpr::VK_PPC_TOC16_LO: 166 Type = ELF::R_PPC64_TOC16_LO; 167 break; 168 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO: 169 Type = ELF::R_PPC64_GOT_TLSLD16_LO; 170 break; 171 } 172 break; 173 case PPC::fixup_ppc_toc16_ds: 174 switch (Modifier) { 175 default: llvm_unreachable("Unsupported Modifier"); 176 case MCSymbolRefExpr::VK_PPC_TOC_ENTRY: 177 Type = ELF::R_PPC64_TOC16_DS; 178 break; 179 case MCSymbolRefExpr::VK_PPC_TOC16_LO: 180 Type = ELF::R_PPC64_TOC16_LO_DS; 181 break; 182 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO: 183 Type = ELF::R_PPC64_GOT_TPREL16_LO_DS; 184 break; 185 } 186 break; 187 case PPC::fixup_ppc_tlsreg: 188 Type = ELF::R_PPC64_TLS; 189 break; 190 case PPC::fixup_ppc_nofixup: 191 switch (Modifier) { 192 default: llvm_unreachable("Unsupported Modifier"); 193 case MCSymbolRefExpr::VK_PPC_TLSGD: 194 Type = ELF::R_PPC64_TLSGD; 195 break; 196 case MCSymbolRefExpr::VK_PPC_TLSLD: 197 Type = ELF::R_PPC64_TLSLD; 198 break; 199 } 200 break; 201 case FK_Data_8: 202 switch (Modifier) { 203 default: llvm_unreachable("Unsupported Modifier"); 204 case MCSymbolRefExpr::VK_PPC_TOC: 205 Type = ELF::R_PPC64_TOC; 206 break; 207 case MCSymbolRefExpr::VK_None: 208 Type = ELF::R_PPC64_ADDR64; 209 break; 210 } 211 break; 212 case FK_Data_4: 213 Type = ELF::R_PPC_ADDR32; 214 break; 215 case FK_Data_2: 216 Type = ELF::R_PPC_ADDR16; 217 break; 218 } 219 } 220 return Type; 221 } 222 223 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target, 224 const MCFixup &Fixup, 225 bool IsPCRel, 226 bool IsRelocWithSymbol, 227 int64_t Addend) const { 228 return getRelocTypeInner(Target, Fixup, IsPCRel); 229 } 230 231 const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target, 232 const MCFixup &Fixup, 233 bool IsPCRel) const { 234 assert(Target.getSymA() && "SymA cannot be 0"); 235 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol(); 236 237 unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel); 238 239 // The .odp creation emits a relocation against the symbol ".TOC." which 240 // create a R_PPC64_TOC relocation. However the relocation symbol name 241 // in final object creation should be NULL, since the symbol does not 242 // really exist, it is just the reference to TOC base for the current 243 // object file. 244 bool EmitThisSym = RelocType != ELF::R_PPC64_TOC; 245 246 if (EmitThisSym && !Symbol.isTemporary()) 247 return &Symbol; 248 return NULL; 249 } 250 251 void PPCELFObjectWriter:: 252 adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) { 253 switch ((unsigned)Fixup.getKind()) { 254 case PPC::fixup_ppc_ha16: 255 case PPC::fixup_ppc_lo16: 256 case PPC::fixup_ppc_toc16: 257 case PPC::fixup_ppc_toc16_ds: 258 RelocOffset += 2; 259 break; 260 default: 261 break; 262 } 263 } 264 265 // The standard sorter only sorts on the r_offset field, but PowerPC can 266 // have multiple relocations at the same offset. Sort secondarily on the 267 // relocation type to avoid nondeterminism. 268 void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm, 269 std::vector<ELFRelocationEntry> &Relocs) { 270 271 // Copy to a temporary vector of relocation entries having a different 272 // sort function. 273 std::vector<PPCELFRelocationEntry> TmpRelocs; 274 275 for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin(); 276 R != Relocs.end(); ++R) { 277 TmpRelocs.push_back(PPCELFRelocationEntry(*R)); 278 } 279 280 // Sort in place by ascending r_offset and descending r_type. 281 array_pod_sort(TmpRelocs.begin(), TmpRelocs.end()); 282 283 // Copy back to the original vector. 284 unsigned I = 0; 285 for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin(); 286 R != TmpRelocs.end(); ++R, ++I) { 287 Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type, 288 R->Symbol, R->r_addend, *R->Fixup); 289 } 290 } 291 292 293 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS, 294 bool Is64Bit, 295 uint8_t OSABI) { 296 MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI); 297 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false); 298 } 299