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"
25 // This entire union should probably be a
26 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
31 DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
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) << "))";
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;
46 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
47 return !operator==(a, b);
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;
56 template <class content_type>
57 class content_iterator
58 : public std::iterator<std::forward_iterator_tag, content_type> {
62 content_iterator(content_type symb) : Current(std::move(symb)) {}
64 const content_type *operator->() const { return &Current; }
66 const content_type &operator*() const { return Current; }
68 bool operator==(const content_iterator &other) const {
69 return Current == other.Current;
72 bool operator!=(const content_iterator &other) const {
73 return !(*this == other);
76 content_iterator &operator++() { // preincrement
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;
91 enum Flags : unsigned {
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
109 BasicSymbolRef() : OwningObject(nullptr) { }
110 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
112 bool operator==(const BasicSymbolRef &Other) const;
113 bool operator<(const BasicSymbolRef &Other) const;
117 std::error_code printName(raw_ostream &OS) const;
119 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
120 uint32_t getFlags() const;
122 DataRefImpl getRawDataRefImpl() const;
123 const SymbolicFile *getObject() const;
126 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
128 class SymbolicFile : public Binary {
130 ~SymbolicFile() override;
131 SymbolicFile(unsigned int Type, MemoryBufferRef Source);
133 // virtual interface.
134 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
136 virtual std::error_code printSymbolName(raw_ostream &OS,
137 DataRefImpl Symb) const = 0;
139 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
141 virtual basic_symbol_iterator symbol_begin() const = 0;
143 virtual basic_symbol_iterator symbol_end() const = 0;
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());
152 static Expected<std::unique_ptr<SymbolicFile>>
153 createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
154 LLVMContext *Context);
156 static Expected<std::unique_ptr<SymbolicFile>>
157 createSymbolicFile(MemoryBufferRef Object) {
158 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
160 static Expected<OwningBinary<SymbolicFile>>
161 createSymbolicFile(StringRef ObjectPath);
163 static inline bool classof(const Binary *v) {
164 return v->isSymbolic();
168 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
169 const SymbolicFile *Owner)
170 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
172 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
173 return SymbolPimpl == Other.SymbolPimpl;
176 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
177 return SymbolPimpl < Other.SymbolPimpl;
180 inline void BasicSymbolRef::moveNext() {
181 return OwningObject->moveSymbolNext(SymbolPimpl);
184 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
185 return OwningObject->printSymbolName(OS, SymbolPimpl);
188 inline uint32_t BasicSymbolRef::getFlags() const {
189 return OwningObject->getSymbolFlags(SymbolPimpl);
192 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
196 inline const SymbolicFile *BasicSymbolRef::getObject() const {