blob: f226d6a45a5ab637500c2f1a8d4e8292ea718148 [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"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010015#include "llvm/MC/MCObjectWriter.h"
16#include "llvm/MC/MCSectionELF.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#include "llvm/Support/Casting.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cstdint>
20#include <vector>
21
22namespace llvm {
23
24class MCAssembler;
25class MCContext;
26class MCFixup;
27class MCObjectWriter;
28class MCSymbol;
29class MCSymbolELF;
30class MCValue;
31
32struct 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.
39
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) {}
45
46 void print(raw_ostream &Out) const {
47 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
48 << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
49 << ", OriginalAddend=" << OriginalAddend;
50 }
51
52 void dump() const { print(errs()); }
53};
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055class MCELFObjectTargetWriter : public MCObjectTargetWriter {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 const uint8_t OSABI;
57 const uint16_t EMachine;
58 const unsigned HasRelocationAddend : 1;
59 const unsigned Is64Bit : 1;
60
61protected:
62 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
63 bool HasRelocationAddend);
64
65public:
66 virtual ~MCELFObjectTargetWriter() = default;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
69 static bool classof(const MCObjectTargetWriter *W) {
70 return W->getFormat() == Triple::ELF;
71 }
72
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 static uint8_t getOSABI(Triple::OSType OSType) {
74 switch (OSType) {
75 case Triple::CloudABI:
76 return ELF::ELFOSABI_CLOUDABI;
Andrew Scull0372a572018-11-16 15:47:06 +000077 case Triple::HermitCore:
78 return ELF::ELFOSABI_STANDALONE;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 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 Scullcdfcccc2018-10-05 20:58:37 +010096 virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);
97
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098 /// \name Accessors
99 /// @{
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; }
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 unsigned setRType(unsigned Value, unsigned Type) const {
133 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
134 }
135 unsigned setRType2(unsigned Value, unsigned Type) const {
136 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
137 }
138 unsigned setRType3(unsigned Value, unsigned Type) const {
139 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
140 }
141 unsigned setRSsym(unsigned Value, unsigned Type) const {
142 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
143 }
144};
145
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146/// Construct a new ELF writer instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100147///
148/// \param MOTW - The target specific ELF writer subclass.
149/// \param OS - The stream to write to.
150/// \returns The constructed object writer.
151std::unique_ptr<MCObjectWriter>
152createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
153 raw_pwrite_stream &OS, bool IsLittleEndian);
154
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100155std::unique_ptr<MCObjectWriter>
156createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
157 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
158 bool IsLittleEndian);
159
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100160} // end namespace llvm
161
162#endif // LLVM_MC_MCELFOBJECTWRITER_H