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"
26 /// @name Relocation Data
29 struct ELFRelocationEntry {
30 // Make these big enough for both 32-bit and 64-bit
34 const MCSymbol *Symbol;
39 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0), Fixup(0) {}
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) {}
47 class MCELFObjectTargetWriter {
49 const uint16_t EMachine;
50 const unsigned HasRelocationAddend : 1;
51 const unsigned Is64Bit : 1;
52 const unsigned IsN64 : 1;
56 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
57 uint16_t EMachine_, bool HasRelocationAddend,
61 static uint8_t getOSABI(Triple::OSType OSType) {
64 return ELF::ELFOSABI_FREEBSD;
66 return ELF::ELFOSABI_LINUX;
68 return ELF::ELFOSABI_NONE;
72 virtual ~MCELFObjectTargetWriter() {}
74 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
75 bool IsPCRel, bool IsRelocWithSymbol,
76 int64_t Addend) const = 0;
77 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
78 const MCValue &Target,
82 virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
86 virtual void sortRelocs(const MCAssembler &Asm,
87 std::vector<ELFRelocationEntry> &Relocs);
91 uint8_t getOSABI() const { return OSABI; }
92 uint16_t getEMachine() const { return EMachine; }
93 bool hasRelocationAddend() const { return HasRelocationAddend; }
94 bool is64Bit() const { return Is64Bit; }
95 bool isN64() const { return IsN64; }
98 // Instead of changing everyone's API we pack the N64 Type fields
99 // into the existing 32 bit data unsigned.
100 #define R_TYPE_SHIFT 0
101 #define R_TYPE_MASK 0xffffff00
102 #define R_TYPE2_SHIFT 8
103 #define R_TYPE2_MASK 0xffff00ff
104 #define R_TYPE3_SHIFT 16
105 #define R_TYPE3_MASK 0xff00ffff
106 #define R_SSYM_SHIFT 24
107 #define R_SSYM_MASK 0x00ffffff
109 // N64 relocation type accessors
110 unsigned getRType(uint32_t Type) const {
111 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
113 unsigned getRType2(uint32_t Type) const {
114 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
116 unsigned getRType3(uint32_t Type) const {
117 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
119 unsigned getRSsym(uint32_t Type) const {
120 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
123 // N64 relocation type setting
124 unsigned setRType(unsigned Value, unsigned Type) const {
125 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
127 unsigned setRType2(unsigned Value, unsigned Type) const {
128 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
130 unsigned setRType3(unsigned Value, unsigned Type) const {
131 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
133 unsigned setRSsym(unsigned Value, unsigned Type) const {
134 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
138 /// \brief Construct a new ELF writer instance.
140 /// \param MOTW - The target specific ELF writer subclass.
141 /// \param OS - The stream to write to.
142 /// \returns The constructed object writer.
143 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
144 raw_ostream &OS, bool IsLittleEndian);
145 } // End llvm namespace