Coverage Report

Created: 2018-11-12 17:33

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/SymbolicFile.h
Line
Count
Source
1
//===- SymbolicFile.h - Interface that only provides symbols ----*- 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
// This file declares the SymbolicFile interface.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_OBJECT_SYMBOLICFILE_H
15
#define LLVM_OBJECT_SYMBOLICFILE_H
16
17
#include "llvm/ADT/StringRef.h"
18
#include "llvm/ADT/iterator_range.h"
19
#include "llvm/BinaryFormat/Magic.h"
20
#include "llvm/Object/Binary.h"
21
#include "llvm/Support/Error.h"
22
#include "llvm/Support/FileSystem.h"
23
#include "llvm/Support/Format.h"
24
#include "llvm/Support/MemoryBuffer.h"
25
#include <cinttypes>
26
#include <cstdint>
27
#include <cstring>
28
#include <iterator>
29
#include <memory>
30
#include <system_error>
31
32
namespace llvm {
33
namespace object {
34
35
union DataRefImpl {
36
  // This entire union should probably be a
37
  // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
38
  struct {
39
    uint32_t a, b;
40
  } d;
41
  uintptr_t p;
42
43
323k
  DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
44
};
45
46
template <typename OStream>
47
OStream& operator<<(OStream &OS, const DataRefImpl &D) {
48
  OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
49
     << ", " << format("0x%08x", D.d.b) << "))";
50
  return OS;
51
}
52
53
338k
inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
54
338k
  // Check bitwise identical. This is the only legal way to compare a union w/o
55
338k
  // knowing which member is in use.
56
338k
  return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
57
338k
}
58
59
inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
60
  return !operator==(a, b);
61
}
62
63
inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
64
  // Check bitwise identical. This is the only legal way to compare a union w/o
65
  // knowing which member is in use.
66
  return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
