LLVM  10.0.0svn
Archive.h
Go to the documentation of this file.
1 //===- Archive.h - ar archive file format -----------------------*- 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 ar archive file format class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_ARCHIVE_H
14 #define LLVM_OBJECT_ARCHIVE_H
15 
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Support/Chrono.h"
22 #include "llvm/Support/Error.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <cstdint>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 namespace llvm {
33 namespace object {
34 
35 class Archive;
36 
38 public:
39  friend class Archive;
40 
41  ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
42  uint64_t Size, Error *Err);
43  // ArchiveMemberHeader() = default;
44 
45  /// Get the name without looking up long names.
47 
48  /// Get the name looking up long names.
49  Expected<StringRef> getName(uint64_t Size) const;
50 
52 
55 
57  return StringRef(ArMemHdr->LastModified,
58  sizeof(ArMemHdr->LastModified)).rtrim(' ');
59  }
60 
61  Expected<unsigned> getUID() const;
62  Expected<unsigned> getGID() const;
63 
64  // This returns the size of the private struct ArMemHdrType
65  uint64_t getSizeOf() const {
66  return sizeof(ArMemHdrType);
67  }
68 
69 private:
70  struct ArMemHdrType {
71  char Name[16];
72  char LastModified[12];
73  char UID[6];
74  char GID[6];
75  char AccessMode[8];
76  char Size[10]; ///< Size of data, not including header or padding.
77  char Terminator[2];
78  };
79  Archive const *Parent;
80  ArMemHdrType const *ArMemHdr;
81 };
82 
83 class Archive : public Binary {
84  virtual void anchor();
85 
86 public:
87  class Child {
88  friend Archive;
89  friend ArchiveMemberHeader;
90 
91  const Archive *Parent;
92  ArchiveMemberHeader Header;
93  /// Includes header but not padding byte.
95  /// Offset from Data to the start of the file.
96  uint16_t StartOfFile;
97 
98  Expected<bool> isThinMember() const;
99 
100  public:
101  Child(const Archive *Parent, const char *Start, Error *Err);
102  Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
103 
104  bool operator ==(const Child &other) const {
105  assert(!Parent || !other.Parent || Parent == other.Parent);
106  return Data.begin() == other.Data.begin();
107  }
108 
109  const Archive *getParent() const { return Parent; }
110  Expected<Child> getNext() const;
111 
113  Expected<std::string> getFullName() const;
114  Expected<StringRef> getRawName() const { return Header.getRawName(); }
115 
117  return Header.getLastModified();
118  }
119 
121  return Header.getRawLastModified();
122  }
123 
124  Expected<unsigned> getUID() const { return Header.getUID(); }
125  Expected<unsigned> getGID() const { return Header.getGID(); }
126 
128  return Header.getAccessMode();
129  }
130 
131  /// \return the size of the archive member without the header or padding.
132  Expected<uint64_t> getSize() const;
133  /// \return the size in the archive header for this member.
134  Expected<uint64_t> getRawSize() const;
135 
136  Expected<StringRef> getBuffer() const;
137  uint64_t getChildOffset() const;
138 
139  Expected<MemoryBufferRef> getMemoryBufferRef() const;
140 
142  getAsBinary(LLVMContext *Context = nullptr) const;
143  };
144 
146  Child C;
147 
148  public:
149  ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
150  ChildFallibleIterator(const Child &C) : C(C) {}
151 
152  const Child *operator->() const { return &C; }
153  const Child &operator*() const { return C; }
154 
155  bool operator==(const ChildFallibleIterator &other) const {
156  // Ignore errors here: If an error occurred during increment then getNext
157  // will have been set to child_end(), and the following comparison should
158  // do the right thing.
159  return C == other.C;
160  }
161 
162  bool operator!=(const ChildFallibleIterator &other) const {
163  return !(*this == other);
164  }
165 
166  Error inc() {
167  auto NextChild = C.getNext();
168  if (!NextChild)
169  return NextChild.takeError();
170  C = std::move(*NextChild);
171  return Error::success();
172  }
173  };
174 
176 
177  class Symbol {
178  const Archive *Parent;
179  uint32_t SymbolIndex;
180  uint32_t StringIndex; // Extra index to the string.
181 
182  public:
183  Symbol(const Archive *p, uint32_t symi, uint32_t stri)
184  : Parent(p)
185  , SymbolIndex(symi)
186  , StringIndex(stri) {}
187 
188  bool operator ==(const Symbol &other) const {
189  return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
190  }
191 
192  StringRef getName() const;
193  Expected<Child> getMember() const;
194  Symbol getNext() const;
195  };
196 
198  Symbol symbol;
199 
200  public:
201  symbol_iterator(const Symbol &s) : symbol(s) {}
202 
203  const Symbol *operator->() const { return &symbol; }
204  const Symbol &operator*() const { return symbol; }
205 
206  bool operator==(const symbol_iterator &other) const {
207  return symbol == other.symbol;
208  }
209 
210  bool operator!=(const symbol_iterator &other) const {
211  return !(*this == other);
212  }
213 
214  symbol_iterator& operator++() { // Preincrement
215  symbol = symbol.getNext();
216  return *this;
217  }
218  };
219 
221  static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
222 
223  /// Size field is 10 decimal digits long
224  static const uint64_t MaxMemberSize = 9999999999;
225 
226  enum Kind {
232  K_COFF
233  };
234 
235  Kind kind() const { return (Kind)Format; }
236  bool isThin() const { return IsThin; }
237 
238  child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
239  child_iterator child_end() const;
241  bool SkipInternal = true) const {
242  return make_range(child_begin(Err, SkipInternal), child_end());
243  }
244 
245  symbol_iterator symbol_begin() const;
246  symbol_iterator symbol_end() const;
248  return make_range(symbol_begin(), symbol_end());
249  }
250 
251  // Cast methods.
252  static bool classof(Binary const *v) {
253  return v->isArchive();
254  }
255 
256  // check if a symbol is in the archive
257  Expected<Optional<Child>> findSym(StringRef name) const;
258 
259  bool isEmpty() const;
260  bool hasSymbolTable() const;
261  StringRef getSymbolTable() const { return SymbolTable; }
262  StringRef getStringTable() const { return StringTable; }
263  uint32_t getNumberOfSymbols() const;
264 
265  std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
266  return std::move(ThinBuffers);
267  }
268 
269 private:
270  StringRef SymbolTable;
271  StringRef StringTable;
272 
273  StringRef FirstRegularData;
274  uint16_t FirstRegularStartOfFile = -1;
275  void setFirstRegular(const Child &C);
276 
277  unsigned Format : 3;
278  unsigned IsThin : 1;
279  mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
280 };
281 
282 } // end namespace object
283 } // end namespace llvm
284 
285 #endif // LLVM_OBJECT_ARCHIVE_H
uint64_t CallInst * C
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Expected< uint64_t > getSize() const
Definition: Archive.cpp:226
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Expected< Child > getNext() const
Definition: Archive.cpp:456
bool operator==(const symbol_iterator &other) const
Definition: Archive.h:206
Expected< StringRef > getRawName() const
Get the name without looking up long names.
Definition: Archive.cpp:100
std::vector< std::unique_ptr< MemoryBuffer > > takeThinBuffers()
Definition: Archive.h:265
ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr, uint64_t Size, Error *Err)
Definition: Archive.cpp:53
StringRef getRawLastModified() const
Definition: Archive.h:120
Expected< unsigned > getUID() const
Definition: Archive.cpp:283
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
bool operator==(const SectionedAddress &LHS, const SectionedAddress &RHS)
Definition: ObjectFile.h:157
Expected< unsigned > getGID() const
Definition: Archive.cpp:302
Symbol(const Archive *p, uint32_t symi, uint32_t stri)
Definition: Archive.h:183
bool isArchive() const
Definition: Binary.h:106
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.h:116
StringRef getRawLastModified() const
Definition: Archive.h:56
bool operator!=(const ChildFallibleIterator &other) const
Definition: Archive.h:162
uint64_t getSizeOf() const
Definition: Archive.h:65
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
StringRef getStringTable() const
Definition: Archive.h:262
A wrapper class for fallible iterators.
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.cpp:244
const Symbol & operator*() const
Definition: Archive.h:204
Expected< unsigned > getUID() const
Definition: Archive.h:124
const Symbol * operator->() const
Definition: Archive.h:203
const Archive * getParent() const
Definition: Archive.h:109
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.h:127
Expected< StringRef > getName(uint64_t Size) const
Get the name looking up long names.
Definition: Archive.cpp:128
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
Kind kind() const
Definition: Archive.h:235
A range adaptor for a pair of iterators.
Expected< StringRef > getRawName() const
Definition: Archive.h:114
bool operator!=(const symbol_iterator &other) const
Definition: Archive.h:210
iterator begin() const
Definition: StringRef.h:101
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition: Archive.h:240
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.cpp:263
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
iterator_range< symbol_iterator > symbols() const
Definition: Archive.h:247
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
static bool classof(Binary const *v)
Definition: Archive.h:252
bool isThin() const
Definition: Archive.h:236
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:803
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Expected< unsigned > getGID() const
Definition: Archive.h:125
bool operator==(const ChildFallibleIterator &other) const
Definition: Archive.h:155
StringRef getSymbolTable() const
Definition: Archive.h:261
symbol_iterator & operator++()
Definition: Archive.h:214