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