67
}
68
69
template <class content_type>
70
class content_iterator
71
    : public std::iterator<std::forward_iterator_tag, content_type> {
72
  content_type Current;
73
74
public:
75
209k
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef>::content_iterator(llvm::irsymtab::Reader::SymbolRef)
Line
Count
Source
75
1.27k
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::RelocationRef>::content_iterator(llvm::object::RelocationRef)
Line
Count
Source
75
17.9k
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::BasicSymbolRef>::content_iterator(llvm::object::BasicSymbolRef)
Line
Count
Source
75
42.9k
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::ImportedSymbolRef>::content_iterator(llvm::object::ImportedSymbolRef)
Line
Count
Source
75
66
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::SectionRef>::content_iterator(llvm::object::SectionRef)
Line
Count
Source
75
146k
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::ImportDirectoryEntryRef>::content_iterator(llvm::object::ImportDirectoryEntryRef)
Line
Count
Source
75
104
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::DelayImportDirectoryEntryRef>::content_iterator(llvm::object::DelayImportDirectoryEntryRef)
Line
Count
Source
75
46
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::ExportDirectoryEntryRef>::content_iterator(llvm::object::ExportDirectoryEntryRef)
Line
Count
Source
75
234
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::BaseRelocRef>::content_iterator(llvm::object::BaseRelocRef)
Line
Count
Source
75
28
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::DiceRef>::content_iterator(llvm::object::DiceRef)
Line
Count
Source
75
122
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::ExportEntry>::content_iterator(llvm::object::ExportEntry)
Line
Count
Source
75
50
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::MachORebaseEntry>::content_iterator(llvm::object::MachORebaseEntry)
Line
Count
Source
75
24
  content_iterator(content_type symb) : Current(std::move(symb)) {}
llvm::object::content_iterator<llvm::object::MachOBindEntry>::content_iterator(llvm::object::MachOBindEntry)
Line
Count
Source
75
96
  content_iterator(content_type symb) : Current(std::move(symb)) {}
76
77
60.9k
  const content_type *operator->() const { return &Current; }
llvm::object::content_iterator<llvm::object::BasicSymbolRef>::operator->() const
Line
Count
Source
77
22.9k
  const content_type *operator->() const { return &Current; }
llvm::object::content_iterator<llvm::object::SectionRef>::operator->() const
Line
Count
Source
77
30.9k
  const content_type *operator->() const { return &Current; }
llvm::object::content_iterator<llvm::object::RelocationRef>::operator->() const
Line
Count
Source
77
7.15k
  const content_type *operator->() const { return &Current; }
78
79
275k
  const content_type &operator*() const { return Current; }
llvm::object::content_iterator<llvm::object::SectionRef>::operator*() const
Line
Count
Source
79
191k
  const content_type &operator*() const { return Current; }
llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef>::operator*() const
Line
Count
Source
79
1.33k
  const content_type &operator*() const { return Current; }
llvm::object::content_iterator<llvm::object::RelocationRef>::operator*() const
Line
Count
Source
79
14.1k
  const content_type &operator*() const { return Current; }
llvm::object::content_iterator<llvm::object::BasicSymbolRef>::operator*() const
Line
Count
Source
79
69.3k
  const content_type &operator*() const { return Current; }
80
81
338k
  bool operator==(const content_iterator &other) const {
82
338k
    return Current == other.Current;
83
338k
  }
llvm::object::content_iterator<llvm::object::SectionRef>::operator==(llvm::object::content_iterator<llvm::object::SectionRef> const&) const
Line
Count
Source
81
250k
  bool operator==(const content_iterator &other) const {
82
250k
    return Current == other.Current;
83
250k
  }
llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef>::operator==(llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef> const&) const
Line
Count
Source
81
1.96k
  bool operator==(const content_iterator &other) const {
82
1.96k
    return Current == other.Current;
83
1.96k
  }
llvm::object::content_iterator<llvm::object::RelocationRef>::operator==(llvm::object::content_iterator<llvm::object::RelocationRef> const&) const
Line
Count
Source
81
22.8k
  bool operator==(const content_iterator &other) const {
82
22.8k
    return Current == other.Current;
83
22.8k
  }
llvm::object::content_iterator<llvm::object::BasicSymbolRef>::operator==(llvm::object::content_iterator<llvm::object::BasicSymbolRef> const&) const
Line
Count
Source
81
63.4k
  bool operator==(const content_iterator &other) const {
82
63.4k
    return Current == other.Current;
83
63.4k
  }
84
85
307k
  bool operator!=(const content_iterator &other) const {
86
307k
    return !(*this == other);
87
307k
  }
llvm::object::content_iterator<llvm::object::SectionRef>::operator!=(llvm::object::content_iterator<llvm::object::SectionRef> const&) const
Line
Count
Source
85
224k
  bool operator!=(const content_iterator &other) const {
86
224k
    return !(*this == other);
87
224k
  }
llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef>::operator!=(llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef> const&) const
Line
Count
Source
85
1.96k
  bool operator!=(const content_iterator &other) const {
86
1.96k
    return !(*this == other);
87
1.96k
  }
llvm::object::content_iterator<llvm::object::RelocationRef>::operator!=(llvm::object::content_iterator<llvm::object::RelocationRef> const&) const
Line
Count
Source
85
20.3k
  bool operator!=(const content_iterator &other) const {
86
20.3k
    return !(*this == other);
87
20.3k
  }
llvm::object::content_iterator<llvm::object::BasicSymbolRef>::operator!=(llvm::object::content_iterator<llvm::object::BasicSymbolRef> const&) const
Line
Count
Source
85
61.2k
  bool operator!=(const content_iterator &other) const {
86
61.2k
    return !(*this == other);
87
61.2k
  }
88
89
219k
  content_iterator &operator++() { // preincrement
90
219k
    Current.moveNext();
91
219k
    return *this;
92
219k
  }
llvm::object::content_iterator<llvm::object::SectionRef>::operator++()
Line
Count
Source
89
155k
  content_iterator &operator++() { // preincrement
90
155k
    Current.moveNext();
91
155k
    return *this;
92
155k
  }
llvm::object::content_iterator<llvm::irsymtab::Reader::SymbolRef>::operator++()
Line
Count
Source
89
1.33k
  content_iterator &operator++() { // preincrement
90
1.33k
    Current.moveNext();
91
1.33k
    return *this;
92
1.33k
  }
llvm::object::content_iterator<llvm::object::RelocationRef>::operator++()
Line
Count
Source
89
14.7k
  content_iterator &operator++() { // preincrement
90
14.7k
    Current.moveNext();
91
14.7k
    return *this;
92
14.7k
  }
llvm::object::content_iterator<llvm::object::BasicSymbolRef>::operator++()
Line
Count
Source
89
48.4k
  content_iterator &operator++() { // preincrement
90
48.4k
    Current.moveNext();
91
48.4k
    return *this;
92
48.4k
  }
93
};
94
95
class SymbolicFile;
96
97
/// This is a value type class that represents a single symbol in the list of
98
/// symbols in the object file.
99
class BasicSymbolRef {
100
  DataRefImpl SymbolPimpl;
101
  const SymbolicFile *OwningObject = nullptr;
102
103
public:
104
  enum Flags : unsigned {
105
    SF_None = 0,
106
    SF_Undefined = 1U << 0,      // Symbol is defined in another object file
107
    SF_Global = 1U << 1,         // Global symbol
108
    SF_Weak = 1U << 2,           // Weak symbol
109
    SF_Absolute = 1U << 3,       // Absolute symbol
110
    SF_Common = 1U << 4,         // Symbol has common linkage
111
    SF_Indirect = 1U << 5,       // Symbol is an alias to another symbol
112
    SF_Exported = 1U << 6,       // Symbol is visible to other DSOs
113
    SF_FormatSpecific = 1U << 7, // Specific to the object file format
114
                                 // (e.g. section symbols)
115
    SF_Thumb = 1U << 8,          // Thumb symbol in a 32-bit ARM binary
116
    SF_Hidden = 1U << 9,         // Symbol has hidden visibility
117
    SF_Const = 1U << 10,         // Symbol value is constant
118
    SF_Executable = 1U << 11,    // Symbol points to an executable section
119
                                 // (IR only)
120
  };
121
122
  BasicSymbolRef() = default;
123
  BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
124
125
  bool operator==(const BasicSymbolRef &Other) const;
126
  bool operator<(const BasicSymbolRef &Other) const;
127
128
  void moveNext();
129
130
  std::error_code printName(raw_ostream &OS) const;
131
132
  /// Get symbol flags (bitwise OR of SymbolRef::Flags)
133
  uint32_t getFlags() const;
134
135
  DataRefImpl getRawDataRefImpl() const;
136
  const SymbolicFile *getObject() const;
137
};
138
139
using basic_symbol_iterator = content_iterator<BasicSymbolRef>;
140
141
class SymbolicFile : public Binary {
142
public:
143
  SymbolicFile(unsigned int Type, MemoryBufferRef Source);
144
  ~SymbolicFile() override;
145
146
  // virtual interface.
147
  virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
148
149
  virtual std::error_code printSymbolName(raw_ostream &OS,
150
                                          DataRefImpl Symb) const = 0;
151
152
  virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
153
154
  virtual basic_symbol_iterator symbol_begin() const = 0;
155
156
  virtual basic_symbol_iterator symbol_end() const = 0;
157
158
  // convenience wrappers.
159
  using basic_symbol_iterator_range = iterator_range<basic_symbol_iterator>;
160
  basic_symbol_iterator_range symbols() const {
161
    return basic_symbol_iterator_range(symbol_begin(), symbol_end());
162
  }
163
164
  // construction aux.
165
  static Expected<std::unique_ptr<SymbolicFile>>
166
  createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type,
167
                     LLVMContext *Context);
