Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ----------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_MC_MCELFOBJECTWRITER_H |
| 10 | #define LLVM_MC_MCELFOBJECTWRITER_H |
| 11 | |
| 12 | #include "llvm/ADT/Triple.h" |
| 13 | #include "llvm/BinaryFormat/ELF.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 14 | #include "llvm/MC/MCObjectWriter.h" |
| 15 | #include "llvm/MC/MCSectionELF.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 54 | class MCELFObjectTargetWriter : public MCObjectTargetWriter { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | const uint8_t OSABI; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 56 | const uint8_t ABIVersion; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | const uint16_t EMachine; |
| 58 | const unsigned HasRelocationAddend : 1; |
| 59 | const unsigned Is64Bit : 1; |
| 60 | |
| 61 | protected: |
| 62 | MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 63 | bool HasRelocationAddend_, uint8_t ABIVersion_ = 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 64 | |
| 65 | public: |
| 66 | virtual ~MCELFObjectTargetWriter() = default; |
| 67 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 68 | virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; } |
| 69 | static bool classof(const MCObjectTargetWriter *W) { |
| 70 | return W->getFormat() == Triple::ELF; |
| 71 | } |
| 72 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | static uint8_t getOSABI(Triple::OSType OSType) { |
| 74 | switch (OSType) { |
| 75 | case Triple::CloudABI: |
| 76 | return ELF::ELFOSABI_CLOUDABI; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 77 | case Triple::HermitCore: |
| 78 | return ELF::ELFOSABI_STANDALONE; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | case Triple::PS4: |
| 80 | case Triple::FreeBSD: |
| 81 | return ELF::ELFOSABI_FREEBSD; |
| 82 | default: |
| 83 | return ELF::ELFOSABI_NONE; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 88 | const MCFixup &Fixup, bool IsPCRel) const = 0; |
| 89 | |
| 90 | virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, |
| 91 | unsigned Type) const; |
| 92 | |
| 93 | virtual void sortRelocs(const MCAssembler &Asm, |
| 94 | std::vector<ELFRelocationEntry> &Relocs); |
| 95 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 96 | virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec); |
| 97 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 98 | /// \name Accessors |
| 99 | /// @{ |
| 100 | uint8_t getOSABI() const { return OSABI; } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 101 | uint8_t getABIVersion() const { return ABIVersion; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | uint16_t getEMachine() const { return EMachine; } |
| 103 | bool hasRelocationAddend() const { return HasRelocationAddend; } |
| 104 | bool is64Bit() const { return Is64Bit; } |
| 105 | /// @} |
| 106 | |
| 107 | // Instead of changing everyone's API we pack the N64 Type fields |
| 108 | // into the existing 32 bit data unsigned. |
| 109 | #define R_TYPE_SHIFT 0 |
| 110 | #define R_TYPE_MASK 0xffffff00 |
| 111 | #define R_TYPE2_SHIFT 8 |
| 112 | #define R_TYPE2_MASK 0xffff00ff |
| 113 | #define R_TYPE3_SHIFT 16 |
| 114 | #define R_TYPE3_MASK 0xff00ffff |
| 115 | #define R_SSYM_SHIFT 24 |
| 116 | #define R_SSYM_MASK 0x00ffffff |
| 117 | |
| 118 | // N64 relocation type accessors |
| 119 | uint8_t getRType(uint32_t Type) const { |
| 120 | return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff); |
| 121 | } |
| 122 | uint8_t getRType2(uint32_t Type) const { |
| 123 | return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff); |
| 124 | } |
| 125 | uint8_t getRType3(uint32_t Type) const { |
| 126 | return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff); |
| 127 | } |
| 128 | uint8_t getRSsym(uint32_t Type) const { |
| 129 | return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff); |
| 130 | } |
| 131 | |
| 132 | // N64 relocation type setting |
| 133 | unsigned setRType(unsigned Value, unsigned Type) const { |
| 134 | return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT)); |
| 135 | } |
| 136 | unsigned setRType2(unsigned Value, unsigned Type) const { |
| 137 | return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT); |
| 138 | } |
| 139 | unsigned setRType3(unsigned Value, unsigned Type) const { |
| 140 | return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT); |
| 141 | } |
| 142 | unsigned setRSsym(unsigned Value, unsigned Type) const { |
| 143 | return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT); |
| 144 | } |
| 145 | }; |
| 146 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 147 | /// Construct a new ELF writer instance. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 148 | /// |
| 149 | /// \param MOTW - The target specific ELF writer subclass. |
| 150 | /// \param OS - The stream to write to. |
| 151 | /// \returns The constructed object writer. |
| 152 | std::unique_ptr<MCObjectWriter> |
| 153 | createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 154 | raw_pwrite_stream &OS, bool IsLittleEndian); |
| 155 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 156 | std::unique_ptr<MCObjectWriter> |
| 157 | createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 158 | raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, |
| 159 | bool IsLittleEndian); |
| 160 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 161 | } // end namespace llvm |
| 162 | |
| 163 | #endif // LLVM_MC_MCELFOBJECTWRITER_H |