blob: 9ff7011df1d9e7f14c2dfa77e620793b3b7a4ef2 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
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 Scullcdfcccc2018-10-05 20:58:37 +010014#include "llvm/MC/MCObjectWriter.h"
15#include "llvm/MC/MCSectionELF.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016#include "llvm/Support/Casting.h"
17#include "llvm/Support/raw_ostream.h"
18#include <cstdint>
19#include <vector>
20
21namespace llvm {
22
23class MCAssembler;
24class MCContext;
25class MCFixup;
26class MCObjectWriter;
27class MCSymbol;
28class MCSymbolELF;
29class MCValue;
30
31struct 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 Scullcdfcccc2018-10-05 20:58:37 +010054class MCELFObjectTargetWriter : public MCObjectTargetWriter {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055 const uint8_t OSABI;
56 const uint16_t EMachine;
57 const unsigned HasRelocationAddend : 1;
58 const unsigned Is64Bit : 1;
59
60protected:
61 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62 bool HasRelocationAddend);
63
64public:
65 virtual ~MCELFObjectTargetWriter() = default;
66
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067 virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
68 static bool classof(const MCObjectTargetWriter *W) {
69 return W->getFormat() == Triple::ELF;
70 }
71
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 static uint8_t getOSABI(Triple::OSType OSType) {
73 switch (OSType) {
74 case Triple::CloudABI:
75 return ELF::ELFOSABI_CLOUDABI;
Andrew Scull0372a572018-11-16 15:47:06 +000076 case Triple::HermitCore:
77 return ELF::ELFOSABI_STANDALONE;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078 case Triple::PS4:
79 case Triple::FreeBSD:
80 return ELF::ELFOSABI_FREEBSD;
81 default:
82 return ELF::ELFOSABI_NONE;
83 }
84 }
85
86 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
87 const MCFixup &Fixup, bool IsPCRel) const = 0;
88
89 virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
90 unsigned Type) const;
91
92 virtual void sortRelocs(const MCAssembler &Asm,
93 std::vector<ELFRelocationEntry> &Relocs);
94
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);
96
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 /// \name Accessors
98 /// @{
99 uint8_t getOSABI() const { return OSABI; }
100 uint16_t getEMachine() const { return EMachine; }
101 bool hasRelocationAddend() const { return HasRelocationAddend; }
102 bool is64Bit() const { return Is64Bit; }
103 /// @}
104
105 // Instead of changing everyone's API we pack the N64 Type fields
106 // into the existing 32 bit data unsigned.
107#define R_TYPE_SHIFT 0
108#define R_TYPE_MASK 0xffffff00
109#define R_TYPE2_SHIFT 8
110#define R_TYPE2_MASK 0xffff00ff
111#define R_TYPE3_SHIFT 16
112#define R_TYPE3_MASK 0xff00ffff
113#define R_SSYM_SHIFT 24
114#define R_SSYM_MASK 0x00ffffff
115
116 // N64 relocation type accessors
117 uint8_t getRType(uint32_t Type) const {
118 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
119 }
120 uint8_t getRType2(uint32_t Type) const {
121 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
122 }
123 uint8_t getRType3(uint32_t Type) const {
124 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
125 }
126 uint8_t getRSsym(uint32_t Type) const {
127 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
128 }
129
130 // N64 relocation type setting
131 unsigned setRType(unsigned Value, unsigned Type) const {
132 return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
133 }
134 unsigned setRType2(unsigned Value, unsigned Type) const {
135 return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
136 }
137 unsigned setRType3(unsigned Value, unsigned Type) const {
138 return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
139 }
140 unsigned setRSsym(unsigned Value, unsigned Type) const {
141 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
142 }
143};
144
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100145/// Construct a new ELF writer instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100146///
147/// \param MOTW - The target specific ELF writer subclass.
148/// \param OS - The stream to write to.
149/// \returns The constructed object writer.
150std::unique_ptr<MCObjectWriter>
151createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
152 raw_pwrite_stream &OS, bool IsLittleEndian);
153
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100154std::unique_ptr<MCObjectWriter>
155createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
156 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
157 bool IsLittleEndian);
158
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100159} // end namespace llvm
160
161#endif // LLVM_MC_MCELFOBJECTWRITER_H