Don't own the buffer in object::Binary.
[oota-llvm.git] / 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
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> class content_iterator {
49   content_type Current;
50
51 public:
52   content_iterator(content_type symb) : Current(symb) {}
53
54   const content_type *operator->() const { return &Current; }
55
56   const content_type &operator*() const { return Current; }
57
58   bool operator==(const content_iterator &other) const {
59     return Current == other.Current;
60   }
61
62   bool operator!=(const content_iterator &other) const {
63     return !(*this == other);
64   }
65
66   content_iterator &operator++() { // preincrement
67     Current.moveNext();
68     return *this;
69   }
70 };
71
72 class SymbolicFile;
73
74 /// This is a value type class that represents a single symbol in the list of
75 /// symbols in the object file.
76 class BasicSymbolRef {
77   DataRefImpl SymbolPimpl;
78   const SymbolicFile *OwningObject;
79
80 public:
81   // FIXME: should we add a SF_Text?
82   enum Flags : unsigned {
83     SF_None = 0,
84     SF_Undefined = 1U << 0,      // Symbol is defined in another object file
85     SF_Global = 1U << 1,         // Global symbol
86     SF_Weak = 1U << 2,           // Weak symbol
87     SF_Absolute = 1U << 3,       // Absolute symbol
88     SF_Common = 1U << 4,         // Symbol has common linkage
89     SF_Indirect = 1U << 5,       // Symbol is an alias to another symbol
90     SF_FormatSpecific = 1U << 6, // Specific to the object file format
91                                  // (e.g. section symbols)
92     SF_Thumb = 1U << 7           // Thumb symbol in a 32-bit ARM binary
93   };
94
95   BasicSymbolRef() : OwningObject(nullptr) { }
96   BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
97
98   bool operator==(const BasicSymbolRef &Other) const;
99   bool operator<(const BasicSymbolRef &Other) const;
100
101   void moveNext();
102
103   std::error_code printName(raw_ostream &OS) const;
104
105   /// Get symbol flags (bitwise OR of SymbolRef::Flags)
106   uint32_t getFlags() const;
107
108   DataRefImpl getRawDataRefImpl() const;
109   const SymbolicFile *getObject() const;
110 };
111
112 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
113
114 const uint64_t UnknownAddressOrSize = ~0ULL;
115
116 class SymbolicFile : public Binary {
117 public:
118   virtual ~SymbolicFile();
119   SymbolicFile(unsigned int Type, MemoryBufferRef Source);
120
121   // virtual interface.
122   virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
123
124   virtual std::error_code printSymbolName(raw_ostream &OS,
125                                           DataRefImpl Symb) const = 0;
126
127   virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
128
129   virtual basic_symbol_iterator symbol_begin_impl() const = 0;
130
131   virtual basic_symbol_iterator symbol_end_impl() const = 0;
132
133   // convenience wrappers.
134   basic_symbol_iterator symbol_begin() const {
135     return symbol_begin_impl();
136   }
137   basic_symbol_iterator symbol_end() const {
138     return symbol_end_impl();
139   }
140   typedef iterator_range<basic_symbol_iterator> basic_symbol_iterator_range;
141   basic_symbol_iterator_range symbols() const {
142     return basic_symbol_iterator_range(symbol_begin(), symbol_end());
143   }
144
145   // construction aux.
146   static ErrorOr<std::unique_ptr<SymbolicFile>>
147   createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
148                      LLVMContext *Context);
149
150   static ErrorOr<std::unique_ptr<SymbolicFile>>
151   createSymbolicFile(MemoryBufferRef Object) {
152     return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
153   }
154   static ErrorOr<OwningBinary<SymbolicFile>>
155   createSymbolicFile(StringRef ObjectPath);
156
157   static inline bool classof(const Binary *v) {
158     return v->isSymbolic();
159   }
160 };
161
162 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
163                                       const SymbolicFile *Owner)
164     : SymbolPimpl(SymbolP), OwningObject(Owner) {}
165
166 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
167   return SymbolPimpl == Other.SymbolPimpl;
168 }
169
170 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
171   return SymbolPimpl < Other.SymbolPimpl;
172 }
173
174 inline void BasicSymbolRef::moveNext() {
175   return OwningObject->moveSymbolNext(SymbolPimpl);
176 }
177
178 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
179   return OwningObject->printSymbolName(OS, SymbolPimpl);
180 }
181
182 inline uint32_t BasicSymbolRef::getFlags() const {
183   return OwningObject->getSymbolFlags(SymbolPimpl);
184 }
185
186 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
187   return SymbolPimpl;
188 }
189
190 inline const SymbolicFile *BasicSymbolRef::getObject() const {
191   return OwningObject;
192 }
193
194 }
195 }
196
197 #endif