]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Object/SymbolicFile.h
Upgrade to OpenSSH 7.4p1.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Object / SymbolicFile.h
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 {
28     uint32_t a, b;
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>
57 class content_iterator
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.
86 class BasicSymbolRef {
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
122   DataRefImpl getRawDataRefImpl() const;
123   const SymbolicFile *getObject() const;
124 };
125
126 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
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.
146   typedef iterator_range<basic_symbol_iterator> basic_symbol_iterator_range;
147   basic_symbol_iterator_range symbols() const {
148     return basic_symbol_iterator_range(symbol_begin(), symbol_end());
149   }
150
151   // construction aux.
152   static Expected<std::unique_ptr<SymbolicFile>>
153   createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
154                      LLVMContext *Context);
155
156   static Expected<std::unique_ptr<SymbolicFile>>
157   createSymbolicFile(MemoryBufferRef Object) {
158     return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
159   }
160   static Expected<OwningBinary<SymbolicFile>>
161   createSymbolicFile(StringRef ObjectPath);
162
163   static inline bool classof(const Binary *v) {
164     return v->isSymbolic();
165   }
166 };
167
168 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
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
180 inline void BasicSymbolRef::moveNext() {
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
188 inline uint32_t BasicSymbolRef::getFlags() const {
189   return OwningObject->getSymbolFlags(SymbolPimpl);
190 }
191
192 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
193   return SymbolPimpl;
194 }
195
196 inline const SymbolicFile *BasicSymbolRef::getObject() const {
197   return OwningObject;
198 }
199
200 }
201 }
202
203 #endif