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