168
169
  static Expected<std::unique_ptr<SymbolicFile>>
170
  createSymbolicFile(MemoryBufferRef Object) {
171
    return createSymbolicFile(Object, llvm::file_magic::unknown, nullptr);
172
  }
173
  static Expected<OwningBinary<SymbolicFile>>
174
  createSymbolicFile(StringRef ObjectPath);
175
176
  static bool classof(const Binary *v) {
177
    return v->isSymbolic();
178
  }
179
};
180
181
inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
182
                                      const SymbolicFile *Owner)
183
41.4k
    : SymbolPimpl(SymbolP), OwningObject(Owner) {}
184
185
63.4k
inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
186
63.4k
  return SymbolPimpl == Other.SymbolPimpl;
187
63.4k
}
188
189
inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
190
  return SymbolPimpl < Other.SymbolPimpl;
191
}
192
193
48.4k
inline void BasicSymbolRef::moveNext() {
194
48.4k
  return OwningObject->moveSymbolNext(SymbolPimpl);
195
48.4k
}
196
197
inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
198
  return OwningObject->printSymbolName(OS, SymbolPimpl);
199
}
200
201
inline uint32_t BasicSymbolRef::getFlags() const {
202
  return OwningObject->getSymbolFlags(SymbolPimpl);
203
}
204
205
138k
inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
206
138k
  return SymbolPimpl;
207
138k
}
208
209
115k
inline const SymbolicFile *BasicSymbolRef::getObject() const {
210
115k
  return OwningObject;
211
115k
}
212
213
} // end namespace object
214
} // end namespace llvm
215
216
#endif // LLVM_OBJECT_SYMBOLICFILE_H