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