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/Support/Casting.h"
16 #include "llvm/Support/raw_ostream.h"
30 struct ELFRelocationEntry {
31 uint64_t Offset; // Where is the relocation.
32 const MCSymbolELF *Symbol; // The symbol to relocate with.
33 unsigned Type; // The type of the relocation.
34 uint64_t Addend; // The addend to use.
35 const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it.
36 uint64_t OriginalAddend; // The original value of addend.
38 ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
39 uint64_t Addend, const MCSymbolELF *OriginalSymbol,
40 uint64_t OriginalAddend)
41 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend),
42 OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {}
44 void print(raw_ostream &Out) const {
45 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
46 << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
47 << ", OriginalAddend=" << OriginalAddend;
50 void dump() const { print(errs()); }
53 class MCELFObjectTargetWriter {
55 const uint16_t EMachine;
56 const unsigned HasRelocationAddend : 1;
57 const unsigned Is64Bit : 1;
60 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
61 bool HasRelocationAddend);
64 virtual ~MCELFObjectTargetWriter() = default;
66 static uint8_t getOSABI(Triple::OSType OSType) {
68 case Triple::CloudABI:
69 return ELF::ELFOSABI_CLOUDABI;
72 return ELF::ELFOSABI_FREEBSD;
74 return ELF::ELFOSABI_NONE;
78 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
79 const MCFixup &Fixup, bool IsPCRel) const = 0;
81 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
84 virtual void sortRelocs(const MCAssembler &Asm,
85 std::vector<ELFRelocationEntry> &Relocs);
89 uint8_t getOSABI() const { return OSABI; }
90 uint16_t getEMachine() const { return EMachine; }
91 bool hasRelocationAddend() const { return HasRelocationAddend; }
92 bool is64Bit() const { return Is64Bit; }
95 // Instead of changing everyone's API we pack the N64 Type fields
96 // into the existing 32 bit data unsigned.
97 #define R_TYPE_SHIFT 0
98 #define R_TYPE_MASK 0xffffff00
99 #define R_TYPE2_SHIFT 8
100 #define R_TYPE2_MASK 0xffff00ff
101 #define R_TYPE3_SHIFT 16
102 #define R_TYPE3_MASK 0xff00ffff
103 #define R_SSYM_SHIFT 24
104 #define R_SSYM_MASK 0x00ffffff
106 // N64 relocation type accessors
107 uint8_t getRType(uint32_t Type) const {
108 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
110 uint8_t getRType2(uint32_t Type) const {
111 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
113 uint8_t getRType3(uint32_t Type) const {
114 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
116 uint8_t getRSsym(uint32_t Type) const {
117 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
120 // N64 relocation type setting
121 unsigned setRType(unsigned Value, unsigned Type) const {
122 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
124 unsigned setRType2(unsigned Value, unsigned Type) const {
125 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
127 unsigned setRType3(unsigned Value, unsigned Type) const {
128 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
130 unsigned setRSsym(unsigned Value, unsigned Type) const {
131 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
135 /// \brief Construct a new ELF writer instance.
137 /// \param MOTW - The target specific ELF writer subclass.
138 /// \param OS - The stream to write to.
139 /// \returns The constructed object writer.
140 std::unique_ptr<MCObjectWriter>
141 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
142 raw_pwrite_stream &OS, bool IsLittleEndian);
144 } // end namespace llvm
146 #endif // LLVM_MC_MCELFOBJECTWRITER_H