LLVM API Documentation
00001 //===-- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_MC_MCELFOBJECTWRITER_H 00011 #define LLVM_MC_MCELFOBJECTWRITER_H 00012 00013 #include "llvm/ADT/Triple.h" 00014 #include "llvm/Support/DataTypes.h" 00015 #include "llvm/Support/ELF.h" 00016 #include <vector> 00017 00018 namespace llvm { 00019 class MCAssembler; 00020 class MCFixup; 00021 class MCFragment; 00022 class MCObjectWriter; 00023 class MCSectionData; 00024 class MCSymbol; 00025 class MCSymbolData; 00026 class MCValue; 00027 00028 class MCELFObjectTargetWriter { 00029 const uint8_t OSABI; 00030 const uint16_t EMachine; 00031 const unsigned HasRelocationAddend : 1; 00032 const unsigned Is64Bit : 1; 00033 const unsigned IsN64 : 1; 00034 00035 protected: 00036 00037 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, 00038 uint16_t EMachine_, bool HasRelocationAddend, 00039 bool IsN64=false); 00040 00041 public: 00042 static uint8_t getOSABI(Triple::OSType OSType) { 00043 switch (OSType) { 00044 case Triple::FreeBSD: 00045 return ELF::ELFOSABI_FREEBSD; 00046 case Triple::Linux: 00047 return ELF::ELFOSABI_LINUX; 00048 default: 00049 return ELF::ELFOSABI_NONE; 00050 } 00051 } 00052 00053 virtual ~MCELFObjectTargetWriter() {} 00054 00055 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, 00056 bool IsPCRel) const = 0; 00057 00058 virtual bool needsRelocateWithSymbol(const MCSymbolData &SD, 00059 unsigned Type) const; 00060 00061 /// @name Accessors 00062 /// @{ 00063 uint8_t getOSABI() const { return OSABI; } 00064 uint16_t getEMachine() const { return EMachine; } 00065 bool hasRelocationAddend() const { return HasRelocationAddend; } 00066 bool is64Bit() const { return Is64Bit; } 00067 bool isN64() const { return IsN64; } 00068 /// @} 00069 00070 // Instead of changing everyone's API we pack the N64 Type fields 00071 // into the existing 32 bit data unsigned. 00072 #define R_TYPE_SHIFT 0 00073 #define R_TYPE_MASK 0xffffff00 00074 #define R_TYPE2_SHIFT 8 00075 #define R_TYPE2_MASK 0xffff00ff 00076 #define R_TYPE3_SHIFT 16 00077 #define R_TYPE3_MASK 0xff00ffff 00078 #define R_SSYM_SHIFT 24 00079 #define R_SSYM_MASK 0x00ffffff 00080 00081 // N64 relocation type accessors 00082 uint8_t getRType(uint32_t Type) const { 00083 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff); 00084 } 00085 uint8_t getRType2(uint32_t Type) const { 00086 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff); 00087 } 00088 uint8_t getRType3(uint32_t Type) const { 00089 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff); 00090 } 00091 uint8_t getRSsym(uint32_t Type) const { 00092 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff); 00093 } 00094 00095 // N64 relocation type setting 00096 unsigned setRType(unsigned Value, unsigned Type) const { 00097 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT)); 00098 } 00099 unsigned setRType2(unsigned Value, unsigned Type) const { 00100 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT); 00101 } 00102 unsigned setRType3(unsigned Value, unsigned Type) const { 00103 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT); 00104 } 00105 unsigned setRSsym(unsigned Value, unsigned Type) const { 00106 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT); 00107 } 00108 }; 00109 00110 /// \brief Construct a new ELF writer instance. 00111 /// 00112 /// \param MOTW - The target specific ELF writer subclass. 00113 /// \param OS - The stream to write to. 00114 /// \returns The constructed object writer. 00115 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW, 00116 raw_ostream &OS, bool IsLittleEndian); 00117 } // End llvm namespace 00118 00119 #endif