LLVM  14.0.0git
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.
50 
52 
55 
57  return StringRef(ArMemHdr->LastModified, sizeof(ArMemHdr->LastModified))
58  .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 { return sizeof(ArMemHdrType); }
66 
67 private:
68  struct ArMemHdrType {
69  char Name[16];
70  char LastModified[12];
71  char UID[6];
72  char GID[6];
73  char AccessMode[8];
74  char Size[10]; ///< Size of data, not including header or padding.
75  char Terminator[2];
76  };
77  Archive const *Parent;
78  ArMemHdrType const *ArMemHdr;
79 };
80 
81 class Archive : public Binary {
82  virtual void anchor();
83 
84 public:
85  class Child {
86  friend Archive;
87  friend ArchiveMemberHeader;
88 
89  const Archive *Parent;
90  ArchiveMemberHeader Header;
91  /// Includes header but not padding byte.
92  StringRef Data;
93  /// Offset from Data to the start of the file.
94  uint16_t StartOfFile;
95 
96  Expected<bool> isThinMember() const;
97 
98  public:
99  Child(const Archive *Parent, const char *Start, Error *Err);
100  Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
101 
102  bool operator==(const Child &other) const {
103  assert(!Parent || !other.Parent || Parent == other.Parent);
104  return Data.begin() == other.Data.begin();
105  }
106 
107  const Archive *getParent() const { return Parent; }
108  Expected<Child> getNext() const;
109 
112  Expected<StringRef> getRawName() const { return Header.getRawName(); }
113 
115  return Header.getLastModified();
116  }
117 
118  StringRef getRawLastModified() const { return Header.getRawLastModified(); }
119 
120  Expected<unsigned> getUID() const { return Header.getUID(); }
121  Expected<unsigned> getGID() const { return Header.getGID(); }
122 
124  return Header.getAccessMode();
125  }
126 
127  /// \return the size of the archive member without the header or padding.
128  Expected<uint64_t> getSize() const;
129  /// \return the size in the archive header for this member.
131 
133  uint64_t getChildOffset() const;
134  uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; }
135 
137 
139  getAsBinary(LLVMContext *Context = nullptr) const;
140  };
141 
143  Child C;
144 
145  public:
146  ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
148 
149  const Child *operator->() const { return &C; }
150  const Child &operator*() const { return C; }
151 
152  bool operator==(const ChildFallibleIterator &other) const {
153  // Ignore errors here: If an error occurred during increment then getNext
154  // will have been set to child_end(), and the following comparison should
155  // do the right thing.
156  return C == other.C;
157  }
158 
159  bool operator!=(const ChildFallibleIterator &other) const {
160  return !(*this == other);
161  }
162 
163  Error inc() {
164  auto NextChild = C.getNext();
165  if (!NextChild)
166  return NextChild.takeError();
167  C = std::move(*NextChild);
168  return Error::success();
169  }
170  };
171 
173 
174  class Symbol {
175  const Archive *Parent;
176  uint32_t SymbolIndex;
177  uint32_t StringIndex; // Extra index to the string.
178 
179  public:
180  Symbol(const Archive *p, uint32_t symi, uint32_t stri)
181  : Parent(p), SymbolIndex(symi), StringIndex(stri) {}
182 
183  bool operator==(const Symbol &other) const {
184  return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
185  }
186 
187  StringRef getName() const;
188  Expected<Child> getMember() const;
189  Symbol getNext() const;
190  };
191 
193  Symbol symbol;
194 
195  public:
196  symbol_iterator(const Symbol &s) : symbol(s) {}
197 
198  const Symbol *operator->() const { return &symbol; }
199  const Symbol &operator*() const { return symbol; }
200 
201  bool operator==(const symbol_iterator &other) const {
202  return symbol == other.symbol;
203  }
204 
205  bool operator!=(const symbol_iterator &other) const {
206  return !(*this == other);
207  }
208 
209  symbol_iterator &operator++() { // Preincrement
210  symbol = symbol.getNext();
211  return *this;
212  }
213  };
214 
217 
218  /// Size field is 10 decimal digits long
219  static const uint64_t MaxMemberSize = 9999999999;
220 
222 
223  Kind kind() const { return (Kind)Format; }
224  bool isThin() const { return IsThin; }
225 
226  child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
227  child_iterator child_end() const;
229  bool SkipInternal = true) const {
230  return make_range(child_begin(Err, SkipInternal), child_end());
231  }
232 
234  symbol_iterator symbol_end() const;
236  return make_range(symbol_begin(), symbol_end());
237  }
238 
239  // Cast methods.
240  static bool classof(Binary const *v) { return v->isArchive(); }
241 
242  // check if a symbol is in the archive
244 
245  bool isEmpty() const;
246  bool hasSymbolTable() const;
247  StringRef getSymbolTable() const { return SymbolTable; }
248  StringRef getStringTable() const { return StringTable; }
250 
251  std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
252  return std::move(ThinBuffers);
253  }
254 
255 private:
256  StringRef SymbolTable;
257  StringRef StringTable;
258 
259  StringRef FirstRegularData;
260  uint16_t FirstRegularStartOfFile = -1;
261  void setFirstRegular(const Child &C);
262 
263  unsigned Format : 3;
264  unsigned IsThin : 1;
265  mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
266 };
267 
268 } // end namespace object
269 } // end namespace llvm
270 
271 #endif // LLVM_OBJECT_ARCHIVE_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
MemoryBuffer.h
llvm::object::Archive::K_GNU64
@ K_GNU64
Definition: Archive.h:221
llvm::object::Archive::K_COFF
@ K_COFF
Definition: Archive.h:221
llvm::object::Archive::Kind
Kind
Definition: Archive.h:221
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::object::Archive::K_BSD
@ K_BSD
Definition: Archive.h:221
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
Optional.h
llvm::object::Archive::Child::getAsBinary
Expected< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
Definition: Archive.cpp:527
llvm::object::ArchiveMemberHeader::getRawLastModified
StringRef getRawLastModified() const
Definition: Archive.h:56
llvm::object::Archive::Child::getNext
Expected< Child > getNext() const
Definition: Archive.cpp:464
FileSystem.h
llvm::object::Archive::Child::operator==
bool operator==(const Child &other) const
Definition: Archive.h:102
llvm::object::Archive::child_end
child_iterator child_end() const
Definition: Archive.cpp:798
llvm::object::Archive::Archive
Archive(MemoryBufferRef Source, Error &Err)
Definition: Archive.cpp:551
llvm::StringRef::rtrim
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:856
llvm::object::Archive::children
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition: Archive.h:228
llvm::object::Archive::Child::Child
Child(const Archive *Parent, const char *Start, Error *Err)
Definition: Archive.cpp:338
StringRef.h
llvm::object::Archive::Child::getFullName
Expected< std::string > getFullName() const
Definition: Archive.cpp:424
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::object::ArchiveMemberHeader::getGID
Expected< unsigned > getGID() const
Definition: Archive.cpp:313
llvm::object::Archive::symbol_iterator::operator==
bool operator==(const symbol_iterator &other) const
Definition: Archive.h:201
llvm::object::Archive::getStringTable
StringRef getStringTable() const
Definition: Archive.h:248
llvm::object::Archive::symbol_iterator::operator*
const Symbol & operator*() const
Definition: Archive.h:199
Error.h
llvm::object::Archive::symbol_iterator::symbol_iterator
symbol_iterator(const Symbol &s)
Definition: Archive.h:196
llvm::object::Archive::K_GNU
@ K_GNU
Definition: Archive.h:221
llvm::object::ArchiveMemberHeader::getName
Expected< StringRef > getName(uint64_t Size) const
Get the name looking up long names.
Definition: Archive.cpp:127
llvm::object::Archive::ChildFallibleIterator
Definition: Archive.h:142
llvm::object::Archive::getNumberOfSymbols
uint32_t getNumberOfSymbols() const
Definition: Archive.cpp:965
llvm::object::Archive::ChildFallibleIterator::inc
Error inc()
Definition: Archive.h:163
llvm::object::Archive::symbol_begin
symbol_iterator symbol_begin() const
Definition: Archive.cpp:905
llvm::object::Archive::Symbol
Definition: Archive.h:174
llvm::object::Archive::Child::getGID
Expected< unsigned > getGID() const
Definition: Archive.h:121
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::object::ArchiveMemberHeader::getLastModified
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.cpp:271
llvm::object::ArchiveMemberHeader::getSizeOf
uint64_t getSizeOf() const
Definition: Archive.h:65
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
p
the resulting code requires compare and branches when and if * p
Definition: README.txt:396
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::Archive::ChildFallibleIterator::operator->
const Child * operator->() const
Definition: Archive.h:149
llvm::object::Archive::Symbol::getName
StringRef getName() const
Definition: Archive.cpp:802
llvm::object::Archive::symbol_iterator
Definition: Archive.h:192
Chrono.h
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::object::Archive::Symbol::Symbol
Symbol(const Archive *p, uint32_t symi, uint32_t stri)
Definition: Archive.h:180
llvm::object::Archive
Definition: Archive.h:81
llvm::object::Binary::isArchive
bool isArchive() const
Definition: Binary.h:112
llvm::object::Archive::isEmpty
bool isEmpty() const
Definition: Archive.cpp:1000
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::object::Archive::takeThinBuffers
std::vector< std::unique_ptr< MemoryBuffer > > takeThinBuffers()
Definition: Archive.h:251
llvm::object::Archive::MaxMemberSize
static const uint64_t MaxMemberSize
Size field is 10 decimal digits long.
Definition: Archive.h:219
llvm::object::Archive::symbol_iterator::operator!=
bool operator!=(const symbol_iterator &other) const
Definition: Archive.h:205
llvm::object::Archive::getSymbolTable
StringRef getSymbolTable() const
Definition: Archive.h:247
fallible_iterator.h
llvm::object::Archive::Child::getBuffer
Expected< StringRef > getBuffer() const
Definition: Archive.cpp:442
llvm::object::Archive::ChildFallibleIterator::ChildFallibleIterator
ChildFallibleIterator()
Definition: Archive.h:146
llvm::object::Archive::Child::getLastModified
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.h:114
llvm::object::ArchiveMemberHeader::ArchiveMemberHeader
ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr, uint64_t Size, Error *Err)
Definition: Archive.cpp:52
llvm::object::Archive::isThin
bool isThin() const
Definition: Archive.h:224
llvm::object::Archive::Child::getDataOffset
uint64_t getDataOffset() const
Definition: Archive.h:134
llvm::object::Archive::Child::getRawName
Expected< StringRef > getRawName() const
Definition: Archive.h:112
llvm::object::Archive::Child::getUID
Expected< unsigned > getUID() const
Definition: Archive.h:120
llvm::object::Archive::classof
static bool classof(Binary const *v)
Definition: Archive.h:240
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::object::Archive::K_DARWIN64
@ K_DARWIN64
Definition: Archive.h:221
uint64_t
llvm::object::symbol_iterator
Definition: ObjectFile.h:207
llvm::object::Archive::ChildFallibleIterator::ChildFallibleIterator
ChildFallibleIterator(const Child &C)
Definition: Archive.h:147
llvm::object::ArchiveMemberHeader::getRawName
Expected< StringRef > getRawName() const
Get the name without looking up long names.
Definition: Archive.cpp:99
llvm::object::Archive::symbol_end
symbol_iterator symbol_end() const
Definition: Archive.cpp:961
s
multiplies can be turned into SHL s
Definition: README.txt:370
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::object::ArchiveMemberHeader::getUID
Expected< unsigned > getUID() const
Definition: Archive.cpp:293
llvm::object::ArchiveMemberHeader::Archive
friend class Archive
Definition: Archive.h:39
llvm::object::Archive::Child::getAccessMode
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.h:123
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::Archive::hasSymbolTable
bool hasSymbolTable() const
Definition: Archive.cpp:1002
llvm::object::Archive::ChildFallibleIterator::operator==
bool operator==(const ChildFallibleIterator &other) const
Definition: Archive.h:152
iterator_range.h
llvm::object::Archive::child_begin
child_iterator child_begin(Error &Err, bool SkipInternal=true) const
Definition: Archive.cpp:782
llvm::fallible_iterator
A wrapper class for fallible iterators.
Definition: fallible_iterator.h:68
llvm::object::Archive::Child::getRawSize
Expected< uint64_t > getRawSize() const
Definition: Archive.cpp:412
llvm::object::Archive::Symbol::getMember
Expected< Child > getMember() const
Definition: Archive.cpp:806
llvm::object::Archive::findSym
Expected< Optional< Child > > findSym(StringRef name) const
Definition: Archive.cpp:983
llvm::object::Archive::Child::getRawLastModified
StringRef getRawLastModified() const
Definition: Archive.h:118
llvm::object::Archive::symbol_iterator::operator++
symbol_iterator & operator++()
Definition: Archive.h:209
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary
Definition: Binary.h:32
uint32_t
llvm::object::Archive::Child::getName
Expected< StringRef > getName() const
Definition: Archive.cpp:503
llvm::object::Archive::ChildFallibleIterator::operator*
const Child & operator*() const
Definition: Archive.h:150
llvm::object::Archive::Child
Definition: Archive.h:85
llvm::object::Archive::Symbol::operator==
bool operator==(const Symbol &other) const
Definition: Archive.h:183
llvm::object::Archive::Symbol::getNext
Symbol getNext() const
Definition: Archive.cpp:866
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:78
llvm::object::Archive::Child::getChildOffset
uint64_t getChildOffset() const
Definition: Archive.cpp:496
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
uint16_t
llvm::object::Archive::symbol_iterator::operator->
const Symbol * operator->() const
Definition: Archive.h:198
llvm::object::ArchiveMemberHeader::getAccessMode
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.cpp:249
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::object::Archive::kind
Kind kind() const
Definition: Archive.h:223
llvm::object::Archive::K_DARWIN
@ K_DARWIN
Definition: Archive.h:221
llvm::object::Archive::Child::getSize
Expected< uint64_t > getSize() const
Definition: Archive.cpp:406
Binary.h
llvm::object::ArchiveMemberHeader::getSize
Expected< uint64_t > getSize() const
Definition: Archive.cpp:227
llvm::object::ArchiveMemberHeader
Definition: Archive.h:37
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::object::Archive::child_iterator
fallible_iterator< ChildFallibleIterator > child_iterator
Definition: Archive.h:172
llvm::object::Archive::Child::getMemoryBufferRef
Expected< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:515
llvm::object::Archive::Child::getParent
const Archive * getParent() const
Definition: Archive.h:107
llvm::object::Archive::symbols
iterator_range< symbol_iterator > symbols() const
Definition: Archive.h:235
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
llvm::object::Archive::create
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:538
llvm::MCID::Terminator
@ Terminator
Definition: MCInstrDesc.h:155
llvm::sys::fs::AccessMode
AccessMode
Definition: FileSystem.h:450
llvm::object::Archive::ChildFallibleIterator::operator!=
bool operator!=(const ChildFallibleIterator &other) const
Definition: Archive.h:159