1 //===- SymbolicFile.h - Interface that only provides symbols ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the SymbolicFile interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_SYMBOLICFILE_H
15 #define LLVM_OBJECT_SYMBOLICFILE_H
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Support/Format.h"
26 // This entire union should probably be a
27 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
32 DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
35 template <typename OStream>
36 OStream& operator<<(OStream &OS, const DataRefImpl &D) {
37 OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
38 << ", " << format("0x%08x", D.d.b) << "))";
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;
48 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
49 return !operator==(a, b);
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;
58 template <class content_type>
59 class content_iterator
60 : public std::iterator<std::forward_iterator_tag, content_type> {
64 content_iterator(content_type symb) : Current(std::move(symb)) {}
66 const content_type *operator->() const { return &Current; }
68 const content_type &operator*() const { return Current; }
70 bool operator==(const content_iterator &other) const {
71 return Current == other.Current;
74 bool operator!=(const content_iterator &other) const {
75 return !(*this == other);
78 content_iterator &operator++() { // preincrement
86 /// This is a value type class that represents a single symbol in the list of
87 /// symbols in the object file.
88 class BasicSymbolRef {
89 DataRefImpl SymbolPimpl;
90 const SymbolicFile *OwningObject;
93 enum Flags : unsigned {
95 SF_Undefined = 1U << 0, // Symbol is defined in another object file
96 SF_Global = 1U << 1, // Global symbol
97 SF_Weak = 1U << 2, // Weak symbol
98 SF_Absolute = 1U << 3, // Absolute symbol
99 SF_Common = 1U << 4, // Symbol has common linkage
100 SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
101 SF_Exported = 1U << 6, // Symbol is visible to other DSOs
102 SF_FormatSpecific = 1U << 7, // Specific to the object file format
103 // (e.g. section symbols)
104 SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
105 SF_Hidden = 1U << 9, // Symbol has hidden visibility
106 SF_Const = 1U << 10, // Symbol value is constant
107 SF_Executable = 1U << 11, // Symbol points to an executable section
111 BasicSymbolRef() : OwningObject(nullptr) { }
112 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
114 bool operator==(const BasicSymbolRef &Other) const;
115 bool operator<(const BasicSymbolRef &Other) const;
119 std::error_code printName(raw_ostream &OS) const;
121 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
122 uint32_t getFlags() const;
124 DataRefImpl getRawDataRefImpl() const;
125 const SymbolicFile *getObject() const;
128 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
130 class SymbolicFile : public Binary {
132 ~SymbolicFile() override;
133 SymbolicFile(unsigned int Type, MemoryBufferRef Source);
135 // virtual interface.
136 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
138 virtual std::error_code printSymbolName(raw_ostream &OS,
139 DataRefImpl Symb) const = 0;
141 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
143 virtual basic_symbol_iterator symbol_begin() const = 0;
145 virtual basic_symbol_iterator symbol_end() const = 0;
147 // convenience wrappers.
148 typedef iterator_range<basic_symbol_iterator> basic_symbol_iterator_range;
149 basic_symbol_iterator_range symbols() const {
150 return basic_symbol_iterator_range(symbol_begin(), symbol_end());
154 static Expected<std::unique_ptr<SymbolicFile>>
155 createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
156 LLVMContext *Context);
158 static Expected<std::unique_ptr<SymbolicFile>>
159 createSymbolicFile(MemoryBufferRef Object) {
160 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
162 static Expected<OwningBinary<SymbolicFile>>
163 createSymbolicFile(StringRef ObjectPath);
165 static inline bool classof(const Binary *v) {
166 return v->isSymbolic();
170 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
171 const SymbolicFile *Owner)
172 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
174 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
175 return SymbolPimpl == Other.SymbolPimpl;
178 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
179 return SymbolPimpl < Other.SymbolPimpl;
182 inline void BasicSymbolRef::moveNext() {
183 return OwningObject->moveSymbolNext(SymbolPimpl);
186 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
187 return OwningObject->printSymbolName(OS, SymbolPimpl);
190 inline uint32_t BasicSymbolRef::getFlags() const {
191 return OwningObject->getSymbolFlags(SymbolPimpl);
194 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
198 inline const SymbolicFile *BasicSymbolRef::getObject() const {