1 //===- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ----------*- C++ -*-===// 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 #ifndef LLVM_MC_MCELFOBJECTWRITER_H 11 #define LLVM_MC_MCELFOBJECTWRITER_H 12 13 #include "llvm/ADT/Triple.h" 14 #include "llvm/BinaryFormat/ELF.h" 15 #include "llvm/Support/Casting.h" 16 #include "llvm/Support/raw_ostream.h" 17 #include <cstdint> 18 #include <vector> 19 20 namespace llvm { 21 22 class MCAssembler; 23 class MCContext; 24 class MCFixup; 25 class MCObjectWriter; 26 class MCSymbol; 27 class MCSymbolELF; 28 class MCValue; 29 30 struct ELFRelocationEntry { 31 uint64_t Offset; // Where is the relocation. 32 const MCSymbolELF *Symbol; // The symbol to relocate with. 33 unsigned Type; // The type of the relocation. 34 uint64_t Addend; // The addend to use. 35 const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it. 36 uint64_t OriginalAddend; // The original value of addend. 37 38 ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, 39 uint64_t Addend, const MCSymbolELF *OriginalSymbol, 40 uint64_t OriginalAddend) 41 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend), 42 OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {} 43 44 void print(raw_ostream &Out) const { 45 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type 46 << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol 47 << ", OriginalAddend=" << OriginalAddend; 48 } 49 50 void dump() const { print(errs()); } 51 }; 52 53 class MCELFObjectTargetWriter { 54 const uint8_t OSABI; 55 const uint16_t EMachine; 56 const unsigned HasRelocationAddend : 1; 57 const unsigned Is64Bit : 1; 58 const unsigned IsN64 : 1; 59 60 protected: 61 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_, 62 bool HasRelocationAddend, bool IsN64 = false); 63 64 public: 65 virtual ~MCELFObjectTargetWriter() = default; 66 67 static uint8_t getOSABI(Triple::OSType OSType) { 68 switch (OSType) { 69 case Triple::CloudABI: 70 return ELF::ELFOSABI_CLOUDABI; 71 case Triple::PS4: 72 case Triple::FreeBSD: 73 return ELF::ELFOSABI_FREEBSD; 74 default: 75 return ELF::ELFOSABI_NONE; 76 } 77 } 78 79 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 80 const MCFixup &Fixup, bool IsPCRel) const = 0; 81 82 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, 83 unsigned Type) const; 84 85 virtual void sortRelocs(const MCAssembler &Asm, 86 std::vector<ELFRelocationEntry> &Relocs); 87 88 /// \name Accessors 89 /// @{ 90 uint8_t getOSABI() const { return OSABI; } 91 uint16_t getEMachine() const { return EMachine; } 92 bool hasRelocationAddend() const { return HasRelocationAddend; } 93 bool is64Bit() const { return Is64Bit; } 94 bool isN64() const { return IsN64; } 95 /// @} 96 97 // Instead of changing everyone's API we pack the N64 Type fields 98 // into the existing 32 bit data unsigned. 99 #define R_TYPE_SHIFT 0 100 #define R_TYPE_MASK 0xffffff00 101 #define R_TYPE2_SHIFT 8 102 #define R_TYPE2_MASK 0xffff00ff 103 #define R_TYPE3_SHIFT 16 104 #define R_TYPE3_MASK 0xff00ffff 105 #define R_SSYM_SHIFT 24 106 #define R_SSYM_MASK 0x00ffffff 107 108 // N64 relocation type accessors 109 uint8_t getRType(uint32_t Type) const { 110 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff); 111 } 112 uint8_t getRType2(uint32_t Type) const { 113 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff); 114 } 115 uint8_t getRType3(uint32_t Type) const { 116 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff); 117 } 118 uint8_t getRSsym(uint32_t Type) const { 119 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff); 120 } 121 122 // N64 relocation type setting 123 unsigned setRType(unsigned Value, unsigned Type) const { 124 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT)); 125 } 126 unsigned setRType2(unsigned Value, unsigned Type) const { 127 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT); 128 } 129 unsigned setRType3(unsigned Value, unsigned Type) const { 130 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT); 131 } 132 unsigned setRSsym(unsigned Value, unsigned Type) const { 133 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT); 134 } 135 }; 136 137 /// \brief Construct a new ELF writer instance. 138 /// 139 /// \param MOTW - The target specific ELF writer subclass. 140 /// \param OS - The stream to write to. 141 /// \returns The constructed object writer. 142 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW, 143 raw_pwrite_stream &OS, 144 bool IsLittleEndian); 145 146 } // end namespace llvm 147 148 #endif // LLVM_MC_MCELFOBJECTWRITER_H 149