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/ADT/iterator_range.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/MemoryBuffer.h"
29 #include <system_error>
35 // This entire union should probably be a
36 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
42 DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
45 template <typename OStream>
46 OStream& operator<<(OStream &OS, const DataRefImpl &D) {
47 OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
48 << ", " << format("0x%08x", D.d.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 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
59 return !operator==(a, b);
62 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
63 // Check bitwise identical. This is the only legal way to compare a union w/o
64 // knowing which member is in use.
65 return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
68 template <class content_type>
69 class content_iterator
70 : public std::iterator<std::forward_iterator_tag, content_type> {
74 content_iterator(content_type symb) : Current(std::move(symb)) {}
76 const content_type *operator->() const { return &Current; }
78 const content_type &operator*() const { return Current; }
80 bool operator==(const content_iterator &other) const {
81 return Current == other.Current;
84 bool operator!=(const content_iterator &other) const {
85 return !(*this == other);
88 content_iterator &operator++() { // preincrement
96 /// This is a value type class that represents a single symbol in the list of
97 /// symbols in the object file.
98 class BasicSymbolRef {
99 DataRefImpl SymbolPimpl;
100 const SymbolicFile *OwningObject = nullptr;
103 enum Flags : unsigned {
105 SF_Undefined = 1U << 0, // Symbol is defined in another object file
106 SF_Global = 1U << 1, // Global symbol
107 SF_Weak = 1U << 2, // Weak symbol
108 SF_Absolute = 1U << 3, // Absolute symbol
109 SF_Common = 1U << 4, // Symbol has common linkage
110 SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
111 SF_Exported = 1U << 6, // Symbol is visible to other DSOs
112 SF_FormatSpecific = 1U << 7, // Specific to the object file format
113 // (e.g. section symbols)
114 SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
115 SF_Hidden = 1U << 9, // Symbol has hidden visibility
116 SF_Const = 1U << 10, // Symbol value is constant
117 SF_Executable = 1U << 11, // Symbol points to an executable section
121 BasicSymbolRef() = default;
122 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
124 bool operator==(const BasicSymbolRef &Other) const;
125 bool operator<(const BasicSymbolRef &Other) const;
129 std::error_code printName(raw_ostream &OS) const;
131 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
132 uint32_t getFlags() const;
134 DataRefImpl getRawDataRefImpl() const;
135 const SymbolicFile *getObject() const;
138 using basic_symbol_iterator = content_iterator<BasicSymbolRef>;
140 class SymbolicFile : public Binary {
142 SymbolicFile(unsigned int Type, MemoryBufferRef Source);
143 ~SymbolicFile() override;
145 // virtual interface.
146 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
148 virtual std::error_code printSymbolName(raw_ostream &OS,
149 DataRefImpl Symb) const = 0;
151 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
153 virtual basic_symbol_iterator symbol_begin() const = 0;
155 virtual basic_symbol_iterator symbol_end() const = 0;
157 // convenience wrappers.
158 using basic_symbol_iterator_range = iterator_range<basic_symbol_iterator>;
159 basic_symbol_iterator_range symbols() const {
160 return basic_symbol_iterator_range(symbol_begin(), symbol_end());
164 static Expected<std::unique_ptr<SymbolicFile>>
165 createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
166 LLVMContext *Context);
168 static Expected<std::unique_ptr<SymbolicFile>>
169 createSymbolicFile(MemoryBufferRef Object) {
170 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
172 static Expected<OwningBinary<SymbolicFile>>
173 createSymbolicFile(StringRef ObjectPath);
175 static inline bool classof(const Binary *v) {
176 return v->isSymbolic();
180 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
181 const SymbolicFile *Owner)
182 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
184 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
185 return SymbolPimpl == Other.SymbolPimpl;
188 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
189 return SymbolPimpl < Other.SymbolPimpl;
192 inline void BasicSymbolRef::moveNext() {
193 return OwningObject->moveSymbolNext(SymbolPimpl);
196 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
197 return OwningObject->printSymbolName(OS, SymbolPimpl);
200 inline uint32_t BasicSymbolRef::getFlags() const {
201 return OwningObject->getSymbolFlags(SymbolPimpl);
204 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
208 inline const SymbolicFile *BasicSymbolRef::getObject() const {
212 } // end namespace object
213 } // end namespace llvm
215 #endif // LLVM_OBJECT_SYMBOLICFILE_H