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/BinaryFormat/ELF.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/raw_ostream.h"
31 struct ELFRelocationEntry {
32 uint64_t Offset; // Where is the relocation.
33 const MCSymbolELF *Symbol; // The symbol to relocate with.
34 unsigned Type; // The type of the relocation.
35 uint64_t Addend; // The addend to use.
36 const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it.
37 uint64_t OriginalAddend; // The original value of addend.
39 ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
40 uint64_t Addend, const MCSymbolELF *OriginalSymbol,
41 uint64_t OriginalAddend)
42 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend),
43 OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {}
45 void print(raw_ostream &Out) const {
46 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
47 << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
48 << ", OriginalAddend=" << OriginalAddend;
51 void dump() const { print(errs()); }
54 class MCELFObjectTargetWriter : public MCObjectTargetWriter {
56 const uint16_t EMachine;
57 const unsigned HasRelocationAddend : 1;
58 const unsigned Is64Bit : 1;
61 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62 bool HasRelocationAddend);
65 virtual ~MCELFObjectTargetWriter() = default;
67 virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
68 static bool classof(const MCObjectTargetWriter *W) {
69 return W->getFormat() == Triple::ELF;
72 static uint8_t getOSABI(Triple::OSType OSType) {
74 case Triple::CloudABI:
75 return ELF::ELFOSABI_CLOUDABI;
78 return ELF::ELFOSABI_FREEBSD;
80 return ELF::ELFOSABI_NONE;
84 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
85 const MCFixup &Fixup, bool IsPCRel) const = 0;
87 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
90 virtual void sortRelocs(const MCAssembler &Asm,
91 std::vector<ELFRelocationEntry> &Relocs);
95 uint8_t getOSABI() const { return OSABI; }
96 uint16_t getEMachine() const { return EMachine; }
97 bool hasRelocationAddend() const { return HasRelocationAddend; }
98 bool is64Bit() const { return Is64Bit; }
101 // Instead of changing everyone's API we pack the N64 Type fields
102 // into the existing 32 bit data unsigned.
103 #define R_TYPE_SHIFT 0
104 #define R_TYPE_MASK 0xffffff00
105 #define R_TYPE2_SHIFT 8
106 #define R_TYPE2_MASK 0xffff00ff
107 #define R_TYPE3_SHIFT 16
108 #define R_TYPE3_MASK 0xff00ffff
109 #define R_SSYM_SHIFT 24
110 #define R_SSYM_MASK 0x00ffffff
112 // N64 relocation type accessors
113 uint8_t getRType(uint32_t Type) const {
114 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
116 uint8_t getRType2(uint32_t Type) const {
117 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
119 uint8_t getRType3(uint32_t Type) const {
120 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
122 uint8_t getRSsym(uint32_t Type) const {
123 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
126 // N64 relocation type setting
127 unsigned setRType(unsigned Value, unsigned Type) const {
128 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
130 unsigned setRType2(unsigned Value, unsigned Type) const {
131 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
133 unsigned setRType3(unsigned Value, unsigned Type) const {
134 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
136 unsigned setRSsym(unsigned Value, unsigned Type) const {
137 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
141 /// Construct a new ELF writer instance.
143 /// \param MOTW - The target specific ELF writer subclass.
144 /// \param OS - The stream to write to.
145 /// \returns The constructed object writer.
146 std::unique_ptr<MCObjectWriter>
147 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
148 raw_pwrite_stream &OS, bool IsLittleEndian);
150 std::unique_ptr<MCObjectWriter>
151 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
152 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
153 bool IsLittleEndian);
155 } // end namespace llvm
157 #endif // LLVM_MC_MCELFOBJECTWRITER_H