MCELFObjectWriter.h revision 00ca888cccd130dd3ebcfc02cf2b9187b54d116e
1//===-- 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/MC/MCObjectWriter.h"
14#include "llvm/Support/DataTypes.h"
15#include "llvm/Support/ELF.h"
16#include <vector>
17
18namespace llvm {
19/// @name Relocation Data
20/// @{
21
22struct ELFRelocationEntry {
23  // Make these big enough for both 32-bit and 64-bit
24  uint64_t r_offset;
25  int Index;
26  unsigned Type;
27  const MCSymbol *Symbol;
28  uint64_t r_addend;
29  const MCFixup *Fixup;
30
31  ELFRelocationEntry()
32    : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0), Fixup(0) {}
33
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) {}
38
39  // Support lexicographic sorting.
40  bool operator<(const ELFRelocationEntry &RE) const {
41    return RE.r_offset < r_offset;
42  }
43};
44
45class MCELFObjectTargetWriter {
46  const uint8_t OSABI;
47  const uint16_t EMachine;
48  const unsigned HasRelocationAddend : 1;
49  const unsigned Is64Bit : 1;
50
51protected:
52
53  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
54                          uint16_t EMachine_,  bool HasRelocationAddend_);
55
56public:
57  static uint8_t getOSABI(Triple::OSType OSType) {
58    switch (OSType) {
59      case Triple::FreeBSD:
60        return ELF::ELFOSABI_FREEBSD;
61      case Triple::Linux:
62        return ELF::ELFOSABI_LINUX;
63      default:
64        return ELF::ELFOSABI_NONE;
65    }
66  }
67
68  virtual ~MCELFObjectTargetWriter() {}
69
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,
76                                         const MCFragment &F,
77                                         const MCFixup &Fixup,
78                                         bool IsPCRel) const;
79  virtual void adjustFixupOffset(const MCFixup &Fixup,
80                                 uint64_t &RelocOffset);
81
82  virtual void sortRelocs(const MCAssembler &Asm,
83                          std::vector<ELFRelocationEntry> &Relocs);
84
85  /// @name Accessors
86  /// @{
87  uint8_t getOSABI() { return OSABI; }
88  uint16_t getEMachine() { return EMachine; }
89  bool hasRelocationAddend() { return HasRelocationAddend; }
90  bool is64Bit() const { return Is64Bit; }
91  /// @}
92};
93
94/// \brief Construct a new ELF writer instance.
95///
96/// \param MOTW - The target specific ELF writer subclass.
97/// \param OS - The stream to write to.
98/// \returns The constructed object writer.
99MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
100                                      raw_ostream &OS, bool IsLittleEndian);
101} // End llvm namespace
102
103#endif
104