Coverage Report

Created: 2018-11-12 17:33

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