LLVM  4.0.0
SymbolicFile.h
Go to the documentation of this file.
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/Object/Binary.h"
18 #include "llvm/Support/Format.h"
19 #include <utility>
20 
21 namespace llvm {
22 namespace object {
23 
24 union DataRefImpl {
25  // This entire union should probably be a
26  // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
27  struct {
29  } d;
30  uintptr_t p;
31  DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
32 };
33 
34 template <typename OStream>
35 OStream& operator<<(OStream &OS, const DataRefImpl &D) {
36  OS << "(" << format("0x%x8", D.p) << " (" << format("0x%x8", D.d.a) << ", " << format("0x%x8", D.d.b) << "))";
37  return OS;
38 }
39 
40 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
41  // Check bitwise identical. This is the only legal way to compare a union w/o
42  // knowing which member is in use.
43  return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
44 }
45 
46 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
47  return !operator==(a, b);
48 }
49 
50 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
51  // Check bitwise identical. This is the only legal way to compare a union w/o
52  // knowing which member is in use.
53  return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
54 }
55 
56 template <class content_type>
58  : public std::iterator<std::forward_iterator_tag, content_type> {
59  content_type Current;
60 
61 public:
62  content_iterator(content_type symb) : Current(std::move(symb)) {}
63 
64  const content_type *operator->() const { return &Current; }
65 
66  const content_type &operator*() const { return Current; }
67 
68  bool operator==(const content_iterator &other) const {
69  return Current == other.Current;
70  }
71 
72  bool operator!=(const content_iterator &other) const {
73  return !(*this == other);
74  }
75 
76  content_iterator &operator++() { // preincrement
77  Current.moveNext();
78  return *this;
79  }
80 };
81 
82 class SymbolicFile;
83 
84 /// This is a value type class that represents a single symbol in the list of
85 /// symbols in the object file.
87  DataRefImpl SymbolPimpl;
88  const SymbolicFile *OwningObject;
89 
90 public:
91  enum Flags : unsigned {
92  SF_None = 0,
93  SF_Undefined = 1U << 0, // Symbol is defined in another object file
94  SF_Global = 1U << 1, // Global symbol
95  SF_Weak = 1U << 2, // Weak symbol
96  SF_Absolute = 1U << 3, // Absolute symbol
97  SF_Common = 1U << 4, // Symbol has common linkage
98  SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
99  SF_Exported = 1U << 6, // Symbol is visible to other DSOs
100  SF_FormatSpecific = 1U << 7, // Specific to the object file format
101  // (e.g. section symbols)
102  SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
103  SF_Hidden = 1U << 9, // Symbol has hidden visibility
104  SF_Const = 1U << 10, // Symbol value is constant
105  SF_Executable = 1U << 11, // Symbol points to an executable section
106  // (IR only)
107  };
108 
109  BasicSymbolRef() : OwningObject(nullptr) { }
110  BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
111 
112  bool operator==(const BasicSymbolRef &Other) const;
113  bool operator<(const BasicSymbolRef &Other) const;
114 
115  void moveNext();
116 
117  std::error_code printName(raw_ostream &OS) const;
118 
119  /// Get symbol flags (bitwise OR of SymbolRef::Flags)
120  uint32_t getFlags() const;
121 
123  const SymbolicFile *getObject() const;
124 };
125 
127 
128 class SymbolicFile : public Binary {
129 public:
130  ~SymbolicFile() override;
131  SymbolicFile(unsigned int Type, MemoryBufferRef Source);
132 
133  // virtual interface.
134  virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
135 
136  virtual std::error_code printSymbolName(raw_ostream &OS,
137  DataRefImpl Symb) const = 0;
138 
139  virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
140 
141  virtual basic_symbol_iterator symbol_begin() const = 0;
142 
143  virtual basic_symbol_iterator symbol_end() const = 0;
144 
145  // convenience wrappers.
149  }
150 
151  // construction aux.
155 
158  return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
159  }
161  createSymbolicFile(StringRef ObjectPath);
162 
163  static inline bool classof(const Binary *v) {
164  return v->isSymbolic();
165  }
166 };
167 
169  const SymbolicFile *Owner)
170  : SymbolPimpl(SymbolP), OwningObject(Owner) {}
171 
172 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
173  return SymbolPimpl == Other.SymbolPimpl;
174 }
175 
176 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
177  return SymbolPimpl < Other.SymbolPimpl;
178 }
179 
181  return OwningObject->moveSymbolNext(SymbolPimpl);
182 }
183 
184 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
185  return OwningObject->printSymbolName(OS, SymbolPimpl);
186 }
187 
189  return OwningObject->getSymbolFlags(SymbolPimpl);
190 }
191 
193  return SymbolPimpl;
194 }
195 
197  return OwningObject;
198 }
199 
200 }
201 }
202 
203 #endif
bool operator==(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:172
LLVMContext & Context
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:192
iterator_range< basic_symbol_iterator > basic_symbol_iterator_range
Definition: SymbolicFile.h:146
bool operator<(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:50
struct llvm::object::DataRefImpl::@119 d
virtual basic_symbol_iterator symbol_begin() const =0
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
bool operator<(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:176
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
Tagged union holding either a T or a Error.
bool operator!=(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:46
bool operator!=(const content_iterator &other) const
Definition: SymbolicFile.h:72
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
content_iterator(content_type symb)
Definition: SymbolicFile.h:62
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
std::error_code printName(raw_ostream &OS) const
Definition: SymbolicFile.h:184
virtual basic_symbol_iterator symbol_end() const =0
uint32_t getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
Definition: SymbolicFile.h:188
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:126
bool operator==(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:40
content_iterator & operator++()
Definition: SymbolicFile.h:76
A range adaptor for a pair of iterators.
basic_symbol_iterator_range symbols() const
Definition: SymbolicFile.h:147
bool operator==(const content_iterator &other) const
Definition: SymbolicFile.h:68
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:240
virtual std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const =0
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:196
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:86
virtual void moveSymbolNext(DataRefImpl &Symb) const =0
const content_type & operator*() const
Definition: SymbolicFile.h:66
OStream & operator<<(OStream &OS, const DataRefImpl &D)
Definition: SymbolicFile.h:35
const content_type * operator->() const
Definition: SymbolicFile.h:64
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static bool classof(const Binary *v)
Definition: SymbolicFile.h:163
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context)
bool isSymbolic() const
Definition: Binary.h:96
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object)
Definition: SymbolicFile.h:157