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"
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>
49 class content_iterator
50 : public std::iterator<std::forward_iterator_tag, content_type> {
54 content_iterator(content_type symb) : Current(symb) {}
56 const content_type *operator->() const { return &Current; }
58 const content_type &operator*() const { return Current; }
60 bool operator==(const content_iterator &other) const {
61 return Current == other.Current;
64 bool operator!=(const content_iterator &other) const {
65 return !(*this == other);
68 content_iterator &operator++() { // preincrement
76 /// This is a value type class that represents a single symbol in the list of
77 /// symbols in the object file.
78 class BasicSymbolRef {
79 DataRefImpl SymbolPimpl;
80 const SymbolicFile *OwningObject;
83 // FIXME: should we add a SF_Text?
84 enum Flags : unsigned {
86 SF_Undefined = 1U << 0, // Symbol is defined in another object file
87 SF_Global = 1U << 1, // Global symbol
88 SF_Weak = 1U << 2, // Weak symbol
89 SF_Absolute = 1U << 3, // Absolute symbol
90 SF_Common = 1U << 4, // Symbol has common linkage
91 SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
92 SF_Exported = 1U << 6, // Symbol is visible to other DSOs
93 SF_FormatSpecific = 1U << 7, // Specific to the object file format
94 // (e.g. section symbols)
95 SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
98 BasicSymbolRef() : OwningObject(nullptr) { }
99 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
101 bool operator==(const BasicSymbolRef &Other) const;
102 bool operator<(const BasicSymbolRef &Other) const;
106 std::error_code printName(raw_ostream &OS) const;
108 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
109 uint32_t getFlags() const;
111 DataRefImpl getRawDataRefImpl() const;
112 const SymbolicFile *getObject() const;
115 typedef content_iterator<BasicSymbolRef> basic_symbol_iterator;
117 const uint64_t UnknownAddressOrSize = ~0ULL;
119 class SymbolicFile : public Binary {
121 ~SymbolicFile() override;
122 SymbolicFile(unsigned int Type, MemoryBufferRef Source);
124 // virtual interface.
125 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
127 virtual std::error_code printSymbolName(raw_ostream &OS,
128 DataRefImpl Symb) const = 0;
130 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
132 virtual basic_symbol_iterator symbol_begin_impl() const = 0;
134 virtual basic_symbol_iterator symbol_end_impl() const = 0;
136 // convenience wrappers.
137 basic_symbol_iterator symbol_begin() const {
138 return symbol_begin_impl();
140 basic_symbol_iterator symbol_end() const {
141 return symbol_end_impl();
143 typedef iterator_range<basic_symbol_iterator> basic_symbol_iterator_range;
144 basic_symbol_iterator_range symbols() const {
145 return basic_symbol_iterator_range(symbol_begin(), symbol_end());
149 static ErrorOr<std::unique_ptr<SymbolicFile>>
150 createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type,
151 LLVMContext *Context);
153 static ErrorOr<std::unique_ptr<SymbolicFile>>
154 createSymbolicFile(MemoryBufferRef Object) {
155 return createSymbolicFile(Object, sys::fs::file_magic::unknown, nullptr);
157 static ErrorOr<OwningBinary<SymbolicFile>>
158 createSymbolicFile(StringRef ObjectPath);
160 static inline bool classof(const Binary *v) {
161 return v->isSymbolic();
165 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
166 const SymbolicFile *Owner)
167 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
169 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
170 return SymbolPimpl == Other.SymbolPimpl;
173 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
174 return SymbolPimpl < Other.SymbolPimpl;
177 inline void BasicSymbolRef::moveNext() {
178 return OwningObject->moveSymbolNext(SymbolPimpl);
181 inline std::error_code BasicSymbolRef::printName(raw_ostream &OS) const {
182 return OwningObject->printSymbolName(OS, SymbolPimpl);
185 inline uint32_t BasicSymbolRef::getFlags() const {
186 return OwningObject->getSymbolFlags(SymbolPimpl);
189 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
193 inline const SymbolicFile *BasicSymbolRef::getObject() const {