[RuntimeDyld] Make LoadedObjectInfo::getLoadedSectionAddress take a SectionRef
[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 #include "llvm/Support/Format.h"
19
20 namespace llvm {
21 namespace object {
22
23 union DataRefImpl {
24   // This entire union should probably be a
25   // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
26   struct {
27     uint32_t a, b;
28   } d;
29   uintptr_t p;
30   DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
31 };
32
33 template <typename OStream>
34 OStream& operator<<(OStream &OS, const DataRefImpl &D) {
35   OS << "(" << format("0x%x8", D.p) << " (" << format("0x%x8", D.d.a) << ", " << format("0x%x8", D.d.b) << "))";
36   return OS;
37 }
38
39 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
40   // Check bitwise identical. This is the only legal way to compare a union w/o
41   // knowing which member is in use.
42   return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
43 }
44
45 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
46   return !operator==(a, b);
47 }
48
49 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
50   // Check bitwise identical. This is the only legal way to compare a union w/o
51   // knowing which member is in use.
52   return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
53 }
54
55 template <class content_type>
56 class content_iterator
57     : public std::iterator<std::forward_iterator_tag, content_type> {
58   content_type Current;
59
60 public:
61   content_iterator(content_type symb) : Current(symb) {}
62
63   const content_type *operator->() const { return &Current; }
64
65   const content_type &operator*() const { return Current; }
66
67   bool operator==(const content_iterator &other) const {
68     return Current == other.Current;
69   }
70
71   bool operator!=(const content_iterator &other) const {
72     return !(*this == other);
73   }
74
75   content_iterator &operator++() { // preincrement
76     Current.moveNext();
77     return *this;
78   }
79 };
80
81 class SymbolicFile;
82
83 /// This is a value type class that represents a single symbol in the list of
84 /// symbols in the object file.
85 class BasicSymbolRef {
86   DataRefImpl SymbolPimpl;
87   const SymbolicFile *OwningObject;
88
89 public:
90   // FIXME: should we add a SF_Text?
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   };
105
106   BasicSymbolRef() : OwningObject(nullptr) { }
107   BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
108
109   bool operator==(const BasicSymbolRef &Other) const;
110   bool operator<(const BasicSymbolRef &Other) const;
111
112   void moveNext();
113
114   std::error_code printName(raw_ostream &OS) const;
115
116   /// Get symbol flags (bitwise OR of SymbolRef::Flags)
117   uint32_t getFlags() const;
118
119   DataRefImpl getRawDataRefImpl() const;
120   const SymbolicFile *getObject() const;
121 };
122
123 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
124
125 class SymbolicFile : public Binary {
126 public:
127   ~SymbolicFile() override;
128   SymbolicFile(unsigned int Type, MemoryBufferRef Source);
129
130   // virtual interface.
131   virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
132
133   virtual std::error_code printSymbolName(raw_ostream &OS,
134                                           DataRefImpl Symb) const = 0;
135
136   virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
137
138   virtual basic_symbol_iterator symbol_begin_impl() const = 0;
139
140   virtual basic_symbol_iterator symbol_end_impl() const = 0;
141
142   // convenience wrappers.
143   basic_symbol_iterator symbol_begin() const {
144     return symbol_begin_impl();
145   }
146   basic_symbol_iterator symbol_end() const {
147     return symbol_end_impl();
148   }
149   typedef iterator_range<basic_symbol_iterator> basic_symbol_iterator_range;
150   basic_symbol_iterator_range symbols() const {
151     return basic_symbol_iterator_range(symbol_begin(), symbol_end());
152   }
153
154   // construction aux.
155   static ErrorOr<std::unique_ptr<SymbolicFile>>
156   createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
157                      LLVMContext *Context);
158
159   static ErrorOr<std::unique_ptr<SymbolicFile>>
160   createSymbolicFile(MemoryBufferRef Object) {
161     return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
162   }
163   static ErrorOr<OwningBinary<SymbolicFile>>
164   createSymbolicFile(StringRef ObjectPath);
165
166   static inline bool classof(const Binary *v) {
167     return v->isSymbolic();
168   }
169 };
170
171 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
172                                       const SymbolicFile *Owner)
173     : SymbolPimpl(SymbolP), OwningObject(Owner) {}
174
175 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
176   return SymbolPimpl == Other.SymbolPimpl;
177 }
178
179 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
180   return SymbolPimpl < Other.SymbolPimpl;
181 }
182
183 inline void BasicSymbolRef::moveNext() {
184   return OwningObject->moveSymbolNext(SymbolPimpl);
185 }
186
187 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
188   return OwningObject->printSymbolName(OS, SymbolPimpl);
189 }
190
191 inline uint32_t BasicSymbolRef::getFlags() const {
192   return OwningObject->getSymbolFlags(SymbolPimpl);
193 }
194
195 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
196   return SymbolPimpl;
197 }
198
199 inline const SymbolicFile *BasicSymbolRef::getObject() const {
200   return OwningObject;
201 }
202
203 }
204 }
205
206 #endif