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 MCELFObjectTargetWriter {
29 const uint16_t EMachine;
30 const unsigned HasRelocationAddend : 1;
31 const unsigned Is64Bit : 1;
32 const unsigned IsN64 : 1;
36 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
37 uint16_t EMachine_, bool HasRelocationAddend,
41 static uint8_t getOSABI(Triple::OSType OSType) {
44 return ELF::ELFOSABI_FREEBSD;
46 return ELF::ELFOSABI_LINUX;
48 return ELF::ELFOSABI_NONE;
52 virtual ~MCELFObjectTargetWriter() {}
54 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
55 bool IsPCRel) const = 0;
57 virtual bool needsRelocateWithSymbol(unsigned Type) const;
61 uint8_t getOSABI() const { return OSABI; }
62 uint16_t getEMachine() const { return EMachine; }
63 bool hasRelocationAddend() const { return HasRelocationAddend; }
64 bool is64Bit() const { return Is64Bit; }
65 bool isN64() const { return IsN64; }
68 // Instead of changing everyone's API we pack the N64 Type fields
69 // into the existing 32 bit data unsigned.
70 #define R_TYPE_SHIFT 0
71 #define R_TYPE_MASK 0xffffff00
72 #define R_TYPE2_SHIFT 8
73 #define R_TYPE2_MASK 0xffff00ff
74 #define R_TYPE3_SHIFT 16
75 #define R_TYPE3_MASK 0xff00ffff
76 #define R_SSYM_SHIFT 24
77 #define R_SSYM_MASK 0x00ffffff
79 // N64 relocation type accessors
80 uint8_t getRType(uint32_t Type) const {
81 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
83 uint8_t getRType2(uint32_t Type) const {
84 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
86 uint8_t getRType3(uint32_t Type) const {
87 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
89 uint8_t getRSsym(uint32_t Type) const {
90 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
93 // N64 relocation type setting
94 unsigned setRType(unsigned Value, unsigned Type) const {
95 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
97 unsigned setRType2(unsigned Value, unsigned Type) const {
98 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
100 unsigned setRType3(unsigned Value, unsigned Type) const {
101 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
103 unsigned setRSsym(unsigned Value, unsigned Type) const {
104 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
108 /// \brief Construct a new ELF writer instance.
110 /// \param MOTW - The target specific ELF writer subclass.
111 /// \param OS - The stream to write to.
112 /// \returns The constructed object writer.
113 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
114 raw_ostream &OS, bool IsLittleEndian);
115 } // End llvm namespace