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_SYMBOLIC_FILE_H
15 #define LLVM_OBJECT_SYMBOLIC_FILE_H
17 #include "llvm/Object/Binary.h"
23 // This entire union should probably be a
24 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
29 DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
32 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
33 // Check bitwise identical. This is the only legal way to compare a union w/o
34 // knowing which member is in use.
35 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
38 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
39 return !operator==(a, 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 template <class content_type> class content_iterator {
52 content_iterator(content_type symb) : Current(symb) {}
54 const content_type *operator->() const { return &Current; }
56 const content_type &operator*() const { return Current; }
58 bool operator==(const content_iterator &other) const {
59 return Current == other.Current;
62 bool operator!=(const content_iterator &other) const {
63 return !(*this == other);
66 content_iterator &operator++() { // preincrement
74 /// This is a value type class that represents a single symbol in the list of
75 /// symbols in the object file.
76 class BasicSymbolRef {
77 DataRefImpl SymbolPimpl;
78 const SymbolicFile *OwningObject;
81 // FIXME: should we add a SF_Text?
82 enum Flags : unsigned {
84 SF_Undefined = 1U << 0, // Symbol is defined in another object file
85 SF_Global = 1U << 1, // Global symbol
86 SF_Weak = 1U << 2, // Weak symbol
87 SF_Absolute = 1U << 3, // Absolute symbol
88 SF_Common = 1U << 4, // Symbol has common linkage
89 SF_FormatSpecific = 1U << 5 // Specific to the object file format
90 // (e.g. section symbols)
93 BasicSymbolRef() : OwningObject(nullptr) { }
94 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
96 bool operator==(const BasicSymbolRef &Other) const;
97 bool operator<(const BasicSymbolRef &Other) const;
101 error_code printName(raw_ostream &OS) const;
103 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
104 uint32_t getFlags() const;
106 DataRefImpl getRawDataRefImpl() const;
107 const SymbolicFile *getObject() const;
110 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
112 const uint64_t UnknownAddressOrSize = ~0ULL;
114 class SymbolicFile : public Binary {
116 virtual ~SymbolicFile();
117 SymbolicFile(unsigned int Type, MemoryBuffer *Source, bool BufferOwned);
119 // virtual interface.
120 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
122 virtual error_code printSymbolName(raw_ostream &OS,
123 DataRefImpl Symb) const = 0;
125 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
127 virtual basic_symbol_iterator symbol_begin_impl() const = 0;
129 virtual basic_symbol_iterator symbol_end_impl() const = 0;
131 // convenience wrappers.
132 basic_symbol_iterator symbol_begin() const {
133 return symbol_begin_impl();
135 basic_symbol_iterator symbol_end() const {
136 return symbol_end_impl();
140 static ErrorOr<SymbolicFile *> createIRObjectFile(MemoryBuffer *Object,
141 LLVMContext &Context,
142 bool BufferOwned = true);
144 static ErrorOr<SymbolicFile *> createSymbolicFile(MemoryBuffer *Object,
146 sys::fs::file_magic Type,
147 LLVMContext *Context);
149 static ErrorOr<SymbolicFile *> createSymbolicFile(MemoryBuffer *Object) {
150 return createSymbolicFile(Object, true, sys::fs::file_magic::unknown,
153 static ErrorOr<SymbolicFile *> createSymbolicFile(StringRef ObjectPath);
155 static inline bool classof(const Binary *v) {
156 return v->isSymbolic();
160 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
161 const SymbolicFile *Owner)
162 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
164 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
165 return SymbolPimpl == Other.SymbolPimpl;
168 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
169 return SymbolPimpl < Other.SymbolPimpl;
172 inline void BasicSymbolRef::moveNext() {
173 return OwningObject->moveSymbolNext(SymbolPimpl);
176 inline error_code BasicSymbolRef::printName(raw_ostream &OS) const {
177 return OwningObject->printSymbolName(OS, SymbolPimpl);
180 inline uint32_t BasicSymbolRef::getFlags() const {
181 return OwningObject->getSymbolFlags(SymbolPimpl);
184 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
188 inline const SymbolicFile *BasicSymbolRef::getObject() const {