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