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