• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCELFOBJECTWRITER_H
11 #define LLVM_MC_MCELFOBJECTWRITER_H
12 
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/ELF.h"
16 #include <vector>
17 
18 namespace llvm {
19 class MCAssembler;
20 class MCFixup;
21 class MCFragment;
22 class MCObjectWriter;
23 class MCSectionData;
24 class MCSymbol;
25 class MCValue;
26 
27 class MCELFObjectTargetWriter {
28   const uint8_t OSABI;
29   const uint16_t EMachine;
30   const unsigned HasRelocationAddend : 1;
31   const unsigned Is64Bit : 1;
32   const unsigned IsN64 : 1;
33 
34 protected:
35 
36   MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
37                           uint16_t EMachine_,  bool HasRelocationAddend,
38                           bool IsN64=false);
39 
40 public:
getOSABI(Triple::OSType OSType)41   static uint8_t getOSABI(Triple::OSType OSType) {
42     switch (OSType) {
43       case Triple::FreeBSD:
44         return ELF::ELFOSABI_FREEBSD;
45       case Triple::Linux:
46         return ELF::ELFOSABI_LINUX;
47       default:
48         return ELF::ELFOSABI_NONE;
49     }
50   }
51 
~MCELFObjectTargetWriter()52   virtual ~MCELFObjectTargetWriter() {}
53 
54   virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
55                                 bool IsPCRel) const = 0;
56 
57   virtual bool needsRelocateWithSymbol(unsigned Type) const;
58 
59   /// @name Accessors
60   /// @{
getOSABI()61   uint8_t getOSABI() const { return OSABI; }
getEMachine()62   uint16_t getEMachine() const { return EMachine; }
hasRelocationAddend()63   bool hasRelocationAddend() const { return HasRelocationAddend; }
is64Bit()64   bool is64Bit() const { return Is64Bit; }
isN64()65   bool isN64() const { return IsN64; }
66   /// @}
67 
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
78 
79   // N64 relocation type accessors
getRType(uint32_t Type)80   uint8_t getRType(uint32_t Type) const {
81     return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
82   }
getRType2(uint32_t Type)83   uint8_t getRType2(uint32_t Type) const {
84     return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
85   }
getRType3(uint32_t Type)86   uint8_t getRType3(uint32_t Type) const {
87     return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
88   }
getRSsym(uint32_t Type)89   uint8_t getRSsym(uint32_t Type) const {
90     return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
91   }
92 
93   // N64 relocation type setting
setRType(unsigned Value,unsigned Type)94   unsigned setRType(unsigned Value, unsigned Type) const {
95     return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
96   }
setRType2(unsigned Value,unsigned Type)97   unsigned setRType2(unsigned Value, unsigned Type) const {
98     return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
99   }
setRType3(unsigned Value,unsigned Type)100   unsigned setRType3(unsigned Value, unsigned Type) const {
101     return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
102   }
setRSsym(unsigned Value,unsigned Type)103   unsigned setRSsym(unsigned Value, unsigned Type) const {
104     return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
105   }
106 };
107 
108 /// \brief Construct a new ELF writer instance.
109 ///
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
116 
117 #endif
118