LLVM  13.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 
16 #include "llvm/ADT/StringRef.h"
19 #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 #include <system_error>
29 
30 namespace llvm {
31 namespace object {
32 
33 union DataRefImpl {
34  // This entire union should probably be a
35  // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
36  struct {
38  } d;
39  uintptr_t p;
40 
41  DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
42 };
43 
44 template <typename OStream>
45 OStream& operator<<(OStream &OS, const DataRefImpl &D) {
46  OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
47  << ", " << format("0x%08x", D.d.b) << "))";
48  return OS;
49 }
50 
51 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
52  // Check bitwise identical. This is the only legal way to compare a union w/o
53  // knowing which member is in use.
54  return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
55 }
56 
57 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
58  return !operator==(a, b);
59 }
60 
61 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
62  // Check bitwise identical. This is the only legal way to compare a union w/o
63  // knowing which member is in use.
64  return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
65 }
66 
67 template <class content_type> class content_iterator {
68  content_type Current;
69 
70 public:
71  using iterator_category = std::forward_iterator_tag;
72  using value_type = content_type;
73  using difference_type = std::ptrdiff_t;
74  using pointer = value_type *;
75  using reference = value_type &;
76 
77  content_iterator(content_type symb) : Current(std::move(symb)) {}
78 
79  const content_type *operator->() const { return &Current; }
80 
81  const content_type &operator*() const { return Current; }
82 
83  bool operator==(const content_iterator &other) const {
84  return Current == other.Current;
85  }
86 
87  bool operator!=(const content_iterator &other) const {
88  return !(*this == other);
89  }
90 
91  content_iterator &operator++() { // preincrement
92  Current.moveNext();
93  return *this;
94  }
95 };
96 
97 class SymbolicFile;
98 
99 /// This is a value type class that represents a single symbol in the list of
100 /// symbols in the object file.
102  DataRefImpl SymbolPimpl;
103  const SymbolicFile *OwningObject = nullptr;
104 
105 public:
106  enum Flags : unsigned {
107  SF_None = 0,
108  SF_Undefined = 1U << 0, // Symbol is defined in another object file
109  SF_Global = 1U << 1, // Global symbol
110  SF_Weak = 1U << 2, // Weak symbol
111  SF_Absolute = 1U << 3, // Absolute symbol
112  SF_Common = 1U << 4, // Symbol has common linkage
113  SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
114  SF_Exported = 1U << 6, // Symbol is visible to other DSOs
115  SF_FormatSpecific = 1U << 7, // Specific to the object file format
116  // (e.g. section symbols)
117  SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
118  SF_Hidden = 1U << 9, // Symbol has hidden visibility
119  SF_Const = 1U << 10, // Symbol value is constant
120  SF_Executable = 1U << 11, // Symbol points to an executable section
121  // (IR only)
122  };
123 
124  BasicSymbolRef() = default;
125  BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
126 
127  bool operator==(const BasicSymbolRef &Other) const;
128  bool operator<(const BasicSymbolRef &Other) const;
129 
130  void moveNext();
131 
132  Error printName(raw_ostream &OS) const;
133 
134  /// Get symbol flags (bitwise OR of SymbolRef::Flags)
136 
138  const SymbolicFile *getObject() const;
139 };
140 
142 
143 class SymbolicFile : public Binary {
144 public:
145  SymbolicFile(unsigned int Type, MemoryBufferRef Source);
146  ~SymbolicFile() override;
147 
148  // virtual interface.
149  virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
150 
151  virtual Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const = 0;
152 
153  virtual Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const = 0;
154 
155  virtual basic_symbol_iterator symbol_begin() const = 0;
156 
157  virtual basic_symbol_iterator symbol_end() const = 0;
158 
159  // convenience wrappers.
163  }
164 
165  // construction aux.
168  LLVMContext *Context, bool InitContent = true);
169 
173  }
174 
175  static bool classof(const Binary *v) {
176  return v->isSymbolic();
177  }
178 
179  static bool isSymbolicFile(file_magic Type, const LLVMContext *Context);
180 };
181 
183  const SymbolicFile *Owner)
184  : SymbolPimpl(SymbolP), OwningObject(Owner) {}
185 
186 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
187  return SymbolPimpl == Other.SymbolPimpl;
188 }
189 
190 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
191  return SymbolPimpl < Other.SymbolPimpl;
192 }
193 
195  return OwningObject->moveSymbolNext(SymbolPimpl);
196 }
197 
199  return OwningObject->printSymbolName(OS, SymbolPimpl);
200 }
201 
203  return OwningObject->getSymbolFlags(SymbolPimpl);
204 }
205 
207  return SymbolPimpl;
208 }
209 
211  return OwningObject;
212 }
213 
214 } // end namespace object
215 } // end namespace llvm
216 
217 #endif // LLVM_OBJECT_SYMBOLICFILE_H
MemoryBuffer.h
llvm::object::DataRefImpl::DataRefImpl
DataRefImpl()
Definition: SymbolicFile.h:41
llvm::file_magic::unknown
@ unknown
Unrecognized file.
Definition: Magic.h:22
llvm
Definition: AllocatorList.h:23
llvm::object::DataRefImpl::a
uint32_t a
Definition: SymbolicFile.h:37
llvm::file_magic
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
llvm::object::content_iterator::operator==
bool operator==(const content_iterator &other) const
Definition: SymbolicFile.h:83
llvm::object::BasicSymbolRef::SF_Indirect
@ SF_Indirect
Definition: SymbolicFile.h:113
StringRef.h
llvm::object::BasicSymbolRef::SF_Common
@ SF_Common
Definition: SymbolicFile.h:112
llvm::object::content_iterator::operator!=
bool operator!=(const content_iterator &other) const
Definition: SymbolicFile.h:87
llvm::object::SymbolicFile::printSymbolName
virtual Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const =0
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:928
Error.h
llvm::object::BasicSymbolRef::SF_Exported
@ SF_Exported
Definition: SymbolicFile.h:114
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::object::SymbolicFile::moveSymbolNext
virtual void moveSymbolNext(DataRefImpl &Symb) const =0
llvm::object::BasicSymbolRef::Flags
Flags
Definition: SymbolicFile.h:106
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::object::BasicSymbolRef::SF_Weak
@ SF_Weak
Definition: SymbolicFile.h:110
Format.h
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::BasicSymbolRef::SF_Executable
@ SF_Executable
Definition: SymbolicFile.h:120
llvm::object::BasicSymbolRef::printName
Error printName(raw_ostream &OS) const
Definition: SymbolicFile.h:198
a
=0.0 ? 0.0 :(a > 0.0 ? 1.0 :-1.0) a
Definition: README.txt:489
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::object::SymbolicFile::symbol_end
virtual basic_symbol_iterator symbol_end() const =0
llvm::object::SymbolicFile::getSymbolFlags
virtual Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const =0
Magic.h
llvm::object::BasicSymbolRef::getRawDataRefImpl
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:206
llvm::object::BasicSymbolRef
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:101
llvm::object::content_iterator::value_type
content_type value_type
Definition: SymbolicFile.h:72
b
the resulting code requires compare and branches when and if the revised code is with conditional branches instead of More there is a byte word extend before each where there should be only and the condition codes are not remembered when the same two values are compared twice More LSR enhancements i8 and i32 load store addressing modes are identical int b
Definition: README.txt:418
llvm::object::content_iterator::operator*
const content_type & operator*() const
Definition: SymbolicFile.h:81
llvm::object::content_iterator::pointer
value_type * pointer
Definition: SymbolicFile.h:74
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::object::SymbolicFile::symbols
basic_symbol_iterator_range symbols() const
Definition: SymbolicFile.h:161
llvm::object::DataRefImpl::p
uintptr_t p
Definition: SymbolicFile.h:39
llvm::object::BasicSymbolRef::getObject
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:210
llvm::OutputFileType::Object
@ Object
llvm::object::SymbolicFile::isSymbolicFile
static bool isSymbolicFile(file_magic Type, const LLVMContext *Context)
Definition: SymbolicFile.cpp:97
llvm::object::operator<<
raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
Definition: ObjectFile.cpp:35
llvm::object::BasicSymbolRef::SF_Const
@ SF_Const
Definition: SymbolicFile.h:119
llvm::object::Binary::isSymbolic
bool isSymbolic() const
Definition: Binary.h:108
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::object::BasicSymbolRef::SF_FormatSpecific
@ SF_FormatSpecific
Definition: SymbolicFile.h:115
llvm::object::BasicSymbolRef::getFlags
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
Definition: SymbolicFile.h:202
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::object::BasicSymbolRef::SF_None
@ SF_None
Definition: SymbolicFile.h:107
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::object::SymbolicFile::basic_symbol_iterator_range
iterator_range< basic_symbol_iterator > basic_symbol_iterator_range
Definition: SymbolicFile.h:160
llvm::object::BasicSymbolRef::SF_Undefined
@ SF_Undefined
Definition: SymbolicFile.h:108
llvm::object::content_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: SymbolicFile.h:71
llvm::object::SymbolicFile::createSymbolicFile
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object)
Definition: SymbolicFile.h:171
llvm::object::BasicSymbolRef::SF_Thumb
@ SF_Thumb
Definition: SymbolicFile.h:117
llvm::move
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:1540
llvm::object::content_iterator
Definition: SymbolicFile.h:67
llvm::object::content_iterator::operator->
const content_type * operator->() const
Definition: SymbolicFile.h:79
iterator_range.h
llvm::object::BasicSymbolRef::operator<
bool operator<(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:190
llvm::object::DataRefImpl::d
struct llvm::object::DataRefImpl::@313 d
llvm::object::operator!=
bool operator!=(const DataRefImpl &a, const DataRefImpl &b)
Definition: SymbolicFile.h:57
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::object::content_iterator::reference
value_type & reference
Definition: SymbolicFile.h:75
llvm::object::Binary
Definition: Binary.h:32
uint32_t
llvm::object::SymbolicFile::symbol_begin
virtual basic_symbol_iterator symbol_begin() const =0
llvm::object::DataRefImpl
Definition: SymbolicFile.h:33
llvm::object::SymbolicFile::SymbolicFile
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
Definition: SymbolicFile.cpp:32
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::object::operator<
bool operator<(const SectionedAddress &LHS, const SectionedAddress &RHS)
Definition: ObjectFile.h:148
llvm::object::BasicSymbolRef::BasicSymbolRef
BasicSymbolRef()=default
std
Definition: BitVector.h:838
llvm::object::content_iterator::operator++
content_iterator & operator++()
Definition: SymbolicFile.h:91
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::object::SymbolicFile::createSymbolicFile
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
llvm::object::BasicSymbolRef::SF_Absolute
@ SF_Absolute
Definition: SymbolicFile.h:111
llvm::object::operator==
bool operator==(const SectionedAddress &LHS, const SectionedAddress &RHS)
Definition: ObjectFile.h:154
Binary.h
llvm::object::BasicSymbolRef::moveNext
void moveNext()
Definition: SymbolicFile.h:194
llvm::object::BasicSymbolRef::SF_Global
@ SF_Global
Definition: SymbolicFile.h:109
llvm::object::BasicSymbolRef::SF_Hidden
@ SF_Hidden
Definition: SymbolicFile.h:118
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::object::content_iterator::difference_type
std::ptrdiff_t difference_type
Definition: SymbolicFile.h:73
llvm::object::content_iterator::content_iterator
content_iterator(content_type symb)
Definition: SymbolicFile.h:77
llvm::object::SymbolicFile::classof
static bool classof(const Binary *v)
Definition: SymbolicFile.h:175
llvm::object::DataRefImpl::b
uint32_t b
Definition: SymbolicFile.h:37
llvm::object::SymbolicFile::~SymbolicFile
~SymbolicFile() override
llvm::object::SymbolicFile
Definition: SymbolicFile.h:143
llvm::object::BasicSymbolRef::operator==
bool operator==(const BasicSymbolRef &Other) const
Definition: SymbolicFile.h:186
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1167