LLVM 22.0.0git
SymbolicFile.h
Go to the documentation of this file.
1//===- SymbolicFile.h - Interface that only provides symbols ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the SymbolicFile interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_SYMBOLICFILE_H
14#define LLVM_OBJECT_SYMBOLICFILE_H
15
18#include "llvm/Object/Binary.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/Format.h"
23#include <cinttypes>
24#include <cstdint>
25#include <cstring>
26#include <iterator>
27#include <memory>
28
29namespace llvm {
30
31class LLVMContext;
32class raw_ostream;
33
34namespace object {
35
37 // This entire union should probably be a
38 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
39 struct {
41 } d;
42 uintptr_t p;
43
44 DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
45};
46
47template <typename OStream>
48OStream& operator<<(OStream &OS, const DataRefImpl &D) {
49 OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
50 << ", " << format("0x%08x", D.d.b) << "))";
51 return OS;
52}
53
54inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
55 // Check bitwise identical. This is the only legal way to compare a union w/o
56 // knowing which member is in use.
57 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
58}
59
60inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
61 return !operator==(a, b);
62}
63
64inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
65 // Check bitwise identical. This is the only legal way to compare a union w/o
66 // knowing which member is in use.
67 return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
68}
69
70template <class content_type> class content_iterator {
71 content_type Current;
72
73public:
74 using iterator_category = std::forward_iterator_tag;
75 using value_type = const content_type;
76 using difference_type = std::ptrdiff_t;
79
80 content_iterator(content_type symb) : Current(std::move(symb)) {}
81
82 const content_type *operator->() const { return &Current; }
83
84 const content_type &operator*() const { return Current; }
85
86 bool operator==(const content_iterator &other) const {
87 return Current == other.Current;
88 }
89
90 bool operator!=(const content_iterator &other) const {
91 return !(*this == other);
92 }
93
94 content_iterator &operator++() { // preincrement
95 Current.moveNext();
96 return *this;
97 }
98};
99
100class SymbolicFile;
101
102/// This is a value type class that represents a single symbol in the list of
103/// symbols in the object file.
105 DataRefImpl SymbolPimpl;
106 const SymbolicFile *OwningObject = nullptr;
107
108public:
109 enum Flags : unsigned {
111 SF_Undefined = 1U << 0, // Symbol is defined in another object file
112 SF_Global = 1U << 1, // Global symbol
113 SF_Weak = 1U << 2, // Weak symbol
114 SF_Absolute = 1U << 3, // Absolute symbol
115 SF_Common = 1U << 4, // Symbol has common linkage
116 SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
117 SF_Exported = 1U << 6, // Symbol is visible to other DSOs
118 SF_FormatSpecific = 1U << 7, // Specific to the object file format
119 // (e.g. section symbols)
120 SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
121 SF_Hidden = 1U << 9, // Symbol has hidden visibility
122 SF_Const = 1U << 10, // Symbol value is constant
123 SF_Executable = 1U << 11, // Symbol points to an executable section
124 // (IR only)
125 };
126
127 BasicSymbolRef() = default;
128 BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
129
130 bool operator==(const BasicSymbolRef &Other) const;
131 bool operator<(const BasicSymbolRef &Other) const;
132
133 void moveNext();
134
136
137 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
139
141 const SymbolicFile *getObject() const;
142};
143
145
147public:
148 SymbolicFile(unsigned int Type, MemoryBufferRef Source);
149 ~SymbolicFile() override;
150
151 // virtual interface.
152 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
153
154 virtual Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const = 0;
155
157
159
161
162 virtual bool is64Bit() const = 0;
163
164 // convenience wrappers.
167 return basic_symbol_iterator_range(symbol_begin(), symbol_end());
168 }
169
170 // construction aux.
172 createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type,
173 LLVMContext *Context, bool InitContent = true);
174
177 return createSymbolicFile(Object, llvm::file_magic::unknown, nullptr);
178 }
179
180 static bool classof(const Binary *v) {
181 return v->isSymbolic();
182 }
183
184 static bool isSymbolicFile(file_magic Type, const LLVMContext *Context);
185};
186
188 const SymbolicFile *Owner)
189 : SymbolPimpl(SymbolP), OwningObject(Owner) {}
190
192 return SymbolPimpl == Other.SymbolPimpl;
193}
194
196 return SymbolPimpl < Other.SymbolPimpl;
197}
198
200 return OwningObject->moveSymbolNext(SymbolPimpl);
201}
202
204 return OwningObject->printSymbolName(OS, SymbolPimpl);
205}
206
208 return OwningObject->getSymbolFlags(SymbolPimpl);
209}
210
212 return SymbolPimpl;
213}
214
216 return OwningObject;
217}
218
219} // end namespace object
220} // end namespace llvm
221
222#endif // LLVM_OBJECT_SYMBOLICFILE_H
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ABI
Definition: Compiler.h:213
raw_pwrite_stream & OS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A range adaptor for a pair of iterators.
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:104
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
Definition: SymbolicFile.h:207
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:215
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:211
bool operator==(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:191
Error printName(raw_ostream &OS) const
Definition: SymbolicFile.h:203
bool operator<(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:195
static bool classof(const Binary *v)
Definition: SymbolicFile.h:180
virtual basic_symbol_iterator symbol_begin() const =0
virtual bool is64Bit() const =0
virtual basic_symbol_iterator symbol_end() const =0
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object)
Definition: SymbolicFile.h:176
virtual Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const =0
virtual Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const =0
basic_symbol_iterator_range symbols() const
Definition: SymbolicFile.h:166
virtual void moveSymbolNext(DataRefImpl &Symb) const =0
const content_type & operator*() const
Definition: SymbolicFile.h:84
const content_type * operator->() const
Definition: SymbolicFile.h:82
std::forward_iterator_tag iterator_category
Definition: SymbolicFile.h:74
bool operator==(const content_iterator &other) const
Definition: SymbolicFile.h:86
content_iterator & operator++()
Definition: SymbolicFile.h:94
bool operator!=(const content_iterator &other) const
Definition: SymbolicFile.h:90
const content_type value_type
Definition: SymbolicFile.h:75
content_iterator(content_type symb)
Definition: SymbolicFile.h:80
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B)
bool operator!=(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:60
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
Definition: ObjectFile.cpp:35
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:126
@ Other
Any other memory.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:21
@ unknown
Unrecognized file.
Definition: Magic.h:23
struct llvm::object::DataRefImpl::@378 d