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/MC/MCValue.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/ELF.h"
17 #include "llvm/Support/raw_ostream.h"
29 class raw_pwrite_stream;
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;
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;
62 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
63 uint16_t EMachine_, bool HasRelocationAddend,
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 ~MCELFObjectTargetWriter() {}
81 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
82 const MCFixup &Fixup, bool IsPCRel) const = 0;
84 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
87 virtual void sortRelocs(const MCAssembler &Asm,
88 std::vector<ELFRelocationEntry> &Relocs);
92 uint8_t getOSABI() const { return OSABI; }
93 uint16_t getEMachine() const { return EMachine; }
94 bool hasRelocationAddend() const { return HasRelocationAddend; }
95 bool is64Bit() const { return Is64Bit; }
96 bool isN64() const { return IsN64; }
99 // Instead of changing everyone's API we pack the N64 Type fields
100 // into the existing 32 bit data unsigned.
101 #define R_TYPE_SHIFT 0
102 #define R_TYPE_MASK 0xffffff00
103 #define R_TYPE2_SHIFT 8
104 #define R_TYPE2_MASK 0xffff00ff
105 #define R_TYPE3_SHIFT 16
106 #define R_TYPE3_MASK 0xff00ffff
107 #define R_SSYM_SHIFT 24
108 #define R_SSYM_MASK 0x00ffffff
110 // N64 relocation type accessors
111 uint8_t getRType(uint32_t Type) const {
112 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
114 uint8_t getRType2(uint32_t Type) const {
115 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
117 uint8_t getRType3(uint32_t Type) const {
118 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
120 uint8_t getRSsym(uint32_t Type) const {
121 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
124 // N64 relocation type setting
125 unsigned setRType(unsigned Value, unsigned Type) const {
126 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
128 unsigned setRType2(unsigned Value, unsigned Type) const {
129 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
131 unsigned setRType3(unsigned Value, unsigned Type) const {
132 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
134 unsigned setRSsym(unsigned Value, unsigned Type) const {
135 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
139 /// \brief Construct a new ELF writer instance.
141 /// \param MOTW - The target specific ELF writer subclass.
142 /// \param OS - The stream to write to.
143 /// \returns The constructed object writer.
144 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
145 raw_pwrite_stream &OS,
146 bool IsLittleEndian);
147 } // End llvm namespace