1 //===-- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCELFOBJECTWRITER_H
11 #define LLVM_MC_MCELFOBJECTWRITER_H
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/ELF.h"
28 class MCELFObjectTargetWriter {
30 const uint16_t EMachine;
31 const unsigned HasRelocationAddend : 1;
32 const unsigned Is64Bit : 1;
33 const unsigned IsN64 : 1;
37 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
38 uint16_t EMachine_, bool HasRelocationAddend,
42 static uint8_t getOSABI(Triple::OSType OSType) {
45 return ELF::ELFOSABI_FREEBSD;
47 return ELF::ELFOSABI_LINUX;
49 return ELF::ELFOSABI_NONE;
53 virtual ~MCELFObjectTargetWriter() {}
55 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
56 bool IsPCRel) const = 0;
58 virtual bool needsRelocateWithSymbol(const MCSymbolData &SD,
63 uint8_t getOSABI() const { return OSABI; }
64 uint16_t getEMachine() const { return EMachine; }
65 bool hasRelocationAddend() const { return HasRelocationAddend; }
66 bool is64Bit() const { return Is64Bit; }
67 bool isN64() const { return IsN64; }
70 // Instead of changing everyone's API we pack the N64 Type fields
71 // into the existing 32 bit data unsigned.
72 #define R_TYPE_SHIFT 0
73 #define R_TYPE_MASK 0xffffff00
74 #define R_TYPE2_SHIFT 8
75 #define R_TYPE2_MASK 0xffff00ff
76 #define R_TYPE3_SHIFT 16
77 #define R_TYPE3_MASK 0xff00ffff
78 #define R_SSYM_SHIFT 24
79 #define R_SSYM_MASK 0x00ffffff
81 // N64 relocation type accessors
82 uint8_t getRType(uint32_t Type) const {
83 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
85 uint8_t getRType2(uint32_t Type) const {
86 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
88 uint8_t getRType3(uint32_t Type) const {
89 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
91 uint8_t getRSsym(uint32_t Type) const {
92 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
95 // N64 relocation type setting
96 unsigned setRType(unsigned Value, unsigned Type) const {
97 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
99 unsigned setRType2(unsigned Value, unsigned Type) const {
100 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
102 unsigned setRType3(unsigned Value, unsigned Type) const {
103 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
105 unsigned setRSsym(unsigned Value, unsigned Type) const {
106 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
110 /// \brief Construct a new ELF writer instance.
112 /// \param MOTW - The target specific ELF writer subclass.
113 /// \param OS - The stream to write to.
114 /// \returns The constructed object writer.
115 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
116 raw_ostream &OS, bool IsLittleEndian);
117 } // End llvm namespace