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 class raw_pwrite_stream;
28 struct ELFRelocationEntry {
29 uint64_t Offset; // Where is the relocation.
30 const MCSymbolELF *Symbol; // The symbol to relocate with.
31 unsigned Type; // The type of the relocation.
32 uint64_t Addend; // The addend to use.
34 ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
36 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
39 class MCELFObjectTargetWriter {
41 const uint16_t EMachine;
42 const unsigned HasRelocationAddend : 1;
43 const unsigned Is64Bit : 1;
44 const unsigned IsN64 : 1;
48 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
49 uint16_t EMachine_, bool HasRelocationAddend,
53 static uint8_t getOSABI(Triple::OSType OSType) {
55 case Triple::CloudABI:
56 return ELF::ELFOSABI_CLOUDABI;
59 return ELF::ELFOSABI_FREEBSD;
61 return ELF::ELFOSABI_LINUX;
63 return ELF::ELFOSABI_NONE;
67 virtual ~MCELFObjectTargetWriter() {}
69 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
70 bool IsPCRel) const = 0;
72 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
75 virtual void sortRelocs(const MCAssembler &Asm,
76 std::vector<ELFRelocationEntry> &Relocs);
80 uint8_t getOSABI() const { return OSABI; }
81 uint16_t getEMachine() const { return EMachine; }
82 bool hasRelocationAddend() const { return HasRelocationAddend; }
83 bool is64Bit() const { return Is64Bit; }
84 bool isN64() const { return IsN64; }
87 // Instead of changing everyone's API we pack the N64 Type fields
88 // into the existing 32 bit data unsigned.
89 #define R_TYPE_SHIFT 0
90 #define R_TYPE_MASK 0xffffff00
91 #define R_TYPE2_SHIFT 8
92 #define R_TYPE2_MASK 0xffff00ff
93 #define R_TYPE3_SHIFT 16
94 #define R_TYPE3_MASK 0xff00ffff
95 #define R_SSYM_SHIFT 24
96 #define R_SSYM_MASK 0x00ffffff
98 // N64 relocation type accessors
99 uint8_t getRType(uint32_t Type) const {
100 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
102 uint8_t getRType2(uint32_t Type) const {
103 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
105 uint8_t getRType3(uint32_t Type) const {
106 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
108 uint8_t getRSsym(uint32_t Type) const {
109 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
112 // N64 relocation type setting
113 unsigned setRType(unsigned Value, unsigned Type) const {
114 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
116 unsigned setRType2(unsigned Value, unsigned Type) const {
117 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
119 unsigned setRType3(unsigned Value, unsigned Type) const {
120 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
122 unsigned setRSsym(unsigned Value, unsigned Type) const {
123 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
127 /// \brief Construct a new ELF writer instance.
129 /// \param MOTW - The target specific ELF writer subclass.
130 /// \param OS - The stream to write to.
131 /// \returns The constructed object writer.
132 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
133 raw_pwrite_stream &OS,
134 bool IsLittleEndian);
135 } // End llvm namespace