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"
27 class raw_pwrite_stream;
29 struct ELFRelocationEntry {
30 uint64_t Offset; // Where is the relocation.
31 const MCSymbol *Symbol; // The symbol to relocate with.
32 unsigned Type; // The type of the relocation.
33 uint64_t Addend; // The addend to use.
35 ELFRelocationEntry(uint64_t Offset, const MCSymbol *Symbol, unsigned Type,
37 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
40 class MCELFObjectTargetWriter {
42 const uint16_t EMachine;
43 const unsigned HasRelocationAddend : 1;
44 const unsigned Is64Bit : 1;
45 const unsigned IsN64 : 1;
49 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
50 uint16_t EMachine_, bool HasRelocationAddend,
54 static uint8_t getOSABI(Triple::OSType OSType) {
56 case Triple::CloudABI:
57 return ELF::ELFOSABI_CLOUDABI;
60 return ELF::ELFOSABI_FREEBSD;
62 return ELF::ELFOSABI_LINUX;
64 return ELF::ELFOSABI_NONE;
68 virtual ~MCELFObjectTargetWriter() {}
70 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
71 bool IsPCRel) const = 0;
73 virtual bool needsRelocateWithSymbol(const MCSymbolData &SD,
76 virtual void sortRelocs(const MCAssembler &Asm,
77 std::vector<ELFRelocationEntry> &Relocs);
81 uint8_t getOSABI() const { return OSABI; }
82 uint16_t getEMachine() const { return EMachine; }
83 bool hasRelocationAddend() const { return HasRelocationAddend; }
84 bool is64Bit() const { return Is64Bit; }
85 bool isN64() const { return IsN64; }
88 // Instead of changing everyone's API we pack the N64 Type fields
89 // into the existing 32 bit data unsigned.
90 #define R_TYPE_SHIFT 0
91 #define R_TYPE_MASK 0xffffff00
92 #define R_TYPE2_SHIFT 8
93 #define R_TYPE2_MASK 0xffff00ff
94 #define R_TYPE3_SHIFT 16
95 #define R_TYPE3_MASK 0xff00ffff
96 #define R_SSYM_SHIFT 24
97 #define R_SSYM_MASK 0x00ffffff
99 // N64 relocation type accessors
100 uint8_t getRType(uint32_t Type) const {
101 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
103 uint8_t getRType2(uint32_t Type) const {
104 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
106 uint8_t getRType3(uint32_t Type) const {
107 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
109 uint8_t getRSsym(uint32_t Type) const {
110 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
113 // N64 relocation type setting
114 unsigned setRType(unsigned Value, unsigned Type) const {
115 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
117 unsigned setRType2(unsigned Value, unsigned Type) const {
118 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
120 unsigned setRType3(unsigned Value, unsigned Type) const {
121 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
123 unsigned setRSsym(unsigned Value, unsigned Type) const {
124 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
128 /// \brief Construct a new ELF writer instance.
130 /// \param MOTW - The target specific ELF writer subclass.
131 /// \param OS - The stream to write to.
132 /// \returns The constructed object writer.
133 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
134 raw_pwrite_stream &OS,
135 bool IsLittleEndian);
136 } // End llvm namespace