1 //===- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_MC_MCELFOBJECTWRITER_H 10 #define LLVM_MC_MCELFOBJECTWRITER_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/SmallVector.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" 19 #include "llvm/TargetParser/Triple.h" 20 #include <cstdint> 21 #include <memory> 22 #include <optional> 23 #include <vector> 24 25 namespace llvm { 26 27 class MCAssembler; 28 class MCContext; 29 class MCFixup; 30 class MCSymbol; 31 class MCSymbolELF; 32 class MCTargetOptions; 33 class MCValue; 34 35 struct ELFRelocationEntry { 36 uint64_t Offset; // Where is the relocation. 37 const MCSymbolELF *Symbol; // The symbol to relocate with. 38 unsigned Type; // The type of the relocation. 39 uint64_t Addend; // The addend to use. 40 41 ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, 42 uint64_t Addend) 43 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {} 44 45 void print(raw_ostream &Out) const { 46 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type 47 << ", Addend=" << Addend; 48 } 49 50 LLVM_DUMP_METHOD void dump() const { print(errs()); } 51 }; 52 53 class MCELFObjectTargetWriter : public MCObjectTargetWriter { 54 const uint8_t OSABI; 55 const uint8_t ABIVersion; 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_, uint8_t ABIVersion_ = 0); 63 64 public: 65 virtual ~MCELFObjectTargetWriter() = default; 66 67 Triple::ObjectFormatType getFormat() const override { 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::HermitCore: 75 return ELF::ELFOSABI_STANDALONE; 76 case Triple::PS4: 77 case Triple::FreeBSD: 78 return ELF::ELFOSABI_FREEBSD; 79 case Triple::Solaris: 80 return ELF::ELFOSABI_SOLARIS; 81 case Triple::OpenBSD: 82 return ELF::ELFOSABI_OPENBSD; 83 default: 84 return ELF::ELFOSABI_NONE; 85 } 86 } 87 88 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 89 const MCFixup &Fixup, bool IsPCRel) const = 0; 90 91 virtual bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym, 92 unsigned Type) const; 93 94 virtual void sortRelocs(const MCAssembler &Asm, 95 std::vector<ELFRelocationEntry> &Relocs); 96 97 /// \name Accessors 98 /// @{ 99 uint8_t getOSABI() const { return OSABI; } 100 uint8_t getABIVersion() const { return ABIVersion; } 101 uint16_t getEMachine() const { return EMachine; } 102 bool hasRelocationAddend() const { return HasRelocationAddend; } 103 bool is64Bit() const { return Is64Bit; } 104 /// @} 105 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 116 117 // N64 relocation type accessors 118 uint8_t getRType(uint32_t Type) const { 119 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff); 120 } 121 uint8_t getRType2(uint32_t Type) const { 122 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff); 123 } 124 uint8_t getRType3(uint32_t Type) const { 125 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff); 126 } 127 uint8_t getRSsym(uint32_t Type) const { 128 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff); 129 } 130 131 // N64 relocation type setting 132 static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3) { 133 return ((Value1 & 0xff) << R_TYPE_SHIFT) | 134 ((Value2 & 0xff) << R_TYPE2_SHIFT) | 135 ((Value3 & 0xff) << R_TYPE3_SHIFT); 136 } 137 unsigned setRSsym(unsigned Value, unsigned Type) const { 138 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT); 139 } 140 }; 141 142 class ELFObjectWriter final : public MCObjectWriter { 143 unsigned ELFHeaderEFlags = 0; 144 145 public: 146 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter; 147 raw_pwrite_stream &OS; 148 raw_pwrite_stream *DwoOS = nullptr; 149 150 DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations; 151 DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames; 152 bool IsLittleEndian = false; 153 bool SeenGnuAbi = false; 154 std::optional<uint8_t> OverrideABIVersion; 155 156 struct Symver { 157 SMLoc Loc; 158 const MCSymbol *Sym; 159 StringRef Name; 160 // True if .symver *, *@@@* or .symver *, *, remove. 161 bool KeepOriginalSym; 162 }; 163 SmallVector<Symver, 0> Symvers; 164 165 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, 166 raw_pwrite_stream &OS, bool IsLittleEndian); 167 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, 168 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, 169 bool IsLittleEndian); 170 171 void reset() override; 172 void executePostLayoutBinding(MCAssembler &Asm) override; 173 void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment, 174 const MCFixup &Fixup, MCValue Target, 175 uint64_t &FixedValue) override; 176 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, 177 const MCSymbol &SymA, 178 const MCFragment &FB, bool InSet, 179 bool IsPCRel) const override; 180 uint64_t writeObject(MCAssembler &Asm) override; 181 182 bool hasRelocationAddend() const; 183 bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const; 184 185 bool shouldRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val, 186 const MCSymbolELF *Sym, uint64_t C, 187 unsigned Type) const; 188 189 bool checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From, 190 const MCSectionELF *To); 191 192 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; } 193 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; } 194 195 // Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN, STB_GNU_UNIQUE). 196 void markGnuAbi() { SeenGnuAbi = true; } 197 bool seenGnuAbi() const { return SeenGnuAbi; } 198 199 // Override the default e_ident[EI_ABIVERSION] in the ELF header. 200 void setOverrideABIVersion(uint8_t V) { OverrideABIVersion = V; } 201 }; 202 } // end namespace llvm 203 204 #endif // LLVM_MC_MCELFOBJECTWRITER_H 205