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/ADT/Triple.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/ELF.h"
26 /// @name Relocation Data
29 struct ELFRelocationEntry {
30 // Make these big enough for both 32-bit and 64-bit
34 const MCSymbol *Symbol;
39 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0), Fixup(0) {}
41 ELFRelocationEntry(uint64_t RelocOffset, int Idx, unsigned RelType,
42 const MCSymbol *Sym, uint64_t Addend, const MCFixup &Fixup)
43 : r_offset(RelocOffset), Index(Idx), Type(RelType), Symbol(Sym),
44 r_addend(Addend), Fixup(&Fixup) {}
46 // Support lexicographic sorting.
47 bool operator<(const ELFRelocationEntry &RE) const {
48 return RE.r_offset < r_offset;
52 class MCELFObjectTargetWriter {
54 const uint16_t EMachine;
55 const unsigned HasRelocationAddend : 1;
56 const unsigned Is64Bit : 1;
60 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
61 uint16_t EMachine_, bool HasRelocationAddend_);
64 static uint8_t getOSABI(Triple::OSType OSType) {
67 return ELF::ELFOSABI_FREEBSD;
69 return ELF::ELFOSABI_LINUX;
71 return ELF::ELFOSABI_NONE;
75 virtual ~MCELFObjectTargetWriter() {}
77 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
78 bool IsPCRel, bool IsRelocWithSymbol,
79 int64_t Addend) const = 0;
80 virtual unsigned getEFlags() const;
81 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
82 const MCValue &Target,
86 virtual void adjustFixupOffset(const MCFixup &Fixup,
87 uint64_t &RelocOffset);
89 virtual void sortRelocs(const MCAssembler &Asm,
90 std::vector<ELFRelocationEntry> &Relocs);
94 uint8_t getOSABI() { return OSABI; }
95 uint16_t getEMachine() { return EMachine; }
96 bool hasRelocationAddend() { return HasRelocationAddend; }
97 bool is64Bit() const { return Is64Bit; }
101 /// \brief Construct a new ELF writer instance.
103 /// \param MOTW - The target specific ELF writer subclass.
104 /// \param OS - The stream to write to.
105 /// \returns The constructed object writer.
106 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
107 raw_ostream &OS, bool IsLittleEndian);
108 } // End llvm namespace