]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCELFObjectWriter.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / MC / MCELFObjectWriter.h
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/BinaryFormat/ELF.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cstdint>
19 #include <vector>
20
21 namespace llvm {
22
23 class MCAssembler;
24 class MCContext;
25 class MCFixup;
26 class MCObjectWriter;
27 class MCSymbol;
28 class MCSymbolELF;
29 class MCValue;
30
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.
38
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) {}
44
45   void print(raw_ostream &Out) const {
46     Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
47         << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
48         << ", OriginalAddend=" << OriginalAddend;
49   }
50
51   void dump() const { print(errs()); }
52 };
53
54 class MCELFObjectTargetWriter : public MCObjectTargetWriter {
55   const uint8_t OSABI;
56   const uint16_t EMachine;
57   const unsigned HasRelocationAddend : 1;
58   const unsigned Is64Bit : 1;
59
60 protected:
61   MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62                           bool HasRelocationAddend);
63
64 public:
65   virtual ~MCELFObjectTargetWriter() = default;
66
67   virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
68   static bool classof(const MCObjectTargetWriter *W) {
69     return W->getFormat() == Triple::ELF;
70   }
71
72   static uint8_t getOSABI(Triple::OSType OSType) {
73     switch (OSType) {
74       case Triple::CloudABI:
75         return ELF::ELFOSABI_CLOUDABI;
76       case Triple::PS4:
77       case Triple::FreeBSD:
78         return ELF::ELFOSABI_FREEBSD;
79       default:
80         return ELF::ELFOSABI_NONE;
81     }
82   }
83
84   virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
85                                 const MCFixup &Fixup, bool IsPCRel) const = 0;
86
87   virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
88                                        unsigned Type) const;
89
90   virtual void sortRelocs(const MCAssembler &Asm,
91                           std::vector<ELFRelocationEntry> &Relocs);
92
93   /// \name Accessors
94   /// @{
95   uint8_t getOSABI() const { return OSABI; }
96   uint16_t getEMachine() const { return EMachine; }
97   bool hasRelocationAddend() const { return HasRelocationAddend; }
98   bool is64Bit() const { return Is64Bit; }
99   /// @}
100
101   // Instead of changing everyone's API we pack the N64 Type fields
102   // into the existing 32 bit data unsigned.
103 #define R_TYPE_SHIFT 0
104 #define R_TYPE_MASK 0xffffff00
105 #define R_TYPE2_SHIFT 8
106 #define R_TYPE2_MASK 0xffff00ff
107 #define R_TYPE3_SHIFT 16
108 #define R_TYPE3_MASK 0xff00ffff
109 #define R_SSYM_SHIFT 24
110 #define R_SSYM_MASK 0x00ffffff
111
112   // N64 relocation type accessors
113   uint8_t getRType(uint32_t Type) const {
114     return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
115   }
116   uint8_t getRType2(uint32_t Type) const {
117     return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
118   }
119   uint8_t getRType3(uint32_t Type) const {
120     return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
121   }
122   uint8_t getRSsym(uint32_t Type) const {
123     return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
124   }
125
126   // N64 relocation type setting
127   unsigned setRType(unsigned Value, unsigned Type) const {
128     return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
129   }
130   unsigned setRType2(unsigned Value, unsigned Type) const {
131     return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
132   }
133   unsigned setRType3(unsigned Value, unsigned Type) const {
134     return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
135   }
136   unsigned setRSsym(unsigned Value, unsigned Type) const {
137     return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
138   }
139 };
140
141 /// Construct a new ELF writer instance.
142 ///
143 /// \param MOTW - The target specific ELF writer subclass.
144 /// \param OS - The stream to write to.
145 /// \returns The constructed object writer.
146 std::unique_ptr<MCObjectWriter>
147 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
148                       raw_pwrite_stream &OS, bool IsLittleEndian);
149
150 std::unique_ptr<MCObjectWriter>
151 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
152                          raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
153                          bool IsLittleEndian);
154
155 } // end namespace llvm
156
157 #endif // LLVM_MC_MCELFOBJECTWRITER_H