LLVM  9.0.0svn
SymbolicFile.h
Go to the documentation of this file.
1 //===- SymbolicFile.h - Interface that only provides symbols ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the SymbolicFile interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_SYMBOLICFILE_H
14 #define LLVM_OBJECT_SYMBOLICFILE_H
15 
16 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/Error.h"
22 #include "llvm/Support/Format.h"
24 #include <cinttypes>
25 #include <cstdint>
26 #include <cstring>
27 #include <iterator>
28 #include <memory>
29 #include <system_error>
30 
31 namespace llvm {
32 namespace object {
33 
34 union DataRefImpl {
35  // This entire union should probably be a
36  // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
37  struct {
39  } d;
40  uintptr_t p;
41 
42  DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
43 };
44 
45 template <typename OStream>
46 OStream& operator<<(OStream &OS, const DataRefImpl &D) {
47  OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
48  << ", " << format("0x%08x", D.d.b) << "))";
49  return OS;
50 }
51 
52 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
53  // Check bitwise identical. This is the only legal way to compare a union w/o
54  // knowing which member is in use.
55  return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
56 }
57 
58 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
59  return !operator==(a, b);
60 }
61 
62 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
63  // Check bitwise identical. This is the only legal way to compare a union w/o
64  // knowing which member is in use.
65  return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
66 }
67 
68 template <class content_type>
70  : public std::iterator<std::forward_iterator_tag, content_type> {
71  content_type Current;
72 
73 public:
74  content_iterator(content_type symb) : Current(std::move(symb)) {}
75 
76  const content_type *operator->() const { return &Current; }
77 
78  const content_type &operator*() const { return Current; }
79 
80  bool operator==(const content_iterator &other) const {
81  return Current == other.Current;
82  }
83 
84  bool operator!=(const content_iterator &other) const {
85  return !(*this == other);
86  }
87 
88  content_iterator &operator++() { // preincrement
89  Current.moveNext();
90  return *this;
91  }
92 };
93 
94 class SymbolicFile;
95 
96 /// This is a value type class that represents a single symbol in the list of
97 /// symbols in the object file.
99  DataRefImpl SymbolPimpl;
100  const SymbolicFile *OwningObject = nullptr;
101 
102 public:
103  enum Flags : unsigned {
104  SF_None = 0,
105  SF_Undefined = 1U << 0, // Symbol is defined in another object file
106  SF_Global = 1U << 1, // Global symbol
107  SF_Weak = 1U << 2, // Weak symbol
108  SF_Absolute = 1U << 3, // Absolute symbol
109  SF_Common = 1U << 4, // Symbol has common linkage
110  SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
111  SF_Exported = 1U << 6, // Symbol is visible to other DSOs
112  SF_FormatSpecific = 1U << 7, // Specific to the object file format
113  // (e.g. section symbols)
114  SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
115  SF_Hidden = 1U << 9, // Symbol has hidden visibility
116  SF_Const = 1U << 10, // Symbol value is constant
117  SF_Executable = 1U << 11, // Symbol points to an executable section
118  // (IR only)
119  };
120 
121  BasicSymbolRef() = default;
122  BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
123 
124  bool operator==(const BasicSymbolRef &Other) const;
125  bool operator<(const BasicSymbolRef &Other) const;
126 
127  void moveNext();
128 
129  std::error_code printName(raw_ostream &OS) const;
130 
131  /// Get symbol flags (bitwise OR of SymbolRef::Flags)
132  uint32_t getFlags() const;
133 
134  DataRefImpl getRawDataRefImpl() const;
135  const SymbolicFile *getObject() const;
136 };
137 
139 
140 class SymbolicFile : public Binary {
141 public:
142  SymbolicFile(unsigned int Type, MemoryBufferRef Source);
143  ~SymbolicFile() override;
144 
145  // virtual interface.
146  virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
147 
148  virtual std::error_code printSymbolName(raw_ostream &OS,
149  DataRefImpl Symb) const = 0;
150 
151  virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
152 
153  virtual basic_symbol_iterator symbol_begin() const = 0;
154 
155  virtual basic_symbol_iterator symbol_end() const = 0;
156 
157  // convenience wrappers.
160  return basic_symbol_iterator_range(symbol_begin(), symbol_end());
161  }
162 
163  // construction aux.
165  createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type,
167 
170  return createSymbolicFile(Object, llvm::file_magic::unknown, nullptr);
171  }
173  createSymbolicFile(StringRef ObjectPath);
174 
175  static bool classof(const Binary *v) {
176  return v->isSymbolic();
177  }
178 };
179 
181  const SymbolicFile *Owner)
182  : SymbolPimpl(SymbolP), OwningObject(Owner) {}
183 
184 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
185  return SymbolPimpl == Other.SymbolPimpl;
186 }
187 
188 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
189  return SymbolPimpl < Other.SymbolPimpl;
190 }
191 
193  return OwningObject->moveSymbolNext(SymbolPimpl);
194 }
195 
196 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
197  return OwningObject->printSymbolName(OS, SymbolPimpl);
198 }
199 
201  return OwningObject->getSymbolFlags(SymbolPimpl);
202 }
203 
205  return SymbolPimpl;
206 }
207 
209  return OwningObject;
210 }
211 
212 } // end namespace object
213 } // end namespace llvm
214 
215 #endif // LLVM_OBJECT_SYMBOLICFILE_H
bool operator<(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:188
std::error_code printName(raw_ostream &OS) const
Definition: SymbolicFile.h:196
const content_type & operator*() const
Definition: SymbolicFile.h:78
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
bool operator==(const content_iterator &other) const
Definition: SymbolicFile.h:80
static void printName(raw_ostream &OS, StringRef Name)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:123
bool operator<(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:62
bool operator!=(const content_iterator &other) const
Definition: SymbolicFile.h:84
bool isSymbolic() const
Definition: Binary.h:98
Definition: BitVector.h:937
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:204
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
bool operator!=(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:58
struct llvm::object::DataRefImpl::@282 d
const content_type * operator->() const
Definition: SymbolicFile.h:76
content_iterator(content_type symb)
Definition: SymbolicFile.h:74
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
bool operator==(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:184
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:68
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:208
bool operator==(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:52
content_iterator & operator++()
Definition: SymbolicFile.h:88
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
A range adaptor for a pair of iterators.
uint32_t getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
Definition: SymbolicFile.h:200
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:823
virtual std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const =0
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:98
virtual void moveSymbolNext(DataRefImpl &Symb) const =0
basic_symbol_iterator_range symbols() const
Definition: SymbolicFile.h:159
OStream & operator<<(OStream &OS, const DataRefImpl &D)
Definition: SymbolicFile.h:46
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static bool classof(const Binary *v)
Definition: SymbolicFile.h:175
Unrecognized file.
Definition: Magic.h:22
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object)
Definition: SymbolicFile.h:169
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20