MCELFObjectWriter.h revision f1d0f7781e766df878bec4e7977fa3204374f394
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 18namespace llvm { 19class MCAssembler; 20class MCFixup; 21class MCFragment; 22class MCObjectWriter; 23class MCSymbol; 24class MCValue; 25 26/// @name Relocation Data 27/// @{ 28 29struct ELFRelocationEntry { 30 // Make these big enough for both 32-bit and 64-bit 31 uint64_t r_offset; 32 int Index; 33 unsigned Type; 34 const MCSymbol *Symbol; 35 uint64_t r_addend; 36 const MCFixup *Fixup; 37 38 ELFRelocationEntry() 39 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0), Fixup(0) {} 40 41 ELFRelocationEntry(uint64_t RelocOffset, int Idx, unsigned RelType, 42 const MCSymbol *Sym, uint64_t Addend, const MCFixup &Fixup) 43 : r_offset(RelocOffset), Index(Idx), Type(RelType), Symbol(Sym), 44 r_addend(Addend), Fixup(&Fixup) {} 45 46 // Support lexicographic sorting. 47 bool operator<(const ELFRelocationEntry &RE) const { 48 return RE.r_offset < r_offset; 49 } 50}; 51 52class MCELFObjectTargetWriter { 53 const uint8_t OSABI; 54 const uint16_t EMachine; 55 const unsigned HasRelocationAddend : 1; 56 const unsigned Is64Bit : 1; 57 58protected: 59 60 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, 61 uint16_t EMachine_, bool HasRelocationAddend_); 62 63public: 64 static uint8_t getOSABI(Triple::OSType OSType) { 65 switch (OSType) { 66 case Triple::FreeBSD: 67 return ELF::ELFOSABI_FREEBSD; 68 case Triple::Linux: 69 return ELF::ELFOSABI_LINUX; 70 default: 71 return ELF::ELFOSABI_NONE; 72 } 73 } 74 75 virtual ~MCELFObjectTargetWriter() {} 76 77 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, 78 bool IsPCRel, bool IsRelocWithSymbol, 79 int64_t Addend) const = 0; 80 virtual unsigned getEFlags() const; 81 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm, 82 const MCValue &Target, 83 const MCFragment &F, 84 const MCFixup &Fixup, 85 bool IsPCRel) const; 86 virtual void adjustFixupOffset(const MCFixup &Fixup, 87 uint64_t &RelocOffset); 88 89 virtual void sortRelocs(const MCAssembler &Asm, 90 std::vector<ELFRelocationEntry> &Relocs); 91 92 /// @name Accessors 93 /// @{ 94 uint8_t getOSABI() { return OSABI; } 95 uint16_t getEMachine() { return EMachine; } 96 bool hasRelocationAddend() { return HasRelocationAddend; } 97 bool is64Bit() const { return Is64Bit; } 98 /// @} 99}; 100 101/// \brief Construct a new ELF writer instance. 102/// 103/// \param MOTW - The target specific ELF writer subclass. 104/// \param OS - The stream to write to. 105/// \returns The constructed object writer. 106MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW, 107 raw_ostream &OS, bool IsLittleEndian); 108} // End llvm namespace 109 110#endif 111