xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Object/SymbolicFile.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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/iterator_range.h"
17 #include "llvm/BinaryFormat/Magic.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/MemoryBufferRef.h"
23 #include <cinttypes>
24 #include <cstdint>
25 #include <cstring>
26 #include <iterator>
27 #include <memory>
28 
29 namespace llvm {
30 
31 class LLVMContext;
32 class raw_ostream;
33 
34 namespace object {
35 
36 union DataRefImpl {
37   // This entire union should probably be a
38   // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
39   struct {
40     uint32_t a, b;
41   } d;
42   uintptr_t p;
43 
DataRefImpl()44   DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
45 };
46 
47 template <typename OStream>
48 OStream& 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 
54 inline 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 
60 inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
61   return !operator==(a, b);
62 }
63 
64 inline 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 
70 template <class content_type> class content_iterator {
71   content_type Current;
72 
73 public:
74   using iterator_category = std::forward_iterator_tag;
75   using value_type = const content_type;
76   using difference_type = std::ptrdiff_t;
77   using pointer = value_type *;
78   using reference = value_type &;
79 
content_iterator(content_type symb)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 
100 class SymbolicFile;
101 
102 /// This is a value type class that represents a single symbol in the list of
103 /// symbols in the object file.
104 class BasicSymbolRef {
105   DataRefImpl SymbolPimpl;
106   const SymbolicFile *OwningObject = nullptr;
107 
108 public:
109   enum Flags : unsigned {
110     SF_None = 0,
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 
135   Error printName(raw_ostream &OS) const;
136 
137   /// Get symbol flags (bitwise OR of SymbolRef::Flags)
138   Expected<uint32_t> getFlags() const;
139 
140   DataRefImpl getRawDataRefImpl() const;
141   const SymbolicFile *getObject() const;
142 };
143 
144 using basic_symbol_iterator = content_iterator<BasicSymbolRef>;
145 
146 class LLVM_ABI SymbolicFile : public Binary {
147 public:
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 
156   virtual Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const = 0;
157 
158   virtual basic_symbol_iterator symbol_begin() const = 0;
159 
160   virtual basic_symbol_iterator symbol_end() const = 0;
161 
162   virtual bool is64Bit() const = 0;
163 
164   // convenience wrappers.
165   using basic_symbol_iterator_range = iterator_range<basic_symbol_iterator>;
symbols()166   basic_symbol_iterator_range symbols() const {
167     return basic_symbol_iterator_range(symbol_begin(), symbol_end());
168   }
169 
170   // construction aux.
171   static Expected<std::unique_ptr<SymbolicFile>>
172   createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type,
173                      LLVMContext *Context, bool InitContent = true);
174 
175   static Expected<std::unique_ptr<SymbolicFile>>
createSymbolicFile(MemoryBufferRef Object)176   createSymbolicFile(MemoryBufferRef Object) {
177     return createSymbolicFile(Object, llvm::file_magic::unknown, nullptr);
178   }
179 
classof(const Binary * v)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 
BasicSymbolRef(DataRefImpl SymbolP,const SymbolicFile * Owner)187 inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
188                                       const SymbolicFile *Owner)
189     : SymbolPimpl(SymbolP), OwningObject(Owner) {}
190 
191 inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
192   return SymbolPimpl == Other.SymbolPimpl;
193 }
194 
195 inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
196   return SymbolPimpl < Other.SymbolPimpl;
197 }
198 
moveNext()199 inline void BasicSymbolRef::moveNext() {
200   return OwningObject->moveSymbolNext(SymbolPimpl);
201 }
202 
printName(raw_ostream & OS)203 inline Error BasicSymbolRef::printName(raw_ostream &OS) const {
204   return OwningObject->printSymbolName(OS, SymbolPimpl);
205 }
206 
getFlags()207 inline Expected<uint32_t> BasicSymbolRef::getFlags() const {
208   return OwningObject->getSymbolFlags(SymbolPimpl);
209 }
210 
getRawDataRefImpl()211 inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
212   return SymbolPimpl;
213 }
214 
getObject()215 inline const SymbolicFile *BasicSymbolRef::getObject() const {
216   return OwningObject;
217 }
218 
219 } // end namespace object
220 } // end namespace llvm
221 
222 #endif // LLVM_OBJECT_SYMBOLICFILE_H
223