Coverage Report

Created: 2018-07-19 03:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/MC/MCELFObjectWriter.h
Line
Count
Source (jump to first uncovered line)
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/ADT/Triple.h"
14
#include "llvm/BinaryFormat/ELF.h"
15
#include "llvm/MC/MCObjectWriter.h"
16
#include "llvm/Support/Casting.h"
17
#include "llvm/Support/raw_ostream.h"
18
#include <cstdint>
19
#include <vector>
20
21
namespace llvm {
22
23
class MCAssembler;
24
class MCContext;
25
class MCFixup;
26
class MCObjectWriter;
27
class MCSymbol;
28
class MCSymbolELF;
29
class MCValue;
30
31
struct 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
22.1k
        OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {}
44
45
0
  void print(raw_ostream &Out) const {
46
0
    Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
47
0
        << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
48
0
        << ", OriginalAddend=" << OriginalAddend;
49
0
  }
50
51
0
  void dump() const { print(errs()); }
52
};
53
54
class MCELFObjectTargetWriter : public MCObjectTargetWriter {
55
  const uint8_t OSABI;
56
  const uint16_t EMachine;
57
  const unsigned HasRelocationAddend : 1;
58
  const unsigned Is64Bit : 1;
59
60
protected:
61
  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62
                          bool HasRelocationAddend);
63
64
public:
65
22.2k
  virtual ~MCELFObjectTargetWriter() = default;
66
67
22.3k
  virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
68
0
  static bool classof(const MCObjectTargetWriter *W) {
69
0
    return W->getFormat() == Triple::ELF;
70
0
  }
71
72
19.8k
  static uint8_t getOSABI(Triple::OSType OSType) {
73
19.8k
    switch (OSType) {
74
19.8k
      case Triple::CloudABI:
75
4
        return ELF::ELFOSABI_CLOUDABI;
76
19.8k
      case Triple::PS4:
77
177
      case Triple::FreeBSD:
78
177
        return ELF::ELFOSABI_FREEBSD;
79
19.6k
      default:
80
19.6k
        return ELF::ELFOSABI_NONE;
81
19.8k
    }
82
19.8k
  }
83
84
  virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
85
                                const MCFixup &Fixup, bool IsPCRel) const = 0;
86
87
  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
88
                                       unsigned Type) const;
89
90
  virtual void sortRelocs(const MCAssembler &Asm,
91
                          std::vector<ELFRelocationEntry> &Relocs);
92
93
  /// \name Accessors
94
  /// @{
95
4.25k
  uint8_t getOSABI() const { return OSABI; }
96
31.9k
  uint16_t getEMachine() const { return EMachine; }
97
56.2k
  bool hasRelocationAddend() const { return HasRelocationAddend; }
98
2.61M
  bool is64Bit() const { return Is64Bit; }
99
  /// @}
100
101
  // Instead of changing everyone's API we pack the N64 Type fields
102
  // into the existing 32 bit data unsigned.
103
10.3k
#define R_TYPE_SHIFT 0
104
71
#define R_TYPE_MASK 0xffffff00
105
11.4k
#define R_TYPE2_SHIFT 8
106
71
#define R_TYPE2_MASK 0xffff00ff
107
11.4k
#define R_TYPE3_SHIFT 16
108
71
#define R_TYPE3_MASK 0xff00ffff
109
10.3k
#define R_SSYM_SHIFT 24
110
#define R_SSYM_MASK 0x00ffffff
111
112
  // N64 relocation type accessors
113
10.3k
  uint8_t getRType(uint32_t Type) const {
114
10.3k
    return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
115
10.3k
  }
116
11.3k
  uint8_t getRType2(uint32_t Type) const {
117
11.3k
    return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
118
11.3k
  }
119
11.3k
  uint8_t getRType3(uint32_t Type) const {
120
11.3k
    return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
121
11.3k
  }
122
10.3k
  uint8_t getRSsym(uint32_t Type) const {
123
10.3k
    return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
124
10.3k
  }
125
126
  // N64 relocation type setting
127
71
  unsigned setRType(unsigned Value, unsigned Type) const {
128
71
    return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
129
71
  }
130
71
  unsigned setRType2(unsigned Value, unsigned Type) const {
131
71
    return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
132
71
  }
133
71
  unsigned setRType3(unsigned Value, unsigned Type) const {
134
71
    return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
135
71
  }
136
0
  unsigned setRSsym(unsigned Value, unsigned Type) const {
137
0
    return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
138
0
  }
139
};
140
141
/// Construct a new ELF writer instance.
142
///
143
/// \param MOTW - The target specific ELF writer subclass.
144
/// \param OS - The stream to write to.
145
/// \returns The constructed object writer.
146
std::unique_ptr<MCObjectWriter>
147
createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
148
                      raw_pwrite_stream &OS, bool IsLittleEndian);
149
150
std::unique_ptr<MCObjectWriter>
151
createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
152
                         raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
153
                         bool IsLittleEndian);
154
155
} // end namespace llvm
156
157
#endif // LLVM_MC_MCELFOBJECTWRITER_H