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;
58 const unsigned IsN64 : 1;
61 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62 bool HasRelocationAddend, bool IsN64 = false);
65 virtual ~MCELFObjectTargetWriter() = default;
67 static uint8_t getOSABI(Triple::OSType OSType) {
69 case Triple::CloudABI:
70 return ELF::ELFOSABI_CLOUDABI;
73 return ELF::ELFOSABI_FREEBSD;
75 return ELF::ELFOSABI_NONE;
79 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
80 const MCFixup &Fixup, bool IsPCRel) const = 0;
82 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
85 virtual void sortRelocs(const MCAssembler &Asm,
86 std::vector<ELFRelocationEntry> &Relocs);
90 uint8_t getOSABI() const { return OSABI; }
91 uint16_t getEMachine() const { return EMachine; }
92 bool hasRelocationAddend() const { return HasRelocationAddend; }
93 bool is64Bit() const { return Is64Bit; }
94 bool isN64() const { return IsN64; }
97 // Instead of changing everyone's API we pack the N64 Type fields
98 // into the existing 32 bit data unsigned.
99 #define R_TYPE_SHIFT 0
100 #define R_TYPE_MASK 0xffffff00
101 #define R_TYPE2_SHIFT 8
102 #define R_TYPE2_MASK 0xffff00ff
103 #define R_TYPE3_SHIFT 16
104 #define R_TYPE3_MASK 0xff00ffff
105 #define R_SSYM_SHIFT 24
106 #define R_SSYM_MASK 0x00ffffff
108 // N64 relocation type accessors
109 uint8_t getRType(uint32_t Type) const {
110 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
112 uint8_t getRType2(uint32_t Type) const {
113 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
115 uint8_t getRType3(uint32_t Type) const {
116 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
118 uint8_t getRSsym(uint32_t Type) const {
119 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
122 // N64 relocation type setting
123 unsigned setRType(unsigned Value, unsigned Type) const {
124 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
126 unsigned setRType2(unsigned Value, unsigned Type) const {
127 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
129 unsigned setRType3(unsigned Value, unsigned Type) const {
130 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
132 unsigned setRSsym(unsigned Value, unsigned Type) const {
133 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
137 /// \brief Construct a new ELF writer instance.
139 /// \param MOTW - The target specific ELF writer subclass.
140 /// \param OS - The stream to write to.
141 /// \returns The constructed object writer.
142 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
143 raw_pwrite_stream &OS,
144 bool IsLittleEndian);
146 } // end namespace llvm
148 #endif // LLVM_MC_MCELFOBJECTWRITER_H