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) {}
46 // Support lexicographic sorting.
47 bool operator<(const ELFRelocationEntry &RE) const {
48 if (RE.r_offset != r_offset)
49 return RE.r_offset < r_offset;
51 return Type < RE.Type;
52 if (Index != RE.Index)
53 return Index < RE.Index;
54 llvm_unreachable("ELFRelocs might be unstable!");
59 class MCELFObjectTargetWriter {
61 const uint16_t EMachine;
62 const unsigned HasRelocationAddend : 1;
63 const unsigned Is64Bit : 1;
64 const unsigned IsN64 : 1;
68 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
69 uint16_t EMachine_, bool HasRelocationAddend,
73 static uint8_t getOSABI(Triple::OSType OSType) {
76 return ELF::ELFOSABI_FREEBSD;
78 return ELF::ELFOSABI_LINUX;
80 return ELF::ELFOSABI_NONE;
84 virtual ~MCELFObjectTargetWriter() {}
86 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
87 bool IsPCRel, bool IsRelocWithSymbol,
88 int64_t Addend) const = 0;
89 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
90 const MCValue &Target,
94 virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
97 virtual void adjustFixupOffset(const MCFixup &Fixup,
98 uint64_t &RelocOffset);
100 virtual void sortRelocs(const MCAssembler &Asm,
101 std::vector<ELFRelocationEntry> &Relocs);
105 uint8_t getOSABI() const { return OSABI; }
106 uint16_t getEMachine() const { return EMachine; }
107 bool hasRelocationAddend() const { return HasRelocationAddend; }
108 bool is64Bit() const { return Is64Bit; }
109 bool isN64() const { return IsN64; }
112 // Instead of changing everyone's API we pack the N64 Type fields
113 // into the existing 32 bit data unsigned.
114 #define R_TYPE_SHIFT 0
115 #define R_TYPE_MASK 0xffffff00
116 #define R_TYPE2_SHIFT 8
117 #define R_TYPE2_MASK 0xffff00ff
118 #define R_TYPE3_SHIFT 16
119 #define R_TYPE3_MASK 0xff00ffff
120 #define R_SSYM_SHIFT 24
121 #define R_SSYM_MASK 0x00ffffff
123 // N64 relocation type accessors
124 unsigned getRType(uint32_t Type) const {
125 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
127 unsigned getRType2(uint32_t Type) const {
128 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
130 unsigned getRType3(uint32_t Type) const {
131 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
133 unsigned getRSsym(uint32_t Type) const {
134 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
137 // N64 relocation type setting
138 unsigned setRType(unsigned Value, unsigned Type) const {
139 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
141 unsigned setRType2(unsigned Value, unsigned Type) const {
142 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
144 unsigned setRType3(unsigned Value, unsigned Type) const {
145 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
147 unsigned setRSsym(unsigned Value, unsigned Type) const {
148 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
152 /// \brief Construct a new ELF writer instance.
154 /// \param MOTW - The target specific ELF writer subclass.
155 /// \param OS - The stream to write to.
156 /// \returns The constructed object writer.
157 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
158 raw_ostream &OS, bool IsLittleEndian);
159 } // End llvm namespace