1 //===-- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCELFOBJECTWRITER_H
11 #define LLVM_MC_MCELFOBJECTWRITER_H
13 #include "llvm/MC/MCObjectWriter.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/ELF.h"
19 /// @name Relocation Data
22 struct ELFRelocationEntry {
23 // Make these big enough for both 32-bit and 64-bit
27 const MCSymbol *Symbol;
32 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0), Fixup(0) {}
34 ELFRelocationEntry(uint64_t RelocOffset, int Idx, unsigned RelType,
35 const MCSymbol *Sym, uint64_t Addend, const MCFixup &Fixup)
36 : r_offset(RelocOffset), Index(Idx), Type(RelType), Symbol(Sym),
37 r_addend(Addend), Fixup(&Fixup) {}
39 // Support lexicographic sorting.
40 bool operator<(const ELFRelocationEntry &RE) const {
41 return RE.r_offset < r_offset;
45 class MCELFObjectTargetWriter {
47 const uint16_t EMachine;
48 const unsigned HasRelocationAddend : 1;
49 const unsigned Is64Bit : 1;
53 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
54 uint16_t EMachine_, bool HasRelocationAddend_);
57 static uint8_t getOSABI(Triple::OSType OSType) {
60 return ELF::ELFOSABI_FREEBSD;
62 return ELF::ELFOSABI_LINUX;
64 return ELF::ELFOSABI_NONE;
68 virtual ~MCELFObjectTargetWriter() {}
70 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
71 bool IsPCRel, bool IsRelocWithSymbol,
72 int64_t Addend) const = 0;
73 virtual unsigned getEFlags() const;
74 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
75 const MCValue &Target,
79 virtual void adjustFixupOffset(const MCFixup &Fixup,
80 uint64_t &RelocOffset);
82 virtual void sortRelocs(const MCAssembler &Asm,
83 std::vector<ELFRelocationEntry> &Relocs);
87 uint8_t getOSABI() { return OSABI; }
88 uint16_t getEMachine() { return EMachine; }
89 bool hasRelocationAddend() { return HasRelocationAddend; }
90 bool is64Bit() const { return Is64Bit; }
94 /// \brief Construct a new ELF writer instance.
96 /// \param MOTW - The target specific ELF writer subclass.
97 /// \param OS - The stream to write to.
98 /// \returns The constructed object writer.
99 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
100 raw_ostream &OS, bool IsLittleEndian);
101 } // End llvm namespace