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/BinaryFormat/ELF.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/raw_ostream.h"
32 struct ELFRelocationEntry {
33 uint64_t Offset; // Where is the relocation.
34 const MCSymbolELF *Symbol; // The symbol to relocate with.
35 unsigned Type; // The type of the relocation.
36 uint64_t Addend; // The addend to use.
37 const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it.
38 uint64_t OriginalAddend; // The original value of addend.
40 ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
41 uint64_t Addend, const MCSymbolELF *OriginalSymbol,
42 uint64_t OriginalAddend)
43 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend),
44 OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {}
46 void print(raw_ostream &Out) const {
47 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
48 << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
49 << ", OriginalAddend=" << OriginalAddend;
52 void dump() const { print(errs()); }
55 class MCELFObjectTargetWriter : public MCObjectTargetWriter {
57 const uint16_t EMachine;
58 const unsigned HasRelocationAddend : 1;
59 const unsigned Is64Bit : 1;
62 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
63 bool HasRelocationAddend);
66 virtual ~MCELFObjectTargetWriter() = default;
68 virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
69 static bool classof(const MCObjectTargetWriter *W) {
70 return W->getFormat() == Triple::ELF;
73 static uint8_t getOSABI(Triple::OSType OSType) {
75 case Triple::CloudABI:
76 return ELF::ELFOSABI_CLOUDABI;
77 case Triple::HermitCore:
78 return ELF::ELFOSABI_STANDALONE;
81 return ELF::ELFOSABI_FREEBSD;
83 return ELF::ELFOSABI_NONE;
87 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
88 const MCFixup &Fixup, bool IsPCRel) const = 0;
90 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
93 virtual void sortRelocs(const MCAssembler &Asm,
94 std::vector<ELFRelocationEntry> &Relocs);
96 virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);
100 uint8_t getOSABI() const { return OSABI; }
101 uint16_t getEMachine() const { return EMachine; }
102 bool hasRelocationAddend() const { return HasRelocationAddend; }
103 bool is64Bit() const { return Is64Bit; }
106 // Instead of changing everyone's API we pack the N64 Type fields
107 // into the existing 32 bit data unsigned.
108 #define R_TYPE_SHIFT 0
109 #define R_TYPE_MASK 0xffffff00
110 #define R_TYPE2_SHIFT 8
111 #define R_TYPE2_MASK 0xffff00ff
112 #define R_TYPE3_SHIFT 16
113 #define R_TYPE3_MASK 0xff00ffff
114 #define R_SSYM_SHIFT 24
115 #define R_SSYM_MASK 0x00ffffff
117 // N64 relocation type accessors
118 uint8_t getRType(uint32_t Type) const {
119 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
121 uint8_t getRType2(uint32_t Type) const {
122 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
124 uint8_t getRType3(uint32_t Type) const {
125 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
127 uint8_t getRSsym(uint32_t Type) const {
128 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
131 // N64 relocation type setting
132 unsigned setRType(unsigned Value, unsigned Type) const {
133 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
135 unsigned setRType2(unsigned Value, unsigned Type) const {
136 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
138 unsigned setRType3(unsigned Value, unsigned Type) const {
139 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
141 unsigned setRSsym(unsigned Value, unsigned Type) const {
142 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
146 /// Construct a new ELF writer instance.
148 /// \param MOTW - The target specific ELF writer subclass.
149 /// \param OS - The stream to write to.
150 /// \returns The constructed object writer.
151 std::unique_ptr<MCObjectWriter>
152 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
153 raw_pwrite_stream &OS, bool IsLittleEndian);
155 std::unique_ptr<MCObjectWriter>
156 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
157 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
158 bool IsLittleEndian);
160 } // end namespace llvm
162 #endif // LLVM_MC_MCELFOBJECTWRITER_H