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"
24 // This entire union should probably be a
25 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
30 DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
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) << "))";
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;
45 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
46 return !operator==(a, b);
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;
55 template <class content_type>
56 class content_iterator
57 : public std::iterator<std::forward_iterator_tag, content_type> {
61 content_iterator(content_type symb) : Current(symb) {}
63 const content_type *operator->() const { return &Current; }
65 const content_type &operator*() const { return Current; }
67 bool operator==(const content_iterator &other) const {
68 return Current == other.Current;
71 bool operator!=(const content_iterator &other) const {
72 return !(*this == other);
75 content_iterator &operator++() { // preincrement
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;
90 // FIXME: should we add a SF_Text?
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
106 BasicSymbolRef() : OwningObject(nullptr) { }
107 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
109 bool operator==(const BasicSymbolRef &Other) const;
110 bool operator<(const BasicSymbolRef &Other) const;
114 std::error_code printName(raw_ostream &OS) const;
116 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
117 uint32_t getFlags() const;
119 DataRefImpl getRawDataRefImpl() const;
120 const SymbolicFile *getObject() const;
123 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
125 class SymbolicFile : public Binary {
127 ~SymbolicFile() override;
128 SymbolicFile(unsigned int Type, MemoryBufferRef Source);
130 // virtual interface.
131 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
133 virtual std::error_code printSymbolName(raw_ostream &OS,
134 DataRefImpl Symb) const = 0;
136 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
138 virtual basic_symbol_iterator symbol_begin_impl() const = 0;
140 virtual basic_symbol_iterator symbol_end_impl() const = 0;
142 // convenience wrappers.
143 basic_symbol_iterator symbol_begin() const {
144 return symbol_begin_impl();
146 basic_symbol_iterator symbol_end() const {
147 return symbol_end_impl();
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());
155 static ErrorOr<std::unique_ptr<SymbolicFile>>
156 createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
157 LLVMContext *Context);
159 static ErrorOr<std::unique_ptr<SymbolicFile>>
160 createSymbolicFile(MemoryBufferRef Object) {
161 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
163 static ErrorOr<OwningBinary<SymbolicFile>>
164 createSymbolicFile(StringRef ObjectPath);
166 static inline bool classof(const Binary *v) {
167 return v->isSymbolic();
171 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
172 const SymbolicFile *Owner)
173 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
175 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
176 return SymbolPimpl == Other.SymbolPimpl;
179 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
180 return SymbolPimpl < Other.SymbolPimpl;
183 inline void BasicSymbolRef::moveNext() {
184 return OwningObject->moveSymbolNext(SymbolPimpl);
187 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
188 return OwningObject->printSymbolName(OS, SymbolPimpl);
191 inline uint32_t BasicSymbolRef::getFlags() const {
192 return OwningObject->getSymbolFlags(SymbolPimpl);
195 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
199 inline const SymbolicFile *BasicSymbolRef::getObject() const {