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_DTPREL16_LO: 157 Type = ELF::R_PPC64_DTPREL16_LO; 158 break; 159 case MCSymbolRefExpr::VK_None: 160 Type = ELF::R_PPC64_TOC16; 161 break; 162 case MCSymbolRefExpr::VK_PPC_TOC16_LO: 163 Type = ELF::R_PPC64_TOC16_LO; 164 break; 165 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO: 166 Type = ELF::R_PPC64_GOT_TLSLD16_LO; 167 break; 168 } 169 break; 170 case PPC::fixup_ppc_toc16_ds: 171 switch (Modifier) { 172 default: llvm_unreachable("Unsupported Modifier"); 173 case MCSymbolRefExpr::VK_PPC_TOC_ENTRY: 174 Type = ELF::R_PPC64_TOC16_DS; 175 break; 176 case MCSymbolRefExpr::VK_PPC_TOC16_LO: 177 Type = ELF::R_PPC64_TOC16_LO_DS; 178 break; 179 case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO: 180 Type = ELF::R_PPC64_GOT_TPREL16_LO_DS; 181 break; 182 } 183 break; 184 case PPC::fixup_ppc_tlsreg: 185 Type = ELF::R_PPC64_TLS; 186 break; 187 case PPC::fixup_ppc_nofixup: 188 switch (Modifier) { 189 default: llvm_unreachable("Unsupported Modifier"); 190 case MCSymbolRefExpr::VK_PPC_TLSGD: 191 Type = ELF::R_PPC64_TLSGD; 192 break; 193 case MCSymbolRefExpr::VK_PPC_TLSLD: 194 Type = ELF::R_PPC64_TLSLD; 195 break; 196 } 197 break; 198 case FK_Data_8: 199 switch (Modifier) { 200 default: llvm_unreachable("Unsupported Modifier"); 201 case MCSymbolRefExpr::VK_PPC_TOC: 202 Type = ELF::R_PPC64_TOC; 203 break; 204 case MCSymbolRefExpr::VK_None: 205 Type = ELF::R_PPC64_ADDR64; 206 break; 207 } 208 break; 209 case FK_Data_4: 210 Type = ELF::R_PPC_ADDR32; 211 break; 212 case FK_Data_2: 213 Type = ELF::R_PPC_ADDR16; 214 break; 215 } 216 } 217 return Type; 218 } 219 220 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target, 221 const MCFixup &Fixup, 222 bool IsPCRel, 223 bool IsRelocWithSymbol, 224 int64_t Addend) const { 225 return getRelocTypeInner(Target, Fixup, IsPCRel); 226 } 227 228 const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target, 229 const MCFixup &Fixup, 230 bool IsPCRel) const { 231 assert(Target.getSymA() && "SymA cannot be 0"); 232 const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol(); 233 234 unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel); 235 236 // The .odp creation emits a relocation against the symbol ".TOC." which 237 // create a R_PPC64_TOC relocation. However the relocation symbol name 238 // in final object creation should be NULL, since the symbol does not 239 // really exist, it is just the reference to TOC base for the current 240 // object file. 241 bool EmitThisSym = RelocType != ELF::R_PPC64_TOC; 242 243 if (EmitThisSym && !Symbol.isTemporary()) 244 return &Symbol; 245 return NULL; 246 } 247 248 void PPCELFObjectWriter:: 249 adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) { 250 switch ((unsigned)Fixup.getKind()) { 251 case PPC::fixup_ppc_ha16: 252 case PPC::fixup_ppc_lo16: 253 case PPC::fixup_ppc_toc16: 254 case PPC::fixup_ppc_toc16_ds: 255 RelocOffset += 2; 256 break; 257 default: 258 break; 259 } 260 } 261 262 // The standard sorter only sorts on the r_offset field, but PowerPC can 263 // have multiple relocations at the same offset. Sort secondarily on the 264 // relocation type to avoid nondeterminism. 265 void PPCELFObjectWriter::sortRelocs(const MCAssembler &Asm, 266 std::vector<ELFRelocationEntry> &Relocs) { 267 268 // Copy to a temporary vector of relocation entries having a different 269 // sort function. 270 std::vector<PPCELFRelocationEntry> TmpRelocs; 271 272 for (std::vector<ELFRelocationEntry>::iterator R = Relocs.begin(); 273 R != Relocs.end(); ++R) { 274 TmpRelocs.push_back(PPCELFRelocationEntry(*R)); 275 } 276 277 // Sort in place by ascending r_offset and descending r_type. 278 array_pod_sort(TmpRelocs.begin(), TmpRelocs.end()); 279 280 // Copy back to the original vector. 281 unsigned I = 0; 282 for (std::vector<PPCELFRelocationEntry>::iterator R = TmpRelocs.begin(); 283 R != TmpRelocs.end(); ++R, ++I) { 284 Relocs[I] = ELFRelocationEntry(R->r_offset, R->Index, R->Type, 285 R->Symbol, R->r_addend, *R->Fixup); 286 } 287 } 288 289 290 MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS, 291 bool Is64Bit, 292 uint8_t OSABI) { 293 MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI); 294 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false); 295 } 296