xref: /freebsd/contrib/llvm-project/llvm/include/llvm/MC/MCELFObjectWriter.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
ELFRelocationEntryELFRelocationEntry41   ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
42                      uint64_t Addend)
43       : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
44 
printELFRelocationEntry45   void print(raw_ostream &Out) const {
46     Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
47         << ", Addend=" << Addend;
48   }
49 
dumpELFRelocationEntry50   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 
getFormat()67   Triple::ObjectFormatType getFormat() const override { return Triple::ELF; }
classof(const MCObjectTargetWriter * W)68   static bool classof(const MCObjectTargetWriter *W) {
69     return W->getFormat() == Triple::ELF;
70   }
71 
getOSABI(Triple::OSType OSType)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(const MCFixup &Fixup, const MCValue &Target,
89                                 bool IsPCRel) const = 0;
90 
needsRelocateWithSymbol(const MCValue &,unsigned Type)91   virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const {
92     return false;
93   }
94 
95   virtual void sortRelocs(std::vector<ELFRelocationEntry> &Relocs);
96 
97   /// \name Accessors
98   /// @{
getOSABI()99   uint8_t getOSABI() const { return OSABI; }
getABIVersion()100   uint8_t getABIVersion() const { return ABIVersion; }
getEMachine()101   uint16_t getEMachine() const { return EMachine; }
hasRelocationAddend()102   bool hasRelocationAddend() const { return HasRelocationAddend; }
is64Bit()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
getRType(uint32_t Type)118   uint8_t getRType(uint32_t Type) const {
119     return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
120   }
getRType2(uint32_t Type)121   uint8_t getRType2(uint32_t Type) const {
122     return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
123   }
getRType3(uint32_t Type)124   uint8_t getRType3(uint32_t Type) const {
125     return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
126   }
getRSsym(uint32_t Type)127   uint8_t getRSsym(uint32_t Type) const {
128     return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
129   }
130 
131   // N64 relocation type setting
setRTypes(unsigned Value1,unsigned Value2,unsigned Value3)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   }
setRSsym(unsigned Value,unsigned Type)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   // .weakref aliases
153   SmallVector<const MCSymbolELF *, 0> Weakrefs;
154   bool IsLittleEndian = false;
155   bool SeenGnuAbi = false;
156   std::optional<uint8_t> OverrideABIVersion;
157 
158   struct Symver {
159     SMLoc Loc;
160     const MCSymbol *Sym;
161     StringRef Name;
162     // True if .symver *, *@@@* or .symver *, *, remove.
163     bool KeepOriginalSym;
164   };
165   SmallVector<Symver, 0> Symvers;
166 
167   ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
168                   raw_pwrite_stream &OS, bool IsLittleEndian);
169   ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
170                   raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
171                   bool IsLittleEndian);
172 
173   void reset() override;
174   void setAssembler(MCAssembler *Asm) override;
175   void executePostLayoutBinding() override;
176   void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
177                         MCValue Target, uint64_t &FixedValue) override;
178   bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA,
179                                               const MCFragment &FB, bool InSet,
180                                               bool IsPCRel) const override;
181   uint64_t writeObject() override;
182 
183   bool hasRelocationAddend() const;
184   bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const;
185 
186   bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C,
187                         unsigned Type) const;
188 
189   bool checkRelocation(SMLoc Loc, const MCSectionELF *From,
190                        const MCSectionELF *To);
191 
getELFHeaderEFlags()192   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
setELFHeaderEFlags(unsigned Flags)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).
markGnuAbi()196   void markGnuAbi() { SeenGnuAbi = true; }
seenGnuAbi()197   bool seenGnuAbi() const { return SeenGnuAbi; }
198 
199   // Override the default e_ident[EI_ABIVERSION] in the ELF header.
setOverrideABIVersion(uint8_t V)200   void setOverrideABIVersion(uint8_t V) { OverrideABIVersion = V; }
201 };
202 } // end namespace llvm
203 
204 #endif // LLVM_MC_MCELFOBJECTWRITER_H
205